From 0e2954fd31794aa3b56f4281eec6e1c8ff3bf13a Mon Sep 17 00:00:00 2001 From: Markus Wolf Date: Fri, 7 Sep 2018 15:59:59 +0200 Subject: [PATCH 1/2] 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 #21136 --- src/compiler/sys.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index eaa910a0b5c14..455a00279e24f 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -582,6 +582,7 @@ namespace ts { const Buffer: { new (input: string, encoding?: string): any; from?(input: string, encoding?: string): any; + alloc(length: number): any; } = require("buffer").Buffer; const nodeVersion = getNodeMajorVersion(); @@ -984,6 +985,30 @@ namespace ts { if (!fileExists(fileName)) { return undefined; } + + { + // GH#21136: check if this is an MPEG-TS file + const buffer = Buffer.alloc(50 * 188); + const fd = _fs.openSync(fileName); + try { + const bytesRead = _fs.readSync(fd, buffer, 0, 50 * 188); + // check if we have a multiple of 188 bytes (frames) + if (bytesRead % 188 === 0) { + let allHex47 = true; + for (let i = 0, n = i * 188; i < n; i = i + 188) { + if (i < bytesRead && buffer[i] !== 0x47) { + allHex47 = false; + } + } + if (allHex47) { + return undefined; + } + } + } finally { + _fs.closeSync(fd); + } + } + const buffer = _fs.readFileSync(fileName); let len = buffer.length; if (len >= 2 && buffer[0] === 0xFE && buffer[1] === 0xFF) { From 5ab946564e8eb397157fd064d30f6c165c978ade Mon Sep 17 00:00:00 2001 From: Markus Wolf Date: Fri, 7 Sep 2018 16:46:04 +0200 Subject: [PATCH 2/2] fix: update implementation to correct tests --- src/compiler/sys.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 455a00279e24f..0a4d31a672afe 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -989,13 +989,13 @@ namespace ts { { // GH#21136: check if this is an MPEG-TS file const buffer = Buffer.alloc(50 * 188); - const fd = _fs.openSync(fileName); + const fd = _fs.openSync(fileName, "r"); try { const bytesRead = _fs.readSync(fd, buffer, 0, 50 * 188); // check if we have a multiple of 188 bytes (frames) - if (bytesRead % 188 === 0) { + if (bytesRead > 0 && bytesRead % 188 === 0) { let allHex47 = true; - for (let i = 0, n = i * 188; i < n; i = i + 188) { + for (let i = 0, n = 50 * 188; i < n; i = i + 188) { if (i < bytesRead && buffer[i] !== 0x47) { allHex47 = false; }