|
| 1 | +// @ts-check |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const { TextEncoder, TextDecoder } = require('util') |
| 5 | + |
| 6 | +class Blob { |
| 7 | + /** |
| 8 | + * |
| 9 | + * @param {BlobPart[]} init |
| 10 | + * @param {Object} [options] |
| 11 | + * @param {string} [options.type] |
| 12 | + * |
| 13 | + */ |
| 14 | + constructor (init, options = {}) { |
| 15 | + /** @type {Uint8Array[]} */ |
| 16 | + const parts = [] |
| 17 | + |
| 18 | + let size = 0 |
| 19 | + for (const part of init) { |
| 20 | + if (typeof part === 'string') { |
| 21 | + const bytes = new TextEncoder().encode(part) |
| 22 | + parts.push(bytes) |
| 23 | + size += bytes.byteLength |
| 24 | + } else if (part instanceof Blob) { |
| 25 | + size += part.size |
| 26 | + // @ts-ignore - `_parts` is marked private so TS will complain about |
| 27 | + // accessing it. |
| 28 | + parts.push(...part._parts) |
| 29 | + } else if (part instanceof ArrayBuffer) { |
| 30 | + parts.push(new Uint8Array(part)) |
| 31 | + size += part.byteLength |
| 32 | + } else if (part instanceof Uint8Array) { |
| 33 | + parts.push(part) |
| 34 | + size += part.byteLength |
| 35 | + } else if (ArrayBuffer.isView(part)) { |
| 36 | + const { buffer, byteOffset, byteLength } = part |
| 37 | + parts.push(new Uint8Array(buffer, byteOffset, byteLength)) |
| 38 | + size += byteLength |
| 39 | + } else { |
| 40 | + throw new TypeError(`Can not convert ${part} value to a BlobPart`) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** @private */ |
| 45 | + this._size = size |
| 46 | + /** @private */ |
| 47 | + this._type = options.type || '' |
| 48 | + /** @private */ |
| 49 | + this._parts = parts |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * A string indicating the MIME type of the data contained in the Blob. |
| 54 | + * If the type is unknown, this string is empty. |
| 55 | + * @type {string} |
| 56 | + */ |
| 57 | + get type () { |
| 58 | + return this._type |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * The size, in bytes, of the data contained in the Blob object. |
| 63 | + * @type {number} |
| 64 | + */ |
| 65 | + get size () { |
| 66 | + return this._size |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns a new Blob object containing the data in the specified range of |
| 71 | + * bytes of the blob on which it's called. |
| 72 | + * @param {number} [start=0] - An index into the Blob indicating the first |
| 73 | + * byte to include in the new Blob. If you specify a negative value, it's |
| 74 | + * treated as an offset from the end of the Blob toward the beginning. For |
| 75 | + * example, `-10` would be the 10th from last byte in the Blob. The default |
| 76 | + * value is `0`. If you specify a value for start that is larger than the |
| 77 | + * size of the source Blob, the returned Blob has size 0 and contains no |
| 78 | + * data. |
| 79 | + * @param {number} [end] - An index into the `Blob` indicating the first byte |
| 80 | + * that will *not* be included in the new `Blob` (i.e. the byte exactly at |
| 81 | + * this index is not included). If you specify a negative value, it's treated |
| 82 | + * as an offset from the end of the Blob toward the beginning. For example, |
| 83 | + * `-10` would be the 10th from last byte in the `Blob`. The default value is |
| 84 | + * size. |
| 85 | + * @param {string} [type] - The content type to assign to the new Blob; |
| 86 | + * this will be the value of its type property. The default value is an empty |
| 87 | + * string. |
| 88 | + * @returns {Blob} |
| 89 | + */ |
| 90 | + slice (start = 0, end = this.size, type = '') { |
| 91 | + const { size, _parts } = this |
| 92 | + let offset = start < 0 |
| 93 | + ? Math.max(size + start, 0) |
| 94 | + : Math.min(start, size) |
| 95 | + |
| 96 | + let limit = (end < 0 ? Math.max(size + end, 0) : Math.min(end, size)) |
| 97 | + const span = Math.max(limit - offset, 0) |
| 98 | + |
| 99 | + let blobSize = 0 |
| 100 | + const blobParts = [] |
| 101 | + for (const part of _parts) { |
| 102 | + const { byteLength } = part |
| 103 | + if (offset > 0 && byteLength <= offset) { |
| 104 | + offset -= byteLength |
| 105 | + limit -= byteLength |
| 106 | + } else { |
| 107 | + const chunk = part.subarray(offset, Math.min(byteLength, limit)) |
| 108 | + blobParts.push(chunk) |
| 109 | + blobSize += chunk.byteLength |
| 110 | + // no longer need to take that into account |
| 111 | + offset = 0 |
| 112 | + |
| 113 | + // don't add the overflow to new blobParts |
| 114 | + if (blobSize >= span) { |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + const blob = new Blob([], { type }) |
| 121 | + blob._parts = blobParts |
| 122 | + blob._size = blobSize |
| 123 | + |
| 124 | + return blob |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns a promise that resolves with an ArrayBuffer containing the entire |
| 129 | + * contents of the Blob as binary data. |
| 130 | + * @returns {Promise<ArrayBuffer>} |
| 131 | + */ |
| 132 | + // eslint-disable-next-line require-await |
| 133 | + async arrayBuffer () { |
| 134 | + const buffer = new ArrayBuffer(this.size) |
| 135 | + const bytes = new Uint8Array(buffer) |
| 136 | + let offset = 0 |
| 137 | + for (const part of this._parts) { |
| 138 | + bytes.set(part, offset) |
| 139 | + offset += part.byteLength |
| 140 | + } |
| 141 | + return buffer |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns a promise that resolves with a USVString containing the entire |
| 146 | + * contents of the Blob interpreted as UTF-8 text. |
| 147 | + * @returns {Promise<string>} |
| 148 | + */ |
| 149 | + // eslint-disable-next-line require-await |
| 150 | + async text () { |
| 151 | + const decoder = new TextDecoder() |
| 152 | + let text = '' |
| 153 | + for (const part of this._parts) { |
| 154 | + text += decoder.decode(part) |
| 155 | + } |
| 156 | + return text |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @returns {never} |
| 161 | + */ |
| 162 | + // eslint-disable-next-line valid-jsdoc |
| 163 | + stream () { |
| 164 | + throw Error('Not implemented') |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Non standard, but if `ReadableStream`s are extended to be |
| 169 | + * made async iterable why not blobs. |
| 170 | + * @returns {AsyncIterator<Uint8Array>} |
| 171 | + */ |
| 172 | + // eslint-disable-next-line require-await |
| 173 | + async * [Symbol.asyncIterator] () { |
| 174 | + yield * this._parts |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +exports.Blob = Blob |
| 179 | + |
| 180 | +/** |
| 181 | + * Universal blob reading function |
| 182 | + * @param {Blob} blob |
| 183 | + * @returns {AsyncIterable<Uint8Array>} |
| 184 | + */ |
| 185 | +const readBlob = (blob) => blob |
| 186 | +exports.readBlob = readBlob |
0 commit comments