@@ -273,51 +273,29 @@ 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
bytes ( ) {
316
294
if ( ! isBlob ( this ) )
317
- throw new ERR_INVALID_THIS ( 'Blob' ) ;
295
+ return PromiseReject ( new ERR_INVALID_THIS ( 'Blob' ) ) ;
318
296
319
297
return PromisePrototypeThen (
320
- this . arrayBuffer ( ) ,
298
+ arrayBuffer ( this ) ,
321
299
( buffer ) => new Uint8Array ( buffer ) ) ;
322
300
}
323
301
@@ -436,6 +414,7 @@ ObjectDefineProperties(Blob.prototype, {
436
414
stream : kEnumerableProperty ,
437
415
text : kEnumerableProperty ,
438
416
arrayBuffer : kEnumerableProperty ,
417
+ bytes : kEnumerableProperty ,
439
418
} ) ;
440
419
441
420
function resolveObjectURL ( url ) {
@@ -487,6 +466,34 @@ function createBlobFromFilePath(path, options) {
487
466
return res ;
488
467
}
489
468
469
+ function arrayBuffer ( blob ) {
470
+ const { promise, resolve, reject } = createDeferredPromise ( ) ;
471
+ const reader = blob [ kHandle ] . getReader ( ) ;
472
+ const buffers = [ ] ;
473
+ const readNext = ( ) => {
474
+ reader . pull ( ( status , buffer ) => {
475
+ if ( status === 0 ) {
476
+ // EOS, concat & resolve
477
+ // buffer should be undefined here
478
+ resolve ( concat ( buffers ) ) ;
479
+ return ;
480
+ } else if ( status < 0 ) {
481
+ // The read could fail for many different reasons when reading
482
+ // from a non-memory resident blob part (e.g. file-backed blob).
483
+ // The error details the system error code.
484
+ const error = lazyDOMException ( 'The blob could not be read' , 'NotReadableError' ) ;
485
+ reject ( error ) ;
486
+ return ;
487
+ }
488
+ if ( buffer !== undefined )
489
+ buffers . push ( buffer ) ;
490
+ queueMicrotask ( ( ) => readNext ( ) ) ;
491
+ } ) ;
492
+ } ;
493
+ readNext ( ) ;
494
+ return promise ;
495
+ }
496
+
490
497
module . exports = {
491
498
Blob,
492
499
createBlob,
0 commit comments