[node] Allow SharedArrayBuffer in Buffer.from (#27933)

* Move from namespace to static attributes

* Allow SharedArrayBuffer in Buffer.from

* Address comments

* Add SharedArrayBuffer to byteLength

* Fix tests
This commit is contained in:
Thomas den Hollander
2018-08-15 08:25:20 -07:00
committed by Andy
parent 21487e4d9e
commit d42a3e4e16
2 changed files with 14 additions and 5 deletions
+9 -5
View File
@@ -26,6 +26,7 @@
// Lishude <https://github.com/islishude>
// Andrew Makarov <https://github.com/r3nya>
// Zane Hannan AU <https://github.com/ZaneHannanAU>
// Thomas den Hollander <https://github.com/ThomasdenH>
// Eugene Y. Q. Shen <https://github.com/eyqs>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -172,6 +173,10 @@ interface SymbolConstructor {
readonly asyncIterator: symbol;
}
declare var Symbol: SymbolConstructor;
interface SharedArrayBuffer {
readonly byteLength: number;
slice(begin?: number, end?: number): SharedArrayBuffer;
}
// Node.js ESNEXT support
interface String {
@@ -349,13 +354,13 @@ declare var Buffer: {
new(array: Uint8Array): Buffer;
/**
* Produces a Buffer backed by the same allocated memory as
* the given {ArrayBuffer}.
* the given {ArrayBuffer}/{SharedArrayBuffer}.
*
*
* @param arrayBuffer The ArrayBuffer with which to share memory.
* @deprecated since v10.0.0 - Use `Buffer.from(arrayBuffer[, byteOffset[, length]])` instead.
*/
new(arrayBuffer: ArrayBuffer): Buffer;
new(arrayBuffer: ArrayBuffer | SharedArrayBuffer): Buffer;
/**
* Allocates a new buffer containing the given {array} of octets.
*
@@ -379,8 +384,7 @@ declare var Buffer: {
*
* @param arrayBuffer The .buffer property of any TypedArray or a new ArrayBuffer()
*/
from(arrayBuffer: ArrayBuffer, byteOffset?: number, length?: number): Buffer;
// from(arrayBuffer: SharedArrayBuffer, byteOffset?: number, length?: number): Buffer;
from(arrayBuffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): Buffer;
/**
* Creates a new Buffer using the passed {data}
* @param data data to create a new Buffer
@@ -418,7 +422,7 @@ declare var Buffer: {
* @param string string to test.
* @param encoding encoding used to evaluate (defaults to 'utf8')
*/
byteLength(string: string | NodeJS.TypedArray | DataView | ArrayBuffer /*| SharedArrayBuffer */, encoding?: string): number;
byteLength(string: string | NodeJS.TypedArray | DataView | ArrayBuffer | SharedArrayBuffer, encoding?: string): number;
/**
* Returns a buffer which is the result of concatenating all the buffers in the list together.
*
+5
View File
@@ -446,6 +446,11 @@ function bufferTests() {
const arrUint8: Uint8Array = new Uint8Array(2);
const buf5: Buffer = Buffer.from(arrUint8);
const buf6: Buffer = Buffer.from(buf1);
const sharedArrayBuffer: SharedArrayBuffer = {
byteLength: 10,
slice: (begin?: number, end?: number) => sharedArrayBuffer
};
const buf7: Buffer = Buffer.from(sharedArrayBuffer);
}
// Class Method: Buffer.from(arrayBuffer[, byteOffset[, length]])