main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
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; // node 11.7+
|
---|
15 | } catch (e) { /**/ }
|
---|
16 |
|
---|
17 | // https://262.ecma-international.org/8.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 | if (!IsDetachedBuffer(arrayBuffer)) { // node v21.0.0+ throws when you structuredClone a detached buffer
|
---|
27 | if (typeof structuredClone === 'function') {
|
---|
28 | structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
|
---|
29 | } else if (typeof postMessage === 'function') {
|
---|
30 | postMessage('', '/', [arrayBuffer]); // TODO: see if this might trigger listeners
|
---|
31 | } else if (MessageChannel) {
|
---|
32 | (new MessageChannel()).port1.postMessage(null, [arrayBuffer]);
|
---|
33 | } else {
|
---|
34 | throw new $SyntaxError('DetachArrayBuffer is not supported in this environment');
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | return null;
|
---|
39 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.