Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

workerboxjs

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workerboxjs - npm Package Compare versions

Comparing version 2.3.0 to 3.1.0

88

lib/index.js

@@ -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"
}
}

16

README.md

@@ -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!'
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc