{"version":3,"file":"AgentRegistry.cjs","sources":["../../../src/AgentRegistry.ts"],"sourcesContent":["import { HostKey, Region } from \"./pumpFun.types.js\";\r\nimport http from \"http\";\r\nimport https from \"https\";\r\nimport {\r\n  ASTRA_ENDPOINT_BY_REGION,\r\n  NEXTBLOCK_ENDPOINT_BY_REGION,\r\n  NODE1_ENDPOINT_BY_REGION,\r\n  SLOT_ENDPOINT_BY_REGION,\r\n} from \"./pumpFun.consts.js\";\r\n\r\nclass AgentRegistry {\r\n  private static agents: Partial<Record<HostKey, http.Agent>> = {};\r\n  private static config: Map<HostKey, { host: string; port: number }> =\r\n    new Map();\r\n  /** Lazy-create & memoize */\r\n  static get(key: HostKey): http.Agent {\r\n    if (!this.agents[key]) {\r\n      const config = this.config[key];\r\n      const isHttps = config.port === 443;\r\n\r\n      this.agents[key] = isHttps\r\n        ? new https.Agent({\r\n            keepAlive: true,\r\n            keepAliveMsecs: 60_000,\r\n            maxSockets: 6, // tune per host\r\n            maxFreeSockets: 6,\r\n          })\r\n        : new http.Agent({\r\n            keepAlive: true,\r\n            keepAliveMsecs: 60_000,\r\n            maxSockets: 6, // tune per host\r\n            maxFreeSockets: 6,\r\n          });\r\n      // wireAgentDebug(this.agents[key]!, key);\r\n    }\r\n    return this.agents[key]!;\r\n  }\r\n\r\n  static registerInConfig(key: HostKey, region: Region) {\r\n    if (this.config.has(key)) {\r\n      throw new Error(`Host key ${key} is already registered.`);\r\n    }\r\n    switch (key) {\r\n      case \"slot\":\r\n        this.config.set(key, {\r\n          host: SLOT_ENDPOINT_BY_REGION[region],\r\n          port: 80,\r\n        });\r\n        break;\r\n      case \"node\":\r\n        this.config.set(key, {\r\n          host: NODE1_ENDPOINT_BY_REGION[region]!,\r\n          port: 80,\r\n        });\r\n        break;\r\n      case \"nextBlock\":\r\n        this.config.set(key, {\r\n          host: NEXTBLOCK_ENDPOINT_BY_REGION[region]!,\r\n          port: 80,\r\n        });\r\n        break;\r\n      case \"astra\":\r\n        this.config.set(key, {\r\n          host: ASTRA_ENDPOINT_BY_REGION[region]!,\r\n          port: 80,\r\n        });\r\n        break;\r\n      default:\r\n        throw new Error(`Unknown host key: ${key}`);\r\n    }\r\n  }\r\n\r\n  static deleteFromConfig(key: HostKey) {\r\n    if (!this.config.has(key)) {\r\n      throw new Error(`Host key ${key} is not registered.`);\r\n    }\r\n    this.config.delete(key);\r\n    delete this.agents[key];\r\n  }\r\n\r\n  static target(key: HostKey) {\r\n    return this.config[key];\r\n  }\r\n\r\n  static callUpstream(\r\n    key: HostKey,\r\n    path: string,\r\n    options: {\r\n      method?: string;\r\n      headers?: Record<string, string | number>;\r\n      body?: string;\r\n      timeout?: number;\r\n    } = {}\r\n  ): Promise<string> {\r\n    return new Promise((resolve, reject) => {\r\n      const { host, port } = this.target(key);\r\n      const agent = this.get(key);\r\n      const isHttps = port === 443;\r\n      const requestOptions: http.RequestOptions = {\r\n        hostname: host,\r\n        path: path,\r\n        port: port,\r\n        method: options.method || \"GET\",\r\n        headers: options.headers || {},\r\n        agent: agent,\r\n        timeout: options.timeout || 5000, // 5 second timeout\r\n      };\r\n      const requestModule = isHttps ? https : http;\r\n      const req = requestModule.request(requestOptions, (res) => {\r\n        let data = \"\";\r\n        res.on(\"data\", (chunk) => {\r\n          data += chunk;\r\n        });\r\n\r\n        res.on(\"end\", () => {\r\n          if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {\r\n            resolve(data);\r\n          } else {\r\n            reject(new Error(`HTTP ${res.statusCode}: ${data}`));\r\n          }\r\n        });\r\n      });\r\n      req.on(\"socket\", (sock) => {\r\n        sock.setNoDelay(true);\r\n      });\r\n\r\n      req.on(\"error\", (err) => {\r\n        reject(err);\r\n      });\r\n\r\n      req.on(\"timeout\", () => {\r\n        req.destroy();\r\n        reject(new Error(\"Request timeout\"));\r\n      });\r\n\r\n      // Write body if provided\r\n      if (options.body) {\r\n        req.write(options.body);\r\n      }\r\n\r\n      req.end();\r\n    });\r\n  }\r\n}\r\n\r\nexport default Agent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