@gravito/stream 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +27 -1
  2. package/dist/BatchConsumer.d.ts +81 -0
  3. package/dist/Consumer.d.ts +215 -0
  4. package/dist/DashboardProvider.d.ts +20 -0
  5. package/dist/Job.d.ts +183 -0
  6. package/dist/OrbitStream.d.ts +151 -0
  7. package/dist/QueueManager.d.ts +319 -0
  8. package/dist/Queueable.d.ts +91 -0
  9. package/dist/Scheduler.d.ts +214 -0
  10. package/dist/StreamEventBackend.d.ts +114 -0
  11. package/dist/SystemEventJob.d.ts +33 -0
  12. package/dist/Worker.d.ts +139 -0
  13. package/dist/benchmarks/PerformanceReporter.d.ts +99 -0
  14. package/dist/consumer/ConcurrencyGate.d.ts +55 -0
  15. package/dist/consumer/ConsumerStrategy.d.ts +41 -0
  16. package/dist/consumer/GroupSequencer.d.ts +57 -0
  17. package/dist/consumer/HeartbeatManager.d.ts +65 -0
  18. package/dist/consumer/JobExecutor.d.ts +61 -0
  19. package/dist/consumer/JobSourceGenerator.d.ts +31 -0
  20. package/dist/consumer/PollingStrategy.d.ts +42 -0
  21. package/dist/consumer/ReactiveStrategy.d.ts +41 -0
  22. package/dist/consumer/StreamingConsumer.d.ts +88 -0
  23. package/dist/consumer/index.d.ts +13 -0
  24. package/dist/consumer/types.d.ts +102 -0
  25. package/dist/drivers/BinaryJobFrame.d.ts +78 -0
  26. package/dist/drivers/BullMQDriver.d.ts +186 -0
  27. package/dist/drivers/DatabaseDriver.d.ts +131 -0
  28. package/dist/drivers/GrpcDriver.d.ts +16 -0
  29. package/dist/drivers/KafkaDriver.d.ts +148 -0
  30. package/dist/drivers/MemoryDriver.d.ts +108 -0
  31. package/dist/drivers/QueueDriver.d.ts +250 -0
  32. package/dist/drivers/RabbitMQDriver.d.ts +102 -0
  33. package/dist/drivers/RedisDriver.d.ts +294 -0
  34. package/dist/drivers/SQSDriver.d.ts +111 -0
  35. package/dist/drivers/kafka/BackpressureController.d.ts +60 -0
  36. package/dist/drivers/kafka/BatchProcessor.d.ts +50 -0
  37. package/dist/drivers/kafka/ConsumerLifecycleManager.d.ts +80 -0
  38. package/dist/drivers/kafka/ErrorCategorizer.d.ts +39 -0
  39. package/dist/drivers/kafka/ErrorRecoveryManager.d.ts +100 -0
  40. package/dist/drivers/kafka/HeartbeatManager.d.ts +57 -0
  41. package/dist/drivers/kafka/KafkaDriver.d.ts +138 -0
  42. package/dist/drivers/kafka/KafkaMetrics.d.ts +88 -0
  43. package/dist/drivers/kafka/KafkaNotifier.d.ts +54 -0
  44. package/dist/drivers/kafka/MessageBuffer.d.ts +71 -0
  45. package/dist/drivers/kafka/OffsetTracker.d.ts +63 -0
  46. package/dist/drivers/kafka/PerformanceMonitor.d.ts +88 -0
  47. package/dist/drivers/kafka/RateLimiter.d.ts +52 -0
  48. package/dist/drivers/kafka/RebalanceHandler.d.ts +104 -0
  49. package/dist/drivers/kafka/RingBuffer.d.ts +63 -0
  50. package/dist/drivers/kafka/index.d.ts +22 -0
  51. package/dist/drivers/kafka/types.d.ts +553 -0
  52. package/dist/drivers/prepareJobForTransport.d.ts +10 -0
  53. package/dist/index.cjs +5644 -5508
  54. package/dist/index.cjs.map +71 -0
  55. package/dist/index.d.ts +60 -4378
  56. package/dist/index.js +5609 -5453
  57. package/dist/index.js.map +71 -0
  58. package/dist/locks/DistributedLock.d.ts +175 -0
  59. package/dist/persistence/BufferedPersistence.d.ts +130 -0
  60. package/dist/persistence/BunBufferedPersistence.d.ts +173 -0
  61. package/dist/persistence/MySQLPersistence.d.ts +134 -0
  62. package/dist/persistence/SQLitePersistence.d.ts +133 -0
  63. package/dist/serializers/BinarySerializer.d.ts +42 -0
  64. package/dist/serializers/CachedSerializer.d.ts +38 -0
  65. package/dist/serializers/CborNativeSerializer.d.ts +56 -0
  66. package/dist/serializers/ClassNameSerializer.d.ts +58 -0
  67. package/dist/serializers/JobSerializer.d.ts +33 -0
  68. package/dist/serializers/JsonSerializer.d.ts +28 -0
  69. package/dist/serializers/JsonlSerializer.d.ts +90 -0
  70. package/dist/serializers/MessagePackSerializer.d.ts +29 -0
  71. package/dist/types.d.ts +653 -0
  72. package/dist/workers/BinaryWorkerProtocol.d.ts +77 -0
  73. package/dist/workers/BunWorker.d.ts +179 -0
  74. package/dist/workers/SandboxedWorker.d.ts +132 -0
  75. package/dist/workers/WorkerFactory.d.ts +128 -0
  76. package/dist/workers/WorkerPool.d.ts +186 -0
  77. package/dist/workers/bun-job-executor.d.ts +14 -0
  78. package/dist/workers/index.d.ts +13 -0
  79. package/dist/workers/job-executor.d.ts +9 -0
  80. package/package.json +6 -4
  81. package/dist/index.d.cts +0 -4387
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Binary Worker Protocol - Worker 執行緒二進制傳輸協定
3
+ *
4
+ * 實作 JSON + TextEncoder/TextDecoder + ArrayBuffer Transfer 方案,
5
+ * 讓 Main Thread 與 Worker Thread 之間以零拷貝方式傳遞 Job 資料。
6
+ *
7
+ * 設計原則:
8
+ * - 不依賴 CBOR 或外部函式庫,使用 Bun 原生優化的 TextEncoder/TextDecoder
9
+ * - ArrayBuffer Transfer 確保零拷貝語意(transfer 後原 buffer 失效)
10
+ * - 呼叫端須自行保留 job 物件引用,以支援潛在的重試機制
11
+ *
12
+ * @module workers/BinaryWorkerProtocol
13
+ */
14
+ import type { SerializedJob } from '../types';
15
+ /**
16
+ * Binary Protocol 版本號,未來可用於版本協商。
17
+ */
18
+ export declare const BINARY_PROTOCOL_VERSION = 1;
19
+ /**
20
+ * 使用 Binary Protocol 傳送給 Worker 的訊息格式。
21
+ */
22
+ export interface BinaryWorkerMessage {
23
+ /**
24
+ * 訊息類型識別符,固定為 'execute-binary'。
25
+ */
26
+ type: 'execute-binary';
27
+ /**
28
+ * 以 ArrayBuffer 格式包裝的序列化 Job 資料。
29
+ *
30
+ * 注意:此 buffer 在 postMessage transfer 後即失效(零拷貝語意)。
31
+ * Main Thread 應保留原始 job 物件以支援重試,而非依賴此 buffer。
32
+ */
33
+ buffer: ArrayBuffer;
34
+ }
35
+ /**
36
+ * 將 SerializedJob 編碼為可傳輸的 ArrayBuffer。
37
+ *
38
+ * 編碼流程:
39
+ * 1. JSON.stringify(job) — 序列化為 JSON 字串
40
+ * 2. TextEncoder.encode(json) — 轉換為 UTF-8 Uint8Array
41
+ * 3. 取出底層 ArrayBuffer 並以 slice 建立獨立副本,確保 buffer 邊界正確
42
+ *
43
+ * @param job - 要傳輸的已序列化 Job 物件
44
+ * @returns 包含 Job 資料的 ArrayBuffer(可作為 transfer list 傳遞給 postMessage)
45
+ * @throws {Error} 若 job 序列化失敗
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const buffer = encodeJobForTransfer(job)
50
+ * worker.postMessage({ type: 'execute-binary', buffer }, [buffer])
51
+ * // buffer 在此之後失效,請勿再使用
52
+ * ```
53
+ */
54
+ export declare function encodeJobForTransfer(job: SerializedJob): ArrayBuffer;
55
+ /**
56
+ * 從 ArrayBuffer 解碼 SerializedJob。
57
+ *
58
+ * 解碼流程:
59
+ * 1. new Uint8Array(buffer) — 建立 buffer 的視圖
60
+ * 2. TextDecoder.decode(view) — 從 UTF-8 還原 JSON 字串
61
+ * 3. JSON.parse(json) — 還原為 SerializedJob 物件
62
+ *
63
+ * @param buffer - 由 encodeJobForTransfer 產生的 ArrayBuffer
64
+ * @returns 還原的 SerializedJob 物件
65
+ * @throws {Error} 若 buffer 為空、JSON 解析失敗或結果不符合 SerializedJob 結構
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * self.onmessage = async (event) => {
70
+ * if (event.data.type === 'execute-binary') {
71
+ * const job = decodeJobFromTransfer(event.data.buffer)
72
+ * await executeJob(job)
73
+ * }
74
+ * }
75
+ * ```
76
+ */
77
+ export declare function decodeJobFromTransfer(buffer: ArrayBuffer): SerializedJob;
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Bun Worker Implementation.
3
+ *
4
+ * Executes jobs in isolated Bun Worker Threads to provide context isolation,
5
+ * error containment, and leverages Bun's performance optimizations.
6
+ *
7
+ * Key advantages over Node.js Worker Threads:
8
+ * - Native TypeScript support (no compilation needed)
9
+ * - 2-241x faster message passing for common patterns
10
+ * - Memory-efficient with `smol` mode
11
+ * - Built-in module preloading
12
+ *
13
+ * @public
14
+ */
15
+ import type { SerializedJob } from '../types';
16
+ /**
17
+ * Configuration options for the Bun Worker.
18
+ */
19
+ export interface BunWorkerConfig {
20
+ /**
21
+ * Maximum execution time for a job in milliseconds.
22
+ *
23
+ * Jobs exceeding this duration will be forcefully terminated.
24
+ * @default 30000 (30 seconds)
25
+ */
26
+ maxExecutionTime?: number;
27
+ /**
28
+ * Maximum memory limit for the worker in MB.
29
+ *
30
+ * Note: Bun's memory management differs from Node.js.
31
+ * This is advisory and may not be strictly enforced.
32
+ * @default undefined (unlimited)
33
+ */
34
+ maxMemory?: number;
35
+ /**
36
+ * Whether to isolate contexts for each job.
37
+ *
38
+ * If `true`, a new Worker is created for every job execution.
39
+ * If `false`, the Worker is reused across multiple jobs.
40
+ * @default false
41
+ */
42
+ isolateContexts?: boolean;
43
+ /**
44
+ * Idle timeout for the Worker in milliseconds.
45
+ *
46
+ * The worker will be terminated if it remains idle for this duration to save resources.
47
+ * @default 60000 (60 seconds)
48
+ */
49
+ idleTimeout?: number;
50
+ /**
51
+ * Enable Bun's memory-saving mode with minimal performance tradeoff.
52
+ *
53
+ * Reduces memory footprint by approximately 20-30% per worker.
54
+ * @default false
55
+ */
56
+ smol?: boolean;
57
+ /**
58
+ * Modules to preload before worker starts.
59
+ *
60
+ * Useful for large libraries or frequently used dependencies.
61
+ * Can be a single path or array of paths.
62
+ * @default undefined
63
+ */
64
+ preload?: string | string[];
65
+ /**
66
+ * Enable debugging for this worker (opens inspector port).
67
+ *
68
+ * Useful for debugging worker code.
69
+ * @default undefined
70
+ */
71
+ inspectPort?: number;
72
+ }
73
+ /**
74
+ * Bun Worker.
75
+ *
76
+ * Manages the lifecycle of a Bun Worker Thread for job execution.
77
+ * Provides features like:
78
+ * - Context Isolation: Run code in a separate thread.
79
+ * - Timeout Enforcement: Terminate hangs or long-running jobs.
80
+ * - Memory Optimization: Native support for memory-efficient mode (`smol`).
81
+ * - Error Containment: Worker crashes do not crash the main application.
82
+ * - Performance: Leverages Bun's optimized message passing.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const worker = new BunWorker({
87
+ * maxExecutionTime: 30000,
88
+ * maxMemory: 512,
89
+ * isolateContexts: true,
90
+ * smol: true
91
+ * });
92
+ *
93
+ * await worker.execute(serializedJob);
94
+ * await worker.terminate();
95
+ * ```
96
+ */
97
+ export declare class BunWorker {
98
+ private worker;
99
+ private state;
100
+ private config;
101
+ private idleTimer;
102
+ private executionTimer;
103
+ /**
104
+ * Creates a BunWorker instance.
105
+ *
106
+ * @param config - Configuration options for the worker.
107
+ */
108
+ constructor(config?: BunWorkerConfig);
109
+ /**
110
+ * Initializes the Bun Worker.
111
+ *
112
+ * @returns The active Worker instance.
113
+ * @throws {Error} If worker initialization fails or times out.
114
+ */
115
+ private initWorker;
116
+ /**
117
+ * Executes a job in the isolated Bun worker.
118
+ *
119
+ * @param job - The serialized job data to execute.
120
+ * @throws {Error} If execution fails, times out, or the worker crashes.
121
+ */
122
+ execute(job: SerializedJob): Promise<void>;
123
+ /**
124
+ * Internal method to send execution message to the worker thread.
125
+ *
126
+ * 使用 Binary Protocol(ArrayBuffer Transfer)傳遞 Job 資料:
127
+ * 1. 將 job 編碼為 ArrayBuffer(JSON + TextEncoder)
128
+ * 2. 透過 postMessage transfer list 零拷貝傳遞給 Worker
129
+ * 3. 注意:buffer transfer 後即失效,但 job 引用仍有效(支援重試)
130
+ *
131
+ * @param worker - The worker instance.
132
+ * @param job - Job data(保留此引用以支援潛在重試,不依賴已 transfer 的 buffer)
133
+ */
134
+ private executeInWorker;
135
+ /**
136
+ * Creates a promise that rejects after the configured timeout.
137
+ */
138
+ private createTimeoutPromise;
139
+ /**
140
+ * Starts the idle timer to auto-terminate the worker.
141
+ */
142
+ private startIdleTimer;
143
+ /**
144
+ * Terminates the Worker immediately.
145
+ *
146
+ * Stops any running job and releases resources.
147
+ */
148
+ terminate(): Promise<void>;
149
+ /**
150
+ * Gets the current state of the worker.
151
+ *
152
+ * @returns The current `WorkerState`.
153
+ */
154
+ getState(): string;
155
+ /**
156
+ * Checks if the worker is ready to accept a job.
157
+ *
158
+ * @returns `true` if ready, `false` otherwise.
159
+ */
160
+ isReady(): boolean;
161
+ /**
162
+ * Checks if the worker is currently executing a job.
163
+ *
164
+ * @returns `true` if busy, `false` otherwise.
165
+ */
166
+ isBusy(): boolean;
167
+ /**
168
+ * Unreferences the worker, allowing the process to exit even if worker is running.
169
+ *
170
+ * Bun-specific optimization for background workers.
171
+ */
172
+ unref(): void;
173
+ /**
174
+ * Re-references the worker, preventing process exit while worker is running.
175
+ *
176
+ * Bun-specific optimization for background workers.
177
+ */
178
+ ref(): void;
179
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Sandboxed Worker Implementation.
3
+ *
4
+ * Executes jobs in isolated Worker Threads to provide context isolation,
5
+ * error containment, and resource limits.
6
+ *
7
+ * @public
8
+ */
9
+ import type { SerializedJob } from '../types';
10
+ /**
11
+ * Configuration options for the Sandboxed Worker.
12
+ */
13
+ export interface SandboxedWorkerConfig {
14
+ /**
15
+ * Maximum execution time for a job in milliseconds.
16
+ *
17
+ * Jobs exceeding this duration will be forcefully terminated.
18
+ * @default 30000 (30 seconds)
19
+ */
20
+ maxExecutionTime?: number;
21
+ /**
22
+ * Maximum memory limit for the worker in MB.
23
+ *
24
+ * If the worker exceeds this limit, it will be terminated and restarted.
25
+ * Note: Relies on `resourceLimits` which may vary by platform.
26
+ * @default undefined (unlimited)
27
+ */
28
+ maxMemory?: number;
29
+ /**
30
+ * Whether to isolate contexts for each job.
31
+ *
32
+ * If `true`, a new Worker Thread is created for every job execution.
33
+ * If `false`, the Worker Thread is reused across multiple jobs.
34
+ * @default false
35
+ */
36
+ isolateContexts?: boolean;
37
+ /**
38
+ * Idle timeout for the Worker Thread in milliseconds.
39
+ *
40
+ * The worker will be terminated if it remains idle for this duration to save resources.
41
+ * @default 60000 (60 seconds)
42
+ */
43
+ idleTimeout?: number;
44
+ }
45
+ /**
46
+ * Sandboxed Worker.
47
+ *
48
+ * Manages the lifecycle of a Node.js Worker Thread for job execution.
49
+ * Provides features like:
50
+ * - Context Isolation: Run code in a separate thread.
51
+ * - Timeout Enforcement: Terminate hangs or long-running jobs.
52
+ * - Memory Limits: Prevent OOM issues affecting the main process.
53
+ * - Error Containment: Worker crashes do not crash the main application.
54
+ *
55
+ * @example
56
+ * ```typescript
57
+ * const worker = new SandboxedWorker({
58
+ * maxExecutionTime: 30000,
59
+ * maxMemory: 512,
60
+ * isolateContexts: true
61
+ * });
62
+ *
63
+ * await worker.execute(serializedJob);
64
+ * await worker.terminate();
65
+ * ```
66
+ */
67
+ export declare class SandboxedWorker {
68
+ private worker;
69
+ private state;
70
+ private config;
71
+ private idleTimer;
72
+ private executionTimer;
73
+ /**
74
+ * Creates a SandboxedWorker instance.
75
+ *
76
+ * @param config - Configuration options for the worker.
77
+ */
78
+ constructor(config?: SandboxedWorkerConfig);
79
+ /**
80
+ * Initializes the Worker Thread.
81
+ *
82
+ * @returns The active Worker Thread instance.
83
+ * @throws {Error} If worker initialization fails or times out.
84
+ */
85
+ private initWorker;
86
+ /**
87
+ * Executes a job in the sandboxed environment.
88
+ *
89
+ * @param job - The serialized job data to execute.
90
+ * @throws {Error} If execution fails, times out, or the worker crashes.
91
+ */
92
+ execute(job: SerializedJob): Promise<void>;
93
+ /**
94
+ * Internal method to send execution message to the worker thread.
95
+ *
96
+ * @param worker - The worker thread instance.
97
+ * @param job - Job data.
98
+ */
99
+ private executeInWorker;
100
+ /**
101
+ * Creates a promise that rejects after the configured timeout.
102
+ */
103
+ private createTimeoutPromise;
104
+ /**
105
+ * Starts the idle timer to auto-terminate the worker.
106
+ */
107
+ private startIdleTimer;
108
+ /**
109
+ * Terminates the Worker Thread immediately.
110
+ *
111
+ * Stops any running job and releases resources.
112
+ */
113
+ terminate(): Promise<void>;
114
+ /**
115
+ * Gets the current state of the worker.
116
+ *
117
+ * @returns The current `WorkerState`.
118
+ */
119
+ getState(): string;
120
+ /**
121
+ * Checks if the worker is ready to accept a job.
122
+ *
123
+ * @returns `true` if ready, `false` otherwise.
124
+ */
125
+ isReady(): boolean;
126
+ /**
127
+ * Checks if the worker is currently executing a job.
128
+ *
129
+ * @returns `true` if busy, `false` otherwise.
130
+ */
131
+ isBusy(): boolean;
132
+ }
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Worker Factory Implementation.
3
+ *
4
+ * Provides runtime-aware worker creation with automatic environment detection.
5
+ * Abstracts differences between Bun Workers and Node.js Worker Threads,
6
+ * allowing transparent switching based on the runtime environment.
7
+ *
8
+ * @public
9
+ */
10
+ import { BunWorker, type BunWorkerConfig } from './BunWorker';
11
+ import { SandboxedWorker, type SandboxedWorkerConfig } from './SandboxedWorker';
12
+ /**
13
+ * Union type for worker configuration.
14
+ * Accepts both Bun and Node.js specific configs.
15
+ */
16
+ export type WorkerConfig = BunWorkerConfig | SandboxedWorkerConfig;
17
+ /**
18
+ * Union type for worker instances.
19
+ */
20
+ export type Worker = BunWorker | SandboxedWorker;
21
+ /**
22
+ * Runtime environment type.
23
+ */
24
+ export type RuntimeEnvironment = 'bun' | 'node' | 'auto';
25
+ /**
26
+ * Worker Factory Interface.
27
+ *
28
+ * Defines the contract for creating workers in different runtime environments.
29
+ */
30
+ export interface IWorkerFactory {
31
+ /**
32
+ * Create a worker appropriate for the current runtime.
33
+ *
34
+ * @param config - Configuration for the worker.
35
+ * @returns A worker instance (BunWorker or SandboxedWorker).
36
+ */
37
+ create(config: WorkerConfig): BunWorker | SandboxedWorker;
38
+ /**
39
+ * Get the detected runtime environment.
40
+ *
41
+ * @returns The runtime environment ('bun' or 'node').
42
+ */
43
+ getRuntime(): 'bun' | 'node';
44
+ /**
45
+ * Check if running in Bun environment.
46
+ *
47
+ * @returns `true` if running on Bun, `false` otherwise.
48
+ */
49
+ isBun(): boolean;
50
+ /**
51
+ * Check if running in Node.js environment.
52
+ *
53
+ * @returns `true` if running on Node.js, `false` otherwise.
54
+ */
55
+ isNode(): boolean;
56
+ }
57
+ /**
58
+ * Runtime-aware Worker Factory.
59
+ *
60
+ * Automatically detects the runtime environment (Bun or Node.js) and creates
61
+ * the appropriate worker type. Provides a unified interface that abstracts
62
+ * away runtime-specific details.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * // Automatic detection
67
+ * const factory = new RuntimeAwareWorkerFactory()
68
+ * const worker = factory.create({ maxExecutionTime: 30000 })
69
+ *
70
+ * // Force specific runtime (testing/development)
71
+ * const bunFactory = new RuntimeAwareWorkerFactory('bun')
72
+ * const nodeFactory = new RuntimeAwareWorkerFactory('node')
73
+ * ```
74
+ */
75
+ export declare class RuntimeAwareWorkerFactory implements IWorkerFactory {
76
+ private runtime;
77
+ /**
78
+ * Creates a RuntimeAwareWorkerFactory instance.
79
+ *
80
+ * @param runtimeOverride - Optional override for runtime detection.
81
+ * - `'auto'` or undefined: Automatically detect (default)
82
+ * - `'bun'`: Force Bun runtime
83
+ * - `'node'`: Force Node.js runtime
84
+ */
85
+ constructor(runtimeOverride?: RuntimeEnvironment);
86
+ /**
87
+ * Automatically detects the runtime environment.
88
+ *
89
+ * Detection strategy:
90
+ * 1. Check for Bun global object
91
+ * 2. Fall back to Node.js
92
+ *
93
+ * @returns The detected runtime ('bun' or 'node').
94
+ */
95
+ private detectRuntime;
96
+ /**
97
+ * Creates a worker appropriate for the current runtime.
98
+ *
99
+ * @param config - Configuration for the worker.
100
+ * @returns A BunWorker if running on Bun, SandboxedWorker otherwise.
101
+ */
102
+ create(config: WorkerConfig): BunWorker | SandboxedWorker;
103
+ /**
104
+ * Gets the detected runtime environment.
105
+ *
106
+ * @returns The runtime ('bun' or 'node').
107
+ */
108
+ getRuntime(): 'bun' | 'node';
109
+ /**
110
+ * Checks if running in Bun environment.
111
+ *
112
+ * @returns `true` if running on Bun, `false` otherwise.
113
+ */
114
+ isBun(): boolean;
115
+ /**
116
+ * Checks if running in Node.js environment.
117
+ *
118
+ * @returns `true` if running on Node.js, `false` otherwise.
119
+ */
120
+ isNode(): boolean;
121
+ }
122
+ /**
123
+ * Helper function to create a default factory with optional runtime override.
124
+ *
125
+ * @param runtimeOverride - Optional runtime override.
126
+ * @returns A new RuntimeAwareWorkerFactory instance.
127
+ */
128
+ export declare function createWorkerFactory(runtimeOverride?: RuntimeEnvironment): RuntimeAwareWorkerFactory;
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Worker Pool Implementation.
3
+ *
4
+ * Manages a pool of Sandboxed Workers to provide concurrency control,
5
+ * worker reuse, load balancing, and health monitoring.
6
+ *
7
+ * @public
8
+ */
9
+ import type { SerializedJob } from '../types';
10
+ import type { SandboxedWorkerConfig } from './SandboxedWorker';
11
+ import { type IWorkerFactory, type RuntimeEnvironment } from './WorkerFactory';
12
+ /**
13
+ * Configuration options for the Worker Pool.
14
+ */
15
+ export interface WorkerPoolConfig extends SandboxedWorkerConfig {
16
+ /**
17
+ * The maximum number of workers allowed in the pool.
18
+ *
19
+ * @default 4
20
+ */
21
+ poolSize?: number;
22
+ /**
23
+ * The minimum number of workers to keep alive.
24
+ *
25
+ * The pool will pre-warm and maintain at least this many ready workers.
26
+ * @default 0
27
+ */
28
+ minWorkers?: number;
29
+ /**
30
+ * Interval for performing health checks in milliseconds.
31
+ *
32
+ * Periodically scans for and removes terminated or unhealthy workers.
33
+ * @default 30000 (30 seconds)
34
+ */
35
+ healthCheckInterval?: number;
36
+ /**
37
+ * Custom worker factory for creating workers.
38
+ *
39
+ * If not provided, RuntimeAwareWorkerFactory will be used with the runtime option.
40
+ * @default undefined
41
+ */
42
+ factory?: IWorkerFactory;
43
+ /**
44
+ * Runtime environment to use for worker creation.
45
+ *
46
+ * - `'auto'` or undefined: Automatically detect (Bun or Node.js)
47
+ * - `'bun'`: Force Bun runtime
48
+ * - `'node'`: Force Node.js runtime
49
+ * - `'bun'`/`'node'`: Only used if factory is not provided
50
+ *
51
+ * @default 'auto'
52
+ */
53
+ runtime?: RuntimeEnvironment;
54
+ }
55
+ /**
56
+ * Runtime statistics for the Worker Pool.
57
+ */
58
+ export interface WorkerPoolStats {
59
+ /** Total number of workers (ready + busy). */
60
+ total: number;
61
+ /** Number of idle workers ready for new jobs. */
62
+ ready: number;
63
+ /** Number of workers currently executing jobs. */
64
+ busy: number;
65
+ /** Number of workers in terminated state awaiting cleanup. */
66
+ terminated: number;
67
+ /** Number of jobs waiting in the queue. */
68
+ pending: number;
69
+ /** Total number of successfully completed jobs. */
70
+ completed: number;
71
+ /** Total number of failed jobs. */
72
+ failed: number;
73
+ }
74
+ /**
75
+ * Worker Pool.
76
+ *
77
+ * Orchestrates multiple `SandboxedWorker` or `BunWorker` instances to execute jobs concurrently.
78
+ * Automatically selects the best worker implementation based on the runtime environment.
79
+ *
80
+ * Key features:
81
+ * - **Concurrency Control**: Limits the number of simultaneous job executions (`poolSize`).
82
+ * - **Queueing**: Queues jobs when all workers are busy.
83
+ * - **Lifecycle Management**: Automatically creates, reuses, and terminates workers.
84
+ * - **Health Monitoring**: Periodically cleans up dead workers and maintains `minWorkers`.
85
+ * - **Runtime-Aware**: Automatically uses BunWorker on Bun, SandboxedWorker on Node.js.
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * // Auto-detection (uses best available runtime)
90
+ * const pool = new WorkerPool({
91
+ * poolSize: 8,
92
+ * minWorkers: 2,
93
+ * maxExecutionTime: 30000
94
+ * });
95
+ *
96
+ * // Force specific runtime
97
+ * const bunPool = new WorkerPool({
98
+ * runtime: 'bun',
99
+ * poolSize: 8
100
+ * });
101
+ *
102
+ * await pool.execute(job);
103
+ * await pool.shutdown();
104
+ * ```
105
+ */
106
+ export declare class WorkerPool {
107
+ private workers;
108
+ private factory;
109
+ private config;
110
+ private queue;
111
+ private healthCheckTimer;
112
+ private stats;
113
+ /**
114
+ * Creates a WorkerPool instance.
115
+ *
116
+ * @param config - Configuration options for the pool.
117
+ */
118
+ constructor(config?: WorkerPoolConfig);
119
+ /**
120
+ * Pre-warms the pool by creating the minimum number of workers.
121
+ */
122
+ private warmUp;
123
+ /**
124
+ * Creates a new Worker (BunWorker or SandboxedWorker) and adds it to the pool.
125
+ *
126
+ * Uses the configured factory to create the appropriate worker type
127
+ * based on the runtime environment.
128
+ *
129
+ * @returns The newly created worker.
130
+ */
131
+ private createWorker;
132
+ /**
133
+ * Retrieves an available worker from the pool.
134
+ *
135
+ * Priorities:
136
+ * 1. Reuse an existing ready worker.
137
+ * 2. Create a new worker if the pool is not full.
138
+ * 3. Return `null` if the pool is saturated.
139
+ *
140
+ * @returns An available worker or `null`.
141
+ */
142
+ private getAvailableWorker;
143
+ /**
144
+ * Executes a job using the worker pool.
145
+ *
146
+ * If a worker is available, the job starts immediately.
147
+ * Otherwise, it is added to the pending queue.
148
+ *
149
+ * @param job - The serialized job data.
150
+ * @throws {Error} If execution fails.
151
+ */
152
+ execute(job: SerializedJob): Promise<void>;
153
+ /**
154
+ * Processes the next job in the queue if a worker is available.
155
+ */
156
+ private processQueue;
157
+ /**
158
+ * Starts the periodic health check.
159
+ */
160
+ private startHealthCheck;
161
+ /**
162
+ * Performs a health check on the pool.
163
+ *
164
+ * Removes terminated workers and ensures `minWorkers` are available.
165
+ */
166
+ private performHealthCheck;
167
+ /**
168
+ * Gets the current statistics of the worker pool.
169
+ *
170
+ * @returns Snapshot of pool statistics.
171
+ */
172
+ getStats(): WorkerPoolStats;
173
+ /**
174
+ * Shuts down the worker pool.
175
+ *
176
+ * Terminates all workers and rejects any pending jobs.
177
+ */
178
+ shutdown(): Promise<void>;
179
+ /**
180
+ * Waits for all active and pending jobs to complete.
181
+ *
182
+ * @param timeout - Maximum wait time in milliseconds. 0 for infinite.
183
+ * @throws {Error} If the timeout is reached.
184
+ */
185
+ waitForCompletion(timeout?: number): Promise<void>;
186
+ }