Inject timeout protection into JavaScript loops to prevent infinite loops. Supports while, for, do-while, for-in, and for-of loops with customizable timeout and error messages.
npm install open-loop-killer
---
```
npm i open-loop-killer
- Runs Untrusted code securely with no open loops issue.
- Add one more layer of safety for your code.
- Protects while, for, do-while, for...in, and for...of loops from running indefinitely.
1. Parses the code to make sure it's valid JavaScript.
2. Converts it to AST (Abstract Syntax Tree).
3. Detects all loops and injects protection code.
4. Converts AST back to executable JavaScript string.
Injects loop protection code into JavaScript source code.
#### Parameters
- code (string, required) - The JavaScript code to protect
- options (object, optional) - Configuration options
- timeout (number, optional) - Timeout in milliseconds before throwing error. Default: 1000errorMessage
- (string, optional) - Custom error message. Default: 'Open Loop Detected!'
#### Returns
- (string) - The protected JavaScript code with injected loop protection
#### Throws
- Error if code is invalid JavaScript
- Error if options are invalid
This package includes TypeScript type definitions out of the box. No need to install separate @types packages!
`typescript
import { injector, InjectorOptions } from 'open-loop-killer';
const code = 'while(true) { console.log("test"); }';
const options: InjectorOptions = {
timeout: 2000,
errorMessage: 'Loop timeout!'
};
const protectedCode: string = injector(code, options);
`
`javascript
const {injector} = require('open-loop-killer');
let code =
while(true){
}`
let injectedCode = injector(code);
`javascript
const {injector} = require('open-loop-killer');
let code =
for(let i = 0; i < 1000000; i++){
// Some operation
}`
let injectedCode = injector(code, {
timeout: 5000 // 5 seconds
});
`javascript
const {injector} = require('open-loop-killer');
let code =
while(true){
}`
let injectedCode = injector(code, {
errorMessage: 'Loop execution timeout exceeded!'
});
`javascript
const {injector} = require('open-loop-killer');
let injectedCode = injector(code, {
timeout: 2000,
errorMessage: 'Custom timeout message'
});
`
Input:
`javascript`
while(true) { }
Output:
`javascript`
let _a3f9b2 = Date.now();
while (true) {
if (Date.now() - _a3f9b2 > 1000) {
throw new Error('Open Loop Detected!');
}
{
}
}
✅ Fully Protected:
- while loopsfor
- loopsdo-while
- loopsfor...in
- loopsfor...of
- loops
⚠️ Important: This package has the following limitations:
1. No protection (not yet supported) for:
- ❌ for await...of loops (async iteration).forEach()
- ❌ Recursive functions
- ❌ Async loops or promises without await
- ❌ Array methods like , .map(), etc.
2. Timeout behavior:
- Timeout is checked on each iteration
- If a single iteration takes longer than the timeout, it won't be caught
- Protection works best for loops with many fast iterations
- For for...in and for...of`, protection works on iteration count, not property/item count
3. Error handling:
- When a loop times out, it throws an error
- Make sure to wrap execution in try-catch if needed
[npm-image]: https://img.shields.io/npm/v/open-loop-killer.svg
[npm-url]: https://www.npmjs.com/package/open-loop-killer
[downloads-image]: https://img.shields.io/npm/dm/open-loop-killer.svg?style=flat-square
[downloads-url]: https://www.npmjs.com/package/open-loop-killer