From 8f5e73bd0fee5927f7bd2d88dd03a7993c4b9a29 Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 14 Jul 2020 22:43:55 +0200 Subject: [PATCH 1/3] Make node detection a bit more forgiving --- src/util/minimal.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/util/minimal.js b/src/util/minimal.js index e384b62fb..4bf53fabf 100644 --- a/src/util/minimal.js +++ b/src/util/minimal.js @@ -25,8 +25,18 @@ util.pool = require("@protobufjs/pool"); // utility to work with the low and high bits of a 64 bit value util.LongBits = require("./longbits"); +/** Tests whether the specified object is most likely a node.js process. */ +function isNodeProcess(process) { + return Boolean(process && process.versions && process.versions.node); +} + +/** Tests whether the specified object is most likely a node.js global. */ +function isNodeGlobal(global) { + return global && isNodeProcess(global.process); +} + // global object reference -util.global = typeof global !== "undefined" && Object.prototype.toString.call(global) === "[object global]" && global +util.global = typeof global !== "undefined" && isNodeGlobal(global) && global || typeof window !== "undefined" && window || typeof self !== "undefined" && self || this; // eslint-disable-line no-invalid-this @@ -52,7 +62,7 @@ util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next * * @type {boolean} * @const */ -util.isNode = Boolean(util.global.process && util.global.process.versions && util.global.process.versions.node); +util.isNode = isNodeProcess(util.global.process); /** * Tests if the specified value is an integer. From bde8f340c7a9e888574e29b1eea0d799206e2c86 Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 14 Jul 2020 23:12:42 +0200 Subject: [PATCH 2/3] reorder --- index.d.ts | 9 ++++++--- src/util/minimal.js | 35 ++++++++++++++++------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/index.d.ts b/index.d.ts index 772250491..cb86b61e6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1951,15 +1951,18 @@ export namespace util { public length(): number; } + /** Whether running within node or not. */ + let isNode: boolean; + + /** Global object reference. */ + let global: object; + /** An immuable empty array. */ const emptyArray: any[]; /** An immutable empty object. */ const emptyObject: object; - /** Whether running within node or not. */ - const isNode: boolean; - /** * Tests if the specified value is an integer. * @param value Value to test diff --git a/src/util/minimal.js b/src/util/minimal.js index 4bf53fabf..064b6dd3d 100644 --- a/src/util/minimal.js +++ b/src/util/minimal.js @@ -25,18 +25,23 @@ util.pool = require("@protobufjs/pool"); // utility to work with the low and high bits of a 64 bit value util.LongBits = require("./longbits"); -/** Tests whether the specified object is most likely a node.js process. */ -function isNodeProcess(process) { - return Boolean(process && process.versions && process.versions.node); -} - -/** Tests whether the specified object is most likely a node.js global. */ -function isNodeGlobal(global) { - return global && isNodeProcess(global.process); -} +/** + * Whether running within node or not. + * @memberof util + * @type {boolean} + */ +util.isNode = typeof global !== "undefined" + && global + && global.process + && global.process.versions + && global.process.versions.node; -// global object reference -util.global = typeof global !== "undefined" && isNodeGlobal(global) && global +/** + * Global object reference. + * @memberof util + * @type {Object} + */ +util.global = util.isNode && global || typeof window !== "undefined" && window || typeof self !== "undefined" && self || this; // eslint-disable-line no-invalid-this @@ -56,14 +61,6 @@ util.emptyArray = Object.freeze ? Object.freeze([]) : /* istanbul ignore next */ */ util.emptyObject = Object.freeze ? Object.freeze({}) : /* istanbul ignore next */ {}; // used on prototypes -/** - * Whether running within node or not. - * @memberof util - * @type {boolean} - * @const - */ -util.isNode = isNodeProcess(util.global.process); - /** * Tests if the specified value is an integer. * @function From b649044a793ec8b6e42a600e11bea3569b2d8553 Mon Sep 17 00:00:00 2001 From: dcode Date: Tue, 14 Jul 2020 23:19:39 +0200 Subject: [PATCH 3/3] make sure we get a boolean --- src/util/minimal.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/minimal.js b/src/util/minimal.js index 064b6dd3d..3c406dee7 100644 --- a/src/util/minimal.js +++ b/src/util/minimal.js @@ -30,11 +30,11 @@ util.LongBits = require("./longbits"); * @memberof util * @type {boolean} */ -util.isNode = typeof global !== "undefined" - && global - && global.process - && global.process.versions - && global.process.versions.node; +util.isNode = Boolean(typeof global !== "undefined" + && global + && global.process + && global.process.versions + && global.process.versions.node); /** * Global object reference.