c85ac8d28ebb96668158a187b38a33dc15ddfc5fef3606847278dd4b0030e824df0994313ed82739a86bb75663a7f288f154217d622dacc736ba5100e63a78 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. export declare const CHILD_MESSAGE_INITIALIZE: 0;
  11. export declare const CHILD_MESSAGE_CALL: 1;
  12. export declare const CHILD_MESSAGE_END: 2;
  13. export declare const PARENT_MESSAGE_OK: 0;
  14. export declare const PARENT_MESSAGE_CLIENT_ERROR: 1;
  15. export declare const PARENT_MESSAGE_SETUP_ERROR: 2;
  16. export declare type PARENT_MESSAGE_ERROR = typeof PARENT_MESSAGE_CLIENT_ERROR | typeof PARENT_MESSAGE_SETUP_ERROR;
  17. export 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. }
  25. export 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. }
  33. export declare type PoolExitResult = {
  34. forceExited: boolean;
  35. };
  36. export type { ForkOptions };
  37. export 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. };
  47. export declare type WorkerPoolOptions = {
  48. setupArgs: Array<unknown>;
  49. forkOptions: ForkOptions;
  50. maxRetries: number;
  51. numWorkers: number;
  52. enableWorkerThreads: boolean;
  53. };
  54. export declare type WorkerOptions = {
  55. forkOptions: ForkOptions;
  56. setupArgs: Array<unknown>;
  57. maxRetries: number;
  58. workerId: number;
  59. workerPath: string;
  60. };
  61. export declare type MessagePort = typeof EventEmitter & {
  62. postMessage(message: unknown): void;
  63. };
  64. export declare type MessageChannel = {
  65. port1: MessagePort;
  66. port2: MessagePort;
  67. };
  68. export declare type ChildMessageInitialize = [typeof CHILD_MESSAGE_INITIALIZE, // type
  69. boolean, // processed
  70. string, // file
  71. // file
  72. Array<unknown> | undefined, // setupArgs
  73. // setupArgs
  74. MessagePort | undefined];
  75. export declare type ChildMessageCall = [typeof CHILD_MESSAGE_CALL, // type
  76. boolean, // processed
  77. string, // method
  78. Array<unknown>];
  79. export declare type ChildMessageEnd = [typeof CHILD_MESSAGE_END, // type
  80. boolean];
  81. export declare type ChildMessage = ChildMessageInitialize | ChildMessageCall | ChildMessageEnd;
  82. export declare type ParentMessageOk = [typeof PARENT_MESSAGE_OK, // type
  83. unknown];
  84. export declare type ParentMessageError = [PARENT_MESSAGE_ERROR, // type
  85. string, // constructor
  86. string, // message
  87. string, // stack
  88. unknown];
  89. export declare type ParentMessage = ParentMessageOk | ParentMessageError;
  90. export declare type OnStart = (worker: WorkerInterface) => void;
  91. export declare type OnEnd = (err: Error | null, result: unknown) => void;
  92. export declare type QueueChildMessage = {
  93. request: ChildMessage;
  94. onStart: OnStart;
  95. onEnd: OnEnd;
  96. };
  97. export declare type QueueItem = {
  98. task: QueueChildMessage;
  99. next: QueueItem | null;
  100. };