1
1
'use strict'
2
2
3
3
const bufferModule = require ( 'buffer' )
4
- const { kResistStopPropagation, SymbolDispose } = require ( './primordials' )
4
+ const { format, inspect } = require ( './util/inspect' )
5
+ const {
6
+ codes : { ERR_INVALID_ARG_TYPE }
7
+ } = require ( './errors' )
8
+ const { kResistStopPropagation, AggregateError, SymbolDispose } = require ( './primordials' )
5
9
const AbortSignal = globalThis . AbortSignal || require ( 'abort-controller' ) . AbortSignal
6
10
const AbortController = globalThis . AbortController || require ( 'abort-controller' ) . AbortController
7
11
const AsyncFunction = Object . getPrototypeOf ( async function ( ) { } ) . constructor
@@ -24,22 +28,8 @@ const validateAbortSignal = (signal, name) => {
24
28
}
25
29
}
26
30
const validateFunction = ( value , name ) => {
27
- if ( typeof value !== 'function' ) throw new ERR_INVALID_ARG_TYPE ( name , 'Function' , value )
28
- }
29
-
30
- // This is a simplified version of AggregateError
31
- class AggregateError extends Error {
32
- constructor ( errors ) {
33
- if ( ! Array . isArray ( errors ) ) {
34
- throw new TypeError ( `Expected input to be an Array, got ${ typeof errors } ` )
35
- }
36
- let message = ''
37
- for ( let i = 0 ; i < errors . length ; i ++ ) {
38
- message += ` ${ errors [ i ] . stack } \n`
39
- }
40
- super ( message )
41
- this . name = 'AggregateError'
42
- this . errors = errors
31
+ if ( typeof value !== 'function' ) {
32
+ throw new ERR_INVALID_ARG_TYPE ( name , 'Function' , value )
43
33
}
44
34
}
45
35
module . exports = {
@@ -83,50 +73,8 @@ module.exports = {
83
73
debuglog ( ) {
84
74
return function ( ) { }
85
75
} ,
86
- format ( format , ...args ) {
87
- // Simplified version of https://nodejs.org/api/util.html#utilformatformat-args
88
- return format . replace ( / % ( [ s d i f j ] ) / g, function ( ...[ _unused , type ] ) {
89
- const replacement = args . shift ( )
90
- if ( type === 'f' ) {
91
- return replacement . toFixed ( 6 )
92
- } else if ( type === 'j' ) {
93
- return JSON . stringify ( replacement )
94
- } else if ( type === 's' && typeof replacement === 'object' ) {
95
- const ctor = replacement . constructor !== Object ? replacement . constructor . name : ''
96
- return `${ ctor } {}` . trim ( )
97
- } else {
98
- return replacement . toString ( )
99
- }
100
- } )
101
- } ,
102
- inspect ( value ) {
103
- // Vastly simplified version of https://nodejs.org/api/util.html#utilinspectobject-options
104
- switch ( typeof value ) {
105
- case 'string' :
106
- if ( value . includes ( "'" ) ) {
107
- if ( ! value . includes ( '"' ) ) {
108
- return `"${ value } "`
109
- } else if ( ! value . includes ( '`' ) && ! value . includes ( '${' ) ) {
110
- return `\`${ value } \``
111
- }
112
- }
113
- return `'${ value } '`
114
- case 'number' :
115
- if ( isNaN ( value ) ) {
116
- return 'NaN'
117
- } else if ( Object . is ( value , - 0 ) ) {
118
- return String ( value )
119
- }
120
- return value
121
- case 'bigint' :
122
- return `${ String ( value ) } n`
123
- case 'boolean' :
124
- case 'undefined' :
125
- return String ( value )
126
- case 'object' :
127
- return '{}'
128
- }
129
- } ,
76
+ format,
77
+ inspect,
130
78
types : {
131
79
isAsyncFunction ( fn ) {
132
80
return fn instanceof AsyncFunction
0 commit comments