This commit is contained in:
Dmitry Vasilev
2023-05-18 15:40:21 +03:00
parent f75220747c
commit db3166154d
4 changed files with 52 additions and 36 deletions

View File

@@ -12,6 +12,36 @@ Object.assign(globalThis,
}
)
export const original_setTimeout = globalThis.setTimeout
export const patch_builtin = new Function(`
// Substitute some builtin functions: fetch, setTimeout, Math.random to be
// able to patch them in tests
const originals = {
random: Math.random,
fetch: globalThis.fetch,
setTimeout: globalThis.setTimeout,
}
const patched = {}
const patch = (obj, name) => {
originals[name] = obj[name]
obj[name] = (...args) => patched[name] == null
? originals[name](...args)
: patched[name](...args)
}
patch(globalThis, 'fetch')
patch(globalThis, 'setTimeout')
patch(Math, 'random')
return (name, fn) => {
patched[name] = fn
}
`)()
export const parse_modules = (entry, modules) =>
load_modules(entry, module_name => modules[module_name])