Skip to content

Commit

Permalink
feat(#263): Add feature flag for new runtime and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-treason committed Apr 11, 2024
1 parent b58d747 commit e528a2f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 57 deletions.
8 changes: 0 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const QueryResult = ({ item, collection, data, dataBuffer, width, disableRunEven
</div>
{error ? (
<div>
<div className="text-red-500">{error}</div>
<pre className="text-red-500 whitespace-pre-wrap">{error}</pre>

{error && typeof error === 'string' && error.toLowerCase().includes('self signed certificate') ? (
<div className="mt-6 muted text-xs">
Expand Down
32 changes: 14 additions & 18 deletions packages/bruno-cli/src/runner/run-single-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,16 @@ const runSingleRequest = async function (
get(collectionRoot, 'request.script.req'),
get(bruJson, 'request.script.req')
]).join(os.EOL);
// TODO: Add feature Flag
if (false) {
const variables = {
envVariables,
collectionVariables,
processEnvVars
};
if (scriptingConfig?.runtime === 'node') {
const result = await runScript(
decomment(requestScriptFile),
request,
null,
variables,
{
envVariables,
collectionVariables,
processEnvVars
},
false,
collectionPath,
scriptingConfig,
Expand Down Expand Up @@ -298,18 +296,16 @@ const runSingleRequest = async function (
get(collectionRoot, 'request.script.res'),
get(bruJson, 'request.script.res')
]).join(os.EOL);
// TODO: Add feature flag
if (false) {
const variables = {
envVariables,
collectionVariables,
processEnvVars
};
if (scriptingConfig?.runtime === 'node') {
const result = await runScript(
decomment(responseScriptFile),
request,
response,
variables,
{
envVariables,
collectionVariables,
processEnvVars
},
false,
collectionPath,
scriptingConfig,
Expand Down Expand Up @@ -366,11 +362,11 @@ const runSingleRequest = async function (
let testResults = [];
const testFile = compact([get(collectionRoot, 'request.tests'), get(bruJson, 'request.tests')]).join(os.EOL);

if (false) {
if (scriptingConfig?.runtime === 'node') {
const result = await runScript(
decomment(testFile),
request,
null,
response,
{
envVariables,
collectionVariables,
Expand Down
41 changes: 18 additions & 23 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const contentDispositionParser = require('content-disposition');
const mime = require('mime-types');
const { ipcMain } = require('electron');
const { isUndefined, isNull, each, get, compact, cloneDeep } = require('lodash');
const { VarsRuntime, AssertRuntime, ScriptRuntime, TestRuntime } = require('@usebruno/js');
const { VarsRuntime, AssertRuntime, ScriptRuntime, TestRuntime, runScript } = require('@usebruno/js');
const prepareRequest = require('./prepare-request');
const prepareCollectionRequest = require('./prepare-collection-request');
const prepareGqlIntrospectionRequest = require('./prepare-gql-introspection-request');
Expand Down Expand Up @@ -320,10 +320,9 @@ const registerNetworkIpc = (mainWindow) => {
}

// run pre-request script
let scriptResult, nextRequestName;
let scriptResult;
const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(os.EOL);
// TODO: Add feature flags
if (false) {
if (scriptingConfig?.runtime === 'node') {
scriptResult = await runScript(
decomment(requestScript),
request,
Expand Down Expand Up @@ -415,17 +414,16 @@ const registerNetworkIpc = (mainWindow) => {
}

// run post-response script
let scriptResult, nextRequestName;
let scriptResult;
const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join(
os.EOL
);

// TODO: Add feature flag
if (false) {
if (scriptingConfig?.runtime === 'node') {
scriptResult = await runScript(
decomment(responseScript),
request,
null,
response,
{
envVariables: envVars,
collectionVariables,
Expand All @@ -436,10 +434,6 @@ const registerNetworkIpc = (mainWindow) => {
scriptingConfig,
onConsoleLog
);
if (scriptResult?.nextRequestName !== undefined) {
nextRequestName = scriptResult.nextRequestName;
}

mainWindow.webContents.send('main:script-environment-update', {
envVariables: scriptResult.envVariables,
collectionVariables: scriptResult.collectionVariables,
Expand All @@ -460,9 +454,12 @@ const registerNetworkIpc = (mainWindow) => {
processEnvVars,
scriptingConfig
);
if (scriptResult?.nextRequestName !== undefined) {
nextRequestName = scriptResult.nextRequestName;
}
mainWindow.webContents.send('main:script-environment-update', {
envVariables: scriptResult.envVariables,
collectionVariables: scriptResult.collectionVariables,
requestUid,
collectionUid
});
}
}
return scriptResult;
Expand Down Expand Up @@ -631,12 +628,11 @@ const registerNetworkIpc = (mainWindow) => {
]).join(os.EOL);
if (typeof testFile === 'string') {
let testResults;
// TODO: Add feature flag
if (false) {
if (scriptingConfig?.runtime === 'node') {
testResults = await runScript(
decomment(testFile),
request,
null,
response,
{
envVariables: envVars,
collectionVariables,
Expand All @@ -645,7 +641,7 @@ const registerNetworkIpc = (mainWindow) => {
true,
collectionPath,
scriptingConfig,
null
onConsoleLog
);
} else {
const testRuntime = new TestRuntime();
Expand Down Expand Up @@ -1088,12 +1084,11 @@ const registerNetworkIpc = (mainWindow) => {
]).join(os.EOL);
if (typeof testFile === 'string') {
let testResults;
// TODO: Add feature flag
if (false) {
if (scriptingConfig?.runtime === 'node') {
testResults = await runScript(
decomment(testFile),
request,
null,
response,
{
envVariables: envVars,
collectionVariables,
Expand All @@ -1102,7 +1097,7 @@ const registerNetworkIpc = (mainWindow) => {
true,
collectionPath,
scriptingConfig,
null
onConsoleLog
);
} else {
const testRuntime = new TestRuntime();
Expand Down
14 changes: 7 additions & 7 deletions packages/bruno-js/src/runtime/vm-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ const defaultModuleWhiteList = [
'axios',
'chai',
'crypto-js',
'node-vault'
'node-vault',
'node-fetch'
];

/**
Expand All @@ -155,7 +156,6 @@ function createCustomRequire(scriptingConfig, collectionPath) {

const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
if (allowScriptFilesystemAccess) {
// TODO: Allow other modules like os, child_process etc too?
whitelistedModules.push('fs');
}

Expand All @@ -167,7 +167,7 @@ function createCustomRequire(scriptingConfig, collectionPath) {
additionalContextRootsAbsolute.push(collectionPath);

return (moduleName) => {
// First check If we want to require a native node module or
// First check If we want to require a native node module or an internal node module
// Remove the "node:" prefix, to make sure "node:fs" and "fs" can be required, and we only need to whitelist one
if (whitelistedModules.includes(moduleName.replace(/^node:/, ''))) {
try {
Expand Down Expand Up @@ -195,12 +195,12 @@ function createCustomRequire(scriptingConfig, collectionPath) {
const fullScriptPath = path.join(contextRoot, moduleName);
try {
return require(fullScriptPath);
} catch (error) {
triedPaths.push({ fullScriptPath, error });
} catch {
triedPaths.push({ fullScriptPath });
}
}

const triedPathsFormatted = triedPaths.map((i) => `- "${i.fullScriptPath}": ${i.error}\n`);
const triedPathsFormatted = triedPaths.map((i) => `- ${i.fullScriptPath}\n`);
throw new Error(`Failed to require "${moduleName}"!
If you tried to require a internal node module / external package, make sure its whitelisted in the "bruno.json" under "scriptConfig".
Expand All @@ -218,7 +218,7 @@ ${triedPathsFormatted}`);
function createCustomConsole(onConsoleLog) {
const customLogger = (type) => {
return (...args) => {
onConsoleLog(type, cleanJson(args));
onConsoleLog && onConsoleLog(type, cleanJson(args));
};
};
return {
Expand Down

0 comments on commit e528a2f

Please sign in to comment.