mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-12 15:40:30 +08:00
fix(operators): _js encode and decode into QuickJS to escape chars in json.
This commit is contained in:
parent
8bd18346b6
commit
45644db111
@ -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();
|
||||
|
@ -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: `
|
||||
|
Loading…
x
Reference in New Issue
Block a user