feat: support loading external libraries

This commit is contained in:
Anoop M D 2023-01-29 12:33:12 +05:30
parent 905f459ed0
commit d89f12c071
4 changed files with 26 additions and 16 deletions

View File

@ -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({

View File

@ -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);
});

View File

@ -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,

View File

@ -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,