|
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 | // Lazily loaded |
9 | 7 | let fs; |
10 | 8 | let fd; |
@@ -51,83 +49,84 @@ function doEmitWarning(warning) { |
51 | 49 | return () => process.emit('warning', warning); |
52 | 50 | } |
53 | 51 |
|
54 | | -function setupProcessWarnings() { |
55 | | - if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') { |
56 | | - process.on('warning', (warning) => { |
57 | | - if (!(warning instanceof Error)) return; |
58 | | - const isDeprecation = warning.name === 'DeprecationWarning'; |
59 | | - if (isDeprecation && process.noDeprecation) return; |
60 | | - const trace = process.traceProcessWarnings || |
61 | | - (isDeprecation && process.traceDeprecation); |
62 | | - var msg = prefix; |
63 | | - if (warning.code) |
64 | | - msg += `[${warning.code}] `; |
65 | | - if (trace && warning.stack) { |
66 | | - msg += `${warning.stack}`; |
67 | | - } else { |
68 | | - const toString = |
69 | | - typeof warning.toString === 'function' ? |
70 | | - warning.toString : Error.prototype.toString; |
71 | | - msg += `${toString.apply(warning)}`; |
72 | | - } |
73 | | - if (typeof warning.detail === 'string') { |
74 | | - msg += `\n${warning.detail}`; |
75 | | - } |
76 | | - const warningFile = lazyOption(); |
77 | | - if (warningFile) { |
78 | | - return writeToFile(msg); |
79 | | - } |
80 | | - writeOut(msg); |
81 | | - }); |
| 52 | +function onWarning(warning) { |
| 53 | + if (!(warning instanceof Error)) return; |
| 54 | + const isDeprecation = warning.name === 'DeprecationWarning'; |
| 55 | + if (isDeprecation && process.noDeprecation) return; |
| 56 | + const trace = process.traceProcessWarnings || |
| 57 | + (isDeprecation && process.traceDeprecation); |
| 58 | + var msg = prefix; |
| 59 | + if (warning.code) |
| 60 | + msg += `[${warning.code}] `; |
| 61 | + if (trace && warning.stack) { |
| 62 | + msg += `${warning.stack}`; |
| 63 | + } else { |
| 64 | + const toString = |
| 65 | + typeof warning.toString === 'function' ? |
| 66 | + warning.toString : Error.prototype.toString; |
| 67 | + msg += `${toString.apply(warning)}`; |
| 68 | + } |
| 69 | + if (typeof warning.detail === 'string') { |
| 70 | + msg += `\n${warning.detail}`; |
| 71 | + } |
| 72 | + const warningFile = lazyOption(); |
| 73 | + if (warningFile) { |
| 74 | + return writeToFile(msg); |
82 | 75 | } |
| 76 | + writeOut(msg); |
| 77 | +} |
83 | 78 |
|
84 | | - // process.emitWarning(error) |
85 | | - // process.emitWarning(str[, type[, code]][, ctor]) |
86 | | - // process.emitWarning(str[, options]) |
87 | | - process.emitWarning = (warning, type, code, ctor, now) => { |
88 | | - let detail; |
89 | | - if (type !== null && typeof type === 'object' && !Array.isArray(type)) { |
90 | | - ctor = type.ctor; |
91 | | - code = type.code; |
92 | | - if (typeof type.detail === 'string') |
93 | | - detail = type.detail; |
94 | | - type = type.type || 'Warning'; |
95 | | - } else if (typeof type === 'function') { |
96 | | - ctor = type; |
97 | | - code = undefined; |
98 | | - type = 'Warning'; |
99 | | - } |
100 | | - if (type !== undefined && typeof type !== 'string') { |
101 | | - throw new ERR_INVALID_ARG_TYPE('type', 'string', type); |
102 | | - } |
103 | | - if (typeof code === 'function') { |
104 | | - ctor = code; |
105 | | - code = undefined; |
106 | | - } else if (code !== undefined && typeof code !== 'string') { |
107 | | - throw new ERR_INVALID_ARG_TYPE('code', 'string', code); |
108 | | - } |
109 | | - if (typeof warning === 'string') { |
110 | | - // Improve error creation performance by skipping the error frames. |
111 | | - // They are added in the `captureStackTrace()` function below. |
112 | | - const tmpStackLimit = Error.stackTraceLimit; |
113 | | - Error.stackTraceLimit = 0; |
114 | | - // eslint-disable-next-line no-restricted-syntax |
115 | | - warning = new Error(warning); |
116 | | - Error.stackTraceLimit = tmpStackLimit; |
117 | | - warning.name = String(type || 'Warning'); |
118 | | - if (code !== undefined) warning.code = code; |
119 | | - if (detail !== undefined) warning.detail = detail; |
120 | | - Error.captureStackTrace(warning, ctor || process.emitWarning); |
121 | | - } else if (!(warning instanceof Error)) { |
122 | | - throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning); |
123 | | - } |
124 | | - if (warning.name === 'DeprecationWarning') { |
125 | | - if (process.noDeprecation) |
126 | | - return; |
127 | | - if (process.throwDeprecation) |
128 | | - throw warning; |
129 | | - } |
130 | | - if (now) process.emit('warning', warning); |
131 | | - else process.nextTick(doEmitWarning(warning)); |
132 | | - }; |
| 79 | +// process.emitWarning(error) |
| 80 | +// process.emitWarning(str[, type[, code]][, ctor]) |
| 81 | +// process.emitWarning(str[, options]) |
| 82 | +function emitWarning(warning, type, code, ctor, now) { |
| 83 | + let detail; |
| 84 | + if (type !== null && typeof type === 'object' && !Array.isArray(type)) { |
| 85 | + ctor = type.ctor; |
| 86 | + code = type.code; |
| 87 | + if (typeof type.detail === 'string') |
| 88 | + detail = type.detail; |
| 89 | + type = type.type || 'Warning'; |
| 90 | + } else if (typeof type === 'function') { |
| 91 | + ctor = type; |
| 92 | + code = undefined; |
| 93 | + type = 'Warning'; |
| 94 | + } |
| 95 | + if (type !== undefined && typeof type !== 'string') { |
| 96 | + throw new ERR_INVALID_ARG_TYPE('type', 'string', type); |
| 97 | + } |
| 98 | + if (typeof code === 'function') { |
| 99 | + ctor = code; |
| 100 | + code = undefined; |
| 101 | + } else if (code !== undefined && typeof code !== 'string') { |
| 102 | + throw new ERR_INVALID_ARG_TYPE('code', 'string', code); |
| 103 | + } |
| 104 | + if (typeof warning === 'string') { |
| 105 | + // Improve error creation performance by skipping the error frames. |
| 106 | + // They are added in the `captureStackTrace()` function below. |
| 107 | + const tmpStackLimit = Error.stackTraceLimit; |
| 108 | + Error.stackTraceLimit = 0; |
| 109 | + // eslint-disable-next-line no-restricted-syntax |
| 110 | + warning = new Error(warning); |
| 111 | + Error.stackTraceLimit = tmpStackLimit; |
| 112 | + warning.name = String(type || 'Warning'); |
| 113 | + if (code !== undefined) warning.code = code; |
| 114 | + if (detail !== undefined) warning.detail = detail; |
| 115 | + Error.captureStackTrace(warning, ctor || process.emitWarning); |
| 116 | + } else if (!(warning instanceof Error)) { |
| 117 | + throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning); |
| 118 | + } |
| 119 | + if (warning.name === 'DeprecationWarning') { |
| 120 | + if (process.noDeprecation) |
| 121 | + return; |
| 122 | + if (process.throwDeprecation) |
| 123 | + throw warning; |
| 124 | + } |
| 125 | + if (now) process.emit('warning', warning); |
| 126 | + else process.nextTick(doEmitWarning(warning)); |
133 | 127 | } |
| 128 | + |
| 129 | +module.exports = { |
| 130 | + onWarning, |
| 131 | + emitWarning |
| 132 | +}; |
0 commit comments