-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafety.js
48 lines (39 loc) · 1.58 KB
/
safety.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
NanoPlay Web API
Copyright (C) Subnodal Technologies. All Rights Reserved.
https://nanoplay.subnodal.com
Licenced by the Subnodal Open-Source Licence, which can be found at LICENCE.md.
*/
namespace("com.subnodal.nanoplay.webapi.safety", function(exports) {
// This codes checks over other code and ensures that that code cannot enter
// an infinite loop, otherwise it'll freeze the NanoPlay
// Manual minification of variable names:
// __e = __escape
// __i = __iterationsMade
// __l = __lastIteration
exports.makeSafe = function(code) {
var codeToInject = `__e(__condition)`;
code = `
var __i = 0;
var __l = new Date().getTime();
function __e(c) {
if (__i > 500 && new Date().getTime() - __l <= 3000) {
throw new Error("Too long without deferring");
} else if (__i > 500) {
__i = 0;
__l = new Date().getTime();
} else {
__i++;
}
return c;
}
` + code;
code = code
.replace(/while\s*\((.*?)\)/g, `while (${codeToInject.replace("__condition", "$1")})`)
.replace(/for\s*\((.*?);(.*?);(.*?)\)/g, `for ($1; ${codeToInject.replace("__condition", "$2")}; $3)`)
.replace(/function\s*loop\s*\((.*?)\)\s*{/g, `function loop($1) {__i=0; __l=0;`)
.replace(/var\s*loop\s*=\s*function\s*\((.*?)\)\s*{/g, `function loop($1) {__i=0; __l=0;`)
;
return code;
};
});