@adaptic/utils 0.1.47 → 0.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * HTTP connection pooling and keep-alive configuration.
3
+ *
4
+ * Node.js (>=20) uses undici as its built-in fetch implementation, which
5
+ * automatically maintains an internal connection pool with keep-alive enabled
6
+ * by default. This module provides:
7
+ *
8
+ * 1. Explicit documentation of the connection pooling behavior
9
+ * 2. A configured HTTP Agent for use with node:http/node:https if needed
10
+ * 3. Verification utilities to confirm keep-alive is active
11
+ *
12
+ * ## Connection Pooling Behavior
13
+ *
14
+ * **Native fetch (undici):** The global `fetch()` used throughout this package
15
+ * uses Node.js built-in undici which maintains persistent connections by default.
16
+ * Connections are kept alive and reused across requests to the same origin.
17
+ * No additional configuration is needed for fetch-based API calls.
18
+ *
19
+ * **Alpaca SDK (@alpacahq/alpaca-trade-api):** The SDK uses its own internal
20
+ * HTTP client with connection reuse. The SDK is configured through `AlpacaClient`
21
+ * in `src/alpaca/client.ts` via the `createAlpacaClient()` factory which caches
22
+ * client instances by API key + account type.
23
+ *
24
+ * **WebSocket connections:** Trading API and market data WebSocket connections
25
+ * are long-lived persistent connections managed by `AlpacaTradingAPI` and
26
+ * `AlpacaMarketDataAPI` respectively.
27
+ *
28
+ * ## API Client Summary
29
+ *
30
+ * | API Client | Transport | Keep-Alive | Connection Reuse |
31
+ * |------------|-----------|------------|------------------|
32
+ * | Alpaca Trading (direct) | fetch() | Built-in | Undici pool |
33
+ * | Alpaca Market Data | fetch() | Built-in | Undici pool |
34
+ * | Alpaca SDK | internal | Built-in | SDK managed |
35
+ * | Massive.com | fetch() via fetchWithRetry | Built-in | Undici pool |
36
+ * | Alpha Vantage | fetch() | Built-in | Undici pool |
37
+ * | WebSocket (Trading) | ws | Persistent | Long-lived |
38
+ * | WebSocket (Market Data) | ws | Persistent | Long-lived |
39
+ */
40
+ import http from "http";
41
+ import https from "https";
42
+ /**
43
+ * Default keep-alive configuration values.
44
+ * These are applied when creating explicit HTTP agents for non-fetch use cases.
45
+ */
46
+ export declare const KEEP_ALIVE_DEFAULTS: {
47
+ /** Enable keep-alive connections */
48
+ readonly keepAlive: true;
49
+ /** Initial delay before sending keep-alive probes (in milliseconds) */
50
+ readonly keepAliveMsecs: 60000;
51
+ /** Maximum number of sockets per host */
52
+ readonly maxSockets: 50;
53
+ /** Maximum total number of sockets */
54
+ readonly maxTotalSockets: 256;
55
+ /** Maximum number of free (idle) sockets per host */
56
+ readonly maxFreeSockets: 10;
57
+ /** Socket timeout for idle connections (in milliseconds) */
58
+ readonly timeout: 60000;
59
+ };
60
+ /**
61
+ * Pre-configured HTTP agent with keep-alive enabled.
62
+ * Use this when making requests through node:http (not needed for fetch).
63
+ */
64
+ export declare const httpAgent: http.Agent;
65
+ /**
66
+ * Pre-configured HTTPS agent with keep-alive enabled.
67
+ * Use this when making requests through node:https (not needed for fetch).
68
+ */
69
+ export declare const httpsAgent: https.Agent;
70
+ /**
71
+ * Connection pool status information
72
+ */
73
+ export interface ConnectionPoolStatus {
74
+ /** Name of the HTTP agent */
75
+ name: string;
76
+ /** Whether keep-alive is enabled */
77
+ keepAlive: boolean;
78
+ /** Number of active sockets */
79
+ activeSockets: number;
80
+ /** Number of free (idle) sockets */
81
+ freeSockets: number;
82
+ /** Number of pending requests (waiting for a socket) */
83
+ pendingRequests: number;
84
+ /** Maximum sockets per host */
85
+ maxSockets: number;
86
+ /** Maximum free sockets per host */
87
+ maxFreeSockets: number;
88
+ }
89
+ /**
90
+ * Get the connection pool status for an HTTP/HTTPS agent.
91
+ * Useful for monitoring and debugging connection reuse.
92
+ *
93
+ * @param agent - The HTTP/HTTPS agent to inspect
94
+ * @param name - A human-readable name for the agent
95
+ * @returns Connection pool status information
96
+ */
97
+ export declare function getAgentPoolStatus(agent: http.Agent | https.Agent, name?: string): ConnectionPoolStatus;
98
+ /**
99
+ * Verifies that keep-alive is properly configured for the Node.js built-in fetch.
100
+ * Node.js >= 20 uses undici internally, which has keep-alive enabled by default.
101
+ *
102
+ * @returns Object indicating whether keep-alive is expected to be active
103
+ */
104
+ export declare function verifyFetchKeepAlive(): {
105
+ supported: boolean;
106
+ nodeVersion: string;
107
+ undiciBuiltIn: boolean;
108
+ keepAliveExpected: boolean;
109
+ };
110
+ //# sourceMappingURL=http-keep-alive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-keep-alive.d.ts","sourceRoot":"","sources":["../../../src/utils/http-keep-alive.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;GAGG;AACH,eAAO,MAAM,mBAAmB;IAC9B,oCAAoC;;IAEpC,uEAAuE;;IAEvE,yCAAyC;;IAEzC,sCAAsC;;IAEtC,qDAAqD;;IAErD,4DAA4D;;CAEpD,CAAC;AAEX;;;GAGG;AACH,eAAO,MAAM,SAAS,YAOpB,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,UAAU,aAOrB,CAAC;AAEH;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,SAAS,EAAE,OAAO,CAAC;IACnB,+BAA+B;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,oCAAoC;IACpC,WAAW,EAAE,MAAM,CAAC;IACpB,wDAAwD;IACxD,eAAe,EAAE,MAAM,CAAC;IACxB,+BAA+B;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,oCAAoC;IACpC,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAC/B,IAAI,GAAE,MAAkB,GACvB,oBAAoB,CA4BtB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,IAAI;IACtC,SAAS,EAAE,OAAO,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,OAAO,CAAC;IACvB,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAeA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaptic/utils",
3
- "version": "0.1.47",
3
+ "version": "0.1.49",
4
4
  "author": "Adaptic.ai",
5
5
  "description": "Utility functions used in Adaptic app and Lambda functions",
6
6
  "always-build-npm": true,
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=financial-regression.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"financial-regression.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/financial-regression.test.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=price-utils.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"price-utils.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/price-utils.test.ts"],"names":[],"mappings":""}