From 461ccc9c2329f7c9132593e9de98b93861229c8f Mon Sep 17 00:00:00 2001 From: Kelvin Schoofs Date: Mon, 25 Feb 2019 10:38:56 +0100 Subject: [PATCH] Fix node DEP0005 (Buffer() being deprecated) --- src/manager.ts | 6 +++--- src/sshFileSystem.ts | 1 - src/streams.ts | 8 ++++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/manager.ts b/src/manager.ts index 3777e01..533b10c 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -69,7 +69,7 @@ function createConfigFs(manager: Manager): SSHFileSystem { config = config || manager.getActive().find(c => c.name === name); activeButDeleted = true; } - let str; + let str: string; if (config) { str = JSON.stringify({ ...config, name: undefined }, undefined, 4); let prefix = `// If you haven't already, associate .jsonc files with "JSON with Comments (jsonc)\n`; @@ -78,12 +78,12 @@ function createConfigFs(manager: Manager): SSHFileSystem { } else { str = await toPromise(cb => readFile(path.resolve(__dirname, '../resources/defaultConfig.jsonc'), 'utf-8', cb)); } - return new Uint8Array(new Buffer(str)); + return new Uint8Array(Buffer.from(str)); }, writeFile: async (uri: vscode.Uri, content: Uint8Array) => { const name = uri.path.substring(1, uri.path.length - 12); const errors: ParseError[] = []; - const config = parseJsonc(new Buffer(content).toString(), errors); + const config = parseJsonc(Buffer.from(content).toString(), errors); if (!config || errors.length) { vscode.window.showErrorMessage(`Couldn't parse this config as JSON`); return; diff --git a/src/sshFileSystem.ts b/src/sshFileSystem.ts index 7519861..e6358d1 100644 --- a/src/sshFileSystem.ts +++ b/src/sshFileSystem.ts @@ -94,7 +94,6 @@ export class SSHFileSystem implements vscode.FileSystemProvider { } public readFile(uri: vscode.Uri): Uint8Array | Promise { return new Promise((resolve, reject) => { - const array = new Buffer(0); const stream = this.sftp.createReadStream(this.relative(uri.path), { autoClose: true }); const bufs = []; stream.on('data', bufs.push.bind(bufs)); diff --git a/src/streams.ts b/src/streams.ts index e620dfc..15cc104 100644 --- a/src/streams.ts +++ b/src/streams.ts @@ -2,7 +2,7 @@ import { Duplex, Writable } from 'stream'; export class MemoryDuplex extends Duplex { - protected buffer = new Buffer(this.size); + protected buffer = Buffer.alloc(this.size); protected buffered = 0; protected doPush = false; constructor(public readonly size: number = 4092) { @@ -10,7 +10,7 @@ export class MemoryDuplex extends Duplex { } // tslint:disable-next-line:function-name public _write(chunk: any, encoding: string, callback: (err?: Error) => void) { - const buffer = chunk instanceof Buffer ? chunk : new Buffer(chunk, encoding); + const buffer = chunk instanceof Buffer ? chunk : Buffer.from(chunk, encoding); const end = this.buffered + buffer.length; if (end > this.size) { return callback(new Error('Buffer overflow')); @@ -23,7 +23,7 @@ export class MemoryDuplex extends Duplex { public _read() { const slice = this.buffer.slice(0, this.buffered); this.buffered = 0; - this.buffer = new Buffer(this.size); + this.buffer = Buffer.alloc(this.size); this.doPush = this.push(slice); } public bytesBuffered() { @@ -37,7 +37,7 @@ export class WritableFunctionStream extends Writable { } // tslint:disable-next-line:function-name public async _write(chunk: any, encoding: string, callback: (err?: Error) => void) { - const buffer = chunk instanceof Buffer ? chunk : new Buffer(chunk, encoding); + const buffer = chunk instanceof Buffer ? chunk : Buffer.from(chunk, encoding); try { await this.func(buffer); callback();