From 73ca187e8d3a27e437009b31498f02c6b91c907c Mon Sep 17 00:00:00 2001 From: Nikischin Date: Sat, 14 Sep 2024 19:26:42 +0200 Subject: [PATCH 1/3] Implement detection for runner --- packages/bruno-app/src/components/CodeEditor/index.js | 3 ++- packages/bruno-electron/src/ipc/network/index.js | 3 +++ packages/bruno-js/src/bru.js | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index df9103891d..82c37af1f0 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -71,7 +71,8 @@ if (!SERVER_RENDERED) { 'bru.setNextRequest(requestName)', 'req.disableParsingResponseJson()', 'bru.getRequestVar(key)', - 'bru.sleep(ms)' + 'bru.sleep(ms)', + 'bru.isRunner()' ]; CodeMirror.registerHelper('hint', 'brunoJS', (editor, options) => { const cursor = editor.getCursor(); diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 768505e271..d85a947934 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -871,6 +871,7 @@ const registerNetworkIpc = (mainWindow) => { const scriptingConfig = get(brunoConfig, 'scripts', {}); scriptingConfig.runtime = getJsSandboxRuntime(collection); const collectionRoot = get(collection, 'root', {}); + runtimeVariables["run-folder-event"] = true; const abortController = new AbortController(); saveCancelToken(cancelTokenUid, abortController); @@ -1126,6 +1127,8 @@ const registerNetworkIpc = (mainWindow) => { ...eventData }); + runtimeVariables["run-folder-event"] = false; + mainWindow.webContents.send('main:script-environment-update', { envVariables: testResults.envVariables, runtimeVariables: testResults.runtimeVariables, diff --git a/packages/bruno-js/src/bru.js b/packages/bruno-js/src/bru.js index 7f24cea14b..624deb21a3 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -116,6 +116,10 @@ class Bru { sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } + + isRunner() { + return this.runtimeVariables["run-folder-event"] === true; + } } module.exports = Bru; From ef1922b37d8558977c45338db0c430e7a8093ed0 Mon Sep 17 00:00:00 2001 From: Nikischin Date: Sat, 14 Sep 2024 22:27:32 +0200 Subject: [PATCH 2/3] Different approach with cli variant --- packages/bruno-app/src/components/CodeEditor/index.js | 4 ++-- packages/bruno-cli/src/runner/run-single-request.js | 3 +++ packages/bruno-electron/src/ipc/network/index.js | 8 ++++++-- packages/bruno-js/src/bru.js | 4 ++-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index 82c37af1f0..c8079db38c 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -71,8 +71,8 @@ if (!SERVER_RENDERED) { 'bru.setNextRequest(requestName)', 'req.disableParsingResponseJson()', 'bru.getRequestVar(key)', - 'bru.sleep(ms)', - 'bru.isRunner()' + 'bru.getExecutionMode()', + 'bru.sleep(ms)' ]; CodeMirror.registerHelper('hint', 'brunoJS', (editor, options) => { const cursor = editor.getCursor(); diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index b260f6be97..0e40e5d18b 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -36,6 +36,9 @@ const runSingleRequest = async function ( collectionRoot, runtime ) { + + runtimeVariables["__internal__executionMode"] = 'cli'; + try { let request; let nextRequestName; diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index d85a947934..3793edf687 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -490,6 +490,8 @@ const registerNetworkIpc = (mainWindow) => { const cancelTokenUid = uuid(); const requestUid = uuid(); + runtimeVariables["__internal__executionMode"] = 'single'; + mainWindow.webContents.send('main:run-request-event', { type: 'request-queued', requestUid, @@ -659,6 +661,8 @@ const registerNetworkIpc = (mainWindow) => { collectionUid }); + runtimeVariables["__internal__executionMode"] = undefined; + mainWindow.webContents.send('main:script-environment-update', { envVariables: testResults.envVariables, runtimeVariables: testResults.runtimeVariables, @@ -871,7 +875,7 @@ const registerNetworkIpc = (mainWindow) => { const scriptingConfig = get(brunoConfig, 'scripts', {}); scriptingConfig.runtime = getJsSandboxRuntime(collection); const collectionRoot = get(collection, 'root', {}); - runtimeVariables["run-folder-event"] = true; + runtimeVariables["__internal__executionMode"] = 'runner'; const abortController = new AbortController(); saveCancelToken(cancelTokenUid, abortController); @@ -1127,7 +1131,7 @@ const registerNetworkIpc = (mainWindow) => { ...eventData }); - runtimeVariables["run-folder-event"] = false; + runtimeVariables["__internal__executionMode"] = undefined; mainWindow.webContents.send('main:script-environment-update', { envVariables: testResults.envVariables, diff --git a/packages/bruno-js/src/bru.js b/packages/bruno-js/src/bru.js index 624deb21a3..2c077dbc8e 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -117,8 +117,8 @@ class Bru { return new Promise((resolve) => setTimeout(resolve, ms)); } - isRunner() { - return this.runtimeVariables["run-folder-event"] === true; + getExecutionMode() { + return this.runtimeVariables["__internal__executionMode"]; } } From fdb6c49a842215676af82af88a7e72d0c4bffcda Mon Sep 17 00:00:00 2001 From: Nikischin Date: Sun, 15 Sep 2024 09:05:03 +0200 Subject: [PATCH 3/3] Fix error in Safe Mode --- packages/bruno-js/src/sandbox/quickjs/shims/bru.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/bruno-js/src/sandbox/quickjs/shims/bru.js b/packages/bruno-js/src/sandbox/quickjs/shims/bru.js index f045b134b4..acf327b2a5 100644 --- a/packages/bruno-js/src/sandbox/quickjs/shims/bru.js +++ b/packages/bruno-js/src/sandbox/quickjs/shims/bru.js @@ -81,6 +81,12 @@ const addBruShimToContext = (vm, bru) => { vm.setProp(bruObject, 'getCollectionVar', getCollectionVar); getCollectionVar.dispose(); + let getExecutionMode = vm.newFunction('getExecutionMode', function () { + return marshallToVm(bru.getExecutionMode(), vm); + }); + vm.setProp(bruObject, 'getExecutionMode', getExecutionMode); + getExecutionMode.dispose(); + const sleep = vm.newFunction('sleep', (timer) => { const t = vm.getString(timer); const promise = vm.newPromise();