wipe-node-cache
Advanced tools
Comparing version 2.1.1 to 2.1.2
var path = require('path'), | ||
wipe = require("../index"); | ||
wipe = require("../src"); | ||
@@ -28,2 +28,2 @@ | ||
return !(moduleName.indexOf('node_modules') > 0) && !(moduleName.indexOf('core') > 0) | ||
}); | ||
}); |
var path = require('path'), | ||
wipe = require("../index"); | ||
wipe = require("../src"); | ||
@@ -14,2 +14,2 @@ | ||
return !resolver(null, moduleName); | ||
}); | ||
}); |
@@ -28,8 +28,43 @@ "use strict"; | ||
function removeModuleFromParent(parent, removedChild) { | ||
if (parent && parent.children && removedChild) { | ||
parent.children = parent.children.filter(function (child) { | ||
return child !== removedChild; | ||
}); | ||
} | ||
} | ||
function burn(cache, wipeList, lookup, callback) { | ||
var removeFromCache = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : removeFromCache_nodejs; | ||
var parentReference = {}; | ||
var remove = function remove(moduleName) { | ||
var lookupcache = lookup[moduleName]; | ||
var module = cache[moduleName]; | ||
delete parentReference[moduleName]; | ||
if (lookupcache) { | ||
lookupcache.parents.forEach(function (parent) { | ||
if (!parentReference[parent]) { | ||
parentReference[parent] = []; | ||
} | ||
// set a flag to remove this module from a parent record | ||
parentReference[parent].push(module); | ||
wipeList.push(parent); | ||
}); | ||
delete lookup[moduleName]; | ||
} | ||
removeFromCache(moduleName); | ||
}; | ||
// primary wave | ||
// execute what was set to be removed | ||
var removeList = wipeList; | ||
wipeList = []; | ||
removeList.forEach(remove); | ||
// Secondary wave | ||
// remove parents of evicted modules while possible | ||
while (wipeList.length) { | ||
var removeList = wipeList; | ||
removeList = wipeList; | ||
wipeList = []; | ||
@@ -39,11 +74,13 @@ | ||
if (callback(moduleName)) { | ||
if (lookup[moduleName]) { | ||
wipeList.push.apply(wipeList, lookup[moduleName].parents); | ||
delete lookup[moduleName]; | ||
} | ||
removeFromCache(moduleName); | ||
remove(moduleName); | ||
} | ||
}); | ||
} | ||
// post cleanup - remove references from parent | ||
Object.keys(parentReference).forEach(function (parent) { | ||
return parentReference[parent].forEach(function (child) { | ||
return removeModuleFromParent(cache[parent], child); | ||
}); | ||
}); | ||
} | ||
@@ -55,10 +92,2 @@ | ||
function reverseString(str) { | ||
var result = ""; | ||
for (var i = str.length - 1; i >= 0; i--) { | ||
result += str[i]; | ||
} | ||
return result; | ||
} | ||
function buildIndexForward(cache) { | ||
@@ -100,3 +129,2 @@ return Object.keys(cache); | ||
callback(compositeIndex, function (name) { | ||
removeFromCache(name); | ||
wipeList.push(name); | ||
@@ -103,0 +131,0 @@ }); |
{ | ||
"name": "wipe-node-cache", | ||
"version": "2.1.1", | ||
"description": "Wipes node.js cache in a controled way.", | ||
"version": "2.1.2", | ||
"description": "Wipes node.js cache in a controlled way.", | ||
"main": "lib/index.js", | ||
@@ -21,3 +21,4 @@ "scripts": { | ||
"mock", | ||
"stub" | ||
"stub", | ||
"clean cache" | ||
], | ||
@@ -24,0 +25,0 @@ "author": "Anton Korzunov", |
@@ -10,3 +10,3 @@ function waveCallback_default() { | ||
function assignParents(modules) { | ||
var result = {}; | ||
const result = {}; | ||
Object.keys(modules).forEach(function (moduleName) { | ||
@@ -23,6 +23,39 @@ const parent = modules[moduleName]; | ||
function removeModuleFromParent(parent, removedChild) { | ||
if(parent && parent.children && removedChild) { | ||
parent.children = parent.children.filter(child => child !== removedChild); | ||
} | ||
} | ||
function burn(cache, wipeList, lookup, callback, removeFromCache = removeFromCache_nodejs) { | ||
// Secondary wave | ||
const parentReference = {}; | ||
const remove = (moduleName) => { | ||
const lookupcache = lookup[moduleName]; | ||
const module = cache[moduleName]; | ||
delete parentReference[moduleName]; | ||
if (lookupcache) { | ||
lookupcache.parents.forEach(parent => { | ||
if (!parentReference[parent]) { | ||
parentReference[parent]=[] | ||
} | ||
// set a flag to remove this module from a parent record | ||
parentReference[parent].push(module); | ||
wipeList.push(parent); | ||
}); | ||
delete lookup[moduleName]; | ||
} | ||
removeFromCache(moduleName); | ||
} | ||
// primary wave | ||
// execute what was set to be removed | ||
let removeList = wipeList; | ||
wipeList = []; | ||
removeList.forEach(remove); | ||
// Secondary wave | ||
// remove parents of evicted modules while possible | ||
while (wipeList.length) { | ||
var removeList = wipeList; | ||
removeList = wipeList; | ||
wipeList = []; | ||
@@ -32,11 +65,11 @@ | ||
if (callback(moduleName)) { | ||
if (lookup[moduleName]) { | ||
wipeList.push.apply(wipeList, lookup[moduleName].parents); | ||
delete lookup[moduleName]; | ||
} | ||
removeFromCache(moduleName); | ||
remove(moduleName); | ||
} | ||
}); | ||
} | ||
// post cleanup - remove references from parent | ||
Object.keys(parentReference).forEach( | ||
(parent) => parentReference[parent].forEach(child => removeModuleFromParent(cache[parent], child)) | ||
); | ||
} | ||
@@ -48,10 +81,2 @@ | ||
function reverseString(str) { | ||
var result = ""; | ||
for (var i = str.length - 1; i >= 0; i--) { | ||
result += str[i]; | ||
} | ||
return result; | ||
} | ||
function buildIndexForward(cache) { | ||
@@ -94,3 +119,2 @@ return Object.keys(cache); | ||
callback(compositeIndex, name => { | ||
removeFromCache(name); | ||
wipeList.push(name); | ||
@@ -97,0 +121,0 @@ }); |
@@ -6,3 +6,3 @@ /*jshint asi:true*/ | ||
var assert = require('assert'); | ||
const {wipeCache:wipe} = require('../src/index'); | ||
const {wipeCache:wipe} = require('../src'); | ||
@@ -23,2 +23,15 @@ var stubs = { | ||
function collectModules() { | ||
const modules = require.cache; | ||
var result = {}; | ||
Object.keys(modules).forEach(function (moduleName) { | ||
const parent = modules[moduleName]; | ||
const line = parent.children || []; | ||
line.forEach(({id: childName}) => { | ||
result[childName] = true; | ||
}); | ||
}); | ||
return Object.keys(result); | ||
} | ||
describe('cache wipe', function () { | ||
@@ -73,2 +86,6 @@ describe('standart flow', function () { | ||
// A and B are in the cache | ||
assert.notEqual(collectModules().indexOf(require.resolve('./src/c.js')), -1); | ||
assert.notEqual(collectModules().indexOf(require.resolve('./src/b.js')), -1); | ||
wipe(null, function (stub, fileName) { | ||
@@ -78,2 +95,6 @@ return fileName.indexOf('test/src/c') > 0 | ||
// A and B are in no longer the cache | ||
assert.equal(collectModules().indexOf(require.resolve('./src/c.js')), -1); | ||
assert.equal(collectModules().indexOf(require.resolve('./src/b.js')), -1); | ||
var c = require('./src/c.js'); | ||
@@ -94,2 +115,6 @@ assert.equal(c.fn(), 1); | ||
// A and B are in the cache | ||
assert.notEqual(collectModules().indexOf(require.resolve('./src/c.js')), -1); | ||
assert.notEqual(collectModules().indexOf(require.resolve('./src/b.js')), -1); | ||
wipe(null, function (stub, fileName) { | ||
@@ -101,2 +126,7 @@ return fileName.indexOf('test/src/c') > 0 | ||
// C not in | ||
assert.equal(collectModules().indexOf(require.resolve('./src/c.js')), -1); | ||
// B is in | ||
assert.notEqual(collectModules().indexOf(require.resolve('./src/b.js')), -1); | ||
var c = require('./src/c.js'); | ||
@@ -103,0 +133,0 @@ assert.equal(c.fn(), 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
17404
395