11'use strict' ;
22
3- // New Streams API - Utility Functions
4-
53const {
6- Array,
74 ArrayPrototypeSlice,
5+ TypedArrayPrototypeGetBuffer,
86 TypedArrayPrototypeGetByteLength,
9- TypedArrayPrototypeSet ,
7+ TypedArrayPrototypeGetByteOffset ,
108 Uint8Array,
119} = primordials ;
1210
1311const { TextEncoder } = require ( 'internal/encoding' ) ;
1412const {
1513 codes : {
1614 ERR_INVALID_ARG_TYPE ,
17- ERR_INVALID_ARG_VALUE ,
1815 } ,
1916} = require ( 'internal/errors' ) ;
17+
18+ const { Buffer } = require ( 'buffer' ) ;
19+
2020const { isUint8Array } = require ( 'internal/util/types' ) ;
2121
22+ const { validateOneOf } = require ( 'internal/validators' ) ;
23+
2224// Shared TextEncoder instance for string conversion.
2325const encoder = new TextEncoder ( ) ;
2426
@@ -55,6 +57,7 @@ function toUint8Array(chunk) {
5557 * @returns {boolean }
5658 */
5759function allUint8Array ( chunks ) {
60+ // Ok, well, kind of. This is more a check for "no strings"...
5861 for ( let i = 0 ; i < chunks . length ; i ++ ) {
5962 if ( typeof chunks [ i ] === 'string' ) return false ;
6063 }
@@ -67,28 +70,11 @@ function allUint8Array(chunks) {
6770 * @returns {Uint8Array }
6871 */
6972function concatBytes ( chunks ) {
70- if ( chunks . length === 0 ) {
71- return new Uint8Array ( 0 ) ;
72- }
73- if ( chunks . length === 1 ) {
74- return chunks [ 0 ] ;
75- }
76-
77- const len = chunks . length ;
78- const lengths = new Array ( len ) ;
79- let total = 0 ;
80- for ( let i = 0 ; i < len ; i ++ ) {
81- const l = TypedArrayPrototypeGetByteLength ( chunks [ i ] ) ;
82- lengths [ i ] = l ;
83- total += l ;
84- }
85- const result = new Uint8Array ( total ) ;
86- let offset = 0 ;
87- for ( let i = 0 ; i < len ; i ++ ) {
88- TypedArrayPrototypeSet ( result , chunks [ i ] , offset ) ;
89- offset += lengths [ i ] ;
90- }
91- return result ;
73+ const buf = Buffer . concat ( chunks ) ;
74+ return new Uint8Array (
75+ TypedArrayPrototypeGetBuffer ( buf ) ,
76+ TypedArrayPrototypeGetByteOffset ( buf ) ,
77+ TypedArrayPrototypeGetByteLength ( buf ) ) ;
9278}
9379
9480/**
@@ -154,14 +140,12 @@ function parsePullArgs(args) {
154140 * @param {string } value
155141 */
156142function validateBackpressure ( value ) {
157- if ( value !== 'strict' &&
158- value !== 'block' &&
159- value !== 'drop-oldest' &&
160- value !== 'drop-newest' ) {
161- throw new ERR_INVALID_ARG_VALUE (
162- 'options.backpressure' , value ,
163- 'must be "strict", "block", "drop-oldest", or "drop-newest"' ) ;
164- }
143+ validateOneOf ( value , 'options.backpressure' , [
144+ 'strict' ,
145+ 'block' ,
146+ 'drop-oldest' ,
147+ 'drop-newest' ,
148+ ] ) ;
165149}
166150
167151module . exports = {
0 commit comments