| 343 | // non-negative value seeds the stream with log rows seq > value |
| 344 | // before live events start flowing. |
| 345 | private makeLiveStream(record: ExecRecord, replayAfter: number): ReadableStream<ExecEvent> { |
| 346 | if (record.subscriber !== undefined) { |
| 347 | throw new ExecError("EEXEC_BUSY", `exec ${record.id} already has a live subscriber`); |
| 348 | } |
| 349 | let controller: ReadableStreamDefaultController<ExecEvent> | undefined; |
| 350 | let closed = false; |
| 351 | const sub: LiveSubscriber = { |
| 352 | paused: false, |
| 353 | enqueue: (event) => { |
| 354 | if (closed) return; |
| 355 | if (controller === undefined) return; |
| 356 | try { |
| 357 | controller.enqueue(event); |
| 358 | } catch { |
| 359 | return; |
| 360 | } |
| 361 | if ((controller.desiredSize ?? 1) <= 0) sub.pause(); |
| 362 | }, |
| 363 | close: () => { |
| 364 | if (closed) return; |
| 365 | closed = true; |
| 366 | if (controller !== undefined) { |
| 367 | try { |
| 368 | controller.close(); |
| 369 | } catch { |
| 370 | /* */ |
| 371 | } |
| 372 | } |
| 373 | }, |
| 374 | error: (err) => { |
| 375 | closed = true; |
| 376 | if (controller !== undefined) { |
| 377 | try { |
| 378 | controller.error(err); |
| 379 | } catch { |
| 380 | /* */ |
| 381 | } |
| 382 | } |
| 383 | }, |
| 384 | pause: () => { |
| 385 | if (sub.paused) return; |
| 386 | sub.paused = true; |
| 387 | record.child.stdout?.pause(); |
| 388 | record.child.stderr?.pause(); |
| 389 | }, |
| 390 | resume: () => { |
| 391 | if (!sub.paused) return; |
| 392 | sub.paused = false; |
| 393 | record.child.stdout?.resume(); |
| 394 | record.child.stderr?.resume(); |
| 395 | }, |
| 396 | }; |
| 397 | record.subscriber = sub; |
| 398 | |
| 399 | return new ReadableStream<ExecEvent>({ |
| 400 | start: (c) => { |
| 401 | controller = c; |
| 402 | if (replayAfter >= 0) { |