Skip to content

Commit

Permalink
Fixes #71993: Ensure Buffer is always used when available
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Apr 9, 2019
1 parent 54e2124 commit f3e1c7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/vs/base/common/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

declare var Buffer: any;
const hasBuffer = (typeof Buffer !== 'undefined');
export const hasBuffer = (typeof Buffer !== 'undefined');

let textEncoder: TextEncoder | null;
let textDecoder: TextDecoder | null;
Expand All @@ -20,6 +20,11 @@ export class VSBuffer {
}

public static wrap(actual: Uint8Array): VSBuffer {
if (hasBuffer && !(Buffer.isBuffer(actual))) {
// https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
// Create a zero-copy Buffer wrapper around the ArrayBuffer pointed to by the Uint8Array
actual = Buffer.from(actual.buffer, actual.byteOffset, actual.byteLength);
}
return new VSBuffer(actual);
}

Expand Down
20 changes: 20 additions & 0 deletions src/vs/base/test/common/buffer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/


import * as assert from 'assert';
import { hasBuffer, VSBuffer } from 'vs/base/common/buffer';

suite('Buffer', () => {

if (hasBuffer) {
test('issue #71993 - VSBuffer#toString returns numbers', () => {
const data = new Uint8Array([1, 2, 3, 'h'.charCodeAt(0), 'i'.charCodeAt(0), 4, 5]).buffer;
const buffer = VSBuffer.wrap(new Uint8Array(data, 3, 2));
assert.deepEqual(buffer.toString(), 'hi');
});
}

});

0 comments on commit f3e1c7d

Please sign in to comment.