11'use strict' ;
22
33const {
4+ ArrayBuffer,
45 ArrayFrom,
56 MathMax,
67 MathMin,
@@ -21,7 +22,7 @@ const {
2122
2223const {
2324 createBlob : _createBlob ,
24- createBlobFromFileHandle : _createBlobFromFileHandle ,
25+ createBlobFromFilePath : _createBlobFromFilePath ,
2526 concat,
2627 getDataObject,
2728} = internalBinding ( 'blob' ) ;
@@ -48,6 +49,7 @@ const {
4849 customInspectSymbol : kInspect ,
4950 kEmptyObject,
5051 kEnumerableProperty,
52+ lazyDOMException,
5153} = require ( 'internal/util' ) ;
5254const { inspect } = require ( 'internal/util/inspect' ) ;
5355
@@ -58,14 +60,17 @@ const {
5860 ERR_INVALID_THIS ,
5961 ERR_BUFFER_TOO_LARGE ,
6062 } ,
61- errnoException,
6263} = require ( 'internal/errors' ) ;
6364
6465const {
6566 isUint32,
6667 validateDictionary,
6768} = require ( 'internal/validators' ) ;
6869
70+ const {
71+ CountQueuingStrategy,
72+ } = require ( 'internal/webstreams/queuingstrategies' ) ;
73+
6974const kHandle = Symbol ( 'kHandle' ) ;
7075const kType = Symbol ( 'kType' ) ;
7176const kLength = Symbol ( 'kLength' ) ;
@@ -265,16 +270,23 @@ class Blob {
265270 return PromiseResolve ( new ArrayBuffer ( 0 ) ) ;
266271 }
267272
268- const { promise, resolve } = createDeferredPromise ( ) ;
273+ const { promise, resolve, reject } = createDeferredPromise ( ) ;
269274 const reader = this [ kHandle ] . getReader ( ) ;
270275 const buffers = [ ] ;
271276 const readNext = ( ) => {
272277 reader . pull ( ( status , buffer ) => {
273- if ( status === - 1 ) {
278+ if ( status === 0 ) {
274279 // EOS, concat & resolve
275280 // buffer should be undefined here
276281 resolve ( concat ( buffers ) ) ;
277282 return ;
283+ } else if ( status < 0 ) {
284+ // The read could fail for many different reasons when reading
285+ // from a non-memory resident blob part (e.g. file-backed blob).
286+ // The error details the system error code.
287+ const error = lazyDOMException ( 'The blob could not be read' , 'NotReadableError' ) ;
288+ reject ( error ) ;
289+ return ;
278290 }
279291 if ( buffer !== undefined )
280292 buffers . push ( buffer ) ;
@@ -319,7 +331,7 @@ class Blob {
319331 } ,
320332 pull ( c ) {
321333 const { promise, resolve, reject } = createDeferredPromise ( ) ;
322- this . pendingPulls . push ( { resolve, reject} ) ;
334+ this . pendingPulls . push ( { resolve, reject } ) ;
323335 reader . pull ( ( status , buffer ) => {
324336 // If pendingPulls is empty here, the stream had to have
325337 // been canceled, and we don't really care about the result.
@@ -328,18 +340,24 @@ class Blob {
328340 return ;
329341 }
330342 const pending = this . pendingPulls . shift ( ) ;
331- if ( status === - 1 || ( status === 0 && buffer === undefined ) ) {
343+ if ( status === 0 ) {
332344 // EOS
333345 c . close ( ) ;
334346 pending . resolve ( ) ;
335347 return ;
336348 } else if ( status < 0 ) {
337- const error = errnoException ( status , 'read' ) ;
349+ // The read could fail for many different reasons when reading
350+ // from a non-memory resident blob part (e.g. file-backed blob).
351+ // The error details the system error code.
352+ const error = lazyDOMException ( 'The blob could not be read' , 'NotReadableError' ) ;
353+
338354 c . error ( error ) ;
339355 pending . reject ( error ) ;
340356 return ;
341357 }
342- c . enqueue ( new Uint8Array ( buffer ) ) ;
358+ if ( buffer !== undefined ) {
359+ c . enqueue ( new Uint8Array ( buffer ) ) ;
360+ }
343361 pending . resolve ( ) ;
344362 } ) ;
345363 return promise ;
@@ -422,16 +440,22 @@ function resolveObjectURL(url) {
422440 }
423441}
424442
425- function createBlobFromFileHandle ( handle ) {
426- const [ blob , length ] = _createBlobFromFileHandle ( handle ) ;
427- return createBlob ( blob , length ) ;
443+ // TODO(@jasnell): Now that the File class exists, we might consider having
444+ // this return a `File` instead of a `Blob`.
445+ function createBlobFromFilePath ( path , options ) {
446+ const maybeBlob = _createBlobFromFilePath ( path ) ;
447+ if ( maybeBlob === undefined ) {
448+ return lazyDOMException ( 'The blob could not be read' , 'NotReadableError' ) ;
449+ }
450+ const { 0 : blob , 1 : length } = maybeBlob ;
451+ return createBlob ( blob , length , options ?. type ) ;
428452}
429453
430454module . exports = {
431455 Blob,
432456 ClonedBlob,
433457 createBlob,
434- createBlobFromFileHandle ,
458+ createBlobFromFilePath ,
435459 isBlob,
436460 kHandle,
437461 resolveObjectURL,
0 commit comments