feat: added bru.sleep shim (#2858)

* fix: added console fn for cli

* wip: added bru.sleep shim

---------

Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
lohit 2024-08-19 18:04:00 +05:30 committed by GitHub
parent 0c668c16f7
commit 35376305ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 19 deletions

View File

@ -21,6 +21,10 @@ const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../utils/proxy-util'
const path = require('path');
const protocolRegex = /^([-+\w]{1,25})(:?\/\/|:)/;
const onConsoleLog = (type, args) => {
console[type](...args);
};
const runSingleRequest = async function (
filename,
bruJson,
@ -83,7 +87,7 @@ const runSingleRequest = async function (
envVariables,
runtimeVariables,
collectionPath,
null,
onConsoleLog,
processEnvVars,
scriptingConfig
);

View File

@ -9,7 +9,6 @@ const { newQuickJSWASMModule, memoizePromiseFactory } = require('quickjs-emscrip
// execute `npm run sandbox:bundle-libraries` if the below file doesn't exist
const getBundledCode = require('../bundle-browser-rollup');
const addSleepShimToContext = require('./shims/sleep');
let QuickJSSyncContext;
const loader = memoizePromiseFactory(() => newQuickJSWASMModule());
@ -103,7 +102,6 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
req && addBrunoRequestShimToContext(vm, req);
res && addBrunoResponseShimToContext(vm, res);
consoleFn && addConsoleShimToContext(vm, consoleFn);
addSleepShimToContext(vm);
addLocalModuleLoaderShimToContext(vm, collectionPath);
await addLibraryShimsToContext(vm);
@ -113,10 +111,10 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
const script = `
(async () => {
const setTimeout = async(fn, timer) => {
v = await sleep(timer);
v = await bru.sleep(timer);
fn.apply();
}
await sleep(0);
await bru.sleep(0);
try {
${externalScript}
}

View File

@ -63,6 +63,17 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'getSecretVar', getSecretVar);
getSecretVar.dispose();
const sleep = vm.newFunction('sleep', (timer) => {
const t = vm.getString(timer);
const promise = vm.newPromise();
setTimeout(() => {
promise.resolve(vm.newString('slept'));
}, t);
promise.settled.then(vm.runtime.executePendingJobs);
return promise.handle;
});
sleep.consume((handle) => vm.setProp(bruObject, 'sleep', handle));
vm.setProp(vm.global, 'bru', bruObject);
bruObject.dispose();
};

View File

@ -1,14 +0,0 @@
const addSleepShimToContext = (vm) => {
const sleepHandle = vm.newFunction('sleep', (timer) => {
const t = vm.getString(timer);
const promise = vm.newPromise();
setTimeout(() => {
promise.resolve(vm.newString('slept'));
}, t);
promise.settled.then(vm.runtime.executePendingJobs);
return promise.handle;
});
sleepHandle.consume((handle) => vm.setProp(vm.global, 'sleep', handle));
};
module.exports = addSleepShimToContext;