DetachArrayBuffer.js 1.1 KB

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