From bcfa52bd9a8ca6b10e452fbd60eade7167bd0f6b Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 4 Aug 2017 15:27:08 -0700 Subject: [PATCH] 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. --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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) -- 2.34.1