This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test-framework): add infinite loop protection (#42)
- Loading branch information
1 parent
3db76f2
commit b7cff4c
Showing
5 changed files
with
33 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default (code) => { | ||
const timeout = 500 | ||
const Babel = require('babel-standalone') | ||
const protect = require('loop-protect') | ||
|
||
Babel.registerPlugin('loopProtection', protect(timeout)) | ||
const transform = source => Babel.transform(source, { | ||
plugins: ['loopProtection'], | ||
}).code | ||
|
||
const error = `Error('Timed out after ${timeout}ms due to an infinite loop. Check your code and try again.')` | ||
const _this = new RegExp(`if \\(Date\\.now\\(\\) - _LP > ${timeout}\\)\\s+break;`) | ||
const withThis = `if (Date.now() - _LP > ${timeout}) throw new ${error}` | ||
|
||
// use babel and loop-protect to transform user code | ||
// into protected loops. Instead of simply breaking the | ||
// loop, add replacement logic to throw error to warn user | ||
return transform(code).replace(_this, withThis) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters