Skip to content

Commit 9cd44bb

Browse files
thefourtheyesilverwind
authored andcommitted
util: prepend '(node) ' to deprecation messages
Changes included in this commit are 1. Making the deprecation messages consistent. The messages will be in the following format x is deprecated. Use y instead. If there is no alternative for `x`, then the ` Use y instead.` part will not be there in the message. 2. All the internal deprecation messages are printed with the prefix `(node) `, except when the `--trace-deprecation` flag is set. Fixes: #1883 PR-URL: #1892 Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent d55a778 commit 9cd44bb

16 files changed

+116
-64
lines changed

lib/_http_outgoing.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const assert = require('assert').ok;
44
const Stream = require('stream');
55
const timers = require('timers');
66
const util = require('util');
7+
const internalUtil = require('internal/util');
78
const Buffer = require('buffer').Buffer;
89
const common = require('_http_common');
910

@@ -644,6 +645,6 @@ OutgoingMessage.prototype.flushHeaders = function() {
644645
this._send('');
645646
};
646647

647-
OutgoingMessage.prototype.flush = util.deprecate(function() {
648+
OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
648649
this.flushHeaders();
649-
}, 'flush is deprecated. Use flushHeaders instead.');
650+
}, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.');

lib/_stream_writable.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = Writable;
88
Writable.WritableState = WritableState;
99

1010
const util = require('util');
11+
const internalUtil = require('internal/util');
1112
const Stream = require('stream');
1213
const Buffer = require('buffer').Buffer;
1314

@@ -120,10 +121,10 @@ WritableState.prototype.getBuffer = function writableStateGetBuffer() {
120121
};
121122

122123
Object.defineProperty(WritableState.prototype, 'buffer', {
123-
get: util.deprecate(function() {
124+
get: internalUtil.deprecate(function() {
124125
return this.getBuffer();
125-
}, '_writableState.buffer is deprecated. Use ' +
126-
'_writableState.getBuffer() instead.')
126+
}, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' +
127+
'instead.')
127128
});
128129

129130
function Writable(options) {

lib/buffer.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ Buffer.prototype.get = internalUtil.deprecate(function get(offset) {
468468
if (offset < 0 || offset >= this.length)
469469
throw new RangeError('index out of range');
470470
return this[offset];
471-
}, '.get() is deprecated. Access using array indexes instead.');
471+
}, 'Buffer.get is deprecated. Use array indexes instead.');
472472

473473

474474
// XXX remove in v0.13
@@ -477,14 +477,15 @@ Buffer.prototype.set = internalUtil.deprecate(function set(offset, v) {
477477
if (offset < 0 || offset >= this.length)
478478
throw new RangeError('index out of range');
479479
return this[offset] = v;
480-
}, '.set() is deprecated. Set using array indexes instead.');
480+
}, 'Buffer.set is deprecated. Use array indexes instead.');
481481

482482

