From fe82aebfeca1fd22cb265e5423c3d70d7bebfb0b Mon Sep 17 00:00:00 2001 From: Nicolas Dextraze Date: Sat, 2 Nov 2019 15:40:02 -0700 Subject: [PATCH] Fix #91 --- package-lock.json | 41 +++++++++++++++++++++++++++++----------- package.json | 8 ++++---- src/messages/messages.js | 14 ++++++++------ src/utils/modules.js | 17 +++++++++++++++++ test/common/base_test.js | 5 +++++ 5 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 src/utils/modules.js diff --git a/package-lock.json b/package-lock.json index 4705498..1606006 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1953,7 +1953,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -1974,12 +1975,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1994,17 +1997,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2121,7 +2127,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2133,6 +2140,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2147,6 +2155,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2154,12 +2163,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2178,6 +2189,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2258,7 +2270,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2270,6 +2283,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2355,7 +2369,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2391,6 +2406,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2410,6 +2426,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2453,12 +2470,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/package.json b/package.json index c0a5938..f2cbece 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-eventstore-client", - "version": "0.2.13", + "version": "0.2.14", "description": "A port of the EventStore .Net ClientAPI to Node.js", "main": "index.js", "types": "index.d.ts", @@ -9,13 +9,13 @@ }, "engineStrict": true, "scripts": { - "clean": "rm lib/dist.js", + "clean": "rm -f lib/dist.js", "prebuild": "npm run clean", - "build": "webpack", + "build": "webpack -o ./lib/dist.js", "pretest": "npm run build", "test": "nodeunit", "test-debug": "TESTS_VERBOSE_LOGGING=1 nodeunit", - "prepublish": "npm run build && npm run gendocs", + "prepublishOnly": "npm run build && npm run gendocs", "gendocs": "rm -rf docs && jsdoc src -r -d docs" }, "files": [ diff --git a/src/messages/messages.js b/src/messages/messages.js index eb0d7e7..ad02668 100644 --- a/src/messages/messages.js +++ b/src/messages/messages.js @@ -1,15 +1,17 @@ /*eslint-disable block-scoped-var, id-length, no-control-regex, no-magic-numbers, no-prototype-builtins, no-redeclare, no-shadow, no-var, sort-vars*/ (function(global, factory) { /* global define, require, module */ - /* AMD */ if (typeof define === 'function' && define.amd) - define(["protobufjs/minimal"], factory); + /* CommonJS */if (typeof require === 'function' && typeof module === 'object' && module && module.exports) + module.exports = factory(require("protobufjs/minimal"), require("../utils/modules")); - /* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports) - module.exports = factory(require("protobufjs/minimal")); - -})(this, function($protobuf) { +})(this, function($protobuf, $moduleUtil) { "use strict"; + // This fixes bug #91 by giving us an isolate copy of protobufjs library so any tampering with it doesn't affect us + var old = $moduleUtil.uncache(require.resolve('protobufjs/minimal')); + $protobuf = require('protobufjs/minimal'); + $moduleUtil.recache(old); + // Common aliases var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util; diff --git a/src/utils/modules.js b/src/utils/modules.js new file mode 100644 index 0000000..3506f0a --- /dev/null +++ b/src/utils/modules.js @@ -0,0 +1,17 @@ +exports.uncache = function uncache(id) { + var mod = require.cache[id]; + if (!mod) return; + delete require.cache[id]; + if (mod.children) + for(var i = 0; i < mod.children.length; i++) + uncache(mod.children[i].id); + return mod; +}; + +exports.recache = function recache(mod) { + if (require.cache[mod.id] === mod) return; + require.cache[mod.id] = mod; + if (mod.children) + for(var i = 0; i < mod.children.length; i++) + recache(mod.children[i]); +}; diff --git a/test/common/base_test.js b/test/common/base_test.js index 8de1ce0..33df59c 100644 --- a/test/common/base_test.js +++ b/test/common/base_test.js @@ -5,6 +5,11 @@ var client = require('../../lib/dist'); var FileLogger = require('../../src/common/log/fileLogger'); var NoopLogger = require('../../src/common/log/noopLogger'); +// Make sure we mess with protobufjs setup for bug #91 +var protobufJS = require('protobufjs'); +protobufJS.util.Long = undefined; +protobufJS.configure(); + var settings = { log: new NoopLogger() };