blobHelpers.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. /*! Based on fetch-blob. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> & David Frank */
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.sliceBlob = exports.consumeBlobParts = void 0;
  5. const isFunction_1 = require("./isFunction");
  6. const CHUNK_SIZE = 65536;
  7. async function* clonePart(part) {
  8. const end = part.byteOffset + part.byteLength;
  9. let position = part.byteOffset;
  10. while (position !== end) {
  11. const size = Math.min(end - position, CHUNK_SIZE);
  12. const chunk = part.buffer.slice(position, position + size);
  13. position += chunk.byteLength;
  14. yield new Uint8Array(chunk);
  15. }
  16. }
  17. async function* consumeNodeBlob(blob) {
  18. let position = 0;
  19. while (position !== blob.size) {
  20. const chunk = blob.slice(position, Math.min(blob.size, position + CHUNK_SIZE));
  21. const buffer = await chunk.arrayBuffer();
  22. position += buffer.byteLength;
  23. yield new Uint8Array(buffer);
  24. }
  25. }
  26. async function* consumeBlobParts(parts, clone = false) {
  27. for (const part of parts) {
  28. if (ArrayBuffer.isView(part)) {
  29. if (clone) {
  30. yield* clonePart(part);
  31. }
  32. else {
  33. yield part;
  34. }
  35. }
  36. else if ((0, isFunction_1.isFunction)(part.stream)) {
  37. yield* part.stream();
  38. }
  39. else {
  40. yield* consumeNodeBlob(part);
  41. }
  42. }
  43. }
  44. exports.consumeBlobParts = consumeBlobParts;
  45. function* sliceBlob(blobParts, blobSize, start = 0, end) {
  46. end !== null && end !== void 0 ? end : (end = blobSize);
  47. let relativeStart = start < 0
  48. ? Math.max(blobSize + start, 0)
  49. : Math.min(start, blobSize);
  50. let relativeEnd = end < 0
  51. ? Math.max(blobSize + end, 0)
  52. : Math.min(end, blobSize);
  53. const span = Math.max(relativeEnd - relativeStart, 0);
  54. let added = 0;
  55. for (const part of blobParts) {
  56. if (added >= span) {
  57. break;
  58. }
  59. const partSize = ArrayBuffer.isView(part) ? part.byteLength : part.size;
  60. if (relativeStart && partSize <= relativeStart) {
  61. relativeStart -= partSize;
  62. relativeEnd -= partSize;
  63. }
  64. else {
  65. let chunk;
  66. if (ArrayBuffer.isView(part)) {
  67. chunk = part.subarray(relativeStart, Math.min(partSize, relativeEnd));
  68. added += chunk.byteLength;
  69. }
  70. else {
  71. chunk = part.slice(relativeStart, Math.min(partSize, relativeEnd));
  72. added += chunk.size;
  73. }
  74. relativeEnd -= partSize;
  75. relativeStart = 0;
  76. yield chunk;
  77. }
  78. }
  79. }
  80. exports.sliceBlob = sliceBlob;