@@ -5,19 +5,24 @@ const signals = process.binding('constants').os.signals;
5
5
6
6
const kArrowMessagePrivateSymbolIndex = binding [ 'arrow_message_private_symbol' ] ;
7
7
const kDecoratedPrivateSymbolIndex = binding [ 'decorated_private_symbol' ] ;
8
+ const noCrypto = ! process . versions . openssl ;
8
9
9
- // The `buffer` module uses this. Defining it here instead of in the public
10
- // `util` module makes it accessible without having to `require('util')` there.
11
- exports . customInspectSymbol = Symbol ( 'util.inspect.custom' ) ;
10
+ function isError ( e ) {
11
+ return objectToString ( e ) === '[object Error]' || e instanceof Error ;
12
+ }
13
+
14
+ function objectToString ( o ) {
15
+ return Object . prototype . toString . call ( o ) ;
16
+ }
12
17
13
18
// Mark that a method should not be used.
14
19
// Returns a modified function which warns once by default.
15
20
// If --no-deprecation is set, then it is a no-op.
16
- exports . deprecate = function deprecate ( fn , msg , code ) {
21
+ function deprecate ( fn , msg , code ) {
17
22
// Allow for deprecating things in the process of starting up.
18
23
if ( global . process === undefined ) {
19
- return function ( ) {
20
- return exports . deprecate ( fn , msg , code ) . apply ( this , arguments ) ;
24
+ return function ( ... args ) {
25
+ return deprecate ( fn , msg ) . apply ( this , args ) ;
21
26
} ;
22
27
}
23
28
@@ -29,7 +34,7 @@ exports.deprecate = function deprecate(fn, msg, code) {
29
34
throw new TypeError ( '`code` argument must be a string' ) ;
30
35
31
36
var warned = false ;
32
- function deprecated ( ) {
37
+ function deprecated ( ... args ) {
33
38
if ( ! warned ) {
34
39
warned = true ;
35
40
if ( code !== undefined ) {
@@ -39,9 +44,9 @@ exports.deprecate = function deprecate(fn, msg, code) {
39
44
}
40
45
}
41
46
if ( new . target ) {
42
- return Reflect . construct ( fn , arguments , new . target ) ;
47
+ return Reflect . construct ( fn , args , new . target ) ;
43
48
}
44
- return fn . apply ( this , arguments ) ;
49
+ return fn . apply ( this , args ) ;
45
50
}
46
51
47
52
// The wrapper will keep the same prototype as fn to maintain prototype chain
@@ -54,10 +59,10 @@ exports.deprecate = function deprecate(fn, msg, code) {
54
59
}
55
60
56
61
return deprecated ;
57
- } ;
62
+ }
58
63
59
- exports . decorateErrorStack = function decorateErrorStack ( err ) {
60
- if ( ! ( exports . isError ( err ) && err . stack ) ||
64
+ function decorateErrorStack ( err ) {
65
+ if ( ! ( isError ( err ) && err . stack ) ||
61
66
binding . getHiddenValue ( err , kDecoratedPrivateSymbolIndex ) === true )
62
67
return ;
63
68
@@ -67,30 +72,19 @@ exports.decorateErrorStack = function decorateErrorStack(err) {
67
72
err . stack = arrow + err . stack ;
68
73
binding . setHiddenValue ( err , kDecoratedPrivateSymbolIndex , true ) ;
69
74
}
70
- } ;
71
-
72
- exports . isError = function isError ( e ) {
73
- return exports . objectToString ( e ) === '[object Error]' || e instanceof Error ;
74
- } ;
75
-
76
- exports . objectToString = function objectToString ( o ) {
77
- return Object . prototype . toString . call ( o ) ;
78
- } ;
75
+ }
79
76
80
- const noCrypto = ! process . versions . openssl ;
81
- exports . assertCrypto = function ( ) {
77
+ function assertCrypto ( ) {
82
78
if ( noCrypto )
83
79
throw new Error ( 'Node.js is not compiled with openssl crypto support' ) ;
84
- } ;
85
-
86
- exports . kIsEncodingSymbol = Symbol ( 'node.isEncoding' ) ;
80
+ }
87
81
88
82
// The loop should only run at most twice, retrying with lowercased enc
89
83
// if there is no match in the first pass.
90
84
// We use a loop instead of branching to retry with a helper
91
85
// function in order to avoid the performance hit.
92
86
// Return undefined if there is no match.
93
- exports . normalizeEncoding = function normalizeEncoding ( enc ) {
87
+ function normalizeEncoding ( enc ) {
94
88
if ( ! enc ) return 'utf8' ;
95
89
var retried ;
96
90
while ( true ) {
@@ -116,11 +110,9 @@ exports.normalizeEncoding = function normalizeEncoding(enc) {
116
110
retried = true ;
117
111
}
118
112
}
119
- } ;
113
+ }
120
114
121
- // Filters duplicate strings. Used to support functions in crypto and tls
122
- // modules. Implemented specifically to maintain existing behaviors in each.
123
- exports . filterDuplicateStrings = function filterDuplicateStrings ( items , low ) {
115
+ function filterDuplicateStrings ( items , low ) {
124
116
const map = new Map ( ) ;
125
117
for ( var i = 0 ; i < items . length ; i ++ ) {
126
118
const item = items [ i ] ;
@@ -132,24 +124,24 @@ exports.filterDuplicateStrings = function filterDuplicateStrings(items, low) {
132
124
}
133
125
}
134
126
return Array . from ( map . values ( ) ) . sort ( ) ;
135
- } ;
127
+ }
136
128
137
- exports . cachedResult = function cachedResult ( fn ) {
129
+ function cachedResult ( fn ) {
138
130
var result ;
139
131
return ( ) => {
140
132
if ( result === undefined )
141
133
result = fn ( ) ;
142
134
return result . slice ( ) ;
143
135
} ;
144
- } ;
136
+ }
145
137
146
138
// Useful for Wrapping an ES6 Class with a constructor Function that
147
139
// does not require the new keyword. For instance:
148
140
// class A { constructor(x) {this.x = x;}}
149
141
// const B = createClassWrapper(A);
150
142
// B() instanceof A // true
151
143
// B() instanceof B // true
152
- exports . createClassWrapper = function createClassWrapper ( type ) {
144
+ function createClassWrapper ( type ) {
153
145
const fn = function ( ...args ) {
154
146
return Reflect . construct ( type , args , new . target || type ) ;
155
147
} ;
@@ -161,7 +153,7 @@ exports.createClassWrapper = function createClassWrapper(type) {
161
153
Object . setPrototypeOf ( fn , type ) ;
162
154
fn . prototype = type . prototype ;
163
155
return fn ;
164
- } ;
156
+ }
165
157
166
158
let signalsToNamesMapping ;
167
159
function getSignalsToNamesMapping ( ) {
@@ -176,7 +168,7 @@ function getSignalsToNamesMapping() {
176
168
return signalsToNamesMapping ;
177
169
}
178
170
179
- exports . convertToValidSignal = function convertToValidSignal ( signal ) {
171
+ function convertToValidSignal ( signal ) {
180
172
if ( typeof signal === 'number' && getSignalsToNamesMapping ( ) [ signal ] )
181
173
return signal ;
182
174
@@ -186,4 +178,25 @@ exports.convertToValidSignal = function convertToValidSignal(signal) {
186
178
}
187
179
188
180
throw new Error ( 'Unknown signal: ' + signal ) ;
181
+ }
182
+
183
+ module . exports = exports = {
184
+ assertCrypto,
185
+ cachedResult,
186
+ convertToValidSignal,
187
+ createClassWrapper,
188
+ decorateErrorStack,
189
+ deprecate,
190
+ filterDuplicateStrings,
191
+ isError,
192
+ normalizeEncoding,
193
+ objectToString,
194
+
195
+ // Symbol used to provide a custom inspect function for an object as an
196
+ // alternative to using 'inspect'
197
+ customInspectSymbol : Symbol ( 'util.inspect.custom' ) ,
198
+
199
+ // Used by the buffer module to capture an internal reference to the
200
+ // default isEncoding implementation, just in case userland overrides it.
201
+ kIsEncodingSymbol : Symbol ( 'node.isEncoding' )
189
202
} ;
0 commit comments