From 03915663ef04a7bbae88524faecbcdbfef793568 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 5 Aug 2015 14:44:46 +0200 Subject: [PATCH] ArrayBuffer support in Buffer constructor --- index.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 10931c6..d72a180 100644 --- a/index.js +++ b/index.js @@ -130,8 +130,13 @@ function fromObject (that, object) { throw new TypeError('must start with number, buffer, array or string') } - if (typeof ArrayBuffer !== 'undefined' && object.buffer instanceof ArrayBuffer) { - return fromTypedArray(that, object) + if (typeof ArrayBuffer !== 'undefined') { + if (object.buffer instanceof ArrayBuffer) { + return fromTypedArray(that, object) + } + if (object instanceof ArrayBuffer) { + return fromArrayBuffer(that, object) + } } if (object.length) return fromArrayLike(that, object) @@ -168,6 +173,18 @@ function fromTypedArray (that, array) { return that } +function fromArrayBuffer (that, array) { + if (Buffer.TYPED_ARRAY_SUPPORT) { + // Return an augmented `Uint8Array` instance, for best performance + array.byteLength + that = Buffer._augment(new Uint8Array(array)) + } else { + // Fallback: Return an object instance of the Buffer class + that = fromTypedArray(that, new Uint8Array(array)) + } + return that +} + function fromArrayLike (that, array) { var length = checked(array.length) | 0 that = allocate(that, length) @@ -459,13 +476,13 @@ Buffer.prototype.indexOf = function indexOf (val, byteOffset) { throw new TypeError('val must be string, number or Buffer') } -// `get` will be removed in Node 0.13+ +// `get` is deprecated Buffer.prototype.get = function get (offset) { console.log('.get() is deprecated. Access using array indexes instead.') return this.readUInt8(offset) } -// `set` will be removed in Node 0.13+ +// `set` is deprecated Buffer.prototype.set = function set (v, offset) { console.log('.set() is deprecated. Access using array indexes instead.') return this.writeUInt8(v, offset) @@ -1232,7 +1249,7 @@ Buffer._augment = function _augment (arr) { // save reference to original Uint8Array set method before overwriting arr._set = arr.set - // deprecated, will be removed in node 0.13+ + // deprecated arr.get = BP.get arr.set = BP.set -- 2.34.1