UNPKG

4.94 kBTypeScriptView Raw
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" />
8import type { ForkOptions } from 'child_process';
9import type { EventEmitter } from 'events';
10export interface ResourceLimits {
11 maxYoungGenerationSizeMb?: number;
12 maxOldGenerationSizeMb?: number;
13 codeRangeSizeMb?: number;
14 stackSizeMb?: number;
15}
16export declare const CHILD_MESSAGE_INITIALIZE: 0;
17export declare const CHILD_MESSAGE_CALL: 1;
18export declare const CHILD_MESSAGE_END: 2;
19export declare const PARENT_MESSAGE_OK: 0;
20export declare const PARENT_MESSAGE_CLIENT_ERROR: 1;
21export declare const PARENT_MESSAGE_SETUP_ERROR: 2;
22export declare const PARENT_MESSAGE_CUSTOM: 3;
23export declare type PARENT_MESSAGE_ERROR = typeof PARENT_MESSAGE_CLIENT_ERROR | typeof PARENT_MESSAGE_SETUP_ERROR;
24export interface WorkerPoolInterface {
25 getStderr(): NodeJS.ReadableStream;
26 getStdout(): NodeJS.ReadableStream;
27 getWorkers(): Array<WorkerInterface>;
28 createWorker(options: WorkerOptions): WorkerInterface;
29 send(workerId: number, request: ChildMessage, onStart: OnStart, onEnd: OnEnd, onCustomMessage: OnCustomMessage): void;
30 end(): Promise<PoolExitResult>;
31}
32export interface WorkerInterface {
33 send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd, onCustomMessage: OnCustomMessage): void;
34 waitForExit(): Promise<void>;
35 forceExit(): void;
36 getWorkerId(): number;
37 getStderr(): NodeJS.ReadableStream | null;
38 getStdout(): NodeJS.ReadableStream | null;
39}
40export declare type PoolExitResult = {
41 forceExited: boolean;
42};
43export interface PromiseWithCustomMessage<T> extends Promise<T> {
44 UNSTABLE_onCustomMessage?: (listener: OnCustomMessage) => () => void;
45}
46export type { ForkOptions };
47export interface TaskQueue {
48 /**
49 * Enqueues the task in the queue for the specified worker or adds it to the
50 * queue shared by all workers
51 * @param task the task to queue
52 * @param workerId the id of the worker that should process this task or undefined
53 * if there's no preference.
54 */
55 enqueue(task: QueueChildMessage, workerId?: number): void;
56 /**
57 * Dequeues the next item from the queue for the speified worker
58 * @param workerId the id of the worker for which the next task should be retrieved
59 */
60 dequeue(workerId: number): QueueChildMessage | null;
61}
62export declare type FarmOptions = {
63 computeWorkerKey?: (method: string, ...args: Array<unknown>) => string | null;
64 exposedMethods?: ReadonlyArray<string>;
65 forkOptions?: ForkOptions;
66 workerSchedulingPolicy?: 'round-robin' | 'in-order';
67 resourceLimits?: ResourceLimits;
68 setupArgs?: Array<unknown>;
69 maxRetries?: number;
70 numWorkers?: number;
71 taskQueue?: TaskQueue;
72 WorkerPool?: (workerPath: string, options?: WorkerPoolOptions) => WorkerPoolInterface;
73 enableWorkerThreads?: boolean;
74};
75export declare type WorkerPoolOptions = {
76 setupArgs: Array<unknown>;
77 forkOptions: ForkOptions;
78 resourceLimits: ResourceLimits;
79 maxRetries: number;
80 numWorkers: number;
81 enableWorkerThreads: boolean;
82};
83export declare type WorkerOptions = {
84 forkOptions: ForkOptions;
85 resourceLimits: ResourceLimits;
86 setupArgs: Array<unknown>;
87 maxRetries: number;
88 workerId: number;
89 workerPath: string;
90};
91export declare type MessagePort = typeof EventEmitter & {
92 postMessage(message: unknown): void;
93};
94export declare type MessageChannel = {
95 port1: MessagePort;
96 port2: MessagePort;
97};
98export declare type ChildMessageInitialize = [
99 typeof CHILD_MESSAGE_INITIALIZE,
100 boolean,
101 string,
102 // file
103 Array<unknown> | undefined,
104 // setupArgs
105 MessagePort | undefined
106];
107export declare type ChildMessageCall = [
108 typeof CHILD_MESSAGE_CALL,
109 boolean,
110 string,
111 Array<unknown>
112];
113export declare type ChildMessageEnd = [
114 typeof CHILD_MESSAGE_END,
115 boolean
116];
117export declare type ChildMessage = ChildMessageInitialize | ChildMessageCall | ChildMessageEnd;
118export declare type ParentMessageCustom = [
119 typeof PARENT_MESSAGE_CUSTOM,
120 unknown
121];
122export declare type ParentMessageOk = [
123 typeof PARENT_MESSAGE_OK,
124 unknown
125];
126export declare type ParentMessageError = [
127 PARENT_MESSAGE_ERROR,
128 string,
129 string,
130 string,
131 unknown
132];
133export declare type ParentMessage = ParentMessageOk | ParentMessageError | ParentMessageCustom;
134export declare type OnStart = (worker: WorkerInterface) => void;
135export declare type OnEnd = (err: Error | null, result: unknown) => void;
136export declare type OnCustomMessage = (message: Array<unknown> | unknown) => void;
137export declare type QueueChildMessage = {
138 request: ChildMessageCall;
139 onStart: OnStart;
140 onEnd: OnEnd;
141 onCustomMessage: OnCustomMessage;
142};