Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

array-buffer-transfer.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var globalThis = require('../internals/global-this');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var uncurryThisAccessor = require('../internals/function-uncurry-this-accessor');
  5. var toIndex = require('../internals/to-index');
  6. var notDetached = require('../internals/array-buffer-not-detached');
  7. var arrayBufferByteLength = require('../internals/array-buffer-byte-length');
  8. var detachTransferable = require('../internals/detach-transferable');
  9. var PROPER_STRUCTURED_CLONE_TRANSFER = require('../internals/structured-clone-proper-transfer');
  10. var structuredClone = globalThis.structuredClone;
  11. var ArrayBuffer = globalThis.ArrayBuffer;
  12. var DataView = globalThis.DataView;
  13. var min = Math.min;
  14. var ArrayBufferPrototype = ArrayBuffer.prototype;
  15. var DataViewPrototype = DataView.prototype;
  16. var slice = uncurryThis(ArrayBufferPrototype.slice);
  17. var isResizable = uncurryThisAccessor(ArrayBufferPrototype, 'resizable', 'get');
  18. var maxByteLength = uncurryThisAccessor(ArrayBufferPrototype, 'maxByteLength', 'get');
  19. var getInt8 = uncurryThis(DataViewPrototype.getInt8);
  20. var setInt8 = uncurryThis(DataViewPrototype.setInt8);
  21. module.exports = (PROPER_STRUCTURED_CLONE_TRANSFER || detachTransferable) && function (arrayBuffer, newLength, preserveResizability) {
  22. var byteLength = arrayBufferByteLength(arrayBuffer);
  23. var newByteLength = newLength === undefined ? byteLength : toIndex(newLength);
  24. var fixedLength = !isResizable || !isResizable(arrayBuffer);
  25. var newBuffer;
  26. notDetached(arrayBuffer);
  27. if (PROPER_STRUCTURED_CLONE_TRANSFER) {
  28. arrayBuffer = structuredClone(arrayBuffer, { transfer: [arrayBuffer] });
  29. if (byteLength === newByteLength && (preserveResizability || fixedLength)) return arrayBuffer;
  30. }
  31. if (byteLength >= newByteLength && (!preserveResizability || fixedLength)) {
  32. newBuffer = slice(arrayBuffer, 0, newByteLength);
  33. } else {
  34. var options = preserveResizability && !fixedLength && maxByteLength ? { maxByteLength: maxByteLength(arrayBuffer) } : undefined;
  35. newBuffer = new ArrayBuffer(newByteLength, options);
  36. var a = new DataView(arrayBuffer);
  37. var b = new DataView(newBuffer);
  38. var copyLength = min(newByteLength, byteLength);
  39. for (var i = 0; i < copyLength; i++) setInt8(b, i, getInt8(a, i));
  40. }
  41. if (!PROPER_STRUCTURED_CLONE_TRANSFER) detachTransferable(arrayBuffer);
  42. return newBuffer;
  43. };