Skip to content

Commit

Permalink
change callback into promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ceelsoin committed Apr 14, 2023
1 parent d7420c6 commit c090375
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const req = {
body: { x: 10, y: 10}
};

const taskId = `${namespace}/${id}-${Date.now()}`; //or can be any uniq id

executeFunctionInSandbox(taskId, {
env: {
MY_VAR: 'TRUE', // environment variable will be available on Backstage.env.MY_VAR
Expand All @@ -79,14 +81,14 @@ executeFunctionInSandbox(taskId, {
namespace: "foo",
functionName: "bar",
options,
}, (result) => { // when result callback was called, the child process of sandbox execution will be died
if(result.error) {
console.error({ error: err.message || err }) //print error return from function execution
}

console.log(result) //print return from function execution
})

/* can return result using callback or using async await */
.then(result => {
console.log(result)
})
.catch(err => {
console.log(err)
})
```

## Configuration
Expand Down
34 changes: 22 additions & 12 deletions lib/ForkSandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ const child_process = require('child_process');

const Sandbox = require("./Sandbox");

const tasks = {};
const childs = {};

const codeFileName = (namespace, codeId) => {
return `${namespace}/${codeId}.js`;
}

const executeFunctionInSandbox = (id, data, callback) => {

childs[id] = child_process.fork(path.resolve(__filename)); //require this file to execute process.on('message') below
const executeFunctionInSandbox = (id, data) => {
return new Promise((resolve, reject) => {
childs[id] = child_process.fork(path.resolve(__filename)); //require this file to execute process.on('message') below

childs[id].send({ id, data });

childs[id].send({ id, data });
tasks[id] = callback;
childs[id].on('message', (message) => {
const result = message;
if (result.error) {
reject(result.error);
}

resolve(result.data);
delete childs[id];

childs[id].on('message', (message) => {
const result = message.data;
tasks[message.id](result);
delete tasks[id];
delete childs[id];
});
});
}

Expand All @@ -36,8 +39,15 @@ process.on('message', async (message) => {
const sandbox = new Sandbox({ env, globalModules, asyncTimeout, syncTimeout, config });
const filename = codeFileName(namespace, functionName);

const _req = Object.assign({
method: null,
headers: {},
body: {},
query: {},
}, req);

const script = sandbox.compileCode(filename, preCode.code)
const result = await sandbox.runScript(script, req, options);
const result = await sandbox.runScript(script, _req, options);
process.send({ id, data: result });
process.exit(0);
} catch (ex) {
Expand Down

0 comments on commit c090375

Please sign in to comment.