Comparing version 0.0.1 to 0.0.2
32
index.js
// cache the real JS module loader | ||
var jsLoader = require.extensions['.js'] | ||
// this will hold overrides keyed on module absolute path | ||
, resolvedPaths = {} | ||
, registeredOverrides = {} | ||
, overrides = {} | ||
@@ -13,3 +13,3 @@ ; | ||
*/ | ||
function applyOverrides(mod) { | ||
function applyOverrides(mod, modPath) { | ||
// save original current module path | ||
@@ -20,5 +20,5 @@ var originalPaths = module.paths; | ||
// go over exports and register all declared overrides | ||
Object.keys(module.exports).forEach(function (path) { | ||
if (!resolvedPaths[path]) { | ||
resolvedPaths[path] = module.exports[path]; | ||
Object.keys(module.exports).forEach(function (moduleName) { | ||
if (!registeredOverrides[moduleName]) { | ||
registeredOverrides[moduleName] = module.exports[moduleName]; | ||
} | ||
@@ -28,5 +28,19 @@ }); | ||
// go over declared overrides and look for them in current caller's path | ||
Object.keys(resolvedPaths).forEach(function (modName) { | ||
if (resolvedPaths[modName]) { | ||
overrides[require.resolve(modName)] = resolvedPaths[modName]; | ||
Object.keys(registeredOverrides).forEach(function (moduleName) { | ||
try { | ||
var modulePath = require.resolve(moduleName); | ||
overrides[modulePath] = registeredOverrides[moduleName]; | ||
} catch (e) { | ||
// maybe it's a relative-path module? let's try | ||
try { | ||
var relativeModuleName = mod.paths[0].split('/'); | ||
relativeModuleName = relativeModuleName[relativeModuleName.length-2]; | ||
if (relativeModuleName === moduleName) { | ||
// modPath is already resolved here, so we'll just use it | ||
overrides[modPath] = registeredOverrides[moduleName]; | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
@@ -41,3 +55,3 @@ }); | ||
require.extensions['.js'] = function (mod, modPath) { | ||
applyOverrides(mod); | ||
applyOverrides(mod, modPath); | ||
if (!overrides[modPath]) return jsLoader(mod, modPath); | ||
@@ -44,0 +58,0 @@ mod.exports = overrides[modPath]; |
@@ -18,4 +18,4 @@ { "name": "embargo" | ||
} | ||
, "version": "0.0.1" | ||
, "version": "0.0.2" | ||
, "main": "index.js" | ||
} | ||
} |
@@ -28,3 +28,3 @@ # Embargo | ||
By doing this, you're saying that `require('someModule')` will return an object with a `doStuff` property that you specified in `overrides` object, instead of actual module exports. | ||
This override will propagate to all `require` calls in the current VM. | ||
This override will propagate to all `require` calls in the current VM. It should also work for modules `require`d through relative path. | ||
@@ -31,0 +31,0 @@ # License |
@@ -10,4 +10,11 @@ var embargo = require('../'); | ||
embargo['relative_mod'] = { | ||
'getValue': function () { | ||
return 'Fake relative_mod'; | ||
} | ||
}; | ||
var mod1 = require('./lib/mod1/mod1') | ||
, mod2 = require('./lib/mod2/mod2') | ||
, mod3 = require('./lib/mod3/mod3') | ||
, assert = require('assert') | ||
@@ -22,2 +29,18 @@ ; | ||
}) | ||
}); | ||
describe('Exports of mod2', function() { | ||
it('should not equal to exports of relative_mod', function(done){ | ||
assert.equal(mod2(), 'Fake relative_mod'); | ||
done(); | ||
}) | ||
}); | ||
describe('Exports of mod3', function() { | ||
it('should not equal to exports of dir/relative_mod', function(done){ | ||
assert.equal(mod3(), 'Fake relative_mod'); | ||
done(); | ||
}) | ||
}); |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
6066
16
101