Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c8949b

Browse files
author
Johannes
committed
added comments
1 parentd432d90 commit2c8949b

10 files changed

+72
-49
lines changed

‎lib/__get__.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22
* This function will be stringified and then injected into every rewired module.
33
* Then you can leak private variables by calling myModule.__get__("myPrivateVar");
44
*
5+
* All variables within this function are namespaced in the arguments array because every
6+
* var declaration could possibly clash with a variable in the module scope.
7+
*
58
*@param {!String} name name of the variable to retrieve
69
*@throws {TypeError}
710
*@return {*}
811
*/
9-
module.exports=function__get__(name){
10-
if(typeofname!=="string"||name.length===0){
12+
function__get__(){
13+
arguments.varName=arguments[0];
14+
if(typeofarguments.varName!=="string"||arguments.varName.length===0){
1115
thrownewTypeError("__get__ expects a non-empty string");
1216
}
1317

14-
returneval(name);
15-
};
18+
returneval(arguments.varName);
19+
}
20+
21+
module.exports=__get__;

‎lib/__set__.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
*@param {!String|!Object} varName name of the variable to set
99
*@param {String} varValue new value
1010
*@throws {TypeError}
11+
*@throws {ReferenceError} When the variable is unknown
1112
*@return {*}
1213
*/
13-
module.exports=function__set__(){
14+
function__set__(){
1415
arguments.varName=arguments[0];
1516
arguments.varValue=arguments[1];
1617
arguments.src="";
17-
arguments.checkExistsSrc=function(varName){
18-
return"if (typeof "+varName+" === 'undefined') { throw new ReferenceError('Cannot __set__(): "+varName+" is not declared within the module.');} ";
18+
arguments.checkExistsSrc=function(varName,varValue){
19+
return"if (typeof "+varName+" === 'undefined') { throw new ReferenceError('Cannot __set__("+varName+", "+varValue+"): "+
20+
varName+" is not declared within the module.'); } ";
1921
};
2022

2123
if(typeofarguments[0]==="object"){
@@ -32,10 +34,12 @@ module.exports = function __set__() {
3234
if(!arguments.varName){
3335
thrownewTypeError("__set__ expects a non-empty string as a variable name");
3436
}
35-
arguments.src=arguments.checkExistsSrc(arguments.varName)+arguments.varName+" = arguments.varValue;";
37+
arguments.src=arguments.checkExistsSrc(arguments.varName,arguments.varValue)+arguments.varName+" = arguments.varValue;";
3638
}else{
3739
thrownewTypeError("__set__ expects an environment object or a non-empty string as a variable name");
3840
}
3941

4042
eval(arguments.src);
41-
};
43+
}
44+
45+
module.exports=__set__;

‎lib/browserify/browserifyMiddleware.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ function getInjectionSrc() {
2727
'require = window.browserifyRequire.getProxy(require, __filename);';
2828
}
2929

30-
functionwrapCodeInDecorativeComments(filename,src){
31-
vartopLine="",
32-
bottomLine="",
33-
lineLength=80;
34-
35-
while(topLine.length<=lineLength){
36-
37-
}
38-
}
39-
4030
functionbrowserifyMiddleware(b){
4131
functioninjectRewire(src,filename){
4232
varrewireRequires,
@@ -68,16 +58,11 @@ function browserifyMiddleware(b) {
6858
if(filename.indexOf("/rewire/lib")===-1){
6959
src=
7060
strictMode+// either '' or ' "use strict"; '
61+
"/* this line was injected by rewire() */"+
7162
"var global = window; "+// window is our new global object
7263
importGlobalsSrc+
7364
injectionSrc+"\n"+
74-
// For a better debugging experience we're adding a comment with the filename
75-
"//// "+filename+" /////////////////////////////////////////////////////////////////////////////////////////////////////////////\n"+
76-
"\n"+
77-
src+"\n"+
78-
"\n"+
79-
"/////"+filename.replace(/./g,"/")+"//////////////////////////////////////////////////////////////////////////////////////////////////////////////\n"+
80-
"//@ sourceURL="+filename+"\n";
65+
src;
8166
}
8267

8368
returnsrc;

‎lib/browserify/browserifyRewire.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
varpathUtil=require("path"),
22
browserifyRequire=window.browserifyRequire;
33

4+
// Saves all setters and getters for every module according to its filename
45
varregistry={},
5-
rewiredModules=[];// cache for all rewired modules so it can be reset anytime
6+
// Cache for all rewired modules so it can be reset anytime
7+
rewiredModules=[];
68

7-
functionrewire(parentModulePath,targetPath,cache){
9+
/**
10+
* Executes the given module and adds a special setter and getter that allow you to set and get private variables.
11+
* The parentModulePath is usually set by the requireProxy.
12+
*
13+
*@param {!String} parentModulePath __filename of the module, that wants to rewire() another module.
14+
*@param {!String} targetPath path to the module that shall be rewired
15+
*@param {Boolean=true} cache indicates whether the rewired module should be cached or not
16+
*@return {Object}
17+
*/
18+
functionbrowserifyRewire(parentModulePath,targetPath,cache){
819
varoriginalModule,
920
rewiredModule={},
1021
registeredTargetModule;
1122

23+
// Default cache to true
1224
if(cache===undefined){
1325
cache=true;
1426
}
@@ -29,12 +41,14 @@ function rewire(parentModulePath, targetPath, cache) {
2941
//@see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
3042
originalModule=(require)(targetPath);
3143

44+
// Copy all exported values to our rewired module
3245
for(varkeyinoriginalModule){
3346
if(originalModule.hasOwnProperty(key)){
3447
rewiredModule[key]=originalModule[key];
3548
}
3649
}
3750

51+
// If caching is enabled we store the rewiredModule in the cache
3852
if(cache){
3953
browserifyRequire.modules[targetPath]._cached=rewiredModule;
4054
}
@@ -52,7 +66,14 @@ function rewire(parentModulePath, targetPath, cache) {
5266
returnrewiredModule;
5367
}
5468

55-
rewire.register=function(filename,setter,getter){
69+
/**
70+
* Registers the setter and getter of every module according to its filename
71+
*
72+
*@param {!String} filename the absolute path to the module (module id)
73+
*@param {!Function} setter
74+
*@param {!Function} getter
75+
*/
76+
browserifyRewire.register=function(filename,setter,getter){
5677
registry[filename]={
5778
setter:setter,
5879
getter:getter
@@ -62,7 +83,7 @@ rewire.register = function (filename, setter, getter) {
6283
/**
6384
* Deletes all rewired modules from the cache
6485
*/
65-
rewire.reset=function(){
86+
browserifyRewire.reset=function(){
6687
varmodules=browserifyRequire.modules,
6788
i;
6889

@@ -73,4 +94,4 @@ rewire.reset = function () {
7394
rewiredModules=[];
7495
};
7596

76-
module.exports=rewire;
97+
module.exports=browserifyRewire;

‎lib/detectStrictMode.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Returns true if the source code is intended to run in strict mode. Does not detect
3+
* "use strict" if it occurs in a nested function.
4+
*
5+
*@param {!String} src
6+
*@return {Boolean}
7+
*/
18
functiondetectStrictMode(src){
29
return(/^\s*"usestrict";/g).test(src);
310
}

‎lib/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
varrewireModule;
22

33
/**
4-
*This function is needed to determinethecalling parent module.
5-
*Thus rewire acts exactly the same like require() in the test module.
4+
*Adds a special setter and getter tothemodule located at filename. After the module has been rewired, you can
5+
*call myModule.__set__(name, value) and myModule.__get__(name) to manipulate private variables.
66
*
7-
*@param {!String}request Path to the module that shall be rewired. Use it exactly like require().
7+
*@param {!String}filename Path to the module that shall be rewired. Use it exactly like require().
88
*@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.
99
*@return {*} the rewired module
1010
*/
11-
functionrewire(request,cache){
12-
deleterequire.cache[__filename];// deleting self from module cache so the parent module is always up to date
13-
11+
functionrewire(filename,cache){
1412
if(cache===undefined){
1513
cache=true;
1614
}
1715

18-
returnrewireModule(module.parent,request,cache);
16+
returnrewireModule(module.parent,filename,cache);
1917
}
2018

2119
// Conditional require for different environments
2220
if(process.title==="browser"){
2321
module.exports=require("./browserify/browserifyRewire.js");
2422
}else{
23+
deleterequire.cache[__filename];// deleting self from module cache so the parent module is always up to date
24+
2525
// Putting (require) within brackets is a hack to disable browserify's require sniffing
2626
//@see https://github.com/substack/node-browserify/issues/132#issuecomment-5281470
27-
rewireModule=(require)("./rewire.js");
27+
rewireModule=(require)("./internalRewire.js");
2828

2929
rewire.reset=rewireModule.reset;
3030
rewire.browserify=(require)("./browserify/browserifyMiddleware.js");

‎lib/rewire.jsrenamed to‎lib/internalRewire.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function restoreOriginalWrappers() {
1717
/**
1818
* Does actual rewiring the module. For further documentation @see index.js
1919
*/
20-
functionrewire(parentModulePath,targetPath,cache){
20+
functioninternalRewire(parentModulePath,targetPath,cache){
2121
vartestModule,
2222
nodeRequire,
2323
prepend,
@@ -95,7 +95,7 @@ function rewire(parentModulePath, targetPath, cache) {
9595
/**
9696
* Deletes all rewired modules from the cache
9797
*/
98-
rewire.reset=function(){
98+
internalRewire.reset=function(){
9999
vari;
100100

101101
for(i=0;i<rewiredModules.length;i++){
@@ -105,4 +105,4 @@ rewire.reset = function () {
105105
rewiredModules=[];
106106
};
107107

108-
module.exports=rewire;
108+
module.exports=internalRewire;

‎package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" :"rewire",
3-
"version" :"0.2.2",
3+
"version" :"0.3.0",
44
"description" :"Dependency injection for node.js applications",
55
"keywords" : [
66
"dependency",
@@ -35,6 +35,6 @@
3535
"browserify":"1.13.x"
3636
},
3737
"scripts" : {
38-
"test" :"mocha"
38+
"test" :"mocha -R spec"
3939
}
4040
}

‎test/browserify.browserifyRewire.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ var vm = require("vm"),
22
fs=require("fs"),
33
pathUtil=require("path"),
44
expect=require("expect.js"),
5-
browserify=require("browserify"),
6-
browserifyMiddleware=require("../lib/index.js").browserify;
5+
browserify=require("browserify");
76

87
/**
98
* Executes the source in a context that pretends to be a browser
@@ -96,12 +95,13 @@ describe("browserifyRewire", function () {
9695
});
9796
});
9897
it("should run all sharedTestCases without exception",function(done){
99-
varb=browserify(),
98+
varb=browserify({debug:true}),
99+
middleware=require("rewire").browserify,
100100
browserOutput=__dirname+"/browser/browseroutput.js",
101101
browserBundle,
102102
vmBundle;
103103

104-
b.use(browserifyMiddleware);
104+
b.use(middleware);
105105
b.require(__dirname+"/testModules/sharedTestCases.js");
106106
vmBundle=b.bundle();
107107
browserBundle=vmBundle;

‎test/rewire.test.jsrenamed to‎test/internalRewire.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// In case this module was in strict mode, all other modules called by this would also be strict.
33
// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
44

5-
describe("rewire",function(){
5+
describe("internalRewire (node.js)",function(){
66
before(require("./testHelpers/createFakePackageJSON.js"));
77
after(require("./testHelpers/removeFakePackageJSON.js"));
88
it("should pass all shared test cases",function(){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp