]> git.codecow.com Git - nano25519.git/commitdiff
Get specific about Node host thread. Consolidate worker URL check in async implementa...
authorChris Duncan <chris@codecow.com>
Thu, 18 Jun 2026 18:55:30 +0000 (11:55 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 18 Jun 2026 18:55:30 +0000 (11:55 -0700)
src/lib/nano25519.ts

index 3aea89b339246d160ba2427fa9c936574a446700..e8f7f4b623de017dc8d659eb388b5a9a2912bcd3 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2026 Chris Duncan <chris@codecow.com>
 //! 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 })
        }