Skip to content

Commit

Permalink
feat:add res() function shim to safe-mode (usebruno#3345)
Browse files Browse the repository at this point in the history
  • Loading branch information
lohxt1 authored Oct 24, 2024
1 parent c82203a commit d70d4a4
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/bruno-js/src/sandbox/quickjs/shims/bruno-response.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const { marshallToVm } = require('../utils');

const addBrunoResponseShimToContext = (vm, res) => {
const resObject = vm.newObject();
let resFn = vm.newFunction('res', function (exprStr) {
return marshallToVm(res(vm.dump(exprStr)), vm);
});

const status = marshallToVm(res?.status, vm);
const headers = marshallToVm(res?.headers, vm);
const body = marshallToVm(res?.body, vm);
const responseTime = marshallToVm(res?.responseTime, vm);

vm.setProp(resObject, 'status', status);
vm.setProp(resObject, 'headers', headers);
vm.setProp(resObject, 'body', body);
vm.setProp(resObject, 'responseTime', responseTime);
vm.setProp(resFn, 'status', status);
vm.setProp(resFn, 'headers', headers);
vm.setProp(resFn, 'body', body);
vm.setProp(resFn, 'responseTime', responseTime);

status.dispose();
headers.dispose();
Expand All @@ -21,41 +23,41 @@ const addBrunoResponseShimToContext = (vm, res) => {
let getStatus = vm.newFunction('getStatus', function () {
return marshallToVm(res.getStatus(), vm);
});
vm.setProp(resObject, 'getStatus', getStatus);
vm.setProp(resFn, 'getStatus', getStatus);
getStatus.dispose();

let getHeader = vm.newFunction('getHeader', function (name) {
return marshallToVm(res.getHeader(vm.dump(name)), vm);
});
vm.setProp(resObject, 'getHeader', getHeader);
vm.setProp(resFn, 'getHeader', getHeader);
getHeader.dispose();

let getHeaders = vm.newFunction('getHeaders', function () {
return marshallToVm(res.getHeaders(), vm);
});
vm.setProp(resObject, 'getHeaders', getHeaders);
vm.setProp(resFn, 'getHeaders', getHeaders);
getHeaders.dispose();

let getBody = vm.newFunction('getBody', function () {
return marshallToVm(res.getBody(), vm);
});
vm.setProp(resObject, 'getBody', getBody);
vm.setProp(resFn, 'getBody', getBody);
getBody.dispose();

let getResponseTime = vm.newFunction('getResponseTime', function () {
return marshallToVm(res.getResponseTime(), vm);
});
vm.setProp(resObject, 'getResponseTime', getResponseTime);
vm.setProp(resFn, 'getResponseTime', getResponseTime);
getResponseTime.dispose();

let setBody = vm.newFunction('setBody', function (data) {
res.setBody(vm.dump(data));
});
vm.setProp(resObject, 'setBody', setBody);
vm.setProp(resFn, 'setBody', setBody);
setBody.dispose();

vm.setProp(vm.global, 'res', resObject);
resObject.dispose();
vm.setProp(vm.global, 'res', resFn);
resFn.dispose();
};

module.exports = addBrunoResponseShimToContext;

0 comments on commit d70d4a4

Please sign in to comment.