mirror of
https://github.com/usebruno/bruno.git
synced 2025-02-21 12:11:23 +01:00
chore: code cleanup
This commit is contained in:
parent
d4beb7ddda
commit
0c668c16f7
@ -92,7 +92,8 @@ class ScriptRuntime {
|
|||||||
if (this.runtime === 'quickjs') {
|
if (this.runtime === 'quickjs') {
|
||||||
await executeQuickJsVmAsync({
|
await executeQuickJsVmAsync({
|
||||||
script: script,
|
script: script,
|
||||||
context: context
|
context: context,
|
||||||
|
collectionPath
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -203,7 +204,8 @@ class ScriptRuntime {
|
|||||||
if (this.runtime === 'quickjs') {
|
if (this.runtime === 'quickjs') {
|
||||||
await executeQuickJsVmAsync({
|
await executeQuickJsVmAsync({
|
||||||
script: script,
|
script: script,
|
||||||
context: context
|
context: context,
|
||||||
|
collectionPath
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -4,6 +4,7 @@ const addConsoleShimToContext = require('./shims/console');
|
|||||||
const addBrunoResponseShimToContext = require('./shims/bruno-response');
|
const addBrunoResponseShimToContext = require('./shims/bruno-response');
|
||||||
const addTestShimToContext = require('./shims/test');
|
const addTestShimToContext = require('./shims/test');
|
||||||
const addLibraryShimsToContext = require('./shims/lib');
|
const addLibraryShimsToContext = require('./shims/lib');
|
||||||
|
const addLocalModuleLoaderShimToContext = require('./shims/local-module');
|
||||||
const { newQuickJSWASMModule, memoizePromiseFactory } = require('quickjs-emscripten');
|
const { newQuickJSWASMModule, memoizePromiseFactory } = require('quickjs-emscripten');
|
||||||
|
|
||||||
// execute `npm run sandbox:bundle-libraries` if the below file doesn't exist
|
// execute `npm run sandbox:bundle-libraries` if the below file doesn't exist
|
||||||
@ -55,7 +56,7 @@ const executeQuickJsVm = ({ script: externalScript, context: externalContext, sc
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const executeQuickJsVmAsync = async ({ script: externalScript, context: externalContext }) => {
|
const executeQuickJsVmAsync = async ({ script: externalScript, context: externalContext, collectionPath }) => {
|
||||||
if (!isNaN(Number(externalScript))) {
|
if (!isNaN(Number(externalScript))) {
|
||||||
return toNumber(externalScript);
|
return toNumber(externalScript);
|
||||||
}
|
}
|
||||||
@ -64,13 +65,35 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
|
|||||||
const vm = module.newContext();
|
const vm = module.newContext();
|
||||||
|
|
||||||
const bundledCode = getBundledCode?.toString() || '';
|
const bundledCode = getBundledCode?.toString() || '';
|
||||||
|
const moduleLoaderCode = function() {
|
||||||
|
return `
|
||||||
|
globalThis.require = (mod) => {
|
||||||
|
let lib = globalThis.requireObject[mod];
|
||||||
|
if (lib) {
|
||||||
|
return lib;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// fetch local module
|
||||||
|
let localModuleCode = globalThis.__brunoLoadLocalModule(mod);
|
||||||
|
|
||||||
|
// compile local module as iife
|
||||||
|
(function (){
|
||||||
|
const initModuleExportsCode = "const module = { exports: {} };"
|
||||||
|
const copyModuleExportsCode = "\\n;globalThis.requireObject[mod] = module.exports;";
|
||||||
|
eval(initModuleExportsCode + localModuleCode + copyModuleExportsCode);
|
||||||
|
})();
|
||||||
|
|
||||||
|
// resolve module
|
||||||
|
return globalThis.requireObject[mod];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
};
|
||||||
|
|
||||||
vm.evalCode(
|
vm.evalCode(
|
||||||
`
|
`
|
||||||
(${bundledCode})()
|
(${bundledCode})()
|
||||||
globalThis.require = (module) => {
|
${moduleLoaderCode()}
|
||||||
return globalThis.requireObject[module];
|
|
||||||
}
|
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -81,6 +104,7 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
|
|||||||
res && addBrunoResponseShimToContext(vm, res);
|
res && addBrunoResponseShimToContext(vm, res);
|
||||||
consoleFn && addConsoleShimToContext(vm, consoleFn);
|
consoleFn && addConsoleShimToContext(vm, consoleFn);
|
||||||
addSleepShimToContext(vm);
|
addSleepShimToContext(vm);
|
||||||
|
addLocalModuleLoaderShimToContext(vm, collectionPath);
|
||||||
|
|
||||||
await addLibraryShimsToContext(vm);
|
await addLibraryShimsToContext(vm);
|
||||||
|
|
||||||
|
31
packages/bruno-js/src/sandbox/quickjs/shims/local-module.js
Normal file
31
packages/bruno-js/src/sandbox/quickjs/shims/local-module.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
const { marshallToVm } = require('../utils');
|
||||||
|
|
||||||
|
const addLocalModuleLoaderShimToContext = (vm, collectionPath) => {
|
||||||
|
let loadLocalModuleHandle = vm.newFunction('loadLocalModule', function (module) {
|
||||||
|
const filename = vm.dump(module);
|
||||||
|
|
||||||
|
// Check if the filename has an extension
|
||||||
|
const hasExtension = path.extname(filename) !== '';
|
||||||
|
const resolvedFilename = hasExtension ? filename : `${filename}.js`;
|
||||||
|
|
||||||
|
// Resolve the file path and check if it's within the collectionPath
|
||||||
|
const filePath = path.resolve(collectionPath, resolvedFilename);
|
||||||
|
const relativePath = path.relative(collectionPath, filePath);
|
||||||
|
|
||||||
|
// Ensure the resolved file path is inside the collectionPath
|
||||||
|
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
||||||
|
throw new Error('Access to files outside of the collectionPath is not allowed.');
|
||||||
|
}
|
||||||
|
|
||||||
|
let code = fs.readFileSync(filePath).toString();
|
||||||
|
|
||||||
|
return marshallToVm(code, vm);
|
||||||
|
});
|
||||||
|
|
||||||
|
vm.setProp(vm.global, '__brunoLoadLocalModule', loadLocalModuleHandle);
|
||||||
|
loadLocalModuleHandle.dispose();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = addLocalModuleLoaderShimToContext;
|
5
packages/bruno-tests/collection/lib/constants.js
Normal file
5
packages/bruno-tests/collection/lib/constants.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
const PI = 3.14;
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
PI
|
||||||
|
};
|
@ -1,5 +1,9 @@
|
|||||||
|
const { PI } = require('./lib/constants');
|
||||||
|
|
||||||
const sum = (a, b) => a + b;
|
const sum = (a, b) => a + b;
|
||||||
|
const areaOfCircle = (radius) => PI * radius * radius;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sum
|
sum,
|
||||||
};
|
areaOfCircle
|
||||||
|
};
|
@ -9,3 +9,8 @@ get {
|
|||||||
body: none
|
body: none
|
||||||
auth: none
|
auth: none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
script:pre-request {
|
||||||
|
var CryptoJS = require("crypto-js");
|
||||||
|
console.log(CryptoJS);
|
||||||
|
}
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
meta {
|
||||||
|
name: sum (without js extn)
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
post {
|
||||||
|
url: {{host}}/api/echo/json
|
||||||
|
body: json
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert {
|
||||||
|
res.status: eq 200
|
||||||
|
}
|
||||||
|
|
||||||
|
script:pre-request {
|
||||||
|
const math = require("./lib/math");
|
||||||
|
console.log(math, 'math');
|
||||||
|
|
||||||
|
const body = req.getBody();
|
||||||
|
body.sum = math.sum(body.a, body.b);
|
||||||
|
body.areaOfCircle = math.areaOfCircle(2);
|
||||||
|
|
||||||
|
req.setBody(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
tests {
|
||||||
|
test("should return json", function() {
|
||||||
|
const data = res.getBody();
|
||||||
|
expect(res.getBody()).to.eql({
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"sum": 3,
|
||||||
|
"areaOfCircle": 12.56
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -22,8 +22,7 @@ assert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
script:pre-request {
|
script:pre-request {
|
||||||
const math = require("./lib/math");
|
const math = require("./lib/math.js");
|
||||||
|
|
||||||
const body = req.getBody();
|
const body = req.getBody();
|
||||||
body.sum = math.sum(body.a, body.b);
|
body.sum = math.sum(body.a, body.b);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user