fix: fixed issue where vm2 instantiated objects were not being sent to renderer process

This commit is contained in:
Anoop M D 2023-09-23 03:14:27 +05:30
parent e3ce420216
commit 1c89ab3450
3 changed files with 32 additions and 11 deletions

View File

@ -10,6 +10,7 @@ const punycode = require('punycode');
const Bru = require('../bru');
const BrunoRequest = require('../bruno-request');
const BrunoResponse = require('../bruno-response');
const { cleanJson } = require('../utils');
// Inbuilt Library Support
const atob = require('atob');
@ -37,7 +38,7 @@ class ScriptRuntime {
if (onConsoleLog && typeof onConsoleLog === 'function') {
const customLogger = (type) => {
return (...args) => {
onConsoleLog(type, args);
onConsoleLog(type, cleanJson(args));
};
};
context.console = {
@ -81,8 +82,8 @@ class ScriptRuntime {
await asyncVM();
return {
request,
envVariables,
collectionVariables
envVariables: cleanJson(envVariables),
collectionVariables: cleanJson(collectionVariables)
};
}
@ -100,7 +101,7 @@ class ScriptRuntime {
if (onConsoleLog && typeof onConsoleLog === 'function') {
const customLogger = (type) => {
return (...args) => {
onConsoleLog(type, args);
onConsoleLog(type, cleanJson(args));
};
};
context.console = {
@ -136,8 +137,8 @@ class ScriptRuntime {
return {
response,
envVariables,
collectionVariables
envVariables: cleanJson(envVariables),
collectionVariables: cleanJson(collectionVariables)
};
}
}

View File

@ -6,6 +6,7 @@ const BrunoRequest = require('../bruno-request');
const BrunoResponse = require('../bruno-response');
const Test = require('../test');
const TestResults = require('../test-results');
const { cleanJson } = require('../utils');
// Inbuilt Library Support
const atob = require('atob');
@ -49,7 +50,7 @@ class TestRuntime {
if (onConsoleLog && typeof onConsoleLog === 'function') {
const customLogger = (type) => {
return (...args) => {
onConsoleLog(type, args);
onConsoleLog(type, cleanJson(args));
};
};
context.console = {
@ -82,9 +83,9 @@ class TestRuntime {
return {
request,
envVariables,
collectionVariables,
results: __brunoTestResults.getResults()
envVariables: cleanJson(envVariables),
collectionVariables: cleanJson(collectionVariables),
results: cleanJson(__brunoTestResults.getResults())
};
}
}

View File

@ -118,9 +118,28 @@ const createResponseParser = (response = {}) => {
return res;
};
/**
* Objects that are created inside vm2 execution context result in an serilaization error when sent to the renderer process
* Error sending from webFrameMain: Error: Failed to serialize arguments
* at s.send (node:electron/js2c/browser_init:169:631)
* at g.send (node:electron/js2c/browser_init:165:2156)
* How to reproduce
* Remove the cleanJson fix and execute the below post response script
* bru.setVar("a", {b:3});
* Todo: Find a better fix
*/
const cleanJson = (data) => {
try {
return JSON.parse(JSON.stringify(data));
} catch (e) {
return data;
}
};
module.exports = {
evaluateJsExpression,
evaluateJsTemplateLiteral,
createResponseParser,
internalExpressionCache
internalExpressionCache,
cleanJson
};