From: Feross Aboukhadijeh Date: Mon, 20 Jan 2014 08:15:17 +0000 (-0800) Subject: Add buf.toArrayBuffer on Firefox (fix #17) X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=01a869da90c72f7ecff2c9774739322de789de2f;p=buffer.git Add buf.toArrayBuffer on Firefox (fix #17) --- diff --git a/index.js b/index.js index afb970a..8159a3b 100644 --- a/index.js +++ b/index.js @@ -834,11 +834,21 @@ Buffer.prototype.inspect = function () { /** * Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance. - * Added in Node 0.12. Not added to Buffer.prototype since it should only - * be available in browsers that support ArrayBuffer. + * Added in Node 0.12. Only available in browsers that support ArrayBuffer. */ -function BufferToArrayBuffer () { - return (new Buffer(this)).buffer +Buffer.prototype.toArrayBuffer = function () { + if (typeof Uint8Array === 'function') { + if (Buffer._useTypedArrays) { + return (new Buffer(this)).buffer + } else { + var buf = new Uint8Array(this.length) + for (var i = 0, len = buf.length; i < len; i += 1) + buf[i] = this[i] + return buf.buffer + } + } else { + throw new Error('Buffer.toArrayBuffer not supported in this browser') + } } // HELPER FUNCTIONS @@ -901,7 +911,7 @@ function augment (arr) { arr.writeDoubleBE = BP.writeDoubleBE arr.fill = BP.fill arr.inspect = BP.inspect - arr.toArrayBuffer = BufferToArrayBuffer + arr.toArrayBuffer = BP.toArrayBuffer return arr }