提交 f3e1c7db 编写于 作者: A Alex Dima

Fixes #71993: Ensure Buffer is always used when available

上级 54e2124f
......@@ -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;
......@@ -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);
}
......
/*---------------------------------------------------------------------------------------------
* 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');
});
}
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册