feat:add res() function shim to safe-mode (#3345)

This commit is contained in:
lohit 2024-10-24 14:59:54 +05:30 committed by GitHub
parent c82203a059
commit d70d4a482b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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();
@ -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;