// 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
}
const b = () => {
- /**
- * 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 {
- 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
/**
* Listens for messages from a calling function.
*/
- 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
- postMessage(buf, [buf])
- })
+ addEventListener('message', (message: any): void => {
+ 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])
})
- }
+ })
async function calculate (data: any[]): Promise<any[]> {
return new Promise(async (resolve) => {
// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
// SPDX-License-Identifier: GPL-3.0-or-later
-/**
-* Polyfill for window methods which do not exist when executing Node.js tests.
-*/
-if (typeof window === 'undefined' && typeof self === 'undefined') {
- const { Worker } = await import('node:worker_threads')
- //@ts-expect-error
- Worker.prototype.addEventListener = Worker.prototype.addListener
- //@ts-expect-error
- globalThis.Worker = Worker
-}
-
type Thread = {
isBusy: boolean
worker: Worker