]> zoso.dev Git - libnemo.git/commitdiff
Testing fixes to worker polyfill.
authorChris Duncan <chris@zoso.dev>
Thu, 5 Dec 2024 13:24:11 +0000 (05:24 -0800)
committerChris Duncan <chris@zoso.dev>
Thu, 5 Dec 2024 13:24:11 +0000 (05:24 -0800)
src/lib/bip44-ckd.ts

index b6ed3a55f8d0024f8c5686f5aa17ef852a2118ae..00a6dce24067228f2e3fe8411f68249dcb3eec35 100644 (file)
@@ -1,6 +1,8 @@
 // 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
@@ -10,17 +12,23 @@ 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 {
-                       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
@@ -32,10 +40,10 @@ const b = () => {
        */
        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])
                        })
                })
@@ -135,4 +143,8 @@ export const Bip44Ckd = b()
 
 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)