Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

require-inject

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

require-inject - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

test/injection-leaking-1.js

27

index.js

@@ -5,10 +5,21 @@ "use strict";

module.exports = function (toLoad, mocks) {
// Copy the existing cache
var originalCache = {}
Object.keys(require.cache).forEach(function(name) {
originalCache[name] = require.cache[name]
})
// Inject all of our mocks
Object.keys(mocks).forEach(function(name){
var path = require.resolve(name)
require.cache[path] = {exports: mocks[name]}
if (mocks[name] == null) {
delete require.cache[path]
}
else {
require.cache[path] = {exports: mocks[name]}
}
})
if (/^[.][.]?\//.test(toLoad)) {
toLoad = path.resolve(path.dirname(module.parent.filename), toLoad)
toLoad = path.resolve(path.dirname(module.parent.filename), toLoad)
}

@@ -19,12 +30,12 @@ var toLoadPath = require.resolve(toLoad)

delete require.cache[toLoadPath]
// load our new version using our mocks
var mocked = module.parent.require(toLoadPath)
// remove our version from the cache so anyone else gets the real thing
delete require.cache[toLoadPath]
// Remove our injected mocks
Object.keys(mocks).forEach(function(name){
delete require.cache[require.resolve(name)]
})
// restore the cache, we can't just assign originalCache to require.cache as the require
// object is unique to each module, even though require.cache is shared
Object.keys(require.cache).forEach(function(name){ delete require.cache[name] })
Object.keys(originalCache).forEach(function(name){ require.cache[name] = originalCache[name] })
return mocked
}
{
"name": "require-inject",
"version": "1.1.0",
"version": "1.1.1",
"description": "A simple mock injector compatible needing no instrumentation in the libraries being tested",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "tap test/*.js"
},

@@ -21,3 +21,6 @@ "author": "Rebecca Turner <me@re-becca.org> (http://re-becca.org)",

},
"license": "ISC"
"license": "ISC",
"devDependencies": {
"tap": "^0.4.13"
}
}

Sorry, the diff of this file is not supported yet

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