@@ -30,7 +30,7 @@ const {
3030} = internalBinding ( 'buffer' ) ;
3131
3232const {
33- TextDecoder ,
33+ getUtf8Decoder ,
3434 TextEncoder,
3535} = require ( 'internal/encoding' ) ;
3636const { URL } = require ( 'internal/url' ) ;
@@ -88,7 +88,6 @@ let ReadableStream;
8888let TextDecoderStream ;
8989
9090const enc = new TextEncoder ( ) ;
91- let dec ;
9291
9392// Yes, lazy loading is annoying but because of circular
9493// references between the url, internal/blob, and buffer
@@ -312,7 +311,7 @@ class Blob {
312311 if ( ! isBlob ( this ) )
313312 return PromiseReject ( new ERR_INVALID_THIS ( 'Blob' ) ) ;
314313
315- dec ??= new TextDecoder ( ) ;
314+ const dec = getUtf8Decoder ( ) ;
316315
317316 return PromisePrototypeThen (
318317 arrayBuffer ( this ) ,
@@ -466,6 +465,43 @@ function arrayBuffer(blob) {
466465 return promise ;
467466}
468467
468+ /**
469+ * Read a blob's data synchronously. This is only possible when every part
470+ * of the blob is memory-resident, in which case the reader's pull callbacks
471+ * are invoked synchronously; otherwise (e.g. for file-backed blobs)
472+ * undefined is returned.
473+ * @param {Blob } blob
474+ * @returns {ArrayBuffer|undefined }
475+ */
476+ function getBlobDataSync ( blob ) {
477+ const reader = blob [ kHandle ] . getReader ( ) ;
478+ const buffers = [ ] ;
479+ let result ;
480+ let ended = false ;
481+ while ( ! ended ) {
482+ let sync = false ;
483+ reader . pull ( ( status , buffer ) => {
484+ sync = true ;
485+ if ( status === 0 ) {
486+ // EOS; buffer should be undefined here.
487+ result = concat ( buffers ) ;
488+ ended = true ;
489+ return ;
490+ } else if ( status < 0 ) {
491+ ended = true ;
492+ return ;
493+ }
494+ if ( buffer !== undefined )
495+ ArrayPrototypePush ( buffers , buffer ) ;
496+ } ) ;
497+ if ( ! sync ) {
498+ // The data is not available synchronously.
499+ break ;
500+ }
501+ }
502+ return result ;
503+ }
504+
469505function createBlobReaderStream ( reader ) {
470506 return new lazyReadableStream ( {
471507 type : 'bytes' ,
@@ -644,6 +680,7 @@ module.exports = {
644680 createBlobFromFilePath,
645681 createBlobReaderIterable,
646682 createBlobReaderStream,
683+ getBlobDataSync,
647684 isBlob,
648685 kHandle,
649686 resolveObjectURL,
0 commit comments