55const { types } = require ( 'util' )
66const { validateHeaderName, validateHeaderValue } = require ( 'http' )
77const { kHeadersList } = require ( '../../core/symbols' )
8- const { InvalidHTTPTokenError, HTTPInvalidHeaderValueError, InvalidArgumentError, InvalidThisError } = require ( '../../core/errors' )
8+ const {
9+ InvalidHTTPTokenError,
10+ HTTPInvalidHeaderValueError,
11+ InvalidThisError
12+ } = require ( '../../core/errors' )
913
1014function binarySearch ( arr , val ) {
1115 let low = 0
@@ -49,21 +53,21 @@ function isHeaders (object) {
4953function fill ( headers , object ) {
5054 if ( isHeaders ( object ) ) {
5155 // Object is instance of Headers
52- headers [ kHeadersList ] = Array . splice ( object [ kHeadersList ] )
56+ headers [ kHeadersList ] = [ ... object [ kHeadersList ] ]
5357 } else if ( Array . isArray ( object ) ) {
5458 // Support both 1D and 2D arrays of header entries
5559 if ( Array . isArray ( object [ 0 ] ) ) {
5660 // Array of arrays
5761 for ( let i = 0 ; i < object . length ; i ++ ) {
5862 if ( object [ i ] . length !== 2 ) {
59- throw new InvalidArgumentError ( `The argument 'init' is not of length 2. Received ${ object [ i ] } ` )
63+ throw new TypeError ( `The argument 'init' is not of length 2. Received ${ object [ i ] } ` )
6064 }
6165 headers . append ( object [ i ] [ 0 ] , object [ i ] [ 1 ] )
6266 }
6367 } else if ( typeof object [ 0 ] === 'string' || Buffer . isBuffer ( object [ 0 ] ) ) {
6468 // Flat array of strings or Buffers
6569 if ( object . length % 2 !== 0 ) {
66- throw new InvalidArgumentError ( `The argument 'init' is not even in length. Received ${ object } ` )
70+ throw new TypeError ( `The argument 'init' is not even in length. Received ${ object } ` )
6771 }
6872 for ( let i = 0 ; i < object . length ; i += 2 ) {
6973 headers . append (
@@ -73,7 +77,7 @@ function fill (headers, object) {
7377 }
7478 } else {
7579 // All other array based entries
76- throw new InvalidArgumentError ( `The argument 'init' is not a valid array entry. Received ${ object } ` )
80+ throw new TypeError ( `The argument 'init' is not a valid array entry. Received ${ object } ` )
7781 }
7882 } else if ( ! types . isBoxedPrimitive ( object ) ) {
7983 // Object of key/value entries
@@ -94,12 +98,20 @@ class Headers {
9498 constructor ( init = { } ) {
9599 // validateObject allowArray = true
96100 if ( ! Array . isArray ( init ) && typeof init !== 'object' ) {
97- throw new InvalidArgumentError ( 'The argument \'init\' must be one of type Object or Array' )
101+ throw new TypeError ( 'The argument \'init\' must be one of type Object or Array' )
98102 }
99103 this [ kHeadersList ] = [ ]
100104 fill ( this , init )
101105 }
102106
107+ get [ Symbol . toStringTag ] ( ) {
108+ return this . constructor . name
109+ }
110+
111+ toString ( ) {
112+ return Object . prototype . toString . call ( this )
113+ }
114+
103115 append ( ...args ) {
104116 if ( ! isHeaders ( this ) ) {
105117 throw new InvalidThisError ( 'Header' )
@@ -233,8 +245,31 @@ class Headers {
233245 callback . call ( thisArg , this [ kHeadersList ] [ index + 1 ] , this [ kHeadersList ] [ index ] , this )
234246 }
235247 }
248+
249+ [ Symbol . for ( 'nodejs.util.inspect.custom' ) ] ( ) {
250+ return Object . fromEntries ( this . entries ( ) )
251+ }
236252}
237253
254+ // Re-shaping object for Web IDL tests.
255+ Object . defineProperties (
256+ Headers . prototype ,
257+ [
258+ 'append' ,
259+ 'delete' ,
260+ 'entries' ,
261+ 'forEach' ,
262+ 'get' ,
263+ 'has' ,
264+ 'keys' ,
265+ 'set' ,
266+ 'values'
267+ ] . reduce ( ( result , property ) => {
268+ result [ property ] = { enumerable : true }
269+ return result
270+ } , { } )
271+ )
272+
238273Headers . prototype [ Symbol . iterator ] = Headers . prototype . entries
239274
240275module . exports = Headers
0 commit comments