// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
// SPDX-License-Identifier: GPL-3.0-or-later
+import { isMainThread, parentPort } from 'node:worker_threads'
+
type ExtendedKey = {
privateKey: DataView
chainCode: DataView
/**
* Polyfill for window methods which do not exist when executing Node.js tests.
*/
+ console.log(`isMainThread: ${isMainThread}`)
+ console.log(`parentPort: ${parentPort}`)
if (typeof window === 'undefined' && typeof self === 'undefined') {
+ console.log(`in one`)
try {
- import('node:worker_threads').then(worker_threads => {
- const { isMainThread, parentPort } = worker_threads
- if (!isMainThread && parentPort) {
- var addEventListener = Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
- var postMessage = Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
- }
- })
+ if (!isMainThread && parentPort) {
+ console.log(`in two`)
+ //@ts-expect-error
+ var addEventListener = addEventListener || Object.getPrototypeOf(parentPort).addListener.bind(parentPort)
+ //@ts-expect-error
+ var postMessage = postMessage || Object.getPrototypeOf(parentPort).postMessage.bind(parentPort)
+ }
} catch { }
}
+ console.log(`typeof addEventListener: ${typeof addEventListener}`)
+ console.log(`typeof postMessage: ${typeof postMessage}`)
+ console.log(' ')
const BIP44_COIN_NANO = 165
const BIP44_PURPOSE = 44
*/
if (typeof addEventListener === 'function') {
addEventListener('message', (message: any): void => {
+ console.log(`bip44 worker`)
const data = JSON.parse(new TextDecoder().decode(message.data ?? message))
calculate(data).then(results => {
const buf = new TextEncoder().encode(JSON.stringify(results)).buffer
- //@ts-expect-error
postMessage(buf, [buf])
})
})
const start = b.toString().indexOf('{') + 1
const end = b.toString().lastIndexOf('return')
-export const worker = b.toString().substring(start, end)
+let worker_threads = ''
+if (typeof window === 'undefined' && typeof self === 'undefined') {
+ worker_threads += `import { isMainThread, parentPort } from 'node:worker_threads'\n`
+}
+export const worker = worker_threads + b.toString().substring(start, end)