/// import type { TransferListItem } from 'worker_threads'; import type { Messenger } from '../messenger/index.js'; import type { WorkerOptions } from './misc.js'; import type { Awaitable } from './utilities.js'; /** * The base options for calling a task. These are the options for calling a task within * a service, and they are extended by `BaseWorkerOptions` for calling a task as its own * worker. */ export type CallOptions = { /** * The name of the task to call. Must be present on the set of definitions created within * the `define` function. */ name: Name; } & (Params['length'] extends 0 ? { params?: undefined; } : { /** * The parameters for the task in array form. */ params: Params; }); export type ServiceCallOptions = { /** * An optional array of {@link TransferListItem}s. See the * [Node.js documentation](https://nodejs.org/api/worker_threads.html#workerpostmessagevalue-transferlist) for more information. */ transferList?: readonly TransferListItem[]; } & CallOptions; /** * The base options that are available when spinning up either a task or a service. */ export type BaseWorkerOptions = { /** * An object containing *most* of the options available on the `Worker` constructor. See the * [Node.js documentation](https://nodejs.org/api/worker_threads.html#new-workerfilename-options) to * understand all of these options. */ options?: WorkerOptions; /** * Whether or not to treat the worker as priority over the others when being queued into the `pool`. */ priority?: boolean; /** * When `true`, [`worker.ref()`](https://nodejs.org/api/worker_threads.html#workerref) will be called. * When `false`, [`worker.unref()`](https://nodejs.org/api/worker_threads.html#workerunref) will be called. * Defaults to `true`. */ reffed?: boolean; /** * An array of {@link Messenger}s that should be accessible to the worker when it is running. */ messengers?: Messenger[]; /** * Whether or not environment variables should be shared between the parent thread and the child that will be created. Defaults to `true`. */ shareEnv?: boolean; }; /** * The options for spinning up a task worker. */ export type TaskWorkerOptions = CallOptions & BaseWorkerOptions; export type ExceptionHandlerContext = { /** * An `Error` object containing the error that was thrown in the worker. */ error: Error; /** * An asynchronous function that can be used to exit the service's process. */ terminate: (code?: ExitCode) => void; }; /** * The options for spinning up a service worker that can run multiple tasks. */ export type ServiceWorkerOptions = BaseWorkerOptions & { /** * An optional but recommended option that allows for the catching of uncaught exceptions * within the service. */ exceptionHandler?: ({ error, terminate }: ExceptionHandlerContext) => Awaitable; }; export type ExitCode = number;