From 4648aa66c80b875e62dd4f1426934c25fae06f6d Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Sun, 12 Jan 2020 20:10:16 +0100 Subject: [PATCH 1/4] add test for enable/disable of existing instances --- test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test.js b/test.js index f61e079b..a1d6f633 100644 --- a/test.js +++ b/test.js @@ -117,5 +117,24 @@ describe('debug', () => { assert.deepStrictEqual(oldNames.map(String), debug.names.map(String)); assert.deepStrictEqual(oldSkips.map(String), debug.skips.map(String)); }); + + it('handles re-enabling existing instances', () => { + debug.disable('*'); + const inst = debug('foo'); + const messages = []; + inst.log = msg => messages.push(msg.replace(/^[^@]*@([^@]+)@.*$/, '$1')); + + inst('@test@'); + assert.deepStrictEqual(messages, []); + debug.enable('foo'); + assert.deepStrictEqual(messages, []); + inst('@test2@'); + assert.deepStrictEqual(messages, ['test2']); + inst('@test3@'); + assert.deepStrictEqual(messages, ['test2', 'test3']); + debug.disable('*'); + inst('@test4@'); + assert.deepStrictEqual(messages, ['test2', 'test3']); + }); }); }); From 7da8eff7e63aac3443118ddfdf9bbd22f061b25c Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Sun, 12 Jan 2020 20:14:48 +0100 Subject: [PATCH 2/4] fix memory leak within debug instance --- src/common.js | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/common.js b/src/common.js index 32b4fd62..e721a256 100644 --- a/src/common.js +++ b/src/common.js @@ -17,11 +17,6 @@ function setup(env) { createDebug[key] = env[key]; }); - /** - * Active `debug` instances. - */ - createDebug.instances = []; - /** * The currently active debug mode names, and names to skip. */ @@ -63,6 +58,7 @@ function setup(env) { */ function createDebug(namespace) { let prevTime; + let enableOverride = null; function debug(...args) { // Disabled? @@ -115,31 +111,27 @@ function setup(env) { } debug.namespace = namespace; - debug.enabled = createDebug.enabled(namespace); debug.useColors = createDebug.useColors(); debug.color = selectColor(namespace); - debug.destroy = destroy; debug.extend = extend; + Object.defineProperty(debug, 'enabled', { + enumerable: true, + configurable: false, + get: () => enableOverride === null ? createDebug.enabled(namespace) : enableOverride, + set: v => { + enableOverride = v; + } + }); + // Env-specific initialization logic for debug instances if (typeof createDebug.init === 'function') { createDebug.init(debug); } - createDebug.instances.push(debug); - return debug; } - function destroy() { - const index = createDebug.instances.indexOf(this); - if (index !== -1) { - createDebug.instances.splice(index, 1); - return true; - } - return false; - } - function extend(namespace, delimiter) { const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace); newDebug.log = this.log; @@ -177,11 +169,6 @@ function setup(env) { createDebug.names.push(new RegExp('^' + namespaces + '$')); } } - - for (i = 0; i < createDebug.instances.length; i++) { - const instance = createDebug.instances[i]; - instance.enabled = createDebug.enabled(instance.namespace); - } } /** From 60872413aa064ec1c967793359b8aebe0892b979 Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Sun, 12 Jan 2020 20:25:37 +0100 Subject: [PATCH 3/4] add deprecation notice for debug.destroy() --- src/browser.js | 10 ++++++++++ src/common.js | 10 ++++++++++ src/node.js | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/src/browser.js b/src/browser.js index ac3f7e13..cd0fc35d 100644 --- a/src/browser.js +++ b/src/browser.js @@ -9,6 +9,16 @@ exports.save = save; exports.load = load; exports.useColors = useColors; exports.storage = localstorage(); +exports.destroy = (() => { + let warned = false; + + return () => { + if (!warned) { + warned = true; + console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'); + } + }; +})(); /** * Colors. diff --git a/src/common.js b/src/common.js index e721a256..699f566b 100644 --- a/src/common.js +++ b/src/common.js @@ -12,6 +12,7 @@ function setup(env) { createDebug.enable = enable; createDebug.enabled = enabled; createDebug.humanize = require('ms'); + createDebug.destroy = destroy; Object.keys(env).forEach(key => { createDebug[key] = env[key]; @@ -114,6 +115,7 @@ function setup(env) { debug.useColors = createDebug.useColors(); debug.color = selectColor(namespace); debug.extend = extend; + debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release. Object.defineProperty(debug, 'enabled', { enumerable: true, @@ -243,6 +245,14 @@ function setup(env) { return val; } + /** + * XXX DO NOT USE. This is a temporary stub function. + * XXX It WILL be removed in the next major release. + */ + function destroy() { + console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'); + } + createDebug.enable(createDebug.load()); return createDebug; diff --git a/src/node.js b/src/node.js index 5e1f1541..12a11f40 100644 --- a/src/node.js +++ b/src/node.js @@ -15,6 +15,10 @@ exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; +exports.destroy = util.deprecate( + () => {}, + 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.' +); /** * Colors. From c3646d011ca8b4391127068b9af99e219045b160 Mon Sep 17 00:00:00 2001 From: thewalkingmonty Date: Thu, 27 Aug 2020 11:18:28 +0200 Subject: [PATCH 4/4] Instance local caching for "enabled" property --- src/common.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/common.js b/src/common.js index 699f566b..135c3c68 100644 --- a/src/common.js +++ b/src/common.js @@ -25,6 +25,11 @@ function setup(env) { createDebug.names = []; createDebug.skips = []; + /** + * Revision id of the currently active debug mode names, and names to skip, configuration. + */ + createDebug.namesRev = ''; + /** * Map of special "%n" handling functions, for the debug "format" argument. * @@ -59,7 +64,6 @@ function setup(env) { */ function createDebug(namespace) { let prevTime; - let enableOverride = null; function debug(...args) { // Disabled? @@ -119,10 +123,16 @@ function setup(env) { Object.defineProperty(debug, 'enabled', { enumerable: true, - configurable: false, - get: () => enableOverride === null ? createDebug.enabled(namespace) : enableOverride, - set: v => { - enableOverride = v; + get() { + if (this._namesRev !== createDebug.namesRev) { + this._enabled = createDebug.enabled(namespace); + this._namesRev = createDebug.namesRev; + } + return this._enabled; + }, + set(v) { + this._namesRev = createDebug.namesRev; + this._enabled = v; } }); @@ -153,6 +163,10 @@ function setup(env) { createDebug.names = []; createDebug.skips = []; + // Updates the revision number + const now = Date.now().toString(36); + createDebug.namesRev = now + (createDebug.namesRev.startsWith(now) ? '_' + (parseInt(createDebug.namesRev.split('_')[1] || '0', 36) + 1).toString(36) : ''); + let i; const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/); const len = split.length;