From ff8b9a5c6f74613eba735d8b7f7be11842ce01ea Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Tue, 22 Aug 2017 09:51:03 -0700 Subject: [PATCH 1/7] set logger.info to be enable only when debugLevel is ablove log (previously info) --- lib/logger.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index 7e3a852..a1842eb 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -12,12 +12,26 @@ var defaultLogger = { child: function() { return this; } }; -var consoleLogger = { - debugLevel: 'info', // 'info|debug|trace' +var consoleLogger = Object.create({ + debugLevel: null, // (log|trace|debug) + // Displayed when debugLevel is enabled + log: console.log, fatal: console.error, error: console.error, warn : console.warning || console.info, - info : console.info, + // Displayed when debugLevel is (trace|debug) + info: function (data, ctx) { + var debug = console.debug || console.info; + if ( + consoleLogger.debugLevel === 'trace' || + consoleLogger.debugLevel === 'debug' + ) { + var args = (typeof ctx === 'string' ? [ctx, data] : arguments); + debug.apply(console, args); + } + return this; + }, + // Displayed when debugLevel is (trace|debug) debug: function (data, ctx) { var debug = console.debug || console.info; if ( @@ -27,22 +41,24 @@ var consoleLogger = { var args = (typeof ctx === 'string' ? [ctx, data] : arguments); debug.apply(console, args); } + return this; }, - // Trace only + // Displayed when debugLevel is trace only trace: function (data, ctx) { var debug = console.debug || console.info; if (consoleLogger.debugLevel === 'trace') { - consoleLogger.debug(data, ctx); + return consoleLogger.debug(data, ctx); } + return this; }, // Trace only child: function(msg) { if (consoleLogger.debugLevel === 'trace') { - console.info(msg); + return console.info(msg); } return this; } -}; +}); module.exports = { consoleLogger: consoleLogger, From 8a649a3a1210c65abb69d40e94e5a558207f2d86 Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Tue, 22 Aug 2017 09:51:12 -0700 Subject: [PATCH 2/7] set logger.info to be enable only when debugLevel is ablove log (previously info) --- lib/configuration.js | 11 ++++++++--- lib/http2-cache.js | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/configuration.js b/lib/configuration.js index 606b803..0544361 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -48,14 +48,19 @@ util.inherits(Configuration, EventEmitter); var confProto = Configuration.prototype; -confProto.setDebugLevel = function (level) { +confProto.setDebugLevel = function (debugLevel) { - level = typeof level === 'string' ? level : level === true ? 'info' : null; + debugLevel = typeof debugLevel === 'string' ? debugLevel : debugLevel === true ? 'log' : null; // Init debug/log if (this._log && this._log.hasOwnProperty('debugLevel')) { - this._log.debugLevel = level; + this._log.debugLevel = debugLevel; } + + this.debug = debugLevel; + + // Chainable + return this; }; confProto.configuring = function () { diff --git a/lib/http2-cache.js b/lib/http2-cache.js index a75754b..8dc6b1c 100644 --- a/lib/http2-cache.js +++ b/lib/http2-cache.js @@ -4,7 +4,7 @@ enableXHROverH2 = require('./xhr.js').enableXHROverH2; var configuration = new Configuration({ - debug: false // true='info' or (info|debug|trace) + debug: false // true='log' or (log|debug|trace) }); enableXHROverH2(XMLHttpRequest, configuration); From 3a77fce7a8db631f908c6baf36e2828fbfffc5ce Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Tue, 22 Aug 2017 09:56:26 -0700 Subject: [PATCH 3/7] fix lib/logger --- lib/logger.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/logger.js b/lib/logger.js index a1842eb..0b43414 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -12,22 +12,22 @@ var defaultLogger = { child: function() { return this; } }; -var consoleLogger = Object.create({ - debugLevel: null, // (log|trace|debug) - // Displayed when debugLevel is enabled +var consoleLogger = { + debugLevel: 'log', // 'log|debug|trace' log: console.log, fatal: console.error, error: console.error, warn : console.warning || console.info, + //info : console.info, // Displayed when debugLevel is (trace|debug) info: function (data, ctx) { - var debug = console.debug || console.info; + var info = console.info; if ( consoleLogger.debugLevel === 'trace' || consoleLogger.debugLevel === 'debug' ) { var args = (typeof ctx === 'string' ? [ctx, data] : arguments); - debug.apply(console, args); + info.apply(console, args); } return this; }, @@ -56,11 +56,11 @@ var consoleLogger = Object.create({ if (consoleLogger.debugLevel === 'trace') { return console.info(msg); } - return this; + return this; } -}); +}; module.exports = { - consoleLogger: consoleLogger, - defaultLogger: defaultLogger + consoleLogger: consoleLogger, + defaultLogger: defaultLogger }; \ No newline at end of file From 01a40724ef6151c12b600ed8dfd56460c05d6e65 Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Tue, 22 Aug 2017 10:00:26 -0700 Subject: [PATCH 4/7] implement persistant debugLeval via 'XMLHttpRequest.configuration.setDebugLevel('debug', true);' --- lib/configuration.js | 29 ++++++++++++++++++++++++++--- lib/http2-cache.js | 10 ++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/configuration.js b/lib/configuration.js index 0544361..064c7aa 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -40,18 +40,41 @@ function Configuration(options) { self.agent = new Agent({ log: self._log }); - - } util.inherits(Configuration, EventEmitter); var confProto = Configuration.prototype; -confProto.setDebugLevel = function (debugLevel) { + +var _DEBUG_LEVEL_KEY = 'http2-cache.configuration._debugLevel', + _DEBUG_LEVEL_DEFAULT = 'log'; + +confProto.setDebugLevel = function (debugLevel, persit) { debugLevel = typeof debugLevel === 'string' ? debugLevel : debugLevel === true ? 'log' : null; + // Setter with persit but not 'auto' + if (debugLevel !== 'auto' && persit === true) { + + _DEBUG_LEVEL_DEFAULT = debugLevel; + + // Use localStorage if available + if (typeof localStorage !== 'undefined') { + localStorage.setItem(_DEBUG_LEVEL_KEY, _DEBUG_LEVEL_DEFAULT); + } + + // Compute default value from storet then const + } else if (debugLevel === 'auto') { + + // Use localStorage if available + if (typeof localStorage !== 'undefined') { + debugLevel = localStorage.getItem(_DEBUG_LEVEL_KEY, _DEBUG_LEVEL_DEFAULT); + } + + debugLevel = debugLevel || _DEBUG_LEVEL_DEFAULT; + } + // Init debug/log if (this._log && this._log.hasOwnProperty('debugLevel')) { this._log.debugLevel = debugLevel; diff --git a/lib/http2-cache.js b/lib/http2-cache.js index 8dc6b1c..07b5d9f 100644 --- a/lib/http2-cache.js +++ b/lib/http2-cache.js @@ -3,13 +3,15 @@ var Configuration = require('./configuration.js'), enableXHROverH2 = require('./xhr.js').enableXHROverH2; - var configuration = new Configuration({ - debug: false // true='log' or (log|debug|trace) - }); - enableXHROverH2(XMLHttpRequest, configuration); + enableXHROverH2(XMLHttpRequest, new Configuration({ + debug: 'auto' // true='log' or (auto|log|debug|trace) + })); // To update debug level after injection: //- XMLHttpRequest.configuration.setDebugLevel('debug'); //- XMLHttpRequest.configuration.setDebugLevel('trace'); + // To persit update debug level after injection: + //- XMLHttpRequest.configuration.setDebugLevel('debug', true); + }(this)); From 020dda6af6400d5a63f417d0cc3657b622377ddd Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Tue, 22 Aug 2017 10:02:43 -0700 Subject: [PATCH 5/7] fix typo --- lib/configuration.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/configuration.js b/lib/configuration.js index 064c7aa..6bc988b 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -50,12 +50,12 @@ var confProto = Configuration.prototype; var _DEBUG_LEVEL_KEY = 'http2-cache.configuration._debugLevel', _DEBUG_LEVEL_DEFAULT = 'log'; -confProto.setDebugLevel = function (debugLevel, persit) { +confProto.setDebugLevel = function (debugLevel, persist) { debugLevel = typeof debugLevel === 'string' ? debugLevel : debugLevel === true ? 'log' : null; - // Setter with persit but not 'auto' - if (debugLevel !== 'auto' && persit === true) { + // Setter with persist but not 'auto' + if (debugLevel !== 'auto' && persist === true) { _DEBUG_LEVEL_DEFAULT = debugLevel; From 4b2a94e7bb7c3c98bea193659341fe7ff401b93d Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Wed, 13 Dec 2017 16:39:48 -0800 Subject: [PATCH 6/7] fix typo --- lib/configuration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/configuration.js b/lib/configuration.js index d71757e..fe487d7 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -65,7 +65,7 @@ confProto.setDebugLevel = function (debugLevel, persist) { localStorage.setItem(_DEBUG_LEVEL_KEY, _DEBUG_LEVEL_DEFAULT); } - // Compute default value from storet then const + // Compute default value from store then const } else if (debugLevel === 'auto') { // Use localStorage if available From cd9d0c7241ffad96f98b6240d02c70569bf7ce8b Mon Sep 17 00:00:00 2001 From: Harold Thetiot Date: Tue, 29 May 2018 14:37:31 -0700 Subject: [PATCH 7/7] fix spec --- lib/configuration.js | 10 ++++++---- lib/http2-cache.js | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/configuration.js b/lib/configuration.js index df6b304..d953ed7 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -79,11 +79,11 @@ confProto.setOptions = function (options) { confProto.debug = false; var _DEBUG_LEVEL_KEY = 'http2-cache.configuration._debugLevel', - _DEBUG_LEVEL_DEFAULT = 'log'; + _DEBUG_LEVEL_DEFAULT = false; confProto.setDebugLevel = function (debugLevel, persist) { - debugLevel = typeof debugLevel === 'string' ? debugLevel : debugLevel === true ? 'log' : null; + debugLevel = typeof debugLevel === 'string' ? debugLevel : debugLevel === true ? 'info' : null; // Setter with persist but not 'auto' if (debugLevel !== 'auto' && persist === true) { @@ -114,7 +114,8 @@ confProto.setDebugLevel = function (debugLevel, persist) { // Sync current options value this.options.debug = debugLevel; - this.debug = debugLevel; + // Sync and cast has boolean + this.debug = !!debugLevel; // Chainable return this; @@ -166,7 +167,8 @@ confProto.getTransport = function (url) { if (uri.protocol === 'ws:' || uri.protocol === 'wss:') { activeTransportConnections[uri.href] = websocket(uri.href, "h2", { // TODO, maybe enable perMessageDeflate in production or on debug?? - perMessageDeflate: that.debug === false + // Do perMessageDeflate false only on browser and if debug cause if make nodeJS spec fail + perMessageDeflate: typeof process !== 'undefined' || that.debug === false }); } else if(uri.protocol === 'tcp:') { diff --git a/lib/http2-cache.js b/lib/http2-cache.js index b28b86a..91dad5a 100644 --- a/lib/http2-cache.js +++ b/lib/http2-cache.js @@ -11,7 +11,7 @@ enableXHROverH2 = require('./xhr.js').enableXHROverH2; enableXHROverH2(XMLHttpRequest, new Configuration({ - debug: 'auto' // true='log' or (auto|log|debug|trace) + debug: 'auto' // true='info' or (auto|info|debug|trace) })); // To update debug level after injection: