1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import type {ForkOptions} from 'child_process';
|
10 | import type {ResourceLimits} from 'worker_threads';
|
11 |
|
12 | declare const CHILD_MESSAGE_CALL = 1;
|
13 |
|
14 | declare const CHILD_MESSAGE_CALL_SETUP = 4;
|
15 |
|
16 | declare const CHILD_MESSAGE_END = 2;
|
17 |
|
18 | declare const CHILD_MESSAGE_INITIALIZE = 0;
|
19 |
|
20 | declare const CHILD_MESSAGE_MEM_USAGE = 3;
|
21 |
|
22 | declare type ChildMessage =
|
23 | | ChildMessageInitialize
|
24 | | ChildMessageCall
|
25 | | ChildMessageEnd
|
26 | | ChildMessageMemUsage
|
27 | | ChildMessageCallSetup;
|
28 |
|
29 | declare type ChildMessageCall = [
|
30 | type: typeof CHILD_MESSAGE_CALL,
|
31 | isProcessed: boolean,
|
32 | methodName: string,
|
33 | args: Array<unknown>,
|
34 | ];
|
35 |
|
36 | declare type ChildMessageCallSetup = [type: typeof CHILD_MESSAGE_CALL_SETUP];
|
37 |
|
38 | declare type ChildMessageEnd = [
|
39 | type: typeof CHILD_MESSAGE_END,
|
40 | isProcessed: boolean,
|
41 | ];
|
42 |
|
43 | declare type ChildMessageInitialize = [
|
44 | type: typeof CHILD_MESSAGE_INITIALIZE,
|
45 | isProcessed: boolean,
|
46 | fileName: string,
|
47 | setupArgs: Array<unknown>,
|
48 | workerId: string | undefined,
|
49 | ];
|
50 |
|
51 | declare type ChildMessageMemUsage = [type: typeof CHILD_MESSAGE_MEM_USAGE];
|
52 |
|
53 | declare type ComputeTaskPriorityCallback = (
|
54 | method: string,
|
55 | ...args: Array<unknown>
|
56 | ) => number;
|
57 |
|
58 | declare type ExcludeReservedKeys<K> = Exclude<K, ReservedKeys>;
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | export declare class FifoQueue implements TaskQueue {
|
66 | private _workerQueues;
|
67 | private readonly _sharedQueue;
|
68 | enqueue(task: QueueChildMessage, workerId?: number): void;
|
69 | dequeue(workerId: number): QueueChildMessage | null;
|
70 | }
|
71 |
|
72 | declare type FunctionLike = (...args: any) => unknown;
|
73 |
|
74 | declare type HeapItem = {
|
75 | priority: number;
|
76 | };
|
77 |
|
78 | export declare type JestWorkerFarm<T extends Record<string, unknown>> =
|
79 | Worker_2 & WorkerModule<T>;
|
80 |
|
81 | export declare function messageParent(
|
82 | message: unknown,
|
83 | parentProcess?: NodeJS.Process,
|
84 | ): void;
|
85 |
|
86 | declare type MethodLikeKeys<T> = {
|
87 | [K in keyof T]: T[K] extends FunctionLike ? K : never;
|
88 | }[keyof T];
|
89 |
|
90 | declare class MinHeap<TItem extends HeapItem> {
|
91 | private readonly _heap;
|
92 | peek(): TItem | null;
|
93 | add(item: TItem): void;
|
94 | poll(): TItem | null;
|
95 | }
|
96 |
|
97 | declare type OnCustomMessage = (message: Array<unknown> | unknown) => void;
|
98 |
|
99 | declare type OnEnd = (err: Error | null, result: unknown) => void;
|
100 |
|
101 | declare type OnStart = (worker: WorkerInterface) => void;
|
102 |
|
103 | declare type OnStateChangeHandler = (
|
104 | state: WorkerStates,
|
105 | oldState: WorkerStates,
|
106 | ) => void;
|
107 |
|
108 | declare type PoolExitResult = {
|
109 | forceExited: boolean;
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | export declare class PriorityQueue implements TaskQueue {
|
122 | private readonly _computePriority;
|
123 | private _queue;
|
124 | private readonly _sharedQueue;
|
125 | constructor(_computePriority: ComputeTaskPriorityCallback);
|
126 | enqueue(task: QueueChildMessage, workerId?: number): void;
|
127 | _enqueue(task: QueueChildMessage, queue: MinHeap<QueueItem>): void;
|
128 | dequeue(workerId: number): QueueChildMessage | null;
|
129 | _getWorkerQueue(workerId: number): MinHeap<QueueItem>;
|
130 | }
|
131 |
|
132 | export declare interface PromiseWithCustomMessage<T> extends Promise<T> {
|
133 | UNSTABLE_onCustomMessage?: (listener: OnCustomMessage) => () => void;
|
134 | }
|
135 |
|
136 | declare type Promisify<T extends FunctionLike> = ReturnType<T> extends Promise<
|
137 | infer R
|
138 | >
|
139 | ? (...args: Parameters<T>) => Promise<R>
|
140 | : (...args: Parameters<T>) => Promise<ReturnType<T>>;
|
141 |
|
142 | declare type QueueChildMessage = {
|
143 | request: ChildMessageCall;
|
144 | onStart: OnStart;
|
145 | onEnd: OnEnd;
|
146 | onCustomMessage: OnCustomMessage;
|
147 | };
|
148 |
|
149 | declare type QueueItem = {
|
150 | task: QueueChildMessage;
|
151 | priority: number;
|
152 | };
|
153 |
|
154 | declare type ReservedKeys =
|
155 | | 'end'
|
156 | | 'getStderr'
|
157 | | 'getStdout'
|
158 | | 'setup'
|
159 | | 'teardown';
|
160 |
|
161 | export declare interface TaskQueue {
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | enqueue(task: QueueChildMessage, workerId?: number): void;
|
170 | |
171 |
|
172 |
|
173 |
|
174 | dequeue(workerId: number): QueueChildMessage | null;
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | declare class Worker_2 {
|
203 | private _ending;
|
204 | private readonly _farm;
|
205 | private readonly _options;
|
206 | private readonly _workerPool;
|
207 | constructor(workerPath: string | URL, options?: WorkerFarmOptions);
|
208 | private _bindExposedWorkerMethods;
|
209 | private _callFunctionWithArgs;
|
210 | getStderr(): NodeJS.ReadableStream;
|
211 | getStdout(): NodeJS.ReadableStream;
|
212 | start(): Promise<void>;
|
213 | end(): Promise<PoolExitResult>;
|
214 | }
|
215 | export {Worker_2 as Worker};
|
216 |
|
217 | declare type WorkerCallback = (
|
218 | workerId: number,
|
219 | request: ChildMessage,
|
220 | onStart: OnStart,
|
221 | onEnd: OnEnd,
|
222 | onCustomMessage: OnCustomMessage,
|
223 | ) => void;
|
224 |
|
225 | declare enum WorkerEvents {
|
226 | STATE_CHANGE = 'state-change',
|
227 | }
|
228 |
|
229 | export declare type WorkerFarmOptions = {
|
230 | computeWorkerKey?: (method: string, ...args: Array<unknown>) => string | null;
|
231 | enableWorkerThreads?: boolean;
|
232 | exposedMethods?: ReadonlyArray<string>;
|
233 | forkOptions?: ForkOptions;
|
234 | maxRetries?: number;
|
235 | numWorkers?: number;
|
236 | resourceLimits?: ResourceLimits;
|
237 | setupArgs?: Array<unknown>;
|
238 | taskQueue?: TaskQueue;
|
239 | WorkerPool?: new (
|
240 | workerPath: string,
|
241 | options?: WorkerPoolOptions,
|
242 | ) => WorkerPoolInterface;
|
243 | workerSchedulingPolicy?: WorkerSchedulingPolicy;
|
244 | idleMemoryLimit?: number;
|
245 | };
|
246 |
|
247 | declare interface WorkerInterface {
|
248 | get state(): WorkerStates;
|
249 | send(
|
250 | request: ChildMessage,
|
251 | onProcessStart: OnStart,
|
252 | onProcessEnd: OnEnd,
|
253 | onCustomMessage: OnCustomMessage,
|
254 | ): void;
|
255 | waitForExit(): Promise<void>;
|
256 | forceExit(): void;
|
257 | getWorkerId(): number;
|
258 | getStderr(): NodeJS.ReadableStream | null;
|
259 | getStdout(): NodeJS.ReadableStream | null;
|
260 | |
261 |
|
262 |
|
263 | getWorkerSystemId(): number;
|
264 | getMemoryUsage(): Promise<number | null>;
|
265 | |
266 |
|
267 |
|
268 | isWorkerRunning(): boolean;
|
269 | |
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | waitForWorkerReady(): Promise<void>;
|
277 | }
|
278 |
|
279 | declare type WorkerModule<T> = {
|
280 | [K in keyof T as Extract<
|
281 | ExcludeReservedKeys<K>,
|
282 | MethodLikeKeys<T>
|
283 | >]: T[K] extends FunctionLike ? Promisify<T[K]> : never;
|
284 | };
|
285 |
|
286 | declare type WorkerOptions_2 = {
|
287 | forkOptions: ForkOptions;
|
288 | resourceLimits: ResourceLimits;
|
289 | setupArgs: Array<unknown>;
|
290 | maxRetries: number;
|
291 | workerId: number;
|
292 | workerData?: unknown;
|
293 | workerPath: string;
|
294 | |
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 | idleMemoryLimit?: number;
|
304 | |
305 |
|
306 |
|
307 |
|
308 | childWorkerPath?: string;
|
309 | |
310 |
|
311 |
|
312 |
|
313 | silent?: boolean;
|
314 | |
315 |
|
316 |
|
317 | on?: {
|
318 | [WorkerEvents.STATE_CHANGE]:
|
319 | | OnStateChangeHandler
|
320 | | ReadonlyArray<OnStateChangeHandler>;
|
321 | };
|
322 | };
|
323 |
|
324 | export declare interface WorkerPoolInterface {
|
325 | getStderr(): NodeJS.ReadableStream;
|
326 | getStdout(): NodeJS.ReadableStream;
|
327 | getWorkers(): Array<WorkerInterface>;
|
328 | createWorker(options: WorkerOptions_2): WorkerInterface;
|
329 | send: WorkerCallback;
|
330 | start(): Promise<void>;
|
331 | end(): Promise<PoolExitResult>;
|
332 | }
|
333 |
|
334 | export declare type WorkerPoolOptions = {
|
335 | setupArgs: Array<unknown>;
|
336 | forkOptions: ForkOptions;
|
337 | resourceLimits: ResourceLimits;
|
338 | maxRetries: number;
|
339 | numWorkers: number;
|
340 | enableWorkerThreads: boolean;
|
341 | idleMemoryLimit?: number;
|
342 | };
|
343 |
|
344 | declare type WorkerSchedulingPolicy = 'round-robin' | 'in-order';
|
345 |
|
346 | declare enum WorkerStates {
|
347 | STARTING = 'starting',
|
348 | OK = 'ok',
|
349 | OUT_OF_MEMORY = 'oom',
|
350 | RESTARTING = 'restarting',
|
351 | SHUTTING_DOWN = 'shutting-down',
|
352 | SHUT_DOWN = 'shut-down',
|
353 | }
|
354 |
|
355 | export {};
|