make-code-sync
Makes common async calls like setTimout
sync.
This module takes an abstract syntax tree (AST) and transforms it so that all calls to e.g. setTimeout
will be executed synchronously.
If your code does not use any other async funtions like fetch
, it will be easy to do analyses on it or run it using vm.runInNewContext
.
Caveats
- Right now only
setTimeout
works, but I plan to supportsetImmediate
as wellsetInterval
. - The
setTimeout
mock is not spec-compatible. Actually, it is quite lacking.
Installing
npm install make-code-sync
Usage
const parse = parseconst generate = generateconst vm = const assert = const syncify = // `now` filters out all calls to `setTimeout`.// `defer`, running in the end, executes all `setTimeout` callbacks, ordered by delay.const now defer = const code = ` let x = 2 setTimeout(() => { x += 1 }, 100) x = 0` const parsed = const sync = const transformed = + '\nx' // to get the value of `x`sconst ctx = setTimeout syncnameOfNow: now syncnameOfDefer: defer assert
Contributing
If you have a question, found a bug or want to propose a feature, have a look at the issues page.