483483
// TODO(trevnorris): fix these checks to follow new standard
484484
// write(string, offset = 0, length = buffer.length, encoding = 'utf8')
485485
var writeWarned = false;
486-
const writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
487-
' Use write(string[, offset[, length]][, encoding]) instead.';
486+
const writeMsg = 'Buffer.write(string, encoding, offset, length) is ' +
487+
'deprecated. Use write(string[, offset[, length]]' +
488+
'[, encoding]) instead.';
488489
Buffer.prototype.write = function(string, offset, length, encoding) {
489490
// Buffer#write(string);
490491
if (offset === undefined) {

lib/child_process.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const util = require('util');
4+
const internalUtil = require('internal/util');
45
const debug = util.debuglog('child_process');
56
const constants = require('constants');
67

@@ -269,11 +270,12 @@ exports.execFile = function(file /* args, options, callback */) {
269270
return child;
270271
};
271272

272-
var _deprecatedCustomFds = util.deprecate(function(options) {
273+
var _deprecatedCustomFds = internalUtil.deprecate(function(options) {
273274
options.stdio = options.customFds.map(function(fd) {
274275
return fd === -1 ? 'pipe' : fd;
275276
});
276-
}, 'child_process: customFds option is deprecated, use stdio instead.');
277+
}, 'child_process: options.customFds option is deprecated. ' +
278+
'Use options.stdio instead.');
277279

278280
function _convertCustomFds(options) {
279281
if (options && options.customFds && !options.stdio) {

lib/crypto.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Buffer = require('buffer').Buffer;
1919
const constants = require('constants');
2020
const stream = require('stream');
2121
const util = require('util');
22+
const internalUtil = require('internal/util');
2223

2324
const DH_GENERATOR = 2;
2425

@@ -682,10 +683,13 @@ function filterDuplicates(names) {
682683
}
683684

684685
// Legacy API
685-
exports.__defineGetter__('createCredentials', util.deprecate(function() {
686-
return require('tls').createSecureContext;
687-
}, 'createCredentials() is deprecated, use tls.createSecureContext instead'));
686+
exports.__defineGetter__('createCredentials',
687+
internalUtil.deprecate(function() {
688+
return require('tls').createSecureContext;
689+
}, 'crypto.createCredentials is deprecated. ' +
690+
'Use tls.createSecureContext instead.'));
688691

689-
exports.__defineGetter__('Credentials', util.deprecate(function() {
692+
exports.__defineGetter__('Credentials', internalUtil.deprecate(function() {
690693
return require('tls').SecureContext;
691-
}, 'Credentials is deprecated, use tls.createSecureContext instead'));
694+
}, 'crypto.Credentials is deprecated. ' +
695+
'Use tls.createSecureContext instead.'));

lib/http.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const util = require('util');
4+
const internalUtil = require('internal/util');
45
const EventEmitter = require('events').EventEmitter;
56

67

@@ -91,9 +92,8 @@ Client.prototype.request = function(method, path, headers) {
9192
return c;
9293
};
9394

94-
exports.Client = util.deprecate(Client,
95-
'http.Client will be removed soon. Do not use it.');
95+
exports.Client = internalUtil.deprecate(Client, 'http.Client is deprecated.');
9696

97-
exports.createClient = util.deprecate(function(port, host) {
97+
exports.createClient = internalUtil.deprecate(function(port, host) {
9898
return new Client(port, host);
99-
}, 'http.createClient is deprecated. Use `http.request` instead.');
99+
}, 'http.createClient is deprecated. Use http.request instead.');

lib/internal/util.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
'use strict';
22

3+
const prefix = '(node) ';
4+
5+
// All the internal deprecations have to use this function only, as this will
6+
// prepend the prefix to the actual message.
7+
exports.deprecate = function(fn, msg) {
8+
return exports._deprecate(fn, `${prefix}${msg}`);
9+
};
10+
11+
// All the internal deprecations have to use this function only, as this will
12+
// prepend the prefix to the actual message.
313
exports.printDeprecationMessage = function(msg, warned) {
14+
return exports._printDeprecationMessage(`${prefix}${msg}`, warned);
15+
};
16+
17+
exports._printDeprecationMessage = function(msg, warned) {
418
if (process.noDeprecation)
519
return true;
620

@@ -10,7 +24,7 @@ exports.printDeprecationMessage = function(msg, warned) {
1024
if (process.throwDeprecation)
1125
throw new Error(msg);
1226
else if (process.traceDeprecation)
13-
console.trace(msg);
27+
console.trace(msg.startsWith(prefix) ? msg.replace(prefix, '') : msg);
1428
else
1529
console.error(msg);
1630

@@ -20,11 +34,11 @@ exports.printDeprecationMessage = function(msg, warned) {
2034
// Mark that a method should not be used.
2135
// Returns a modified function which warns once by default.
2236
// If --no-deprecation is set, then it is a no-op.
23-
exports.deprecate = function(fn, msg) {
37+
exports._deprecate = function(fn, msg) {
2438
// Allow for deprecating things in the process of starting up.
2539
if (global.process === undefined) {
2640
return function() {
27-
return exports.deprecate(fn, msg).apply(this, arguments);
41+
return exports._deprecate(fn, msg).apply(this, arguments);
2842
};
2943
}
3044

@@ -34,7 +48,7 @@ exports.deprecate = function(fn, msg) {
3448

3549
var warned = false;
3650
function deprecated() {
37-
warned = exports.printDeprecationMessage(msg, warned);
51+
warned = exports._printDeprecationMessage(msg, warned);
3852
return fn.apply(this, arguments);
3953
}
4054

lib/module.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const NativeModule = require('native_module');
44
const util = require('util');
5+
const internalUtil = require('internal/util');
56
const runInThisContext = require('vm').runInThisContext;
67
const assert = require('assert').ok;
78
const fs = require('fs');
@@ -120,12 +121,7 @@ function tryExtensions(p, exts) {
120121
return false;
121122
}
122123

123-
124-
const noopDeprecateRequireDot = util.deprecate(function() {},
125-
'warning: require(\'.\') resolved outside the package directory. ' +
126-
'This functionality is deprecated and will be removed soon.');
127-
128-
124+
var warned = false;
129125
Module._findPath = function(request, paths) {
130126
var exts = Object.keys(Module._extensions);
131127

@@ -172,7 +168,13 @@ Module._findPath = function(request, paths) {
172168

173169
if (filename) {
174170
// Warn once if '.' resolved outside the module dir
175-
if (request === '.' && i > 0) noopDeprecateRequireDot();
171+
if (request === '.' && i > 0) {
172+
warned = internalUtil.printDeprecationMessage(
173+
'warning: require(\'.\') resolved outside the package ' +
174+
'directory. This functionality is deprecated and will be removed ' +
175+
'soon.', warned);
176+
}
177+
176178
Module._pathCache[cacheKey] = filename;
177179
return filename;
178180
}

lib/net.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const events = require('events');
44
const stream = require('stream');
55
const timers = require('timers');
66
const util = require('util');
7+
const internalUtil = require('internal/util');
78
const assert = require('assert');
89
const cares = process.binding('cares_wrap');
910
const uv = process.binding('uv');
@@ -1077,16 +1078,17 @@ function Server(options, connectionListener) {
10771078
this._connections = 0;
10781079

10791080
Object.defineProperty(this, 'connections', {
1080-
get: util.deprecate(function() {
1081+
get: internalUtil.deprecate(function() {
10811082

10821083
if (self._usingSlaves) {
10831084
return null;
10841085
}
10851086
return self._connections;
1086-
}, 'connections property is deprecated. Use getConnections() method'),
1087-
set: util.deprecate(function(val) {
1087+
}, 'Server.connections property is deprecated. ' +
1088+
'Use Server.getConnections method instead.'),
1089+
set: internalUtil.deprecate(function(val) {
10881090
return (self._connections = val);
1089-
}, 'connections property is deprecated. Use getConnections() method'),
1091+
}, 'Server.connections property is deprecated.'),
10901092
configurable: true, enumerable: false
10911093
});
10921094

@@ -1498,9 +1500,9 @@ function emitCloseNT(self) {
14981500
}
14991501

15001502

1501-
Server.prototype.listenFD = util.deprecate(function(fd, type) {
1503+
Server.prototype.listenFD = internalUtil.deprecate(function(fd, type) {
15021504
return this.listen({ fd: fd });
1503-
}, 'listenFD is deprecated. Use listen({fd: <number>}).');
1505+
}, 'Server.listenFD is deprecated. Use Server.listen({fd: <number>}) instead.');
15041506

15051507
Server.prototype._setupSlave = function(socketList) {
15061508
this._usingSlaves = true;

lib/os.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const binding = process.binding('os');
44
const util = require('util');
5+
const internalUtil = require('internal/util');
56
const isWindows = process.platform === 'win32';
67

78
exports.hostname = binding.getHostname;
@@ -46,9 +47,10 @@ exports.tmpdir = function() {
4647

4748
exports.tmpDir = exports.tmpdir;
4849

49-
exports.getNetworkInterfaces = util.deprecate(function() {
50+
exports.getNetworkInterfaces = internalUtil.deprecate(function() {
5051
return exports.networkInterfaces();
51-
}, 'getNetworkInterfaces is now called `os.networkInterfaces`.');
52+
}, 'os.getNetworkInterfaces is deprecated. ' +
53+
'Use os.networkInterfaces instead.');
5254

5355
exports.EOL = isWindows ? '\r\n' : '\n';
5456

lib/readline.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
const kHistorySize = 30;
1010

1111
const util = require('util');
12+
const internalUtil = require('internal/util');
1213
const inherits = util.inherits;
1314
const Buffer = require('buffer').Buffer;
1415
const EventEmitter = require('events').EventEmitter;
@@ -1417,8 +1418,9 @@ function codePointAt(str, index) {
14171418
}
14181419
return code;
14191420
}
1420-
exports.codePointAt = util.deprecate(codePointAt,
1421-
'codePointAt() is deprecated. Use String.prototype.codePointAt');
1421+
exports.codePointAt = internalUtil.deprecate(codePointAt,
1422+
'readline.codePointAt is deprecated. ' +
1423+
'Use String.prototype.codePointAt instead.');
14221424

14231425

14241426
/**

lib/smalloc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
const util = require('internal/util');
44

55
module.exports = require('internal/smalloc');
6-
util.printDeprecationMessage('smalloc is deprecated.');
6+
util.printDeprecationMessage('smalloc is deprecated. ' +
7+
'Use typed arrays instead.');

lib/tty.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const util = require('util');
4+
const internalUtil = require('internal/util');
45
const net = require('net');
56
const TTY = process.binding('tty_wrap').TTY;
67
const isTTY = process.binding('tty_wrap').isTTY;
@@ -14,12 +15,13 @@ exports.isatty = function(fd) {
1415

1516

1617
// backwards-compat
17-
exports.setRawMode = util.deprecate(function(flag) {
18+
exports.setRawMode = internalUtil.deprecate(function(flag) {
1819
if (!process.stdin.isTTY) {
1920
throw new Error('can\'t set raw mode on non-tty');
2021
}
2122
process.stdin.setRawMode(flag);
22-
}, 'tty.setRawMode: Use `process.stdin.setRawMode()` instead.');
23+
}, 'tty.setRawMode is deprecated. ' +
24+
'Use process.stdin.setRawMode instead.');
2325

2426

2527
function ReadStream(fd, options) {

0 commit comments

Comments
 (0)