| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fb8470a commit 3a6e724
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -273,54 +273,32 @@ class Blob { | |||
| 273 | 273 | if (!isBlob(this)) | |
| 274 | 274 | return PromiseReject(new ERR_INVALID_THIS('Blob')); | |
| 275 | 275 | ||
| 276 | - const { promise, resolve, reject } = createDeferredPromise(); | ||
| 277 | - const reader = this[kHandle].getReader(); | ||
| 278 | - const buffers = []; | ||
| 279 | - const readNext = () => { | ||
| 280 | - reader.pull((status, buffer) => { | ||
| 281 | - if (status === 0) { | ||
| 282 | - // EOS, concat & resolve | ||
| 283 | - // buffer should be undefined here | ||
| 284 | - resolve(concat(buffers)); | ||
| 285 | - return; | ||
| 286 | - } else if (status < 0) { | ||
| 287 | - // The read could fail for many different reasons when reading | ||
| 288 | - // from a non-memory resident blob part (e.g. file-backed blob). | ||
| 289 | - // The error details the system error code. | ||
| 290 | - const error = lazyDOMException('The blob could not be read', 'NotReadableError'); | ||
| 291 | - reject(error); | ||
| 292 | - return; | ||
| 293 | - } | ||
| 294 | - if (buffer !== undefined) | ||
| 295 | - buffers.push(buffer); | ||
| 296 | - queueMicrotask(() => readNext()); | ||
| 297 | - }); | ||
| 298 | - }; | ||
| 299 | - readNext(); | ||
| 300 | - return promise; | ||
| 276 | + return arrayBuffer(this); | ||
| 301 | 277 | } | |
| 302 | 278 | ||
| 303 | 279 | /** | |
| 304 | 280 | * @returns {Promise<string>} | |
| 305 | 281 | */ | |
| 306 | - async text() { | ||
| 282 | + text() { | ||
| 307 | 283 | if (!isBlob(this)) | |
| 308 | - throw new ERR_INVALID_THIS('Blob'); | ||
| 284 | + return PromiseReject(new ERR_INVALID_THIS('Blob')); | ||
| 309 | 285 | ||
| 310 | 286 | dec ??= new TextDecoder(); | |
| 311 | 287 | ||
| 312 | - return dec.decode(await this.arrayBuffer()); | ||
| 288 | + return PromisePrototypeThen( | ||
| 289 | + arrayBuffer(this), | ||
| 290 | + (buffer) => dec.decode(buffer)); | ||
| 313 | 291 | } | |
| 314 | 292 | ||
| 315 | 293 | /** | |
| 316 | 294 | * @returns {Promise<Uint8Array>} | |
| 317 | 295 | */ | |
| 318 | 296 | bytes() { | |
| 319 | 297 | if (!isBlob(this)) | |
| 320 | - throw new ERR_INVALID_THIS('Blob'); | ||
| 298 | + return PromiseReject(new ERR_INVALID_THIS('Blob')); | ||
| 321 | 299 | ||
| 322 | 300 | return PromisePrototypeThen( | |
| 323 | - this.arrayBuffer(), | ||
| 301 | + arrayBuffer(this), | ||
| 324 | 302 | (buffer) => new Uint8Array(buffer)); | |
| 325 | 303 | } | |
| 326 | 304 | ||
@@ -439,6 +417,7 @@ ObjectDefineProperties(Blob.prototype, { | |||
| 439 | 417 | stream: kEnumerableProperty, | |
| 440 | 418 | text: kEnumerableProperty, | |
| 441 | 419 | arrayBuffer: kEnumerableProperty, | |
| 420 | + bytes: kEnumerableProperty, | ||
| 442 | 421 | }); | |
| 443 | 422 | ||
| 444 | 423 | function resolveObjectURL(url) { | |
@@ -490,6 +469,34 @@ function createBlobFromFilePath(path, options) { | |||
| 490 | 469 | return res; | |
| 491 | 470 | } | |
| 492 | 471 | ||
| 472 | + function arrayBuffer(blob) { | ||
| 473 | + const { promise, resolve, reject } = createDeferredPromise(); | ||
| 474 | + const reader = blob[kHandle].getReader(); | ||
| 475 | + const buffers = []; | ||
| 476 | + const readNext = () => { | ||
| 477 | + reader.pull((status, buffer) => { | ||
| 478 | + if (status === 0) { | ||
| 479 | + // EOS, concat & resolve | ||
| 480 | + // buffer should be undefined here | ||
| 481 | + resolve(concat(buffers)); | ||
| 482 | + return; | ||
| 483 | + } else if (status < 0) { | ||
| 484 | + // The read could fail for many different reasons when reading | ||
| 485 | + // from a non-memory resident blob part (e.g. file-backed blob). | ||
| 486 | + // The error details the system error code. | ||
| 487 | + const error = lazyDOMException('The blob could not be read', 'NotReadableError'); | ||
| 488 | + reject(error); | ||
| 489 | + return; | ||
| 490 | + } | ||
| 491 | + if (buffer !== undefined) | ||
| 492 | + buffers.push(buffer); | ||
| 493 | + queueMicrotask(() => readNext()); | ||
| 494 | + }); | ||
| 495 | + }; | ||
| 496 | + readNext(); | ||
| 497 | + return promise; | ||
| 498 | + } | ||
| 499 | + | ||
| 493 | 500 | module.exports = { | |
| 494 | 501 | Blob, | |
| 495 | 502 | createBlob, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -197,6 +197,7 @@ assert.throws(() => new Blob({}), { | |||
| 197 | 197 | 'stream', | |
| 198 | 198 | 'text', | |
| 199 | 199 | 'arrayBuffer', | |
| 200 | + 'bytes', | ||
| 200 | 201 | ]; | |
| 201 | 202 | ||
| 202 | 203 | for (const prop of enumerable) { | |
@@ -409,10 +410,13 @@ assert.throws(() => new Blob({}), { | |||
| 409 | 410 | } | |
| 410 | 411 | ||
| 411 | 412 | (async () => { | |
| 412 | - await assert.rejects(async () => Blob.prototype.arrayBuffer.call(), { | ||
| 413 | + await assert.rejects(() => Blob.prototype.arrayBuffer.call(), { | ||
| 413 | 414 | code: 'ERR_INVALID_THIS', | |
| 414 | 415 | }); | |
| 415 | - await assert.rejects(async () => Blob.prototype.text.call(), { | ||
| 416 | + await assert.rejects(() => Blob.prototype.text.call(), { | ||
| 417 | + code: 'ERR_INVALID_THIS', | ||
| 418 | + }); | ||
| 419 | + await assert.rejects(() => Blob.prototype.bytes.call(), { | ||
| 416 | 420 | code: 'ERR_INVALID_THIS', | |
| 417 | 421 | }); | |
| 418 | 422 | })().then(common.mustCall()); | |
@@ -490,3 +494,16 @@ assert.throws(() => new Blob({}), { | |||
| 490 | 494 | assert.ok(structuredClone(blob).size === blob.size); | |
| 491 | 495 | assert.ok((await structuredClone(blob).text()) === (await blob.text())); | |
| 492 | 496 | })().then(common.mustCall()); | |
| 497 | + | ||
| 498 | + (async () => { | ||
| 499 | + const blob = new Blob(['hello']); | ||
| 500 | + const { arrayBuffer } = Blob.prototype; | ||
| 501 | + | ||
| 502 | + Blob.prototype.arrayBuffer = common.mustNotCall(); | ||
| 503 | + | ||
| 504 | + try { | ||
| 505 | + assert.strictEqual(await blob.text(), 'hello'); | ||
| 506 | + } finally { | ||
| 507 | + Blob.prototype.arrayBuffer = arrayBuffer; | ||
| 508 | + } | ||
| 509 | + })().then(common.mustCall()); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments