index.d.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. /// <reference types="node" />
  8. import type {EventEmitter} from 'events';
  9. import type {ForkOptions} from 'child_process';
  10. import type {ResourceLimits} from 'worker_threads';
  11. declare const CHILD_MESSAGE_CALL = 1;
  12. declare const CHILD_MESSAGE_END = 2;
  13. declare const CHILD_MESSAGE_INITIALIZE = 0;
  14. declare type ChildMessage =
  15. | ChildMessageInitialize
  16. | ChildMessageCall
  17. | ChildMessageEnd;
  18. declare type ChildMessageCall = [
  19. typeof CHILD_MESSAGE_CALL,
  20. boolean,
  21. string,
  22. Array<unknown>,
  23. ];
  24. declare type ChildMessageEnd = [typeof CHILD_MESSAGE_END, boolean];
  25. declare type ChildMessageInitialize = [
  26. typeof CHILD_MESSAGE_INITIALIZE,
  27. boolean,
  28. string,
  29. // file
  30. Array<unknown> | undefined,
  31. // setupArgs
  32. MessagePort_2 | undefined,
  33. ];
  34. declare type ComputeTaskPriorityCallback = (
  35. method: string,
  36. ...args: Array<unknown>
  37. ) => number;
  38. declare type ExcludeReservedKeys<K> = Exclude<K, ReservedKeys>;
  39. /**
  40. * First-in, First-out task queue that manages a dedicated pool
  41. * for each worker as well as a shared queue. The FIFO ordering is guaranteed
  42. * across the worker specific and shared queue.
  43. */
  44. export declare class FifoQueue implements TaskQueue {
  45. private _workerQueues;
  46. private _sharedQueue;
  47. enqueue(task: QueueChildMessage, workerId?: number): void;
  48. dequeue(workerId: number): QueueChildMessage | null;
  49. }
  50. declare type FunctionLike = (...args: any) => unknown;
  51. declare type HeapItem = {
  52. priority: number;
  53. };
  54. export declare type JestWorkerFarm<T extends Record<string, unknown>> =
  55. Worker_2 & WorkerModule<T>;
  56. export declare function messageParent(
  57. message: unknown,
  58. parentProcess?: NodeJS.Process,
  59. ): void;
  60. declare type MessagePort_2 = typeof EventEmitter & {
  61. postMessage(message: unknown): void;
  62. };
  63. declare type MethodLikeKeys<T> = {
  64. [K in keyof T]: T[K] extends FunctionLike ? K : never;
  65. }[keyof T];
  66. declare class MinHeap<TItem extends HeapItem> {
  67. private _heap;
  68. peek(): TItem | null;
  69. add(item: TItem): void;
  70. poll(): TItem | null;
  71. }
  72. declare type OnCustomMessage = (message: Array<unknown> | unknown) => void;
  73. declare type OnEnd = (err: Error | null, result: unknown) => void;
  74. declare type OnStart = (worker: WorkerInterface) => void;
  75. declare type PoolExitResult = {
  76. forceExited: boolean;
  77. };
  78. /**
  79. * Priority queue that processes tasks in natural ordering (lower priority first)
  80. * according to the priority computed by the function passed in the constructor.
  81. *
  82. * FIFO ordering isn't guaranteed for tasks with the same priority.
  83. *
  84. * Worker specific tasks with the same priority as a non-worker specific task
  85. * are always processed first.
  86. */
  87. export declare class PriorityQueue implements TaskQueue {
  88. private _computePriority;
  89. private _queue;
  90. private _sharedQueue;
  91. constructor(_computePriority: ComputeTaskPriorityCallback);
  92. enqueue(task: QueueChildMessage, workerId?: number): void;
  93. _enqueue(task: QueueChildMessage, queue: MinHeap<QueueItem>): void;
  94. dequeue(workerId: number): QueueChildMessage | null;
  95. _getWorkerQueue(workerId: number): MinHeap<QueueItem>;
  96. }
  97. export declare interface PromiseWithCustomMessage<T> extends Promise<T> {
  98. UNSTABLE_onCustomMessage?: (listener: OnCustomMessage) => () => void;
  99. }
  100. declare type Promisify<T extends FunctionLike> = ReturnType<T> extends Promise<
  101. infer R
  102. >
  103. ? (...args: Parameters<T>) => Promise<R>
  104. : (...args: Parameters<T>) => Promise<ReturnType<T>>;
  105. declare type QueueChildMessage = {
  106. request: ChildMessageCall;
  107. onStart: OnStart;
  108. onEnd: OnEnd;
  109. onCustomMessage: OnCustomMessage;
  110. };
  111. declare type QueueItem = {
  112. task: QueueChildMessage;
  113. priority: number;
  114. };
  115. declare type ReservedKeys =
  116. | 'end'
  117. | 'getStderr'
  118. | 'getStdout'
  119. | 'setup'
  120. | 'teardown';
  121. export declare interface TaskQueue {
  122. /**
  123. * Enqueues the task in the queue for the specified worker or adds it to the
  124. * queue shared by all workers
  125. * @param task the task to queue
  126. * @param workerId the id of the worker that should process this task or undefined
  127. * if there's no preference.
  128. */
  129. enqueue(task: QueueChildMessage, workerId?: number): void;
  130. /**
  131. * Dequeues the next item from the queue for the specified worker
  132. * @param workerId the id of the worker for which the next task should be retrieved
  133. */
  134. dequeue(workerId: number): QueueChildMessage | null;
  135. }
  136. /**
  137. * The Jest farm (publicly called "Worker") is a class that allows you to queue
  138. * methods across multiple child processes, in order to parallelize work. This
  139. * is done by providing an absolute path to a module that will be loaded on each
  140. * of the child processes, and bridged to the main process.
  141. *
  142. * Bridged methods are specified by using the "exposedMethods" property of the
  143. * "options" object. This is an array of strings, where each of them corresponds
  144. * to the exported name in the loaded module.
  145. *
  146. * You can also control the amount of workers by using the "numWorkers" property
  147. * of the "options" object, and the settings passed to fork the process through
  148. * the "forkOptions" property. The amount of workers defaults to the amount of
  149. * CPUS minus one.
  150. *
  151. * Queueing calls can be done in two ways:
  152. * - Standard method: calls will be redirected to the first available worker,
  153. * so they will get executed as soon as they can.
  154. *
  155. * - Sticky method: if a "computeWorkerKey" method is provided within the
  156. * config, the resulting string of this method will be used as a key.
  157. * Every time this key is returned, it is guaranteed that your job will be
  158. * processed by the same worker. This is specially useful if your workers
  159. * are caching results.
  160. */
  161. declare class Worker_2 {
  162. private _ending;
  163. private _farm;
  164. private _options;
  165. private _workerPool;
  166. constructor(workerPath: string, options?: WorkerFarmOptions);
  167. private _bindExposedWorkerMethods;
  168. private _callFunctionWithArgs;
  169. getStderr(): NodeJS.ReadableStream;
  170. getStdout(): NodeJS.ReadableStream;
  171. end(): Promise<PoolExitResult>;
  172. }
  173. export {Worker_2 as Worker};
  174. declare type WorkerCallback = (
  175. workerId: number,
  176. request: ChildMessage,
  177. onStart: OnStart,
  178. onEnd: OnEnd,
  179. onCustomMessage: OnCustomMessage,
  180. ) => void;
  181. export declare type WorkerFarmOptions = {
  182. computeWorkerKey?: (method: string, ...args: Array<unknown>) => string | null;
  183. enableWorkerThreads?: boolean;
  184. exposedMethods?: ReadonlyArray<string>;
  185. forkOptions?: ForkOptions;
  186. maxRetries?: number;
  187. numWorkers?: number;
  188. resourceLimits?: ResourceLimits;
  189. setupArgs?: Array<unknown>;
  190. taskQueue?: TaskQueue;
  191. WorkerPool?: new (
  192. workerPath: string,
  193. options?: WorkerPoolOptions,
  194. ) => WorkerPoolInterface;
  195. workerSchedulingPolicy?: WorkerSchedulingPolicy;
  196. };
  197. declare interface WorkerInterface {
  198. send(
  199. request: ChildMessage,
  200. onProcessStart: OnStart,
  201. onProcessEnd: OnEnd,
  202. onCustomMessage: OnCustomMessage,
  203. ): void;
  204. waitForExit(): Promise<void>;
  205. forceExit(): void;
  206. getWorkerId(): number;
  207. getStderr(): NodeJS.ReadableStream | null;
  208. getStdout(): NodeJS.ReadableStream | null;
  209. }
  210. declare type WorkerModule<T> = {
  211. [K in keyof T as Extract<
  212. ExcludeReservedKeys<K>,
  213. MethodLikeKeys<T>
  214. >]: T[K] extends FunctionLike ? Promisify<T[K]> : never;
  215. };
  216. declare type WorkerOptions_2 = {
  217. forkOptions: ForkOptions;
  218. resourceLimits: ResourceLimits;
  219. setupArgs: Array<unknown>;
  220. maxRetries: number;
  221. workerId: number;
  222. workerData?: unknown;
  223. workerPath: string;
  224. };
  225. export declare interface WorkerPoolInterface {
  226. getStderr(): NodeJS.ReadableStream;
  227. getStdout(): NodeJS.ReadableStream;
  228. getWorkers(): Array<WorkerInterface>;
  229. createWorker(options: WorkerOptions_2): WorkerInterface;
  230. send: WorkerCallback;
  231. end(): Promise<PoolExitResult>;
  232. }
  233. export declare type WorkerPoolOptions = {
  234. setupArgs: Array<unknown>;
  235. forkOptions: ForkOptions;
  236. resourceLimits: ResourceLimits;
  237. maxRetries: number;
  238. numWorkers: number;
  239. enableWorkerThreads: boolean;
  240. };
  241. declare type WorkerSchedulingPolicy = 'round-robin' | 'in-order';
  242. export {};