diff --git a/cjs/dist/index.d.ts b/cjs/dist/index.d.ts index e95ce80..996fda5 100644 --- a/cjs/dist/index.d.ts +++ b/cjs/dist/index.d.ts @@ -5,4 +5,4 @@ export declare function solveChallenge(challenge: string, salt: string, algorith promise: Promise; controller: AbortController; }; -export declare function solveChallengeWorkers(workerScript: string | URL, concurrency: number, challenge: string, salt: string, algorithm?: string, max?: number, startNumber?: number): Promise; +export declare function solveChallengeWorkers(workerScript: string | URL | (() => Worker), concurrency: number, challenge: string, salt: string, algorithm?: string, max?: number, startNumber?: number): Promise; diff --git a/cjs/dist/index.js b/cjs/dist/index.js index a61eeb2..80acd5e 100644 --- a/cjs/dist/index.js +++ b/cjs/dist/index.js @@ -76,9 +76,14 @@ async function solveChallengeWorkers(workerScript, concurrency, challenge, salt, throw new Error('Too many workers. Max. 16 allowed workers.'); } for (let i = 0; i < concurrency; i++) { - workers.push(new Worker(workerScript, { - type: 'module', - })); + if (typeof workerScript === 'function') { + workers.push(workerScript()); + } + else { + workers.push(new Worker(workerScript, { + type: 'module', + })); + } } const step = Math.ceil(max / concurrency); const solutions = await Promise.all(workers.map((worker, i) => { diff --git a/deno_dist/index.ts b/deno_dist/index.ts index f6d0ab9..0e4d835 100644 --- a/deno_dist/index.ts +++ b/deno_dist/index.ts @@ -92,7 +92,7 @@ export function solveChallenge( } export async function solveChallengeWorkers( - workerScript: string | URL, + workerScript: string | URL | (() => Worker), concurrency: number, challenge: string, salt: string, @@ -108,11 +108,15 @@ export async function solveChallengeWorkers( throw new Error('Too many workers. Max. 16 allowed workers.'); } for (let i = 0; i < concurrency; i++) { - workers.push( - new Worker(workerScript, { - type: 'module', - }) - ); + if (typeof workerScript === 'function') { + workers.push(workerScript()); + } else { + workers.push( + new Worker(workerScript, { + type: 'module', + }) + ); + } } const step = Math.ceil(max / concurrency); const solutions = await Promise.all( diff --git a/dist/index.d.ts b/dist/index.d.ts index e95ce80..996fda5 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -5,4 +5,4 @@ export declare function solveChallenge(challenge: string, salt: string, algorith promise: Promise; controller: AbortController; }; -export declare function solveChallengeWorkers(workerScript: string | URL, concurrency: number, challenge: string, salt: string, algorithm?: string, max?: number, startNumber?: number): Promise; +export declare function solveChallengeWorkers(workerScript: string | URL | (() => Worker), concurrency: number, challenge: string, salt: string, algorithm?: string, max?: number, startNumber?: number): Promise; diff --git a/dist/index.js b/dist/index.js index 2680d60..5433eb2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -70,9 +70,14 @@ export async function solveChallengeWorkers(workerScript, concurrency, challenge throw new Error('Too many workers. Max. 16 allowed workers.'); } for (let i = 0; i < concurrency; i++) { - workers.push(new Worker(workerScript, { - type: 'module', - })); + if (typeof workerScript === 'function') { + workers.push(workerScript()); + } + else { + workers.push(new Worker(workerScript, { + type: 'module', + })); + } } const step = Math.ceil(max / concurrency); const solutions = await Promise.all(workers.map((worker, i) => { diff --git a/lib/index.ts b/lib/index.ts index a70ead6..de9d70e 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -92,7 +92,7 @@ export function solveChallenge( } export async function solveChallengeWorkers( - workerScript: string | URL, + workerScript: string | URL | (() => Worker), concurrency: number, challenge: string, salt: string, @@ -108,11 +108,15 @@ export async function solveChallengeWorkers( throw new Error('Too many workers. Max. 16 allowed workers.'); } for (let i = 0; i < concurrency; i++) { - workers.push( - new Worker(workerScript, { - type: 'module', - }) - ); + if (typeof workerScript === 'function') { + workers.push(workerScript()); + } else { + workers.push( + new Worker(workerScript, { + type: 'module', + }) + ); + } } const step = Math.ceil(max / concurrency); const solutions = await Promise.all( diff --git a/package.json b/package.json index 4edebe1..0f554c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "altcha-lib", - "version": "0.1.3", + "version": "0.1.4", "description": "A library for creating and verifying ALTCHA challenges for Node.js, Bun and Deno.", "author": "Daniel Regeci", "license": "MIT", diff --git a/tests/challenge.test.ts b/tests/challenge.test.ts index c72a4bf..c149ccb 100644 --- a/tests/challenge.test.ts +++ b/tests/challenge.test.ts @@ -202,7 +202,9 @@ describe('challenge', () => { hmacKey, }); const result = await solveChallengeWorkers( - './lib/worker.ts', + () => new Worker('./lib/worker.ts', { + type: 'module', + }), 4, challenge.challenge, challenge.salt,