fix(operators): _js encode and decode into QuickJS to escape chars in json.

This commit is contained in:
Gervwyk 2021-04-04 23:24:26 +02:00
parent 8bd18346b6
commit 45644db111
2 changed files with 28 additions and 15 deletions

View File

@ -46,20 +46,18 @@ function createFunction({ params, location, methodName }) {
body = params.body;
}
const fn = (...args) => {
// TODO: User serializer instead so serialize dates in. To do this we need to dependency free serializer,
// might be a good idea just adding it inline instead of porting in the serializer function.
const jsFnString = `
var args = JSON.parse(decodeURIComponent('${encodeURIComponent(JSON.stringify(args))}'));
function fn() ${body}
var result = JSON.stringify(fn());
`;
const codeHandle = QuickJsVm.unwrapResult(
QuickJsVm.evalCode(
`
var args = JSON.parse('${JSON.stringify(args)}');
function fn() {
${body}
}
var result = JSON.stringify(fn());
`,
{
shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000),
memoryLimitBytes: 1024 * 1024,
}
)
QuickJsVm.evalCode(jsFnString, {
shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000),
memoryLimitBytes: 1024 * 1024,
})
);
const resultHandle = QuickJsVm.getProp(QuickJsVm.global, 'result');
codeHandle.dispose();

View File

@ -32,17 +32,32 @@ test('_js.function with body and args specified', () => {
expect(fn(1, 2)).toEqual(3);
});
test('_js.function with body and no args specified', () => {
test('_js.evaluate with body and no args specified', () => {
const params = {
body: `{
const value = "world";
return 'a new ' + 'vm for Hello ' + value;
}`,
};
const fn = _js({ location, params, methodName: 'evaluate' });
expect(_js({ location, params, methodName: 'evaluate' })).toEqual('a new vm for Hello world');
});
test('_js.evaluate with body with args that needs escaped characters', () => {
const params = {
body: `{
return "<div>" + args[0].html + "</html>";
}`,
args: [
{
html: '<a href="https://lowdefy.com">Lowdefy Website</a>',
},
],
};
expect(_js({ location, params, methodName: 'evaluate' })).toEqual(
'<div><a href="https://lowdefy.com">Lowdefy Website</a></html>'
);
});
test('_js.function with file specified', () => {
const params = {
file: `