4 * Copyright 2019 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var fs = require("fs");
21 var error_1 = require("../error");
22 /** Read a file, but return undefined if it is an MPEG '.ts' file. */
23 function tryReadFile(filename, logger) {
24 return tslib_1.__awaiter(this, void 0, void 0, function () {
26 return tslib_1.__generator(this, function (_a) {
27 if (!fs.existsSync(filename)) {
28 throw new error_1.FatalError("Unable to open file: " + filename);
30 buffer = Buffer.allocUnsafe(256);
31 fd = fs.openSync(filename, "r");
33 fs.readSync(fd, buffer, 0, 256, 0);
34 if (buffer.readInt8(0) === 0x47 && buffer.readInt8(188) === 0x47) {
35 // MPEG transport streams use the '.ts' file extension. They use 0x47 as the frame
36 // separator, repeating every 188 bytes. It is unlikely to find that pattern in
37 // TypeScript source, so tslint ignores files with the specific pattern.
38 logger.error(filename + ": ignoring MPEG transport stream\n");
39 return [2 /*return*/, undefined];
45 return [2 /*return*/, fs.readFileSync(filename, "utf8")];
49 exports.tryReadFile = tryReadFile;