laverdet / isolated-vm

Secure & isolated JS environments for nodejs
ISC License
2.19k stars 154 forks source link

TypeError: function XXX could not be cloned. #482

Closed jerodvenemafm closed 4 months ago

jerodvenemafm commented 4 months ago

I'm writing a function that allows users to define transforms. This is my sample, which takes an incoming webhook data chunk, transforms it into a new format, and sends it along. The process is pretty straightforward and works fine if I remove the "operate" array of actions from the list. However, with it the function fails with this error:

TypeError: function convert () { return 'query SampleQuery { client(id: "' + arguments[1] + '") { id name } }' } could not be cloned. at :5:11 at ()

Sample code is below. "transform" is a reference to the node-json-transform library.

` const { transform } = require("node-json-transform"); let req = { body: { data: { webHookEvent: { topic: 'CLIENT_UPDATE', appId: 'xxx', accountId: 'yyy', itemId: 'zzz', occurredAt: '2024-07-04T15:04:13-06:00' } } } }

// https://www.npmjs.com/package/node-json-transform const isolate = new ivm.Isolate({ memoryLimit: 64 }); const context = isolate.createContextSync(); const jail = context.global; jail.setSync('global', jail.derefInto()); jail.setSync('transform', transform);

console.log('Isolate created') let evalEd = context.evalClosureSync( function convert () { return 'query SampleQuery { client(id: "' + arguments[1] + '") { id name } }' } let x = transform(JSON.parse($0), { item: { query: 'data.webHookEvent.itemId' }, operate: [{ run: convert, on: 'query' }] }) return JSON.stringify(x);, [JSON.stringify(req.body)]); console.log(evalEd)`

laverdet commented 4 months ago

This is covered in the "frequently asked question" section of the readme