@@ -2,13 +2,14 @@ import { EventEmitter } from "eventemitter3";
22import lighthouse from "@lighthouse-web3/sdk" ;
33import { readFileSync , createWriteStream , promises as fsPromises } from "fs" ;
44import { dirname } from "path" ;
5- import axios from "axios" ;
5+ import axios , { AxiosRequestConfig , AxiosResponse } from "axios" ;
66import { AuthenticationManager } from "./auth/AuthenticationManager" ;
77import { ProgressTracker } from "./progress/ProgressTracker" ;
88import { ErrorHandler } from "./errors/ErrorHandler" ;
99import { CircuitBreaker } from "./errors/CircuitBreaker" ;
1010import { EncryptionManager } from "./encryption/EncryptionManager" ;
1111import { RateLimiter } from "./utils/RateLimiter" ;
12+ import { ConnectionPool , ConnectionPoolConfig } from "./pool" ;
1213import {
1314 LighthouseConfig ,
1415 UploadOptions ,
@@ -78,6 +79,7 @@ export class LighthouseAISDK extends EventEmitter {
7879 private circuitBreaker : CircuitBreaker ;
7980 private encryption : EncryptionManager ;
8081 private rateLimiter : RateLimiter ;
82+ private connectionPool : ConnectionPool | null ;
8183 private memoryManager : MemoryManager ;
8284 private config : LighthouseConfig ;
8385
@@ -101,6 +103,22 @@ export class LighthouseAISDK extends EventEmitter {
101103 autoCleanup : true ,
102104 } ) ;
103105
106+ // Initialize connection pool (unless explicitly disabled)
107+ if ( config . pool === false ) {
108+ this . connectionPool = null ;
109+ } else {
110+ const poolConfig : ConnectionPoolConfig = {
111+ maxConnections : 10 ,
112+ acquireTimeout : 5000 ,
113+ idleTimeout : 60000 ,
114+ requestTimeout : config . timeout || 30000 ,
115+ keepAlive : true ,
116+ maxSockets : 50 ,
117+ ...( typeof config . pool === "object" ? config . pool : { } ) ,
118+ } ;
119+ this . connectionPool = new ConnectionPool ( poolConfig ) ;
120+ }
121+
104122 // Forward authentication events
105123 this . auth . on ( "auth:error" , ( error ) => this . emit ( "auth:error" , error ) ) ;
106124 this . auth . on ( "auth:refresh" , ( ) => this . emit ( "auth:refresh" ) ) ;
@@ -145,6 +163,15 @@ export class LighthouseAISDK extends EventEmitter {
145163 this . emit ( "encryption:access:control:error" , event ) ,
146164 ) ;
147165
166+ // Forward connection pool events
167+ if ( this . connectionPool ) {
168+ this . connectionPool . on ( "acquire" , ( event ) => this . emit ( "pool:acquire" , event ) ) ;
169+ this . connectionPool . on ( "create" , ( event ) => this . emit ( "pool:create" , event ) ) ;
170+ this . connectionPool . on ( "queue" , ( event ) => this . emit ( "pool:queue" , event ) ) ;
171+ this . connectionPool . on ( "release" , ( event ) => this . emit ( "pool:release" , event ) ) ;
172+ this . connectionPool . on ( "cleanup" , ( event ) => this . emit ( "pool:cleanup" , event ) ) ;
173+ }
174+
148175 // Forward memory manager events
149176 this . memoryManager . on ( "backpressure:start" , ( event ) =>
150177 this . emit ( "memory:backpressure:start" , event ) ,
@@ -363,6 +390,25 @@ Maximum file size may be exceeded. Try uploading a smaller file.`);
363390 ] ) ;
364391 }
365392
393+ /**
394+ * Execute an HTTP request using the connection pool if available,
395+ * otherwise fall back to a direct axios call.
396+ */
397+ private async executeHttpRequest < T = any > ( config : AxiosRequestConfig ) : Promise < AxiosResponse < T > > {
398+ if ( this . connectionPool ) {
399+ const instance = await this . connectionPool . acquire ( ) ;
400+ try {
401+ return await instance . request < T > ( config ) ;
402+ } finally {
403+ this . connectionPool . release ( instance ) ;
404+ }
405+ } else {
406+ const axiosLib : { request : ( config : AxiosRequestConfig ) => Promise < AxiosResponse < T > > } =
407+ eval ( "require" ) ( "axios" ) ;
408+ return axiosLib . request ( config ) ;
409+ }
410+ }
411+
366412 /**
367413 * Upload file via direct API call as fallback when SDK fails
368414 */
@@ -375,12 +421,14 @@ Maximum file size may be exceeded. Try uploading a smaller file.`);
375421 // when the standard SDK fails (usually due to node.lighthouse.storage being down)
376422
377423 const FormData = eval ( "require" ) ( "form-data" ) ;
378- const axios = eval ( "require" ) ( "axios" ) ;
379424
380425 const formData = new FormData ( ) ;
381426 formData . append ( "file" , fileBuffer , fileName ) ;
382427
383- const response = await axios . post ( "https://api.lighthouse.storage/api/v0/add" , formData , {
428+ const response = await this . executeHttpRequest ( {
429+ method : "POST" ,
430+ url : "https://api.lighthouse.storage/api/v0/add" ,
431+ data : formData ,
384432 headers : {
385433 ...formData . getHeaders ( ) ,
386434 Authorization : `Bearer ${ apiKey } ` ,
@@ -932,6 +980,24 @@ Check your internet connection and try again.`);
932980 return this . circuitBreaker . getMetrics ( ) ;
933981 }
934982
983+ /**
984+ * Get connection pool statistics.
985+ * Returns null if the connection pool is disabled.
986+ */
987+ getConnectionPoolStats ( ) : {
988+ totalConnections : number ;
989+ activeConnections : number ;
990+ idleConnections : number ;
991+ queuedRequests : number ;
992+ totalRequests : number ;
993+ averageWaitTime : number ;
994+ } | null {
995+ if ( ! this . connectionPool ) {
996+ return null ;
997+ }
998+ return this . connectionPool . getStats ( ) ;
999+ }
1000+
9351001 /**
9361002 * Reset error metrics
9371003 */
@@ -1627,6 +1693,9 @@ Check your internet connection and try again.`);
16271693 this . auth . destroy ( ) ;
16281694 this . progress . cleanup ( ) ;
16291695 this . encryption . destroy ( ) ;
1696+ if ( this . connectionPool ) {
1697+ this . connectionPool . destroy ( ) ;
1698+ }
16301699 this . memoryManager . destroy ( ) ;
16311700 this . removeAllListeners ( ) ;
16321701 }
0 commit comments