Skip to content

Commit 5b39929

Browse files
committed
Add --no-deprecation and --trace-deprecation flags
1 parent 260695a commit 5b39929

File tree

11 files changed

+153
-76
lines changed

11 files changed

+153
-76
lines changed

doc/node.1

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ focused on creating simple, easy to build network clients
4141
and servers.
4242

4343

44+
.SH OPTIONS
45+
46+
-v, --version print node's version
47+
48+
-e, --eval script evaluate script
49+
50+
-p, --print print result of --eval
51+
52+
-i, --interactive always enter the REPL even if stdin
53+
does not appear to be a terminal
54+
55+
--no-deprecation silence deprecation warnings
56+
57+
--trace-deprecation show stack traces on deprecations
58+
59+
--v8-options print v8 command line options
60+
61+
--max-stack-size=val set max v8 stack size (bytes)
62+
63+
4464
.SH ENVIRONMENT VARIABLES
4565

4666
.IP NODE_PATH

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ fs.watchFile = function(filename) {
919919
// a little on the slow side but let's stick with it for now to keep
920920
// behavioral changes to a minimum.
921921
interval: 5007,
922-
persistent: true,
922+
persistent: true
923923
};
924924

925925
if ('object' == typeof arguments[1]) {

lib/http.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1831,12 +1831,9 @@ Client.prototype.request = function(method, path, headers) {
18311831
return c;
18321832
};
18331833

1834-
exports.Client = Client;
1834+
exports.Client = util.deprecate(Client,
1835+
'http.Client will be removed soon. Do not use it.');
18351836

1836-
// TODO http.Client can be removed in v0.9. Until then leave this message.
1837-
module.deprecate('Client', 'It will be removed soon. Do not use it.');
1838-
1839-
exports.createClient = function(port, host) {
1837+
exports.createClient = util.deprecate(function(port, host) {
18401838
return new Client(port, host);
1841-
};
1842-
module.deprecate('createClient', 'Use `http.request` instead.');
1839+
}, 'http.createClient is deprecated. Use `http.request` instead.');

lib/net.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,7 @@ function Socket(options) {
133133

134134
if (typeof options == 'number') {
135135
// Legacy interface.
136-
// Must support legacy interface. Old versions of npm depend on it.
137-
// https://github.com/isaacs/npm/blob/c7824f412f0cb59d6f55cf0bc220253c39e6029f/lib/utils/output.js#L110
138136
var fd = options;
139-
140-
// Uncomment the following lines after libuv backend is stable and API
141-
// compatibile with legaacy.
142-
// console.error('Deprecated interface net.Socket(fd).');
143-
// console.trace();
144137
this._handle = createPipe();
145138
this._handle.open(fd);
146139
this.readable = this.writable = true;
@@ -1092,14 +1085,9 @@ Server.prototype._emitCloseIfDrained = function() {
10921085
};
10931086

10941087

1095-
var listenFDwarn = false;
1096-
Server.prototype.listenFD = function(fd, type) {
1097-
if (!listenFDwarn) {
1098-
console.error('listenFD is deprecated. Use server.listen()');
1099-
listenFDwarn = true;
1100-
}
1101-
this.listen({ fd: fd });
1102-
};
1088+
Server.prototype.listenFD = util.deprecate(function(fd, type) {
1089+
return this.listen({ fd: fd });
1090+
}, 'listenFD is deprecated. Use listen({fd: <number>}).');
11031091

11041092
// when sending a socket using fork IPC this function is executed
11051093
Server.prototype._setupSlave = function(socketList) {

lib/os.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
var binding = process.binding('os');
23+
var util = require('util');
2324

2425
exports.hostname = binding.getHostname;
2526
exports.loadavg = binding.getLoadAvg;
@@ -46,10 +47,8 @@ exports.tmpDir = function() {
4647
(process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp');
4748
};
4849

49-
exports.getNetworkInterfaces = function() {
50+
exports.getNetworkInterfaces = util.deprecate(function() {
5051
return exports.networkInterfaces();
51-
};
52-
module.deprecate('getNetworkInterfaces',
53-
'It is now called `os.networkInterfaces`.');
52+
}, 'getNetworkInterfaces is now called `os.networkInterfaces`.');
5453

5554
exports.EOL = process.platform === 'win32' ? '\r\n' : '\n';

lib/path.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
var isWindows = process.platform === 'win32';
24-
var _deprecationWarning = require('util')._deprecationWarning;
24+
var util = require('util');
2525

2626

2727
// resolves . and .. elements in a path array with directory names there
@@ -415,16 +415,14 @@ exports.extname = function(path) {
415415
};
416416

417417

418-
exports.exists = function(path, callback) {
418+
exports.exists = util.deprecate(function(path, callback) {
419419
require('fs').exists(path, callback);
420-
};
421-
module.deprecate('exists', 'It is now called `fs.exists`.');
420+
}, 'path.exists is now called `fs.exists`.');
422421

423422

424-
exports.existsSync = function(path) {
423+
exports.existsSync = util.deprecate(function(path) {
425424
return require('fs').existsSync(path);
426-
};
427-
module.deprecate('existsSync', 'It is now called `fs.existsSync`.');
425+
}, 'path.existsSync is now called `fs.existsSync`.');
428426

429427

430428
if (isWindows) {

lib/tty.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ var inherits = require('util').inherits;
2424
var net = require('net');
2525
var TTY = process.binding('tty_wrap').TTY;
2626
var isTTY = process.binding('tty_wrap').isTTY;
27+
var util = require('util');
2728

2829
exports.isatty = function(fd) {
2930
return isTTY(fd);
3031
};
3132

3233

3334
// backwards-compat
34-
exports.setRawMode = function(flag) {
35+
exports.setRawMode = util.deprecate(function(flag) {
3536
if (!process.stdin.isTTY) {
3637
throw new Error('can\'t set raw mode on non-tty');
3738
}
3839
process.stdin.setRawMode(flag);
39-
};
40-
module.deprecate('setRawMode', 'Use `process.stdin.setRawMode()` instead.');
40+
}, 'tty.setRawMode: Use `process.stdin.setRawMode()` instead.');
4141

4242

4343
function ReadStream(fd) {

lib/util.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ exports.format = function(f) {
5454
};
5555

5656

57+
// Mark that a method should not be used.
58+
// Returns a modified function which warns once by default.
59+
// If --no-deprecation is set, then it is a no-op.
60+
exports.deprecate = function(fn, msg) {
61+
if (process.noDeprecation === true) {
62+
return fn;
63+
}
64+
65+
var warned = false;
66+
function deprecated() {
67+
if (!warned) {
68+
if (process.traceDeprecation) {
69+
console.trace(msg);
70+
} else {
71+
console.error(msg);
72+
}
73+
warned = true;
74+
}
75+
return fn.apply(this, arguments);
76+
}
77+
78+
return deprecated;
79+
};
80+
81+
5782
exports.print = function() {
5883
for (var i = 0, len = arguments.length; i < len; ++i) {
5984
process.stdout.write(String(arguments[i]));
@@ -407,12 +432,11 @@ function objectToString(o) {
407432
}
408433

409434

410-
exports.p = function() {
435+
exports.p = exports.deprecate(function() {
411436
for (var i = 0, len = arguments.length; i < len; ++i) {
412437
error(exports.inspect(arguments[i]));
413438
}
414-
};
415-
module.deprecate('p', 'Use `util.puts(util.inspect())` instead.');
439+
}, 'util.p: Use console.error() instead.');
416440

417441

418442
function pad(n) {
@@ -438,10 +462,9 @@ exports.log = function(msg) {
438462
};
439463

440464

441-
exports.exec = function() {
465+
exports.exec = exports.deprecate(function() {
442466
return require('child_process').exec.apply(this, arguments);
443-
};
444-
module.deprecate('exec', 'It is now called `child_process.exec`.');
467+
}, 'util.exec is now called `child_process.exec`.');
445468

446469

447470
exports.pump = function(readStream, writeStream, callback) {

src/node.cc

+28-4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ static Persistent<String> disposed_symbol;
120120

121121
static bool print_eval = false;
122122
static bool force_repl = false;
123+
static bool no_deprecation = false;
124+
static bool trace_deprecation = false;
123125
static char *eval_string = NULL;
124126
static int option_end_index = 0;
125127
static bool use_debug_agent = false;
@@ -1094,12 +1096,16 @@ enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
10941096
} else if (strcasecmp(*encoding, "hex") == 0) {
10951097
return HEX;
10961098
} else if (strcasecmp(*encoding, "raw") == 0) {
1097-
fprintf(stderr, "'raw' (array of integers) has been removed. "
1098-
"Use 'binary'.\n");
1099+
if (!no_deprecation) {
1100+
fprintf(stderr, "'raw' (array of integers) has been removed. "
1101+
"Use 'binary'.\n");
1102+
}
10991103
return BINARY;
11001104
} else if (strcasecmp(*encoding, "raws") == 0) {
1101-
fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
1102-
"Please update your code.\n");
1105+
if (!no_deprecation) {
1106+
fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
1107+
"Please update your code.\n");
1108+
}
11031109
return BINARY;
11041110
} else {
11051111
return _default;
@@ -2224,6 +2230,16 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
22242230
process->Set(String::NewSymbol("_forceRepl"), True());
22252231
}
22262232

2233+
// --no-deprecation
2234+
if (no_deprecation) {
2235+
process->Set(String::NewSymbol("noDeprecation"), True());
2236+
}
2237+
2238+
// --trace-deprecation
2239+
if (trace_deprecation) {
2240+
process->Set(String::NewSymbol("traceDeprecation"), True());
2241+
}
2242+
22272243
size_t size = 2*PATH_MAX;
22282244
char* execPath = new char[size];
22292245
if (uv_exepath(execPath, &size) != 0) {
@@ -2371,6 +2387,8 @@ static void PrintHelp() {
23712387
" -p, --print print result of --eval\n"
23722388
" -i, --interactive always enter the REPL even if stdin\n"
23732389
" does not appear to be a terminal\n"
2390+
" --no-deprecation silence deprecation warnings\n"
2391+
" --trace-deprecation show stack traces on deprecations\n"
23742392
" --v8-options print v8 command line options\n"
23752393
" --max-stack-size=val set max v8 stack size (bytes)\n"
23762394
"\n"
@@ -2428,6 +2446,12 @@ static void ParseArgs(int argc, char **argv) {
24282446
argv[i] = const_cast<char*>("");
24292447
} else if (strcmp(arg, "--v8-options") == 0) {
24302448
argv[i] = const_cast<char*>("--help");
2449+
} else if (strcmp(arg, "--no-deprecation") == 0) {
2450+
argv[i] = const_cast<char*>("");
2451+
no_deprecation = true;
2452+
} else if (strcmp(arg, "--trace-deprecation") == 0) {
2453+
argv[i] = const_cast<char*>("");
2454+
trace_deprecation = true;
24312455
} else if (argv[i][0] != '-') {
24322456
break;
24332457
}

src/node.js

-29
Original file line numberDiff line numberDiff line change
@@ -600,34 +600,5 @@
600600
NativeModule._cache[this.id] = this;
601601
};
602602

603-
// Wrap a core module's method in a wrapper that will warn on first use
604-
// and then return the result of invoking the original function. After
605-
// first being called the original method is restored.
606-
NativeModule.prototype.deprecate = function(method, message) {
607-
var original = this.exports[method];
608-
var self = this;
609-
var warned = false;
610-
message = message || '';
611-
612-
Object.defineProperty(this.exports, method, {
613-
enumerable: false,
614-
value: function() {
615-
if (!warned) {
616-
warned = true;
617-
message = self.id + '.' + method + ' is deprecated. ' + message;
618-
619-
var moduleIdCheck = new RegExp('\\b' + self.id + '\\b');
620-
if (moduleIdCheck.test(process.env.NODE_DEBUG))
621-
console.trace(message);
622-
else
623-
console.error(message);
624-
625-
self.exports[method] = original;
626-
}
627-
return original.apply(this, arguments);
628-
}
629-
});
630-
};
631-
632603
startup();
633604
});

test/simple/test-deprecation-flags.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var execFile = require('child_process').execFile;
25+
var depmod = require.resolve('../fixtures/deprecated.js');
26+
var node = process.execPath;
27+
28+
var normal = [depmod];
29+
var noDep = ['--no-deprecation', depmod];
30+
var traceDep = ['--trace-deprecation', depmod];
31+
32+
execFile(node, normal, function(er, stdout, stderr) {
33+
console.error('normal: show deprecation warning');
34+
assert.equal(er, null);
35+
assert.equal(stdout, '');
36+
assert.equal(stderr, 'util.p: Use console.error() instead.\n\'This is deprecated\'\n');
37+
console.log('normal ok');
38+
});
39+
40+
execFile(node, noDep, function(er, stdout, stderr) {
41+
console.error('--no-deprecation: silence deprecations');
42+
assert.equal(er, null);
43+
assert.equal(stdout, '');
44+
assert.equal(stderr, '\'This is deprecated\'\n');
45+
console.log('silent ok');
46+
});
47+
48+
execFile(node, traceDep, function(er, stdout, stderr) {
49+
console.error('--trace-deprecation: show stack');
50+
assert.equal(er, null);
51+
assert.equal(stdout, '');
52+
var stack = stderr.trim().split('\n');
53+
// just check the top and bottom.
54+
assert.equal(stack[0], 'Trace: util.p: Use console.error() instead.');
55+
assert.equal(stack.pop(), '\'This is deprecated\'');
56+
console.log('trace ok');
57+
});

0 commit comments

Comments
 (0)