Skip to content

Commit

Permalink
Merge pull request #13 from osukaa/fix-node-8-problem
Browse files Browse the repository at this point in the history
Fix node 8 recursive problem
  • Loading branch information
gabrielcsapo authored Nov 9, 2017
2 parents 724b3e8 + 8085bd9 commit 18e27c3
Show file tree
Hide file tree
Showing 3 changed files with 338 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ node_js:
- "0.12"
- "0.10"
- "iojs"
- "node"
- "lts/argon"
- "lts/boron"
- "lts/carbon"

script:
- "npm run-script cover"
Expand Down
48 changes: 44 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ var path = require('path'),
isAbsolute = require('path-is-absolute'),
resolve = require('resolve');

/**
* Array.includes polyfill
* @param {string[]} array
* @param {string} searchElement
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Polyfill
*/
function includes(array, searchElement, fromIndex) {
var len = array.length >>> 0;

if (len === 0) {
return false;
}

var n = fromIndex | 0;
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

function sameValueZero(x, y) {
return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
}

while (k < len) {
if (sameValueZero(array[k], searchElement)) {
return true;
}
k++;
}

return false;
}

/**
* Gets a reference to a unique instance of a given module.
Expand All @@ -17,6 +46,7 @@ function getFresh(name, cb) {
var orig, fresh;

orig = snapshot(name);

unload(name);

fresh = require(name);
Expand Down Expand Up @@ -48,12 +78,16 @@ function reload(name) {
* @param module module name or absolute path
* @returns {boolean} true if the module was removed or false if not
*/
function unload(module) {
function unload(module, stack) {
var path = require.resolve(module);
stack = stack || [];

if (require.cache[path] && require.cache[path].children) {
require.cache[path].children.forEach(function (child) {
unload(child.id);
if (!includes(stack, child.id)) {
stack.push(path);
unload(child.id, stack);
}
});
}

Expand All @@ -75,9 +109,15 @@ function snapshot(module) {
* Add a module entry back into the cache
* @param module module cache object
*/
function restore(module) {
function restore(module, stack) {
stack = stack || [];
require.cache[module.id] = module;
module.children.forEach(restore);
module.children.forEach(function (child) {
if (!includes(stack, child.id)) {
stack.push(child.id);
restore(child, stack);
}
});
}

function normalize(fn) {
Expand Down
Loading

0 comments on commit 18e27c3

Please sign in to comment.