Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes <johannes.ewald@roomieplanet.de>2012-06-23 18:07:15 +0400
committerJohannes <johannes.ewald@roomieplanet.de>2012-06-23 18:07:15 +0400
commit2c8949bffba28b5d0e6953d804e9fbb6725fb38e (patch)
treebc31b6eab3eeb39620adc795a5350922d77709e8
parentd432d902513b69eb216c95505f755f616b9cf698 (diff)
added comments
-rw-r--r--lib/__get__.js14
-rw-r--r--lib/__set__.js14
-rw-r--r--lib/browserify/browserifyMiddleware.js19
-rw-r--r--lib/browserify/browserifyRewire.js31
-rw-r--r--lib/detectStrictMode.js7
-rw-r--r--lib/index.js16
-rw-r--r--lib/internalRewire.js (renamed from lib/rewire.js)6
-rw-r--r--package.json4
-rw-r--r--test/browserify.browserifyRewire.test.js8
-rw-r--r--test/internalRewire.test.js (renamed from test/rewire.test.js)2
10 files changed, 72 insertions, 49 deletions
diff --git a/lib/__get__.js b/lib/__get__.js
index 47bfb66..d8cdfe7 100644
--- a/lib/__get__.js
+++ b/lib/__get__.js
@@ -2,14 +2,20 @@
* This function will be stringified and then injected into every rewired module.
* Then you can leak private variables by calling myModule.__get__("myPrivateVar");
*
+ * 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
* @throws {TypeError}
* @return {*}
*/
-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);
-}; \ No newline at end of file
+ return eval(arguments.varName);
+}
+
+module.exports = __get__; \ No newline at end of file
diff --git a/lib/__set__.js b/lib/__set__.js
index 49e4494..79175ed 100644
--- a/lib/__set__.js
+++ b/lib/__set__.js
@@ -8,14 +8,16 @@
* @param {!String|!Object} varName name of the variable to set
* @param {String} varValue new value
* @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('Cannot __set__(): " + varName + " is not declared within the module.');} ";
+ arguments.checkExistsSrc = function (varName, varValue) {
+ return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('Cannot __set__(" + varName + ", " + varValue + "): " +
+ varName + " is not declared within the module.'); } ";
};
if (typeof arguments[0] === "object") {
@@ -32,10 +34,12 @@ module.exports = function __set__() {
if (!arguments.varName) {
throw new TypeError("__set__ expects a non-empty string as a variable name");
}
- arguments.src = arguments.checkExistsSrc(arguments.varName) + arguments.varName + " = arguments.varValue;";
+ arguments.src = arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.varValue;";
} else {
throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
}
eval(arguments.src);
-}; \ No newline at end of file
+}
+
+module.exports = __set__; \ No newline at end of file
diff --git a/lib/browserify/browserifyMiddleware.js b/lib/browserify/browserifyMiddleware.js
index 619049c..d62352e 100644
--- a/lib/browserify/browserifyMiddleware.js
+++ b/lib/browserify/browserifyMiddleware.js
@@ -27,16 +27,6 @@ function getInjectionSrc() {
'require = window.browserifyRequire.getProxy(require, __filename);';
}
-function wrapCodeInDecorativeComments(filename, src) {
- var topLine = "",
- bottomLine = "",
- lineLength = 80;
-
- while (topLine.length <= lineLength) {
-
- }
-}
-
function browserifyMiddleware(b) {
function injectRewire(src, filename) {
var rewireRequires,
@@ -68,16 +58,11 @@ function browserifyMiddleware(b) {
if (filename.indexOf("/rewire/lib") === -1) {
src =
strictMode + // either '' or ' "use strict"; '
+ "/* this line was injected by rewire() */" +
"var global = window; " + // window is our new global object
importGlobalsSrc +
injectionSrc + "\n" +
- // For a better debugging experience we're adding a comment with the filename
- "//// " + filename + " /////////////////////////////////////////////////////////////////////////////////////////////////////////////\n" +
- "\n" +
- src + "\n" +
- "\n" +
- "/////" + filename.replace(/./g, "/") + "//////////////////////////////////////////////////////////////////////////////////////////////////////////////\n" +
- "//@ sourceURL=" + filename + "\n";
+ src;
}
return src;
diff --git a/lib/browserify/browserifyRewire.js b/lib/browserify/browserifyRewire.js
index 59ba8cf..d70f31e 100644
--- a/lib/browserify/browserifyRewire.js
+++ b/lib/browserify/browserifyRewire.js
@@ -1,14 +1,26 @@
var pathUtil = require("path"),
browserifyRequire = window.browserifyRequire;
+// Saves all setters and getters for every module according to its filename
var registry = {},
- rewiredModules = []; // cache for all rewired modules so it can be reset anytime
+// Cache for all rewired modules so it can be reset anytime
+ rewiredModules = [];
-function rewire(parentModulePath, targetPath, cache) {
+/**
+ * Executes the given module and adds a special setter and getter that allow you to set and get private variables.
+ * The parentModulePath is usually set by the requireProxy.
+ *
+ * @param {!String} parentModulePath __filename of the module, that wants to rewire() another module.
+ * @param {!String} targetPath path to the module that shall be rewired
+ * @param {Boolean=true} cache indicates whether the rewired module should be cached or not
+ * @return {Object}
+ */
+function browserifyRewire(parentModulePath, targetPath, cache) {
var originalModule,
rewiredModule = {},
registeredTargetModule;
+ // Default cache to true
if (cache === undefined) {
cache = true;
}
@@ -29,12 +41,14 @@ function rewire(parentModulePath, targetPath, cache) {
// @see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
originalModule = (require)(targetPath);
+ // Copy all exported values to our rewired module
for (var key in originalModule) {
if (originalModule.hasOwnProperty(key)) {
rewiredModule[key] = originalModule[key];
}
}
+ // If caching is enabled we store the rewiredModule in the cache
if (cache) {
browserifyRequire.modules[targetPath]._cached = rewiredModule;
}
@@ -52,7 +66,14 @@ function rewire(parentModulePath, targetPath, cache) {
return rewiredModule;
}
-rewire.register = function (filename, setter, getter) {
+/**
+ * Registers the setter and getter of every module according to its filename
+ *
+ * @param {!String} filename the absolute path to the module (module id)
+ * @param {!Function} setter
+ * @param {!Function} getter
+ */
+browserifyRewire.register = function (filename, setter, getter) {
registry[filename] = {
setter: setter,
getter: getter
@@ -62,7 +83,7 @@ rewire.register = function (filename, setter, getter) {
/**
* Deletes all rewired modules from the cache
*/
-rewire.reset = function () {
+browserifyRewire.reset = function () {
var modules = browserifyRequire.modules,
i;
@@ -73,4 +94,4 @@ rewire.reset = function () {
rewiredModules = [];
};
-module.exports = rewire; \ No newline at end of file
+module.exports = browserifyRewire; \ No newline at end of file
diff --git a/lib/detectStrictMode.js b/lib/detectStrictMode.js
index d0107d0..347c455 100644
--- a/lib/detectStrictMode.js
+++ b/lib/detectStrictMode.js
@@ -1,3 +1,10 @@
+/**
+ * Returns true if the source code is intended to run in strict mode. Does not detect
+ * "use strict" if it occurs in a nested function.
+ *
+ * @param {!String} src
+ * @return {Boolean}
+ */
function detectStrictMode(src) {
return (/^\s*"use strict";/g).test(src);
}
diff --git a/lib/index.js b/lib/index.js
index 6763399..491599a 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,30 +1,30 @@
var rewireModule;
/**
- * 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 {!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) {
cache = true;
}
- return rewireModule(module.parent, request, cache);
+ return rewireModule(module.parent, filename, cache);
}
// 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
+
// 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)("./rewire.js");
+ rewireModule = (require)("./internalRewire.js");
rewire.reset = rewireModule.reset;
rewire.browserify = (require)("./browserify/browserifyMiddleware.js");
diff --git a/lib/rewire.js b/lib/internalRewire.js
index 080847f..b4c8e62 100644
--- a/lib/rewire.js
+++ b/lib/internalRewire.js
@@ -17,7 +17,7 @@ function restoreOriginalWrappers() {
/**
* Does actual rewiring the module. For further documentation @see index.js
*/
-function rewire(parentModulePath, targetPath, cache) {
+function internalRewire(parentModulePath, targetPath, cache) {
var testModule,
nodeRequire,
prepend,
@@ -95,7 +95,7 @@ function rewire(parentModulePath, targetPath, cache) {
/**
* Deletes all rewired modules from the cache
*/
-rewire.reset = function () {
+internalRewire.reset = function () {
var i;
for (i = 0; i < rewiredModules.length; i++) {
@@ -105,4 +105,4 @@ rewire.reset = function () {
rewiredModules = [];
};
-module.exports = rewire; \ No newline at end of file
+module.exports = internalRewire; \ No newline at end of file
diff --git a/package.json b/package.json
index 9b5cf20..c05ab1a 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name" : "rewire",
- "version" : "0.2.2",
+ "version" : "0.3.0",
"description" : "Dependency injection for node.js applications",
"keywords" : [
"dependency",
@@ -35,6 +35,6 @@
"browserify": "1.13.x"
},
"scripts" : {
- "test" : "mocha"
+ "test" : "mocha -R spec"
}
} \ No newline at end of file
diff --git a/test/browserify.browserifyRewire.test.js b/test/browserify.browserifyRewire.test.js
index cf8dbd9..43c7082 100644
--- a/test/browserify.browserifyRewire.test.js
+++ b/test/browserify.browserifyRewire.test.js
@@ -2,8 +2,7 @@ var vm = require("vm"),
fs = require("fs"),
pathUtil = require("path"),
expect = require("expect.js"),
- browserify = require("browserify"),
- browserifyMiddleware = require("../lib/index.js").browserify;
+ browserify = require("browserify");
/**
* Executes the source in a context that pretends to be a browser
@@ -96,12 +95,13 @@ describe("browserifyRewire", function () {
});
});
it("should run all sharedTestCases without exception", function (done) {
- var b = browserify(),
+ var b = browserify({debug: true}),
+ middleware = require("rewire").browserify,
browserOutput = __dirname + "/browser/browseroutput.js",
browserBundle,
vmBundle;
- b.use(browserifyMiddleware);
+ b.use(middleware);
b.require(__dirname + "/testModules/sharedTestCases.js");
vmBundle = b.bundle();
browserBundle = vmBundle;
diff --git a/test/rewire.test.js b/test/internalRewire.test.js
index 1783ad5..23d0193 100644
--- a/test/rewire.test.js
+++ b/test/internalRewire.test.js
@@ -2,7 +2,7 @@
// In case this module was in strict mode, all other modules called by this would also be strict.
// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
-describe("rewire", function () {
+describe("internalRewire (node.js)", function () {
before(require("./testHelpers/createFakePackageJSON.js"));
after(require("./testHelpers/removeFakePackageJSON.js"));
it("should pass all shared test cases", function () {