1010const {
1111 ArrayBufferPrototypeGetByteLength,
1212 ArrayBufferPrototypeSlice,
13- ArrayIsArray,
1413 ArrayPrototypeFilter,
1514 ArrayPrototypeMap,
1615 ArrayPrototypePush,
@@ -42,6 +41,8 @@ const {
4241} = require ( 'internal/validators' ) ;
4342
4443const {
44+ from,
45+ fromSync,
4546 isAsyncIterable,
4647 isSyncIterable,
4748} = require ( 'internal/streams/iter/from' ) ;
@@ -69,11 +70,6 @@ function isMergeOptions(value) {
6970
7071// Normalize a yielded value to a Uint8Array[] batch. Sources should yield
7172// Uint8Array[] but a raw Uint8Array or string is tolerated by wrapping it.
72- function ensureBatch ( batch ) {
73- if ( ArrayIsArray ( batch ) ) return batch ;
74- return [ batch ] ;
75- }
76-
7773// =============================================================================
7874// Shared chunk collection helpers
7975// =============================================================================
@@ -85,11 +81,12 @@ function ensureBatch(batch) {
8581 * @returns {Uint8Array[] }
8682 */
8783function collectSync ( source , limit ) {
84+ // Normalize source via fromSync() - accepts strings, ArrayBuffers, protocols, etc.
85+ const normalized = fromSync ( source ) ;
8886 const chunks = [ ] ;
8987 let totalBytes = 0 ;
9088
91- for ( const raw of source ) {
92- const batch = ensureBatch ( raw ) ;
89+ for ( const batch of normalized ) {
9390 for ( let i = 0 ; i < batch . length ; i ++ ) {
9491 const chunk = batch [ i ] ;
9592 if ( limit !== undefined ) {
@@ -115,65 +112,35 @@ function collectSync(source, limit) {
115112async function collectAsync ( source , signal , limit ) {
116113 signal ?. throwIfAborted ( ) ;
117114
115+ // Normalize source via from() - accepts strings, ArrayBuffers, protocols, etc.
116+ const normalized = from ( source ) ;
118117 const chunks = [ ] ;
119118
120119 // Fast path: no signal and no limit
121120 if ( ! signal && limit === undefined ) {
122- if ( isAsyncIterable ( source ) ) {
123- for await ( const raw of source ) {
124- const batch = ensureBatch ( raw ) ;
125- for ( let i = 0 ; i < batch . length ; i ++ ) {
126- ArrayPrototypePush ( chunks , batch [ i ] ) ;
127- }
128- }
129- } else if ( isSyncIterable ( source ) ) {
130- for ( const raw of source ) {
131- const batch = ensureBatch ( raw ) ;
132- for ( let i = 0 ; i < batch . length ; i ++ ) {
133- ArrayPrototypePush ( chunks , batch [ i ] ) ;
134- }
121+ for await ( const batch of normalized ) {
122+ for ( let i = 0 ; i < batch . length ; i ++ ) {
123+ ArrayPrototypePush ( chunks , batch [ i ] ) ;
135124 }
136- } else {
137- throw new ERR_INVALID_ARG_TYPE ( 'source' , [ 'AsyncIterable' , 'Iterable' ] , source ) ;
138125 }
139126 return chunks ;
140127 }
141128
142129 // Slow path: with signal or limit checks
143130 let totalBytes = 0 ;
144131
145- if ( isAsyncIterable ( source ) ) {
146- for await ( const raw of source ) {
147- const batch = ensureBatch ( raw ) ;
148- signal ?. throwIfAborted ( ) ;
149- for ( let i = 0 ; i < batch . length ; i ++ ) {
150- const chunk = batch [ i ] ;
151- if ( limit !== undefined ) {
152- totalBytes += TypedArrayPrototypeGetByteLength ( chunk ) ;
153- if ( totalBytes > limit ) {
154- throw new ERR_OUT_OF_RANGE ( 'totalBytes' , `<= ${ limit } ` , totalBytes ) ;
155- }
156- }
157- ArrayPrototypePush ( chunks , chunk ) ;
158- }
159- }
160- } else if ( isSyncIterable ( source ) ) {
161- for ( const raw of source ) {
162- const batch = ensureBatch ( raw ) ;
163- signal ?. throwIfAborted ( ) ;
164- for ( let i = 0 ; i < batch . length ; i ++ ) {
165- const chunk = batch [ i ] ;
166- if ( limit !== undefined ) {
167- totalBytes += TypedArrayPrototypeGetByteLength ( chunk ) ;
168- if ( totalBytes > limit ) {
169- throw new ERR_OUT_OF_RANGE ( 'totalBytes' , `<= ${ limit } ` , totalBytes ) ;
170- }
132+ for await ( const batch of normalized ) {
133+ signal ?. throwIfAborted ( ) ;
134+ for ( let i = 0 ; i < batch . length ; i ++ ) {
135+ const chunk = batch [ i ] ;
136+ if ( limit !== undefined ) {
137+ totalBytes += TypedArrayPrototypeGetByteLength ( chunk ) ;
138+ if ( totalBytes > limit ) {
139+ throw new ERR_OUT_OF_RANGE ( 'totalBytes' , `<= ${ limit } ` , totalBytes ) ;
171140 }
172- ArrayPrototypePush ( chunks , chunk ) ;
173141 }
142+ ArrayPrototypePush ( chunks , chunk ) ;
174143 }
175- } else {
176- throw new ERR_INVALID_ARG_TYPE ( 'source' , [ 'AsyncIterable' , 'Iterable' ] , source ) ;
177144 }
178145
179146 return chunks ;
@@ -216,7 +183,8 @@ function validateConsumerOptions(options) {
216183 try {
217184 new TextDecoder ( options . encoding ) ;
218185 } catch {
219- throw new ERR_INVALID_ARG_VALUE ( 'options.encoding' , options . encoding ) ;
186+ throw new ERR_INVALID_ARG_VALUE . RangeError (
187+ 'options.encoding' , options . encoding ) ;
220188 }
221189 }
222190}
@@ -234,7 +202,8 @@ function validateSyncConsumerOptions(options) {
234202 try {
235203 new TextDecoder ( options . encoding ) ;
236204 } catch {
237- throw new ERR_INVALID_ARG_VALUE ( 'options.encoding' , options . encoding ) ;
205+ throw new ERR_INVALID_ARG_VALUE . RangeError (
206+ 'options.encoding' , options . encoding ) ;
238207 }
239208 }
240209}
@@ -266,7 +235,6 @@ function textSync(source, options = { __proto__: null }) {
266235 const decoder = new TextDecoder ( options . encoding ?? 'utf-8' , {
267236 __proto__ : null ,
268237 fatal : true ,
269- ignoreBOM : true ,
270238 } ) ;
271239 return decoder . decode ( data ) ;
272240}
@@ -322,7 +290,6 @@ async function text(source, options = { __proto__: null }) {
322290 const decoder = new TextDecoder ( options . encoding ?? 'utf-8' , {
323291 __proto__ : null ,
324292 fatal : true ,
325- ignoreBOM : true ,
326293 } ) ;
327294 return decoder . decode ( data ) ;
328295}
0 commit comments