@xylabs/threads 4.7.7 → 4.7.9

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.
@@ -0,0 +1,400 @@
1
+ /* eslint-disable import-x/export */
2
+ /* eslint-disable unicorn/no-thenable */
3
+
4
+ /* eslint-disable @typescript-eslint/member-ordering */
5
+ /* eslint-disable unicorn/no-array-reduce */
6
+ /* eslint-disable @typescript-eslint/no-explicit-any */
7
+ /* eslint-disable @typescript-eslint/no-namespace */
8
+
9
+ /// <reference lib="esnext" />
10
+
11
+ import DebugLogger from 'debug'
12
+ import {
13
+ multicast, Observable, Subject,
14
+ } from 'observable-fns'
15
+
16
+ import { allSettled } from '../ponyfills.ts'
17
+ import { defaultPoolSize } from './implementation.node.ts'
18
+ import type {
19
+ PoolEvent, QueuedTask, TaskRunFunction, WorkerDescriptor,
20
+ } from './pool-types.ts'
21
+ import { PoolEventType } from './pool-types.ts'
22
+ import { Thread } from './thread.ts'
23
+
24
+ export declare namespace Pool {
25
+ type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>
26
+ type EventType = PoolEventType
27
+ }
28
+
29
+ let nextPoolID = 1
30
+
31
+ function createArray(size: number): number[] {
32
+ const array: number[] = []
33
+ for (let index = 0; index < size; index++) {
34
+ array.push(index)
35
+ }
36
+ return array
37
+ }
38
+
39
+ function delay(ms: number) {
40
+ return new Promise(resolve => setTimeout(resolve, ms))
41
+ }
42
+
43
+ function flatMap<In, Out>(array: In[], mapper: (element: In) => Out[]): Out[] {
44
+ return array.reduce<Out[]>((flattened, element) => [...flattened, ...mapper(element)], [])
45
+ }
46
+
47
+ function slugify(text: string) {
48
+ return text.replaceAll(/\W/g, ' ').trim().replaceAll(/\s+/g, '-')
49
+ }
50
+
51
+ function spawnWorkers<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, count: number): Array<WorkerDescriptor<ThreadType>> {
52
+ return createArray(count).map(
53
+ (): WorkerDescriptor<ThreadType> => ({
54
+ init: spawnWorker(),
55
+ runningTasks: [],
56
+ }),
57
+ )
58
+ }
59
+
60
+ /**
61
+ * Thread pool managing a set of worker threads.
62
+ * Use it to queue tasks that are run on those threads with limited
63
+ * concurrency.
64
+ */
65
+ export interface Pool<ThreadType extends Thread> {
66
+ /**
67
+ * Returns a promise that resolves once the task queue is emptied.
68
+ * Promise will be rejected if any task fails.
69
+ *
70
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
71
+ */
72
+ completed(allowResolvingImmediately?: boolean): Promise<any>
73
+
74
+ /**
75
+ * Returns a promise that resolves once the task queue is emptied.
76
+ * Failing tasks will not cause the promise to be rejected.
77
+ *
78
+ * @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
79
+ */
80
+ settled(allowResolvingImmediately?: boolean): Promise<Error[]>
81
+
82
+ /**
83
+ * Returns an observable that yields pool events.
84
+ */
85
+ events(): Observable<PoolEvent<ThreadType>>
86
+
87
+ /**
88
+ * Queue a task and return a promise that resolves once the task has been dequeued,
89
+ * started and finished.
90
+ *
91
+ * @param task An async function that takes a thread instance and invokes it.
92
+ */
93
+ queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>
94
+
95
+ /**
96
+ * Terminate all pool threads.
97
+ *
98
+ * @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.
99
+ */
100
+ terminate(force?: boolean): Promise<void>
101
+ }
102
+
103
+ interface PoolOptions {
104
+ /** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */
105
+ concurrency?: number
106
+
107
+ /** Maximum no. of jobs to be queued for execution before throwing an error. */
108
+ maxQueuedJobs?: number
109
+
110
+ /** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */
111
+ name?: string
112
+
113
+ /** No. of worker threads to spawn and to be managed by the pool. */
114
+ size?: number
115
+ }
116
+
117
+ class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
118
+ static EventType = PoolEventType
119
+
120
+ private readonly debug: DebugLogger.Debugger
121
+ private readonly eventObservable: Observable<PoolEvent<ThreadType>>
122
+ private readonly options: PoolOptions
123
+ private readonly workers: Array<WorkerDescriptor<ThreadType>>
124
+
125
+ private readonly eventSubject = new Subject<PoolEvent<ThreadType>>()
126
+ private initErrors: Error[] = []
127
+ private isClosing = false
128
+ private nextTaskID = 1
129
+ private taskQueue: Array<QueuedTask<ThreadType, any>> = []
130
+
131
+ constructor(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) {
132
+ const options: PoolOptions = typeof optionsOrSize === 'number' ? { size: optionsOrSize } : optionsOrSize || {}
133
+
134
+ const { size = defaultPoolSize } = options
135
+
136
+ this.debug = DebugLogger(`threads:pool:${slugify(options.name || String(nextPoolID++))}`)
137
+ this.options = options
138
+ this.workers = spawnWorkers(spawnWorker, size)
139
+
140
+ this.eventObservable = multicast(Observable.from(this.eventSubject))
141
+
142
+ Promise.all(this.workers.map(worker => worker.init)).then(
143
+ () =>
144
+ this.eventSubject.next({
145
+ size: this.workers.length,
146
+ type: PoolEventType.initialized,
147
+ }),
148
+ (error) => {
149
+ this.debug('Error while initializing pool worker:', error)
150
+ this.eventSubject.error(error)
151
+ this.initErrors.push(error)
152
+ },
153
+ )
154
+ }
155
+
156
+ private findIdlingWorker(): WorkerDescriptor<ThreadType> | undefined {
157
+ const { concurrency = 1 } = this.options
158
+ return this.workers.find(worker => worker.runningTasks.length < concurrency)
159
+ }
160
+
161
+ private async runPoolTask(worker: WorkerDescriptor<ThreadType>, task: QueuedTask<ThreadType, any>) {
162
+ const workerID = this.workers.indexOf(worker) + 1
163
+
164
+ this.debug(`Running task #${task.id} on worker #${workerID}...`)
165
+ this.eventSubject.next({
166
+ taskID: task.id,
167
+ type: PoolEventType.taskStart,
168
+ workerID,
169
+ })
170
+
171
+ try {
172
+ const returnValue = await task.run(await worker.init)
173
+
174
+ this.debug(`Task #${task.id} completed successfully`)
175
+ this.eventSubject.next({
176
+ returnValue,
177
+ taskID: task.id,
178
+ type: PoolEventType.taskCompleted,
179
+ workerID,
180
+ })
181
+ } catch (ex) {
182
+ const error = ex as Error
183
+ this.debug(`Task #${task.id} failed`)
184
+ this.eventSubject.next({
185
+ error,
186
+ taskID: task.id,
187
+ type: PoolEventType.taskFailed,
188
+ workerID,
189
+ })
190
+ }
191
+ }
192
+
193
+ private run(worker: WorkerDescriptor<ThreadType>, task: QueuedTask<ThreadType, any>) {
194
+ const runPromise = (async () => {
195
+ const removeTaskFromWorkersRunningTasks = () => {
196
+ worker.runningTasks = worker.runningTasks.filter(someRunPromise => someRunPromise !== runPromise)
197
+ }
198
+
199
+ // Defer task execution by one tick to give handlers time to subscribe
200
+ await delay(0)
201
+
202
+ try {
203
+ await this.runPoolTask(worker, task)
204
+ } finally {
205
+ removeTaskFromWorkersRunningTasks()
206
+
207
+ if (!this.isClosing) {
208
+ this.scheduleWork()
209
+ }
210
+ }
211
+ })()
212
+
213
+ worker.runningTasks.push(runPromise)
214
+ }
215
+
216
+ private scheduleWork() {
217
+ this.debug('Attempt de-queueing a task in order to run it...')
218
+
219
+ const availableWorker = this.findIdlingWorker()
220
+ if (!availableWorker) return
221
+
222
+ const nextTask = this.taskQueue.shift()
223
+ if (!nextTask) {
224
+ this.debug('Task queue is empty')
225
+ this.eventSubject.next({ type: PoolEventType.taskQueueDrained })
226
+ return
227
+ }
228
+
229
+ this.run(availableWorker, nextTask)
230
+ }
231
+
232
+ private taskCompletion(taskID: number) {
233
+ return new Promise<any>((resolve, reject) => {
234
+ const eventSubscription = this.events().subscribe((event) => {
235
+ if (event.type === PoolEventType.taskCompleted && event.taskID === taskID) {
236
+ eventSubscription.unsubscribe()
237
+ resolve(event.returnValue)
238
+ } else if (event.type === PoolEventType.taskFailed && event.taskID === taskID) {
239
+ eventSubscription.unsubscribe()
240
+ reject(event.error)
241
+ } else if (event.type === PoolEventType.terminated) {
242
+ eventSubscription.unsubscribe()
243
+ reject(new Error('Pool has been terminated before task was run.'))
244
+ }
245
+ })
246
+ })
247
+ }
248
+
249
+ async settled(allowResolvingImmediately: boolean = false): Promise<Error[]> {
250
+ const getCurrentlyRunningTasks = () => flatMap(this.workers, worker => worker.runningTasks)
251
+
252
+ const taskFailures: Error[] = []
253
+
254
+ const failureSubscription = this.eventObservable.subscribe((event) => {
255
+ if (event.type === PoolEventType.taskFailed) {
256
+ taskFailures.push(event.error)
257
+ }
258
+ })
259
+
260
+ if (this.initErrors.length > 0) {
261
+ throw this.initErrors[0]
262
+ }
263
+ if (allowResolvingImmediately && this.taskQueue.length === 0) {
264
+ await allSettled(getCurrentlyRunningTasks())
265
+ return taskFailures
266
+ }
267
+
268
+ await new Promise<void>((resolve, reject) => {
269
+ const subscription = this.eventObservable.subscribe({
270
+ error: reject,
271
+ next(event) {
272
+ if (event.type === PoolEventType.taskQueueDrained) {
273
+ subscription.unsubscribe()
274
+ resolve(void 0)
275
+ }
276
+ }, // make a pool-wide error reject the completed() result promise
277
+ })
278
+ })
279
+
280
+ await allSettled(getCurrentlyRunningTasks())
281
+ failureSubscription.unsubscribe()
282
+
283
+ return taskFailures
284
+ }
285
+
286
+ async completed(allowResolvingImmediately: boolean = false) {
287
+ const settlementPromise = this.settled(allowResolvingImmediately)
288
+
289
+ const earlyExitPromise = new Promise<Error[]>((resolve, reject) => {
290
+ const subscription = this.eventObservable.subscribe({
291
+ error: reject,
292
+ next(event) {
293
+ if (event.type === PoolEventType.taskQueueDrained) {
294
+ subscription.unsubscribe()
295
+ resolve(settlementPromise)
296
+ } else if (event.type === PoolEventType.taskFailed) {
297
+ subscription.unsubscribe()
298
+ reject(event.error)
299
+ }
300
+ }, // make a pool-wide error reject the completed() result promise
301
+ })
302
+ })
303
+
304
+ const errors = await Promise.race([settlementPromise, earlyExitPromise])
305
+
306
+ if (errors.length > 0) {
307
+ throw errors[0]
308
+ }
309
+ }
310
+
311
+ events() {
312
+ return this.eventObservable
313
+ }
314
+
315
+ queue(taskFunction: TaskRunFunction<ThreadType, any>) {
316
+ const { maxQueuedJobs = Number.POSITIVE_INFINITY } = this.options
317
+
318
+ if (this.isClosing) {
319
+ throw new Error('Cannot schedule pool tasks after terminate() has been called.')
320
+ }
321
+ if (this.initErrors.length > 0) {
322
+ throw this.initErrors[0]
323
+ }
324
+
325
+ const taskID = this.nextTaskID++
326
+ const taskCompletion = this.taskCompletion(taskID)
327
+
328
+ taskCompletion.catch((error) => {
329
+ // Prevent unhandled rejections here as we assume the user will use
330
+ // `pool.completed()`, `pool.settled()` or `task.catch()` to handle errors
331
+ this.debug(`Task #${taskID} errored:`, error)
332
+ })
333
+
334
+ const task: QueuedTask<ThreadType, any> = {
335
+ cancel: () => {
336
+ if (!this.taskQueue.includes(task)) return
337
+ this.taskQueue = this.taskQueue.filter(someTask => someTask !== task)
338
+ this.eventSubject.next({
339
+ taskID: task.id,
340
+ type: PoolEventType.taskCanceled,
341
+ })
342
+ },
343
+ id: taskID,
344
+ run: taskFunction,
345
+ then: taskCompletion.then.bind(taskCompletion),
346
+ }
347
+
348
+ if (this.taskQueue.length >= maxQueuedJobs) {
349
+ throw new Error(
350
+ 'Maximum number of pool tasks queued. Refusing to queue another one.\n'
351
+ + 'This usually happens for one of two reasons: We are either at peak '
352
+ + "workload right now or some tasks just won't finish, thus blocking the pool.",
353
+ )
354
+ }
355
+
356
+ this.debug(`Queueing task #${task.id}...`)
357
+ this.taskQueue.push(task)
358
+
359
+ this.eventSubject.next({
360
+ taskID: task.id,
361
+ type: PoolEventType.taskQueued,
362
+ })
363
+
364
+ this.scheduleWork()
365
+ return task
366
+ }
367
+
368
+ async terminate(force?: boolean) {
369
+ this.isClosing = true
370
+ if (!force) {
371
+ await this.completed(true)
372
+ }
373
+ this.eventSubject.next({
374
+ remainingQueue: [...this.taskQueue],
375
+ type: PoolEventType.terminated,
376
+ })
377
+ this.eventSubject.complete()
378
+ await Promise.all(this.workers.map(async worker => Thread.terminate(await worker.init)))
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
384
+ */
385
+ function PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions) {
386
+ // The function exists only so we don't need to use `new` to create a pool (we still can, though).
387
+ // If the Pool is a class or not is an implementation detail that should not concern the user.
388
+ return new WorkerPool(spawnWorker, optionsOrSize)
389
+ }
390
+
391
+ ;(PoolConstructor as any).EventType = PoolEventType
392
+
393
+ /**
394
+ * Thread pool constructor. Creates a new pool and spawns its worker threads.
395
+ */
396
+ export const Pool = PoolConstructor as typeof PoolConstructor & { EventType: typeof PoolEventType }
397
+
398
+ export type { PoolEvent, QueuedTask } from './pool-types.ts'
399
+ export { PoolEventType } from './pool-types.ts'
400
+ export { Thread } from './thread.ts'
@@ -0,0 +1,83 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /* eslint-disable @typescript-eslint/member-ordering */
3
+ import type { Thread } from './thread.ts'
4
+
5
+ /** Pool event type. Specifies the type of each `PoolEvent`. */
6
+ export enum PoolEventType {
7
+ initialized = 'initialized',
8
+ taskCanceled = 'taskCanceled',
9
+ taskCompleted = 'taskCompleted',
10
+ taskFailed = 'taskFailed',
11
+ taskQueued = 'taskQueued',
12
+ taskQueueDrained = 'taskQueueDrained',
13
+ taskStart = 'taskStart',
14
+ terminated = 'terminated',
15
+ }
16
+
17
+ export type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>
18
+
19
+ /** Pool event. Subscribe to those events using `pool.events()`. Useful for debugging. */
20
+ export type PoolEvent<ThreadType extends Thread> =
21
+ | {
22
+ type: PoolEventType.initialized
23
+ size: number
24
+ }
25
+ | {
26
+ type: PoolEventType.taskQueued
27
+ taskID: number
28
+ }
29
+ | {
30
+ type: PoolEventType.taskQueueDrained
31
+ }
32
+ | {
33
+ type: PoolEventType.taskStart
34
+ taskID: number
35
+ workerID: number
36
+ }
37
+ | {
38
+ type: PoolEventType.taskCompleted
39
+ returnValue: any
40
+ taskID: number
41
+ workerID: number
42
+ }
43
+ | {
44
+ type: PoolEventType.taskFailed
45
+ error: Error
46
+ taskID: number
47
+ workerID: number
48
+ }
49
+ | {
50
+ type: PoolEventType.taskCanceled
51
+ taskID: number
52
+ }
53
+ | {
54
+ type: PoolEventType.terminated
55
+ remainingQueue: Array<QueuedTask<ThreadType, any>>
56
+ }
57
+
58
+ export interface WorkerDescriptor<ThreadType extends Thread> {
59
+ init: Promise<ThreadType>
60
+ runningTasks: Array<Promise<any>>
61
+ }
62
+
63
+ /**
64
+ * Task that has been `pool.queued()`-ed.
65
+ */
66
+ export interface QueuedTask<ThreadType extends Thread, Return> {
67
+ /** @private */
68
+ id: number
69
+
70
+ /** @private */
71
+ run: TaskRunFunction<ThreadType, Return>
72
+
73
+ /**
74
+ * Queued tasks can be cancelled until the pool starts running them on a worker thread.
75
+ */
76
+ cancel(): void
77
+
78
+ /**
79
+ * `QueuedTask` is thenable, so you can `await` it.
80
+ * Resolves when the task has successfully been executed. Rejects if the task fails.
81
+ */
82
+ then: Promise<Return>['then']
83
+ }
@@ -0,0 +1,11 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ import { Worker as WorkerImplementation } from './index-node.ts'
4
+
5
+ declare const window: any
6
+
7
+ if (typeof globalThis !== 'undefined') {
8
+ ;(globalThis as any).Worker = WorkerImplementation
9
+ } else if (window !== undefined) {
10
+ ;(window as any).Worker = WorkerImplementation
11
+ }
@@ -0,0 +1,172 @@
1
+ /* eslint-disable import-x/no-internal-modules */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ /* eslint-disable @typescript-eslint/no-floating-promises */
4
+ import DebugLogger from 'debug'
5
+ import { Observable } from 'observable-fns'
6
+
7
+ import { deserialize } from '../common.ts'
8
+ import { createPromiseWithResolver } from '../promise.ts'
9
+ import {
10
+ $errors, $events, $terminate, $worker,
11
+ } from '../symbols.ts'
12
+ import type {
13
+ FunctionThread,
14
+ ModuleThread,
15
+ PrivateThreadProps,
16
+ StripAsync,
17
+ Worker as WorkerType,
18
+ WorkerEvent,
19
+ WorkerInternalErrorEvent,
20
+ WorkerMessageEvent,
21
+ WorkerTerminationEvent,
22
+ } from '../types/master.ts'
23
+ import { WorkerEventType } from '../types/master.ts'
24
+ import type { WorkerInitMessage, WorkerUncaughtErrorMessage } from '../types/messages.ts'
25
+ import type { WorkerFunction, WorkerModule } from '../types/worker.ts'
26
+ import { createProxyFunction, createProxyModule } from './invocation-proxy.ts'
27
+
28
+ type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & { somekeythatisneverusedinproductioncode123: 'magicmarker123' }
29
+ type ArbitraryThreadType = FunctionThread<any, any> & ModuleThread<any>
30
+
31
+ export type ExposedToThreadType<Exposed extends WorkerFunction | WorkerModule<any>> =
32
+ Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType
33
+ : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>>
34
+ : Exposed extends WorkerModule<any> ? ModuleThread<Exposed>
35
+ : never
36
+
37
+ const debugMessages = DebugLogger('threads:master:messages')
38
+ const debugSpawn = DebugLogger('threads:master:spawn')
39
+ const debugThreadUtils = DebugLogger('threads:master:thread-utils')
40
+
41
+ const isInitMessage = (data: any): data is WorkerInitMessage => data && data.type === ('init' as const)
42
+ const isUncaughtErrorMessage = (data: any): data is WorkerUncaughtErrorMessage => data && data.type === ('uncaughtError' as const)
43
+
44
+ const initMessageTimeout
45
+ = typeof process !== 'undefined' && process.env !== undefined && process.env.THREADS_WORKER_INIT_TIMEOUT
46
+ ? Number.parseInt(process.env.THREADS_WORKER_INIT_TIMEOUT, 10)
47
+ : 10_000
48
+
49
+ async function withTimeout<T>(promise: Promise<T>, timeoutInMs: number, errorMessage: string): Promise<T> {
50
+ let timeoutHandle: any
51
+
52
+ const timeout = new Promise<never>((resolve, reject) => {
53
+ timeoutHandle = setTimeout(() => reject(new Error(errorMessage)), timeoutInMs)
54
+ })
55
+ const result = await Promise.race([promise, timeout])
56
+
57
+ clearTimeout(timeoutHandle)
58
+ return result
59
+ }
60
+
61
+ function receiveInitMessage(worker: WorkerType): Promise<WorkerInitMessage> {
62
+ return new Promise((resolve, reject) => {
63
+ const messageHandler = ((event: MessageEvent) => {
64
+ debugMessages('Message from worker before finishing initialization:', event.data)
65
+ if (isInitMessage(event.data)) {
66
+ worker.removeEventListener('message', messageHandler)
67
+ resolve(event.data)
68
+ } else if (isUncaughtErrorMessage(event.data)) {
69
+ worker.removeEventListener('message', messageHandler)
70
+ reject(deserialize(event.data.error))
71
+ }
72
+ }) as EventListener
73
+ worker.addEventListener('message', messageHandler)
74
+ })
75
+ }
76
+
77
+ function createEventObservable(worker: WorkerType, workerTermination: Promise<any>): Observable<WorkerEvent> {
78
+ return new Observable<WorkerEvent>((observer) => {
79
+ const messageHandler = ((messageEvent: MessageEvent) => {
80
+ const workerEvent: WorkerMessageEvent<any> = {
81
+ data: messageEvent.data,
82
+ type: WorkerEventType.message,
83
+ }
84
+ observer.next(workerEvent)
85
+ }) as EventListener
86
+ const rejectionHandler = ((errorEvent: PromiseRejectionEvent) => {
87
+ debugThreadUtils('Unhandled promise rejection event in thread:', errorEvent)
88
+ const workerEvent: WorkerInternalErrorEvent = {
89
+ error: new Error(errorEvent.reason),
90
+ type: WorkerEventType.internalError,
91
+ }
92
+ observer.next(workerEvent)
93
+ }) as EventListener
94
+ worker.addEventListener('message', messageHandler)
95
+ worker.addEventListener('unhandledrejection', rejectionHandler)
96
+
97
+ workerTermination.then(() => {
98
+ const terminationEvent: WorkerTerminationEvent = { type: WorkerEventType.termination }
99
+ worker.removeEventListener('message', messageHandler)
100
+ worker.removeEventListener('unhandledrejection', rejectionHandler)
101
+ observer.next(terminationEvent)
102
+ observer.complete()
103
+ })
104
+ })
105
+ }
106
+
107
+ function createTerminator(worker: WorkerType): { terminate: () => Promise<void>; termination: Promise<void> } {
108
+ const [termination, resolver] = createPromiseWithResolver<void>()
109
+ const terminate = async () => {
110
+ debugThreadUtils('Terminating worker')
111
+ // Newer versions of worker_threads workers return a promise
112
+ await worker.terminate()
113
+ resolver()
114
+ }
115
+ return { terminate, termination }
116
+ }
117
+
118
+ function setPrivateThreadProps<T>(
119
+ raw: T,
120
+ worker: WorkerType,
121
+ workerEvents: Observable<WorkerEvent>,
122
+ terminate: () => Promise<void>,
123
+ ): T & PrivateThreadProps {
124
+ const workerErrors = workerEvents
125
+ .filter(event => event.type === WorkerEventType.internalError)
126
+ .map(errorEvent => (errorEvent as WorkerInternalErrorEvent).error)
127
+
128
+ return Object.assign(raw as any, {
129
+ [$errors]: workerErrors,
130
+ [$events]: workerEvents,
131
+ [$terminate]: terminate,
132
+ [$worker]: worker,
133
+ })
134
+ }
135
+
136
+ /**
137
+ * Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
138
+ * abstraction layer to provide the transparent API and verifies that
139
+ * the worker has initialized successfully.
140
+ *
141
+ * @param worker Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
142
+ * @param [options]
143
+ * @param [options.timeout] Init message timeout. Default: 10000 or set by environment variable.
144
+ */
145
+ export async function spawn<Exposed extends WorkerFunction | WorkerModule<any> = ArbitraryWorkerInterface>(
146
+ worker: WorkerType,
147
+ options?: { timeout?: number },
148
+ ): Promise<ExposedToThreadType<Exposed>> {
149
+ debugSpawn('Initializing new thread')
150
+
151
+ const timeout = options && options.timeout ? options.timeout : initMessageTimeout
152
+ const initMessage = await withTimeout(
153
+ receiveInitMessage(worker),
154
+ timeout,
155
+ `Timeout: Did not receive an init message from worker after ${timeout}ms. Make sure the worker calls expose().`,
156
+ )
157
+ const exposed = initMessage.exposed
158
+
159
+ const { termination, terminate } = createTerminator(worker)
160
+ const events = createEventObservable(worker, termination)
161
+
162
+ if (exposed.type === 'function') {
163
+ const proxy = createProxyFunction(worker)
164
+ return setPrivateThreadProps(proxy, worker, events, terminate) as ExposedToThreadType<Exposed>
165
+ } else if (exposed.type === 'module') {
166
+ const proxy = createProxyModule(worker, exposed.methods)
167
+ return setPrivateThreadProps(proxy, worker, events, terminate) as ExposedToThreadType<Exposed>
168
+ } else {
169
+ const type = (exposed as WorkerInitMessage['exposed']).type
170
+ throw new Error(`Worker init message states unexpected type of expose(): ${type}`)
171
+ }
172
+ }
@@ -0,0 +1,29 @@
1
+ /* eslint-disable import-x/no-internal-modules */
2
+ import type { Observable } from 'observable-fns'
3
+
4
+ import {
5
+ $errors, $events, $terminate,
6
+ } from '../symbols.ts'
7
+ import type { Thread as ThreadType, WorkerEvent } from '../types/master.ts'
8
+
9
+ function fail(message: string): never {
10
+ throw new Error(message)
11
+ }
12
+
13
+ export type Thread = ThreadType
14
+
15
+ /** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */
16
+ export const Thread = {
17
+ /** Return an observable that can be used to subscribe to all errors happening in the thread. */
18
+ errors<ThreadT extends ThreadType>(thread: ThreadT): Observable<Error> {
19
+ return thread[$errors] || fail('Error observable not found. Make sure to pass a thread instance as returned by the spawn() promise.')
20
+ },
21
+ /** Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. */
22
+ events<ThreadT extends ThreadType>(thread: ThreadT): Observable<WorkerEvent> {
23
+ return thread[$events] || fail('Events observable not found. Make sure to pass a thread instance as returned by the spawn() promise.')
24
+ },
25
+ /** Terminate a thread. Remember to terminate every thread when you are done using it. */
26
+ terminate<ThreadT extends ThreadType>(thread: ThreadT) {
27
+ return thread[$terminate]()
28
+ },
29
+ }