diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index 31c570d55..cdb7e1e9f 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -110,7 +110,7 @@ export const sendRequest = (item, collectionUid) => (dispatch, getState) => { const environment = findEnvironmentInCollection(collectionCopy, collection.activeEnvironmentUid); - sendNetworkRequest(itemCopy, collectionUid, environment) + sendNetworkRequest(itemCopy, collection, environment) .then((response) => { return dispatch( responseReceived({ diff --git a/packages/bruno-app/src/utils/network/index.js b/packages/bruno-app/src/utils/network/index.js index 43589b781..6ddc81e4a 100644 --- a/packages/bruno-app/src/utils/network/index.js +++ b/packages/bruno-app/src/utils/network/index.js @@ -1,8 +1,8 @@ -export const sendNetworkRequest = async (item, collectionUid, environment) => { +export const sendNetworkRequest = async (item, collection, environment) => { return new Promise((resolve, reject) => { if (['http-request', 'graphql-request'].includes(item.type)) { const timeStart = Date.now(); - sendHttpRequest(item, collectionUid, environment) + sendHttpRequest(item, collection, environment) .then((response) => { const timeEnd = Date.now(); resolve({ @@ -20,12 +20,12 @@ export const sendNetworkRequest = async (item, collectionUid, environment) => { }); }; -const sendHttpRequest = async (item, collectionUid, environment) => { +const sendHttpRequest = async (item, collection, environment) => { return new Promise((resolve, reject) => { const { ipcRenderer } = window; ipcRenderer - .invoke('send-http-request', item, collectionUid, environment) + .invoke('send-http-request', item, collection.uid, collection.pathname, environment) .then(resolve) .catch(reject); }); diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index da16832a4..e7fabbed5 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -32,7 +32,7 @@ const getEnvVars = (environment = {}) => { const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { // handler for sending http request - ipcMain.handle('send-http-request', async (event, item, collectionUid, environment) => { + ipcMain.handle('send-http-request', async (event, item, collectionUid, collectionPath, environment) => { const cancelTokenUid = uuid(); try { @@ -59,7 +59,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { if(request.script && request.script.length) { let script = request.script + '\n if (typeof onRequest === "function") {onRequest(brunoRequest);}'; const scriptRuntime = new ScriptRuntime(); - const res = scriptRuntime.runRequestScript(script, request, envVars); + const res = scriptRuntime.runRequestScript(script, request, envVars, collectionPath); mainWindow.webContents.send('main:script-environment-update', { environment: res.environment, @@ -67,6 +67,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { }); } + interpolateVars(request, envVars); mainWindow.webContents.send('main:http-request-sent', { requestSent: { url: request.url, @@ -79,14 +80,12 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { cancelTokenUid }); - interpolateVars(request, envVars); - const result = await axios(request); if(request.script && request.script.length) { let script = request.script + '\n if (typeof onResponse === "function") {onResponse(brunoResponse);}'; const scriptRuntime = new ScriptRuntime(); - const res = scriptRuntime.runResponseScript(script, result, envVars); + const res = scriptRuntime.runResponseScript(script, result, envVars, collectionPath); mainWindow.webContents.send('main:script-environment-update', { environment: res.environment, diff --git a/packages/bruno-js/src/scripts/script-runtime.js b/packages/bruno-js/src/scripts/script-runtime.js index 354bd50fd..ec3f43477 100644 --- a/packages/bruno-js/src/scripts/script-runtime.js +++ b/packages/bruno-js/src/scripts/script-runtime.js @@ -1,4 +1,5 @@ const { NodeVM } = require('vm2'); +const path = require('path'); const Bru = require('./bru'); const BrunoRequest = require('./bruno-request'); const BrunoResponse = require('./bruno-response'); @@ -7,7 +8,7 @@ class ScriptRuntime { constructor() { } - runRequestScript(script, request, environment) { + runRequestScript(script, request, environment, collectionPath) { const bru = new Bru(environment); const brunoRequest = new BrunoRequest(request); @@ -16,10 +17,15 @@ class ScriptRuntime { brunoRequest }; const vm = new NodeVM({ - sandbox: context + sandbox: context, + require: { + context: 'sandbox', + external: true, + root: [collectionPath] + } }); - vm.run(script); + vm.run(script, path.join(collectionPath, 'vm.js')); return { request, @@ -27,7 +33,7 @@ class ScriptRuntime { }; } - runResponseScript(script, response, environment) { + runResponseScript(script, response, environment, collectionPath) { const bru = new Bru(environment); const brunoResponse = new BrunoResponse(response); @@ -36,10 +42,15 @@ class ScriptRuntime { brunoResponse }; const vm = new NodeVM({ - sandbox: context + sandbox: context, + require: { + context: 'sandbox', + external: true, + root: [collectionPath] + } }); - vm.run(script); + vm.run(script, path.join(collectionPath, 'vm.js')); return { response,