Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: working on async scripting support #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
const requestScript = get(request, 'script.req');
if(requestScript && requestScript.length) {
const scriptRuntime = new ScriptRuntime();
const result = scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath);
const result = await scriptRuntime.runRequestScript(requestScript, request, envVars, collectionVariables, collectionPath);

mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables,
Expand Down
30 changes: 28 additions & 2 deletions packages/bruno-js/src/runtime/script-runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const { NodeVM } = require('vm2');
const path = require('path');
const http = require('http');
const https = require('https');
const stream = require('stream');
const util = require('util');
const zlib = require('zlib');
const url = require('url');
const punycode = require('punycode');
const Bru = require('../bru');
const BrunoRequest = require('../bruno-request');
const BrunoResponse = require('../bruno-response');
Expand All @@ -11,13 +18,14 @@ const lodash = require('lodash');
const moment = require('moment');
const uuid = require('uuid');
const nanoid = require('nanoid');
const axios = require('axios');
const CryptoJS = require('crypto-js');

class ScriptRuntime {
constructor() {
}

runRequestScript(script, request, envVariables, collectionVariables, collectionPath) {
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath) {
const bru = new Bru(envVariables, collectionVariables);
const req = new BrunoRequest(request);

Expand All @@ -32,18 +40,36 @@ class ScriptRuntime {
external: true,
root: [collectionPath],
mock: {
// node libs
path,
stream,
util,
url,
http,
https,
punycode,
zlib,
// 3rd party libs
atob,
btoa,
lodash,
moment,
uuid,
nanoid,
axios,
'crypto-js': CryptoJS
}
}
});

vm.run(script, path.join(collectionPath, 'vm.js'));
// wrap script inside a async function that gets called
script = `return (async () => { ${script} })()`;

// bug that needs to be fixed
// vm.run is not awaiting the async function
// created an issue in vm2 repo: https://github.com/patriksimek/vm2/issues/513
const result = await vm.run(script, path.join(collectionPath, 'vm.js'));
console.log(result);

return {
request,
Expand Down