[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $SyntaxError = require('es-errors/syntax');
|
---|
| 4 | var $TypeError = require('es-errors/type');
|
---|
| 5 |
|
---|
| 6 | var IsDetachedBuffer = require('./IsDetachedBuffer');
|
---|
| 7 |
|
---|
| 8 | var isArrayBuffer = require('is-array-buffer');
|
---|
| 9 | var isSharedArrayBuffer = require('is-shared-array-buffer');
|
---|
| 10 |
|
---|
| 11 | var MessageChannel;
|
---|
| 12 | try {
|
---|
| 13 | // eslint-disable-next-line global-require
|
---|
| 14 | MessageChannel = require('worker_threads').MessageChannel;
|
---|
| 15 | } catch (e) { /**/ }
|
---|
| 16 |
|
---|
| 17 | // https://262.ecma-international.org/9.0/#sec-detacharraybuffer
|
---|
| 18 |
|
---|
| 19 | /* globals postMessage */
|
---|
| 20 |
|
---|
| 21 | module.exports = function DetachArrayBuffer(arrayBuffer) {
|
---|
| 22 | if (!isArrayBuffer(arrayBuffer) || isSharedArrayBuffer(arrayBuffer)) {
|
---|
| 23 | throw new $TypeError('Assertion failed: `arrayBuffer` must be an Object with an [[ArrayBufferData]] internal slot, and not a Shared Array Buffer');
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | // commented out since there's no way to set or access this key
|
---|
| 27 | // var key = arguments.length > 1 ? arguments[1] : void undefined;
|
---|
| 28 |
|
---|
| 29 | // if (!SameValue(arrayBuffer[[ArrayBufferDetachKey]], key)) {
|
---|
| 30 | // throw new $TypeError('Assertion failed: `key` must be the value of the [[ArrayBufferDetachKey]] internal slot of `arrayBuffer`');
|
---|
| 31 | // }
|
---|
| 32 |
|
---|
| 33 | if (!IsDetachedBuffer(arrayBuffer)) { // node v21.0.0+ throws when you structuredClone a detached buffer
|
---|
| 34 | if (typeof structuredClone === 'function') {
|
---|
| 35 | structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
|
---|
| 36 | } else if (typeof postMessage === 'function') {
|
---|
| 37 | postMessage('', '/', [arrayBuffer]); // TODO: see if this might trigger listeners
|
---|
| 38 | } else if (MessageChannel) {
|
---|
| 39 | (new MessageChannel()).port1.postMessage(null, [arrayBuffer]);
|
---|
| 40 | } else {
|
---|
| 41 | throw new $SyntaxError('DetachArrayBuffer is not supported in this environment');
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | return null;
|
---|
| 46 | };
|
---|