module-invalidate
Advanced tools
Comparing version 0.9.7 to 0.9.8
62
index.js
@@ -10,8 +10,26 @@ 'use strict'; | ||
const validateCallbacksSym = Symbol(); | ||
const invalid = Symbol(); | ||
function identityFct(value) { | ||
function toPrimitive(value) { | ||
var valueToPrimitive = value[Symbol.toPrimitive]; | ||
if ( typeof(valueToPrimitive) === 'function' ) | ||
return valueToPrimitive; | ||
return function() { | ||
return function(hint) { | ||
return value; | ||
if ( hint === 'number' ) | ||
return Number(value); | ||
if ( hint === 'string' ) | ||
return String(value); | ||
if ( typeof(value) === 'object' ) { | ||
var val = value.valueOf(); | ||
if ( typeof(val) === 'object' ) | ||
return String(val); | ||
return val; | ||
} | ||
return value; | ||
} | ||
@@ -34,3 +52,3 @@ } | ||
Object.setPrototypeOf(bound, fct); // see test "exports property on function" | ||
delete bound.name; // preserves the bound function name | ||
delete bound.name; // preserves the original function name | ||
return bound; | ||
@@ -77,3 +95,3 @@ } | ||
this._exports = null; | ||
this._exports = invalid; | ||
} | ||
@@ -107,3 +125,3 @@ | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.getPrototypeOf(mod._exports); | ||
@@ -114,3 +132,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.setPrototypeOf(mod._exports, prototype); | ||
@@ -121,3 +139,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.isExtensible(mod._exports); | ||
@@ -128,3 +146,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.preventExtensions(mod._exports); | ||
@@ -135,3 +153,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
@@ -145,3 +163,3 @@ if ( prop === 'prototype' && typeof(mod._exports) !== 'function' ) // see ownKeys | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.defineProperty(mod._exports, property, descriptor); | ||
@@ -152,3 +170,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.has(mod._exports, prop); | ||
@@ -159,9 +177,8 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
if ( property === Symbol.hasInstance ) | ||
return hasInstance(mod._exports); | ||
else | ||
if ( property === Symbol.toPrimitive ) | ||
return identityFct(mod._exports); | ||
return toPrimitive(mod._exports); | ||
@@ -171,5 +188,2 @@ // see http://stackoverflow.com/questions/42496414/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 | ||
//return Reflect.get(mod._exports, property, receiver); // fails with native functions | ||
// see V8 issue https://bugs.chromium.org/p/v8/issues/detail?id=5773 | ||
@@ -179,3 +193,3 @@ | ||
if ( typeof(val) === 'function' /*&& val.prototype === undefined*/ ) { // native function has prototype === undefined | ||
if ( typeof(val) === 'function' && !('prototype' in val) ) { // native function has prototype === undefined | ||
@@ -192,3 +206,3 @@ // needed for native function, like Promise.resolve().then, ... | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.set(mod._exports, property, value); | ||
@@ -199,3 +213,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.deleteProperty(mod._exports, property); | ||
@@ -206,3 +220,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
// see https://tc39.github.io/ecma262/#sec-invariants-of-the-essential-internal-methods | ||
@@ -217,3 +231,3 @@ var ownKeys = Reflect.ownKeys(mod._exports); | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.apply(mod._exports, thisArg, argumentsList); | ||
@@ -224,3 +238,3 @@ }, | ||
mod._exports === null && reload(mod); | ||
mod._exports === invalid && reload(mod); | ||
return Reflect.construct(mod._exports, argumentsList, newTarget); | ||
@@ -227,0 +241,0 @@ } |
{ | ||
"name": "module-invalidate", | ||
"version": "0.9.7", | ||
"version": "0.9.8", | ||
"description": "invalidate required modules", | ||
"keywords": [ | ||
@@ -15,9 +12,5 @@ "module", | ||
], | ||
"author": "Franck Freiburger", | ||
"license": "MIT", | ||
"homepage": "https://github.com/FranckFreiburger/module-invalidate#readme", | ||
"repository": { | ||
@@ -27,7 +20,5 @@ "type": "git", | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/FranckFreiburger/module-invalidate/issues" | ||
}, | ||
"main": "index.js", | ||
@@ -37,8 +28,6 @@ "scripts": { | ||
}, | ||
"devDependencies": { | ||
"benchmark": "^2.1.3", | ||
"ffi": "^2.2.0", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
@@ -9,3 +9,3 @@ var assert = require('assert'); | ||
it('non-invalidable exports type', function() { | ||
it('non-invalidable exports object type', function() { | ||
@@ -24,2 +24,16 @@ var mod = new utils.TmpModule(` | ||
it('non-invalidable exports function type', function() { | ||
var mod = new utils.TmpModule(` | ||
module.exports = function(){} | ||
`); | ||
assert.equal(typeof mod.module.exports, 'function'); | ||
mod.module.invalidate(); | ||
assert.equal(typeof mod.module.exports, 'function'); | ||
}); | ||
it('invalidable exports type', function() { | ||
@@ -48,2 +62,3 @@ | ||
`); | ||
assert.equal(mod.module.exports.foo, 'bar'); | ||
@@ -87,2 +102,3 @@ | ||
`); | ||
assert.equal(new mod.module.exports().getValue(), 123); | ||
@@ -134,3 +150,4 @@ | ||
it('exports type primitive', function() { | ||
it('exports type string primitive', function() { | ||
@@ -148,2 +165,57 @@ var mod = new utils.TmpModule(` | ||
it('exports type boolean primitive', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = true; | ||
`); | ||
assert.equal(mod.module.exports, true); | ||
module.constructor.invalidateByExports(mod.module.exports); | ||
assert.equal(mod.module.exports, true); | ||
}); | ||
it('exports type null-prototype object', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = Object.create(null); | ||
module.exports.foo = 123; | ||
`); | ||
assert.equal(Object.getPrototypeOf(mod.module.exports), null); | ||
assert.equal(mod.module.exports.foo, 123); | ||
mod.module.invalidate(); | ||
assert.equal(Object.getPrototypeOf(mod.module.exports), null); | ||
assert.equal(mod.module.exports.foo, 123); | ||
}); | ||
xit('exports type void(0) primitive', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = void(0); | ||
`); | ||
assert.equal(mod.module.exports, void(0)); | ||
module.constructor.invalidateByExports(mod.module.exports); | ||
assert.equal(mod.module.exports, void(0)); | ||
}); | ||
xit('exports type null', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = null; | ||
`); | ||
assert.equal(mod.module.exports === null, true); | ||
module.constructor.invalidateByExports(mod.module.exports); | ||
assert.equal(mod.module.exports === null, true); | ||
}); | ||
it('exports type primitive to primitive', function() { | ||
@@ -267,5 +339,3 @@ | ||
return this.bar; | ||
} | ||
} | ||
@@ -348,4 +418,53 @@ module.exports = new ctor; | ||
}); | ||
it('changing property on function from the module', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
var fct = function() {}; | ||
fct.bar = 456; | ||
module.exports = { | ||
foo: fct, | ||
change: function() { | ||
fct.bar = 789; | ||
} | ||
} | ||
`); | ||
assert.equal(mod.module.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); | ||
}); | ||
it('changing property on function from the outside', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
var fct = function() {}; | ||
fct.bar = 456; | ||
module.exports = { | ||
foo: fct, | ||
check: function() { | ||
return fct.bar; | ||
} | ||
} | ||
`); | ||
assert.equal(mod.module.exports.foo.bar, 456); | ||
mod.module.invalidate(); | ||
assert.equal(mod.module.exports.foo.bar, 456); | ||
mod.module.exports.foo.bar = 789; | ||
assert.equal(mod.module.exports.check(), 789); | ||
}); | ||
it('property on function through the proxy (v1)', function() { | ||
@@ -418,12 +537,51 @@ | ||
val++; | ||
val = '"foo"'; | ||
mod.set(); | ||
mod.module.invalidate(); | ||
assert.equal(mod.module.exports, 2); | ||
assert.equal(mod.module.exports, 'foo'); | ||
}); | ||
it('defined toString()', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = { | ||
toString: function() { | ||
return '123'; | ||
} | ||
} | ||
`); | ||
//assert.throws(function() { mod.module.exports.toString() }, TypeError); | ||
assert.equal(''+mod.module.exports, '123'); | ||
assert.equal(+mod.module.exports, 123); | ||
mod.module.invalidate(); | ||
assert.equal(''+mod.module.exports, '123'); | ||
assert.equal(+mod.module.exports, 123); | ||
}); | ||
it('defined valueOf()', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = { | ||
valueOf: function() { | ||
return 123; | ||
} | ||
} | ||
`); | ||
assert.equal(+mod.module.exports, 123); | ||
mod.module.invalidate(); | ||
assert.equal(+mod.module.exports, 123); | ||
}); | ||
}); |
@@ -15,5 +15,9 @@ var assert = require('assert'); | ||
assert.equal(typeof mod.module.exports.cpus(), 'object'); | ||
mod.module.invalidate(); | ||
assert.equal(typeof mod.module.exports.cpus(), 'object'); | ||
}); | ||
it('os.cpus', function() { | ||
// see V8 issue https://bugs.chromium.org/p/v8/issues/detail?id=5773 | ||
xit('os.cpus', function() { | ||
@@ -26,5 +30,31 @@ var mod = new utils.TmpModule(` | ||
assert.equal(typeof mod.module.exports.cpus(), 'object'); | ||
mod.module.invalidate(); | ||
assert.equal(typeof mod.module.exports.cpus(), 'object'); | ||
}); | ||
xit('os.userInfo', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = require('os'); | ||
`); | ||
assert.equal(typeof mod.module.exports.userInfo(), 'object'); | ||
mod.module.invalidate(); | ||
assert.equal(typeof mod.module.exports.userInfo(), 'object'); | ||
}); | ||
it('process.platform', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = process.platform; | ||
`); | ||
assert.equal(mod.module.exports, process.platform); | ||
mod.module.invalidate(); | ||
assert.equal(mod.module.exports, process.platform); | ||
}); | ||
it('os.cpus as exports', function() { | ||
@@ -38,2 +68,4 @@ | ||
assert.equal(typeof mod.module.exports(), 'object'); | ||
mod.module.invalidate(); | ||
assert.equal(typeof mod.module.exports(), 'object'); | ||
}); | ||
@@ -50,16 +82,7 @@ | ||
assert.equal(typeof mod.module.exports(), 'string'); | ||
mod.module.invalidate(); | ||
assert.equal(typeof mod.module.exports(), 'string'); | ||
}); | ||
it('lib ffi', function() { | ||
var ffi = require('ffi'); | ||
var libm = new ffi.Library('msvcrt', { | ||
'ceil': [ 'double', [ 'double' ] ] | ||
}); | ||
assert.equal(libm.ceil(1.1), 2); | ||
}); | ||
it('Function::bind', function() { | ||
@@ -75,2 +98,5 @@ | ||
assert.equal(mod.module.exports(), 123); | ||
mod.module.invalidate(); | ||
assert.equal(typeof mod.module.exports, 'function'); | ||
assert.equal(mod.module.exports(), 123); | ||
}); | ||
@@ -95,3 +121,26 @@ | ||
it('exports Math.cos to a property', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports.cos = Math.cos; | ||
`); | ||
assert.equal(typeof mod.module.exports.cos, 'function'); | ||
assert.equal(mod.module.exports.cos(0), 1); | ||
}); | ||
it('exports Math.cos directly', function() { | ||
var mod = new utils.TmpModule(` | ||
module.invalidable = true; | ||
module.exports = Math.cos; | ||
`); | ||
assert.equal(mod.module.exports(0), 1); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45011
2
1057