From: Feross Aboukhadijeh Date: Fri, 4 Aug 2017 22:27:08 +0000 (-0700) Subject: Treat ArrayBuffers from another context as valid X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=bcfa52bd9a8ca6b10e452fbd60eade7167bd0f6b;p=buffer.git Treat ArrayBuffers from another context as valid Fixes: https://github.com/feross/buffer/issues/166 ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check but they should be treated as valid. --- diff --git a/index.js b/index.js index b62b110..f696940 100644 --- a/index.js +++ b/index.js @@ -104,7 +104,7 @@ function from (value, encodingOrOffset, length) { throw new TypeError('"value" argument must not be a number') } - if (value instanceof ArrayBuffer) { + if (isArrayBuffer(value)) { return fromArrayBuffer(value, encodingOrOffset, length) } @@ -364,7 +364,7 @@ function byteLength (string, encoding) { if (Buffer.isBuffer(string)) { return string.length } - if (isArrayBufferView(string) || string instanceof ArrayBuffer) { + if (isArrayBufferView(string) || isArrayBuffer(string)) { return string.byteLength } if (typeof string !== 'string') { @@ -1696,6 +1696,14 @@ function blitBuffer (src, dst, offset, length) { return i } +// ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check +// but they should be treated as valid. See: https://github.com/feross/buffer/issues/166 +function isArrayBuffer (obj) { + return obj instanceof ArrayBuffer || + (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' && + typeof obj.byteLength === 'number') +} + // Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView` function isArrayBufferView (obj) { return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj)