Comparing version 2.6.1 to 2.7.0
@@ -7,2 +7,3 @@ import type { Methods, MethodsNames, MethodsProxied, MethodArguments, MethodReturn, Options } from './types'; | ||
private timeout; | ||
private env; | ||
private name; | ||
@@ -9,0 +10,0 @@ private size; |
@@ -11,2 +11,3 @@ /* IMPORT */ | ||
this.terminateTimeout = options.autoterminate ?? 60000; | ||
this.env = options.env || {}; | ||
this.name = options.name ?? 'WorkTank-Worker'; | ||
@@ -76,3 +77,3 @@ this.size = options.size ?? 1; | ||
const name = this._getWorkerName(); | ||
const worker = new Worker(this.methods, name); | ||
const worker = new Worker(this.env, this.methods, name); | ||
this.workersReady.add(worker); | ||
@@ -84,3 +85,3 @@ return worker; | ||
const name = this._getWorkerName(); | ||
const worker = new Worker(this.methods, name); | ||
const worker = new Worker(this.env, this.methods, name); | ||
this.workersReady.add(worker); | ||
@@ -87,0 +88,0 @@ } |
@@ -37,3 +37,7 @@ type FN = (...args: any[]) => any; | ||
type MethodProxied<T extends FN> = (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>; | ||
type Env = Partial<{ | ||
[key: string]: string; | ||
}>; | ||
type Options<T extends Methods> = { | ||
env?: Env; | ||
name?: string; | ||
@@ -56,2 +60,2 @@ size?: number; | ||
export type { Methods, MethodsNames, MethodsFunctions, MethodsProxied, MethodFunction, MethodArguments, MethodReturn, MethodProxied }; | ||
export type { Options, Task }; | ||
export type { Env, Options, Task }; |
@@ -1,2 +0,2 @@ | ||
declare const _default: "globalThis.WorkTankWorkerBackend=new class{constructor(){this.methods={};addEventListener(\"message\",this.message.bind(this))}exec(e,s){let o=this.methods[e],a=new Promise(t=>t(o.apply(void 0,s))),r=t=>{try{postMessage({type:\"result\",value:t})}catch(n){i(n)}},i=t=>{t=t instanceof Error?t:typeof t==\"string\"?new Error(t):new Error;let{message:n,name:d,stack:c}=t;postMessage({type:\"result\",error:{message:n,name:d,stack:c}})};a.then(r,i)}init(){postMessage({type:\"ready\"})}message(e){if(e.data.type===\"init\")return this.init();if(e.data.type===\"exec\")return this.exec(e.data.method,e.data.args)}register(e,s){this.methods[e]=s}}; /*! METHODS_PLACEHOLDER !*/"; | ||
declare const _default: "globalThis.WorkTankWorkerBackend=new class{constructor(){this.methods={};addEventListener(\"message\",this.message.bind(this))}exec(s,t){let i=this.methods[s],a=new Promise(e=>e(i.apply(void 0,t))),r=e=>{try{postMessage({type:\"result\",value:e})}catch(n){o(n)}},o=e=>{e=e instanceof Error?e:typeof e==\"string\"?new Error(e):new Error;let{message:n,name:c,stack:d}=e;postMessage({type:\"result\",error:{message:n,name:c,stack:d}})};a.then(r,o)}init(){postMessage({type:\"ready\"})}message(s){if(s.data.type===\"init\")return this.init();if(s.data.type===\"exec\")return this.exec(s.data.method,s.data.args)}register(s,t){this.methods[s]=t}};globalThis.process||(globalThis.process={});globalThis.process.env={...globalThis.process.env,...JSON.parse(\"/*! ENV_PLACEHOLDER !*/\")}; /*! METHODS_PLACEHOLDER !*/"; | ||
export default _default; |
@@ -1,1 +0,1 @@ | ||
export default 'globalThis.WorkTankWorkerBackend=new class{constructor(){this.methods={};addEventListener("message",this.message.bind(this))}exec(e,s){let o=this.methods[e],a=new Promise(t=>t(o.apply(void 0,s))),r=t=>{try{postMessage({type:"result",value:t})}catch(n){i(n)}},i=t=>{t=t instanceof Error?t:typeof t=="string"?new Error(t):new Error;let{message:n,name:d,stack:c}=t;postMessage({type:"result",error:{message:n,name:d,stack:c}})};a.then(r,i)}init(){postMessage({type:"ready"})}message(e){if(e.data.type==="init")return this.init();if(e.data.type==="exec")return this.exec(e.data.method,e.data.args)}register(e,s){this.methods[e]=s}}; /*! METHODS_PLACEHOLDER !*/'; | ||
export default 'globalThis.WorkTankWorkerBackend=new class{constructor(){this.methods={};addEventListener("message",this.message.bind(this))}exec(s,t){let i=this.methods[s],a=new Promise(e=>e(i.apply(void 0,t))),r=e=>{try{postMessage({type:"result",value:e})}catch(n){o(n)}},o=e=>{e=e instanceof Error?e:typeof e=="string"?new Error(e):new Error;let{message:n,name:c,stack:d}=e;postMessage({type:"result",error:{message:n,name:c,stack:d}})};a.then(r,o)}init(){postMessage({type:"ready"})}message(s){if(s.data.type==="init")return this.init();if(s.data.type==="exec")return this.exec(s.data.method,s.data.args)}register(s,t){this.methods[s]=t}};globalThis.process||(globalThis.process={});globalThis.process.env={...globalThis.process.env,...JSON.parse("/*! ENV_PLACEHOLDER !*/")}; /*! METHODS_PLACEHOLDER !*/'; |
@@ -42,4 +42,9 @@ /* IMPORT */ | ||
}; | ||
/* PLACEHOLDERS */ | ||
globalThis.process || (globalThis.process = {}); | ||
globalThis.process.env = { | ||
...globalThis.process.env, | ||
...JSON.parse("/*! ENV_PLACEHOLDER !*/") | ||
}; | ||
export {}; | ||
/* PLACEHOLDER */ | ||
/*! METHODS_PLACEHOLDER !*/ |
@@ -1,5 +0,5 @@ | ||
import type { Message } from '../types'; | ||
import type { Message, Env } from '../types'; | ||
declare class WorkerFrontend { | ||
private worker; | ||
constructor(methods: string, name: string, listener: Function); | ||
constructor(env: Env, methods: string, name: string, listener: Function); | ||
listen(listener: Function): void; | ||
@@ -6,0 +6,0 @@ send(message: Message): void; |
@@ -7,4 +7,4 @@ /* IMPORT */ | ||
/* CONSTRUCTOR */ | ||
constructor(methods, name, listener) { | ||
const code = `data:text/javascript;charset=utf-8,${encodeURIComponent(WorkerBackend.replace('/*! METHODS_PLACEHOLDER !*/', `\n\n\n${methods}`))}`; | ||
constructor(env, methods, name, listener) { | ||
const code = `data:text/javascript;charset=utf-8,${encodeURIComponent(WorkerBackend.replace('"/*! ENV_PLACEHOLDER !*/"', `'${JSON.stringify(env)}'`).replace('/*! METHODS_PLACEHOLDER !*/', `\n\n\n${methods}`))}`; | ||
this.worker = new WorkerShim(code, { name, type: 'module' }); | ||
@@ -11,0 +11,0 @@ this.listen(listener); |
@@ -1,2 +0,2 @@ | ||
import type { Message, MessageReady, MessageResult, Methods, Task } from '../types'; | ||
import type { Message, MessageReady, MessageResult, Methods, Env, Task } from '../types'; | ||
declare class Worker<T extends Methods> { | ||
@@ -6,2 +6,3 @@ busy: boolean; | ||
terminated: boolean; | ||
private env; | ||
private name; | ||
@@ -11,3 +12,3 @@ private methods; | ||
private worker; | ||
constructor(methods: string, name: string); | ||
constructor(env: Env, methods: string, name: string); | ||
onMessage(message: Message): void; | ||
@@ -14,0 +15,0 @@ onMessageReady(message: MessageReady): void; |
@@ -6,9 +6,10 @@ /* IMPORT */ | ||
/* CONSTRUCTOR */ | ||
constructor(methods, name) { | ||
constructor(env, methods, name) { | ||
this.busy = false; | ||
this.loaded = false; | ||
this.terminated = false; | ||
this.env = env; | ||
this.name = name; | ||
this.methods = methods; | ||
this.worker = new WorkerFrontend(this.methods, this.name, this.onMessage.bind(this)); | ||
this.worker = new WorkerFrontend(this.env, this.methods, this.name, this.onMessage.bind(this)); | ||
this.init(); | ||
@@ -15,0 +16,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "A simple isomorphic library for executing functions inside WebWorkers or Node Threads pools.", | ||
"version": "2.6.1", | ||
"version": "2.7.0", | ||
"type": "module", | ||
@@ -8,0 +8,0 @@ "main": "dist/index.js", |
@@ -39,2 +39,5 @@ # WorkTank | ||
autoterminate: 60000, // The interval of milliseconds at which to check if the pool can be automatically terminated, to free up resources, workers will be spawned up again if needed | ||
env: { // An object containing custom environment variables to pass to the worker threads | ||
CUSTOM_ENV: '123' | ||
}, | ||
methods: { // An object mapping function names to functions objects to serialize and deserialize into each worker thread, only functions that don't depend on their closure can be serialized | ||
@@ -41,0 +44,0 @@ sum: function ( a: number, b: number ): Promise<number> { |
@@ -6,3 +6,3 @@ | ||
import Worker from '~/worker'; | ||
import type {Methods, MethodsNames, MethodsProxied, MethodArguments, MethodFunction, MethodReturn, MethodProxied, Options, Task} from '~/types'; | ||
import type {Methods, MethodsNames, MethodsProxied, MethodArguments, MethodFunction, MethodReturn, MethodProxied, Env, Options, Task} from '~/types'; | ||
@@ -19,2 +19,3 @@ /* MAIN */ | ||
private timeout: number; | ||
private env: Env; | ||
private name: string; | ||
@@ -35,2 +36,3 @@ private size: number; | ||
this.terminateTimeout = options.autoterminate ?? 60000; | ||
this.env = options.env || {}; | ||
this.name = options.name ?? 'WorkTank-Worker'; | ||
@@ -134,3 +136,3 @@ this.size = options.size ?? 1; | ||
const name = this._getWorkerName (); | ||
const worker = new Worker<T> ( this.methods, name ); | ||
const worker = new Worker<T> ( this.env, this.methods, name ); | ||
@@ -148,3 +150,3 @@ this.workersReady.add ( worker ); | ||
const name = this._getWorkerName (); | ||
const worker = new Worker<T> ( this.methods, name ); | ||
const worker = new Worker<T> ( this.env, this.methods, name ); | ||
@@ -151,0 +153,0 @@ this.workersReady.add ( worker ); |
@@ -60,3 +60,8 @@ | ||
type Env = Partial<{ | ||
[key: string]: string | ||
}>; | ||
type Options<T extends Methods> = { | ||
env?: Env, | ||
name?: string, | ||
@@ -83,2 +88,2 @@ size?: number, | ||
export type {Methods, MethodsNames, MethodsFunctions, MethodsProxied, MethodFunction, MethodArguments, MethodReturn, MethodProxied}; | ||
export type {Options, Task}; | ||
export type {Env, Options, Task}; |
@@ -1,1 +0,1 @@ | ||
export default 'globalThis.WorkTankWorkerBackend=new class{constructor(){this.methods={};addEventListener("message",this.message.bind(this))}exec(e,s){let o=this.methods[e],a=new Promise(t=>t(o.apply(void 0,s))),r=t=>{try{postMessage({type:"result",value:t})}catch(n){i(n)}},i=t=>{t=t instanceof Error?t:typeof t=="string"?new Error(t):new Error;let{message:n,name:d,stack:c}=t;postMessage({type:"result",error:{message:n,name:d,stack:c}})};a.then(r,i)}init(){postMessage({type:"ready"})}message(e){if(e.data.type==="init")return this.init();if(e.data.type==="exec")return this.exec(e.data.method,e.data.args)}register(e,s){this.methods[e]=s}}; /*! METHODS_PLACEHOLDER !*/'; | ||
export default 'globalThis.WorkTankWorkerBackend=new class{constructor(){this.methods={};addEventListener("message",this.message.bind(this))}exec(s,t){let i=this.methods[s],a=new Promise(e=>e(i.apply(void 0,t))),r=e=>{try{postMessage({type:"result",value:e})}catch(n){o(n)}},o=e=>{e=e instanceof Error?e:typeof e=="string"?new Error(e):new Error;let{message:n,name:c,stack:d}=e;postMessage({type:"result",error:{message:n,name:c,stack:d}})};a.then(r,o)}init(){postMessage({type:"ready"})}message(s){if(s.data.type==="init")return this.init();if(s.data.type==="exec")return this.exec(s.data.method,s.data.args)}register(s,t){this.methods[s]=t}};globalThis.process||(globalThis.process={});globalThis.process.env={...globalThis.process.env,...JSON.parse("/*! ENV_PLACEHOLDER !*/")}; /*! METHODS_PLACEHOLDER !*/'; |
@@ -69,4 +69,10 @@ | ||
/* PLACEHOLDER */ | ||
/* PLACEHOLDERS */ | ||
globalThis.process ||= {}; | ||
globalThis.process.env = { | ||
...globalThis.process.env, | ||
...JSON.parse ( "/*! ENV_PLACEHOLDER !*/" ) | ||
}; | ||
/*! METHODS_PLACEHOLDER !*/ |
@@ -6,3 +6,3 @@ | ||
import WorkerBackend from '~/worker/backend_compiled'; | ||
import type {Message} from '~/types'; | ||
import type {Message, Env} from '~/types'; | ||
@@ -19,5 +19,5 @@ /* MAIN */ | ||
constructor ( methods: string, name: string, listener: Function ) { | ||
constructor ( env: Env, methods: string, name: string, listener: Function ) { | ||
const code = `data:text/javascript;charset=utf-8,${encodeURIComponent ( WorkerBackend.replace ( '/*! METHODS_PLACEHOLDER !*/', `\n\n\n${methods}` ) )}`; | ||
const code = `data:text/javascript;charset=utf-8,${encodeURIComponent ( WorkerBackend.replace ( '"/*! ENV_PLACEHOLDER !*/"', `'${JSON.stringify ( env )}'` ).replace ( '/*! METHODS_PLACEHOLDER !*/', `\n\n\n${methods}` ) )}`; | ||
@@ -24,0 +24,0 @@ this.worker = new WorkerShim ( code, { name, type: 'module' } ); |
@@ -5,3 +5,3 @@ | ||
import WorkerFrontend from '~/worker/frontend'; | ||
import type {Message, MessageReady, MessageResult, Methods, Task} from '~/types'; | ||
import type {Message, MessageReady, MessageResult, Methods, Env, Task} from '~/types'; | ||
@@ -18,2 +18,3 @@ /* MAIN */ | ||
private env: Env; | ||
private name: string; | ||
@@ -26,3 +27,3 @@ private methods: string; | ||
constructor ( methods: string, name: string ) { | ||
constructor ( env: Env, methods: string, name: string ) { | ||
@@ -32,5 +33,6 @@ this.busy = false; | ||
this.terminated = false; | ||
this.env = env; | ||
this.name = name; | ||
this.methods = methods; | ||
this.worker = new WorkerFrontend ( this.methods, this.name, this.onMessage.bind ( this ) ); | ||
this.worker = new WorkerFrontend ( this.env, this.methods, this.name, this.onMessage.bind ( this ) ); | ||
@@ -37,0 +39,0 @@ this.init (); |
@@ -129,2 +129,24 @@ | ||
it ( 'can pass custom environment variables to workers', async t => { | ||
const pool = new WorkTank ({ | ||
name: 'example', | ||
env: { | ||
CUSTOM_ENV: '123' | ||
}, | ||
methods: { | ||
getEnv: () => { | ||
return globalThis.process.env.CUSTOM_ENV; | ||
} | ||
} | ||
}); | ||
const value = await pool.exec ( 'getEnv' ); | ||
t.is ( value, '123' ); | ||
pool.terminate (); | ||
}); | ||
}); |
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
40056
929
86