Skip to content

Commit

Permalink
fix: Handle failed arraybuffer instanceof checks (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Jun 2, 2021
1 parent cbca602 commit 247be0a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
21 changes: 19 additions & 2 deletions packages/@pollyjs/adapter-fetch/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Adapter from '@pollyjs/adapter';
import { isBufferUtf8Representable } from '@pollyjs/utils';
import { cloneArrayBuffer, isBufferUtf8Representable } from '@pollyjs/utils';
import isNode from 'detect-node';
import { Buffer } from 'buffer/';
import bufferToArrayBuffer from 'to-arraybuffer';
Expand Down Expand Up @@ -167,7 +167,24 @@ export default class FetchAdapter extends Adapter {
}
]);

const buffer = Buffer.from(await response.arrayBuffer());
let arrayBuffer = await response.arrayBuffer();

/*
If the returned array buffer is not an instance of the global ArrayBuffer,
clone it in order to pass Buffer.from's instanceof check. This can happen
when using this adapter with a different context.
https://github.com/feross/buffer/issues/289
*/
if (
arrayBuffer &&
!(arrayBuffer instanceof ArrayBuffer) &&
'byteLength' in arrayBuffer
) {
arrayBuffer = cloneArrayBuffer(arrayBuffer);
}

const buffer = Buffer.from(arrayBuffer);
const isBinaryBuffer = !isBufferUtf8Representable(buffer);

return {
Expand Down
21 changes: 19 additions & 2 deletions packages/@pollyjs/adapter-xhr/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fakeXhr from '@offirgolan/nise/lib/fake-xhr';
import Adapter from '@pollyjs/adapter';
import { isBufferUtf8Representable } from '@pollyjs/utils';
import { cloneArrayBuffer, isBufferUtf8Representable } from '@pollyjs/utils';
import { Buffer } from 'buffer/';
import bufferToArrayBuffer from 'to-arraybuffer';

Expand Down Expand Up @@ -112,7 +112,24 @@ export default class XHRAdapter extends Adapter {

// responseType will either be `arraybuffer` or `text`
if (xhr.responseType === 'arraybuffer') {
const buffer = Buffer.from(xhr.response);
let arrayBuffer = xhr.response;

/*
If the returned array buffer is not an instance of the global ArrayBuffer,
clone it in order to pass Buffer.from's instanceof check. This can happen
when using this adapter with a different context.
https://github.com/feross/buffer/issues/289
*/
if (
arrayBuffer &&
!(arrayBuffer instanceof ArrayBuffer) &&
'byteLength' in arrayBuffer
) {
arrayBuffer = cloneArrayBuffer(arrayBuffer);
}

const buffer = Buffer.from(arrayBuffer);

isBinary = !isBufferUtf8Representable(buffer);
body = buffer.toString(isBinary ? 'hex' : 'utf8');
Expand Down
1 change: 1 addition & 0 deletions packages/@pollyjs/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export { default as URL } from './utils/url';
export {
default as isBufferUtf8Representable
} from './utils/is-buffer-utf8-representable';
export { default as cloneArrayBuffer } from './utils/clone-arraybuffer';
12 changes: 12 additions & 0 deletions packages/@pollyjs/utils/src/utils/clone-arraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Clone an array buffer
*
* @param {ArrayBuffer} arrayBuffer
*/
export default function cloneArrayBuffer(arrayBuffer) {
const clonedArrayBuffer = new ArrayBuffer(arrayBuffer.byteLength);

new Uint8Array(clonedArrayBuffer).set(new Uint8Array(arrayBuffer));

return clonedArrayBuffer;
}

0 comments on commit 247be0a

Please sign in to comment.