workerboxjs
Advanced tools
Comparing version 2.3.0 to 3.1.0
@@ -7,4 +7,4 @@ let workerBoxCount = 0; | ||
randomiseSubdomain: false, | ||
...options, | ||
} | ||
...options | ||
}; | ||
@@ -16,3 +16,3 @@ if (scriptUrl.slice(-1) === '/') { | ||
if (options.appendVersion) { | ||
scriptUrl = scriptUrl + '/v2.3.0/'; | ||
scriptUrl = scriptUrl + '/v3.1.0/'; | ||
} | ||
@@ -32,3 +32,3 @@ | ||
const subdomain = [...Array(30)].map(() => Math.random().toString(36)[2]).join(''); | ||
scriptUrl.host = `${subdomain}.${scriptUrl.host}` | ||
scriptUrl.host = `${subdomain}.${scriptUrl.host}`; | ||
} | ||
@@ -48,16 +48,78 @@ | ||
const promises = {}; | ||
window.addEventListener('message', (event) => { | ||
const callbacks = {}; | ||
let currentCallbackId = 0; | ||
function prepareArgs (args) { | ||
const newArgs = []; | ||
for (const arg of args) { | ||
if (typeof arg === 'function') { | ||
currentCallbackId = currentCallbackId + 1; | ||
callbacks[currentCallbackId] = arg; | ||
newArgs.push(['callback', currentCallbackId]); | ||
} else if (typeof arg === 'object') { | ||
newArgs.push(['object', prepareArgs(arg)]); | ||
} else { | ||
newArgs.push(['literal', arg]); | ||
} | ||
} | ||
return newArgs; | ||
} | ||
window.addEventListener('message', async (event) => { | ||
if (event.data.ready) { | ||
let messageNumber = 0; | ||
resolve(code => { | ||
resolve((code, scope) => { | ||
messageNumber = messageNumber + 1; | ||
const currentMessageNumber = messageNumber; | ||
function prepareScope (scope) { | ||
const newScope = {}; | ||
for (const key in scope) { | ||
if (typeof scope[key] === 'function') { | ||
newScope[key] = ['function', key]; | ||
} else if (typeof scope[key] === 'object') { | ||
newScope[key] = ['object', prepareScope(scope[key])]; | ||
} else { | ||
newScope[key] = ['literal', scope[key]]; | ||
} | ||
} | ||
return newScope; | ||
} | ||
function parseArgs (args) { | ||
const newArgs = []; | ||
for (const arg of args) { | ||
if (arg[0] === 'callback') { | ||
newArgs.push((...rawArgs) => { | ||
return new Promise(resolve => { | ||
const args = prepareArgs([...rawArgs, resolve]); | ||
worker.postMessage({ | ||
messageNumber: currentMessageNumber, | ||
callbackKey: arg[1], | ||
callbackArgs: args | ||
}, '*'); | ||
}); | ||
}); | ||
} else if (arg[0] === 'object') { | ||
newArgs.push(parseArgs(arg[1])); | ||
} else { | ||
newArgs.push(arg[1]); | ||
} | ||
} | ||
return newArgs; | ||
} | ||
const callFunction = async (key, args) => { | ||
const [resolve] = parseArgs(args).slice(-1); | ||
const result = await scope[key].call(null, ...parseArgs(args).slice(0, -1)); | ||
resolve(result); | ||
}; | ||
worker.postMessage({ | ||
messageNumber: currentMessageNumber, | ||
code | ||
code, | ||
scope: prepareScope(scope) | ||
}, '*'); | ||
return new Promise((resolve, reject) => { | ||
promises[currentMessageNumber] = { resolve, reject }; | ||
promises[currentMessageNumber] = { resolve, reject, callFunction }; | ||
}); | ||
@@ -69,7 +131,13 @@ }); | ||
const { messageNumber, error, result } = event.data; | ||
if (event.data.functionKey) { | ||
promises[messageNumber].callFunction(event.data.functionKey, event.data.functionArgs); | ||
return; | ||
} | ||
if (error) { | ||
promises[messageNumber].reject(error); | ||
promises[messageNumber]?.reject(error); | ||
return; | ||
} | ||
promises[messageNumber].resolve(result); | ||
promises[messageNumber]?.resolve(result); | ||
}); | ||
@@ -76,0 +144,0 @@ }); |
{ | ||
"name": "workerboxjs", | ||
"version": "2.3.0", | ||
"version": "3.1.0", | ||
"type": "module", | ||
@@ -37,6 +37,15 @@ "description": "A secure sandbox to execute untrusted user JavaScript, in a web browser, without any risk to your own domain/site/page.", | ||
"@markwylde/ftp-deploy": "^1.2.0", | ||
"basictap": "^3.4.3", | ||
"chokidar": "^3.5.3", | ||
"debounce": "^1.2.1", | ||
"esbuild": "^0.15.12", | ||
"minify": "^9.1.0" | ||
"just-tap": "^1.5.1", | ||
"minify": "^9.1.0", | ||
"process": "^0.11.10", | ||
"puppeteer": "^19.2.0", | ||
"servatron": "^2.4.2" | ||
}, | ||
"dependencies": { | ||
"promise-deferred": "^2.0.3" | ||
} | ||
} |
@@ -20,10 +20,16 @@ # WorkerBox | ||
const scope = { | ||
name: 'Mark', | ||
getMessage: () => 'Have a great day!' | ||
}; | ||
const result = await run(` | ||
function add (a, b) { | ||
return a + b; | ||
async function sayHello (who) { | ||
return 'Hello ' + who + '. ' + await getMessage(); | ||
} | ||
add(1, 2); | ||
`); | ||
// result === 3 | ||
return sayHello(name); | ||
`, scope); | ||
// result === 'Hello Mark. Have a great day!' | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6773
124
35
1
10
+ Addedpromise-deferred@^2.0.3
+ Addedasap@2.0.6(transitive)
+ Addedpromise@8.3.0(transitive)
+ Addedpromise-deferred@2.0.4(transitive)