Skip to content

Fix Buffer.from uses to handle node 5.4.1 bug #25659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ namespace ts {
/*@internal*/ setBlocking?(): void;
base64decode?(input: string): string;
base64encode?(input: string): string;
/*@internal*/ bufferFrom?(input: string, encoding?: string): Buffer;
}

export interface FileWatcher {
Expand Down Expand Up @@ -558,7 +559,7 @@ namespace ts {
getEnvironmentVariable?(name: string): string;
};

// TODO: this is used as if it's certainly defined in many places.
// TODO: GH#18217 this is used as if it's certainly defined in many places.
export let sys: System = (() => {
// NodeJS detects "\uFEFF" at the start of the string and *replaces* it with the actual
// byte order mark from the specified encoding. Using any other byte order mark does
Expand Down Expand Up @@ -675,19 +676,19 @@ namespace ts {
process.stdout._handle.setBlocking(true);
}
},
base64decode: Buffer.from ? input => {
return Buffer.from!(input, "base64").toString("utf8");
} : input => {
return new Buffer(input, "base64").toString("utf8");
},
base64encode: Buffer.from ? input => {
return Buffer.from!(input).toString("base64");
} : input => {
return new Buffer(input).toString("base64");
}
bufferFrom,
base64decode: input => bufferFrom(input, "base64").toString("utf8"),
base64encode: input => bufferFrom(input).toString("base64"),
};
return nodeSystem;

function bufferFrom(input: string, encoding?: string): Buffer {
// See https://github.com/Microsoft/TypeScript/issues/25652
return Buffer.from && (Buffer.from as Function) !== Int8Array.from
? Buffer.from(input, encoding)
: new Buffer(input, encoding);
}

function isFileSystemCaseSensitive(): boolean {
// win32\win64 are case insensitive platforms
if (platform === "win32" || platform === "win64") {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace documents {

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

public static fromSource(text: string): SourceMap | undefined {
Expand Down
5 changes: 1 addition & 4 deletions src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ namespace Utils {

export let currentExecutionEnvironment = getExecutionEnvironment();

// Thanks to browserify, Buffer is always available nowadays
const Buffer: typeof global.Buffer = require("buffer").Buffer;

export function encodeString(s: string): string {
return Buffer.from(s).toString("utf8");
return ts.sys.bufferFrom!(s).toString("utf8");
}

export function byteLength(s: string, encoding?: string): number {
Expand Down
4 changes: 2 additions & 2 deletions src/harness/vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ namespace vfs {

if (isDirectory(node)) throw createIOError("EISDIR");
if (!isFile(node)) throw createIOError("EBADF");
node.buffer = Buffer.isBuffer(data) ? data.slice() : Buffer.from("" + data, encoding || "utf8");
node.buffer = Buffer.isBuffer(data) ? data.slice() : ts.sys.bufferFrom!("" + data, encoding || "utf8");
node.size = node.buffer.byteLength;
node.mtimeMs = time;
node.ctimeMs = time;
Expand Down Expand Up @@ -1204,7 +1204,7 @@ namespace vfs {
}
},
readFileSync(path: string): Buffer {
return Buffer.from(host.readFile(path)!, "utf8"); // TODO: GH#18217
return ts.sys.bufferFrom!(host.readFile(path)!, "utf8"); // TODO: GH#18217
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/convertToBase64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace ts {
describe("convertToBase64", () => {
function runTest(input: string): void {
const actual = convertToBase64(input);
const expected = (Buffer.from ? Buffer.from(input) : new Buffer(input)).toString("base64");
const expected = sys.base64encode!(input);
assert.equal(actual, expected, "Encoded string using convertToBase64 does not match buffer.toString('base64')");
}

Expand Down
4 changes: 2 additions & 2 deletions src/tsserver/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ namespace ts.server {

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

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