|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ArrayFrom, |
| 5 | + ObjectSetPrototypeOf, |
| 6 | + Promise, |
| 7 | + PromiseResolve, |
| 8 | + RegExpPrototypeTest, |
| 9 | + StringPrototypeToLowerCase, |
| 10 | + Symbol, |
| 11 | + SymbolIterator, |
| 12 | + Uint8Array, |
| 13 | +} = primordials; |
| 14 | + |
| 15 | +const { |
| 16 | + createBlob, |
| 17 | + FixedSizeBlobCopyJob, |
| 18 | +} = internalBinding('buffer'); |
| 19 | + |
| 20 | +const { |
| 21 | + JSTransferable, |
| 22 | + kClone, |
| 23 | + kDeserialize, |
| 24 | +} = require('internal/worker/js_transferable'); |
| 25 | + |
| 26 | +const { |
| 27 | + isAnyArrayBuffer, |
| 28 | + isArrayBufferView, |
| 29 | +} = require('internal/util/types'); |
| 30 | + |
| 31 | +const { |
| 32 | + customInspectSymbol: kInspect, |
| 33 | + emitExperimentalWarning, |
| 34 | +} = require('internal/util'); |
| 35 | +const { inspect } = require('internal/util/inspect'); |
| 36 | + |
| 37 | +const { |
| 38 | + AbortError, |
| 39 | + codes: { |
| 40 | + ERR_INVALID_ARG_TYPE, |
| 41 | + ERR_BUFFER_TOO_LARGE, |
| 42 | + ERR_OUT_OF_RANGE, |
| 43 | + } |
| 44 | +} = require('internal/errors'); |
| 45 | + |
| 46 | +const { |
| 47 | + validateObject, |
| 48 | + validateString, |
| 49 | + validateUint32, |
| 50 | + isUint32, |
| 51 | +} = require('internal/validators'); |
| 52 | + |
| 53 | +const kHandle = Symbol('kHandle'); |
| 54 | +const kType = Symbol('kType'); |
| 55 | +const kLength = Symbol('kLength'); |
| 56 | + |
| 57 | +let Buffer; |
| 58 | + |
| 59 | +function deferred() { |
| 60 | + let res, rej; |
| 61 | + const promise = new Promise((resolve, reject) => { |
| 62 | + res = resolve; |
| 63 | + rej = reject; |
| 64 | + }); |
| 65 | + return { promise, resolve: res, reject: rej }; |
| 66 | +} |
| 67 | + |
| 68 | +function lazyBuffer() { |
| 69 | + if (Buffer === undefined) |
| 70 | + Buffer = require('buffer').Buffer; |
| 71 | + return Buffer; |
| 72 | +} |
| 73 | + |
| 74 | +function isBlob(object) { |
| 75 | + return object?.[kHandle] !== undefined; |
| 76 | +} |
| 77 | + |
| 78 | +function getSource(source, encoding) { |
| 79 | + if (isBlob(source)) |
| 80 | + return [source.size, source[kHandle]]; |
| 81 | + |
| 82 | + if (typeof source === 'string') { |
| 83 | + source = lazyBuffer().from(source, encoding); |
| 84 | + } else if (isAnyArrayBuffer(source)) { |
| 85 | + source = new Uint8Array(source); |
| 86 | + } else if (!isArrayBufferView(source)) { |
| 87 | + throw new ERR_INVALID_ARG_TYPE( |
| 88 | + 'source', |
| 89 | + [ |
| 90 | + 'string', |
| 91 | + 'ArrayBuffer', |
| 92 | + 'SharedArrayBuffer', |
| 93 | + 'Buffer', |
| 94 | + 'TypedArray', |
| 95 | + 'DataView' |
| 96 | + ], |
| 97 | + source); |
| 98 | + } |
| 99 | + |
| 100 | + // We copy into a new Uint8Array because the underlying |
| 101 | + // BackingStores are going to be detached and owned by |
| 102 | + // the Blob. We also don't want to have to worry about |
| 103 | + // byte offsets. |
| 104 | + source = new Uint8Array(source); |
| 105 | + return [source.byteLength, source]; |
| 106 | +} |
| 107 | + |
| 108 | +class InternalBlob extends JSTransferable { |
| 109 | + constructor(handle, length, type = '') { |
| 110 | + super(); |
| 111 | + this[kHandle] = handle; |
| 112 | + this[kType] = type; |
| 113 | + this[kLength] = length; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +class Blob extends JSTransferable { |
| 118 | + constructor(sources = [], options) { |
| 119 | + emitExperimentalWarning('buffer.Blob'); |
| 120 | + if (sources === null || |
| 121 | + typeof sources[SymbolIterator] !== 'function' || |
| 122 | + typeof sources === 'string') { |
| 123 | + throw new ERR_INVALID_ARG_TYPE('sources', 'Iterable', sources); |
| 124 | + } |
| 125 | + if (options !== undefined) |
| 126 | + validateObject(options, 'options'); |
| 127 | + const { |
| 128 | + encoding = 'utf8', |
| 129 | + type = '', |
| 130 | + } = { ...options }; |
| 131 | + |
| 132 | + let length = 0; |
| 133 | + const sources_ = ArrayFrom(sources, (source) => { |
| 134 | + const { 0: len, 1: src } = getSource(source, encoding); |
| 135 | + length += len; |
| 136 | + return src; |
| 137 | + }); |
| 138 | + |
| 139 | + // This is a MIME media type but we're not actively checking the syntax. |
| 140 | + // But, to be fair, neither does Chrome. |
| 141 | + validateString(type, 'options.type'); |
| 142 | + |
| 143 | + if (!isUint32(length)) |
| 144 | + throw new ERR_BUFFER_TOO_LARGE(0xFFFFFFFF); |
| 145 | + |
| 146 | + super(); |
| 147 | + this[kHandle] = createBlob(sources_, length); |
| 148 | + this[kLength] = length; |
| 149 | + this[kType] = RegExpPrototypeTest(/[^\u{0020}-\u{007E}]/u, type) ? |
| 150 | + '' : StringPrototypeToLowerCase(type); |
| 151 | + } |
| 152 | + |
| 153 | + [kInspect](depth, options) { |
| 154 | + if (depth < 0) |
| 155 | + return this; |
| 156 | + |
| 157 | + const opts = { |
| 158 | + ...options, |
| 159 | + depth: options.depth == null ? null : options.depth - 1 |
| 160 | + }; |
| 161 | + |
| 162 | + return `Blob ${inspect({ |
| 163 | + size: this.size, |
| 164 | + type: this.type, |
| 165 | + }, opts)}`; |
| 166 | + } |
| 167 | + |
| 168 | + [kClone]() { |
| 169 | + const handle = this[kHandle]; |
| 170 | + const type = this[kType]; |
| 171 | + const length = this[kLength]; |
| 172 | + return { |
| 173 | + data: { handle, type, length }, |
| 174 | + deserializeInfo: 'internal/blob:InternalBlob' |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + [kDeserialize]({ handle, type, length }) { |
| 179 | + this[kHandle] = handle; |
| 180 | + this[kType] = type; |
| 181 | + this[kLength] = length; |
| 182 | + } |
| 183 | + |
| 184 | + get type() { return this[kType]; } |
| 185 | + |
| 186 | + get size() { return this[kLength]; } |
| 187 | + |
| 188 | + slice(start = 0, end = (this[kLength]), type = this[kType]) { |
| 189 | + validateUint32(start, 'start'); |
| 190 | + if (end < 0) end = this[kLength] + end; |
| 191 | + validateUint32(end, 'end'); |
| 192 | + validateString(type, 'type'); |
| 193 | + if (end < start) |
| 194 | + throw new ERR_OUT_OF_RANGE('end', 'greater than start', end); |
| 195 | + if (end > this[kLength]) |
| 196 | + throw new ERR_OUT_OF_RANGE('end', 'less than or equal to length', end); |
| 197 | + return new InternalBlob( |
| 198 | + this[kHandle].slice(start, end), |
| 199 | + end - start, type); |
| 200 | + } |
| 201 | + |
| 202 | + async arrayBuffer() { |
| 203 | + const job = new FixedSizeBlobCopyJob(this[kHandle]); |
| 204 | + |
| 205 | + const ret = job.run(); |
| 206 | + if (ret !== undefined) |
| 207 | + return PromiseResolve(ret); |
| 208 | + |
| 209 | + const { |
| 210 | + promise, |
| 211 | + resolve, |
| 212 | + reject |
| 213 | + } = deferred(); |
| 214 | + job.ondone = (err, ab) => { |
| 215 | + if (err !== undefined) |
| 216 | + return reject(new AbortError()); |
| 217 | + resolve(ab); |
| 218 | + }; |
| 219 | + |
| 220 | + return promise; |
| 221 | + } |
| 222 | + |
| 223 | + async text() { |
| 224 | + const dec = new TextDecoder(); |
| 225 | + return dec.decode(await this.arrayBuffer()); |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +InternalBlob.prototype.constructor = Blob; |
| 230 | +ObjectSetPrototypeOf( |
| 231 | + InternalBlob.prototype, |
| 232 | + Blob.prototype); |
| 233 | + |
| 234 | +module.exports = { |
| 235 | + Blob, |
| 236 | + InternalBlob, |
| 237 | + isBlob, |
| 238 | +}; |
0 commit comments