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.2 KB
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $SyntaxError = require('es-errors/syntax');
|
---|
| 4 | var $TypeError = require('es-errors/type');
|
---|
| 5 |
|
---|
| 6 | var isArrayBuffer = require('is-array-buffer');
|
---|
| 7 |
|
---|
| 8 | var IsDetachedBuffer = require('./IsDetachedBuffer');
|
---|
| 9 |
|
---|
| 10 | var MessageChannel;
|
---|
| 11 | try {
|
---|
| 12 | // eslint-disable-next-line global-require
|
---|
| 13 | MessageChannel = require('worker_threads').MessageChannel; // node 11.7+
|
---|
| 14 | } catch (e) { /**/ }
|
---|
| 15 |
|
---|
| 16 | // https://262.ecma-international.org/6.0/#sec-detacharraybuffer
|
---|
| 17 |
|
---|
| 18 | /* globals postMessage */
|
---|
| 19 |
|
---|
| 20 | module.exports = function DetachArrayBuffer(arrayBuffer) {
|
---|
| 21 | if (!isArrayBuffer(arrayBuffer)) {
|
---|
| 22 | throw new $TypeError('Assertion failed: `arrayBuffer` must be an Object with an [[ArrayBufferData]] internal slot');
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | if (!IsDetachedBuffer(arrayBuffer)) { // node v21.0.0+ throws when you structuredClone a detached buffer
|
---|
| 26 | if (typeof structuredClone === 'function') {
|
---|
| 27 | structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
|
---|
| 28 | } else if (typeof postMessage === 'function') {
|
---|
| 29 | postMessage('', '/', [arrayBuffer]); // TODO: see if this might trigger listeners
|
---|
| 30 | } else if (MessageChannel) {
|
---|
| 31 | (new MessageChannel()).port1.postMessage(null, [arrayBuffer]);
|
---|
| 32 | } else {
|
---|
| 33 | throw new $SyntaxError('DetachArrayBuffer is not supported in this environment');
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | return null;
|
---|
| 38 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.