@@ -22,14 +22,12 @@ export class MetadataStream<T> {
22
22
private retryCount = 0 ;
23
23
private readonly maxRetries : number ;
24
24
private currentChunkIndex = 0 ;
25
- private reader : ReadableStreamDefaultReader < T > ;
26
25
27
26
constructor ( private options : MetadataOptions < T > ) {
28
27
const [ serverStream , consumerStream ] = this . createTeeStreams ( ) ;
29
28
this . serverStream = serverStream ;
30
29
this . consumerStream = consumerStream ;
31
30
this . maxRetries = options . maxRetries ?? 10 ;
32
- this . reader = this . serverStream . getReader ( ) ;
33
31
34
32
this . streamPromise = this . initializeServerStream ( ) ;
35
33
}
@@ -52,6 +50,8 @@ export class MetadataStream<T> {
52
50
}
53
51
54
52
private async makeRequest ( startFromChunk : number = 0 ) : Promise < void > {
53
+ const reader = this . serverStream . getReader ( ) ;
54
+
55
55
return new Promise ( ( resolve , reject ) => {
56
56
const url = new URL ( this . buildUrl ( ) ) ;
57
57
const timeout = 15 * 60 * 1000 ; // 15 minutes
@@ -71,15 +71,19 @@ export class MetadataStream<T> {
71
71
} ) ;
72
72
73
73
req . on ( "error" , ( error ) => {
74
+ reader . releaseLock ( ) ;
74
75
reject ( error ) ;
75
76
} ) ;
76
77
77
78
req . on ( "timeout" , ( ) => {
79
+ reader . releaseLock ( ) ;
78
80
req . destroy ( new Error ( "Request timed out" ) ) ;
79
81
} ) ;
80
82
81
83
req . on ( "response" , ( res ) => {
82
84
if ( res . statusCode === 408 ) {
85
+ reader . releaseLock ( ) ;
86
+
83
87
if ( this . retryCount < this . maxRetries ) {
84
88
this . retryCount ++ ;
85
89
@@ -112,7 +116,7 @@ export class MetadataStream<T> {
112
116
const processStream = async ( ) => {
113
117
try {
114
118
while ( true ) {
115
- const { done, value } = await this . reader . read ( ) ;
119
+ const { done, value } = await reader . read ( ) ;
116
120
117
121
if ( done ) {
118
122
req . end ( ) ;
@@ -124,7 +128,7 @@ export class MetadataStream<T> {
124
128
this . currentChunkIndex ++ ;
125
129
}
126
130
} catch ( error ) {
127
- req . destroy ( error as Error ) ;
131
+ reject ( error ) ;
128
132
}
129
133
} ;
130
134
@@ -135,12 +139,7 @@ export class MetadataStream<T> {
135
139
}
136
140
137
141
private async initializeServerStream ( ) : Promise < void > {
138
- try {
139
- await this . makeRequest ( 0 ) ;
140
- } catch ( error ) {
141
- this . reader . releaseLock ( ) ;
142
- throw error ;
143
- }
142
+ await this . makeRequest ( 0 ) ;
144
143
}
145
144
146
145
public async wait ( ) : Promise < void > {
0 commit comments