|
3 | 3 | const prefix = `(${process.release.name}:${process.pid}) `; |
4 | 4 | const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; |
5 | 5 |
|
6 | | -exports.setup = setupProcessWarnings; |
7 | | - |
8 | 6 | let options; |
9 | 7 | function lazyOption(name) { |
10 | 8 | if (!options) { |
@@ -85,79 +83,80 @@ function doEmitWarning(warning) { |
85 | 83 | }; |
86 | 84 | } |
87 | 85 |
|
88 | | -function setupProcessWarnings() { |
89 | | - if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') { |
90 | | - process.on('warning', (warning) => { |
91 | | - if (!(warning instanceof Error)) return; |
92 | | - const isDeprecation = warning.name === 'DeprecationWarning'; |
93 | | - if (isDeprecation && process.noDeprecation) return; |
94 | | - const trace = process.traceProcessWarnings || |
95 | | - (isDeprecation && process.traceDeprecation); |
96 | | - var msg = prefix; |
97 | | - if (warning.code) |
98 | | - msg += `[${warning.code}] `; |
99 | | - if (trace && warning.stack) { |
100 | | - msg += `${warning.stack}`; |
101 | | - } else { |
102 | | - const toString = |
103 | | - typeof warning.toString === 'function' ? |
104 | | - warning.toString : Error.prototype.toString; |
105 | | - msg += `${toString.apply(warning)}`; |
106 | | - } |
107 | | - if (typeof warning.detail === 'string') { |
108 | | - msg += `\n${warning.detail}`; |
109 | | - } |
110 | | - output(msg); |
111 | | - }); |
| 86 | +function onWarning(warning) { |
| 87 | + if (!(warning instanceof Error)) return; |
| 88 | + const isDeprecation = warning.name === 'DeprecationWarning'; |
| 89 | + if (isDeprecation && process.noDeprecation) return; |
| 90 | + const trace = process.traceProcessWarnings || |
| 91 | + (isDeprecation && process.traceDeprecation); |
| 92 | + var msg = prefix; |
| 93 | + if (warning.code) |
| 94 | + msg += `[${warning.code}] `; |
| 95 | + if (trace && warning.stack) { |
| 96 | + msg += `${warning.stack}`; |
| 97 | + } else { |
| 98 | + const toString = |
| 99 | + typeof warning.toString === 'function' ? |
| 100 | + warning.toString : Error.prototype.toString; |
| 101 | + msg += `${toString.apply(warning)}`; |
112 | 102 | } |
| 103 | + if (typeof warning.detail === 'string') { |
| 104 | + msg += `\n${warning.detail}`; |
| 105 | + } |
| 106 | + output(msg); |
| 107 | +} |
113 | 108 |
|
114 | | - // process.emitWarning(error) |
115 | | - // process.emitWarning(str[, type[, code]][, ctor]) |
116 | | - // process.emitWarning(str[, options]) |
117 | | - process.emitWarning = (warning, type, code, ctor, now) => { |
118 | | - let detail; |
119 | | - if (type !== null && typeof type === 'object' && !Array.isArray(type)) { |
120 | | - ctor = type.ctor; |
121 | | - code = type.code; |
122 | | - if (typeof type.detail === 'string') |
123 | | - detail = type.detail; |
124 | | - type = type.type || 'Warning'; |
125 | | - } else if (typeof type === 'function') { |
126 | | - ctor = type; |
127 | | - code = undefined; |
128 | | - type = 'Warning'; |
129 | | - } |
130 | | - if (type !== undefined && typeof type !== 'string') { |
131 | | - throw new ERR_INVALID_ARG_TYPE('type', 'string', type); |
132 | | - } |
133 | | - if (typeof code === 'function') { |
134 | | - ctor = code; |
135 | | - code = undefined; |
136 | | - } else if (code !== undefined && typeof code !== 'string') { |
137 | | - throw new ERR_INVALID_ARG_TYPE('code', 'string', code); |
138 | | - } |
139 | | - if (typeof warning === 'string') { |
140 | | - // Improve error creation performance by skipping the error frames. |
141 | | - // They are added in the `captureStackTrace()` function below. |
142 | | - const tmpStackLimit = Error.stackTraceLimit; |
143 | | - Error.stackTraceLimit = 0; |
144 | | - // eslint-disable-next-line no-restricted-syntax |
145 | | - warning = new Error(warning); |
146 | | - Error.stackTraceLimit = tmpStackLimit; |
147 | | - warning.name = String(type || 'Warning'); |
148 | | - if (code !== undefined) warning.code = code; |
149 | | - if (detail !== undefined) warning.detail = detail; |
150 | | - Error.captureStackTrace(warning, ctor || process.emitWarning); |
151 | | - } else if (!(warning instanceof Error)) { |
152 | | - throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning); |
153 | | - } |
154 | | - if (warning.name === 'DeprecationWarning') { |
155 | | - if (process.noDeprecation) |
156 | | - return; |
157 | | - if (process.throwDeprecation) |
158 | | - throw warning; |
159 | | - } |
160 | | - if (now) process.emit('warning', warning); |
161 | | - else process.nextTick(doEmitWarning(warning)); |
162 | | - }; |
| 109 | +// process.emitWarning(error) |
| 110 | +// process.emitWarning(str[, type[, code]][, ctor]) |
| 111 | +// process.emitWarning(str[, options]) |
| 112 | +function emitWarning(warning, type, code, ctor, now) { |
| 113 | + let detail; |
| 114 | + if (type !== null && typeof type === 'object' && !Array.isArray(type)) { |
| 115 | + ctor = type.ctor; |
| 116 | + code = type.code; |
| 117 | + if (typeof type.detail === 'string') |
| 118 | + detail = type.detail; |
| 119 | + type = type.type || 'Warning'; |
| 120 | + } else if (typeof type === 'function') { |
| 121 | + ctor = type; |
| 122 | + code = undefined; |
| 123 | + type = 'Warning'; |
| 124 | + } |
| 125 | + if (type !== undefined && typeof type !== 'string') { |
| 126 | + throw new ERR_INVALID_ARG_TYPE('type', 'string', type); |
| 127 | + } |
| 128 | + if (typeof code === 'function') { |
| 129 | + ctor = code; |
| 130 | + code = undefined; |
| 131 | + } else if (code !== undefined && typeof code !== 'string') { |
| 132 | + throw new ERR_INVALID_ARG_TYPE('code', 'string', code); |
| 133 | + } |
| 134 | + if (typeof warning === 'string') { |
| 135 | + // Improve error creation performance by skipping the error frames. |
| 136 | + // They are added in the `captureStackTrace()` function below. |
| 137 | + const tmpStackLimit = Error.stackTraceLimit; |
| 138 | + Error.stackTraceLimit = 0; |
| 139 | + // eslint-disable-next-line no-restricted-syntax |
| 140 | + warning = new Error(warning); |
| 141 | + Error.stackTraceLimit = tmpStackLimit; |
| 142 | + warning.name = String(type || 'Warning'); |
| 143 | + if (code !== undefined) warning.code = code; |
| 144 | + if (detail !== undefined) warning.detail = detail; |
| 145 | + Error.captureStackTrace(warning, ctor || process.emitWarning); |
| 146 | + } else if (!(warning instanceof Error)) { |
| 147 | + throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning); |
| 148 | + } |
| 149 | + if (warning.name === 'DeprecationWarning') { |
| 150 | + if (process.noDeprecation) |
| 151 | + return; |
| 152 | + if (process.throwDeprecation) |
| 153 | + throw warning; |
| 154 | + } |
| 155 | + if (now) process.emit('warning', warning); |
| 156 | + else process.nextTick(doEmitWarning(warning)); |
163 | 157 | } |
| 158 | + |
| 159 | +module.exports = { |
| 160 | + onWarning, |
| 161 | + emitWarning |
| 162 | +}; |
0 commit comments