Blob.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*! Based on fetch-blob. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> & David Frank */
  2. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  5. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  6. };
  7. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  8. if (kind === "m") throw new TypeError("Private method is not writable");
  9. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  10. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  11. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  12. };
  13. var _Blob_parts, _Blob_type, _Blob_size;
  14. import { ReadableStream } from "web-streams-polyfill";
  15. import { isFunction } from "./isFunction.js";
  16. import { consumeBlobParts, sliceBlob } from "./blobHelpers.js";
  17. export class Blob {
  18. constructor(blobParts = [], options = {}) {
  19. _Blob_parts.set(this, []);
  20. _Blob_type.set(this, "");
  21. _Blob_size.set(this, 0);
  22. options !== null && options !== void 0 ? options : (options = {});
  23. if (typeof blobParts !== "object" || blobParts === null) {
  24. throw new TypeError("Failed to construct 'Blob': "
  25. + "The provided value cannot be converted to a sequence.");
  26. }
  27. if (!isFunction(blobParts[Symbol.iterator])) {
  28. throw new TypeError("Failed to construct 'Blob': "
  29. + "The object must have a callable @@iterator property.");
  30. }
  31. if (typeof options !== "object" && !isFunction(options)) {
  32. throw new TypeError("Failed to construct 'Blob': parameter 2 cannot convert to dictionary.");
  33. }
  34. const encoder = new TextEncoder();
  35. for (const raw of blobParts) {
  36. let part;
  37. if (ArrayBuffer.isView(raw)) {
  38. part = new Uint8Array(raw.buffer.slice(raw.byteOffset, raw.byteOffset + raw.byteLength));
  39. }
  40. else if (raw instanceof ArrayBuffer) {
  41. part = new Uint8Array(raw.slice(0));
  42. }
  43. else if (raw instanceof Blob) {
  44. part = raw;
  45. }
  46. else {
  47. part = encoder.encode(String(raw));
  48. }
  49. __classPrivateFieldSet(this, _Blob_size, __classPrivateFieldGet(this, _Blob_size, "f") + (ArrayBuffer.isView(part) ? part.byteLength : part.size), "f");
  50. __classPrivateFieldGet(this, _Blob_parts, "f").push(part);
  51. }
  52. const type = options.type === undefined ? "" : String(options.type);
  53. __classPrivateFieldSet(this, _Blob_type, /^[\x20-\x7E]*$/.test(type) ? type : "", "f");
  54. }
  55. static [(_Blob_parts = new WeakMap(), _Blob_type = new WeakMap(), _Blob_size = new WeakMap(), Symbol.hasInstance)](value) {
  56. return Boolean(value
  57. && typeof value === "object"
  58. && isFunction(value.constructor)
  59. && (isFunction(value.stream)
  60. || isFunction(value.arrayBuffer))
  61. && /^(Blob|File)$/.test(value[Symbol.toStringTag]));
  62. }
  63. get type() {
  64. return __classPrivateFieldGet(this, _Blob_type, "f");
  65. }
  66. get size() {
  67. return __classPrivateFieldGet(this, _Blob_size, "f");
  68. }
  69. slice(start, end, contentType) {
  70. return new Blob(sliceBlob(__classPrivateFieldGet(this, _Blob_parts, "f"), this.size, start, end), {
  71. type: contentType
  72. });
  73. }
  74. async text() {
  75. const decoder = new TextDecoder();
  76. let result = "";
  77. for await (const chunk of consumeBlobParts(__classPrivateFieldGet(this, _Blob_parts, "f"))) {
  78. result += decoder.decode(chunk, { stream: true });
  79. }
  80. result += decoder.decode();
  81. return result;
  82. }
  83. async arrayBuffer() {
  84. const view = new Uint8Array(this.size);
  85. let offset = 0;
  86. for await (const chunk of consumeBlobParts(__classPrivateFieldGet(this, _Blob_parts, "f"))) {
  87. view.set(chunk, offset);
  88. offset += chunk.length;
  89. }
  90. return view.buffer;
  91. }
  92. stream() {
  93. const iterator = consumeBlobParts(__classPrivateFieldGet(this, _Blob_parts, "f"), true);
  94. return new ReadableStream({
  95. async pull(controller) {
  96. const { value, done } = await iterator.next();
  97. if (done) {
  98. return queueMicrotask(() => controller.close());
  99. }
  100. controller.enqueue(value);
  101. },
  102. async cancel() {
  103. await iterator.return();
  104. }
  105. });
  106. }
  107. get [Symbol.toStringTag]() {
  108. return "Blob";
  109. }
  110. }
  111. Object.defineProperties(Blob.prototype, {
  112. type: { enumerable: true },
  113. size: { enumerable: true },
  114. slice: { enumerable: true },
  115. stream: { enumerable: true },
  116. text: { enumerable: true },
  117. arrayBuffer: { enumerable: true }
  118. });