From 45644db11193c571cb886da50d74f397eb337104 Mon Sep 17 00:00:00 2001 From: Gervwyk Date: Sun, 4 Apr 2021 23:24:26 +0200 Subject: [PATCH] fix(operators): _js encode and decode into QuickJS to escape chars in json. --- packages/operators/src/common/js.js | 24 +++++++++++------------ packages/operators/test/common/js.test.js | 19 ++++++++++++++++-- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/packages/operators/src/common/js.js b/packages/operators/src/common/js.js index 6d3ad307f..87a313b36 100644 --- a/packages/operators/src/common/js.js +++ b/packages/operators/src/common/js.js @@ -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(); diff --git a/packages/operators/test/common/js.test.js b/packages/operators/test/common/js.test.js index c67a1fd7a..d55deb674 100644 --- a/packages/operators/test/common/js.test.js +++ b/packages/operators/test/common/js.test.js @@ -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 "
" + args[0].html + ""; + }`, + args: [ + { + html: 'Lowdefy Website', + }, + ], + }; + expect(_js({ location, params, methodName: 'evaluate' })).toEqual( + '
Lowdefy Website' + ); +}); + test('_js.function with file specified', () => { const params = { file: `