-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass async methods in Isolate #322
Comments
Hey, not sure if that's what you're asking - but here's and example of how we're doing this in our project -
In general I'm using
and then - while calling it from the isolate
- using the Not even sure if that's the how it should be done - unfortunately the docs are pretty vague here... |
I think I'm trying something similar, exposing an external slow-running async method to untrusted code within the isolate, and allowing that untrusted code it to chain additional behavior to the resolved promise. I think this is the reverse of what is in @laverdet's example from #125 . I don't have it working though, the promise callbacks within the isolate don't seem to be called. Any help appreciated.
|
The following works with v4.7.2 of the library. Async function can be passed into the isolate and the awaited result can be transferred out of the isolate: import {Isolate, Reference} from "isolated-vm";
(async function () {
const isolate = new Isolate()
async function myAsyncFunction() {
return new Promise((resolve) => {
setTimeout(() => resolve('Hello from async function in isolate!'), 10);
});
}
const context = await isolate.createContext();
const jail = context.global;
await jail.set('myAsyncFunction', new Reference(myAsyncFunction));
const fn = await context.eval(`
(async function untrusted() {
let str = await myAsyncFunction.applySyncPromise();
return str;
})
`, { reference: true })
const value = await fn.apply(undefined, [], { result: { promise: true } })
console.log(value) // 'Hello from async function in isolate!'
})() |
Hey @siddharthvp i've a followup question here, i'm changing the async function here like to return an object, so
how to get this object out of the isolate? |
My bad, Got the answer, Thanks! |
@siddharthvp thanks for your example. Do you think it could be possible to provide code as string? I'm playing with the code but I didn't find the way. |
Hey! I've been trying a lot of methods to pass async functions in my isolate, but nothing works. I tried the raw method, externalcopy, reference, nothing works.
The text was updated successfully, but these errors were encountered: