Skip to content

Commit

Permalink
Merge branch 'master' into fix-167-global-var-leakage
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Dec 18, 2021
2 parents 7bec7f8 + d1474b3 commit 92d9a33
Show file tree
Hide file tree
Showing 11 changed files with 1,497 additions and 961 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- "6"
- "8"
- "9"
- "10"
- "12"

script:
- npm test
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
---------

### 5.0.0
- **Breaking**: Remove Node v6 support. We had to do this because one of our dependencies had security issues and the version with the fix dropped Node v6 as well.
- Update dependencies [#159](https://github.com/jhnns/rewire/pull/159) [#172](https://github.com/jhnns/rewire/issues/172) [#154](https://github.com/jhnns/rewire/issues/154) [#166](https://github.com/jhnns/rewire/issues/166)

### 4.0.1
- Fix a bug where `const` was not properly detected [#139](https://github.com/jhnns/rewire/pull/139)

### 4.0.0
- **Breaking**: Remove official node v4 support. It probably still works with node v4, but no guarantees anymore.
- **Potentially breaking**: Replace babel with regex-based transformation [9b77ed9a293c538ec3eb5160bcb933e012ce517f](https://github.com/jhnns/rewire/commit/9b77ed9a293c538ec3eb5160bcb933e012ce517f).
Expand Down
36 changes: 18 additions & 18 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Copyright (c) 2012 Johannes Ewald
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright (c) 2012 Johannes Ewald

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 20 additions & 20 deletions lib/__get__.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* 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 {*}
*/
function __get__() {
arguments.varName = arguments[0];
if (arguments.varName && typeof arguments.varName === "string") {
return eval(arguments.varName);
} else {
throw new TypeError("__get__ expects a non-empty string");
}
}

/**
* 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 {*}
*/
function __get__() {
arguments.varName = arguments[0];
if (arguments.varName && typeof arguments.varName === "string") {
return eval(arguments.varName);
} else {
throw new TypeError("__get__ expects a non-empty string");
}
}

module.exports = __get__;
56 changes: 28 additions & 28 deletions lib/detectStrictMode.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
var multiLineComment = /^\s*\/\*.*?\*\//;
var singleLineComment = /^\s*\/\/.*?[\r\n]/;
var strictMode = /^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/;

/**
* 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) {
var singleLine;
var multiLine;

while ((singleLine = singleLineComment.test(src)) || (multiLine = multiLineComment.test(src))) {
if (singleLine) {
src = src.replace(singleLineComment, "");
}
if (multiLine) {
src = src.replace(multiLineComment, "");
}
}

return strictMode.test(src);
}

module.exports = detectStrictMode;
var multiLineComment = /^\s*\/\*.*?\*\//;
var singleLineComment = /^\s*\/\/.*?[\r\n]/;
var strictMode = /^\s*(?:"use strict"|'use strict')[ \t]*(?:[\r\n]|;)/;

/**
* 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) {
var singleLine;
var multiLine;

while ((singleLine = singleLineComment.test(src)) || (multiLine = multiLineComment.test(src))) {
if (singleLine) {
src = src.replace(singleLineComment, "");
}
if (multiLine) {
src = src.replace(multiLineComment, "");
}
}

return strictMode.test(src);
}

module.exports = detectStrictMode;
6 changes: 5 additions & 1 deletion lib/moduleEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var moduleWrapper0 = Module.wrapper[0],
// errors anyway, it's probably still a reasonable trade-off.
// Test the regular expresssion at https://regex101.com/r/dvnZPv/2 and also check out testLib/constModule.js.
matchConst = /(^|\s|\}|;)const(\/\*|\s|{)/gm,
// Required for importing modules with shebang declarations, since NodeJS 12.16.0
shebang = /^#!.+/,
nodeRequire,
currentModule;

Expand Down Expand Up @@ -123,7 +125,9 @@ function jsExtension(module, filename) {

_compile.call(
module,
content.replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width
content
.replace(shebang, '') // Remove shebang declarations
.replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width
filename
);
};
Expand Down
Loading

0 comments on commit 92d9a33

Please sign in to comment.