11'use strict' ;
22
33const {
4+ ArrayBufferPrototypeGetByteLength,
5+ ArrayBufferPrototypeGetDetached,
46 ArrayPrototypePush,
7+ DataViewPrototypeGetBuffer,
8+ DataViewPrototypeGetByteLength,
9+ DataViewPrototypeGetByteOffset,
510 MathMax,
611 MathMin,
712 ObjectDefineProperties,
@@ -12,10 +17,14 @@ const {
1217 PromiseWithResolvers,
1318 RegExpPrototypeExec,
1419 RegExpPrototypeSymbolReplace,
20+ SafeWeakSet,
1521 StringPrototypeSplit,
1622 StringPrototypeToLowerCase,
1723 Symbol,
1824 SymbolToStringTag,
25+ TypedArrayPrototypeGetBuffer,
26+ TypedArrayPrototypeGetByteLength,
27+ TypedArrayPrototypeGetByteOffset,
1928 Uint8Array,
2029} = primordials ;
2130
@@ -43,7 +52,9 @@ const {
4352
4453const {
4554 isAnyArrayBuffer,
55+ isArrayBuffer,
4656 isArrayBufferView,
57+ isDataView,
4758} = require ( 'internal/util/types' ) ;
4859
4960const {
@@ -55,22 +66,19 @@ const {
5566const { inspect } = require ( 'internal/util/inspect' ) ;
5667const {
5768 converters,
69+ createDictionaryConverter,
70+ createEnumConverter,
5871 createSequenceConverter,
5972} = require ( 'internal/webidl' ) ;
6073
6174const {
6275 codes : {
6376 ERR_BUFFER_TOO_LARGE ,
64- ERR_INVALID_ARG_VALUE ,
6577 ERR_INVALID_STATE ,
6678 ERR_INVALID_THIS ,
6779 } ,
6880} = require ( 'internal/errors' ) ;
6981
70- const {
71- validateDictionary,
72- } = require ( 'internal/validators' ) ;
73-
7482const {
7583 setImmediate,
7684} = require ( 'timers' ) ;
@@ -81,6 +89,8 @@ const kHandle = Symbol('kHandle');
8189const kType = Symbol ( 'kType' ) ;
8290const kLength = Symbol ( 'kLength' ) ;
8391const kNotCloneable = Symbol ( 'kNotCloneable' ) ;
92+ const kAlreadyConverted = Symbol ( 'kAlreadyConverted' ) ;
93+ const blobBrand = new SafeWeakSet ( ) ;
8494
8595const disallowedTypeCharacters = / [ ^ \u{0020} - \u{007E} ] / u;
8696
@@ -110,16 +120,14 @@ function lazyTextDecoderStream() {
110120const { EOL } = require ( 'internal/constants' ) ;
111121
112122function isBlob ( object ) {
113- return object ?. [ kHandle ] !== undefined ;
123+ return blobBrand . has ( object ) ;
114124}
115125
116126function getSource ( source , endings ) {
117127 if ( isBlob ( source ) )
118- return [ source . size , source [ kHandle ] ] ;
128+ return [ source [ kLength ] , source [ kHandle ] ] ;
119129
120- if ( isAnyArrayBuffer ( source ) ) {
121- source = new Uint8Array ( source ) ;
122- } else if ( ! isArrayBufferView ( source ) ) {
130+ if ( typeof source === 'string' ) {
123131 if ( endings === 'native' )
124132 source = RegExpPrototypeSymbolReplace ( / \r \n | \r | \n / g, source , EOL ) ;
125133 source = enc . encode ( source ) ;
@@ -128,21 +136,49 @@ function getSource(source, endings) {
128136 // We copy into a new Uint8Array because the underlying
129137 // BackingStores are going to be detached and owned by
130138 // the Blob.
131- const { buffer, byteOffset, byteLength } = source ;
132- const slice = buffer . slice ( byteOffset , byteOffset + byteLength ) ;
133- return [ byteLength , new Uint8Array ( slice ) ] ;
139+ const isBuffer = isArrayBuffer ( source ) ;
140+ const isDataViewSource = ! isBuffer && isDataView ( source ) ;
141+ const buffer = isBuffer ? source :
142+ isDataViewSource ? DataViewPrototypeGetBuffer ( source ) : TypedArrayPrototypeGetBuffer ( source ) ;
143+ // Check before reading byteOffset and byteLength, whose DataView getters
144+ // throw when the backing buffer is detached.
145+ if ( ArrayBufferPrototypeGetDetached ( buffer ) )
146+ return [ 0 , new Uint8Array ( ) ] ;
147+ const byteOffset = isBuffer ? 0 :
148+ isDataViewSource ? DataViewPrototypeGetByteOffset ( source ) : TypedArrayPrototypeGetByteOffset ( source ) ;
149+ const byteLength = isBuffer ? ArrayBufferPrototypeGetByteLength ( source ) :
150+ isDataViewSource ? DataViewPrototypeGetByteLength ( source ) : TypedArrayPrototypeGetByteLength ( source ) ;
151+ const copy = new Uint8Array ( new Uint8Array ( buffer , byteOffset , byteLength ) ) ;
152+ return [ byteLength , copy ] ;
134153}
135154
136- const sourcesConverter = createSequenceConverter ( ( source , opts = kEmptyObject ) => {
155+ const blobPartsConverter = createSequenceConverter ( ( source , opts = kEmptyObject ) => {
137156 if ( isBlob ( source ) ) {
138157 return source ;
139158 }
159+ // Match browsers for bare SharedArrayBuffer pending a resolution of
160+ // https://github.com/whatwg/webidl/issues/1491.
140161 if ( isAnyArrayBuffer ( source ) || isArrayBufferView ( source ) ) {
141162 return converters . BufferSource ( source , opts ) ;
142163 }
143- return converters . DOMString ( source , opts ) ;
164+ return converters . USVString ( source , opts ) ;
144165} ) ;
145166
167+ const blobPropertyBag = [
168+ {
169+ key : 'endings' ,
170+ converter : createEnumConverter ( 'EndingType' , [ 'transparent' , 'native' ] ) ,
171+ defaultValue : ( ) => 'transparent' ,
172+ } ,
173+ {
174+ key : 'type' ,
175+ converter : converters . DOMString ,
176+ defaultValue : ( ) => '' ,
177+ } ,
178+ ] ;
179+
180+ const blobPropertyBagConverter = createDictionaryConverter ( 'BlobPropertyBag' , blobPropertyBag ) ;
181+
146182class Blob {
147183 /**
148184 * @typedef {string|ArrayBuffer|ArrayBufferView|Blob } SourcePart
@@ -156,40 +192,38 @@ class Blob {
156192 * }} [options]
157193 * @constructs Blob
158194 */
159- constructor ( sources = [ ] , options ) {
195+ constructor ( sources = [ ] , options , alreadyConverted ) {
160196 markTransferMode ( this , true , false ) ;
161197
162- const sources_ = sourcesConverter ( sources , {
163- __proto__ : null ,
164- context : 'sources' ,
165- } ) ;
166-
167- validateDictionary ( options , 'options' ) ;
168- let {
169- endings = 'transparent' ,
170- type = '' ,
171- } = options ?? kEmptyObject ;
172-
173- endings = `${ endings } ` ;
174- if ( endings !== 'transparent' && endings !== 'native' )
175- throw new ERR_INVALID_ARG_VALUE ( 'options.endings' , endings ) ;
198+ // File converts all of its arguments before entering the Blob constructor.
199+ if ( alreadyConverted !== kAlreadyConverted ) {
200+ sources = blobPartsConverter ( sources , {
201+ __proto__ : null ,
202+ context : 'sources' ,
203+ } ) ;
204+ options = blobPropertyBagConverter ( options , {
205+ __proto__ : null ,
206+ context : 'options' ,
207+ } ) ;
208+ }
209+ const { endings, type } = options ;
176210
177211 let length = 0 ;
178- for ( let i = 0 ; i < sources_ . length ; ++ i ) {
179- const { 0 : len , 1 : src } = getSource ( sources_ [ i ] , endings ) ;
212+ for ( let i = 0 ; i < sources . length ; ++ i ) {
213+ const { 0 : len , 1 : src } = getSource ( sources [ i ] , endings ) ;
180214 length += len ;
181- sources_ [ i ] = src ;
215+ sources [ i ] = src ;
182216 }
183217
184218 if ( length > kMaxLength )
185219 throw new ERR_BUFFER_TOO_LARGE ( kMaxLength ) ;
186220
187- this [ kHandle ] = _createBlob ( sources_ , length ) ;
221+ this [ kHandle ] = _createBlob ( sources , length ) ;
188222 this [ kLength ] = length ;
189223
190- type = `${ type } ` ;
191224 this [ kType ] = RegExpPrototypeExec ( disallowedTypeCharacters , type ) !== null ?
192225 '' : StringPrototypeToLowerCase ( type ) ;
226+ blobBrand . add ( this ) ;
193227 }
194228
195229 [ kInspect ] ( depth , options ) {
@@ -226,6 +260,7 @@ class Blob {
226260 this [ kHandle ] = handle ;
227261 this [ kType ] = type ;
228262 this [ kLength ] = length ;
263+ blobBrand . add ( this ) ;
229264 }
230265
231266 /**
@@ -357,6 +392,7 @@ function TransferableBlob(handle, length, type = '') {
357392 this [ kHandle ] = handle ;
358393 this [ kType ] = type ;
359394 this [ kLength ] = length ;
395+ blobBrand . add ( this ) ;
360396}
361397
362398ObjectSetPrototypeOf ( TransferableBlob . prototype , Blob . prototype ) ;
@@ -701,12 +737,15 @@ async function* createBlobReaderIterable(reader, options = kEmptyObject) {
701737
702738module . exports = {
703739 Blob,
740+ blobPartsConverter,
741+ blobPropertyBag,
704742 createBlob,
705743 createBlobFromFilePath,
706744 createBlobReaderIterable,
707745 createBlobReaderStream,
708746 getBlobDataSync,
709747 isBlob,
748+ kAlreadyConverted,
710749 kHandle,
711750 kType,
712751 resolveObjectURL,
0 commit comments