source: imaps-frontend/node_modules/es-abstract/2022/DetachArrayBuffer.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.7 KB
Line 
1'use strict';
2
3var $SyntaxError = require('es-errors/syntax');
4var $TypeError = require('es-errors/type');
5
6var IsDetachedBuffer = require('./IsDetachedBuffer');
7
8var isArrayBuffer = require('is-array-buffer');
9var isSharedArrayBuffer = require('is-shared-array-buffer');
10
11var MessageChannel;
12try {
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
21module.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};
Note: See TracBrowser for help on using the repository browser.