|
| 1 | +import { ApiClient } from "../apiClient/index.js"; |
| 2 | +import { AsyncIterableStream } from "../streams/asyncIterableStream.js"; |
| 3 | +import { AnyZodFetchOptions } from "../zodfetch.js"; |
| 4 | +import { StreamsWriterV2 } from "./streamsWriterV2.js"; |
| 5 | +import { StreamsWriter, StreamWriteResult } from "./types.js"; |
| 6 | + |
| 7 | +export type SessionStreamInstanceOptions<T> = { |
| 8 | + apiClient: ApiClient; |
| 9 | + baseUrl: string; |
| 10 | + sessionId: string; |
| 11 | + io: "out" | "in"; |
| 12 | + source: ReadableStream<T>; |
| 13 | + signal?: AbortSignal; |
| 14 | + requestOptions?: AnyZodFetchOptions; |
| 15 | + debug?: boolean; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Session-scoped parallel to {@link StreamInstance}. Calls |
| 20 | + * `initializeSessionStream` to fetch S2 credentials for the session's |
| 21 | + * channel, then pipes `source` directly to S2 via {@link StreamsWriterV2}. |
| 22 | + * |
| 23 | + * Sessions are S2-only — there's no v1 (Redis) fallback — so this |
| 24 | + * skips the version-detection dance `StreamInstance` does. |
| 25 | + */ |
| 26 | +export class SessionStreamInstance<T> implements StreamsWriter { |
| 27 | + private streamPromise: Promise<StreamsWriterV2<T>>; |
| 28 | + |
| 29 | + constructor(private options: SessionStreamInstanceOptions<T>) { |
| 30 | + this.streamPromise = this.initializeWriter(); |
| 31 | + } |
| 32 | + |
| 33 | + private async initializeWriter(): Promise<StreamsWriterV2<T>> { |
| 34 | + const response = await this.options.apiClient.initializeSessionStream( |
| 35 | + this.options.sessionId, |
| 36 | + this.options.io, |
| 37 | + this.options?.requestOptions |
| 38 | + ); |
| 39 | + |
| 40 | + const headers = response.headers ?? {}; |
| 41 | + const accessToken = headers["x-s2-access-token"]; |
| 42 | + const basin = headers["x-s2-basin"]; |
| 43 | + const streamName = headers["x-s2-stream-name"]; |
| 44 | + const endpoint = headers["x-s2-endpoint"]; |
| 45 | + const flushIntervalMs = headers["x-s2-flush-interval-ms"] |
| 46 | + ? parseInt(headers["x-s2-flush-interval-ms"]) |
| 47 | + : undefined; |
| 48 | + const maxRetries = headers["x-s2-max-retries"] |
| 49 | + ? parseInt(headers["x-s2-max-retries"]) |
| 50 | + : undefined; |
| 51 | + |
| 52 | + if (!accessToken || !basin || !streamName) { |
| 53 | + throw new Error( |
| 54 | + "Session stream initialize did not return S2 credentials — server may be configured for v1 realtime streams, which sessions do not support." |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return new StreamsWriterV2({ |
| 59 | + basin, |
| 60 | + stream: streamName, |
| 61 | + accessToken, |
| 62 | + endpoint, |
| 63 | + source: this.options.source, |
| 64 | + signal: this.options.signal, |
| 65 | + debug: this.options.debug, |
| 66 | + flushIntervalMs, |
| 67 | + maxRetries, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public async wait(): Promise<StreamWriteResult> { |
| 72 | + const writer = await this.streamPromise; |
| 73 | + return writer.wait(); |
| 74 | + } |
| 75 | + |
| 76 | + public get stream(): AsyncIterableStream<T> { |
| 77 | + const self = this; |
| 78 | + |
| 79 | + return new ReadableStream<T>({ |
| 80 | + async start(controller) { |
| 81 | + const streamWriter = await self.streamPromise; |
| 82 | + |
| 83 | + const iterator = streamWriter[Symbol.asyncIterator](); |
| 84 | + |
| 85 | + while (true) { |
| 86 | + if (self.options.signal?.aborted) { |
| 87 | + controller.close(); |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + const { done, value } = await iterator.next(); |
| 92 | + |
| 93 | + if (done) { |
| 94 | + controller.close(); |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + controller.enqueue(value); |
| 99 | + } |
| 100 | + }, |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments