You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-40Lines changed: 15 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@
4
4
5
5
JS Bin's loop protection implementation as a reusable library.
6
6
7
-
This code protects most cases where user code includes an infinite loop using a `while`, `for` or `do` loop.
7
+
This code protects use cases where user code includes an infinite loop using a `while`, `for` or `do` loop.
8
8
9
-
Note that this does *not* solve the [halting problem](http://en.wikipedia.org/wiki/Halting_problem) but simply rewrites JavaScript (without an AST) wrapping loops with a conditional break. This also *does not* protect against recursive loops.
9
+
Note that this does *not* solve the [halting problem](http://en.wikipedia.org/wiki/Halting_problem) but simply rewrites JavaScript (using Babel's AST) wrapping loops with a conditional break. This also *does not* protect against recursive loops.
10
10
11
11
## Example
12
12
@@ -22,30 +22,25 @@ console.log('All finished');
22
22
23
23
## Usage
24
24
25
-
The loop protection can be used both on the client side and server side. It supports AMD and CommonJS module loading, or can be included as vanilla JavaScript (as it is in JS Bin).
25
+
The loop protection is a babel transform, so can be used on the server or in the client.
26
26
27
+
The previous implementation used an injected library to handle tracking loops - this version does not.
27
28
28
-
### Public methods
29
-
30
-
-`loopProtect.hit(number)`: fired when a potential infinite loop is found. Can be overwritten (see example below).
31
-
-`loopProtect.alias(string)`: used if loopProtect is aliased to a different variable.
32
-
-`loopProtect.debug(bool)`: used for development to trace steps of protection (not included .min file)
33
-
34
-
### Example implementation
29
+
### Example (client) implementation
35
30
36
31
```js
37
-
// we're going to alias the loopProtect object under a different name
38
-
// when it runs inside the iframe, so we need to configure the loopProtect
39
-
// *before* we process the JavaScript.
40
-
loopProtect.alias='protect';
32
+
importBabelfrom'babel-standalone';
33
+
importprotectfrom'loop-protect';
41
34
42
-
// show the user we found an infinite loop and let the code carry on
43
-
loopProtect.hit=function (line) {
44
-
alert('Potential infinite loop found on line '+ line);
// run in an iframe, and expose the loopProtect variable under a new name
51
46
var iframe =getNewFrame();
@@ -58,32 +53,12 @@ var win = iframe.contentWindow;
58
53
var doc =win.document;
59
54
doc.open();
60
55
61
-
// this line is why we use `loopProtect.alias = 'protect'`
62
-
win.protect= loopProtect;
63
-
64
56
doc.write('<script>'+ processed +'<'+'/script>');
65
57
doc.close();
66
58
67
-
// code now runs, and if there's a loop, loopProtect.hit is called.
59
+
// code now runs, and if there's an infinite loop, it's cleanly exited
68
60
```
69
61
70
-
## Abstract
71
-
72
-
The loop protection method takes a string of JavaScript and manually parses the code manually to find `while`, `for` and `do` loops.
73
-
74
-
Once these loops are found, a *marker* is inserted before the loop, and a *test* is called as the first job inside the loop. The first time the test is called, it records the time, and if the loop has taken over 100ms then the loop will `break` out and the remaining JavaScript is executed.
75
-
76
-
### Why not use an AST?
77
-
78
-
The parsing would be 100% robust if we did use an AST, but the overhead would be at the cost of around 200-300K for the parser.
79
-
80
-
For it's purpose, this manual loop protection works pretty well. Most people spot their infinite loops whilst they're typing and will fix the code, but if like [JS Bin](http://jsbin.com) you're rendering the output and JavaScript in real-time, you'll need to prevent the browser from hanging. This simple loop protection is one defence against loops.
81
-
82
-
83
-
## TODO / ideas
84
-
85
-
- If the time between tests is 100ms or more, it's likely we're looking at a loop with an `alert` (or type of blocking call), so perhaps the loop protection can back off?
0 commit comments