//! 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'
}
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
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
NODE: {
if (host == null) {
import('node:worker_threads')
- .then(({ parentPort }): void => {
+ .then(({ parentPort, threadId }): void => {
host = parentPort
+ client = threadId.toString()
host?.on('message', handleMessage)
})
}
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 })
}