Comparing version 0.2.2 to 0.3.0
@@ -1,3 +0,1 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
/** | ||
@@ -7,2 +5,5 @@ * This function will be stringified and then injected into every rewired module. | ||
* | ||
* All variables within this function are namespaced in the arguments array because every | ||
* var declaration could possibly clash with a variable in the module scope. | ||
* | ||
* @param {!String} name name of the variable to retrieve | ||
@@ -12,8 +13,11 @@ * @throws {TypeError} | ||
*/ | ||
module.exports = function __get__(name) { | ||
if (typeof name !== "string" || name.length === 0) { | ||
function __get__() { | ||
arguments.varName = arguments[0]; | ||
if (typeof arguments.varName !== "string" || arguments.varName.length === 0) { | ||
throw new TypeError("__get__ expects a non-empty string"); | ||
} | ||
return eval(name); | ||
}; | ||
return eval(arguments.varName); | ||
} | ||
module.exports = __get__; |
@@ -1,3 +0,1 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
/** | ||
@@ -13,10 +11,12 @@ * This function will be stringified and then injected into every rewired module. | ||
* @throws {TypeError} | ||
* @throws {ReferenceError} When the variable is unknown | ||
* @return {*} | ||
*/ | ||
module.exports = function __set__() { | ||
function __set__() { | ||
arguments.varName = arguments[0]; | ||
arguments.varValue = arguments[1]; | ||
arguments.src = ""; | ||
arguments.checkExistsSrc = function (varName) { | ||
return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('" + varName + " is not defined');} "; | ||
arguments.checkExistsSrc = function (varName, varValue) { | ||
return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('Cannot __set__(" + varName + ", " + varValue + "): " + | ||
varName + " is not declared within the module.'); } "; | ||
}; | ||
@@ -38,3 +38,3 @@ | ||
} | ||
arguments.src = arguments.checkExistsSrc(arguments.varName) + arguments.varName + " = arguments.varValue;"; | ||
arguments.src = arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.varValue;"; | ||
} else { | ||
@@ -45,2 +45,4 @@ throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name"); | ||
eval(arguments.src); | ||
}; | ||
} | ||
module.exports = __set__; |
@@ -1,3 +0,1 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
/** | ||
@@ -4,0 +2,0 @@ * Declares all globals with a var and assigns the global object. Thus you're able to |
@@ -1,19 +0,12 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
var rewireModule; | ||
var rewireModule = require("./rewire.js"); | ||
/** | ||
* This function is needed to determine the calling parent module. | ||
* Thus rewire acts exactly the same like require() in the test module. | ||
* Adds a special setter and getter to the module located at filename. After the module has been rewired, you can | ||
* call myModule.__set__(name, value) and myModule.__get__(name) to manipulate private variables. | ||
* | ||
* @param {!String} request Path to the module that shall be rewired. Use it exactly like require(). | ||
* @param {Object} mocks An object with mocks. Keys should be the exactly same like they're required in the target module. So if you write require("../../myModules/myModuleA.js") you need to pass {"../../myModules/myModuleA.js": myModuleAMock}. | ||
* @param {Object} injections If you pass an object, all keys of the object will be vars within the module. You can also eval a string. Please note: All scripts are injected at the end of the module. So if there is any code in your module that is executed during require(), your injected variables will be undefined at this point. For example: passing {console: {...}} will cause all calls of console.log() to throw an exception if they're executed during require(). | ||
* @param {Array} leaks An array with variable names that should be exported. These variables are accessible via myModule.__ | ||
* @param {!String} filename Path to the module that shall be rewired. Use it exactly like require(). | ||
* @param {Boolean} cache Indicates whether the rewired module should be cached by node so subsequent calls of require() will return the rewired module. Subsequent calls of rewire() will always overwrite the cache. | ||
* @return {*} the rewired module | ||
*/ | ||
function rewire(request, cache) { | ||
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date | ||
function rewire(filename, cache) { | ||
if (cache === undefined) { | ||
@@ -23,7 +16,19 @@ cache = true; | ||
return rewireModule(module.parent, request, cache); | ||
return rewireModule(module.parent, filename, cache); | ||
} | ||
rewire.reset = rewireModule.reset; | ||
// Conditional require for different environments | ||
if (process.title === "browser") { | ||
module.exports = require("./browserify/browserifyRewire.js"); | ||
} else { | ||
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date | ||
module.exports = rewire; | ||
// Putting (require) within brackets is a hack to disable browserify's require sniffing | ||
// @see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470 | ||
rewireModule = (require)("./internalRewire.js"); | ||
rewire.reset = rewireModule.reset; | ||
rewire.browserify = (require)("./browserify/browserifyMiddleware.js"); | ||
module.exports = rewire; | ||
} |
{ | ||
"name" : "rewire", | ||
"version" : "0.2.2", | ||
"version" : "0.3.0", | ||
"description" : "Dependency injection for node.js applications", | ||
@@ -34,7 +34,8 @@ "keywords" : [ | ||
"mocha": "1.2.x", | ||
"expect.js": "0.1.x" | ||
"expect.js": "0.1.x", | ||
"browserify": "1.13.x" | ||
}, | ||
"scripts" : { | ||
"test" : "mocha" | ||
"test" : "mocha -R spec" | ||
} | ||
} | ||
} |
@@ -15,3 +15,3 @@ rewire | ||
[![Build Status](https://secure.travis-ci.org/jhnns/rewire.png?branch=master)](http://travis-ci.org/jhnns/rewire) | ||
Furthermore rewire comes also with support for [browserify](https://github.com/substack/node-browserify). Thus you can mock your modules in the browser as well. | ||
@@ -26,4 +26,13 @@ <br /> | ||
**For older node versions:**<br /> | ||
rewire is tested with node 0.7.x. I recommend to run the unit tests via `mocha` in the rewire-folder before using rewire with older node versions. | ||
rewire is tested with node 0.6.x - 0.7.x. I recommend to run the unit tests via `mocha` in the rewire-folder before using rewire with older node versions. | ||
**Use with [browserify](https://github.com/substack/node-browserify):**<br /> | ||
```javascript | ||
// debug=true splits the source in seperate files in chrome's developer tools | ||
var b = require("browserify")({debug: true}); | ||
b.use(require("rewire").browserify); | ||
``` | ||
<br /> | ||
@@ -39,4 +48,3 @@ | ||
// rewire acts exactly like require. | ||
var myRewiredModule = rewire("../lib/myModule.js"); | ||
myRewiredModule === require("../lib/myModule.js"); // = true | ||
var myModule = rewire("../lib/myModule.js"); | ||
@@ -73,4 +81,4 @@ | ||
// You may also override globals. These changes are only within the module, | ||
// so you don't have to be afraid that other modules are influenced by your mock. | ||
// You may also override globals. These changes are only within the module, so | ||
// you don't have to be concerned that other modules are influenced by your mock. | ||
myModule.__set__({ | ||
@@ -98,2 +106,3 @@ console: { | ||
// subsequent calls of require() will than return the original module again. | ||
rewire("./myModule.js") === require("./myModule.js"); // = true | ||
rewire("./myModule.js", false) === require("./myModule.js"); // = false | ||
@@ -100,0 +109,0 @@ |
@@ -1,3 +0,1 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
var expect = require("expect.js"), | ||
@@ -4,0 +2,0 @@ vm = require("vm"), |
@@ -1,3 +0,1 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
var expect = require("expect.js"), | ||
@@ -4,0 +2,0 @@ __set__ = require("../lib/__set__.js"), |
@@ -1,3 +0,1 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
var rewire = require("../lib/index.js"); | ||
@@ -4,0 +2,0 @@ |
@@ -1,3 +0,1 @@ | ||
"use strict"; // run code in ES5 strict mode | ||
var expect = require("expect.js"), | ||
@@ -4,0 +2,0 @@ vm = require("vm"), |
@@ -32,13 +32,7 @@ "use strict"; // run code in ES5 strict mode | ||
if (typeof global === "undefined") { | ||
//throw new ReferenceError("global is undefined"); | ||
throw new ReferenceError("global is undefined"); | ||
} | ||
if (typeof process === "undefined") { | ||
throw new ReferenceError("process is undefined"); | ||
} | ||
if (typeof console === "undefined") { | ||
throw new ReferenceError("console is undefined"); | ||
} | ||
if (typeof Buffer === "undefined") { | ||
throw new ReferenceError("Buffer is undefined"); | ||
} | ||
if (typeof __filename === "undefined") { | ||
@@ -59,6 +53,2 @@ throw new ReferenceError("__filename is undefined"); | ||
function getProcess() { | ||
return process; | ||
} | ||
function getFilename() { | ||
@@ -76,4 +66,3 @@ return __filename; | ||
exports.getConsole = getConsole; | ||
exports.getProcess = getProcess; | ||
exports.getFilename = getFilename; | ||
exports.someOtherModule = someOtherModule; |
@@ -6,2 +6,3 @@ "use strict"; // run code in ES5 strict mode | ||
myObj = {}, // copy by reference | ||
env = "bla", | ||
fs = require("fs"); | ||
@@ -34,11 +35,5 @@ | ||
} | ||
if (typeof process === "undefined") { | ||
throw new ReferenceError("process is undefined"); | ||
} | ||
if (typeof console === "undefined") { | ||
throw new ReferenceError("console is undefined"); | ||
} | ||
if (typeof Buffer === "undefined") { | ||
throw new ReferenceError("Buffer is undefined"); | ||
} | ||
if (typeof __filename === "undefined") { | ||
@@ -59,6 +54,2 @@ throw new ReferenceError("__filename is undefined"); | ||
function getProcess() { | ||
return process; | ||
} | ||
function getFilename() { | ||
@@ -68,18 +59,11 @@ return __filename; | ||
function main() { | ||
} | ||
main.setMyNumber = setMyNumber; | ||
main.getMyNumber = getMyNumber; | ||
main.setMyObj = setMyObj; | ||
main.getMyObj = getMyObj; | ||
main.readFileSync = readFileSync; | ||
main.checkSomeGlobals = checkSomeGlobals; | ||
main.getConsole = getConsole; | ||
main.getProcess = getProcess; | ||
main.getFilename = getFilename; | ||
main.someOtherModule = someOtherModule; | ||
// different styles of exports in moduleA.js and moduleB.js | ||
module.exports = main; | ||
exports.setMyNumber = setMyNumber; | ||
exports.getMyNumber = getMyNumber; | ||
exports.setMyObj = setMyObj; | ||
exports.getMyObj = getMyObj; | ||
exports.readFileSync = readFileSync; | ||
exports.checkSomeGlobals = checkSomeGlobals; | ||
exports.getConsole = getConsole; | ||
exports.getFilename = getFilename; | ||
exports.someOtherModule = someOtherModule; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
136634
35
1446
156
3
37
15