mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 17:33:28 +01:00
feature(async-script): adding support for using async pre-request scripts
This commit is contained in:
parent
ede122ab09
commit
0ac27dee56
@ -113,7 +113,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
|
|||||||
const requestScript = get(request, 'script.req');
|
const requestScript = get(request, 'script.req');
|
||||||
if(requestScript && requestScript.length) {
|
if(requestScript && requestScript.length) {
|
||||||
const scriptRuntime = new ScriptRuntime();
|
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', {
|
mainWindow.webContents.send('main:script-environment-update', {
|
||||||
envVariables: result.envVariables,
|
envVariables: result.envVariables,
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
const { NodeVM } = require('vm2');
|
const { NodeVM } = require('vm2');
|
||||||
const path = require('path');
|
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 Bru = require('../bru');
|
||||||
const BrunoRequest = require('../bruno-request');
|
const BrunoRequest = require('../bruno-request');
|
||||||
const BrunoResponse = require('../bruno-response');
|
const BrunoResponse = require('../bruno-response');
|
||||||
@ -11,16 +18,16 @@ const lodash = require('lodash');
|
|||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
const uuid = require('uuid');
|
const uuid = require('uuid');
|
||||||
const nanoid = require('nanoid');
|
const nanoid = require('nanoid');
|
||||||
|
const axios = require('axios');
|
||||||
const CryptoJS = require('crypto-js');
|
const CryptoJS = require('crypto-js');
|
||||||
|
|
||||||
class ScriptRuntime {
|
class ScriptRuntime {
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
runRequestScript(script, request, envVariables, collectionVariables, collectionPath) {
|
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath){
|
||||||
const bru = new Bru(envVariables, collectionVariables);
|
const bru = new Bru(envVariables, collectionVariables);
|
||||||
const req = new BrunoRequest(request);
|
const req = new BrunoRequest(request);
|
||||||
|
|
||||||
const context = {
|
const context = {
|
||||||
bru,
|
bru,
|
||||||
req
|
req
|
||||||
@ -32,19 +39,29 @@ class ScriptRuntime {
|
|||||||
external: true,
|
external: true,
|
||||||
root: [collectionPath],
|
root: [collectionPath],
|
||||||
mock: {
|
mock: {
|
||||||
|
// node libs
|
||||||
|
path,
|
||||||
|
stream,
|
||||||
|
util,
|
||||||
|
url,
|
||||||
|
http,
|
||||||
|
https,
|
||||||
|
punycode,
|
||||||
|
zlib,
|
||||||
|
// 3rd party libs
|
||||||
atob,
|
atob,
|
||||||
btoa,
|
btoa,
|
||||||
lodash,
|
lodash,
|
||||||
moment,
|
moment,
|
||||||
uuid,
|
uuid,
|
||||||
nanoid,
|
nanoid,
|
||||||
|
axios,
|
||||||
'crypto-js': CryptoJS
|
'crypto-js': CryptoJS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
const asyncVM = vm.run(`module.exports = async () => { ${script} }`, path.join(collectionPath, 'vm.js'));
|
||||||
vm.run(script, path.join(collectionPath, 'vm.js'));
|
await asyncVM();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
request,
|
request,
|
||||||
envVariables,
|
envVariables,
|
||||||
|
Loading…
Reference in New Issue
Block a user