diff --git a/src/databaseWorker.ts b/src/databaseWorker.ts index 0e134fe..9f6342d 100644 --- a/src/databaseWorker.ts +++ b/src/databaseWorker.ts @@ -19,7 +19,7 @@ import { createWorkerEndpoint } from "./core/sqlite-db"; */ function log(level: 'log' | 'warn' | 'error', ...args: unknown[]) { if (parentPort) { - parentPort.postMessage({ kind: 'log', level, args: args.map(a => a instanceof Error ? a.message : a) }); + parentPort.postMessage({ kind: 'log', level, args: args.map(a => a instanceof Error ? a.message : a) }, []); } else { console[level](...args); } @@ -55,8 +55,12 @@ if (parentPort) { const wasHandled = processProtocolMessage( envelope, databaseEndpoint as Record unknown>, - (response, transfer) => { - parentPort!.postMessage(response, transfer); + (response, transfer?: Transferable[]) => { + if (transfer) { + parentPort!.postMessage(response, transfer as any[]); + } else { + parentPort!.postMessage(response, []); + } } ); diff --git a/src/platform/threadPool.ts b/src/platform/threadPool.ts index 94596a5..2ffc67c 100644 --- a/src/platform/threadPool.ts +++ b/src/platform/threadPool.ts @@ -9,6 +9,8 @@ * all VS Code runtime environments without conditional imports. */ +import type * as wt from 'worker_threads'; + // ============================================================================ // Type Definitions for Cross-Platform Message Passing // ============================================================================ @@ -18,7 +20,7 @@ * Used for communication in Node.js worker_threads. */ interface NodeMessagePort { - postMessage(data: unknown, transfer?: unknown[]): void; + postMessage(data: unknown, transfer?: any[]): void; on(event: string, handler: EventListenerOrEventListenerObject, options?: object): void; off(event: string, handler: EventListenerOrEventListenerObject, options?: object): void; } @@ -84,11 +86,11 @@ export function createBrowserParentPort(scope: BrowserParentPortScope): NodeMess // Runtime Detection and API Export // ============================================================================ -let WorkerImpl: any; -let MessageChannelImpl: any; -let MessagePortImpl: any; -let BroadcastChannelImpl: any; -let parentPortImpl: any; +let WorkerImpl: typeof globalThis.Worker | typeof wt.Worker; +let MessageChannelImpl: typeof globalThis.MessageChannel | typeof wt.MessageChannel; +let MessagePortImpl: typeof globalThis.MessagePort | typeof wt.MessagePort; +let BroadcastChannelImpl: typeof globalThis.BroadcastChannel | typeof wt.BroadcastChannel; +let parentPortImpl: typeof wt.parentPort | NodeMessagePort | null; if (import.meta.env?.VSCODE_BROWSER_EXT) { WorkerImpl = globalThis.Worker; diff --git a/src/workerFactory.ts b/src/workerFactory.ts index e3696dc..65c47a1 100644 --- a/src/workerFactory.ts +++ b/src/workerFactory.ts @@ -328,7 +328,8 @@ async function createWorkerBackedWasmDatabaseConnection( { postMessage: (data: unknown, transfer?: Transferable[]) => { if (transfer) { - workerThread.postMessage(data, transfer); + // worker_threads.Worker postMessage needs TransferListItem[], but we can pass it via any if necessary or let it be inferred if we cast it. Transferable in DOM is ArrayBuffer | MessagePort | ImageBitmap, which intersects with node's TransferListItem (ArrayBuffer | MessagePort | FileHandle | X509Certificate | Blob) at ArrayBuffer and MessagePort. + workerThread.postMessage(data, transfer as any); } else { workerThread.postMessage(data); }