DetachArrayBuffer.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. var $SyntaxError = require('es-errors/syntax');
  3. var $TypeError = require('es-errors/type');
  4. var isArrayBuffer = require('is-array-buffer');
  5. var MessageChannel;
  6. try {
  7. // eslint-disable-next-line global-require
  8. MessageChannel = require('worker_threads').MessageChannel; // node 11.7+
  9. } catch (e) { /**/ }
  10. // https://262.ecma-international.org/6.0/#sec-detacharraybuffer
  11. /* globals postMessage */
  12. module.exports = function DetachArrayBuffer(arrayBuffer) {
  13. if (!isArrayBuffer(arrayBuffer)) {
  14. throw new $TypeError('Assertion failed: `arrayBuffer` must be an Object with an [[ArrayBufferData]] internal slot');
  15. }
  16. if (typeof structuredClone === 'function') {
  17. structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
  18. } else if (typeof postMessage === 'function') {
  19. postMessage('', '/', [arrayBuffer]); // TODO: see if this might trigger listeners
  20. } else if (MessageChannel) {
  21. (new MessageChannel()).port1.postMessage(null, [arrayBuffer]);
  22. } else {
  23. throw new $SyntaxError('DetachArrayBuffer is not supported in this environment');
  24. }
  25. return null;
  26. };