Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

module-invalidate

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-invalidate - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

133

index.js

@@ -12,10 +12,11 @@ 'use strict';

// see https://tc39.github.io/ecma262/#sec-toprimitive
function toPrimitive(value) {
var valueToPrimitive = value[Symbol.toPrimitive];
if ( typeof(valueToPrimitive) === 'function' )
if ( valueToPrimitive !== undefined )
return valueToPrimitive;
return function(hint) {
if ( hint === 'number' )

@@ -26,11 +27,10 @@ return Number(value);

if ( typeof(value) === 'object' ) {
var val = value.valueOf();
if ( typeof(val) === 'object' )
return String(val);
if ( typeof(value) !== 'object' && typeof(value) !== 'function' || value === null )
return value;
var val = value.valueOf();
if ( typeof(val) !== 'object' && typeof(val) !== 'function' || val === null )
return val;
}
return value;
return String(value);
}

@@ -40,5 +40,6 @@ }

function hasInstance(ctor) {
return function(instance) {
return instance instanceof ctor;
return instance instanceof ctor;
}

@@ -50,3 +51,3 @@ }

function bound() {
return fct.apply(value, arguments);

@@ -61,10 +62,9 @@ }

Module.invalidate = function() {
for ( var filename in Module._cache )
if ( 'invalidate' in Module._cache[filename] )
Module._cache[filename].invalidate();
Module._cache[filename].invalidate();
}
Module.invalidateByExports = function(exports) {
for ( var filename in Module._cache )

@@ -76,3 +76,3 @@ if ( Module._cache[filename].exports === exports )

Module.prototype.invalidateByPath = function(path) {
Module._cache[Module._resolveFilename(path, this, this.parent === null)].invalidate();

@@ -85,9 +85,9 @@ }

return;
if ( invalidateCallbacksSym in this ) {
var validateCallbacks = this[validateCallbacksSym] || (this[validateCallbacksSym] = new Set);
this[invalidateCallbacksSym].forEach(callback => {
var validateCallback = callback(this._exports);

@@ -99,3 +99,3 @@ if ( typeof(validateCallback) === 'function' )

}
this._exports = invalid;

@@ -105,3 +105,3 @@ }

Module.prototype.onInvalidate = function(callback) {
var invalidateCallbacks = this[invalidateCallbacksSym] || (this[invalidateCallbacksSym] = new Set);

@@ -116,5 +116,5 @@ return invalidateCallbacks.add(callback).delete.bind(invalidateCallbacks, callback);

mod.load(mod.filename);
if ( validateCallbacksSym in mod ) {
mod[validateCallbacksSym].forEach(callback => callback(mod._exports) );

@@ -127,31 +127,31 @@ mod[validateCallbacksSym].clear();

function createProxy(mod) {
return new Proxy(function() {}, {
getPrototypeOf: function(target) {
mod._exports === invalid && reload(mod);
return Reflect.getPrototypeOf(mod._exports);
},
setPrototypeOf: function(target, prototype) {
mod._exports === invalid && reload(mod);
return Reflect.setPrototypeOf(mod._exports, prototype);
},
isExtensible: function(target) {
mod._exports === invalid && reload(mod);
return Reflect.isExtensible(mod._exports);
},
preventExtensions: function(target) {
mod._exports === invalid && reload(mod);
return Reflect.preventExtensions(mod._exports);
},
getOwnPropertyDescriptor: function(target, prop) {
mod._exports === invalid && reload(mod);

@@ -163,19 +163,19 @@

},
defineProperty: function(target, property, descriptor) {
mod._exports === invalid && reload(mod);
return Reflect.defineProperty(mod._exports, property, descriptor);
},
has: function(target, prop) {
mod._exports === invalid && reload(mod);
return Reflect.has(mod._exports, prop);
},
get: function(target, property) {
mod._exports === invalid && reload(mod);
if ( property === Symbol.hasInstance )

@@ -185,8 +185,3 @@ return hasInstance(mod._exports);

return toPrimitive(mod._exports);
// see http://stackoverflow.com/questions/42496414/illegal-invocation-error-using-es6-proxy-and-node-js
// see https://github.com/nodejs/node/issues/11629 (Illegal invocation error using ES6 Proxy and node.js)
// see http://stackoverflow.com/questions/42594682/how-to-determine-that-a-javascript-function-is-native-without-testing-native
// see V8 issue https://bugs.chromium.org/p/v8/issues/detail?id=5773
var val = Reflect.get(mod._exports, property);

@@ -203,20 +198,20 @@

},
set: function(target, property, value) {
mod._exports === invalid && reload(mod);
return Reflect.set(mod._exports, property, value);
},
deleteProperty: function(target, property) {
mod._exports === invalid && reload(mod);
return Reflect.deleteProperty(mod._exports, property);
},
ownKeys: function(target) {
mod._exports === invalid && reload(mod);
var ownKeys = Reflect.ownKeys(mod._exports);
// see https://tc39.github.io/ecma262/#sec-invariants-of-the-essential-internal-methods
var ownKeys = Reflect.ownKeys(mod._exports);
if ( typeof mod._exports !== 'function' )

@@ -226,13 +221,13 @@ ownKeys.push('prototype');

},
apply: function(target, thisArg, argumentsList) {
mod._exports === invalid && reload(mod);
return Reflect.apply(mod._exports, thisArg, argumentsList);
},
construct: function(target, argumentsList, newTarget) {
mod._exports === invalid && reload(mod);
return Reflect.construct(mod._exports, argumentsList, newTarget);
return Reflect.construct(mod._exports, argumentsList);
}

@@ -244,13 +239,13 @@ });

get: function() {
return !!this._proxy;
},
set: function(value) {
if ( this._proxy ) {
if ( !value )
this._proxy = null;
} else {
if ( value )

@@ -282,6 +277,6 @@ this._proxy = createProxy(this);

delete Module._cache[this.filename];
// remove this module from all module children
for ( var filename in Module._cache ) {
var children = Module._cache[filename].children;

@@ -296,3 +291,3 @@ var pos = children.indexOf(this);

this.children.length = 0;
// remove module from Module._pathCache

@@ -312,3 +307,3 @@ var pathCache = Module._pathCache;

Module.unloadByExports = function(exports) {
for ( var filename in Module._cache )

@@ -315,0 +310,0 @@ if ( Module._cache[filename].exports === exports )

{
"name": "module-invalidate",
"version": "1.0.0",
"version": "1.0.1",
"description": "invalidate required modules",

@@ -5,0 +5,0 @@ "keywords": [

@@ -259,5 +259,19 @@ # module-invalidate

Because the library is unable to know in advance what type of value will be assigned to `module.export`, it choose the most generic one as ES6 Proxy target.
However, `(function(){}) instanceof Object === true`
However, `(function(){}) instanceof Object === true`.
As workaround, you can use `instanceof` against the `module.exports`, this will always retuns the expected result.
##### Example:
###### module `./myModule.js`
```
module.invalidable = true;
module.exports = new Date;
```
###### main module `index.js`
```
require('module-invalidate');
var myModule = require('./myModule.js');
console.log(myModule instanceof Date); // true
```
#### Only direct variable access is handled

@@ -310,6 +324,9 @@

## How can you help?
Give me some [feedback](https://github.com/FranckFreiburger/module-invalidate/issues/new?labels=feedback) and report [bugs](https://github.com/FranckFreiburger/module-invalidate/issues/new?labels=bug&body=Node.js%20version:%20%0Amodule-invalidate%20version:%20%0AIssue%20description:%20%0A).
## Credits
[Franck Freiburger](https://www.franck-freiburger.com)

@@ -9,3 +9,3 @@ var assert = require('assert');

it('basic', function() {
assert.strictEqual(require('module'), module.constructor);

@@ -18,3 +18,3 @@ assert.equal(typeof module.constructor._cache, 'object');

it('export proxy', function() {
var foo = 1;

@@ -27,5 +27,5 @@ var mod = new utils.TmpModule().set(_ => `

`, { autoload: false });
var exp = require(mod.module.filename);
assert.equal(exp.foo, 1);

@@ -40,3 +40,3 @@ foo++;

it('module.invalidateByPath()', function() {
var foo = 1;

@@ -49,3 +49,3 @@ var mod = new utils.TmpModule().set(_ => `

`);
assert.equal(mod.module.exports.foo, 1);

@@ -58,8 +58,8 @@ foo++;

});
it('Module.invalidateByExports()', function() {
var foo = 1;
var mod = new utils.TmpModule().set(_ =>`

@@ -71,3 +71,3 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports.foo, 1);

@@ -82,5 +82,5 @@ foo++;

it('module.invalidate()', function() {
var foo = 1;
var mod = new utils.TmpModule(_ =>`

@@ -92,3 +92,3 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports.foo, 1);

@@ -103,5 +103,5 @@ foo++;

it('exports access level', function() {
var count = 1;
var mod = new utils.TmpModule(_ =>`

@@ -119,6 +119,6 @@ module.invalidable = true;

var b = mod.module.exports.a.b;
assert.equal(mod.module.exports.a.b.c, 1);
assert.equal(b.c, 1);
count++;

@@ -132,3 +132,3 @@ mod.set();

it('selective reload', function() {

@@ -154,3 +154,3 @@

`);
assert.equal(mA.module.exports(), '0,0');

@@ -165,8 +165,8 @@ assert.equal(mA.module.exports(), '1,1');

});
it('selective reload nested', function() {
global.report = '';
var mC = new utils.TmpModule(_ =>`

@@ -176,3 +176,3 @@ global.report += 'c';

`, { autoLoad: false });
var mB = new utils.TmpModule(_ =>`

@@ -189,24 +189,24 @@ global.report += 'b';

`);
mA.load();
assert.equal(global.report, 'abc');
mB.module.invalidate();
mA.module.exports.foobar;
mB.module.exports.foobar;
mC.module.exports.foobar;
assert.equal(global.report, 'abcb');
delete global.report;
});
it('invalidateByExports unique', function() {
global.report = '';
var mB = new utils.TmpModule(_ =>`

@@ -223,11 +223,11 @@ global.report += 'b';

`);
mA.load();
assert.equal(global.report, 'ab');
module.constructor.invalidateByExports(mA.module.exports);
mA.module.exports.foo;
mB.module.exports.foo;
assert.equal(global.report, 'aba');

@@ -238,11 +238,11 @@

mB.module.exports.foo;
assert.equal(global.report, 'abab');
delete global.report;
});
});

@@ -8,14 +8,16 @@ var assert = require('assert');

it('non-invalidable exports object type', function() {
var mod = new utils.TmpModule(`
module.exports = {}
`);
assert.equal(typeof mod.module.exports, 'object');
var exports = mod.module.exports;
assert.equal(typeof exports, 'object');
mod.module.invalidate();
assert.equal(typeof mod.module.exports, 'object');
assert.equal(typeof exports, 'object');
});

@@ -25,12 +27,14 @@

it('non-invalidable exports function type', function() {
var mod = new utils.TmpModule(`
module.exports = function(){}
`);
assert.equal(typeof mod.module.exports, 'function');
var exports = mod.module.exports;
assert.equal(typeof exports, 'function');
mod.module.invalidate();
assert.equal(typeof mod.module.exports, 'function');
assert.equal(typeof exports, 'function');
});

@@ -40,3 +44,3 @@

it('invalidable exports type', function() {
var mod = new utils.TmpModule(`

@@ -47,7 +51,9 @@ module.invalidable = true;

assert.equal(typeof mod.module.exports, 'function');
var exports = mod.module.exports;
assert.equal(typeof exports, 'function');
mod.module.invalidate();
assert.equal(typeof mod.module.exports, 'function');
assert.equal(typeof exports, 'function');
});

@@ -57,3 +63,3 @@

it('exports type Object', function() {
var mod = new utils.TmpModule(`

@@ -65,13 +71,38 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports.foo, 'bar');
var exports = mod.module.exports;
assert.equal(exports.foo, 'bar');
mod.module.invalidate();
assert.equal(mod.module.exports.foo, 'bar');
assert.equal(exports.foo, 'bar');
});
it('exports type Date', function() {
var mod = new utils.TmpModule(`
module.invalidable = true;
module.exports = new Date;
`);
var exports = mod.module.exports;
assert.equal(Object.keys(exports).length, 0);
assert.strictEqual(exports.constructor, Date);
assert(exports instanceof Date);
assert(!isNaN(new Date(exports).getTime()));
mod.module.invalidate();
assert.equal(Object.keys(exports).length, 0);
assert.strictEqual(exports.constructor, Date);
assert(exports instanceof Date);
assert(!isNaN(new Date(exports).getTime()));
});
it('exports type Function', function() {
var mod = new utils.TmpModule(`

@@ -82,7 +113,9 @@ module.invalidable = true;

assert.equal(mod.module.exports(), 'foo');
var exports = mod.module.exports;
assert.equal(exports(), 'foo');
mod.module.invalidate();
assert.equal(mod.module.exports(), 'foo');
assert.equal(exports(), 'foo');
});

@@ -92,3 +125,3 @@

it('exports type constructor', function() {
var mod = new utils.TmpModule(`

@@ -98,7 +131,7 @@ module.invalidable = true;

constructor() {
this.value = 123;
}
getValue() {
return this.value;

@@ -108,8 +141,10 @@ }

`);
assert.equal(new mod.module.exports().getValue(), 123);
module.constructor.invalidateByExports(mod.module.exports);
assert.equal(new mod.module.exports().getValue(), 123);
var exports = mod.module.exports;
assert.equal(new exports().getValue(), 123);
module.constructor.invalidateByExports(exports);
assert.equal(new exports().getValue(), 123);
});

@@ -119,3 +154,3 @@

it('exports type constructor instanceof', function() {
var mod = new utils.TmpModule(`

@@ -126,16 +161,18 @@ module.invalidable = true;

assert.equal(new mod.module.exports() instanceof mod.module.exports, true);
var instance = new mod.module.exports();
module.constructor.invalidateByExports(mod.module.exports);
assert.equal(instance instanceof mod.module.exports, false);
assert.equal(new mod.module.exports() instanceof mod.module.exports, true);
var exports = mod.module.exports;
assert.equal(new exports() instanceof exports, true);
var instance = new exports();
module.constructor.invalidateByExports(exports);
assert.equal(instance instanceof exports, false);
assert.equal(new exports() instanceof exports, true);
});
it('exports type Array', function() {
var mod = new utils.TmpModule(`

@@ -146,11 +183,13 @@ module.invalidable = true;

assert.equal(mod.module.exports[1], 2);
assert.equal(mod.module.exports.length, 3);
assert.equal(typeof mod.module.exports.map, 'function');
module.constructor.invalidateByExports(mod.module.exports);
assert.equal(mod.module.exports[1], 2);
assert.equal(mod.module.exports.length, 3);
assert.equal(typeof mod.module.exports.map, 'function');
var exports = mod.module.exports;
assert.equal(exports[1], 2);
assert.equal(exports.length, 3);
assert.equal(typeof exports.map, 'function');
module.constructor.invalidateByExports(exports);
assert.equal(exports[1], 2);
assert.equal(exports.length, 3);
assert.equal(typeof exports.map, 'function');
});

@@ -161,3 +200,3 @@

it('exports type string primitive', function() {
var mod = new utils.TmpModule(`

@@ -167,6 +206,8 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports, 'foo');
module.constructor.invalidateByExports(mod.module.exports);
assert.equal(mod.module.exports, 'foo');
var exports = mod.module.exports;
assert.equal(exports, 'foo');
module.constructor.invalidateByExports(exports);
assert.equal(exports, 'foo');
});

@@ -176,3 +217,3 @@

it('exports type boolean primitive', function() {
var mod = new utils.TmpModule(`

@@ -182,6 +223,8 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports, true);
module.constructor.invalidateByExports(mod.module.exports);
assert.equal(mod.module.exports, true);
var exports = mod.module.exports;
assert.equal(exports, true);
module.constructor.invalidateByExports(exports);
assert.equal(exports, true);
});

@@ -191,3 +234,3 @@

it('exports type null-prototype object', function() {
var mod = new utils.TmpModule(`

@@ -198,8 +241,10 @@ module.invalidable = true;

`);
assert.equal(Object.getPrototypeOf(mod.module.exports), null);
assert.equal(mod.module.exports.foo, 123);
var exports = mod.module.exports;
assert.equal(Object.getPrototypeOf(exports), null);
assert.equal(exports.foo, 123);
mod.module.invalidate();
assert.equal(Object.getPrototypeOf(mod.module.exports), null);
assert.equal(mod.module.exports.foo, 123);
assert.equal(Object.getPrototypeOf(exports), null);
assert.equal(exports.foo, 123);
});

@@ -209,3 +254,3 @@

xit('exports type void(0) primitive', function() {
var mod = new utils.TmpModule(`

@@ -215,11 +260,13 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports, void(0));
module.constructor.invalidateByExports(mod.module.exports);
assert.equal(mod.module.exports, void(0));
var exports = mod.module.exports;
assert.equal(exports, void(0));
module.constructor.invalidateByExports(exports);
assert.equal(exports, void(0));
});
xit('exports type null', function() {
var mod = new utils.TmpModule(`

@@ -229,6 +276,8 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports === null, true);
module.constructor.invalidateByExports(mod.module.exports);
assert.equal(mod.module.exports === null, true);
var exports = mod.module.exports;
assert.equal(exports === null, true);
module.constructor.invalidateByExports(exports);
assert.equal(exports === null, true);
});

@@ -238,3 +287,3 @@

it('exports type primitive to primitive', function() {
val = '123';

@@ -245,11 +294,13 @@ var mod = new utils.TmpModule(_ => `

`);
assert.equal(mod.module.exports, 123);
var exports = mod.module.exports;
assert.equal(exports, 123);
val = '456';
mod.set();
mod.module.invalidate();
assert.equal(mod.module.exports, 456);
assert.equal(exports, 456);
});

@@ -259,3 +310,3 @@

it('exports type primitive to object', function() {
var val = '123';

@@ -266,8 +317,10 @@ var mod = new utils.TmpModule(_ => `

`);
assert.equal(mod.module.exports, 123);
var exports = mod.module.exports;
assert.equal(exports, 123);
val = '{ a:"bar" }';
mod.set();
mod.module.invalidate();
assert.equal(mod.module.exports.a, 'bar');
assert.equal(exports.a, 'bar');
});

@@ -277,3 +330,3 @@

it('exports type object to primitive', function() {
val = '{ a:"bar" }';

@@ -284,9 +337,11 @@ var mod = new utils.TmpModule(_ => `

`);
assert.equal(mod.module.exports.a, 'bar');
var exports = mod.module.exports;
assert.equal(exports.a, 'bar');
val = '456';
mod.set();
mod.module.invalidate();
assert.equal(mod.module.exports, 456);
assert.equal(exports, 456);
});

@@ -296,3 +351,3 @@

it('exports Object.keys', function() {
var mod = new utils.TmpModule(`

@@ -303,5 +358,7 @@ module.invalidable = true;

assert.equal(Object.keys(mod.module.exports).join(), 'a,b');
var exports = mod.module.exports;
assert.equal(Object.keys(exports).join(), 'a,b');
mod.module.invalidate();
assert.equal(Object.keys(mod.module.exports).join(), 'a,b');
assert.equal(Object.keys(exports).join(), 'a,b');
});

@@ -311,3 +368,3 @@

it('exports for-in', function() {
var mod = new utils.TmpModule(`

@@ -317,14 +374,16 @@ module.invalidable = true;

`);
var exports = mod.module.exports;
var res = '';
for ( var prop in mod.module.exports )
res += prop + mod.module.exports[prop];
for ( var prop in exports )
res += prop + exports[prop];
assert.equal(res, 'a1b2');
mod.module.invalidate();
for ( var prop in mod.module.exports )
res += prop + mod.module.exports[prop];
for ( var prop in exports )
res += prop + exports[prop];
assert.equal(res, 'a1b2a1b2');
});

@@ -334,3 +393,3 @@

it('exports for-of', function() {
var mod = new utils.TmpModule(`

@@ -340,12 +399,14 @@ module.invalidable = true;

`);
var exports = mod.module.exports;
var val = 0;
for ( var v of mod.module.exports )
for ( var v of exports )
val += v;
assert.equal(val, 6);
mod.module.invalidate();
for ( var v of mod.module.exports )
for ( var v of exports )
val += v;

@@ -358,10 +419,10 @@

it('exports this', function() {
var mod = new utils.TmpModule(`
module.invalidable = true;
function ctor() {
this.bar = 123;
this.foo = function() {
return this.bar;

@@ -372,18 +433,20 @@ }

`);
assert.equal(mod.module.exports.constructor.name, 'ctor');
assert.equal(mod.module.exports.foo(), 123);
var exports = mod.module.exports;
assert.equal(exports.constructor.name, 'ctor');
assert.equal(exports.foo(), 123);
mod.module.invalidate();
assert.equal(mod.module.exports.constructor.name, 'ctor');
assert.equal(mod.module.exports.foo(), 123);
assert.equal(exports.constructor.name, 'ctor');
assert.equal(exports.foo(), 123);
});
it('exports keep method name', function() {
var mod = new utils.TmpModule(`
module.invalidable = true;
function ctor() {
this.foo = function foo() {

@@ -397,17 +460,19 @@ }

`);
assert.equal(mod.module.exports.constructor.name, 'ctor');
assert.equal(mod.module.exports.foo.name, 'foo');
assert.equal(mod.module.exports.bar.name, '');
var exports = mod.module.exports;
assert.equal(exports.constructor.name, 'ctor');
assert.equal(exports.foo.name, 'foo');
assert.equal(exports.bar.name, '');
mod.module.invalidate();
assert.equal(mod.module.exports.constructor.name, 'ctor');
assert.equal(mod.module.exports.foo.name, 'foo');
assert.equal(mod.module.exports.bar.name, '');
assert.equal(exports.constructor.name, 'ctor');
assert.equal(exports.foo.name, 'foo');
assert.equal(exports.bar.name, '');
});
it('exports keep function name', function() {
var mod = new utils.TmpModule(`

@@ -419,11 +484,13 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports.name, 'myFct');
var exports = mod.module.exports;
assert.equal(exports.name, 'myFct');
mod.module.invalidate();
assert.equal(mod.module.exports.name, 'myFct');
assert.equal(exports.name, 'myFct');
});
it('property on function', function() {

@@ -433,3 +500,3 @@

module.invalidable = true;
var fct = function() { return 123 };

@@ -439,12 +506,14 @@ fct.bar = 456;

`);
assert.equal(typeof mod.module.exports.foo, 'function');
assert.equal(mod.module.exports.foo(), 123);
assert.equal(mod.module.exports.foo.bar, 456);
var exports = mod.module.exports;
assert.equal(typeof exports.foo, 'function');
assert.equal(exports.foo(), 123);
assert.equal(exports.foo.bar, 456);
mod.module.invalidate();
assert.equal(typeof mod.module.exports.foo, 'function');
assert.equal(mod.module.exports.foo(), 123);
assert.equal(mod.module.exports.foo.bar, 456);
assert.equal(typeof exports.foo, 'function');
assert.equal(exports.foo(), 123);
assert.equal(exports.foo.bar, 456);
});

@@ -457,3 +526,3 @@

module.invalidable = true;
var fct = function() {};

@@ -464,3 +533,3 @@ fct.bar = 456;

change: function() {
fct.bar = 789;

@@ -470,8 +539,10 @@ }

`);
assert.equal(mod.module.exports.foo.bar, 456);
var exports = mod.module.exports;
assert.equal(exports.foo.bar, 456);
mod.module.invalidate();
assert.equal(mod.module.exports.foo.bar, 456);
mod.module.exports.change();
assert.equal(mod.module.exports.foo.bar, 789);
assert.equal(exports.foo.bar, 456);
exports.change();
assert.equal(exports.foo.bar, 789);
});

@@ -484,3 +555,3 @@

module.invalidable = true;
var fct = function() {};

@@ -491,3 +562,3 @@ fct.bar = 456;

check: function() {
return fct.bar;

@@ -497,12 +568,14 @@ }

`);
assert.equal(mod.module.exports.foo.bar, 456);
var exports = mod.module.exports;
assert.equal(exports.foo.bar, 456);
mod.module.invalidate();
assert.equal(mod.module.exports.foo.bar, 456);
mod.module.exports.foo.bar = 789;
assert.equal(exports.foo.bar, 456);
exports.foo.bar = 789;
assert.equal(mod.module.exports.check(), 789);
assert.equal(exports.check(), 789);
});
it('property on function through the proxy (v1)', function() {

@@ -515,15 +588,17 @@

`);
assert.equal(typeof mod.module.exports.foo, 'function');
assert.equal(mod.module.exports.foo(), 123);
assert.equal(mod.module.exports.foo.bar, 456);
var exports = mod.module.exports;
assert.equal(typeof exports.foo, 'function');
assert.equal(exports.foo(), 123);
assert.equal(exports.foo.bar, 456);
mod.module.invalidate();
assert.equal(typeof mod.module.exports.foo, 'function');
assert.equal(mod.module.exports.foo(), 123);
assert.equal(mod.module.exports.foo.bar, 456);
assert.equal(typeof exports.foo, 'function');
assert.equal(exports.foo(), 123);
assert.equal(exports.foo.bar, 456);
});
it('property on function through the proxy (v2)', function() {

@@ -536,25 +611,29 @@

`);
assert.equal(typeof mod.module.exports.foo, 'function');
assert.equal(mod.module.exports.foo.bar, 456);
assert.equal(mod.module.exports.foo(), 123);
var exports = mod.module.exports;
assert.equal(typeof exports.foo, 'function');
assert.equal(exports.foo.bar, 456);
assert.equal(exports.foo(), 123);
mod.module.invalidate();
assert.equal(typeof mod.module.exports.foo, 'function');
assert.equal(mod.module.exports.foo.bar, 456);
assert.equal(mod.module.exports.foo(), 123);
assert.equal(typeof exports.foo, 'function');
assert.equal(exports.foo.bar, 456);
assert.equal(exports.foo(), 123);
});
it('exports json object', function() {
var val = 1;
var mod = new utils.TmpModule(_ =>`{ "a":${val} }`, { ext: 'json' });
mod.module.invalidable = true;
assert.equal(mod.module.exports.a, 1);
var exports = mod.module.exports;
assert.equal(exports.a, 1);
val++;

@@ -564,3 +643,3 @@ mod.set();

assert.equal(mod.module.exports.a, 2);
assert.equal(exports.a, 2);
});

@@ -570,10 +649,12 @@

it('exports json primitive to primitive', function() {
var val = 1;
var mod = new utils.TmpModule(_ =>`${val}`, { ext: 'json' });
mod.module.invalidable = true;
assert.equal(mod.module.exports, 1);
var exports = mod.module.exports;
assert.equal(exports, 1);
val = '"foo"';

@@ -583,5 +664,5 @@ mod.set();

assert.equal(mod.module.exports, 'foo');
assert.equal(exports, 'foo');
});
it('defined toString()', function() {

@@ -593,3 +674,3 @@

toString: function() {
return '123';

@@ -599,13 +680,15 @@ }

`);
assert.equal(''+mod.module.exports, '123');
assert.equal(+mod.module.exports, 123);
var exports = mod.module.exports;
assert.equal(''+exports, '123');
assert.equal(+exports, 123);
mod.module.invalidate();
assert.equal(''+mod.module.exports, '123');
assert.equal(+mod.module.exports, 123);
assert.equal(''+exports, '123');
assert.equal(+exports, 123);
});
it('defined valueOf()', function() {

@@ -617,3 +700,3 @@

valueOf: function() {
return 123;

@@ -623,8 +706,10 @@ }

`);
assert.equal(+mod.module.exports, 123);
var exports = mod.module.exports;
assert.equal(+exports, 123);
mod.module.invalidate();
assert.equal(+mod.module.exports, 123);
assert.equal(+exports, 123);
});

@@ -631,0 +716,0 @@

@@ -10,3 +10,3 @@ var assert = require('assert');

it('module non-invalidable', function() {
var mod = new utils.TmpModule(`

@@ -16,11 +16,13 @@ var count = 0;

`);
assert.equal(mod.module.exports.count(), 0);
var exports = mod.module.exports;
assert.equal(exports.count(), 0);
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.count(), 1);
assert.equal(exports.count(), 1);
});
it('module invalidable from outside', function() {
var mod = new utils.TmpModule(`

@@ -30,12 +32,14 @@ var count = 0;

`);
assert.equal(mod.module.exports.count(), 0);
mod.module.invalidable = true;
var exports = mod.module.exports;
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.count(), 0);
assert.equal(exports.count(), 0);
});
it('module invalidable from inside before exports', function() {
var mod = new utils.TmpModule(`

@@ -46,6 +50,8 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports.count(), 0);
var exports = mod.module.exports;
assert.equal(exports.count(), 0);
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.count(), 0);
assert.equal(exports.count(), 0);
});

@@ -55,3 +61,3 @@

it('module invalidable from inside after exports', function() {
var mod = new utils.TmpModule(`

@@ -62,11 +68,13 @@ var count = 0;

`);
assert.equal(mod.module.exports.count(), 0);
var exports = mod.module.exports;
assert.equal(exports.count(), 0);
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.count(), 0);
assert.equal(exports.count(), 0);
});
it('module invalidable then non-invalidable then invalidable from outside', function() {
var mod = new utils.TmpModule(`

@@ -76,16 +84,40 @@ var count = 0;

`);
assert.equal(mod.module.exports.count(), 0);
mod.module.invalidable = true;
var exports = mod.module.exports;
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.count(), 0);
assert.equal(exports.count(), 0);
mod.module.invalidable = false;
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.count(), 1);
assert.equal(exports.count(), 1);
mod.module.invalidable = true;
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.count(), 0);
assert.equal(exports.count(), 0);
});
it('module invalidable after export', function() {
var mod = new utils.TmpModule(`
var count = 0;
exports.count = function() { return count++; }
`);
var exports = mod.module.exports;
mod.module.invalidable = true;
assert.equal(exports.count(), 0);
mod.module.invalidate();
assert.equal(exports.count(), 1);
exports = mod.module.exports;
assert.equal(exports.count(), 0);
});
});

@@ -9,7 +9,7 @@ var assert = require('assert');

it('os.cpus without non-invalidable', function() {
var mod = new utils.TmpModule(`
module.exports = require('os');
`);
assert.equal(typeof mod.module.exports.cpus(), 'object');

@@ -22,4 +22,4 @@ mod.module.invalidate();

// see V8 issue https://bugs.chromium.org/p/v8/issues/detail?id=5773
xit('os.cpus', function() {
it('os.cpus', function() {
var mod = new utils.TmpModule(`

@@ -29,3 +29,3 @@ module.invalidable = true;

`);
assert.equal(typeof mod.module.exports.cpus(), 'object');

@@ -35,5 +35,5 @@ mod.module.invalidate();

});
xit('os.userInfo', function() {
it('os.userInfo', function() {
var mod = new utils.TmpModule(`

@@ -43,3 +43,3 @@ module.invalidable = true;

`);
assert.equal(typeof mod.module.exports.userInfo(), 'object');

@@ -51,3 +51,3 @@ mod.module.invalidate();

it('process.platform', function() {
var mod = new utils.TmpModule(`

@@ -57,3 +57,3 @@ module.invalidable = true;

`);
assert.equal(mod.module.exports, process.platform);

@@ -66,3 +66,3 @@ mod.module.invalidate();

it('os.cpus as exports', function() {
var mod = new utils.TmpModule(`

@@ -72,3 +72,3 @@ module.invalidable = true;

`);
assert.equal(typeof mod.module.exports(), 'object');

@@ -81,3 +81,3 @@ mod.module.invalidate();

it('os.type as exports', function() {
var mod = new utils.TmpModule(`

@@ -87,3 +87,3 @@ module.invalidable = true;

`);
assert.equal(typeof mod.module.exports(), 'string');

@@ -102,3 +102,3 @@ mod.module.invalidate();

`);
assert.equal(typeof mod.module.exports, 'function');

@@ -112,3 +112,3 @@ assert.equal(mod.module.exports(), 123);

it('exports Promise::then', function() {

@@ -120,12 +120,23 @@

`);
assert.equal(typeof mod.module.exports.then, 'function');
return mod.module.exports.then(function(result) {
assert.equal(result, 123);
});
});
it('exports Math', function() {
var mod = new utils.TmpModule(`
module.invalidable = true;
module.exports = Math;
`);
assert.equal(mod.module.exports.cos(0), 1);
});
it('exports Math.cos to a property', function() {

@@ -137,3 +148,3 @@

`);
assert.equal(typeof mod.module.exports.cos, 'function');

@@ -150,7 +161,7 @@ assert.equal(mod.module.exports.cos(0), 1);

`);
assert.equal(mod.module.exports(0), 1);
});
});

@@ -8,5 +8,5 @@ var assert = require('assert');

it('onInvalidate callback', function() {
var mod = new utils.TmpModule(`

@@ -18,3 +18,3 @@ module.invalidable = true;

`);
var pass = 0;

@@ -25,3 +25,3 @@ mod.module.exports(function() {

});
module.invalidateByPath(mod.module.filename);

@@ -35,3 +35,3 @@ module.invalidateByPath(mod.module.filename);

it('cancel onInvalidate', function() {
var mod = new utils.TmpModule(`

@@ -44,3 +44,3 @@ module.invalidable = true;

`);
var pass = 0;

@@ -51,3 +51,3 @@ mod.module.exports(function() {

});
module.invalidateByPath(mod.module.filename);

@@ -61,6 +61,6 @@ module.invalidateByPath(mod.module.filename);

it('onInvalidate callback immutableExports', function() {
var mod = new utils.TmpModule(`
module.invalidable = true;
var report;

@@ -73,5 +73,5 @@ module.exports = {

};
module.onInvalidate(function(immutableExports) {
report.invalidateCount++;

@@ -81,16 +81,16 @@ report.immutableExports = immutableExports;

`);
var report = {
invalidateCount: 0
};
mod.module.exports.setReport(report);
module.invalidateByPath(mod.module.filename);
assert.equal(report.invalidateCount, 1);
assert.equal(mod.module.exports.foo, 123);
assert.equal(report.immutableExports.foo, 123);
assert.notStrictEqual(module.exports, report.immutableExports);

@@ -101,3 +101,3 @@ });

it('onInvalidate + validate callbacks', function() {
var mod = new utils.TmpModule(`

@@ -107,5 +107,5 @@ module.invalidable = true;

this.connectedUsers = [];
exports.connectUser = function(name) {
this.connectedUsers.push(name);

@@ -115,10 +115,10 @@ }

exports.getConnectedUsers = function() {
return this.connectedUsers;
}
module.onInvalidate(function(immutableExports) {
return function(newExports) {
newExports.connectedUsers = immutableExports.connectedUsers;

@@ -128,4 +128,4 @@ }

`);
mod.module.exports.connectUser('foo');

@@ -135,10 +135,10 @@ mod.module.exports.connectUser('bar');

assert.equal(mod.module.exports.getConnectedUsers().join(), 'foo,bar');
module.invalidateByPath(mod.module.filename);
assert.equal(mod.module.exports.getConnectedUsers().join(), 'foo,bar');
});
});

@@ -9,3 +9,3 @@ var assert = require('assert');

it('unload no reload', function() {
var mod = new utils.TmpModule(_ => `

@@ -15,11 +15,11 @@ module.invalidable = true;

module.onInvalidate(function() {
module.exports.foo;
});
`);
assert.equal(mod.module.exports.foo, 1);
mod.module.unload();
assert.equal(mod.module, null);

@@ -30,3 +30,3 @@ });

it('access unloaded', function() {
var foo = 1;

@@ -37,12 +37,12 @@ var mod = new utils.TmpModule(_ => `

`);
var ref = mod.module.exports;
assert.equal(ref.foo, 1);
mod.module.unload();
foo++;
mod.set(undefined, { autoLoad: false });
assert.equal(ref.foo, 1);

@@ -49,0 +49,0 @@ });

@@ -10,6 +10,6 @@ const fs = require('fs');

function onEnd(...args) {
if ( args.length > 0 )
console.log(...args)
tmpFileList.forEach(filename => fs.unlinkSync(filename));

@@ -24,3 +24,3 @@ tmpFileList.clear();

exports.quoteString = function(str) {
return JSON.stringify(str);

@@ -30,3 +30,3 @@ }

exports.TmpModule = function(moduleContent, opts = {}) {
moduleIndex++;

@@ -37,6 +37,6 @@ var moduleContentFct = null;

this.filename = path.join(__dirname, `_tmp_mod${moduleIndex}.${ext}`);
Object.defineProperty(this, 'module', {
get: () => {
for ( var filename in Module._cache )

@@ -48,3 +48,3 @@ if ( filename === this.filename )

})
this.load = function() {

@@ -54,3 +54,3 @@

}
this.set = function(moduleContent, opts = {}) {

@@ -63,19 +63,19 @@

moduleContentFct = function() { return moduleContent }
moduleContent = moduleContentFct();
fs.writeFileSync(this.filename, moduleContent);
tmpFileList.add(this.filename);
if ( this.module === null && opts.autoLoad !== false )
this.load();
return this;
}
if ( moduleContent !== undefined )
this.set(moduleContent, opts);
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc