workerboxjs
Advanced tools
Comparing version 4.0.3 to 5.0.0
@@ -1,17 +0,7 @@ | ||
import scopeToString from './scopeToString.js'; | ||
import encodeArg from './encodeArg.js'; | ||
function argsToString (args, addCallback, runCallback) { | ||
const newArgs = []; | ||
for (const arg of args) { | ||
if (typeof arg === 'function') { | ||
newArgs.push(['callback', addCallback(arg)]); | ||
} else if (typeof arg === 'object') { | ||
newArgs.push(['object', scopeToString(arg, addCallback, runCallback)]); | ||
} else { | ||
newArgs.push(['literal', arg]); | ||
} | ||
} | ||
return JSON.stringify(newArgs); | ||
return JSON.stringify(encodeArg(args, addCallback, runCallback)); | ||
} | ||
export default argsToString; |
@@ -5,3 +5,3 @@ declare type WorkerBoxOptions = { | ||
declare type Scope = object; | ||
declare function createWorkerBox(scriptUrl: any, options: WorkerBoxOptions): Promise<{ | ||
declare function createWorkerBox(scriptUrl: any, options?: WorkerBoxOptions): Promise<{ | ||
run: (code: string, scope?: Scope) => Promise<any>; | ||
@@ -8,0 +8,0 @@ destroy: () => any; |
@@ -43,3 +43,3 @@ import createCallbackStore from './createCallbackStore.js'; | ||
if (options.appendVersion) { | ||
scriptUrl = scriptUrl + '/v4.0.3/'; | ||
scriptUrl = scriptUrl + '/v5.0.0/'; | ||
} | ||
@@ -99,3 +99,4 @@ | ||
}, | ||
destroy: () => instance.destroy() | ||
destroy: () => instance.destroy(), | ||
scriptUrl: scriptUrl.href | ||
}; | ||
@@ -102,0 +103,0 @@ } |
@@ -0,15 +1,7 @@ | ||
import encodeArg from './encodeArg'; | ||
function scopeToString (scope, addCallback, runCallback) { | ||
const newScope = {}; | ||
for (const key in scope) { | ||
if (typeof scope[key] === 'function') { | ||
newScope[key] = ['callback', addCallback(scope[key])]; | ||
} else if (typeof scope[key] === 'object') { | ||
newScope[key] = ['object', scopeToString(scope[key], addCallback, runCallback)]; | ||
} else { | ||
newScope[key] = ['literal', scope[key]]; | ||
} | ||
} | ||
return JSON.stringify(newScope); | ||
return JSON.stringify(encodeArg(scope || {}, addCallback, runCallback)); | ||
} | ||
export default scopeToString; |
@@ -1,25 +0,7 @@ | ||
import argsToString from './argsToString.js'; | ||
import stringToScope from './stringToScope.js'; | ||
import decodeArg from './decodeArg.js'; | ||
function stringToArgs (args, addCallback, runCallback) { | ||
args = JSON.parse(args); | ||
const newArgs = []; | ||
for (const arg of args) { | ||
if (arg[0] === 'callback') { | ||
const fn = (...args) => runCallback( | ||
arg[1], | ||
argsToString(args, addCallback) | ||
); | ||
newArgs.push(fn); | ||
} else if (arg[0] === 'object') { | ||
newArgs.push(stringToScope(arg[1], addCallback, runCallback)); | ||
} else { | ||
newArgs.push(arg[1]); | ||
} | ||
} | ||
return newArgs; | ||
return decodeArg(JSON.parse(args), addCallback, runCallback); | ||
} | ||
export default stringToArgs; |
@@ -1,25 +0,7 @@ | ||
import argsToString from './argsToString.js'; | ||
import decodeArg from './decodeArg.js'; | ||
function stringToScope (object, addCallback, runCallback) { | ||
object = JSON.parse(object); | ||
const newObject = {}; | ||
for (const key in object) { | ||
if (object[key][0] === 'callback') { | ||
const fn = (...args) => { | ||
return runCallback( | ||
object[key][1], | ||
argsToString(args, addCallback) | ||
); | ||
}; | ||
newObject[key] = fn; | ||
} else if (object[key][0] === 'object') { | ||
newObject[key] = stringToScope(object[key][1], addCallback, runCallback); | ||
} else { | ||
newObject[key] = object[key][1]; | ||
} | ||
} | ||
return newObject; | ||
return decodeArg(JSON.parse(object), addCallback, runCallback); | ||
} | ||
export default stringToScope; |
{ | ||
"name": "workerboxjs", | ||
"version": "4.0.3", | ||
"version": "5.0.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "A secure sandbox to execute untrusted user JavaScript, in a web browser, without any risk to your own domain/site/page.", |
@@ -21,7 +21,21 @@ # WorkerBox | ||
let callback; | ||
const scope = { | ||
name: 'Mark', | ||
getMessage: () => 'Have a great day!' | ||
getMessage: () => 'Have a great day!', | ||
setCallback: fn => { | ||
// You can store arguments, objects, arrays and returned values | ||
// outside of the scope of your main app, and then call them | ||
// from anywhere, so long as the worker is not destroyed. | ||
callback = fn; | ||
} | ||
}; | ||
setInterval(() => { | ||
if (callback) { | ||
// This will communicate with the workerbox transparently. | ||
callback(); | ||
} | ||
}); | ||
// You can save state between running code | ||
@@ -70,9 +84,13 @@ // But this will not save between different workerbox instances. | ||
Visit https://0.0.0.0:8000 in your browser. | ||
### Run the tests | ||
Build the server side component and run the tests: | ||
``` | ||
npm run test | ||
npm run build | ||
npm test | ||
``` | ||
Visit https://0.0.0.0:8000 in your browser. | ||
## How does it work? | ||
@@ -83,2 +101,2 @@ An iframe is inserted into the page from a completely separate domain. | ||
Because the only communication between the user code and the workerbox instead is done through messaging, the argument inputs and outputs must all be JSON serializable. | ||
Because the only communication between the user code and the workerbox is done through messaging, the argument inputs and outputs must all be JSON serializable. |
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
10837
13
100
179