1 import type { AsyncResource
} from
'node:async_hooks'
2 import type { EventLoopUtilization
} from
'node:perf_hooks'
3 import type { MessagePort
, Transferable
} from
'node:worker_threads'
5 import type { WorkerChoiceStrategy
} from
'./pools/selection-strategies/selection-strategies-types.js'
6 import type { KillBehavior
} from
'./worker/worker-options.js'
9 * Message object that is passed between main worker and worker.
10 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
11 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
14 export interface MessageValue
<Data
= unknown
, ErrorData
= unknown
>
17 * Whether the worker starts or stops its activity check.
19 readonly checkActive
?: boolean
23 readonly kill
?: 'failure' | 'success' | KillBehavior
| true
27 readonly port
?: MessagePort
29 * Whether the worker is ready or not.
31 readonly ready
?: boolean
33 * Whether the worker computes the given statistics or not.
35 readonly statistics
?: WorkerStatistics
37 * Task function serialized to string.
39 readonly taskFunction
?: string
41 * Task function operation:
42 * - `'add'` - Add a task function.
43 * - `'remove'` - Remove a task function.
44 * - `'default'` - Set a task function as default.
46 readonly taskFunctionOperation
?: 'add' | 'default' | 'remove'
48 * Whether the task function operation is successful or not.
50 readonly taskFunctionOperationStatus
?: boolean
52 * Task function properties.
54 readonly taskFunctionProperties
?: TaskFunctionProperties
56 * Task functions properties.
58 readonly taskFunctionsProperties
?: TaskFunctionProperties
[]
61 * - `'abort'` - Abort a task.
63 readonly taskOperation
?: 'abort'
67 readonly taskPerformance
?: TaskPerformance
71 readonly workerError
?: WorkerError
<ErrorData
>
75 readonly workerId
?: number
79 * An object holding the task execution response promise resolve/reject callbacks.
80 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
83 export interface PromiseResponseWrapper
<Response
= unknown
> {
85 * The task abort signal.
87 readonly abortSignal
?: AbortSignal
89 * The asynchronous resource used to track the task execution.
91 readonly asyncResource
?: AsyncResource
93 * Reject callback to reject the promise.
95 readonly reject
: (reason
?: unknown
) => void
97 * Resolve callback to fulfill the promise.
99 readonly resolve
: (value
: PromiseLike
<Response
> | Response
) => void
101 * The worker node key executing the task.
103 readonly workerNodeKey
: number
107 * Message object that is passed as a task between main worker and worker.
108 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
111 export interface Task
<Data
= unknown
> {
113 * Whether the task is abortable or not.
115 readonly abortable
?: boolean
117 * Task input data that will be passed to the worker.
123 readonly name
?: string
125 * Task priority. Lower values have higher priority.
128 readonly priority
?: number
130 * Task worker choice strategy.
132 readonly strategy
?: WorkerChoiceStrategy
136 readonly taskId
?: `${string}-${string}-${string}-${string}-${string}`
140 readonly timestamp
?: number
142 * Array of transferable objects.
144 readonly transferList
?: readonly Transferable
[]
148 * Task function properties.
150 export interface TaskFunctionProperties
{
152 * Task function name.
154 readonly name
: string
156 * Task function priority. Lower values have higher priority.
158 readonly priority
?: number
160 * Task function worker choice strategy.
162 readonly strategy
?: WorkerChoiceStrategy
169 export interface TaskPerformance
{
171 * Task event loop utilization.
173 readonly elu
?: EventLoopUtilization
177 readonly name
: string
181 readonly runTime
?: number
183 * Task performance timestamp.
185 readonly timestamp
: number
190 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
192 export interface WorkerError
<Data
= unknown
> {
194 * Whether the error is an abort error or not.
196 readonly aborted
: boolean
198 * Data triggering the error.
204 readonly error
?: Error
208 readonly message
: string
210 * Task function name triggering the error.
212 readonly name
?: string
216 readonly stack
?: string
220 * Worker task performance statistics computation settings.
223 export interface WorkerStatistics
{
225 * Whether the worker computes the task event loop utilization (ELU) or not.
227 readonly elu
: boolean
229 * Whether the worker computes the task runtime or not.
231 readonly runTime
: boolean
235 * Remove readonly modifier from all properties of T.
236 * @typeParam T - Type to remove readonly modifier.
239 export type Writable
<T
> = { -readonly [P
in keyof T
]: T
[P
] }