Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle failed arraybuffer instanceof checks #393

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative here is to do something like arrayBuffer.__proto__ = ArrayBuffer.prototype but that feels very flakey

Copy link
Collaborator Author

@offirgolan offirgolan Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pro to just changing the proto is that we won't need to clone the array buffer at all

}

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;
}