workerboxjs
Advanced tools
Comparing version 3.2.3 to 3.3.0
let workerBoxCount = 0; | ||
function createWorkerBox (scriptUrl, options) { | ||
@@ -22,10 +21,14 @@ options = { | ||
console.error(error); | ||
throw new Error([ | ||
'createWorkerBox must be given a remote sandbox server to isolate unsafe code.', | ||
'a free hosted version is available at https://workerbox.net/' | ||
].join('\n')); | ||
throw new Error( | ||
[ | ||
'createWorkerBox must be given a remote sandbox server to isolate unsafe code.', | ||
'a free hosted version is available at https://workerbox.net/' | ||
].join('\n') | ||
); | ||
} | ||
if (options.randomiseSubdomain) { | ||
const subdomain = [...Array(30)].map(() => Math.random().toString(36)[2]).join(''); | ||
const subdomain = [...Array(30)] | ||
.map(() => Math.random().toString(36)[2]) | ||
.join(''); | ||
scriptUrl.host = `${subdomain}.${scriptUrl.host}`; | ||
@@ -35,11 +38,14 @@ } | ||
workerBoxCount = workerBoxCount + 1; | ||
return new Promise(resolve => { | ||
return new Promise((resolve) => { | ||
const iframe = document.createElement('iframe'); | ||
iframe.sandbox = 'allow-scripts allow-same-origin'; | ||
iframe.id = `workerBox${workerBoxCount}`; | ||
iframe.style = 'position: fixed; height: 0; width: 0; opacity: 0; top: -100px;'; | ||
iframe.style = | ||
'position: fixed; height: 0; width: 0; opacity: 0; top: -100px;'; | ||
iframe.src = scriptUrl.href; | ||
document.body.appendChild(iframe); | ||
const worker = document.getElementById(`workerBox${workerBoxCount}`).contentWindow; | ||
const worker = document.getElementById( | ||
`workerBox${workerBoxCount}` | ||
).contentWindow; | ||
@@ -68,3 +74,3 @@ const promises = {}; | ||
let messageNumber = 0; | ||
resolve((code, scope) => { | ||
const run = (code, scope) => { | ||
messageNumber = messageNumber + 1; | ||
@@ -92,9 +98,12 @@ const currentMessageNumber = messageNumber; | ||
newArgs.push((...rawArgs) => { | ||
return new Promise(resolve => { | ||
return new Promise((resolve) => { | ||
const args = prepareArgs([...rawArgs, resolve]); | ||
worker.postMessage({ | ||
messageNumber: currentMessageNumber, | ||
callbackKey: arg[1], | ||
callbackArgs: args | ||
}, '*'); | ||
worker.postMessage( | ||
{ | ||
messageNumber: currentMessageNumber, | ||
callbackKey: arg[1], | ||
callbackArgs: args | ||
}, | ||
'*' | ||
); | ||
}); | ||
@@ -113,11 +122,17 @@ }); | ||
const [resolve] = parseArgs(args).slice(-1); | ||
const result = await scope[key].call(null, ...parseArgs(args).slice(0, -1)); | ||
const result = await scope[key].call( | ||
null, | ||
...parseArgs(args).slice(0, -1) | ||
); | ||
resolve(result); | ||
}; | ||
worker.postMessage({ | ||
messageNumber: currentMessageNumber, | ||
code, | ||
scope: prepareScope(scope) | ||
}, '*'); | ||
worker.postMessage( | ||
{ | ||
messageNumber: currentMessageNumber, | ||
code, | ||
scope: prepareScope(scope) | ||
}, | ||
'*' | ||
); | ||
@@ -127,3 +142,9 @@ return new Promise((resolve, reject) => { | ||
}); | ||
}); | ||
}; | ||
run.destroy = () => { | ||
iframe.remove(); | ||
}; | ||
resolve(run); | ||
return; | ||
@@ -135,3 +156,6 @@ } | ||
if (event.data.functionKey) { | ||
promises[messageNumber].callFunction(event.data.functionKey, event.data.functionArgs); | ||
promises[messageNumber].callFunction( | ||
event.data.functionKey, | ||
event.data.functionArgs | ||
); | ||
return; | ||
@@ -138,0 +162,0 @@ } |
{ | ||
"name": "workerboxjs", | ||
"version": "3.2.3", | ||
"version": "3.3.0", | ||
"type": "module", | ||
"description": "A secure sandbox to execute untrusted user JavaScript, in a web browser, without any risk to your own domain/site/page.", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"scripts": { | ||
@@ -8,0 +9,0 @@ "start": "node build.js --watch & servatron --http2 --port 8002 --directory server/dist", |
@@ -34,2 +34,4 @@ # WorkerBox | ||
// result === 'Hello Mark. Have a great day!' | ||
run.destroy() // Destroys the worker box, terminating any running workers | ||
``` | ||
@@ -36,0 +38,0 @@ |
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
9373
147
58