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 0.9.4 to 0.9.5

40

index.js

@@ -7,2 +7,7 @@ 'use strict';

const boundCachedSym = Symbol();
const invalidateCallbacksSym = Symbol();
const validateCallbacksSym = Symbol();
Module.invalidate = function() {

@@ -32,6 +37,13 @@

if ( '_invalidateCallbacks' in this ) {
if ( invalidateCallbacksSym in this ) {
var validateCallbacks = this[validateCallbacksSym] || (this[validateCallbacksSym] = new Set);
this._invalidateCallbacks.forEach(callback => callback(this._exports));
this._invalidateCallbacks.clear();
this[invalidateCallbacksSym].forEach(callback => {
var validateCallback = callback(this._exports);
if ( typeof(validateCallback) === 'function' )
validateCallbacks.add(validateCallback);
});
this[invalidateCallbacksSym].clear();
}

@@ -44,3 +56,3 @@

var invalidateCallbacks = this._invalidateCallbacks || (this._invalidateCallbacks = new Set);
var invalidateCallbacks = this[invalidateCallbacksSym] || (this[invalidateCallbacksSym] = new Set);
return invalidateCallbacks.add(callback).delete.bind(invalidateCallbacks, callback);

@@ -54,5 +66,10 @@ }

mod.load(mod.filename);
if ( validateCallbacksSym in mod ) {
mod[validateCallbacksSym].forEach(callback => callback(mod._exports) );
mod[validateCallbacksSym].clear();
}
}
const boundCached = Symbol();

@@ -123,7 +140,7 @@ function createProxy(mod) {

// needed for native function, like Promise.resolve().then, ...
if ( boundCached in val )
return val[boundCached];
if ( boundCachedSym in val )
return val[boundCachedSym];
var bound = val.bind(mod._exports);
Object.setPrototypeOf(bound, val); // see test "exports property on function"
val[boundCached] = bound;
val[boundCachedSym] = bound;
return bound;

@@ -151,5 +168,6 @@ }

// see https://tc39.github.io/ecma262/#sec-invariants-of-the-essential-internal-methods
//throw new TypeError('ownKeys not implemented');
return Reflect.ownKeys(target).concat(Reflect.ownKeys(mod._exports));
//return Reflect.ownKeys(mod._exports);
var ownKeys = Reflect.ownKeys(mod._exports);
if ( typeof mod._exports !== 'function' )
ownKeys.push('prototype');
return ownKeys;
},

@@ -156,0 +174,0 @@

{
"name": "module-invalidate",
"version": "0.9.4",
"version": "0.9.5",
"description": "invalidate required modules",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -236,4 +236,33 @@ # module-invalidate

Gives you the opportunity to free resources created in the module.
eg. temporary files, timers, web routes, ...
The `callback` can return function that is called after the module is reloaded. This can help you restore your module state.
##### Example:
```JavaScript
module.invalidable = true;
this.connectedUsers = [];
exports.connectUser = function(name) {
this.connectedUsers.push(name);
}
exports.getConnectedUsers = function() {
return this.connectedUsers;
}
module.onInvalidate(function(oldExports) {
return function(newExports) {
newExports.connectedUsers = oldExports.connectedUsers;
}
});
```
## How it works

@@ -240,0 +269,0 @@

@@ -180,2 +180,19 @@ var assert = require('assert');

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

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

});
it('onInvalidate callback all', function() {
var mod = new utils.TmpModule(`
module.invalidable = true;
this.connectedUsers = [];
exports.connectUser = function(name) {
this.connectedUsers.push(name);
}
exports.getConnectedUsers = function() {
return this.connectedUsers;
}
module.onInvalidate(function(immutableExports) {
return function(newExports) {
newExports.connectedUsers = immutableExports.connectedUsers;
}
});
`);
mod.module.exports.connectUser('foo');
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');
});
});

@@ -23,3 +23,3 @@ const fs = require('fs');

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

@@ -29,3 +29,4 @@ moduleIndex++;

this.filename = path.join(__dirname, `_tmp_mod${moduleIndex}.js`);
var ext = opts.ext || 'js';
this.filename = path.join(__dirname, `_tmp_mod${moduleIndex}.${ext}`);

@@ -32,0 +33,0 @@ Object.defineProperty(this, 'module', {

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