From 3ec8f640dde450d93f20084685ca5cadfea75c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Sat, 3 Feb 2024 11:06:08 +0100 Subject: [PATCH] Fix loading service worker --- .../src/signed-data-verifier-pool.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/signed-api/src/signed-data-verifier-pool.ts b/packages/signed-api/src/signed-data-verifier-pool.ts index 5032b005..890d7085 100644 --- a/packages/signed-api/src/signed-data-verifier-pool.ts +++ b/packages/signed-api/src/signed-data-verifier-pool.ts @@ -4,16 +4,22 @@ import workerpool, { type Pool } from 'workerpool'; let pool: Pool | undefined; export const initializeVerifierPool = () => { - pool = workerpool.pool(`${__dirname}/signed-data-verifier.ts`, { - // Allow using the worker as a TypeScript module. See: - // https://github.com/josdejong/workerpool/issues/379#issuecomment-1580093502. - // - // Note, that the pool default settings are well set, so we are leaving that as is. - workerType: 'thread', - workerThreadOpts: { - execArgv: ['--require', 'ts-node/register'], - }, - }); + // Allow using the worker from TS (run in development mode) or JS files (when compiled). Note, that transpiling the + // file in development mode is done by ts-node and so it must be available. + const extension = __filename.endsWith('.ts') ? 'ts' : 'js'; + // Allow using the worker as a TypeScript module. See: + // https://github.com/josdejong/workerpool/issues/379#issuecomment-1580093502. + // + // Note, that the pool default settings are well set, so we are leaving that as is. + const options = + extension === 'ts' + ? { + workerThreadOpts: { + execArgv: ['--require', 'ts-node/register'], + }, + } + : {}; + pool = workerpool.pool(`${__dirname}/signed-data-verifier.${extension}`, options); return pool; };