Skip to content

Commit e25a23b

Browse files
author
Andy
authored
Fix Buffer.from uses to handle node 5.4.1 bug (#25659)
1 parent 7ad4fcf commit e25a23b

File tree

6 files changed

+19
-21
lines changed

6 files changed

+19
-21
lines changed

src/compiler/sys.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ namespace ts {
503503
/*@internal*/ setBlocking?(): void;
504504
base64decode?(input: string): string;
505505
base64encode?(input: string): string;
506+
/*@internal*/ bufferFrom?(input: string, encoding?: string): Buffer;
506507
}
507508

508509
export interface FileWatcher {
@@ -558,7 +559,7 @@ namespace ts {
558559
getEnvironmentVariable?(name: string): string;
559560
};
560561

561-
// TODO: this is used as if it's certainly defined in many places.
562+
// TODO: GH#18217 this is used as if it's certainly defined in many places.
562563
export let sys: System = (() => {
563564
// NodeJS detects "\uFEFF" at the start of the string and *replaces* it with the actual
564565
// byte order mark from the specified encoding. Using any other byte order mark does
@@ -675,19 +676,19 @@ namespace ts {
675676
process.stdout._handle.setBlocking(true);
676677
}
677678
},
678-
base64decode: Buffer.from ? input => {
679-
return Buffer.from!(input, "base64").toString("utf8");
680-
} : input => {
681-
return new Buffer(input, "base64").toString("utf8");
682-
},
683-
base64encode: Buffer.from ? input => {
684-
return Buffer.from!(input).toString("base64");
685-
} : input => {
686-
return new Buffer(input).toString("base64");
687-
}
679+
bufferFrom,
680+
base64decode: input => bufferFrom(input, "base64").toString("utf8"),
681+
base64encode: input => bufferFrom(input).toString("base64"),
688682
};
689683
return nodeSystem;
690684

685+
function bufferFrom(input: string, encoding?: string): Buffer {
686+
// See https://github.com/Microsoft/TypeScript/issues/25652
687+
return Buffer.from && (Buffer.from as Function) !== Int8Array.from
688+
? Buffer.from(input, encoding)
689+
: new Buffer(input, encoding);
690+
}
691+
691692
function isFileSystemCaseSensitive(): boolean {
692693
// win32\win64 are case insensitive platforms
693694
if (platform === "win32" || platform === "win64") {

src/harness/documents.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ namespace documents {
148148

149149
public static fromUrl(url: string) {
150150
const match = SourceMap._dataURLRegExp.exec(url);
151-
return match ? new SourceMap(/*mapFile*/ undefined, (Buffer.from ? Buffer.from(match[1], "base64") : new Buffer(match[1], "base64")).toString("utf8")) : undefined;
151+
return match ? new SourceMap(/*mapFile*/ undefined, ts.sys.base64decode!(match[1])) : undefined;
152152
}
153153

154154
public static fromSource(text: string): SourceMap | undefined {

src/harness/harness.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ namespace Utils {
6666

6767
export let currentExecutionEnvironment = getExecutionEnvironment();
6868

69-
// Thanks to browserify, Buffer is always available nowadays
70-
const Buffer: typeof global.Buffer = require("buffer").Buffer;
71-
7269
export function encodeString(s: string): string {
73-
return Buffer.from(s).toString("utf8");
70+
return ts.sys.bufferFrom!(s).toString("utf8");
7471
}
7572

7673
export function byteLength(s: string, encoding?: string): number {

src/harness/vfs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ namespace vfs {
683683

684684
if (isDirectory(node)) throw createIOError("EISDIR");
685685
if (!isFile(node)) throw createIOError("EBADF");
686-
node.buffer = Buffer.isBuffer(data) ? data.slice() : Buffer.from("" + data, encoding || "utf8");
686+
node.buffer = Buffer.isBuffer(data) ? data.slice() : ts.sys.bufferFrom!("" + data, encoding || "utf8");
687687
node.size = node.buffer.byteLength;
688688
node.mtimeMs = time;
689689
node.ctimeMs = time;
@@ -1204,7 +1204,7 @@ namespace vfs {
12041204
}
12051205
},
12061206
readFileSync(path: string): Buffer {
1207-
return Buffer.from(host.readFile(path)!, "utf8"); // TODO: GH#18217
1207+
return ts.sys.bufferFrom!(host.readFile(path)!, "utf8"); // TODO: GH#18217
12081208
}
12091209
};
12101210
}

src/testRunner/unittests/convertToBase64.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace ts {
22
describe("convertToBase64", () => {
33
function runTest(input: string): void {
44
const actual = convertToBase64(input);
5-
const expected = (Buffer.from ? Buffer.from(input) : new Buffer(input)).toString("base64");
5+
const expected = sys.base64encode!(input);
66
assert.equal(actual, expected, "Encoded string using convertToBase64 does not match buffer.toString('base64')");
77
}
88

src/tsserver/server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ namespace ts.server {
199199

200200
private write(s: string) {
201201
if (this.fd >= 0) {
202-
const buf = Buffer.from ? Buffer.from(s) : new Buffer(s);
202+
const buf = sys.bufferFrom!(s);
203203
// tslint:disable-next-line no-null-keyword
204204
fs.writeSync(this.fd, buf, 0, buf.length, /*position*/ null!); // TODO: GH#18217
205205
}
@@ -869,7 +869,7 @@ namespace ts.server {
869869
}
870870

871871
// Override sys.write because fs.writeSync is not reliable on Node 4
872-
sys.write = (s: string) => writeMessage(Buffer.from ? Buffer.from(s, "utf8") : new Buffer(s, "utf8"));
872+
sys.write = (s: string) => writeMessage(sys.bufferFrom!(s, "utf8"));
873873
sys.watchFile = (fileName, callback) => {
874874
const watchedFile = pollingWatchedFileSet.addFile(fileName, callback);
875875
return {

0 commit comments

Comments
 (0)