From c263c98634cadb521e3f617ad0358142c759c163 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 18 Jun 2026 11:55:30 -0700 Subject: [PATCH] Get specific about Node host thread. Consolidate worker URL check in async implementation. --- src/lib/nano25519.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib/nano25519.ts b/src/lib/nano25519.ts index 3aea89b..e8f7f4b 100644 --- a/src/lib/nano25519.ts +++ b/src/lib/nano25519.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { Worker as NodeWorker, threadId } from 'node:worker_threads' +import { MessagePort as NodeMessagePort, Worker as NodeWorker } from 'node:worker_threads' //@ts-expect-error import nano25519_wasm from '../../build/nano25519.wasm' @@ -180,7 +180,8 @@ const nano25519_init = (bytes: number[]): { derive: typeof derive, sign: typeof } let isListening = false - let host: any = null + let host: NodeMessagePort | null = null + let client: string | undefined = globalThis.location?.href /** * Parses inbound data when nano25519 is started as a Web Worker. Only called @@ -208,11 +209,9 @@ const nano25519_init = (bytes: number[]): { derive: typeof derive, sign: typeof throw new TypeError('Invalid nano25519Worker request') } const data: Data = message.data as object & { url: string, id: string, action: string } - url = data.url - id = data.id + const { url, id } = data if (typeof url !== 'string' || typeof id !== 'string') return - BROWSER: if (url !== location.href) return - NODE: if (url !== threadId.toString()) return + if (url !== client) return if (data.action === 'start') { isListening = true @@ -268,8 +267,9 @@ const nano25519_init = (bytes: number[]): { derive: typeof derive, sign: typeof NODE: { if (host == null) { import('node:worker_threads') - .then(({ parentPort }): void => { + .then(({ parentPort, threadId }): void => { host = parentPort + client = threadId.toString() host?.on('message', handleMessage) }) } @@ -307,7 +307,7 @@ function init (): void { NODE: url = worker.threadId.toString() console.log(`nano25519 initialized.`) isWorkerReady = true - } catch (err) { + } catch (err: any) { isWorkerReady = false throw new Error('nano25519 initialization failed.', { cause: err }) } -- 2.52.0