You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
so far i have been successfully using synchronous functions such as this:
constisolate=newivm.Isolate({memoryLimit: 1024});constcontext=isolate.createContextSync();constjail=context.global;jail.setSync('global',jail.derefInto());jail.setSync('rainbowGradient',function(x){//return a color value for a given x value//return an array of r,g,b values//x is a number between 0 and 1letr=Math.floor(Math.sin(0.3*x+0)*127+128);letg=Math.floor(Math.sin(0.3*x+2)*127+128);letb=Math.floor(Math.sin(0.3*x+4)*127+128);return[r,g,b];})
which has been working well.
But now i am trying to use an async function.. which appears to be considerably more difficult.
I think applySyncPromise is what i need to use? i however have no idea how to make use of that.
Is there a way to do this, ideally without several pages of code for a seemingly simple task?
I would be very thankful for any advice; i have been trying to do this for quite a while.
I am aware that there are numerous other issues regarding this topic; i have however not been able to find a single conclusive answer.
The text was updated successfully, but these errors were encountered:
About applySyncPromise this function is used to implement synchronous APIs in the context of asynchronous APIs. For example you would use this to mock something similar to fs.readFileSync but the on the nodejs side it would actually invoke fs.readFile in order to avoid blocking the main loop.
Anyway, here is a plain example of something you might use for async to async invocations. Depending on how many, and what kind of, parameters you want your method to take it would change. Note that resolving a promise is, in effect, a new stack with a new timeout. So if you want to timeout the user's script after a period of time and also support promises then you have to do some bookkeeping for that.
constivm=require('isolated-vm');consttimer=require('node:timers/promises');constisolate=newivm.Isolate();constcontext=isolate.createContextSync();// This is the setup codeconstrequest=(callback,payload)=>{(asyncfunction(){console.log("received",payload);awaittimer.setTimeout(500);console.log("returning");returnpayload;}()).then(value=>callback.apply(null,[null,newivm.ExternalCopy(value).copyInto()],{timeout: 1000}),error=>callback.apply(null,[newivm.ExternalCopy(error).copyInto()],{timeout: 1000}),);};context.evalClosureSync(`globalThis.request = payload => new Promise((resolve, reject) => { const callback = (error, result) => error ? reject(error) : resolve(result); $0.apply(null, [ new $1.Reference(callback), new $1.ExternalCopy(payload).copyInto() ]); });`,[newivm.Reference(request),ivm]);// This is the client codeconstresult=context.eval(`request({ note: 'hello' })`,{promise: true,copy: true});result.then(console.log,console.error);
received { note: 'hello' }
returning
{ note: 'hello' }
Hello,
so far i have been successfully using synchronous functions such as this:
which has been working well.
But now i am trying to use an async function.. which appears to be considerably more difficult.
I think applySyncPromise is what i need to use? i however have no idea how to make use of that.
Is there a way to do this, ideally without several pages of code for a seemingly simple task?
I would be very thankful for any advice; i have been trying to do this for quite a while.
I am aware that there are numerous other issues regarding this topic; i have however not been able to find a single conclusive answer.
The text was updated successfully, but these errors were encountered: