From e1ca8eaf74e09edc9a2af92ff044275571d980b1 Mon Sep 17 00:00:00 2001 From: Vladimir Kurchatkin Date: Thu, 28 May 2015 17:01:56 +0300 Subject: [PATCH 1/2] util: introduce `printDeprecationMessage` function `printDeprecationMessage` is used to deprecate modules and execution branches. --- lib/buffer.js | 12 ++---------- lib/internal/util.js | 18 ++++++++++++++++++ lib/sys.js | 9 +++------ lib/util.js | 12 ++---------- node.gyp | 1 + 5 files changed, 26 insertions(+), 26 deletions(-) create mode 100644 lib/internal/util.js diff --git a/lib/buffer.js b/lib/buffer.js index 1b9c68465d6b03..ccd899da5f32ae 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -3,6 +3,7 @@ const binding = process.binding('buffer'); const smalloc = process.binding('smalloc'); const util = require('util'); +const internalUtil = require('internal/util'); const alloc = smalloc.alloc; const truncate = smalloc.truncate; 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 } else { - if (!writeWarned) { - if (process.throwDeprecation) - throw new Error(writeMsg); - else if (process.traceDeprecation) - console.trace(writeMsg); - else - console.error(writeMsg); - writeWarned = true; - } - + writeWarned = internalUtil.printDeprecationMessage(writeMsg, writeWarned); var swap = encoding; encoding = offset; offset = length >>> 0; diff --git a/lib/internal/util.js b/lib/internal/util.js new file mode 100644 index 00000000000000..76576331adbb99 --- /dev/null +++ b/lib/internal/util.js @@ -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; +}; diff --git a/lib/sys.js b/lib/sys.js index 74143b5c198a07..a34ea0b899f824 100644 --- a/lib/sys.js +++ b/lib/sys.js @@ -1,13 +1,10 @@ 'use strict'; -const util = require('util'); +const util = require('internal/util'); // the sys module was renamed to 'util'. // this shim remains to keep old programs working. // sys is deprecated and shouldn't be used -const setExports = util.deprecate(function() { - module.exports = util; -}, 'sys is deprecated. Use util instead.'); - -setExports(); +module.exports = require('util'); +util.printDeprecationMessage('sys is deprecated. Use util instead.'); diff --git a/lib/util.js b/lib/util.js index 401d55bbe82926..c4aea9d58890b5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,6 +2,7 @@ const uv = process.binding('uv'); const Debug = require('vm').runInDebugContext('Debug'); +const internalUtil = require('internal/util'); const formatRegExp = /%[sdj%]/g; exports.format = function(f) { @@ -63,16 +64,7 @@ exports.deprecate = function(fn, msg) { var warned = false; function deprecated() { - if (!warned) { - if (process.throwDeprecation) { - throw new Error(msg); - } else if (process.traceDeprecation) { - console.trace(msg); - } else { - console.error(msg); - } - warned = true; - } + warned = internalUtil.printDeprecationMessage(msg, warned); return fn.apply(this, arguments); } diff --git a/node.gyp b/node.gyp index 70c9841a89a176..1370fbd0373e28 100644 --- a/node.gyp +++ b/node.gyp @@ -73,6 +73,7 @@ 'lib/internal/freelist.js', 'lib/internal/smalloc.js', 'lib/internal/repl.js', + 'lib/internal/util.js', ], }, From 475795bef515b96629e8cc4bc679b707ab264fd8 Mon Sep 17 00:00:00 2001 From: Vladimir Kurchatkin Date: Thu, 28 May 2015 17:20:57 +0300 Subject: [PATCH 2/2] smalloc: deprecate whole module It makes no sense to allow people use constants from `smalloc`, since it will be removed completely eventually. --- lib/fs.js | 2 +- lib/repl.js | 3 +-- lib/smalloc.js | 27 +++------------------------ 3 files changed, 5 insertions(+), 27 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 4cdd0ef5e31456..50668ebcef803f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -19,7 +19,7 @@ const Readable = Stream.Readable; const Writable = Stream.Writable; const kMinPoolSpace = 128; -const kMaxLength = require('smalloc').kMaxLength; +const kMaxLength = require('internal/smalloc').kMaxLength; const O_APPEND = constants.O_APPEND || 0; const O_CREAT = constants.O_CREAT || 0; diff --git a/lib/repl.js b/lib/repl.js index 40496559bf452f..420fde45eb6a6b 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -61,8 +61,7 @@ exports.writer = util.inspect; exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'punycode', 'querystring', 'readline', 'stream', - 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib', - 'smalloc']; + 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib']; const BLOCK_SCOPED_ERROR = 'Block-scoped declarations (let, ' + diff --git a/lib/smalloc.js b/lib/smalloc.js index 8601a8b3630661..9a74ca94bf8520 100644 --- a/lib/smalloc.js +++ b/lib/smalloc.js @@ -1,27 +1,6 @@ 'use strict'; -const smalloc = require('internal/smalloc'); -const deprecate = require('util').deprecate; +const util = require('internal/util'); -exports.alloc = - deprecate(smalloc.alloc, 'smalloc.alloc: Deprecated, use typed arrays'); - -exports.copyOnto = - deprecate(smalloc.copyOnto, - 'smalloc.copyOnto: Deprecated, use typed arrays'); - -exports.dispose = - deprecate(smalloc.dispose, - 'smalloc.dispose: Deprecated, use typed arrays'); - -exports.hasExternalData = - deprecate(smalloc.hasExternalData, - 'smalloc.hasExternalData: Deprecated, use typed arrays'); - -Object.defineProperty(exports, 'kMaxLength', { - enumerable: true, value: smalloc.kMaxLength, writable: false -}); - -Object.defineProperty(exports, 'Types', { - enumerable: true, value: Object.freeze(smalloc.Types), writable: false -}); +module.exports = require('internal/smalloc'); +util.printDeprecationMessage('smalloc is deprecated.');