Records stream output. Malformed output fails the current and later waits instead of throwing, because this * runs in a stream callback, where a throw would skip the smoke's cleanup.
(chunk: string)
| 71 | /** Records stream output. Malformed output fails the current and later waits instead of throwing, because this |
| 72 | * runs in a stream callback, where a throw would skip the smoke's cleanup. */ |
| 73 | write(chunk: string): void { |
| 74 | if (this.failure) return; |
| 75 | this.text += chunk; |
| 76 | for (let newline = this.text.indexOf("\n"); newline >= 0; newline = this.text.indexOf("\n")) { |
| 77 | const line = this.text.slice(0, newline); |
| 78 | this.text = this.text.slice(newline + 1); |
| 79 | if (!line.trim()) continue; |
| 80 | let event: unknown; |
| 81 | try { event = JSON.parse(line); } |
| 82 | catch (error) { |
| 83 | this.failure = new Error(`Event stream printed a line that is not JSON: ${line.slice(0, 200)}`, { cause: error }); |
| 84 | for (const reject of this.failing) reject(this.failure); |
| 85 | this.failing.clear(); |
| 86 | return; |
| 87 | } |
| 88 | if (typeof event !== "object" || event === null) continue; |
| 89 | const data = (event as { data?: { api_url?: unknown } }).data; |
| 90 | const match = typeof data?.api_url === "string" ? /\/reviews\/(\d+)$/.exec(data.api_url) : null; |
| 91 | if (!match) continue; |
| 92 | const id = Number(match[1]); |
| 93 | this.received.add(id); |
| 94 | for (const resolve of this.waiting.get(id) ?? []) resolve(); |
| 95 | this.waiting.delete(id); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | async wait(id: number, timeoutMs: number): Promise<void> { |
| 101 | this.check(); |