UNPKG

3.6 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 { EventEmitter } from 'events';
9import type { ForkOptions } from 'child_process';
10export declare const CHILD_MESSAGE_INITIALIZE: 0;
11export declare const CHILD_MESSAGE_CALL: 1;
12export declare const CHILD_MESSAGE_END: 2;
13export declare const PARENT_MESSAGE_OK: 0;
14export declare const PARENT_MESSAGE_CLIENT_ERROR: 1;
15export declare const PARENT_MESSAGE_SETUP_ERROR: 2;
16export declare type PARENT_MESSAGE_ERROR = typeof PARENT_MESSAGE_CLIENT_ERROR | typeof PARENT_MESSAGE_SETUP_ERROR;
17export interface WorkerPoolInterface {
18 getStderr(): NodeJS.ReadableStream;
19 getStdout(): NodeJS.ReadableStream;
20 getWorkers(): Array<WorkerInterface>;
21 createWorker(options: WorkerOptions): WorkerInterface;
22 send(workerId: number, request: ChildMessage, onStart: OnStart, onEnd: OnEnd): void;
23 end(): Promise<PoolExitResult>;
24}
25export interface WorkerInterface {
26 send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd): void;
27 waitForExit(): Promise<void>;
28 forceExit(): void;
29 getWorkerId(): number;
30 getStderr(): NodeJS.ReadableStream | null;
31 getStdout(): NodeJS.ReadableStream | null;
32}
33export declare type PoolExitResult = {
34 forceExited: boolean;
35};
36export type { ForkOptions };
37export declare type FarmOptions = {
38 computeWorkerKey?: (method: string, ...args: Array<unknown>) => string | null;
39 exposedMethods?: ReadonlyArray<string>;
40 forkOptions?: ForkOptions;
41 setupArgs?: Array<unknown>;
42 maxRetries?: number;
43 numWorkers?: number;
44 WorkerPool?: (workerPath: string, options?: WorkerPoolOptions) => WorkerPoolInterface;
45 enableWorkerThreads?: boolean;
46};
47export declare type WorkerPoolOptions = {
48 setupArgs: Array<unknown>;
49 forkOptions: ForkOptions;
50 maxRetries: number;
51 numWorkers: number;
52 enableWorkerThreads: boolean;
53};
54export declare type WorkerOptions = {
55 forkOptions: ForkOptions;
56 setupArgs: Array<unknown>;
57 maxRetries: number;
58 workerId: number;
59 workerPath: string;
60};
61export declare type MessagePort = typeof EventEmitter & {
62 postMessage(message: unknown): void;
63};
64export declare type MessageChannel = {
65 port1: MessagePort;
66 port2: MessagePort;
67};
68export declare type ChildMessageInitialize = [typeof CHILD_MESSAGE_INITIALIZE, // type
69boolean, // processed
70string, // file
71// file
72Array<unknown> | undefined, // setupArgs
73// setupArgs
74MessagePort | undefined];
75export declare type ChildMessageCall = [typeof CHILD_MESSAGE_CALL, // type
76boolean, // processed
77string, // method
78Array<unknown>];
79export declare type ChildMessageEnd = [typeof CHILD_MESSAGE_END, // type
80boolean];
81export declare type ChildMessage = ChildMessageInitialize | ChildMessageCall | ChildMessageEnd;
82export declare type ParentMessageOk = [typeof PARENT_MESSAGE_OK, // type
83unknown];
84export declare type ParentMessageError = [PARENT_MESSAGE_ERROR, // type
85string, // constructor
86string, // message
87string, // stack
88unknown];
89export declare type ParentMessage = ParentMessageOk | ParentMessageError;
90export declare type OnStart = (worker: WorkerInterface) => void;
91export declare type OnEnd = (err: Error | null, result: unknown) => void;
92export declare type QueueChildMessage = {
93 request: ChildMessage;
94 onStart: OnStart;
95 onEnd: OnEnd;
96};
97export declare type QueueItem = {
98 task: QueueChildMessage;
99 next: QueueItem | null;
100};