Skip to content

Commit 0e2954f

Browse files
committed
fix: do not parse mpeg-ts files as typescript files
MPEG-TS files consist of frames each 188 bytes long. These files should not be parsed as typescript files and therefore we look into the first 50 frames and check if we find a 'valid' header. Closes microsoft#21136
1 parent cf2f339 commit 0e2954f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/compiler/sys.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ namespace ts {
582582
const Buffer: {
583583
new (input: string, encoding?: string): any;
584584
from?(input: string, encoding?: string): any;
585+
alloc(length: number): any;
585586
} = require("buffer").Buffer;
586587

587588
const nodeVersion = getNodeMajorVersion();
@@ -984,6 +985,30 @@ namespace ts {
984985
if (!fileExists(fileName)) {
985986
return undefined;
986987
}
988+
989+
{
990+
// GH#21136: check if this is an MPEG-TS file
991+
const buffer = Buffer.alloc(50 * 188);
992+
const fd = _fs.openSync(fileName);
993+
try {
994+
const bytesRead = _fs.readSync(fd, buffer, 0, 50 * 188);
995+
// check if we have a multiple of 188 bytes (frames)
996+
if (bytesRead % 188 === 0) {
997+
let allHex47 = true;
998+
for (let i = 0, n = i * 188; i < n; i = i + 188) {
999+
if (i < bytesRead && buffer[i] !== 0x47) {
1000+
allHex47 = false;
1001+
}
1002+
}
1003+
if (allHex47) {
1004+
return undefined;
1005+
}
1006+
}
1007+
} finally {
1008+
_fs.closeSync(fd);
1009+
}
1010+
}
1011+
9871012
const buffer = _fs.readFileSync(fileName);
9881013
let len = buffer.length;
9891014
if (len >= 2 && buffer[0] === 0xFE && buffer[1] === 0xFF) {

0 commit comments

Comments
 (0)