@@ -8,7 +8,7 @@ export type StreamsWriterV1Options<T> = {
88 baseUrl : string ;
99 runId : string ;
1010 key : string ;
11- source : AsyncIterable < T > ;
11+ source : ReadableStream < T > ;
1212 headers ?: Record < string , string > ;
1313 signal ?: AbortSignal ;
1414 version ?: string ;
@@ -43,7 +43,7 @@ export class StreamsWriterV1<T> implements StreamsWriter {
4343 private streamComplete = false ;
4444
4545 constructor ( private options : StreamsWriterV1Options < T > ) {
46- const [ serverStream , consumerStream ] = this . createTeeStreams ( ) ;
46+ const [ serverStream , consumerStream ] = this . options . source . tee ( ) ;
4747 this . serverStream = serverStream ;
4848 this . consumerStream = consumerStream ;
4949 this . maxRetries = options . maxRetries ?? 10 ;
@@ -60,23 +60,6 @@ export class StreamsWriterV1<T> implements StreamsWriter {
6060 return randomBytes ( 4 ) . toString ( "hex" ) ;
6161 }
6262
63- private createTeeStreams ( ) {
64- const readableSource = new ReadableStream < T > ( {
65- start : async ( controller ) => {
66- try {
67- for await ( const value of this . options . source ) {
68- controller . enqueue ( value ) ;
69- }
70- controller . close ( ) ;
71- } catch ( error ) {
72- controller . error ( error ) ;
73- }
74- } ,
75- } ) ;
76-
77- return readableSource . tee ( ) ;
78- }
79-
8063 private startBuffering ( ) : void {
8164 this . streamReader = this . serverStream . getReader ( ) ;
8265
@@ -131,10 +114,10 @@ export class StreamsWriterV1<T> implements StreamsWriter {
131114 if ( this . isRetryableError ( error ) ) {
132115 if ( this . retryCount < this . maxRetries ) {
133116 this . retryCount ++ ;
134-
117+
135118 // Clean up the current request to avoid socket leaks
136119 req . destroy ( ) ;
137-
120+
138121 const delayMs = this . calculateBackoffDelay ( ) ;
139122
140123 await this . delay ( delayMs ) ;
@@ -157,10 +140,10 @@ export class StreamsWriterV1<T> implements StreamsWriter {
157140 // Timeout is retryable
158141 if ( this . retryCount < this . maxRetries ) {
159142 this . retryCount ++ ;
160-
143+
161144 // Clean up the current request to avoid socket leaks
162145 req . destroy ( ) ;
163-
146+
164147 const delayMs = this . calculateBackoffDelay ( ) ;
165148
166149 await this . delay ( delayMs ) ;
@@ -182,13 +165,13 @@ export class StreamsWriterV1<T> implements StreamsWriter {
182165 if ( res . statusCode && this . isRetryableStatusCode ( res . statusCode ) ) {
183166 if ( this . retryCount < this . maxRetries ) {
184167 this . retryCount ++ ;
185-
168+
186169 // Drain and destroy the response and request to avoid socket leaks
187170 // We need to consume the response before destroying it
188171 res . resume ( ) ; // Start draining the response
189172 res . destroy ( ) ; // Destroy the response to free the socket
190173 req . destroy ( ) ; // Destroy the request as well
191-
174+
192175 const delayMs = this . calculateBackoffDelay ( ) ;
193176
194177 await this . delay ( delayMs ) ;
@@ -391,7 +374,7 @@ export class StreamsWriterV1<T> implements StreamsWriter {
391374 if ( this . isRetryableError ( error ) && attempt < maxHeadRetries ) {
392375 // Clean up the current request to avoid socket leaks
393376 req . destroy ( ) ;
394-
377+
395378 await this . delay ( 1000 * ( attempt + 1 ) ) ; // Simple linear backoff
396379 const result = await this . queryServerLastChunkIndex ( attempt + 1 ) ;
397380 resolve ( result ) ;
@@ -424,7 +407,7 @@ export class StreamsWriterV1<T> implements StreamsWriter {
424407 res . resume ( ) ;
425408 res . destroy ( ) ;
426409 req . destroy ( ) ;
427-
410+
428411 await this . delay ( 1000 * ( attempt + 1 ) ) ;
429412 const result = await this . queryServerLastChunkIndex ( attempt + 1 ) ;
430413 resolve ( result ) ;
0 commit comments