5
5
6
6
import './streams.cjs'
7
7
8
- /** @typedef {import('buffer').Blob } NodeBlob} */
9
-
10
8
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
11
9
const POOL_SIZE = 65536
12
10
13
- /** @param {(Blob | NodeBlob | Uint8Array)[] } parts */
11
+ /** @param {(Blob | Uint8Array)[] } parts */
14
12
async function * toIterator ( parts , clone = true ) {
15
13
for ( const part of parts ) {
16
14
if ( 'stream' in part ) {
17
- yield * part . stream ( )
15
+ yield * ( /** @type { AsyncIterableIterator<Uint8Array> } */ ( part . stream ( ) ) )
18
16
} else if ( ArrayBuffer . isView ( part ) ) {
19
17
if ( clone ) {
20
18
let position = part . byteOffset
@@ -28,17 +26,16 @@ async function * toIterator (parts, clone = true) {
28
26
} else {
29
27
yield part
30
28
}
29
+ /* c8 ignore next 10 */
31
30
} else {
32
- /* c8 ignore start */
33
31
// For blobs that have arrayBuffer but no stream method (nodes buffer.Blob)
34
- let position = 0
35
- while ( position !== part . size ) {
36
- const chunk = part . slice ( position , Math . min ( part . size , position + POOL_SIZE ) )
32
+ let position = 0 , b = ( /** @type { Blob } */ ( part ) )
33
+ while ( position !== b . size ) {
34
+ const chunk = b . slice ( position , Math . min ( b . size , position + POOL_SIZE ) )
37
35
const buffer = await chunk . arrayBuffer ( )
38
36
position += buffer . byteLength
39
37
yield new Uint8Array ( buffer )
40
38
}
41
- /* c8 ignore end */
42
39
}
43
40
}
44
41
}
@@ -48,14 +45,15 @@ const _Blob = class Blob {
48
45
#parts = [ ]
49
46
#type = ''
50
47
#size = 0
48
+ #endings = 'transparent'
51
49
52
50
/**
53
51
* The Blob() constructor returns a new Blob object. The content
54
52
* of the blob consists of the concatenation of the values given
55
53
* in the parameter array.
56
54
*
57
55
* @param {* } blobParts
58
- * @param {{ type?: string } } [options]
56
+ * @param {{ type?: string, endings?: string } } [options]
59
57
*/
60
58
constructor ( blobParts = [ ] , options = { } ) {
61
59
if ( typeof blobParts !== 'object' || blobParts === null ) {
@@ -82,15 +80,15 @@ const _Blob = class Blob {
82
80
} else if ( element instanceof Blob ) {
83
81
part = element
84
82
} else {
85
- part = encoder . encode ( element )
83
+ part = encoder . encode ( ` ${ element } ` )
86
84
}
87
85
88
86
this . #size += ArrayBuffer . isView ( part ) ? part . byteLength : part . size
89
87
this . #parts. push ( part )
90
88
}
91
89
90
+ this . #endings = `${ options . endings === undefined ? 'transparent' : options . endings } `
92
91
const type = options . type === undefined ? '' : String ( options . type )
93
-
94
92
this . #type = / ^ [ \x20 - \x7E ] * $ / . test ( type ) ? type : ''
95
93
}
96
94
@@ -156,6 +154,7 @@ const _Blob = class Blob {
156
154
const it = toIterator ( this . #parts, true )
157
155
158
156
return new globalThis . ReadableStream ( {
157
+ // @ts -ignore
159
158
type : 'bytes' ,
160
159
async pull ( ctrl ) {
161
160
const chunk = await it . next ( )
0 commit comments