Skip to content

Commit

Permalink
Relax check for webworker transferability (#8868)
Browse files Browse the repository at this point in the history
In some browser versions, objects can be transfered even though they don't pass the check for `instanceof ArrayBuffer`. We're going to use a more forgiving check that follows a duck-typing approach.
  • Loading branch information
kkaefer authored Oct 14, 2019
1 parent def961a commit e894e7d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/util/web_worker_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ for (const name in expressions) {
register(`Expression_${name}`, expressions[name]);
}

function isArrayBuffer(val: any): boolean {
return val && typeof ArrayBuffer !== 'undefined' &&
(val instanceof ArrayBuffer || (val.constructor && val.constructor.name === 'ArrayBuffer'));
}

/**
* Serialize the given object for transfer to or from a web worker.
*
Expand Down Expand Up @@ -128,9 +133,9 @@ export function serialize(input: mixed, transferables?: Array<Transferable>): Se
return input;
}

if (input instanceof ArrayBuffer) {
if (isArrayBuffer(input)) {
if (transferables) {
transferables.push(input);
transferables.push(((input: any): ArrayBuffer));
}
return input;
}
Expand Down Expand Up @@ -218,7 +223,7 @@ export function deserialize(input: Serialized): mixed {
input instanceof String ||
input instanceof Date ||
input instanceof RegExp ||
input instanceof ArrayBuffer ||
isArrayBuffer(input) ||
ArrayBuffer.isView(input) ||
input instanceof ImageData) {
return input;
Expand Down

0 comments on commit e894e7d

Please sign in to comment.