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 { marshallToVm } = require('../utils');
const addBrunoResponseShimToContext = (vm, res) => { 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 status = marshallToVm(res?.status, vm);
const headers = marshallToVm(res?.headers, vm); const headers = marshallToVm(res?.headers, vm);
const body = marshallToVm(res?.body, vm); const body = marshallToVm(res?.body, vm);
const responseTime = marshallToVm(res?.responseTime, vm); const responseTime = marshallToVm(res?.responseTime, vm);
vm.setProp(resObject, 'status', status); vm.setProp(resFn, 'status', status);
vm.setProp(resObject, 'headers', headers); vm.setProp(resFn, 'headers', headers);
vm.setProp(resObject, 'body', body); vm.setProp(resFn, 'body', body);
vm.setProp(resObject, 'responseTime', responseTime); vm.setProp(resFn, 'responseTime', responseTime);
status.dispose(); status.dispose();
headers.dispose(); headers.dispose();
@ -21,41 +23,41 @@ const addBrunoResponseShimToContext = (vm, res) => {
let getStatus = vm.newFunction('getStatus', function () { let getStatus = vm.newFunction('getStatus', function () {
return marshallToVm(res.getStatus(), vm); return marshallToVm(res.getStatus(), vm);
}); });
vm.setProp(resObject, 'getStatus', getStatus); vm.setProp(resFn, 'getStatus', getStatus);
getStatus.dispose(); getStatus.dispose();
let getHeader = vm.newFunction('getHeader', function (name) { let getHeader = vm.newFunction('getHeader', function (name) {
return marshallToVm(res.getHeader(vm.dump(name)), vm); return marshallToVm(res.getHeader(vm.dump(name)), vm);
}); });
vm.setProp(resObject, 'getHeader', getHeader); vm.setProp(resFn, 'getHeader', getHeader);
getHeader.dispose(); getHeader.dispose();
let getHeaders = vm.newFunction('getHeaders', function () { let getHeaders = vm.newFunction('getHeaders', function () {
return marshallToVm(res.getHeaders(), vm); return marshallToVm(res.getHeaders(), vm);
}); });
vm.setProp(resObject, 'getHeaders', getHeaders); vm.setProp(resFn, 'getHeaders', getHeaders);
getHeaders.dispose(); getHeaders.dispose();
let getBody = vm.newFunction('getBody', function () { let getBody = vm.newFunction('getBody', function () {
return marshallToVm(res.getBody(), vm); return marshallToVm(res.getBody(), vm);
}); });
vm.setProp(resObject, 'getBody', getBody); vm.setProp(resFn, 'getBody', getBody);
getBody.dispose(); getBody.dispose();
let getResponseTime = vm.newFunction('getResponseTime', function () { let getResponseTime = vm.newFunction('getResponseTime', function () {
return marshallToVm(res.getResponseTime(), vm); return marshallToVm(res.getResponseTime(), vm);
}); });
vm.setProp(resObject, 'getResponseTime', getResponseTime); vm.setProp(resFn, 'getResponseTime', getResponseTime);
getResponseTime.dispose(); getResponseTime.dispose();
let setBody = vm.newFunction('setBody', function (data) { let setBody = vm.newFunction('setBody', function (data) {
res.setBody(vm.dump(data)); res.setBody(vm.dump(data));
}); });
vm.setProp(resObject, 'setBody', setBody); vm.setProp(resFn, 'setBody', setBody);
setBody.dispose(); setBody.dispose();
vm.setProp(vm.global, 'res', resObject); vm.setProp(vm.global, 'res', resFn);
resObject.dispose(); resFn.dispose();
}; };
module.exports = addBrunoResponseShimToContext; module.exports = addBrunoResponseShimToContext;