@@ -122,18 +122,21 @@ export class EVaultClient {
122
122
private httpClient : AxiosInstance ;
123
123
private isDisposed = false ;
124
124
125
- constructor ( private registryUrl : string , private platform : string ) {
125
+ constructor (
126
+ private registryUrl : string ,
127
+ private platform : string ,
128
+ ) {
126
129
// Configure axios with connection pooling and timeouts
127
130
this . httpClient = axios . create ( {
128
131
timeout : CONFIG . REQUEST_TIMEOUT ,
129
132
maxRedirects : 3 ,
130
133
// Connection pooling configuration
131
- httpAgent : new ( require ( ' http' ) . Agent ) ( {
134
+ httpAgent : new ( require ( " http" ) . Agent ) ( {
132
135
keepAlive : true ,
133
136
maxSockets : CONFIG . CONNECTION_POOL_SIZE ,
134
137
timeout : CONFIG . CONNECTION_TIMEOUT ,
135
138
} ) ,
136
- httpsAgent : new ( require ( ' https' ) . Agent ) ( {
139
+ httpsAgent : new ( require ( " https" ) . Agent ) ( {
137
140
keepAlive : true ,
138
141
maxSockets : CONFIG . CONNECTION_POOL_SIZE ,
139
142
timeout : CONFIG . CONNECTION_TIMEOUT ,
@@ -146,12 +149,12 @@ export class EVaultClient {
146
149
*/
147
150
public dispose ( ) : void {
148
151
if ( this . isDisposed ) return ;
149
-
152
+
150
153
this . isDisposed = true ;
151
154
this . client = null ;
152
155
this . endpoint = null ;
153
156
this . tokenInfo = null ;
154
-
157
+
155
158
// Close HTTP agents to free connections
156
159
if ( this . httpClient . defaults . httpAgent ) {
157
160
this . httpClient . defaults . httpAgent . destroy ( ) ;
@@ -166,36 +169,36 @@ export class EVaultClient {
166
169
*/
167
170
private async withRetry < T > (
168
171
operation : ( ) => Promise < T > ,
169
- maxRetries : number = CONFIG . MAX_RETRIES
172
+ maxRetries : number = CONFIG . MAX_RETRIES ,
170
173
) : Promise < T > {
171
174
let lastError : Error ;
172
-
175
+
173
176
for ( let attempt = 0 ; attempt <= maxRetries ; attempt ++ ) {
174
177
try {
175
178
return await operation ( ) ;
176
179
} catch ( error ) {
177
180
lastError = error as Error ;
178
-
181
+
179
182
// Don't retry on the last attempt
180
183
if ( attempt === maxRetries ) break ;
181
-
184
+
182
185
// Don't retry on certain errors
183
186
if ( error instanceof Error ) {
184
187
const isRetryable = ! (
185
- error . message . includes ( ' 401' ) ||
186
- error . message . includes ( ' 403' ) ||
187
- error . message . includes ( ' 404' )
188
+ error . message . includes ( " 401" ) ||
189
+ error . message . includes ( " 403" ) ||
190
+ error . message . includes ( " 404" )
188
191
) ;
189
-
192
+
190
193
if ( ! isRetryable ) break ;
191
194
}
192
-
195
+
193
196
// Exponential backoff
194
197
const delay = CONFIG . RETRY_DELAY * Math . pow ( 2 , attempt ) ;
195
- await new Promise ( resolve => setTimeout ( resolve , delay ) ) ;
198
+ await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
196
199
}
197
200
}
198
-
201
+
199
202
throw lastError ! ;
200
203
}
201
204
@@ -206,19 +209,22 @@ export class EVaultClient {
206
209
private async requestPlatformToken ( ) : Promise < TokenInfo > {
207
210
try {
208
211
const response = await this . httpClient . post < PlatformTokenResponse > (
209
- new URL ( "/platforms/certification" , this . registryUrl ) . toString ( ) ,
212
+ new URL (
213
+ "/platforms/certification" ,
214
+ this . registryUrl ,
215
+ ) . toString ( ) ,
210
216
{ platform : this . platform } ,
211
217
{
212
218
headers : {
213
219
"Content-Type" : "application/json" ,
214
220
} ,
215
221
timeout : CONFIG . REQUEST_TIMEOUT ,
216
- }
222
+ } ,
217
223
) ;
218
-
224
+
219
225
const now = Date . now ( ) ;
220
- const expiresAt = response . data . expiresAt || ( now + 3600000 ) ; // Default 1 hour
221
-
226
+ const expiresAt = response . data . expiresAt || now + 3600000 ; // Default 1 hour
227
+
222
228
return {
223
229
token : response . data . token ,
224
230
expiresAt,
@@ -235,10 +241,10 @@ export class EVaultClient {
235
241
*/
236
242
private isTokenExpired ( ) : boolean {
237
243
if ( ! this . tokenInfo ) return true ;
238
-
244
+
239
245
const now = Date . now ( ) ;
240
246
const timeUntilExpiry = this . tokenInfo . expiresAt - now ;
241
-
247
+
242
248
return timeUntilExpiry <= CONFIG . TOKEN_REFRESH_THRESHOLD ;
243
249
}
244
250
@@ -259,7 +265,7 @@ export class EVaultClient {
259
265
new URL ( `/resolve?w3id=${ w3id } ` , this . registryUrl ) . toString ( ) ,
260
266
{
261
267
timeout : CONFIG . REQUEST_TIMEOUT ,
262
- }
268
+ } ,
263
269
) ;
264
270
return new URL ( "/graphql" , response . data . uri ) . toString ( ) ;
265
271
} catch ( error ) {
@@ -294,7 +300,7 @@ export class EVaultClient {
294
300
return null ;
295
301
} ) ;
296
302
if ( ! client ) return v4 ( ) ;
297
-
303
+
298
304
console . log ( "sending payload" , envelope ) ;
299
305
300
306
const response = await client
@@ -306,7 +312,7 @@ export class EVaultClient {
306
312
} ,
307
313
} )
308
314
. catch ( ( ) => null ) ;
309
-
315
+
310
316
if ( ! response ) return v4 ( ) ;
311
317
return response . storeMetaEnvelope . metaEnvelope . id ;
312
318
} ) ;
@@ -327,7 +333,7 @@ export class EVaultClient {
327
333
} ,
328
334
} )
329
335
. catch ( ( ) => null ) ;
330
-
336
+
331
337
if ( ! response ) {
332
338
console . error ( "Failed to store reference" ) ;
333
339
throw new Error ( "Failed to store reference" ) ;
@@ -345,7 +351,7 @@ export class EVaultClient {
345
351
{
346
352
id,
347
353
w3id,
348
- }
354
+ } ,
349
355
) ;
350
356
return response . metaEnvelope ;
351
357
} catch ( error ) {
@@ -357,12 +363,15 @@ export class EVaultClient {
357
363
358
364
async updateMetaEnvelopeById (
359
365
id : string ,
360
- envelope : MetaEnvelope
366
+ envelope : MetaEnvelope ,
361
367
) : Promise < void > {
362
368
return this . withRetry ( async ( ) => {
363
369
console . log ( "sending to eVault" , envelope . w3id ) ;
364
- const client = await this . ensureClient ( envelope . w3id ) . catch ( ( ) => null ) ;
365
- if ( ! client ) throw new Error ( "Failed to establish client connection" ) ;
370
+ const client = await this . ensureClient ( envelope . w3id ) . catch (
371
+ ( ) => null ,
372
+ ) ;
373
+ if ( ! client )
374
+ throw new Error ( "Failed to establish client connection" ) ;
366
375
367
376
try {
368
377
const variables = {
@@ -374,10 +383,11 @@ export class EVaultClient {
374
383
} ,
375
384
} ;
376
385
377
- const response = await client . request < StoreMetaEnvelopeResponse > (
378
- UPDATE_META_ENVELOPE ,
379
- variables
380
- ) ;
386
+ const response =
387
+ await client . request < StoreMetaEnvelopeResponse > (
388
+ UPDATE_META_ENVELOPE ,
389
+ variables ,
390
+ ) ;
381
391
} catch ( error ) {
382
392
console . error ( "Error updating meta envelope:" , error ) ;
383
393
throw error ;
0 commit comments