1 import type { AsyncResource
} from
'node:async_hooks'
2 import type { EventLoopUtilization
} from
'node:perf_hooks'
3 import type { MessagePort
, TransferListItem
} from
'node:worker_threads'
5 import type { KillBehavior
} from
'./worker/worker-options.js'
10 * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
12 export interface WorkerError
<Data
= unknown
> {
14 * Task function name triggering the error.
20 readonly message
: string
22 * Data triggering the error.
32 export interface TaskPerformance
{
38 * Task performance timestamp.
40 readonly timestamp
: number
44 readonly runTime
?: number
46 * Task event loop utilization.
48 readonly elu
?: EventLoopUtilization
52 * Worker task performance statistics computation settings.
56 export interface WorkerStatistics
{
58 * Whether the worker computes the task runtime or not.
60 readonly runTime
: boolean
62 * Whether the worker computes the task event loop utilization (ELU) or not.
68 * Message object that is passed as a task between main worker and worker.
70 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
73 export interface Task
<Data
= unknown
> {
77 readonly name
?: string
79 * Task input data that will be passed to the worker.
83 * Array of transferable objects.
85 readonly transferList
?: TransferListItem
[]
89 readonly timestamp
?: number
93 readonly taskId
?: string
97 * Message object that is passed between main worker and worker.
99 * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
100 * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
103 export interface MessageValue
<Data
= unknown
, ErrorData
= unknown
>
108 readonly workerId
?: number
112 readonly kill
?: KillBehavior
| true | 'success' | 'failure'
116 readonly workerError
?: WorkerError
<ErrorData
>
120 readonly taskPerformance
?: TaskPerformance
122 * Task function operation:
123 * - `'add'` - Add a task function.
124 * - `'remove'` - Remove a task function.
125 * - `'default'` - Set a task function as default.
127 readonly taskFunctionOperation
?: 'add' | 'remove' | 'default'
129 * Whether the task function operation is successful or not.
131 readonly taskFunctionOperationStatus
?: boolean
133 * Task function serialized to string.
135 readonly taskFunction
?: string
137 * Task function name.
139 readonly taskFunctionName
?: string
141 * Task function names.
143 readonly taskFunctionNames
?: string[]
145 * Whether the worker computes the given statistics or not.
147 readonly statistics
?: WorkerStatistics
149 * Whether the worker is ready or not.
151 readonly ready
?: boolean
153 * Whether the worker starts or stops its activity check.
155 readonly checkActive
?: boolean
159 readonly port
?: MessagePort
163 * An object holding the task execution response promise resolve/reject callbacks.
165 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
168 export interface PromiseResponseWrapper
<Response
= unknown
> {
170 * Resolve callback to fulfill the promise.
172 readonly resolve
: (value
: Response
| PromiseLike
<Response
>) => void
174 * Reject callback to reject the promise.
176 readonly reject
: (reason
?: unknown
) => void
178 * The worker node key executing the task.
180 readonly workerNodeKey
: number
182 * The asynchronous resource used to track the task execution.
184 readonly asyncResource
?: AsyncResource
187 export type Writable
<T
> = { -readonly [P
in keyof T
]: T
[P
] }