@xylabs/threads 5.0.95 → 5.0.96

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +348 -214
  2. package/dist/browser/{index-browser-CaVWHLzx.d.ts → index-browser-PIBA6s8X.d.ts} +2 -2
  3. package/dist/browser/index-browser.d.ts +3 -3
  4. package/dist/browser/index-browser.mjs +18 -18
  5. package/dist/browser/index-browser.mjs.map +1 -1
  6. package/dist/browser/master/implementation.browser.d.ts +1 -1
  7. package/dist/browser/master/implementation.browser.mjs +5 -5
  8. package/dist/browser/master/implementation.browser.mjs.map +1 -1
  9. package/dist/browser/master/index-browser.d.ts +3 -3
  10. package/dist/browser/master/index-browser.mjs +18 -18
  11. package/dist/browser/master/index-browser.mjs.map +1 -1
  12. package/dist/browser/master/pool-browser.d.ts +2 -2
  13. package/dist/browser/master/pool-browser.mjs +4 -4
  14. package/dist/browser/master/pool-browser.mjs.map +1 -1
  15. package/dist/browser/{master-tsvhUhUx.d.ts → master-Cbd5sMft.d.ts} +1 -3
  16. package/dist/browser/worker/worker.browser.d.ts +2 -2
  17. package/dist/browser/worker/worker.browser.mjs +7 -7
  18. package/dist/browser/worker/worker.browser.mjs.map +1 -1
  19. package/dist/{node/worker-DW4hSCsH.d.ts → browser/worker-Bk-ckoOM.d.ts} +1 -3
  20. package/dist/neutral/master/register.mjs +5 -5
  21. package/dist/neutral/master/register.mjs.map +1 -1
  22. package/dist/neutral/master/spawn.d.ts +2 -4
  23. package/dist/neutral/master/spawn.mjs +10 -10
  24. package/dist/neutral/master/spawn.mjs.map +1 -1
  25. package/dist/neutral/master/thread.d.ts +1 -1
  26. package/dist/neutral/master/thread.mjs +2 -2
  27. package/dist/neutral/master/thread.mjs.map +1 -1
  28. package/dist/neutral/{master-4pyGz_8-.d.ts → master-C26rzf58.d.ts} +1 -3
  29. package/dist/neutral/observable-promise.mjs.map +1 -1
  30. package/dist/neutral/observable.mjs +1 -1
  31. package/dist/neutral/observable.mjs.map +1 -1
  32. package/dist/node/{index-node-DqmfxCpi.d.ts → index-node-BjdYwJiq.d.ts} +3 -3
  33. package/dist/node/index-node.d.ts +4 -4
  34. package/dist/node/index-node.mjs +19 -19
  35. package/dist/node/index-node.mjs.map +1 -1
  36. package/dist/node/master/implementation.node.d.ts +1 -1
  37. package/dist/node/master/implementation.node.mjs +5 -5
  38. package/dist/node/master/implementation.node.mjs.map +1 -1
  39. package/dist/node/master/index-node.d.ts +4 -4
  40. package/dist/node/master/index-node.mjs +19 -19
  41. package/dist/node/master/index-node.mjs.map +1 -1
  42. package/dist/node/master/pool-node.d.ts +2 -2
  43. package/dist/node/master/pool-node.mjs +3 -3
  44. package/dist/node/master/pool-node.mjs.map +1 -1
  45. package/dist/node/{master-CUiPpMI3.d.ts → master-w3q_qDzT.d.ts} +1 -3
  46. package/dist/node/{pool-types-Ay463MW0.d.ts → pool-types-CV3A06rk.d.ts} +2 -2
  47. package/dist/node/worker/worker.node.d.ts +3 -3
  48. package/dist/node/worker/worker.node.mjs +7 -7
  49. package/dist/node/worker/worker.node.mjs.map +1 -1
  50. package/dist/{browser/worker-DW4hSCsH.d.ts → node/worker-Bk-ckoOM.d.ts} +1 -3
  51. package/package.json +10 -11
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/master/pool-browser.ts","../../../src/master/implementation.browser.ts","../../../src/master/pool-types.ts","../../../src/symbols.ts","../../../src/master/thread.ts"],"sourcesContent":["/* eslint-disable import-x/export */\n/* eslint-disable unicorn/no-thenable */\n\n/* eslint-disable @typescript-eslint/member-ordering */\n/* eslint-disable unicorn/no-array-reduce */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-namespace */\n\n/// <reference lib=\"esnext\" />\n\nimport DebugLogger from 'debug'\nimport {\n multicast, Observable, Subject,\n} from 'observable-fns'\n\nimport { defaultPoolSize } from './implementation.browser.ts'\nimport type {\n PoolEvent, QueuedTask, TaskRunFunction, WorkerDescriptor,\n} from './pool-types.ts'\nimport { PoolEventType } from './pool-types.ts'\nimport { Thread } from './thread.ts'\n\nexport declare namespace Pool {\n type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>\n type EventType = PoolEventType\n}\n\nlet nextPoolID = 1\n\nfunction createArray(size: number): number[] {\n const array: number[] = []\n for (let index = 0; index < size; index++) {\n array.push(index)\n }\n return array\n}\n\nfunction delay(ms: number) {\n return new Promise(resolve => setTimeout(resolve, ms))\n}\n\nfunction flatMap<In, Out>(array: In[], mapper: (element: In) => Out[]): Out[] {\n return array.reduce<Out[]>((flattened, element) => [...flattened, ...mapper(element)], [])\n}\n\nfunction slugify(text: string) {\n return text.replaceAll(/\\W/g, ' ').trim().replaceAll(/\\s+/g, '-')\n}\n\nfunction spawnWorkers<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, count: number): Array<WorkerDescriptor<ThreadType>> {\n return createArray(count).map(\n (): WorkerDescriptor<ThreadType> => ({\n init: spawnWorker(),\n runningTasks: [],\n }),\n )\n}\n\n/**\n * Thread pool managing a set of worker threads.\n * Use it to queue tasks that are run on those threads with limited\n * concurrency.\n */\nexport interface Pool<ThreadType extends Thread> {\n /**\n * Returns a promise that resolves once the task queue is emptied.\n * Promise will be rejected if any task fails.\n *\n * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.\n */\n completed(allowResolvingImmediately?: boolean): Promise<any>\n\n /**\n * Returns a promise that resolves once the task queue is emptied.\n * Failing tasks will not cause the promise to be rejected.\n *\n * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.\n */\n settled(allowResolvingImmediately?: boolean): Promise<Error[]>\n\n /**\n * Returns an observable that yields pool events.\n */\n events(): Observable<PoolEvent<ThreadType>>\n\n /**\n * Queue a task and return a promise that resolves once the task has been dequeued,\n * started and finished.\n *\n * @param task An async function that takes a thread instance and invokes it.\n */\n queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>\n\n /**\n * Terminate all pool threads.\n *\n * @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.\n */\n terminate(force?: boolean): Promise<void>\n}\n\ninterface PoolOptions {\n /** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */\n concurrency?: number\n\n /** Maximum no. of jobs to be queued for execution before throwing an error. */\n maxQueuedJobs?: number\n\n /** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */\n name?: string\n\n /** No. of worker threads to spawn and to be managed by the pool. */\n size?: number\n}\n\nclass WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {\n static EventType = PoolEventType\n\n private readonly debug: DebugLogger.Debugger\n private readonly eventObservable: Observable<PoolEvent<ThreadType>>\n private readonly options: PoolOptions\n private readonly workers: Array<WorkerDescriptor<ThreadType>>\n\n private readonly eventSubject = new Subject<PoolEvent<ThreadType>>()\n private initErrors: Error[] = []\n private isClosing = false\n private nextTaskID = 1\n private taskQueue: Array<QueuedTask<ThreadType, any>> = []\n\n constructor(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) {\n const options: PoolOptions = typeof optionsOrSize === 'number' ? { size: optionsOrSize } : optionsOrSize || {}\n\n const { size = defaultPoolSize } = options\n\n this.debug = DebugLogger(`threads:pool:${slugify(options.name || String(nextPoolID++))}`)\n this.options = options\n this.workers = spawnWorkers(spawnWorker, size)\n\n this.eventObservable = multicast(Observable.from(this.eventSubject))\n\n Promise.all(this.workers.map(worker => worker.init)).then(\n () =>\n this.eventSubject.next({\n size: this.workers.length,\n type: PoolEventType.initialized,\n }),\n (error) => {\n this.debug('Error while initializing pool worker:', error)\n this.eventSubject.error(error)\n this.initErrors.push(error)\n },\n )\n }\n\n private findIdlingWorker(): WorkerDescriptor<ThreadType> | undefined {\n const { concurrency = 1 } = this.options\n return this.workers.find(worker => worker.runningTasks.length < concurrency)\n }\n\n private async runPoolTask(worker: WorkerDescriptor<ThreadType>, task: QueuedTask<ThreadType, any>) {\n const workerID = this.workers.indexOf(worker) + 1\n\n this.debug(`Running task #${task.id} on worker #${workerID}...`)\n this.eventSubject.next({\n taskID: task.id,\n type: PoolEventType.taskStart,\n workerID,\n })\n\n try {\n const returnValue = await task.run(await worker.init)\n\n this.debug(`Task #${task.id} completed successfully`)\n this.eventSubject.next({\n returnValue,\n taskID: task.id,\n type: PoolEventType.taskCompleted,\n workerID,\n })\n } catch (ex) {\n const error = ex as Error\n this.debug(`Task #${task.id} failed`)\n this.eventSubject.next({\n error,\n taskID: task.id,\n type: PoolEventType.taskFailed,\n workerID,\n })\n }\n }\n\n private run(worker: WorkerDescriptor<ThreadType>, task: QueuedTask<ThreadType, any>) {\n const runPromise = (async () => {\n const removeTaskFromWorkersRunningTasks = () => {\n worker.runningTasks = worker.runningTasks.filter(someRunPromise => someRunPromise !== runPromise)\n }\n\n // Defer task execution by one tick to give handlers time to subscribe\n await delay(0)\n\n try {\n await this.runPoolTask(worker, task)\n } finally {\n removeTaskFromWorkersRunningTasks()\n\n if (!this.isClosing) {\n this.scheduleWork()\n }\n }\n })()\n\n worker.runningTasks.push(runPromise)\n }\n\n private scheduleWork() {\n this.debug('Attempt de-queueing a task in order to run it...')\n\n const availableWorker = this.findIdlingWorker()\n if (!availableWorker) return\n\n const nextTask = this.taskQueue.shift()\n if (!nextTask) {\n this.debug('Task queue is empty')\n this.eventSubject.next({ type: PoolEventType.taskQueueDrained })\n return\n }\n\n this.run(availableWorker, nextTask)\n }\n\n private taskCompletion(taskID: number) {\n return new Promise<any>((resolve, reject) => {\n const eventSubscription = this.events().subscribe((event) => {\n if (event.type === PoolEventType.taskCompleted && event.taskID === taskID) {\n eventSubscription.unsubscribe()\n resolve(event.returnValue)\n } else if (event.type === PoolEventType.taskFailed && event.taskID === taskID) {\n eventSubscription.unsubscribe()\n reject(event.error)\n } else if (event.type === PoolEventType.terminated) {\n eventSubscription.unsubscribe()\n reject(new Error('Pool has been terminated before task was run.'))\n }\n })\n })\n }\n\n async settled(allowResolvingImmediately: boolean = false): Promise<Error[]> {\n const getCurrentlyRunningTasks = () => flatMap(this.workers, worker => worker.runningTasks)\n\n const taskFailures: Error[] = []\n\n const failureSubscription = this.eventObservable.subscribe((event) => {\n if (event.type === PoolEventType.taskFailed) {\n taskFailures.push(event.error)\n }\n })\n\n if (this.initErrors.length > 0) {\n throw this.initErrors[0]\n }\n if (allowResolvingImmediately && this.taskQueue.length === 0) {\n await Promise.allSettled(getCurrentlyRunningTasks())\n return taskFailures\n }\n\n await new Promise<void>((resolve, reject) => {\n const subscription = this.eventObservable.subscribe({\n error: reject,\n next(event) {\n if (event.type === PoolEventType.taskQueueDrained) {\n subscription.unsubscribe()\n resolve(void 0)\n }\n }, // make a pool-wide error reject the completed() result promise\n })\n })\n\n await Promise.allSettled(getCurrentlyRunningTasks())\n failureSubscription.unsubscribe()\n\n return taskFailures\n }\n\n async completed(allowResolvingImmediately: boolean = false) {\n const settlementPromise = this.settled(allowResolvingImmediately)\n\n const earlyExitPromise = new Promise<Error[]>((resolve, reject) => {\n const subscription = this.eventObservable.subscribe({\n error: reject,\n next(event) {\n if (event.type === PoolEventType.taskQueueDrained) {\n subscription.unsubscribe()\n resolve(settlementPromise)\n } else if (event.type === PoolEventType.taskFailed) {\n subscription.unsubscribe()\n reject(event.error)\n }\n }, // make a pool-wide error reject the completed() result promise\n })\n })\n\n const errors = await Promise.race([settlementPromise, earlyExitPromise])\n\n if (errors.length > 0) {\n throw errors[0]\n }\n }\n\n events() {\n return this.eventObservable\n }\n\n queue(taskFunction: TaskRunFunction<ThreadType, any>) {\n const { maxQueuedJobs = Number.POSITIVE_INFINITY } = this.options\n\n if (this.isClosing) {\n throw new Error('Cannot schedule pool tasks after terminate() has been called.')\n }\n if (this.initErrors.length > 0) {\n throw this.initErrors[0]\n }\n\n const taskID = this.nextTaskID++\n const taskCompletion = this.taskCompletion(taskID)\n\n taskCompletion.catch((error) => {\n // Prevent unhandled rejections here as we assume the user will use\n // `pool.completed()`, `pool.settled()` or `task.catch()` to handle errors\n this.debug(`Task #${taskID} errored:`, error)\n })\n\n const task: QueuedTask<ThreadType, any> = {\n cancel: () => {\n if (!this.taskQueue.includes(task)) return\n this.taskQueue = this.taskQueue.filter(someTask => someTask !== task)\n this.eventSubject.next({\n taskID: task.id,\n type: PoolEventType.taskCanceled,\n })\n },\n id: taskID,\n run: taskFunction,\n then: taskCompletion.then.bind(taskCompletion),\n }\n\n if (this.taskQueue.length >= maxQueuedJobs) {\n throw new Error(\n 'Maximum number of pool tasks queued. Refusing to queue another one.\\n'\n + 'This usually happens for one of two reasons: We are either at peak '\n + \"workload right now or some tasks just won't finish, thus blocking the pool.\",\n )\n }\n\n this.debug(`Queueing task #${task.id}...`)\n this.taskQueue.push(task)\n\n this.eventSubject.next({\n taskID: task.id,\n type: PoolEventType.taskQueued,\n })\n\n this.scheduleWork()\n return task\n }\n\n async terminate(force?: boolean) {\n this.isClosing = true\n if (!force) {\n await this.completed(true)\n }\n this.eventSubject.next({\n remainingQueue: [...this.taskQueue],\n type: PoolEventType.terminated,\n })\n this.eventSubject.complete()\n await Promise.all(this.workers.map(async worker => Thread.terminate(await worker.init)))\n }\n}\n\n/**\n * Thread pool constructor. Creates a new pool and spawns its worker threads.\n */\nfunction PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) {\n // The function exists only so we don't need to use `new` to create a pool (we still can, though).\n // If the Pool is a class or not is an implementation detail that should not concern the user.\n return new WorkerPool(spawnWorker, optionsOrSize)\n}\n\n;(PoolConstructor as any).EventType = PoolEventType\n\n/**\n * Thread pool constructor. Creates a new pool and spawns its worker threads.\n */\nexport const Pool = PoolConstructor as typeof PoolConstructor & { EventType: typeof PoolEventType }\n\nexport type { PoolEvent, QueuedTask } from './pool-types.ts'\nexport { PoolEventType } from './pool-types.ts'\nexport { Thread } from './thread.ts'\n","/* eslint-disable @stylistic/max-len */\n/* eslint-disable import-x/no-internal-modules */\n// tslint:disable max-classes-per-file\n\nimport type { ImplementationExport, ThreadsWorkerOptions } from '../types/master.ts'\nimport { getBundleURL } from './get-bundle-url.browser.ts'\n\n/** Default thread pool size based on available hardware concurrency, falling back to 4. */\nexport const defaultPoolSize = typeof navigator !== 'undefined' && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4\n\nconst isAbsoluteURL = (value: string) => /^[A-Za-z][\\d+.A-Za-z\\-]*:/.test(value)\n\nfunction createSourceBlobURL(code: string): string {\n const blob = new Blob([code], { type: 'application/javascript' })\n return URL.createObjectURL(blob)\n}\n\nfunction selectWorkerImplementation(): ImplementationExport {\n if (typeof Worker === 'undefined') {\n // Might happen on Safari, for instance\n // The idea is to only fail if the constructor is actually used\n return class NoWebWorker {\n constructor() {\n throw new Error(\n \"No web worker implementation available. You might have tried to spawn a worker within a worker in a browser that doesn't support workers in workers.\",\n )\n }\n } as unknown as ImplementationExport\n }\n\n class WebWorker extends Worker {\n constructor(url: string | URL, options?: ThreadsWorkerOptions) {\n if (typeof url === 'string' && options && options._baseURL) {\n url = new URL(url, options._baseURL)\n } else if (typeof url === 'string' && !isAbsoluteURL(url) && /^file:\\/\\//i.test(getBundleURL())) {\n url = new URL(url, getBundleURL().replace(/\\/[^/]+$/, '/'))\n if (options?.CORSWorkaround ?? true) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n }\n if (\n typeof url === 'string'\n && isAbsoluteURL(url) // Create source code blob loading JS file via `importScripts()`\n // to circumvent worker CORS restrictions\n && (options?.CORSWorkaround ?? true)\n ) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n super(url, options)\n }\n }\n\n class BlobWorker extends WebWorker {\n constructor(blob: Blob, options?: ThreadsWorkerOptions) {\n const url = globalThis.URL.createObjectURL(blob)\n super(url, options)\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WebWorker {\n const blob = new globalThis.Blob([source], { type: 'text/javascript' })\n return new BlobWorker(blob, options)\n }\n }\n\n return {\n blob: BlobWorker,\n default: WebWorker,\n }\n}\n\nlet implementation: ImplementationExport\n\n/**\n * Get the browser-specific worker implementation, lazily initializing it on first call.\n * @returns The platform-specific worker implementation export.\n */\nexport function getWorkerImplementation(): ImplementationExport {\n if (!implementation) {\n implementation = selectWorkerImplementation()\n }\n return implementation\n}\n\n/**\n * Check whether the current code is running inside a web worker context.\n * @returns True if running in a worker, false otherwise.\n */\nexport function isWorkerRuntime() {\n const isWindowContext = typeof globalThis !== 'undefined' && typeof Window !== 'undefined' && globalThis instanceof Window\n return typeof globalThis !== 'undefined' && self['postMessage'] && !isWindowContext ? true : false\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/member-ordering */\nimport type { Thread } from './thread.ts'\n\n/** Pool event type. Specifies the type of each `PoolEvent`. */\nexport enum PoolEventType {\n initialized = 'initialized',\n taskCanceled = 'taskCanceled',\n taskCompleted = 'taskCompleted',\n taskFailed = 'taskFailed',\n taskQueued = 'taskQueued',\n taskQueueDrained = 'taskQueueDrained',\n taskStart = 'taskStart',\n terminated = 'terminated',\n}\n\n/** A function that runs a task on a worker thread and returns a promise of the result. */\nexport type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>\n\n/** Pool event. Subscribe to those events using `pool.events()`. Useful for debugging. */\nexport type PoolEvent<ThreadType extends Thread> =\n | {\n type: PoolEventType.initialized\n size: number\n }\n | {\n type: PoolEventType.taskQueued\n taskID: number\n }\n | {\n type: PoolEventType.taskQueueDrained\n }\n | {\n type: PoolEventType.taskStart\n taskID: number\n workerID: number\n }\n | {\n type: PoolEventType.taskCompleted\n returnValue: any\n taskID: number\n workerID: number\n }\n | {\n type: PoolEventType.taskFailed\n error: Error\n taskID: number\n workerID: number\n }\n | {\n type: PoolEventType.taskCanceled\n taskID: number\n }\n | {\n type: PoolEventType.terminated\n remainingQueue: Array<QueuedTask<ThreadType, any>>\n }\n\n/** Descriptor for a worker in a pool, tracking its initialization and running tasks. */\nexport interface WorkerDescriptor<ThreadType extends Thread> {\n init: Promise<ThreadType>\n runningTasks: Array<Promise<any>>\n}\n\n/**\n * Task that has been `pool.queued()`-ed.\n */\nexport interface QueuedTask<ThreadType extends Thread, Return> {\n /** @private */\n id: number\n\n /** @private */\n run: TaskRunFunction<ThreadType, Return>\n\n /**\n * Queued tasks can be cancelled until the pool starts running them on a worker thread.\n */\n cancel(): void\n\n /**\n * `QueuedTask` is thenable, so you can `await` it.\n * Resolves when the task has successfully been executed. Rejects if the task fails.\n */\n then: Promise<Return>['then']\n}\n","/** Symbol key for accessing a thread's error observable. */\nexport const $errors = Symbol('thread.errors')\n/** Symbol key for accessing a thread's event observable. */\nexport const $events = Symbol('thread.events')\n/** Symbol key for accessing a thread's terminate function. */\nexport const $terminate = Symbol('thread.terminate')\n/** Symbol key for marking an object as a transferable descriptor. */\nexport const $transferable = Symbol('thread.transferable')\n/** Symbol key for accessing the underlying worker instance of a thread. */\nexport const $worker = Symbol('thread.worker')\n","/* eslint-disable import-x/no-internal-modules */\nimport type { Observable } from 'observable-fns'\n\nimport {\n $errors, $events, $terminate,\n} from '../symbols.ts'\nimport type { Thread as ThreadType, WorkerEvent } from '../types/master.ts'\n\nfunction fail(message: string): never {\n throw new Error(message)\n}\n\n/** Re-exported Thread type from the master types module. */\nexport type Thread = ThreadType\n\n/** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */\nexport const Thread = {\n /** Return an observable that can be used to subscribe to all errors happening in the thread. */\n errors<ThreadT extends ThreadType>(thread: ThreadT): Observable<Error> {\n return thread[$errors] || fail('Error observable not found. Make sure to pass a thread instance as returned by the spawn() promise.')\n },\n /** Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. */\n events<ThreadT extends ThreadType>(thread: ThreadT): Observable<WorkerEvent> {\n return thread[$events] || fail('Events observable not found. Make sure to pass a thread instance as returned by the spawn() promise.')\n },\n /** Terminate a thread. Remember to terminate every thread when you are done using it. */\n terminate<ThreadT extends ThreadType>(thread: ThreadT) {\n return thread[$terminate]()\n },\n}\n"],"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AAoC;AAAA,MACxC,QAAQ,MAAM;AACZ,YAAI,CAAC,KAAK,UAAU,SAAS,IAAI,EAAG;AACpC,aAAK,YAAY,KAAK,UAAU,OAAO,cAAY,aAAa,IAAI;AACpE,aAAK,aAAa,KAAK;AAAA,UACrB,QAAQ,KAAK;AAAA,UACb;AAAA,QACF,CAAC;AAAA,MACH;AAAA,MACA,IAAI;AAAA,MACJ,KAAK;AAAA,MACL,MAAM,eAAe,KAAK,KAAK,cAAc;AAAA,IAC/C;AAEA,QAAI,KAAK,UAAU,UAAU,eAAe;AAC1C,YAAM,IAAI;AAAA,QACR;AAAA,MAGF;AAAA,IACF;AAEA,SAAK,MAAM,kBAAkB,KAAK,EAAE,KAAK;AACzC,SAAK,UAAU,KAAK,IAAI;AAExB,SAAK,aAAa,KAAK;AAAA,MACrB,QAAQ,KAAK;AAAA,MACb;AAAA,IACF,CAAC;AAED,SAAK,aAAa;AAClB,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,UAAU,OAAiB;AAC/B,SAAK,YAAY;AACjB,QAAI,CAAC,OAAO;AACV,YAAM,KAAK,UAAU,IAAI;AAAA,IAC3B;AACA,SAAK,aAAa,KAAK;AAAA,MACrB,gBAAgB,CAAC,GAAG,KAAK,SAAS;AAAA,MAClC;AAAA,IACF,CAAC;AACD,SAAK,aAAa,SAAS;AAC3B,UAAM,QAAQ,IAAI,KAAK,QAAQ,IAAI,OAAM,WAAU,OAAO,UAAU,MAAM,OAAO,IAAI,CAAC,CAAC;AAAA,EACzF;AACF;AAKA,SAAS,gBAA2C,aAAwC,eAAsC;AAGhI,SAAO,IAAI,WAAW,aAAa,aAAa;AAClD;AAEE,gBAAwB,YAAY;AAK/B,IAAM,OAAO;","names":["PoolEventType"]}
1
+ {"version":3,"sources":["../../../src/master/pool-browser.ts","../../../src/master/implementation.browser.ts","../../../src/master/pool-types.ts","../../../src/symbols.ts","../../../src/master/thread.ts"],"sourcesContent":["/* eslint-disable import-x/export */\n/* eslint-disable unicorn/no-thenable */\n\n/* eslint-disable @typescript-eslint/member-ordering */\n/* eslint-disable unicorn/no-array-reduce */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-namespace */\n\n/// <reference lib=\"esnext\" />\n\nimport DebugLogger from 'debug'\nimport {\n multicast, Observable, Subject,\n} from 'observable-fns'\n\nimport { defaultPoolSize } from './implementation.browser.ts'\nimport type {\n PoolEvent, QueuedTask, TaskRunFunction, WorkerDescriptor,\n} from './pool-types.ts'\nimport { PoolEventType } from './pool-types.ts'\nimport { Thread } from './thread.ts'\n\nexport declare namespace Pool {\n type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>\n type EventType = PoolEventType\n}\n\nlet nextPoolID = 1\n\nfunction createArray(size: number): number[] {\n const array: number[] = []\n for (let index = 0; index < size; index++) {\n array.push(index)\n }\n return array\n}\n\nfunction delay(ms: number) {\n return new Promise(resolve => setTimeout(resolve, ms))\n}\n\nfunction flatMap<In, Out>(array: In[], mapper: (element: In) => Out[]): Out[] {\n return array.reduce<Out[]>((flattened, element) => [...flattened, ...mapper(element)], [])\n}\n\nfunction slugify(text: string) {\n return text.replaceAll(/\\W/g, ' ').trim().replaceAll(/\\s+/g, '-')\n}\n\nfunction spawnWorkers<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, count: number): WorkerDescriptor<ThreadType>[] {\n return createArray(count).map(\n (): WorkerDescriptor<ThreadType> => ({\n init: spawnWorker(),\n runningTasks: [],\n }),\n )\n}\n\n/**\n * Thread pool managing a set of worker threads.\n * Use it to queue tasks that are run on those threads with limited\n * concurrency.\n */\nexport interface Pool<ThreadType extends Thread> {\n /**\n * Returns a promise that resolves once the task queue is emptied.\n * Promise will be rejected if any task fails.\n *\n * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.\n */\n completed(allowResolvingImmediately?: boolean): Promise<any>\n\n /**\n * Returns a promise that resolves once the task queue is emptied.\n * Failing tasks will not cause the promise to be rejected.\n *\n * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.\n */\n settled(allowResolvingImmediately?: boolean): Promise<Error[]>\n\n /**\n * Returns an observable that yields pool events.\n */\n events(): Observable<PoolEvent<ThreadType>>\n\n /**\n * Queue a task and return a promise that resolves once the task has been dequeued,\n * started and finished.\n *\n * @param task An async function that takes a thread instance and invokes it.\n */\n queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>\n\n /**\n * Terminate all pool threads.\n *\n * @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.\n */\n terminate(force?: boolean): Promise<void>\n}\n\ninterface PoolOptions {\n /** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */\n concurrency?: number\n\n /** Maximum no. of jobs to be queued for execution before throwing an error. */\n maxQueuedJobs?: number\n\n /** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */\n name?: string\n\n /** No. of worker threads to spawn and to be managed by the pool. */\n size?: number\n}\n\nclass WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {\n static EventType = PoolEventType\n\n private readonly debug: DebugLogger.Debugger\n private readonly eventObservable: Observable<PoolEvent<ThreadType>>\n private readonly options: PoolOptions\n private readonly workers: WorkerDescriptor<ThreadType>[]\n\n private readonly eventSubject = new Subject<PoolEvent<ThreadType>>()\n private initErrors: Error[] = []\n private isClosing = false\n private nextTaskID = 1\n private taskQueue: QueuedTask<ThreadType, any>[] = []\n\n constructor(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) {\n const options: PoolOptions = typeof optionsOrSize === 'number' ? { size: optionsOrSize } : optionsOrSize || {}\n\n const { size = defaultPoolSize } = options\n\n this.debug = DebugLogger(`threads:pool:${slugify(options.name ?? String(nextPoolID++))}`)\n this.options = options\n this.workers = spawnWorkers(spawnWorker, size)\n\n this.eventObservable = multicast(Observable.from(this.eventSubject))\n\n Promise.all(this.workers.map(worker => worker.init)).then(\n () =>\n this.eventSubject.next({\n size: this.workers.length,\n type: PoolEventType.initialized,\n }),\n (error) => {\n this.debug('Error while initializing pool worker:', error)\n this.eventSubject.error(error)\n this.initErrors.push(error)\n },\n )\n }\n\n private findIdlingWorker(): WorkerDescriptor<ThreadType> | undefined {\n const { concurrency = 1 } = this.options\n return this.workers.find(worker => worker.runningTasks.length < concurrency)\n }\n\n private async runPoolTask(worker: WorkerDescriptor<ThreadType>, task: QueuedTask<ThreadType, any>) {\n const workerID = this.workers.indexOf(worker) + 1\n\n this.debug(`Running task #${task.id} on worker #${workerID}...`)\n this.eventSubject.next({\n taskID: task.id,\n type: PoolEventType.taskStart,\n workerID,\n })\n\n try {\n const returnValue = await task.run(await worker.init)\n\n this.debug(`Task #${task.id} completed successfully`)\n this.eventSubject.next({\n returnValue,\n taskID: task.id,\n type: PoolEventType.taskCompleted,\n workerID,\n })\n } catch (ex) {\n const error = ex as Error\n this.debug(`Task #${task.id} failed`)\n this.eventSubject.next({\n error,\n taskID: task.id,\n type: PoolEventType.taskFailed,\n workerID,\n })\n }\n }\n\n private run(worker: WorkerDescriptor<ThreadType>, task: QueuedTask<ThreadType, any>) {\n const runPromise = (async () => {\n const removeTaskFromWorkersRunningTasks = () => {\n worker.runningTasks = worker.runningTasks.filter(someRunPromise => someRunPromise !== runPromise)\n }\n\n // Defer task execution by one tick to give handlers time to subscribe\n await delay(0)\n\n try {\n await this.runPoolTask(worker, task)\n } finally {\n removeTaskFromWorkersRunningTasks()\n\n if (!this.isClosing) {\n this.scheduleWork()\n }\n }\n })()\n\n worker.runningTasks.push(runPromise)\n }\n\n private scheduleWork() {\n this.debug('Attempt de-queueing a task in order to run it...')\n\n const availableWorker = this.findIdlingWorker()\n if (!availableWorker) return\n\n const nextTask = this.taskQueue.shift()\n if (!nextTask) {\n this.debug('Task queue is empty')\n this.eventSubject.next({ type: PoolEventType.taskQueueDrained })\n return\n }\n\n this.run(availableWorker, nextTask)\n }\n\n private taskCompletion(taskID: number) {\n return new Promise<any>((resolve, reject) => {\n const eventSubscription = this.events().subscribe((event) => {\n if (event.type === PoolEventType.taskCompleted && event.taskID === taskID) {\n eventSubscription.unsubscribe()\n resolve(event.returnValue)\n } else if (event.type === PoolEventType.taskFailed && event.taskID === taskID) {\n eventSubscription.unsubscribe()\n reject(event.error)\n } else if (event.type === PoolEventType.terminated) {\n eventSubscription.unsubscribe()\n reject(new Error('Pool has been terminated before task was run.'))\n }\n })\n })\n }\n\n async settled(allowResolvingImmediately = false): Promise<Error[]> {\n const getCurrentlyRunningTasks = () => flatMap(this.workers, worker => worker.runningTasks)\n\n const taskFailures: Error[] = []\n\n const failureSubscription = this.eventObservable.subscribe((event) => {\n if (event.type === PoolEventType.taskFailed) {\n taskFailures.push(event.error)\n }\n })\n\n if (this.initErrors.length > 0) {\n throw this.initErrors[0]\n }\n if (allowResolvingImmediately && this.taskQueue.length === 0) {\n await Promise.allSettled(getCurrentlyRunningTasks())\n return taskFailures\n }\n\n await new Promise<void>((resolve, reject) => {\n const subscription = this.eventObservable.subscribe({\n error: reject,\n next(event) {\n if (event.type === PoolEventType.taskQueueDrained) {\n subscription.unsubscribe()\n resolve(void 0)\n }\n }, // make a pool-wide error reject the completed() result promise\n })\n })\n\n await Promise.allSettled(getCurrentlyRunningTasks())\n failureSubscription.unsubscribe()\n\n return taskFailures\n }\n\n async completed(allowResolvingImmediately = false) {\n const settlementPromise = this.settled(allowResolvingImmediately)\n\n const earlyExitPromise = new Promise<Error[]>((resolve, reject) => {\n const subscription = this.eventObservable.subscribe({\n error: reject,\n next(event) {\n if (event.type === PoolEventType.taskQueueDrained) {\n subscription.unsubscribe()\n resolve(settlementPromise)\n } else if (event.type === PoolEventType.taskFailed) {\n subscription.unsubscribe()\n reject(event.error)\n }\n }, // make a pool-wide error reject the completed() result promise\n })\n })\n\n const errors = await Promise.race([settlementPromise, earlyExitPromise])\n\n if (errors.length > 0) {\n throw errors[0]\n }\n }\n\n events() {\n return this.eventObservable\n }\n\n queue(taskFunction: TaskRunFunction<ThreadType, any>) {\n const { maxQueuedJobs = Number.POSITIVE_INFINITY } = this.options\n\n if (this.isClosing) {\n throw new Error('Cannot schedule pool tasks after terminate() has been called.')\n }\n if (this.initErrors.length > 0) {\n throw this.initErrors[0]\n }\n\n const taskID = this.nextTaskID++\n const taskCompletion = this.taskCompletion(taskID)\n\n taskCompletion.catch((error) => {\n // Prevent unhandled rejections here as we assume the user will use\n // `pool.completed()`, `pool.settled()` or `task.catch()` to handle errors\n this.debug(`Task #${taskID} errored:`, error)\n })\n\n const task: QueuedTask<ThreadType, any> = {\n cancel: () => {\n if (!this.taskQueue.includes(task)) return\n this.taskQueue = this.taskQueue.filter(someTask => someTask !== task)\n this.eventSubject.next({\n taskID: task.id,\n type: PoolEventType.taskCanceled,\n })\n },\n id: taskID,\n run: taskFunction,\n then: taskCompletion.then.bind(taskCompletion),\n }\n\n if (this.taskQueue.length >= maxQueuedJobs) {\n throw new Error(\n 'Maximum number of pool tasks queued. Refusing to queue another one.\\n'\n + 'This usually happens for one of two reasons: We are either at peak '\n + \"workload right now or some tasks just won't finish, thus blocking the pool.\",\n )\n }\n\n this.debug(`Queueing task #${task.id}...`)\n this.taskQueue.push(task)\n\n this.eventSubject.next({\n taskID: task.id,\n type: PoolEventType.taskQueued,\n })\n\n this.scheduleWork()\n return task\n }\n\n async terminate(force?: boolean) {\n this.isClosing = true\n if (!force) {\n await this.completed(true)\n }\n this.eventSubject.next({\n remainingQueue: [...this.taskQueue],\n type: PoolEventType.terminated,\n })\n this.eventSubject.complete()\n await Promise.all(this.workers.map(async worker => Thread.terminate(await worker.init)))\n }\n}\n\n/**\n * Thread pool constructor. Creates a new pool and spawns its worker threads.\n */\nfunction PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) {\n // The function exists only so we don't need to use `new` to create a pool (we still can, though).\n // If the Pool is a class or not is an implementation detail that should not concern the user.\n return new WorkerPool(spawnWorker, optionsOrSize)\n}\n\n;(PoolConstructor as any).EventType = PoolEventType\n\n/**\n * Thread pool constructor. Creates a new pool and spawns its worker threads.\n */\nexport const Pool = PoolConstructor as typeof PoolConstructor & { EventType: typeof PoolEventType }\n\nexport type { PoolEvent, QueuedTask } from './pool-types.ts'\nexport { PoolEventType } from './pool-types.ts'\nexport { Thread } from './thread.ts'\n","/* eslint-disable import-x/no-internal-modules */\n\nimport type { ImplementationExport, ThreadsWorkerOptions } from '../types/master.ts'\nimport { getBundleURL } from './get-bundle-url.browser.ts'\n\n/** Default thread pool size based on available hardware concurrency, falling back to 4. */\nexport const defaultPoolSize = typeof navigator !== 'undefined' && navigator.hardwareConcurrency !== 0 ? navigator.hardwareConcurrency : 4\n\nconst isAbsoluteURL = (value: string) => /^[A-Za-z][\\d+.A-Za-z\\-]*:/.test(value)\n\nfunction createSourceBlobURL(code: string): string {\n const blob = new Blob([code], { type: 'application/javascript' })\n return URL.createObjectURL(blob)\n}\n\nfunction selectWorkerImplementation(): ImplementationExport {\n if (typeof Worker === 'undefined') {\n // Might happen on Safari, for instance\n // The idea is to only fail if the constructor is actually used\n return class NoWebWorker {\n constructor() {\n throw new Error(\n \"No web worker implementation available. You might have tried to spawn a worker within a worker in a browser that doesn't support workers in workers.\",\n )\n }\n } as unknown as ImplementationExport\n }\n\n class WebWorker extends Worker {\n constructor(url: string | URL, options?: ThreadsWorkerOptions) {\n if (typeof url === 'string' && options?._baseURL !== undefined && options._baseURL !== '') {\n url = new URL(url, options._baseURL)\n } else if (typeof url === 'string' && !isAbsoluteURL(url) && /^file:\\/\\//i.test(getBundleURL())) {\n url = new URL(url, getBundleURL().replace(/\\/[^/]+$/, '/'))\n if (options?.CORSWorkaround ?? true) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n }\n if (\n typeof url === 'string'\n && isAbsoluteURL(url) // Create source code blob loading JS file via `importScripts()`\n // to circumvent worker CORS restrictions\n && (options?.CORSWorkaround ?? true)\n ) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n super(url, options)\n }\n }\n\n class BlobWorker extends WebWorker {\n constructor(blob: Blob, options?: ThreadsWorkerOptions) {\n const url = globalThis.URL.createObjectURL(blob)\n super(url, options)\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WebWorker {\n const blob = new globalThis.Blob([source], { type: 'text/javascript' })\n return new BlobWorker(blob, options)\n }\n }\n\n return {\n blob: BlobWorker,\n default: WebWorker,\n }\n}\n\nlet implementation: ImplementationExport\n\n/**\n * Get the browser-specific worker implementation, lazily initializing it on first call.\n * @returns The platform-specific worker implementation export.\n */\nexport function getWorkerImplementation(): ImplementationExport {\n if (implementation === undefined) {\n implementation = selectWorkerImplementation()\n }\n return implementation\n}\n\n/**\n * Check whether the current code is running inside a web worker context.\n * @returns True if running in a worker, false otherwise.\n */\nexport function isWorkerRuntime() {\n const isWindowContext = typeof globalThis !== 'undefined' && typeof Window !== 'undefined' && globalThis instanceof Window\n return typeof globalThis !== 'undefined' && self?.postMessage !== undefined && !isWindowContext\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/member-ordering */\nimport type { Thread } from './thread.ts'\n\n/** Pool event type. Specifies the type of each `PoolEvent`. */\nexport enum PoolEventType {\n initialized = 'initialized',\n taskCanceled = 'taskCanceled',\n taskCompleted = 'taskCompleted',\n taskFailed = 'taskFailed',\n taskQueued = 'taskQueued',\n taskQueueDrained = 'taskQueueDrained',\n taskStart = 'taskStart',\n terminated = 'terminated',\n}\n\n/** A function that runs a task on a worker thread and returns a promise of the result. */\nexport type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>\n\n/** Pool event. Subscribe to those events using `pool.events()`. Useful for debugging. */\nexport type PoolEvent<ThreadType extends Thread>\n = | {\n type: PoolEventType.initialized\n size: number\n }\n | {\n type: PoolEventType.taskQueued\n taskID: number\n }\n | {\n type: PoolEventType.taskQueueDrained\n }\n | {\n type: PoolEventType.taskStart\n taskID: number\n workerID: number\n }\n | {\n type: PoolEventType.taskCompleted\n returnValue: any\n taskID: number\n workerID: number\n }\n | {\n type: PoolEventType.taskFailed\n error: Error\n taskID: number\n workerID: number\n }\n | {\n type: PoolEventType.taskCanceled\n taskID: number\n }\n | {\n type: PoolEventType.terminated\n remainingQueue: QueuedTask<ThreadType, any>[]\n }\n\n/** Descriptor for a worker in a pool, tracking its initialization and running tasks. */\nexport interface WorkerDescriptor<ThreadType extends Thread> {\n init: Promise<ThreadType>\n runningTasks: Promise<any>[]\n}\n\n/**\n * Task that has been `pool.queued()`-ed.\n */\nexport interface QueuedTask<ThreadType extends Thread, Return> {\n /** @private */\n id: number\n\n /** @private */\n run: TaskRunFunction<ThreadType, Return>\n\n /**\n * Queued tasks can be cancelled until the pool starts running them on a worker thread.\n */\n cancel(): void\n\n /**\n * `QueuedTask` is thenable, so you can `await` it.\n * Resolves when the task has successfully been executed. Rejects if the task fails.\n */\n then: Promise<Return>['then']\n}\n","/** Symbol key for accessing a thread's error observable. */\nexport const $errors = Symbol('thread.errors')\n/** Symbol key for accessing a thread's event observable. */\nexport const $events = Symbol('thread.events')\n/** Symbol key for accessing a thread's terminate function. */\nexport const $terminate = Symbol('thread.terminate')\n/** Symbol key for marking an object as a transferable descriptor. */\nexport const $transferable = Symbol('thread.transferable')\n/** Symbol key for accessing the underlying worker instance of a thread. */\nexport const $worker = Symbol('thread.worker')\n","/* eslint-disable import-x/no-internal-modules */\nimport type { Observable } from 'observable-fns'\n\nimport {\n $errors, $events, $terminate,\n} from '../symbols.ts'\nimport type { Thread as ThreadType, WorkerEvent } from '../types/master.ts'\n\nfunction fail(message: string): never {\n throw new Error(message)\n}\n\n/** Re-exported Thread type from the master types module. */\nexport type Thread = ThreadType\n\n/** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */\nexport const Thread = {\n /** Return an observable that can be used to subscribe to all errors happening in the thread. */\n errors<ThreadT extends ThreadType>(thread: ThreadT): Observable<Error> {\n return thread[$errors] ?? fail('Error observable not found. Make sure to pass a thread instance as returned by the spawn() promise.')\n },\n /** Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. */\n events<ThreadT extends ThreadType>(thread: ThreadT): Observable<WorkerEvent> {\n return thread[$events] ?? fail('Events observable not found. Make sure to pass a thread instance as returned by the spawn() promise.')\n },\n /** Terminate a thread. Remember to terminate every thread when you are done using it. */\n terminate<ThreadT extends ThreadType>(thread: ThreadT) {\n return thread[$terminate]()\n },\n}\n"],"mappings":";AAUA,OAAO,iBAAiB;AACxB;AAAA,EACE;AAAA,EAAW;AAAA,EAAY;AAAA,OAClB;;;ACPA,IAAM,kBAAkB,OAAO,cAAc,eAAe,UAAU,wBAAwB,IAAI,UAAU,sBAAsB;;;ACDlI,IAAK,gBAAL,kBAAKA,mBAAL;AACL,EAAAA,eAAA,iBAAc;AACd,EAAAA,eAAA,kBAAe;AACf,EAAAA,eAAA,mBAAgB;AAChB,EAAAA,eAAA,gBAAa;AACb,EAAAA,eAAA,gBAAa;AACb,EAAAA,eAAA,sBAAmB;AACnB,EAAAA,eAAA,eAAY;AACZ,EAAAA,eAAA,gBAAa;AARH,SAAAA;AAAA,GAAA;;;ACJL,IAAM,UAAU,uBAAO,eAAe;AAEtC,IAAM,UAAU,uBAAO,eAAe;AAEtC,IAAM,aAAa,uBAAO,kBAAkB;;;ACGnD,SAAS,KAAK,SAAwB;AACpC,QAAM,IAAI,MAAM,OAAO;AACzB;AAMO,IAAM,SAAS;AAAA;AAAA,EAEpB,OAAmC,QAAoC;AACrE,WAAO,OAAO,OAAO,KAAK,KAAK,qGAAqG;AAAA,EACtI;AAAA;AAAA,EAEA,OAAmC,QAA0C;AAC3E,WAAO,OAAO,OAAO,KAAK,KAAK,sGAAsG;AAAA,EACvI;AAAA;AAAA,EAEA,UAAsC,QAAiB;AACrD,WAAO,OAAO,UAAU,EAAE;AAAA,EAC5B;AACF;;;AJFA,IAAI,aAAa;AAEjB,SAAS,YAAY,MAAwB;AAC3C,QAAM,QAAkB,CAAC;AACzB,WAAS,QAAQ,GAAG,QAAQ,MAAM,SA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hI,SAAO,IAAI,WAAW,aAAa,aAAa;AAClD;AAEE,gBAAwB,YAAY;AAK/B,IAAM,OAAO;","names":["PoolEventType"]}
@@ -53,9 +53,7 @@ interface ObservableLike<T> {
53
53
  type StripAsync<Type> = Type extends Promise<infer PromiseBaseType> ? PromiseBaseType : Type extends ObservableLike<infer ObservableBaseType> ? ObservableBaseType : Type;
54
54
  type StripTransfer<Type> = Type extends TransferDescriptor<infer BaseType> ? BaseType : Type;
55
55
  /** An object whose keys are method names and values are functions, representing a worker module's methods. */
56
- type ModuleMethods = {
57
- [methodName: string]: (...args: any) => any;
58
- };
56
+ type ModuleMethods = Record<string, (...args: any) => any>;
59
57
  type ProxyableArgs<Args extends any[]> = Args extends [arg0: infer Arg0, ...rest: infer RestArgs] ? [Arg0 extends Transferable ? Arg0 | TransferDescriptor<Arg0> : Arg0, ...RestArgs] : Args;
60
58
  /** A function proxied from a worker thread, returning an `ObservablePromise` of the unwrapped result. */
61
59
  type ProxyableFunction<Args extends any[], ReturnType> = Args extends [] ? () => ObservablePromise<StripTransfer<StripAsync<ReturnType>>> : (...args: ProxyableArgs<Args>) => ObservablePromise<StripTransfer<StripAsync<ReturnType>>>;
@@ -1,4 +1,4 @@
1
- import { W as WorkerFunction, a as WorkerModule, A as AbstractedWorkerAPI } from '../worker-DW4hSCsH.js';
1
+ import { W as WorkerFunction, a as WorkerModule, A as AbstractedWorkerAPI } from '../worker-Bk-ckoOM.js';
2
2
  export { r as registerSerializer } from '../common-C73XWm6T.js';
3
3
  export { T as Transfer } from '../transferable-sfmQXy9d.js';
4
4
 
@@ -11,7 +11,7 @@ declare const subscribeToMasterMessages: AbstractedWorkerAPI['subscribeToMasterM
11
11
  /** Bound `addEventListener` from the worker global scope. */
12
12
  declare const addEventListener: (eventName: string, listener: (event: Event) => void) => void;
13
13
  /** Bound `postMessage` from the worker global scope. */
14
- declare const postMessage: (message: unknown, transferList?: Readonly<Transferable[]>) => void;
14
+ declare const postMessage: (message: unknown, transferList?: readonly Transferable[]) => void;
15
15
  /** Bound `removeEventListener` from the worker global scope. */
16
16
  declare const removeEventListener: (eventName: string, listener: (event: Event) => void) => void;
17
17
 
@@ -77,12 +77,12 @@ function Transfer(payload, transferables) {
77
77
  }
78
78
 
79
79
  // src/worker/expose.ts
80
- var isErrorEvent = (value) => value && value.error;
80
+ var isErrorEvent = (value) => value !== void 0 && value.error !== void 0;
81
81
  function createExpose(implementation, self2) {
82
82
  let exposeCalled = false;
83
83
  const activeSubscriptions = /* @__PURE__ */ new Map();
84
- const isMasterJobCancelMessage = (thing) => thing && thing.type === "cancel" /* cancel */;
85
- const isMasterJobRunMessage = (thing) => thing && thing.type === "run" /* run */;
84
+ const isMasterJobCancelMessage = (thing) => thing?.type === "cancel" /* cancel */;
85
+ const isMasterJobRunMessage = (thing) => thing?.type === "run" /* run */;
86
86
  const isObservable = (thing) => isSomeObservable(thing) || isZenObservable(thing);
87
87
  function isZenObservable(thing) {
88
88
  return thing && typeof thing === "object" && typeof thing.subscribe === "function";
@@ -192,14 +192,14 @@ function createExpose(implementation, self2) {
192
192
  exposeCalled = true;
193
193
  if (typeof exposed === "function") {
194
194
  implementation.subscribeToMasterMessages((messageData) => {
195
- if (isMasterJobRunMessage(messageData) && !messageData.method) {
195
+ if (isMasterJobRunMessage(messageData) && messageData.method === void 0) {
196
196
  runFunction(messageData.uid, exposed, messageData.args.map(deserialize));
197
197
  }
198
198
  });
199
199
  postFunctionInitMessage();
200
- } else if (typeof exposed === "object" && exposed) {
200
+ } else if (typeof exposed === "object" && exposed !== null) {
201
201
  implementation.subscribeToMasterMessages((messageData) => {
202
- if (isMasterJobRunMessage(messageData) && messageData.method) {
202
+ if (isMasterJobRunMessage(messageData) && messageData.method !== void 0) {
203
203
  runFunction(messageData.uid, exposed[messageData.method], messageData.args.map(deserialize));
204
204
  }
205
205
  });
@@ -246,7 +246,7 @@ function createExpose(implementation, self2) {
246
246
  // src/worker/worker.browser.ts
247
247
  var isWorkerRuntime = function isWorkerRuntime2() {
248
248
  const isWindowContext = self !== void 0 && typeof Window !== "undefined" && self instanceof Window;
249
- return self !== void 0 && self["postMessage"] && !isWindowContext ? true : false;
249
+ return self?.postMessage !== void 0 && !isWindowContext;
250
250
  };
251
251
  var postMessageToMaster = function postMessageToMaster2(data, transferList) {
252
252
  self.postMessage(data, transferList);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/worker/expose.ts","../../../src/serializers.ts","../../../src/common.ts","../../../src/symbols.ts","../../../src/transferable.ts","../../../src/worker/worker.browser.ts"],"sourcesContent":["/* eslint-disable import-x/no-internal-modules */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-floating-promises */\n\nimport isSomeObservable from 'is-observable-2-1-0'\nimport type { Observable, Subscription } from 'observable-fns'\n\nimport { deserialize, serialize } from '../common.ts'\nimport type { TransferDescriptor } from '../transferable.ts'\nimport { isTransferDescriptor } from '../transferable.ts'\nimport type {\n MasterJobCancelMessage,\n MasterJobRunMessage,\n SerializedError,\n WorkerInitMessage,\n WorkerJobErrorMessage,\n WorkerJobResultMessage,\n WorkerJobStartMessage,\n WorkerUncaughtErrorMessage,\n} from '../types/messages.ts'\nimport {\n MasterMessageType,\n WorkerMessageType,\n} from '../types/messages.ts'\nimport type {\n AbstractedWorkerAPI, WorkerFunction, WorkerModule,\n} from '../types/worker.ts'\nimport type { WorkerGlobalScope } from './WorkerGlobalScope.ts'\n\nconst isErrorEvent = (value: Event): value is ErrorEvent => value && (value as ErrorEvent).error\n\n/**\n * Create an `expose()` function bound to a specific worker API implementation and global scope.\n * @param implementation - The abstracted worker API for communicating with the master thread.\n * @param self - The worker's global scope for subscribing to error events.\n * @returns The `expose()` function that workers call to register their API.\n */\nexport function createExpose(implementation: AbstractedWorkerAPI, self: WorkerGlobalScope) {\n let exposeCalled = false\n\n const activeSubscriptions = new Map<number, Subscription<any>>()\n\n const isMasterJobCancelMessage = (thing: any): thing is MasterJobCancelMessage => thing && thing.type === MasterMessageType.cancel\n const isMasterJobRunMessage = (thing: any): thing is MasterJobRunMessage => thing && thing.type === MasterMessageType.run\n\n /**\n * There are issues with `is-observable` not recognizing zen-observable's instances.\n * We are using `observable-fns`, but it's based on zen-observable, too.\n */\n const isObservable = (thing: any): thing is Observable<any> => isSomeObservable(thing) || isZenObservable(thing)\n\n function isZenObservable(thing: any): thing is Observable<any> {\n return thing && typeof thing === 'object' && typeof thing.subscribe === 'function'\n }\n\n function deconstructTransfer(thing: any) {\n return isTransferDescriptor(thing) ? { payload: thing.send, transferables: thing.transferables } : { payload: thing, transferables: undefined }\n }\n\n function postFunctionInitMessage() {\n const initMessage: WorkerInitMessage = {\n exposed: { type: 'function' },\n type: WorkerMessageType.init,\n }\n implementation.postMessageToMaster(initMessage)\n }\n\n function postModuleInitMessage(methodNames: string[]) {\n const initMessage: WorkerInitMessage = {\n exposed: {\n methods: methodNames,\n type: 'module',\n },\n type: WorkerMessageType.init,\n }\n implementation.postMessageToMaster(initMessage)\n }\n\n function postJobErrorMessage(uid: number, rawError: Error | TransferDescriptor<Error>) {\n const { payload: error, transferables } = deconstructTransfer(rawError)\n const errorMessage: WorkerJobErrorMessage = {\n error: serialize(error) as any as SerializedError,\n type: WorkerMessageType.error,\n uid,\n }\n implementation.postMessageToMaster(errorMessage, transferables)\n }\n\n function postJobResultMessage(uid: number, completed: boolean, resultValue?: any) {\n const { payload, transferables } = deconstructTransfer(resultValue)\n const resultMessage: WorkerJobResultMessage = {\n complete: completed ? true : undefined,\n payload,\n type: WorkerMessageType.result,\n uid,\n }\n implementation.postMessageToMaster(resultMessage, transferables)\n }\n\n function postJobStartMessage(uid: number, resultType: WorkerJobStartMessage['resultType']) {\n const startMessage: WorkerJobStartMessage = {\n resultType,\n type: WorkerMessageType.running,\n uid,\n }\n implementation.postMessageToMaster(startMessage)\n }\n\n function postUncaughtErrorMessage(error: Error) {\n try {\n const errorMessage: WorkerUncaughtErrorMessage = {\n error: serialize(error) as any as SerializedError,\n type: WorkerMessageType.uncaughtError,\n }\n implementation.postMessageToMaster(errorMessage)\n } catch (subError) {\n // tslint:disable-next-line no-console\n console.error(\n 'Not reporting uncaught error back to master thread as it ' + 'occured while reporting an uncaught error already.' + '\\nLatest error:',\n subError,\n '\\nOriginal error:',\n error,\n )\n }\n }\n\n async function runFunction(jobUID: number, fn: WorkerFunction, args: any[]) {\n let syncResult: any\n\n try {\n syncResult = fn(...args)\n } catch (ex) {\n const error = ex as Error\n return postJobErrorMessage(jobUID, error)\n }\n\n const resultType = isObservable(syncResult) ? 'observable' : 'promise'\n postJobStartMessage(jobUID, resultType)\n\n if (isObservable(syncResult)) {\n const subscription = syncResult.subscribe(\n value => postJobResultMessage(jobUID, false, serialize(value)),\n (error) => {\n postJobErrorMessage(jobUID, serialize(error) as any)\n activeSubscriptions.delete(jobUID)\n },\n () => {\n postJobResultMessage(jobUID, true)\n activeSubscriptions.delete(jobUID)\n },\n )\n activeSubscriptions.set(jobUID, subscription)\n } else {\n try {\n const result = await syncResult\n postJobResultMessage(jobUID, true, serialize(result))\n } catch (error) {\n postJobErrorMessage(jobUID, serialize(error) as any)\n }\n }\n }\n\n /**\n * Expose a function or a module (an object whose values are functions)\n * to the main thread. Must be called exactly once in every worker thread\n * to signal its API to the main thread.\n *\n * @param exposed Function or object whose values are functions\n */\n const expose = (exposed: WorkerFunction | WorkerModule<any>) => {\n if (!implementation.isWorkerRuntime()) {\n throw new Error('expose() called in the master thread.')\n }\n if (exposeCalled) {\n throw new Error('expose() called more than once. This is not possible. Pass an object to expose() if you want to expose multiple functions.')\n }\n exposeCalled = true\n\n if (typeof exposed === 'function') {\n implementation.subscribeToMasterMessages((messageData: unknown) => {\n if (isMasterJobRunMessage(messageData) && !messageData.method) {\n runFunction(messageData.uid, exposed, messageData.args.map(deserialize))\n }\n })\n postFunctionInitMessage()\n } else if (typeof exposed === 'object' && exposed) {\n implementation.subscribeToMasterMessages((messageData: unknown) => {\n if (isMasterJobRunMessage(messageData) && messageData.method) {\n runFunction(messageData.uid, exposed[messageData.method], messageData.args.map(deserialize))\n }\n })\n\n const methodNames = Object.keys(exposed).filter(key => typeof exposed[key] === 'function')\n postModuleInitMessage(methodNames)\n } else {\n throw new Error(`Invalid argument passed to expose(). Expected a function or an object, got: ${exposed}`)\n }\n\n implementation.subscribeToMasterMessages((messageData: unknown) => {\n if (isMasterJobCancelMessage(messageData)) {\n const jobUID = messageData.uid\n const subscription = activeSubscriptions.get(jobUID)\n\n if (subscription) {\n subscription.unsubscribe()\n activeSubscriptions.delete(jobUID)\n }\n }\n })\n }\n\n if (typeof globalThis !== 'undefined' && typeof self.addEventListener === 'function' && implementation.isWorkerRuntime()) {\n self.addEventListener('error', (event) => {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(isErrorEvent(event) ? event.error : event), 250)\n })\n self.addEventListener('unhandledrejection', (event) => {\n const error = (event as any).reason\n if (error && typeof (error as any).message === 'string') {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(error), 250)\n }\n })\n }\n\n if (typeof process !== 'undefined' && typeof process.on === 'function' && implementation.isWorkerRuntime()) {\n process.on('uncaughtException', (error) => {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(error), 250)\n })\n process.on('unhandledRejection', (error) => {\n if (error && typeof (error as any).message === 'string') {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(error as any), 250)\n }\n })\n }\n\n return expose\n}\n","/* eslint-disable import-x/no-internal-modules */\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { SerializedError } from './types/messages.ts'\n\n/** A serializer that can convert between a message format and an input type. */\nexport interface Serializer<Msg = JsonSerializable, Input = any> {\n deserialize(message: Msg): Input\n serialize(input: Input): Msg\n}\n\n/** A serializer implementation that receives a fallback (default) serializer for chaining. */\nexport interface SerializerImplementation<Msg = JsonSerializable, Input = any> {\n deserialize(message: Msg, defaultDeserialize: (msg: Msg) => Input): Input\n serialize(input: Input, defaultSerialize: (inp: Input) => Msg): Msg\n}\n\n/**\n * Extend a base serializer with an additional serializer implementation, creating a chain.\n * @param extend - The base serializer to extend.\n * @param implementation - The new serializer implementation that wraps the base.\n * @returns A new serializer combining both behaviors.\n */\nexport function extendSerializer<MessageType, InputType = any>(\n extend: Serializer<MessageType, InputType>,\n implementation: SerializerImplementation<MessageType, InputType>,\n): Serializer<MessageType, InputType> {\n const fallbackDeserializer = extend.deserialize.bind(extend)\n const fallbackSerializer = extend.serialize.bind(extend)\n\n return {\n deserialize(message: MessageType): InputType {\n return implementation.deserialize(message, fallbackDeserializer)\n },\n\n serialize(input: InputType): MessageType {\n return implementation.serialize(input, fallbackSerializer)\n },\n }\n}\n\ntype JsonSerializablePrimitive = string | number | boolean | null\n\ntype JsonSerializableObject = {\n [key: string]: JsonSerializablePrimitive | JsonSerializablePrimitive[] | JsonSerializableObject | JsonSerializableObject[] | undefined\n}\n\n/** A JSON-compatible value that can be serialized for worker message passing. */\nexport type JsonSerializable = JsonSerializablePrimitive | JsonSerializablePrimitive[] | JsonSerializableObject | JsonSerializableObject[]\n\nconst DefaultErrorSerializer: Serializer<SerializedError, Error> = {\n deserialize(message: SerializedError): Error {\n return Object.assign(new Error(message.message), {\n name: message.name,\n stack: message.stack,\n })\n },\n serialize(error: Error): SerializedError {\n return {\n __error_marker: '$$error',\n message: error.message,\n name: error.name,\n stack: error.stack,\n }\n },\n}\n\nconst isSerializedError = (thing: any): thing is SerializedError =>\n thing && typeof thing === 'object' && '__error_marker' in thing && thing.__error_marker === '$$error'\n\n/** Default serializer that handles Error instances and passes other values through. */\nexport const DefaultSerializer: Serializer<JsonSerializable> = {\n deserialize(message: JsonSerializable): any {\n return isSerializedError(message) ? DefaultErrorSerializer.deserialize(message) : message\n },\n serialize(input: any): JsonSerializable {\n return input instanceof Error ? (DefaultErrorSerializer.serialize(input) as any as JsonSerializable) : input\n },\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type {\n JsonSerializable, Serializer, SerializerImplementation,\n} from './serializers.ts'\nimport { DefaultSerializer, extendSerializer } from './serializers.ts'\n\ndeclare global {\n var registeredSerializer: Serializer<JsonSerializable>\n}\n\nglobalThis.registeredSerializer = globalThis.registeredSerializer ?? DefaultSerializer\n\n/**\n * Register a custom serializer to extend the default serialization behavior for worker messages.\n * @param serializer - The serializer implementation to register.\n */\nexport function registerSerializer(serializer: SerializerImplementation<JsonSerializable>) {\n globalThis.registeredSerializer = extendSerializer(globalThis.registeredSerializer, serializer)\n}\n\n/**\n * Deserialize a message using the registered serializer.\n * @param message - The serialized message to deserialize.\n * @returns The deserialized value.\n */\nexport function deserialize(message: JsonSerializable): any {\n return globalThis.registeredSerializer.deserialize(message)\n}\n\n/**\n * Serialize an input value using the registered serializer.\n * @param input - The value to serialize.\n * @returns The serialized message.\n */\nexport function serialize(input: any): JsonSerializable {\n return globalThis.registeredSerializer.serialize(input)\n}\n","/** Symbol key for accessing a thread's error observable. */\nexport const $errors = Symbol('thread.errors')\n/** Symbol key for accessing a thread's event observable. */\nexport const $events = Symbol('thread.events')\n/** Symbol key for accessing a thread's terminate function. */\nexport const $terminate = Symbol('thread.terminate')\n/** Symbol key for marking an object as a transferable descriptor. */\nexport const $transferable = Symbol('thread.transferable')\n/** Symbol key for accessing the underlying worker instance of a thread. */\nexport const $worker = Symbol('thread.worker')\n","/// <reference lib=\"webworker\" />\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { $transferable } from './symbols.ts'\n\n/** Descriptor wrapping a value with its associated transferable objects for zero-copy messaging. */\nexport interface TransferDescriptor<T = any> {\n [$transferable]: true\n send: T\n transferables: Transferable[]\n}\n\nfunction isTransferable(thing: any): thing is Transferable {\n if (!thing || typeof thing !== 'object') return false\n // Don't check too thoroughly, since the list of transferable things in JS might grow over time\n return true\n}\n\n/**\n * Check whether a value is a `TransferDescriptor` created by `Transfer()`.\n * @param thing - The value to check.\n * @returns True if the value is a transfer descriptor.\n */\nexport function isTransferDescriptor(thing: any): thing is TransferDescriptor {\n return thing && typeof thing === 'object' && thing[$transferable]\n}\n\n/**\n * Mark a transferable object as such, so it will no be serialized and\n * deserialized on messaging with the main thread, but to transfer\n * ownership of it to the receiving thread.\n *\n * Only works with array buffers, message ports and few more special\n * types of objects, but it's much faster than serializing and\n * deserializing them.\n *\n * Note:\n * The transferable object cannot be accessed by this thread again\n * unless the receiving thread transfers it back again!\n *\n * @param transferable Array buffer, message port or similar.\n * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>\n */\nexport function Transfer(transferable: Transferable): TransferDescriptor\n\n/**\n * Mark transferable objects within an arbitrary object or array as\n * being a transferable object. They will then not be serialized\n * and deserialized on messaging with the main thread, but ownership\n * of them will be tranferred to the receiving thread.\n *\n * Only array buffers, message ports and few more special types of\n * objects can be transferred, but it's much faster than serializing and\n * deserializing them.\n *\n * Note:\n * The transferable object cannot be accessed by this thread again\n * unless the receiving thread transfers it back again!\n *\n * @param transferable Array buffer, message port or similar.\n * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>\n */\nexport function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor\n\nexport function Transfer<T>(payload: T, transferables?: Transferable[]): TransferDescriptor {\n console.log('Transfer')\n if (!transferables) {\n if (!isTransferable(payload)) throw new Error('Not transferable')\n transferables = [payload]\n }\n\n return {\n [$transferable]: true,\n send: payload,\n transferables,\n }\n}\n","/* eslint-disable import-x/no-internal-modules */\n\n/// <reference lib=\"dom\" />\n// tslint:disable no-shadowed-variable\n\nimport type { AbstractedWorkerAPI } from '../types/worker.ts'\nimport { createExpose } from './expose.ts'\nimport type { WorkerGlobalScope } from './WorkerGlobalScope.ts'\n\ndeclare const self: WorkerGlobalScope\n\n/** Check if the current code is running inside a browser web worker context. */\nconst isWorkerRuntime: AbstractedWorkerAPI['isWorkerRuntime'] = function isWorkerRuntime() {\n const isWindowContext = self !== undefined && typeof Window !== 'undefined' && self instanceof Window\n return self !== undefined && self['postMessage'] && !isWindowContext ? true : false\n}\n\n/** Post a message from this worker to the master thread via the global `postMessage`. */\nconst postMessageToMaster: AbstractedWorkerAPI['postMessageToMaster'] = function postMessageToMaster(data, transferList?) {\n self.postMessage(data, transferList)\n}\n\n/** Subscribe to messages from the master thread via the global `addEventListener`. */\nconst subscribeToMasterMessages: AbstractedWorkerAPI['subscribeToMasterMessages'] = function subscribeToMasterMessages(onMessage) {\n const messageHandler = (messageEvent: MessageEvent) => {\n onMessage(messageEvent.data)\n }\n const unsubscribe = () => {\n self.removeEventListener('message', messageHandler as EventListener)\n }\n self.addEventListener('message', messageHandler as EventListener)\n return unsubscribe\n}\n\n/** Bound `addEventListener` from the worker global scope. */\nconst addEventListener = self.addEventListener.bind(this)\n/** Bound `postMessage` from the worker global scope. */\nconst postMessage = self.postMessage.bind(this)\n/** Bound `removeEventListener` from the worker global scope. */\nconst removeEventListener = self.removeEventListener.bind(this)\n\nexport {\n addEventListener,\n postMessage,\n removeEventListener,\n}\n\nconst expose = createExpose({\n isWorkerRuntime, postMessageToMaster, subscribeToMasterMessages,\n}, {\n addEventListener, postMessage, removeEventListener,\n})\n\nexport {\n isWorkerRuntime,\n postMessageToMaster,\n subscribeToMasterMessages,\n}\n\nexport { registerSerializer } from '../common.ts'\nexport { Transfer } from '../transferable.ts'\nexport { expose }\n"],"mappings":";AAIA,OAAO,sBAAsB;;;ACkBtB,SAAS,iBACd,QACA,gBACoC;AACpC,QAAM,uBAAuB,OAAO,YAAY,KAAK,MAAM;AAC3D,QAAM,qBAAqB,OAAO,UAAU,KAAK,MAAM;AAEvD,SAAO;AAAA,IACL,YAAY,SAAiC;AAC3C,aAAO,eAAe,YAAY,SAAS,oBAAoB;AAAA,IACjE;AAAA,IAEA,UAAU,OAA+B;AACvC,aAAO,eAAe,UAAU,OAAO,kBAAkB;AAAA,IAC3D;AAAA,EACF;AACF;AAWA,IAAM,yBAA6D;AAAA,EACjE,YAAY,SAAiC;AAC3C,WAAO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AAChE,SAAO;AACT;AAGA,IAAM,mBAAmB,KAAK,iBAAiB,KAAK,MAAI;AAExD,IAAM,cAAc,KAAK,YAAY,KAAK,MAAI;AAE9C,IAAM,sBAAsB,KAAK,oBAAoB,KAAK,MAAI;AAQ9D,IAAM,SAAS,aAAa;AAAA,EAC1B;AAAA,EAAiB;AAAA,EAAqB;AACxC,GAAG;AAAA,EACD;AAAA,EAAkB;AAAA,EAAa;AACjC,CAAC;","names":["self","expose","isWorkerRuntime","postMessageToMaster","subscribeToMasterMessages"]}
1
+ {"version":3,"sources":["../../../src/worker/expose.ts","../../../src/serializers.ts","../../../src/common.ts","../../../src/symbols.ts","../../../src/transferable.ts","../../../src/worker/worker.browser.ts"],"sourcesContent":["/* eslint-disable import-x/no-internal-modules */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-floating-promises */\n\nimport isSomeObservable from 'is-observable-2-1-0'\nimport type { Observable, Subscription } from 'observable-fns'\n\nimport { deserialize, serialize } from '../common.ts'\nimport type { TransferDescriptor } from '../transferable.ts'\nimport { isTransferDescriptor } from '../transferable.ts'\nimport type {\n MasterJobCancelMessage,\n MasterJobRunMessage,\n SerializedError,\n WorkerInitMessage,\n WorkerJobErrorMessage,\n WorkerJobResultMessage,\n WorkerJobStartMessage,\n WorkerUncaughtErrorMessage,\n} from '../types/messages.ts'\nimport {\n MasterMessageType,\n WorkerMessageType,\n} from '../types/messages.ts'\nimport type {\n AbstractedWorkerAPI, WorkerFunction, WorkerModule,\n} from '../types/worker.ts'\nimport type { WorkerGlobalScope } from './WorkerGlobalScope.ts'\n\nconst isErrorEvent = (value: Event): value is ErrorEvent => value !== undefined && (value as ErrorEvent).error !== undefined\n\n/**\n * Create an `expose()` function bound to a specific worker API implementation and global scope.\n * @param implementation - The abstracted worker API for communicating with the master thread.\n * @param self - The worker's global scope for subscribing to error events.\n * @returns The `expose()` function that workers call to register their API.\n */\nexport function createExpose(implementation: AbstractedWorkerAPI, self: WorkerGlobalScope) {\n let exposeCalled = false\n\n const activeSubscriptions = new Map<number, Subscription<any>>()\n\n const isMasterJobCancelMessage = (thing: any): thing is MasterJobCancelMessage => thing?.type === MasterMessageType.cancel\n const isMasterJobRunMessage = (thing: any): thing is MasterJobRunMessage => thing?.type === MasterMessageType.run\n\n /**\n * There are issues with `is-observable` not recognizing zen-observable's instances.\n * We are using `observable-fns`, but it's based on zen-observable, too.\n */\n const isObservable = (thing: any): thing is Observable<any> => isSomeObservable(thing) || isZenObservable(thing)\n\n function isZenObservable(thing: any): thing is Observable<any> {\n return thing && typeof thing === 'object' && typeof thing.subscribe === 'function'\n }\n\n function deconstructTransfer(thing: any) {\n return isTransferDescriptor(thing) ? { payload: thing.send, transferables: thing.transferables } : { payload: thing, transferables: undefined }\n }\n\n function postFunctionInitMessage() {\n const initMessage: WorkerInitMessage = {\n exposed: { type: 'function' },\n type: WorkerMessageType.init,\n }\n implementation.postMessageToMaster(initMessage)\n }\n\n function postModuleInitMessage(methodNames: string[]) {\n const initMessage: WorkerInitMessage = {\n exposed: {\n methods: methodNames,\n type: 'module',\n },\n type: WorkerMessageType.init,\n }\n implementation.postMessageToMaster(initMessage)\n }\n\n function postJobErrorMessage(uid: number, rawError: Error | TransferDescriptor<Error>) {\n const { payload: error, transferables } = deconstructTransfer(rawError)\n const errorMessage: WorkerJobErrorMessage = {\n error: serialize(error) as any as SerializedError,\n type: WorkerMessageType.error,\n uid,\n }\n implementation.postMessageToMaster(errorMessage, transferables)\n }\n\n function postJobResultMessage(uid: number, completed: boolean, resultValue?: any) {\n const { payload, transferables } = deconstructTransfer(resultValue)\n const resultMessage: WorkerJobResultMessage = {\n complete: completed ? true : undefined,\n payload,\n type: WorkerMessageType.result,\n uid,\n }\n implementation.postMessageToMaster(resultMessage, transferables)\n }\n\n function postJobStartMessage(uid: number, resultType: WorkerJobStartMessage['resultType']) {\n const startMessage: WorkerJobStartMessage = {\n resultType,\n type: WorkerMessageType.running,\n uid,\n }\n implementation.postMessageToMaster(startMessage)\n }\n\n function postUncaughtErrorMessage(error: Error) {\n try {\n const errorMessage: WorkerUncaughtErrorMessage = {\n error: serialize(error) as any as SerializedError,\n type: WorkerMessageType.uncaughtError,\n }\n implementation.postMessageToMaster(errorMessage)\n } catch (subError) {\n // eslint-disable-next-line no-console\n console.error(\n 'Not reporting uncaught error back to master thread as it ' + 'occured while reporting an uncaught error already.' + '\\nLatest error:',\n subError,\n '\\nOriginal error:',\n error,\n )\n }\n }\n\n async function runFunction(jobUID: number, fn: WorkerFunction, args: any[]) {\n let syncResult: any\n\n try {\n syncResult = fn(...args)\n } catch (ex) {\n const error = ex as Error\n return postJobErrorMessage(jobUID, error)\n }\n\n const resultType = isObservable(syncResult) ? 'observable' : 'promise'\n postJobStartMessage(jobUID, resultType)\n\n if (isObservable(syncResult)) {\n const subscription = syncResult.subscribe(\n value => postJobResultMessage(jobUID, false, serialize(value)),\n (error) => {\n postJobErrorMessage(jobUID, serialize(error) as any)\n activeSubscriptions.delete(jobUID)\n },\n () => {\n postJobResultMessage(jobUID, true)\n activeSubscriptions.delete(jobUID)\n },\n )\n activeSubscriptions.set(jobUID, subscription)\n } else {\n try {\n const result = await syncResult\n postJobResultMessage(jobUID, true, serialize(result))\n } catch (error) {\n postJobErrorMessage(jobUID, serialize(error) as any)\n }\n }\n }\n\n /**\n * Expose a function or a module (an object whose values are functions)\n * to the main thread. Must be called exactly once in every worker thread\n * to signal its API to the main thread.\n *\n * @param exposed Function or object whose values are functions\n */\n const expose = (exposed: WorkerFunction | WorkerModule<any>) => {\n if (!implementation.isWorkerRuntime()) {\n throw new Error('expose() called in the master thread.')\n }\n if (exposeCalled) {\n throw new Error('expose() called more than once. This is not possible. Pass an object to expose() if you want to expose multiple functions.')\n }\n exposeCalled = true\n\n if (typeof exposed === 'function') {\n implementation.subscribeToMasterMessages((messageData: unknown) => {\n if (isMasterJobRunMessage(messageData) && messageData.method === undefined) {\n runFunction(messageData.uid, exposed, messageData.args.map(deserialize))\n }\n })\n postFunctionInitMessage()\n } else if (typeof exposed === 'object' && exposed !== null) {\n implementation.subscribeToMasterMessages((messageData: unknown) => {\n if (isMasterJobRunMessage(messageData) && messageData.method !== undefined) {\n runFunction(messageData.uid, exposed[messageData.method], messageData.args.map(deserialize))\n }\n })\n\n const methodNames = Object.keys(exposed).filter(key => typeof exposed[key] === 'function')\n postModuleInitMessage(methodNames)\n } else {\n throw new Error(`Invalid argument passed to expose(). Expected a function or an object, got: ${exposed}`)\n }\n\n implementation.subscribeToMasterMessages((messageData: unknown) => {\n if (isMasterJobCancelMessage(messageData)) {\n const jobUID = messageData.uid\n const subscription = activeSubscriptions.get(jobUID)\n\n if (subscription) {\n subscription.unsubscribe()\n activeSubscriptions.delete(jobUID)\n }\n }\n })\n }\n\n if (typeof globalThis !== 'undefined' && typeof self.addEventListener === 'function' && implementation.isWorkerRuntime()) {\n self.addEventListener('error', (event) => {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(isErrorEvent(event) ? event.error : event), 250)\n })\n self.addEventListener('unhandledrejection', (event) => {\n const error = (event as any).reason\n if (error && typeof (error as any).message === 'string') {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(error), 250)\n }\n })\n }\n\n if (typeof process !== 'undefined' && typeof process.on === 'function' && implementation.isWorkerRuntime()) {\n process.on('uncaughtException', (error) => {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(error), 250)\n })\n process.on('unhandledRejection', (error) => {\n if (error && typeof (error as any).message === 'string') {\n // Post with some delay, so the master had some time to subscribe to messages\n setTimeout(() => postUncaughtErrorMessage(error as any), 250)\n }\n })\n }\n\n return expose\n}\n","/* eslint-disable import-x/no-internal-modules */\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { SerializedError } from './types/messages.ts'\n\n/** A serializer that can convert between a message format and an input type. */\nexport interface Serializer<Msg = JsonSerializable, Input = any> {\n deserialize(message: Msg): Input\n serialize(input: Input): Msg\n}\n\n/** A serializer implementation that receives a fallback (default) serializer for chaining. */\nexport interface SerializerImplementation<Msg = JsonSerializable, Input = any> {\n deserialize(message: Msg, defaultDeserialize: (msg: Msg) => Input): Input\n serialize(input: Input, defaultSerialize: (inp: Input) => Msg): Msg\n}\n\n/**\n * Extend a base serializer with an additional serializer implementation, creating a chain.\n * @param extend - The base serializer to extend.\n * @param implementation - The new serializer implementation that wraps the base.\n * @returns A new serializer combining both behaviors.\n */\nexport function extendSerializer<MessageType, InputType = any>(\n extend: Serializer<MessageType, InputType>,\n implementation: SerializerImplementation<MessageType, InputType>,\n): Serializer<MessageType, InputType> {\n const fallbackDeserializer = extend.deserialize.bind(extend)\n const fallbackSerializer = extend.serialize.bind(extend)\n\n return {\n deserialize(message: MessageType): InputType {\n return implementation.deserialize(message, fallbackDeserializer)\n },\n\n serialize(input: InputType): MessageType {\n return implementation.serialize(input, fallbackSerializer)\n },\n }\n}\n\ntype JsonSerializablePrimitive = string | number | boolean | null\n\ntype JsonSerializableObject = {\n [key: string]: JsonSerializablePrimitive | JsonSerializablePrimitive[] | JsonSerializableObject | JsonSerializableObject[] | undefined\n}\n\n/** A JSON-compatible value that can be serialized for worker message passing. */\nexport type JsonSerializable = JsonSerializablePrimitive | JsonSerializablePrimitive[] | JsonSerializableObject | JsonSerializableObject[]\n\nconst DefaultErrorSerializer: Serializer<SerializedError, Error> = {\n deserialize(message: SerializedError): Error {\n return Object.assign(new Error(message.message), {\n name: message.name,\n stack: message.stack,\n })\n },\n serialize(error: Error): SerializedError {\n return {\n __error_marker: '$$error',\n message: error.message,\n name: error.name,\n stack: error.stack,\n }\n },\n}\n\nconst isSerializedError = (thing: any): thing is SerializedError =>\n thing && typeof thing === 'object' && '__error_marker' in thing && thing.__error_marker === '$$error'\n\n/** Default serializer that handles Error instances and passes other values through. */\nexport const DefaultSerializer: Serializer<JsonSerializable> = {\n deserialize(message: JsonSerializable): any {\n return isSerializedError(message) ? DefaultErrorSerializer.deserialize(message) : message\n },\n serialize(input: any): JsonSerializable {\n return input instanceof Error ? (DefaultErrorSerializer.serialize(input) as any as JsonSerializable) : input\n },\n}\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type {\n JsonSerializable, Serializer, SerializerImplementation,\n} from './serializers.ts'\nimport { DefaultSerializer, extendSerializer } from './serializers.ts'\n\ndeclare global {\n var registeredSerializer: Serializer<JsonSerializable>\n}\n\nglobalThis.registeredSerializer = globalThis.registeredSerializer ?? DefaultSerializer\n\n/**\n * Register a custom serializer to extend the default serialization behavior for worker messages.\n * @param serializer - The serializer implementation to register.\n */\nexport function registerSerializer(serializer: SerializerImplementation<JsonSerializable>) {\n globalThis.registeredSerializer = extendSerializer(globalThis.registeredSerializer, serializer)\n}\n\n/**\n * Deserialize a message using the registered serializer.\n * @param message - The serialized message to deserialize.\n * @returns The deserialized value.\n */\nexport function deserialize(message: JsonSerializable): any {\n return globalThis.registeredSerializer.deserialize(message)\n}\n\n/**\n * Serialize an input value using the registered serializer.\n * @param input - The value to serialize.\n * @returns The serialized message.\n */\nexport function serialize(input: any): JsonSerializable {\n return globalThis.registeredSerializer.serialize(input)\n}\n","/** Symbol key for accessing a thread's error observable. */\nexport const $errors = Symbol('thread.errors')\n/** Symbol key for accessing a thread's event observable. */\nexport const $events = Symbol('thread.events')\n/** Symbol key for accessing a thread's terminate function. */\nexport const $terminate = Symbol('thread.terminate')\n/** Symbol key for marking an object as a transferable descriptor. */\nexport const $transferable = Symbol('thread.transferable')\n/** Symbol key for accessing the underlying worker instance of a thread. */\nexport const $worker = Symbol('thread.worker')\n","/// <reference lib=\"webworker\" />\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { $transferable } from './symbols.ts'\n\n/** Descriptor wrapping a value with its associated transferable objects for zero-copy messaging. */\nexport interface TransferDescriptor<T = any> {\n [$transferable]: true\n send: T\n transferables: Transferable[]\n}\n\nfunction isTransferable(thing: any): thing is Transferable {\n if (!thing || typeof thing !== 'object') return false\n // Don't check too thoroughly, since the list of transferable things in JS might grow over time\n return true\n}\n\n/**\n * Check whether a value is a `TransferDescriptor` created by `Transfer()`.\n * @param thing - The value to check.\n * @returns True if the value is a transfer descriptor.\n */\nexport function isTransferDescriptor(thing: any): thing is TransferDescriptor {\n return thing && typeof thing === 'object' && thing[$transferable]\n}\n\n/**\n * Mark a transferable object as such, so it will no be serialized and\n * deserialized on messaging with the main thread, but to transfer\n * ownership of it to the receiving thread.\n *\n * Only works with array buffers, message ports and few more special\n * types of objects, but it's much faster than serializing and\n * deserializing them.\n *\n * Note:\n * The transferable object cannot be accessed by this thread again\n * unless the receiving thread transfers it back again!\n *\n * @param transferable Array buffer, message port or similar.\n * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>\n */\nexport function Transfer(transferable: Transferable): TransferDescriptor\n\n/**\n * Mark transferable objects within an arbitrary object or array as\n * being a transferable object. They will then not be serialized\n * and deserialized on messaging with the main thread, but ownership\n * of them will be tranferred to the receiving thread.\n *\n * Only array buffers, message ports and few more special types of\n * objects can be transferred, but it's much faster than serializing and\n * deserializing them.\n *\n * Note:\n * The transferable object cannot be accessed by this thread again\n * unless the receiving thread transfers it back again!\n *\n * @param transferable Array buffer, message port or similar.\n * @see <https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast>\n */\nexport function Transfer<T>(payload: T, transferables: Transferable[]): TransferDescriptor\n\nexport function Transfer<T>(payload: T, transferables?: Transferable[]): TransferDescriptor {\n // eslint-disable-next-line no-console\n console.log('Transfer')\n if (!transferables) {\n if (!isTransferable(payload)) throw new Error('Not transferable')\n transferables = [payload]\n }\n\n return {\n [$transferable]: true,\n send: payload,\n transferables,\n }\n}\n","/* eslint-disable import-x/no-internal-modules */\n\n/// <reference lib=\"dom\" />\n\nimport type { AbstractedWorkerAPI } from '../types/worker.ts'\nimport { createExpose } from './expose.ts'\nimport type { WorkerGlobalScope } from './WorkerGlobalScope.ts'\n\ndeclare const self: WorkerGlobalScope\n\n/** Check if the current code is running inside a browser web worker context. */\nconst isWorkerRuntime: AbstractedWorkerAPI['isWorkerRuntime'] = function isWorkerRuntime() {\n const isWindowContext = self !== undefined && typeof Window !== 'undefined' && self instanceof Window\n return self?.postMessage !== undefined && !isWindowContext\n}\n\n/** Post a message from this worker to the master thread via the global `postMessage`. */\nconst postMessageToMaster: AbstractedWorkerAPI['postMessageToMaster'] = function postMessageToMaster(data, transferList?) {\n self.postMessage(data, transferList)\n}\n\n/** Subscribe to messages from the master thread via the global `addEventListener`. */\nconst subscribeToMasterMessages: AbstractedWorkerAPI['subscribeToMasterMessages'] = function subscribeToMasterMessages(onMessage) {\n const messageHandler = (messageEvent: MessageEvent) => {\n onMessage(messageEvent.data)\n }\n const unsubscribe = () => {\n self.removeEventListener('message', messageHandler as EventListener)\n }\n self.addEventListener('message', messageHandler as EventListener)\n return unsubscribe\n}\n\n/** Bound `addEventListener` from the worker global scope. */\nconst addEventListener = self.addEventListener.bind(this)\n/** Bound `postMessage` from the worker global scope. */\nconst postMessage = self.postMessage.bind(this)\n/** Bound `removeEventListener` from the worker global scope. */\nconst removeEventListener = self.removeEventListener.bind(this)\n\nexport {\n addEventListener,\n postMessage,\n removeEventListener,\n}\n\nconst expose = createExpose({\n isWorkerRuntime, postMessageToMaster, subscribeToMasterMessages,\n}, {\n addEventListener, postMessage, removeEventListener,\n})\n\nexport {\n isWorkerRuntime,\n postMessageToMaster,\n subscribeToMasterMessages,\n}\n\nexport { registerSerializer } from '../common.ts'\nexport { Transfer } from '../transferable.ts'\nexport { expose }\n"],"mappings":";AAIA,OAAO,sBAAsB;;;ACkBtB,SAAS,iBACd,QACA,gBACoC;AACpC,QAAM,uBAAuB,OAAO,YAAY,KAAK,MAAM;AAC3D,QAAM,qBAAqB,OAAO,UAAU,KAAK,MAAM;AAEvD,SAAO;AAAA,IACL,YAAY,SAAiC;AAC3C,aAAO,eAAe,YAAY,SAAS,oBAAoB;AAAA,IACjE;AAAA,IAEA,UAAU,OAA+B;AAC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A,EAC7B;AACA,QAAM,cAAc,MAAM;AACxB,SAAK,oBAAoB,WAAW,cAA+B;AAAA,EACrE;AACA,OAAK,iBAAiB,WAAW,cAA+B;AAChE,SAAO;AACT;AAGA,IAAM,mBAAmB,KAAK,iBAAiB,KAAK,MAAI;AAExD,IAAM,cAAc,KAAK,YAAY,KAAK,MAAI;AAE9C,IAAM,sBAAsB,KAAK,oBAAoB,KAAK,MAAI;AAQ9D,IAAM,SAAS,aAAa;AAAA,EAC1B;AAAA,EAAiB;AAAA,EAAqB;AACxC,GAAG;AAAA,EACD;AAAA,EAAkB;AAAA,EAAa;AACjC,CAAC;","names":["self","expose","isWorkerRuntime","postMessageToMaster","subscribeToMasterMessages"]}
@@ -8,8 +8,6 @@ interface AbstractedWorkerAPI {
8
8
  /** A function that can be exposed from a worker thread. */
9
9
  type WorkerFunction = ((...args: any[]) => any) | (() => any);
10
10
  /** An object whose values are functions, representing a module exposed from a worker thread. */
11
- type WorkerModule<Keys extends string> = {
12
- [key in Keys]: WorkerFunction;
13
- };
11
+ type WorkerModule<Keys extends string> = Record<Keys, WorkerFunction>;
14
12
 
15
13
  export type { AbstractedWorkerAPI as A, WorkerFunction as W, WorkerModule as a };
@@ -16,12 +16,12 @@ function initWorkerThreadsWorker() {
16
16
  class Worker2 extends NativeWorker {
17
17
  mappedEventListeners;
18
18
  constructor(scriptPath, options) {
19
- const resolvedScriptPath = options && options.fromSource ? null : resolveScriptPath(scriptPath, (options ?? {})._baseURL);
20
- if (resolvedScriptPath) {
21
- super(resolvedScriptPath, options);
22
- } else {
19
+ const resolvedScriptPath = options?.fromSource ? null : resolveScriptPath(scriptPath, options?._baseURL);
20
+ if (resolvedScriptPath === null) {
23
21
  const sourceCode = scriptPath;
24
22
  super(sourceCode, { ...options, eval: true });
23
+ } else {
24
+ super(resolvedScriptPath, options);
25
25
  }
26
26
  this.mappedEventListeners = /* @__PURE__ */ new WeakMap();
27
27
  allWorkers.push(this);
@@ -62,7 +62,7 @@ function initWorkerThreadsWorker() {
62
62
  }
63
63
  var implementation;
64
64
  function getWorkerImplementation() {
65
- if (!implementation) {
65
+ if (implementation === void 0) {
66
66
  implementation = initWorkerThreadsWorker();
67
67
  }
68
68
  return implementation;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/master/implementation.node.ts","../../../src/master/index-node.ts","../../../src/master/register.ts"],"sourcesContent":["/* eslint-disable unicorn/prefer-add-event-listener */\n/* eslint-disable unicorn/prefer-event-target */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable unicorn/text-encoding-identifier-case */\n\nimport { cpus } from 'node:os'\nimport path from 'node:path'\nimport { cwd } from 'node:process'\nimport { isMainThread, Worker as NativeWorker } from 'node:worker_threads'\n\nimport type {\n ImplementationExport, ThreadsWorkerOptions, WorkerImplementation,\n// eslint-disable-next-line import-x/no-internal-modules\n} from '../types/master.ts'\n\n/** Default thread pool size based on the number of CPU cores. */\nexport const defaultPoolSize = cpus().length\n\nfunction resolveScriptPath(scriptPath: string, baseURL?: string | undefined) {\n const makeAbsolute = (filePath: string) => {\n return path.isAbsolute(filePath) ? filePath : path.join(baseURL ?? cwd(), filePath)\n }\n\n const absolutePath = makeAbsolute(scriptPath)\n return absolutePath\n}\n\nfunction initWorkerThreadsWorker(): ImplementationExport {\n let allWorkers: Array<NativeWorker> = []\n\n class Worker extends NativeWorker {\n private mappedEventListeners: WeakMap<EventListener, EventListener>\n\n constructor(scriptPath: string, options?: ThreadsWorkerOptions & { fromSource: boolean }) {\n const resolvedScriptPath = options && options.fromSource ? null : resolveScriptPath(scriptPath, (options ?? {})._baseURL)\n if (resolvedScriptPath) {\n super(resolvedScriptPath, options)\n } else {\n // `options.fromSource` is true\n const sourceCode = scriptPath\n super(sourceCode, { ...options, eval: true })\n }\n\n this.mappedEventListeners = new WeakMap()\n allWorkers.push(this)\n }\n\n addEventListener(eventName: string, rawListener: EventListener) {\n const listener = (message: any) => {\n rawListener({ data: message } as any)\n }\n this.mappedEventListeners.set(rawListener, listener)\n this.on(eventName, listener)\n }\n\n removeEventListener(eventName: string, rawListener: EventListener) {\n const listener = this.mappedEventListeners.get(rawListener) || rawListener\n this.off(eventName, listener)\n }\n }\n\n const terminateWorkersAndMaster = () => {\n // we should terminate all workers and then gracefully shutdown self process\n Promise.all(allWorkers.map(worker => worker.terminate())).then(\n () => process.exit(0),\n () => process.exit(1),\n )\n allWorkers = []\n }\n\n // Take care to not leave orphaned processes behind. See #147.\n process.on('SIGINT', () => terminateWorkersAndMaster())\n process.on('SIGTERM', () => terminateWorkersAndMaster())\n\n class BlobWorker extends Worker {\n constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {\n super(Buffer.from(blob).toString('utf-8'), { ...options, fromSource: true })\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation {\n return new Worker(source, { ...options, fromSource: true }) as any\n }\n }\n\n return {\n blob: BlobWorker as any,\n default: Worker as any,\n }\n}\n\nlet implementation: ImplementationExport\n\n/**\n * Get the Node.js-specific worker implementation using `worker_threads`, lazily initializing it on first call.\n * @returns The platform-specific worker implementation export.\n */\nexport function getWorkerImplementation(): ImplementationExport {\n if (!implementation) {\n implementation = initWorkerThreadsWorker()\n }\n return implementation\n}\n\n/**\n * Check whether the current code is running inside a Node.js worker thread.\n * @returns True if running in a worker thread (not the main thread), false otherwise.\n */\nexport function isWorkerRuntime() {\n return !isMainThread\n}\n","/* eslint-disable import-x/no-internal-modules */\nimport type { BlobWorker as BlobWorkerClass, Worker as WorkerType } from '../types/master.ts'\nimport * as NodeImplementation from './implementation.node.ts'\n\nexport type { FunctionThread, ModuleThread } from '../types/master.ts'\nexport { Pool } from './pool-browser.ts'\nexport { spawn } from './spawn.ts'\nexport { Thread } from './thread.ts'\n\nexport type BlobWorker = typeof BlobWorkerClass\nexport type Worker = WorkerType\n\n/** Separate class to spawn workers from source code blobs or strings. */\nexport const BlobWorker = NodeImplementation.getWorkerImplementation().blob\n\n/** Worker implementation. Either web worker or a node.js Worker class. */\nexport const Worker = NodeImplementation.getWorkerImplementation().default\n\nexport { isWorkerRuntime } from './implementation.node.ts'\n","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { Worker as WorkerImplementation } from './index-node.ts'\n\ndeclare const window: any\n\nif (typeof globalThis !== 'undefined') {\n ;(globalThis as any).Worker = WorkerImplementation\n} else if (window !== undefined) {\n ;(window as any).Worker = WorkerImplementation\n}\n"],"mappings":";AAKA,SAAS,YAAY;AACrB,OAAO,UAAU;AACjB,SAAS,WAAW;AACpB,SAAS,cAAc,UAAU,oBAAoB;AAQ9C,IAAM,kBAAkB,KAAK,EAAE;AAEtC,SAAS,kBAAkB,YAAoB,SAA8B;AAC3E,QAAM,eAAe,CAAC,aAAqB;AACzC,WAAO,KAAK,WAAW,QAAQ,IAAI,WAAW,KAAK,KAAK,WAAW,IAAI,GAAG,QAAQ;AAAA,EACpF;AAEA,QAAM,eAAe,aAAa,UAAU;AAC5C,SAAO;AACT;AAEA,SAAS,0BAAgD;AACvD,MAAI,aAAkC,CAAC;AAAA,EAEvC,MAAMA,gBAAe,aAAa;AAAA,IACxB;AAAA,IAER,YAAY,YAAoB,SAA0D;AACxF,YAAM,qBAAqB,WAAW,QAAQ,aAAa,OAAO,kBAAkB,aAAa,WAAW,CAAC,GAAG,QAAQ;AACxH,UAAI,oBAAoB;AACtB,cAAM,oBAAoB,OAAO;AAAA,MACnC,OAAO;AAEL,cAAM,aAAa;AACnB,cAAM,YAAY,EAAE,GAAG,SAAS,MAAM,KAAK,CAAC;AAAA,MAC9C;AAEA,WAAK,uBAAuB,oBAAI,QAAQ;AACxC,iBAAW,KAAK,IAAI;AAAA,IACtB;AAAA,IAEA,iBAAiB,WAAmB,aAA4B;AAC9D,YAAM,WAAW,CAAC,YAAiB;AACjC,oBAAY,EAAE,MAAM,QAAQ,CAAQ;AAAA,MACtC;AACA,WAAK,qBAAqB,IAAI,aAAa,QAAQ;AACnD,WAAK,GAAG,WAAW,QAAQ;AAAA,IAC7B;AAAA,IAEA,oBAAoB,WAAmB,aAA4B;AACjE,YAAM,WAAW,KAAK,qBAAqB,IAAI,WAAW,KAAK;AAC/D,WAAK,IAAI,WAAW,QAAQ;AAAA,IAC9B;AAAA,EACF;AAEA,QAAM,4BAA4B,MAAM;AAEtC,YAAQ,IAAI,WAAW,IAAI,YAAU,OAAO,UAAU,CAAC,CAAC,EAAE;AAAA,MACxD,MAAM,QAAQ,KAAK,CAAC;AAAA,MACpB,MAAM,QAAQ,KAAK,CAAC;AAAA,IACtB;AACA,iBAAa,CAAC;AAAA,EAChB;AAGA,UAAQ,GAAG,UAAU,MAAM,0BAA0B,CAAC;AACtD,UAAQ,GAAG,WAAW,MAAM,0BAA0B,CAAC;AAAA,EAEvD,MAAMC,oBAAmBD,QAAO;AAAA,IAC9B,YAAY,MAAkB,SAAgC;AAC5D,YAAM,OAAO,KAAK,IAAI,EAAE,SAAS,OAAO,GAAG,EAAE,GAAG,SAAS,YAAY,KAAK,CAAC;AAAA,IAC7E;AAAA,IAEA,OAAO,SAAS,QAAgB,SAAsD;AACpF,aAAO,IAAIA,QAAO,QAAQ,EAAE,GAAG,SAAS,YAAY,KAAK,CAAC;AAAA,IAC5D;AAAA,EACF;AAEA,SAAO;AAAA,IACL,MAAMC;AAAA,IACN,SAASD;AAAA,EACX;AACF;AAEA,IAAI;AAMG,SAAS,0BAAgD;AAC9D,MAAI,CAAC,gBAAgB;AACnB,qBAAiB,wBAAwB;AAAA,EAC3C;AACA,SAAO;AACT;;;ACxFO,IAAM,aAAgC,wBAAwB,EAAE;AAGhE,IAAM,SAA4B,wBAAwB,EAAE;;;ACVnE,IAAI,OAAO,eAAe,aAAa;AACrC;AAAC,EAAC,WAAmB,SAAS;AAChC,WAAW,WAAW,QAAW;AAC/B;AAAC,EAAC,OAAe,SAAS;AAC5B;","names":["Worker","BlobWorker"]}
1
+ {"version":3,"sources":["../../../src/master/implementation.node.ts","../../../src/master/index-node.ts","../../../src/master/register.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable unicorn/text-encoding-identifier-case */\n\nimport { cpus } from 'node:os'\nimport path from 'node:path'\nimport { cwd } from 'node:process'\nimport { isMainThread, Worker as NativeWorker } from 'node:worker_threads'\n\nimport type {\n ImplementationExport, ThreadsWorkerOptions, WorkerImplementation,\n// eslint-disable-next-line import-x/no-internal-modules\n} from '../types/master.ts'\n\n/** Default thread pool size based on the number of CPU cores. */\nexport const defaultPoolSize = cpus().length\n\nfunction resolveScriptPath(scriptPath: string, baseURL?: string | undefined) {\n const makeAbsolute = (filePath: string) => {\n return path.isAbsolute(filePath) ? filePath : path.join(baseURL ?? cwd(), filePath)\n }\n\n const absolutePath = makeAbsolute(scriptPath)\n return absolutePath\n}\n\nfunction initWorkerThreadsWorker(): ImplementationExport {\n let allWorkers: NativeWorker[] = []\n\n class Worker extends NativeWorker {\n private mappedEventListeners: WeakMap<EventListener, EventListener>\n\n constructor(scriptPath: string, options?: ThreadsWorkerOptions & { fromSource: boolean }) {\n const resolvedScriptPath = options?.fromSource ? null : resolveScriptPath(scriptPath, options?._baseURL)\n if (resolvedScriptPath === null) {\n // `options.fromSource` is true\n const sourceCode = scriptPath\n super(sourceCode, { ...options, eval: true })\n } else {\n super(resolvedScriptPath, options)\n }\n\n this.mappedEventListeners = new WeakMap()\n allWorkers.push(this)\n }\n\n addEventListener(eventName: string, rawListener: EventListener) {\n const listener = (message: any) => {\n rawListener({ data: message } as any)\n }\n this.mappedEventListeners.set(rawListener, listener)\n this.on(eventName, listener)\n }\n\n removeEventListener(eventName: string, rawListener: EventListener) {\n const listener = this.mappedEventListeners.get(rawListener) || rawListener\n this.off(eventName, listener)\n }\n }\n\n const terminateWorkersAndMaster = () => {\n // we should terminate all workers and then gracefully shutdown self process\n Promise.all(allWorkers.map(worker => worker.terminate())).then(\n () => process.exit(0),\n () => process.exit(1),\n )\n allWorkers = []\n }\n\n // Take care to not leave orphaned processes behind. See #147.\n process.on('SIGINT', () => terminateWorkersAndMaster())\n process.on('SIGTERM', () => terminateWorkersAndMaster())\n\n class BlobWorker extends Worker {\n constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {\n super(Buffer.from(blob).toString('utf-8'), { ...options, fromSource: true })\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation {\n return new Worker(source, { ...options, fromSource: true }) as any\n }\n }\n\n return {\n blob: BlobWorker as any,\n default: Worker as any,\n }\n}\n\nlet implementation: ImplementationExport\n\n/**\n * Get the Node.js-specific worker implementation using `worker_threads`, lazily initializing it on first call.\n * @returns The platform-specific worker implementation export.\n */\nexport function getWorkerImplementation(): ImplementationExport {\n if (implementation === undefined) {\n implementation = initWorkerThreadsWorker()\n }\n return implementation\n}\n\n/**\n * Check whether the current code is running inside a Node.js worker thread.\n * @returns True if running in a worker thread (not the main thread), false otherwise.\n */\nexport function isWorkerRuntime() {\n return !isMainThread\n}\n","/* eslint-disable import-x/no-internal-modules */\nimport type { BlobWorker as BlobWorkerClass, Worker as WorkerType } from '../types/master.ts'\nimport * as NodeImplementation from './implementation.node.ts'\n\nexport type { FunctionThread, ModuleThread } from '../types/master.ts'\nexport { Pool } from './pool-browser.ts'\nexport { spawn } from './spawn.ts'\nexport { Thread } from './thread.ts'\n\nexport type BlobWorker = typeof BlobWorkerClass\nexport type Worker = WorkerType\n\n/** Separate class to spawn workers from source code blobs or strings. */\nexport const BlobWorker = NodeImplementation.getWorkerImplementation().blob\n\n/** Worker implementation. Either web worker or a node.js Worker class. */\nexport const Worker = NodeImplementation.getWorkerImplementation().default\n\nexport { isWorkerRuntime } from './implementation.node.ts'\n","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nimport { Worker as WorkerImplementation } from './index-node.ts'\n\ndeclare const window: any\n\nif (typeof globalThis !== 'undefined') {\n ;(globalThis as any).Worker = WorkerImplementation\n} else if (window !== undefined) {\n ;(window as any).Worker = WorkerImplementation\n}\n"],"mappings":";AAGA,SAAS,YAAY;AACrB,OAAO,UAAU;AACjB,SAAS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BAAa,CAAC;AAAA,EAChB;AAGA,UAAQ,GAAG,UAAU,MAAM,0BAA0B,CAAC;AACtD,UAAQ,GAAG,WAAW,MAAM,0BAA0B,CAAC;AAAA,EAEvD,MAAMC,oBAAmBD,QAAO;AAAA,IAC9B,YAAY,MAAkB,SAAgC;AAC5D,YAAM,OAAO,KAAK,IAAI,EAAE,SAAS,OAAO,GAAG,EAAE,GAAG,SAAS,YAAY,KAAK,CAAC;AAAA,IAC7E;AAAA,IAEA,OAAO,SAAS,QAAgB,SAAsD;AACpF,aAAO,IAAIA,QAAO,QAAQ,EAAE,GAAG,SAAS,YAAY,KAAK,CAAC;AAAA,IAC5D;AAAA,EACF;AAEA,SAAO;AAAA,IACL,MAAMC;AAAA,IACN,SAASD;AAAA,EACX;AACF;AAEA,IAAI;AAMG,SAAS,0BAAgD;AAC9D,MAAI,mBAAmB,QAAW;AAChC,qBAAiB,wBAAwB;AAAA,EAC3C;AACA,SAAO;AACT;;;ACtFO,IAAM,aAAgC,wBAAwB,EAAE;AAGhE,IAAM,SAA4B,wBAAwB,EAAE;;;ACVnE,IAAI,OAAO,eAAe,aAAa;AACrC;AAAC,EAAC,WAAmB,SAAS;AAChC,WAAW,WAAW,QAAW;AAC/B;AAAC,EAAC,OAAe,SAAS;AAC5B;","names":["Worker","BlobWorker"]}
@@ -1,13 +1,11 @@
1
- import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker } from '../master-4pyGz_8-.js';
1
+ import { F as FunctionThread, M as ModuleThread, S as StripAsync, a as Worker } from '../master-C26rzf58.js';
2
2
  import 'observable-fns';
3
3
  import '../observable-promise.js';
4
4
 
5
5
  /** A function that can be exposed from a worker thread. */
6
6
  type WorkerFunction = ((...args: any[]) => any) | (() => any);
7
7
  /** An object whose values are functions, representing a module exposed from a worker thread. */
8
- type WorkerModule<Keys extends string> = {
9
- [key in Keys]: WorkerFunction;
10
- };
8
+ type WorkerModule<Keys extends string> = Record<Keys, WorkerFunction>;
11
9
 
12
10
  type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & {
13
11
  somekeythatisneverusedinproductioncode123: 'magicmarker123';
@@ -208,15 +208,15 @@ function isTransferDescriptor(thing) {
208
208
  var debugMessages = DebugLogger("threads:master:messages");
209
209
  var nextJobUID = 1;
210
210
  var dedupe = (array) => [...new Set(array)];
211
- var isJobErrorMessage = (data) => data && data.type === "error" /* error */;
212
- var isJobResultMessage = (data) => data && data.type === "result" /* result */;
213
- var isJobStartMessage = (data) => data && data.type === "running" /* running */;
211
+ var isJobErrorMessage = (data) => data?.type === "error" /* error */;
212
+ var isJobResultMessage = (data) => data?.type === "result" /* result */;
213
+ var isJobStartMessage = (data) => data?.type === "running" /* running */;
214
214
  function createObservableForJob(worker, jobUID) {
215
215
  return new Observable2((observer) => {
216
216
  let asyncType;
217
217
  const messageHandler = ((event) => {
218
218
  debugMessages("Message from worker:", event.data);
219
- if (!event.data || event.data.uid !== jobUID) return;
219
+ if (event.data?.uid !== jobUID) return;
220
220
  if (isJobStartMessage(event.data)) {
221
221
  asyncType = event.data.resultType;
222
222
  } else if (isJobResultMessage(event.data)) {
@@ -237,7 +237,7 @@ function createObservableForJob(worker, jobUID) {
237
237
  }
238
238
  } else if (isJobErrorMessage(event.data)) {
239
239
  const error = deserialize(event.data.error);
240
- if (asyncType === "promise" || !asyncType) {
240
+ if (asyncType === "promise" || asyncType === void 0) {
241
241
  observer.error(error);
242
242
  } else {
243
243
  observer.error(error);
@@ -247,7 +247,7 @@ function createObservableForJob(worker, jobUID) {
247
247
  });
248
248
  worker.addEventListener("message", messageHandler);
249
249
  return () => {
250
- if (asyncType === "observable" || !asyncType) {
250
+ if (asyncType === "observable" || asyncType === void 0) {
251
251
  const cancelMessage = {
252
252
  type: "cancel" /* cancel */,
253
253
  uid: jobUID
@@ -311,9 +311,9 @@ function createProxyModule(worker, methodNames) {
311
311
  var debugMessages2 = DebugLogger2("threads:master:messages");
312
312
  var debugSpawn = DebugLogger2("threads:master:spawn");
313
313
  var debugThreadUtils = DebugLogger2("threads:master:thread-utils");
314
- var isInitMessage = (data) => data && data.type === "init";
315
- var isUncaughtErrorMessage = (data) => data && data.type === "uncaughtError";
316
- var initMessageTimeout = typeof process !== "undefined" && process.env !== void 0 && process.env.THREADS_WORKER_INIT_TIMEOUT ? Number.parseInt(process.env.THREADS_WORKER_INIT_TIMEOUT, 10) : 1e4;
314
+ var isInitMessage = (data) => data?.type === "init";
315
+ var isUncaughtErrorMessage = (data) => data?.type === "uncaughtError";
316
+ var initMessageTimeout = typeof process !== "undefined" && process.env?.THREADS_WORKER_INIT_TIMEOUT !== void 0 && process.env.THREADS_WORKER_INIT_TIMEOUT !== "" ? Number.parseInt(process.env.THREADS_WORKER_INIT_TIMEOUT, 10) : 1e4;
317
317
  async function withTimeout(promise, timeoutInMs, errorMessage) {
318
318
  let timeoutHandle;
319
319
  const timeout = new Promise((resolve, reject) => {
@@ -386,7 +386,7 @@ function setPrivateThreadProps(raw, worker, workerEvents, terminate) {
386
386
  }
387
387
  async function spawn(worker, options) {
388
388
  debugSpawn("Initializing new thread");
389
- const timeout = options && options.timeout ? options.timeout : initMessageTimeout;
389
+ const timeout = options?.timeout ?? initMessageTimeout;
390
390
  const initMessage = await withTimeout(
391
391
  receiveInitMessage(worker),
392
392
  timeout,