diff options
author | Johannes <johannes.ewald@roomieplanet.de> | 2012-06-23 18:07:15 +0400 |
---|---|---|
committer | Johannes <johannes.ewald@roomieplanet.de> | 2012-06-23 18:07:15 +0400 |
commit | 2c8949bffba28b5d0e6953d804e9fbb6725fb38e (patch) | |
tree | bc31b6eab3eeb39620adc795a5350922d77709e8 /lib | |
parent | d432d902513b69eb216c95505f755f616b9cf698 (diff) |
added comments
Diffstat (limited to 'lib')
-rw-r--r-- | lib/__get__.js | 14 | ||||
-rw-r--r-- | lib/__set__.js | 14 | ||||
-rw-r--r-- | lib/browserify/browserifyMiddleware.js | 19 | ||||
-rw-r--r-- | lib/browserify/browserifyRewire.js | 31 | ||||
-rw-r--r-- | lib/detectStrictMode.js | 7 | ||||
-rw-r--r-- | lib/index.js | 16 | ||||
-rw-r--r-- | lib/internalRewire.js (renamed from lib/rewire.js) | 6 |
7 files changed, 65 insertions, 42 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 |