5
5
Error,
6
6
} = primordials ;
7
7
8
+ const assert = require ( 'internal/assert' ) ;
8
9
const { ERR_INVALID_ARG_TYPE } = require ( 'internal/errors' ) . codes ;
9
10
10
11
// Lazily loaded
@@ -87,7 +88,7 @@ function onWarning(warning) {
87
88
// process.emitWarning(error)
88
89
// process.emitWarning(str[, type[, code]][, ctor])
89
90
// process.emitWarning(str[, options])
90
- function emitWarning ( warning , type , code , ctor , now ) {
91
+ function emitWarning ( warning , type , code , ctor ) {
91
92
let detail ;
92
93
if ( type !== null && typeof type === 'object' && ! ArrayIsArray ( type ) ) {
93
94
ctor = type . ctor ;
@@ -110,18 +111,7 @@ function emitWarning(warning, type, code, ctor, now) {
110
111
throw new ERR_INVALID_ARG_TYPE ( 'code' , 'string' , code ) ;
111
112
}
112
113
if ( typeof warning === 'string' ) {
113
- // Improve error creation performance by skipping the error frames.
114
- // They are added in the `captureStackTrace()` function below.
115
- const tmpStackLimit = Error . stackTraceLimit ;
116
- Error . stackTraceLimit = 0 ;
117
- // eslint-disable-next-line no-restricted-syntax
118
- warning = new Error ( warning ) ;
119
- Error . stackTraceLimit = tmpStackLimit ;
120
- warning . name = String ( type || 'Warning' ) ;
121
- if ( code !== undefined ) warning . code = code ;
122
- if ( detail !== undefined ) warning . detail = detail ;
123
- // eslint-disable-next-line no-restricted-syntax
124
- Error . captureStackTrace ( warning , ctor || process . emitWarning ) ;
114
+ warning = createWarningObject ( warning , type , code , ctor , detail ) ;
125
115
} else if ( ! ( warning instanceof Error ) ) {
126
116
throw new ERR_INVALID_ARG_TYPE ( 'warning' , [ 'Error' , 'string' ] , warning ) ;
127
117
}
@@ -131,11 +121,32 @@ function emitWarning(warning, type, code, ctor, now) {
131
121
if ( process . throwDeprecation )
132
122
throw warning ;
133
123
}
134
- if ( now ) process . emit ( 'warning' , warning ) ;
135
- else process . nextTick ( doEmitWarning ( warning ) ) ;
124
+ process . nextTick ( doEmitWarning ( warning ) ) ;
125
+ }
126
+
127
+ function emitWarningSync ( warning ) {
128
+ process . emit ( 'warning' , createWarningObject ( warning ) ) ;
129
+ }
130
+
131
+ function createWarningObject ( warning , type , code , ctor , detail ) {
132
+ assert ( typeof warning === 'string' ) ;
133
+ // Improve error creation performance by skipping the error frames.
134
+ // They are added in the `captureStackTrace()` function below.
135
+ const tmpStackLimit = Error . stackTraceLimit ;
136
+ Error . stackTraceLimit = 0 ;
137
+ // eslint-disable-next-line no-restricted-syntax
138
+ warning = new Error ( warning ) ;
139
+ Error . stackTraceLimit = tmpStackLimit ;
140
+ warning . name = String ( type || 'Warning' ) ;
141
+ if ( code !== undefined ) warning . code = code ;
142
+ if ( detail !== undefined ) warning . detail = detail ;
143
+ // eslint-disable-next-line no-restricted-syntax
144
+ Error . captureStackTrace ( warning , ctor || process . emitWarning ) ;
145
+ return warning ;
136
146
}
137
147
138
148
module . exports = {
149
+ emitWarning,
150
+ emitWarningSync,
139
151
onWarning,
140
- emitWarning
141
152
} ;
0 commit comments