Skip to content

Commit

Permalink
Naming functions & fixed .hit
Browse files Browse the repository at this point in the history
  • Loading branch information
remy authored and allouis committed Jul 31, 2014
1 parent 0749483 commit 01f45e1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ The loop protection can be used both on the client side and server side. It supp

### Methods

- loopProtect.method
- loopProtect.hit

- loopProtect.method (string)
- loopProtect.hit (callback)
- loopProtect.debug (method)
- loopProtect.protect (callback)

## License

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loop-protect",
"main": "loop-protect.js",
"main": "dist/loop-protect.min.js",
"version": "0.9.0",
"homepage": "https://github.com/jsbin/loop-protect",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion dist/loop-protect.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions lib/loop-protect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

if (typeof DEBUG === 'undefined') { DEBUG = true; }

(function (root, factory) {
'use strict';
/*global define*/
Expand All @@ -18,8 +19,8 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
root.loopProtect = factory(root);
}
})(this, function loopProtectModule(root) {
'use strict';
/*global DEBUG*/
'use strict';
var debug = null;

// the standard loops - note that recursive is not supported
Expand All @@ -34,7 +35,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
loopProtect.counters = {};

// expose debug info
loopProtect.debug = function (state) {
loopProtect.debug = function debugSwitch(state) {
debug = state ? function () {
console.log.apply(console, [].slice.apply(arguments));
} : function () {};
Expand Down Expand Up @@ -106,7 +107,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
var beforeLoop = false;

var theRest = lines.slice(lineNum).join('\n').substr(index).replace(labelRe, '');
theRest.replace(reSingle, function (match, capture, i) {
theRest.replace(reSingle, function commentStripper(match, capture, i) {
var target = theRest.substr(0, i).replace(comments, '').trim();
DEBUG && debug('- directlyBeforeLoop: ' + target); // jshint ignore:line
if (target.length === 0) {
Expand All @@ -123,7 +124,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
* Look for for, while and do loops, and inserts *just* at the start of the
* loop, a check function.
*/
loopProtect.rewriteLoops = function (code, offset) {
loopProtect.rewriteLoops = function rewriteLoops(code, offset) {
var recompiled = [];
var lines = code.split('\n');
var disableLoopProtection = false;
Expand All @@ -132,7 +133,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
var pushonly = {};
var labelPostion = null;

var insertReset = function (lineNum, line, matchPosition) {
function insertReset(lineNum, line, matchPosition) {
// recompile the line with the reset **just** before the actual loop
// so that we insert in to the correct location (instead of possibly
// outside the logic
Expand All @@ -143,7 +144,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
offset = 0;
}

lines.forEach(function (line, lineNum) {
lines.forEach(function eachLine(line, lineNum) {
// reset our regexp each time.
re.lastIndex = 0;
labelRe.lastIndex = 0;
Expand Down Expand Up @@ -399,7 +400,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
* loops cropping up in the code, and killing the browser. Returns true
* when the loops has been running for more than 100ms.
*/
loopProtect.protect = function (state) {
loopProtect.protect = function protect(state) {
loopProtect.counters[state.line] = loopProtect.counters[state.line] || {};
var line = loopProtect.counters[state.line];
var now = (new Date()).getTime();
Expand All @@ -413,15 +414,15 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
line.hit++;
if ((now - line.time) > 100) {//} && line.hit !== line.last+1) {
// We've spent over 100ms on this loop... smells infinite.
loopProtect.hit();
loopProtect.hit(state.line);
// Returning true prevents the loop running again
return true;
}
line.last++;
return false;
};

loopProtect.hit = function (line) {
loopProtect.hit = function hit(line) {
var msg = 'Exiting potential infinite loop at line ' + line + '. To disable loop protection: add "// noprotect" to your code';
if (root.proxyConsole) {
root.proxyConsole.error(msg);
Expand All @@ -430,7 +431,7 @@ if (typeof DEBUG === 'undefined') { DEBUG = true; }
}
};

loopProtect.reset = function () {
loopProtect.reset = function reset() {
// reset the counters
loopProtect.counters = {};
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Remy Sharp",
"name": "loop-protect",
"description": "Prevent infinite loops in dynamically eval'd JavaScript.",
"main": "loop-protect",
"main": "lib/loop-protect",
"version": "0.9.0",
"homepage": "https://github.com/jsbin/loop-protect",
"repository": {
Expand Down

0 comments on commit 01f45e1

Please sign in to comment.