7a20b02be10969b84c1fc50dfcfdc1c0269732fad082e8f1cf9e41f7b4f1b47542863c3deb23288113eeae0304f3a6220ef29fe1971a8a950727053457d9a7 3.7 KB

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