stream.ts
This file
Path
src/utils/stream.ts
107 lines · 2.6 KiB · TypeScript
Last changed by
fix(utils/stream): do not let abort listeners crash abort() (#5274)Tushar Pagarbefore adoption5e5b83d
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–79why, one step back1/**2 * @module3 * Stream utility.4 */56export class StreamingApi {7 private writer: WritableStreamDefaultWriter<Uint8Array>8 private encoder: TextEncoder9 private writable: WritableStream10 private abortSubscribers: (() => void | Promise<void>)[] = []11 responseReadable: ReadableStream12 /**13 * Whether the stream has been aborted.14 */15 aborted: boolean = false16 /**17 * Whether the stream has been closed normally.18 */19 closed: boolean = false2021 constructor(writable: WritableStream, _readable: ReadableStream) {22 this.writable = writable23 this.writer = writable.getWriter()24 this.encoder = new TextEncoder()2526 const reader = _readable.getReader()2728 // in case the user disconnects, let the reader know to cancel29 // this in-turn results in responseReadable being closed30 // and writeSSE method no longer blocks indefinitely31 this.abortSubscribers.push(async () => {32 await reader.cancel()33 })3435 this.responseReadable = new ReadableStream({36 async pull(controller) {37 const { done, value } = await reader.read()38 done ? controller.close() : controller.enqueue(value)39 },40 cancel: () => {41 if (!this.closed) {42 this.abort()43 }44 },45 })46 }4748 async write(input: Uint8Array | string): Promise<StreamingApi> {49 try {50 if (typeof input === 'string') {51 input = this.encoder.encode(input)52 }53 await this.writer.write(input)54 } catch {55 // Do nothing. If you want to handle errors, create a stream by yourself.56 }57 return this58 }5960 async writeln(input: string): Promise<StreamingApi> {61 await this.write(input + '\n')62 return this63 }6465 sleep(ms: number): Promise<unknown> {66 return new Promise((res) => setTimeout(res, ms))67 }6869 async close() {70 this.closed = true71 try {72 await this.writer.close()73 } catch {74 // Do nothing. If you want to handle errors, create a stream by yourself.75 }76 }7778 async pipe(body: ReadableStream) {79 this.writer.releaseLock()fix(utils/stream): re-acquire writer lock when pipe() throws (#4988)Sriharsha-dev369before adoptioncd31bc1
lines 80–84why, one step back80 try {81 await body.pipeTo(this.writable, { preventClose: true, preventAbort: true })82 } finally {83 this.writer = this.writable.getWriter()84 }Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 85–97why, one step back85 }8687 onAbort(listener: () => void | Promise<void>) {88 this.abortSubscribers.push(listener)89 }9091 /**92 * Abort the stream.93 * You can call this method when stream is aborted by external event.94 */95 abort() {96 if (!this.aborted) {97 this.aborted = truefix(utils/stream): do not let abort listeners crash abort() (#5274)Tushar Pagarbefore adoption5e5b83d
lines 98–104why, one step back98 this.abortSubscribers.forEach((subscriber) => {99 try {100 void Promise.resolve(subscriber()).catch(() => {})101 } catch {102 // Ignore synchronous listener errors.103 }104 })Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 105–107why, one step back105 }106 }107}