util: introduce printDeprecationMessage
function
`printDeprecationMessage` is used to deprecate modules and execution branches. PR-URL: https://github.com/nodejs/io.js/pull/1822 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
6537fd4b55
commit
628845b816
@ -3,6 +3,7 @@
|
|||||||
const binding = process.binding('buffer');
|
const binding = process.binding('buffer');
|
||||||
const smalloc = process.binding('smalloc');
|
const smalloc = process.binding('smalloc');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
const internalUtil = require('internal/util');
|
||||||
const alloc = smalloc.alloc;
|
const alloc = smalloc.alloc;
|
||||||
const truncate = smalloc.truncate;
|
const truncate = smalloc.truncate;
|
||||||
const sliceOnto = smalloc.sliceOnto;
|
const sliceOnto = smalloc.sliceOnto;
|
||||||
@ -504,16 +505,7 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
|
|||||||
|
|
||||||
// XXX legacy write(string, encoding, offset, length) - remove in v0.13
|
// XXX legacy write(string, encoding, offset, length) - remove in v0.13
|
||||||
} else {
|
} else {
|
||||||
if (!writeWarned) {
|
writeWarned = internalUtil.printDeprecationMessage(writeMsg, writeWarned);
|
||||||
if (process.throwDeprecation)
|
|
||||||
throw new Error(writeMsg);
|
|
||||||
else if (process.traceDeprecation)
|
|
||||||
console.trace(writeMsg);
|
|
||||||
else
|
|
||||||
console.error(writeMsg);
|
|
||||||
writeWarned = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var swap = encoding;
|
var swap = encoding;
|
||||||
encoding = offset;
|
encoding = offset;
|
||||||
offset = length >>> 0;
|
offset = length >>> 0;
|
||||||
|
18
lib/internal/util.js
Normal file
18
lib/internal/util.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
exports.printDeprecationMessage = function(msg, warned) {
|
||||||
|
if (process.noDeprecation)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (warned)
|
||||||
|
return warned;
|
||||||
|
|
||||||
|
if (process.throwDeprecation)
|
||||||
|
throw new Error(msg);
|
||||||
|
else if (process.traceDeprecation)
|
||||||
|
console.trace(msg);
|
||||||
|
else
|
||||||
|
console.error(msg);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
@ -1,13 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const util = require('util');
|
const util = require('internal/util');
|
||||||
|
|
||||||
// the sys module was renamed to 'util'.
|
// the sys module was renamed to 'util'.
|
||||||
// this shim remains to keep old programs working.
|
// this shim remains to keep old programs working.
|
||||||
// sys is deprecated and shouldn't be used
|
// sys is deprecated and shouldn't be used
|
||||||
|
|
||||||
const setExports = util.deprecate(function() {
|
module.exports = require('util');
|
||||||
module.exports = util;
|
util.printDeprecationMessage('sys is deprecated. Use util instead.');
|
||||||
}, 'sys is deprecated. Use util instead.');
|
|
||||||
|
|
||||||
setExports();
|
|
||||||
|
12
lib/util.js
12
lib/util.js
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const uv = process.binding('uv');
|
const uv = process.binding('uv');
|
||||||
const Debug = require('vm').runInDebugContext('Debug');
|
const Debug = require('vm').runInDebugContext('Debug');
|
||||||
|
const internalUtil = require('internal/util');
|
||||||
|
|
||||||
const formatRegExp = /%[sdj%]/g;
|
const formatRegExp = /%[sdj%]/g;
|
||||||
exports.format = function(f) {
|
exports.format = function(f) {
|
||||||
@ -63,16 +64,7 @@ exports.deprecate = function(fn, msg) {
|
|||||||
|
|
||||||
var warned = false;
|
var warned = false;
|
||||||
function deprecated() {
|
function deprecated() {
|
||||||
if (!warned) {
|
warned = internalUtil.printDeprecationMessage(msg, warned);
|
||||||
if (process.throwDeprecation) {
|
|
||||||
throw new Error(msg);
|
|
||||||
} else if (process.traceDeprecation) {
|
|
||||||
console.trace(msg);
|
|
||||||
} else {
|
|
||||||
console.error(msg);
|
|
||||||
}
|
|
||||||
warned = true;
|
|
||||||
}
|
|
||||||
return fn.apply(this, arguments);
|
return fn.apply(this, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user