Comparing version 1.0.0 to 1.1.0
@@ -6,4 +6,3 @@ import { FN, PromiseValue, Methods, MethodsSerialized, Options, Task } from './types'; | ||
size: number; | ||
methods: Methods<MethodName, MethodFunction>; | ||
methodsSerialized: MethodsSerialized<MethodName>; | ||
methods: MethodsSerialized<MethodName> | string; | ||
tasksBusy: Set<Task<MethodName, MethodFunction>>; | ||
@@ -14,3 +13,3 @@ tasksReady: Set<Task<MethodName, MethodFunction>>; | ||
constructor(options: Options<MethodName, MethodFunction>); | ||
_getMethodsSerialized(): MethodsSerialized<MethodName>; | ||
_getMethodsSerialized(methods: Methods<MethodName, MethodFunction> | string): MethodsSerialized<MethodName> | string; | ||
_getTaskReady(): Task<MethodName, MethodFunction> | undefined; | ||
@@ -17,0 +16,0 @@ _getWorkerReady(): Worker<MethodName, MethodFunction> | undefined; |
@@ -12,4 +12,3 @@ "use strict"; | ||
this.size = (_a = options.size) !== null && _a !== void 0 ? _a : 1; | ||
this.methods = options.methods; | ||
this.methodsSerialized = this._getMethodsSerialized(); | ||
this.methods = this._getMethodsSerialized(options.methods); | ||
this.tasksBusy = new Set(); | ||
@@ -21,9 +20,13 @@ this.tasksReady = new Set(); | ||
/* HELPERS */ | ||
_getMethodsSerialized() { | ||
const methods = {}; | ||
for (const method in this.methods) { | ||
const serialized = this.methods[method].toString(); | ||
methods[method] = serialized; | ||
_getMethodsSerialized(methods) { | ||
if (typeof methods === 'string') { // Serialized function that returns the methods, useful for complex workers | ||
return methods; | ||
} | ||
return methods; | ||
else { // Deserialized methods map | ||
const serialized = {}; | ||
for (const method in methods) { | ||
serialized[method] = methods[method].toString(); | ||
} | ||
return serialized; | ||
} | ||
} | ||
@@ -39,3 +42,3 @@ _getTaskReady() { | ||
return; | ||
const worker = new worker_1.default(this.methodsSerialized); | ||
const worker = new worker_1.default(this.methods); | ||
this.workersReady.add(worker); | ||
@@ -42,0 +45,0 @@ return worker; |
@@ -13,3 +13,3 @@ declare type FN = (...args: any[]) => any; | ||
type: 'init'; | ||
methods: MethodsSerialized<string>; | ||
methods: MethodsSerialized<string> | string; | ||
}; | ||
@@ -37,3 +37,3 @@ declare type MessageReady = { | ||
size?: number; | ||
methods: Methods<MethodName, MethodFunction>; | ||
methods: Methods<MethodName, MethodFunction> | string; | ||
}; | ||
@@ -40,0 +40,0 @@ declare type Task<MethodName extends string = string, MethodFunction extends FN = FN, Method extends MethodName = MethodName> = { |
@@ -1,2 +0,2 @@ | ||
declare const _default: "(()=>{var i=(()=>{if(typeof postMessage==\"function\"){let t=globalThis;return{on:(n,r)=>{t.addEventListener(n,d=>{r(d.data)})},send:n=>{t.postMessage(n)}}}else{let t=require(\"worker_threads\").parentPort;return{on:(n,r)=>{t.on(n,r)},send:n=>{t.postMessage(n)}}}})(),o={methods:{},exec:(e,t)=>{let n=o.methods[e],r={require:globalThis.require},d=new Promise(s=>s(n.apply(r,t))),g=s=>{try{i.send({type:\"result\",value:s})}catch(c){a(c)}},a=s=>{s=s instanceof Error?s:typeof s==\"string\"?new Error(s):new Error;let{message:c,name:u,stack:p}=s;i.send({type:\"result\",error:{message:c,name:u,stack:p}})};d.then(g,a)},init:e=>{o.register(e),i.send({type:\"ready\"})},message:e=>{if(e.type===\"init\")return o.init(e.methods);if(e.type===\"exec\")return o.exec(e.method,e.args)},register:e=>{for(let t in e){let n=new Function(`return (${e[t]})`)();o.methods[t]=n}}};i.on(\"message\",o.message);})();"; | ||
declare const _default: "(()=>{const i=(()=>{const e=typeof postMessage==\"function\";if(e){const t=globalThis;return{on:(n,r)=>{t.addEventListener(n,d=>{r(d.data)})},send:n=>{t.postMessage(n)}}}else{const t=require(\"worker_threads\").parentPort;return{on:(n,r)=>{t.on(n,r)},send:n=>{t.postMessage(n)}}}})(),o={methods:{},exec:(e,t)=>{const n=o.methods[e],r={require:globalThis.require},d=new Promise(s=>s(n.apply(r,t))),g=s=>{try{i.send({type:\"result\",value:s})}catch(c){a(c)}},a=s=>{s=s instanceof Error?s:typeof s==\"string\"?new Error(s):new Error;const{message:c,name:u,stack:p}=s;i.send({type:\"result\",error:{message:c,name:u,stack:p}})};d.then(g,a)},init:e=>{o.register(e),i.send({type:\"ready\"})},message:e=>{if(e.type===\"init\")return o.init(e.methods);if(e.type===\"exec\")return o.exec(e.method,e.args)},register:e=>{if(typeof e==\"string\"){const t=new Function(e)();for(const n in t)o.methods[n]=t[n]}else for(const t in e){const n=new Function(`return (${e[t]})`)();o.methods[t]=n}}};i.on(\"message\",o.message);})();"; | ||
export default _default; |
"use strict"; | ||
/* COMPILED WORKER */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = '(()=>{var i=(()=>{if(typeof postMessage=="function"){let t=globalThis;return{on:(n,r)=>{t.addEventListener(n,d=>{r(d.data)})},send:n=>{t.postMessage(n)}}}else{let t=require("worker_threads").parentPort;return{on:(n,r)=>{t.on(n,r)},send:n=>{t.postMessage(n)}}}})(),o={methods:{},exec:(e,t)=>{let n=o.methods[e],r={require:globalThis.require},d=new Promise(s=>s(n.apply(r,t))),g=s=>{try{i.send({type:"result",value:s})}catch(c){a(c)}},a=s=>{s=s instanceof Error?s:typeof s=="string"?new Error(s):new Error;let{message:c,name:u,stack:p}=s;i.send({type:"result",error:{message:c,name:u,stack:p}})};d.then(g,a)},init:e=>{o.register(e),i.send({type:"ready"})},message:e=>{if(e.type==="init")return o.init(e.methods);if(e.type==="exec")return o.exec(e.method,e.args)},register:e=>{for(let t in e){let n=new Function(`return (${e[t]})`)();o.methods[t]=n}}};i.on("message",o.message);})();'; | ||
exports.default = '(()=>{const i=(()=>{const e=typeof postMessage=="function";if(e){const t=globalThis;return{on:(n,r)=>{t.addEventListener(n,d=>{r(d.data)})},send:n=>{t.postMessage(n)}}}else{const t=require("worker_threads").parentPort;return{on:(n,r)=>{t.on(n,r)},send:n=>{t.postMessage(n)}}}})(),o={methods:{},exec:(e,t)=>{const n=o.methods[e],r={require:globalThis.require},d=new Promise(s=>s(n.apply(r,t))),g=s=>{try{i.send({type:"result",value:s})}catch(c){a(c)}},a=s=>{s=s instanceof Error?s:typeof s=="string"?new Error(s):new Error;const{message:c,name:u,stack:p}=s;i.send({type:"result",error:{message:c,name:u,stack:p}})};d.then(g,a)},init:e=>{o.register(e),i.send({type:"ready"})},message:e=>{if(e.type==="init")return o.init(e.methods);if(e.type==="exec")return o.exec(e.method,e.args)},register:e=>{if(typeof e=="string"){const t=new Function(e)();for(const n in t)o.methods[n]=t[n]}else for(const t in e){const n=new Function(`return (${e[t]})`)();o.methods[t]=n}}};i.on("message",o.message);})();'; |
@@ -65,6 +65,14 @@ "use strict"; | ||
register: (methods) => { | ||
for (const method in methods) { | ||
const fn = new Function(`return (${methods[method]})`)(); | ||
Backend.methods[method] = fn; | ||
if (typeof methods === 'string') { // Serialized function that returns the methods | ||
const fns = new Function(methods)(); | ||
for (const method in fns) { | ||
Backend.methods[method] = fns[method]; | ||
} | ||
} | ||
else { // Serialized methods map | ||
for (const method in methods) { | ||
const fn = new Function(`return (${methods[method]})`)(); | ||
Backend.methods[method] = fn; | ||
} | ||
} | ||
} | ||
@@ -71,0 +79,0 @@ }; |
@@ -8,6 +8,6 @@ import { FN, Message, MessageReady, MessageResult, MethodsSerialized, Task } from '../types'; | ||
terminated: boolean; | ||
methods: MethodsSerialized<MethodName>; | ||
methods: MethodsSerialized<MethodName> | string; | ||
task?: Task<MethodName, MethodFunction>; | ||
worker: WorkerWeb | WorkerNode; | ||
constructor(methods: MethodsSerialized<MethodName>); | ||
constructor(methods: MethodsSerialized<MethodName> | string); | ||
onMessage(message: Message): void; | ||
@@ -14,0 +14,0 @@ onMessageReady(message: MessageReady): void; |
{ | ||
"name": "worktank", | ||
"description": "A simple isomorphic library for executing functions inside WebWorkers or Node Threads pools.", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "types": "dist/index.d.ts", |
@@ -16,4 +16,3 @@ | ||
size: number; | ||
methods: Methods<MethodName, MethodFunction>; | ||
methodsSerialized: MethodsSerialized<MethodName>; | ||
methods: MethodsSerialized<MethodName> | string; | ||
tasksBusy: Set<Task<MethodName, MethodFunction>>; | ||
@@ -30,4 +29,3 @@ tasksReady: Set<Task<MethodName, MethodFunction>>; | ||
this.size = options.size ?? 1; | ||
this.methods = options.methods; | ||
this.methodsSerialized = this._getMethodsSerialized (); | ||
this.methods = this._getMethodsSerialized ( options.methods ); | ||
this.tasksBusy = new Set (); | ||
@@ -42,16 +40,22 @@ this.tasksReady = new Set (); | ||
_getMethodsSerialized (): MethodsSerialized<MethodName> { | ||
_getMethodsSerialized ( methods: Methods<MethodName, MethodFunction> | string ): MethodsSerialized<MethodName> | string { | ||
const methods: MethodsSerialized<string> = {}; | ||
if ( typeof methods === 'string' ) { // Serialized function that returns the methods, useful for complex workers | ||
for ( const method in this.methods ) { | ||
return methods; | ||
const serialized = this.methods[method].toString (); | ||
} else { // Deserialized methods map | ||
methods[method] = serialized; | ||
const serialized: MethodsSerialized<string> = {}; | ||
for ( const method in methods ) { | ||
serialized[method] = methods[method].toString (); | ||
} | ||
return serialized; | ||
} | ||
return methods; | ||
} | ||
@@ -71,3 +75,3 @@ | ||
const worker = new Worker ( this.methodsSerialized ); | ||
const worker = new Worker ( this.methods ); | ||
@@ -74,0 +78,0 @@ this.workersReady.add ( worker ); |
@@ -18,3 +18,3 @@ | ||
type: 'init', | ||
methods: MethodsSerialized<string> | ||
methods: MethodsSerialized<string> | string | ||
}; | ||
@@ -52,3 +52,3 @@ | ||
size?: number, | ||
methods: Methods<MethodName, MethodFunction> | ||
methods: Methods<MethodName, MethodFunction> | string | ||
}; | ||
@@ -55,0 +55,0 @@ |
/* COMPILED WORKER */ | ||
export default '(()=>{var i=(()=>{if(typeof postMessage=="function"){let t=globalThis;return{on:(n,r)=>{t.addEventListener(n,d=>{r(d.data)})},send:n=>{t.postMessage(n)}}}else{let t=require("worker_threads").parentPort;return{on:(n,r)=>{t.on(n,r)},send:n=>{t.postMessage(n)}}}})(),o={methods:{},exec:(e,t)=>{let n=o.methods[e],r={require:globalThis.require},d=new Promise(s=>s(n.apply(r,t))),g=s=>{try{i.send({type:"result",value:s})}catch(c){a(c)}},a=s=>{s=s instanceof Error?s:typeof s=="string"?new Error(s):new Error;let{message:c,name:u,stack:p}=s;i.send({type:"result",error:{message:c,name:u,stack:p}})};d.then(g,a)},init:e=>{o.register(e),i.send({type:"ready"})},message:e=>{if(e.type==="init")return o.init(e.methods);if(e.type==="exec")return o.exec(e.method,e.args)},register:e=>{for(let t in e){let n=new Function(`return (${e[t]})`)();o.methods[t]=n}}};i.on("message",o.message);})();'; | ||
export default '(()=>{const i=(()=>{const e=typeof postMessage=="function";if(e){const t=globalThis;return{on:(n,r)=>{t.addEventListener(n,d=>{r(d.data)})},send:n=>{t.postMessage(n)}}}else{const t=require("worker_threads").parentPort;return{on:(n,r)=>{t.on(n,r)},send:n=>{t.postMessage(n)}}}})(),o={methods:{},exec:(e,t)=>{const n=o.methods[e],r={require:globalThis.require},d=new Promise(s=>s(n.apply(r,t))),g=s=>{try{i.send({type:"result",value:s})}catch(c){a(c)}},a=s=>{s=s instanceof Error?s:typeof s=="string"?new Error(s):new Error;const{message:c,name:u,stack:p}=s;i.send({type:"result",error:{message:c,name:u,stack:p}})};d.then(g,a)},init:e=>{o.register(e),i.send({type:"ready"})},message:e=>{if(e.type==="init")return o.init(e.methods);if(e.type==="exec")return o.exec(e.method,e.args)},register:e=>{if(typeof e=="string"){const t=new Function(e)();for(const n in t)o.methods[n]=t[n]}else for(const t in e){const n=new Function(`return (${e[t]})`)();o.methods[t]=n}}};i.on("message",o.message);})();'; |
@@ -78,3 +78,3 @@ | ||
init: ( methods: Record<string, string> ): void => { | ||
init: ( methods: Record<string, string> | string ): void => { | ||
@@ -95,10 +95,24 @@ Backend.register ( methods ); | ||
register: ( methods: Record<string, string> ): void => { | ||
register: ( methods: Record<string, string> | string ): void => { | ||
for ( const method in methods ) { | ||
if ( typeof methods === 'string' ) { // Serialized function that returns the methods | ||
const fn = new Function ( `return (${methods[method]})` )(); | ||
const fns = new Function ( methods )(); | ||
Backend.methods[method] = fn; | ||
for ( const method in fns ) { | ||
Backend.methods[method] = fns[method]; | ||
} | ||
} else { // Serialized methods map | ||
for ( const method in methods ) { | ||
const fn = new Function ( `return (${methods[method]})` )(); | ||
Backend.methods[method] = fn; | ||
} | ||
} | ||
@@ -105,0 +119,0 @@ |
@@ -17,3 +17,3 @@ | ||
terminated: boolean; | ||
methods: MethodsSerialized<MethodName>; | ||
methods: MethodsSerialized<MethodName> | string; | ||
task?: Task<MethodName, MethodFunction>; | ||
@@ -24,3 +24,3 @@ worker: WorkerWeb | WorkerNode; | ||
constructor ( methods: MethodsSerialized<MethodName> ) { | ||
constructor ( methods: MethodsSerialized<MethodName> | string ) { | ||
@@ -27,0 +27,0 @@ this.busy = false; |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
35431
811
2