mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 09:23:17 +01:00
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:
parent
0c668c16f7
commit
35376305ae
@ -21,6 +21,10 @@ const { shouldUseProxy, PatchedHttpsProxyAgent } = require('../utils/proxy-util'
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const protocolRegex = /^([-+\w]{1,25})(:?\/\/|:)/;
|
const protocolRegex = /^([-+\w]{1,25})(:?\/\/|:)/;
|
||||||
|
|
||||||
|
const onConsoleLog = (type, args) => {
|
||||||
|
console[type](...args);
|
||||||
|
};
|
||||||
|
|
||||||
const runSingleRequest = async function (
|
const runSingleRequest = async function (
|
||||||
filename,
|
filename,
|
||||||
bruJson,
|
bruJson,
|
||||||
@ -83,7 +87,7 @@ const runSingleRequest = async function (
|
|||||||
envVariables,
|
envVariables,
|
||||||
runtimeVariables,
|
runtimeVariables,
|
||||||
collectionPath,
|
collectionPath,
|
||||||
null,
|
onConsoleLog,
|
||||||
processEnvVars,
|
processEnvVars,
|
||||||
scriptingConfig
|
scriptingConfig
|
||||||
);
|
);
|
||||||
|
@ -9,7 +9,6 @@ const { newQuickJSWASMModule, memoizePromiseFactory } = require('quickjs-emscrip
|
|||||||
|
|
||||||
// execute `npm run sandbox:bundle-libraries` if the below file doesn't exist
|
// execute `npm run sandbox:bundle-libraries` if the below file doesn't exist
|
||||||
const getBundledCode = require('../bundle-browser-rollup');
|
const getBundledCode = require('../bundle-browser-rollup');
|
||||||
const addSleepShimToContext = require('./shims/sleep');
|
|
||||||
|
|
||||||
let QuickJSSyncContext;
|
let QuickJSSyncContext;
|
||||||
const loader = memoizePromiseFactory(() => newQuickJSWASMModule());
|
const loader = memoizePromiseFactory(() => newQuickJSWASMModule());
|
||||||
@ -103,7 +102,6 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
|
|||||||
req && addBrunoRequestShimToContext(vm, req);
|
req && addBrunoRequestShimToContext(vm, req);
|
||||||
res && addBrunoResponseShimToContext(vm, res);
|
res && addBrunoResponseShimToContext(vm, res);
|
||||||
consoleFn && addConsoleShimToContext(vm, consoleFn);
|
consoleFn && addConsoleShimToContext(vm, consoleFn);
|
||||||
addSleepShimToContext(vm);
|
|
||||||
addLocalModuleLoaderShimToContext(vm, collectionPath);
|
addLocalModuleLoaderShimToContext(vm, collectionPath);
|
||||||
|
|
||||||
await addLibraryShimsToContext(vm);
|
await addLibraryShimsToContext(vm);
|
||||||
@ -113,10 +111,10 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
|
|||||||
const script = `
|
const script = `
|
||||||
(async () => {
|
(async () => {
|
||||||
const setTimeout = async(fn, timer) => {
|
const setTimeout = async(fn, timer) => {
|
||||||
v = await sleep(timer);
|
v = await bru.sleep(timer);
|
||||||
fn.apply();
|
fn.apply();
|
||||||
}
|
}
|
||||||
await sleep(0);
|
await bru.sleep(0);
|
||||||
try {
|
try {
|
||||||
${externalScript}
|
${externalScript}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,17 @@ const addBruShimToContext = (vm, bru) => {
|
|||||||
vm.setProp(bruObject, 'getSecretVar', getSecretVar);
|
vm.setProp(bruObject, 'getSecretVar', getSecretVar);
|
||||||
getSecretVar.dispose();
|
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);
|
vm.setProp(vm.global, 'bru', bruObject);
|
||||||
bruObject.dispose();
|
bruObject.dispose();
|
||||||
};
|
};
|
||||||
|
@ -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;
|
|
Loading…
Reference in New Issue
Block a user