nlcurl 0.1.0 → 0.2.0
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.
- package/README.md +5 -13
- package/dist/cli/args.d.ts +37 -5
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +6 -17
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/index.d.ts +3 -3
- package/dist/cli/index.js +25 -10
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/output.d.ts +24 -7
- package/dist/cli/output.d.ts.map +1 -1
- package/dist/cli/output.js +24 -12
- package/dist/cli/output.js.map +1 -1
- package/dist/cookies/jar.d.ts +45 -13
- package/dist/cookies/jar.d.ts.map +1 -1
- package/dist/cookies/jar.js +88 -29
- package/dist/cookies/jar.js.map +1 -1
- package/dist/cookies/parser.d.ts +25 -3
- package/dist/cookies/parser.d.ts.map +1 -1
- package/dist/cookies/parser.js +12 -7
- package/dist/cookies/parser.js.map +1 -1
- package/dist/core/client.d.ts +49 -33
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +64 -38
- package/dist/core/client.js.map +1 -1
- package/dist/core/errors.d.ts +94 -6
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +95 -6
- package/dist/core/errors.js.map +1 -1
- package/dist/core/request.d.ts +96 -30
- package/dist/core/request.d.ts.map +1 -1
- package/dist/core/request.js +0 -3
- package/dist/core/request.js.map +1 -1
- package/dist/core/response.d.ts +92 -8
- package/dist/core/response.d.ts.map +1 -1
- package/dist/core/response.js +92 -7
- package/dist/core/response.js.map +1 -1
- package/dist/core/session.d.ts +109 -14
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +124 -46
- package/dist/core/session.js.map +1 -1
- package/dist/fingerprints/akamai.d.ts +11 -11
- package/dist/fingerprints/akamai.d.ts.map +1 -1
- package/dist/fingerprints/akamai.js +10 -14
- package/dist/fingerprints/akamai.js.map +1 -1
- package/dist/fingerprints/database.d.ts +14 -15
- package/dist/fingerprints/database.d.ts.map +1 -1
- package/dist/fingerprints/database.js +14 -19
- package/dist/fingerprints/database.js.map +1 -1
- package/dist/fingerprints/extensions.d.ts +121 -27
- package/dist/fingerprints/extensions.d.ts.map +1 -1
- package/dist/fingerprints/extensions.js +132 -49
- package/dist/fingerprints/extensions.js.map +1 -1
- package/dist/fingerprints/ja3.d.ts +34 -18
- package/dist/fingerprints/ja3.d.ts.map +1 -1
- package/dist/fingerprints/ja3.js +34 -18
- package/dist/fingerprints/ja3.js.map +1 -1
- package/dist/fingerprints/profiles/chrome.d.ts +21 -10
- package/dist/fingerprints/profiles/chrome.d.ts.map +1 -1
- package/dist/fingerprints/profiles/chrome.js +25 -22
- package/dist/fingerprints/profiles/chrome.js.map +1 -1
- package/dist/fingerprints/profiles/edge.d.ts +10 -7
- package/dist/fingerprints/profiles/edge.d.ts.map +1 -1
- package/dist/fingerprints/profiles/edge.js +10 -10
- package/dist/fingerprints/profiles/edge.js.map +1 -1
- package/dist/fingerprints/profiles/firefox.d.ts +11 -3
- package/dist/fingerprints/profiles/firefox.d.ts.map +1 -1
- package/dist/fingerprints/profiles/firefox.js +15 -14
- package/dist/fingerprints/profiles/firefox.js.map +1 -1
- package/dist/fingerprints/profiles/safari.d.ts +14 -3
- package/dist/fingerprints/profiles/safari.d.ts.map +1 -1
- package/dist/fingerprints/profiles/safari.js +16 -13
- package/dist/fingerprints/profiles/safari.js.map +1 -1
- package/dist/fingerprints/profiles/tor.d.ts +8 -7
- package/dist/fingerprints/profiles/tor.d.ts.map +1 -1
- package/dist/fingerprints/profiles/tor.js +8 -14
- package/dist/fingerprints/profiles/tor.js.map +1 -1
- package/dist/fingerprints/types.d.ts +70 -47
- package/dist/fingerprints/types.d.ts.map +1 -1
- package/dist/fingerprints/types.js +0 -7
- package/dist/fingerprints/types.js.map +1 -1
- package/dist/http/h1/client.d.ts +30 -9
- package/dist/http/h1/client.d.ts.map +1 -1
- package/dist/http/h1/client.js +152 -15
- package/dist/http/h1/client.js.map +1 -1
- package/dist/http/h1/encoder.d.ts +9 -6
- package/dist/http/h1/encoder.d.ts.map +1 -1
- package/dist/http/h1/encoder.js +8 -12
- package/dist/http/h1/encoder.js.map +1 -1
- package/dist/http/h1/parser.d.ts +68 -14
- package/dist/http/h1/parser.d.ts.map +1 -1
- package/dist/http/h1/parser.js +92 -37
- package/dist/http/h1/parser.js.map +1 -1
- package/dist/http/h2/client.d.ts +81 -14
- package/dist/http/h2/client.d.ts.map +1 -1
- package/dist/http/h2/client.js +465 -63
- package/dist/http/h2/client.js.map +1 -1
- package/dist/http/h2/frames.d.ts +103 -6
- package/dist/http/h2/frames.d.ts.map +1 -1
- package/dist/http/h2/frames.js +96 -17
- package/dist/http/h2/frames.js.map +1 -1
- package/dist/http/h2/hpack.d.ts +30 -5
- package/dist/http/h2/hpack.d.ts.map +1 -1
- package/dist/http/h2/hpack.js +39 -35
- package/dist/http/h2/hpack.js.map +1 -1
- package/dist/http/negotiator.d.ts +35 -12
- package/dist/http/negotiator.d.ts.map +1 -1
- package/dist/http/negotiator.js +89 -24
- package/dist/http/negotiator.js.map +1 -1
- package/dist/http/pool.d.ts +66 -17
- package/dist/http/pool.d.ts.map +1 -1
- package/dist/http/pool.js +47 -20
- package/dist/http/pool.js.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -13
- package/dist/index.js.map +1 -1
- package/dist/middleware/interceptor.d.ts +40 -8
- package/dist/middleware/interceptor.d.ts.map +1 -1
- package/dist/middleware/interceptor.js +28 -6
- package/dist/middleware/interceptor.js.map +1 -1
- package/dist/middleware/rate-limiter.d.ts +18 -5
- package/dist/middleware/rate-limiter.d.ts.map +1 -1
- package/dist/middleware/rate-limiter.js +12 -7
- package/dist/middleware/rate-limiter.js.map +1 -1
- package/dist/middleware/retry.d.ts +17 -5
- package/dist/middleware/retry.d.ts.map +1 -1
- package/dist/middleware/retry.js +13 -11
- package/dist/middleware/retry.js.map +1 -1
- package/dist/proxy/http-proxy.d.ts +17 -9
- package/dist/proxy/http-proxy.d.ts.map +1 -1
- package/dist/proxy/http-proxy.js +9 -13
- package/dist/proxy/http-proxy.js.map +1 -1
- package/dist/proxy/socks.d.ts +20 -9
- package/dist/proxy/socks.d.ts.map +1 -1
- package/dist/proxy/socks.js +20 -31
- package/dist/proxy/socks.js.map +1 -1
- package/dist/tls/constants.d.ts +74 -4
- package/dist/tls/constants.d.ts.map +1 -1
- package/dist/tls/constants.js +75 -21
- package/dist/tls/constants.js.map +1 -1
- package/dist/tls/node-engine.d.ts +17 -16
- package/dist/tls/node-engine.d.ts.map +1 -1
- package/dist/tls/node-engine.js +20 -27
- package/dist/tls/node-engine.js.map +1 -1
- package/dist/tls/stealth/client-hello.d.ts +32 -16
- package/dist/tls/stealth/client-hello.d.ts.map +1 -1
- package/dist/tls/stealth/client-hello.js +13 -37
- package/dist/tls/stealth/client-hello.js.map +1 -1
- package/dist/tls/stealth/engine.d.ts +18 -10
- package/dist/tls/stealth/engine.d.ts.map +1 -1
- package/dist/tls/stealth/engine.js +18 -24
- package/dist/tls/stealth/engine.js.map +1 -1
- package/dist/tls/stealth/handshake.d.ts +31 -17
- package/dist/tls/stealth/handshake.d.ts.map +1 -1
- package/dist/tls/stealth/handshake.js +173 -74
- package/dist/tls/stealth/handshake.js.map +1 -1
- package/dist/tls/stealth/key-schedule.d.ts +89 -32
- package/dist/tls/stealth/key-schedule.d.ts.map +1 -1
- package/dist/tls/stealth/key-schedule.js +62 -42
- package/dist/tls/stealth/key-schedule.js.map +1 -1
- package/dist/tls/stealth/record-layer.d.ts +76 -25
- package/dist/tls/stealth/record-layer.d.ts.map +1 -1
- package/dist/tls/stealth/record-layer.js +66 -36
- package/dist/tls/stealth/record-layer.js.map +1 -1
- package/dist/tls/types.d.ts +33 -25
- package/dist/tls/types.d.ts.map +1 -1
- package/dist/tls/types.js +0 -4
- package/dist/tls/types.js.map +1 -1
- package/dist/utils/buffer-reader.d.ts +99 -7
- package/dist/utils/buffer-reader.d.ts.map +1 -1
- package/dist/utils/buffer-reader.js +99 -7
- package/dist/utils/buffer-reader.js.map +1 -1
- package/dist/utils/buffer-writer.d.ts +99 -10
- package/dist/utils/buffer-writer.d.ts.map +1 -1
- package/dist/utils/buffer-writer.js +101 -12
- package/dist/utils/buffer-writer.js.map +1 -1
- package/dist/utils/encoding.d.ts +33 -8
- package/dist/utils/encoding.d.ts.map +1 -1
- package/dist/utils/encoding.js +58 -13
- package/dist/utils/encoding.js.map +1 -1
- package/dist/utils/logger.d.ts +61 -2
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +52 -4
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/url.d.ts +47 -7
- package/dist/utils/url.d.ts.map +1 -1
- package/dist/utils/url.js +47 -7
- package/dist/utils/url.js.map +1 -1
- package/dist/ws/client.d.ts +59 -15
- package/dist/ws/client.d.ts.map +1 -1
- package/dist/ws/client.js +34 -27
- package/dist/ws/client.js.map +1 -1
- package/dist/ws/frame.d.ts +43 -9
- package/dist/ws/frame.d.ts.map +1 -1
- package/dist/ws/frame.js +35 -19
- package/dist/ws/frame.js.map +1 -1
- package/package.json +2 -2
package/dist/http/pool.d.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Connection pool.
|
|
3
|
-
*
|
|
4
|
-
* Manages TLS connections keyed by origin, supporting both HTTP/1.1
|
|
5
|
-
* keep-alive and HTTP/2 multiplexing. Handles connection reuse,
|
|
6
|
-
* eviction, and idle timeout.
|
|
7
|
-
*/
|
|
8
1
|
import type { TLSSocket } from '../tls/types.js';
|
|
9
2
|
import { H2Client } from './h2/client.js';
|
|
10
3
|
import type { H2Profile } from '../fingerprints/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a single pooled connection entry, holding the TLS socket,
|
|
6
|
+
* protocol version, and lifecycle timestamps.
|
|
7
|
+
*
|
|
8
|
+
* @typedef {Object} PoolEntry
|
|
9
|
+
* @property {string} origin - Origin key in `protocol://hostname:port` form.
|
|
10
|
+
* @property {TLSSocket} socket - The underlying TLS (or TCP) socket.
|
|
11
|
+
* @property {'h1'|'h2'} protocol - Negotiated HTTP protocol version.
|
|
12
|
+
* @property {H2Client} [h2Client] - HTTP/2 client instance when `protocol === 'h2'`.
|
|
13
|
+
* @property {number} createdAt - Unix timestamp (ms) when the connection was established.
|
|
14
|
+
* @property {number} lastUsed - Unix timestamp (ms) when the connection was last checked out.
|
|
15
|
+
* @property {boolean} busy - `true` while an HTTP/1.1 request is in flight.
|
|
16
|
+
*/
|
|
11
17
|
export interface PoolEntry {
|
|
12
18
|
origin: string;
|
|
13
19
|
socket: TLSSocket;
|
|
@@ -17,44 +23,87 @@ export interface PoolEntry {
|
|
|
17
23
|
lastUsed: number;
|
|
18
24
|
busy: boolean;
|
|
19
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Configuration options for the {@link ConnectionPool}.
|
|
28
|
+
*
|
|
29
|
+
* @typedef {Object} PoolOptions
|
|
30
|
+
* @property {number} [maxConnectionsPerOrigin=6] - Maximum simultaneous connections to a single origin.
|
|
31
|
+
* @property {number} [maxTotalConnections=64] - Maximum total connections across all origins.
|
|
32
|
+
* @property {number} [idleTimeout=60000] - Milliseconds of inactivity before a connection is evicted.
|
|
33
|
+
* @property {number} [maxAge=300000] - Maximum lifetime of a connection in milliseconds.
|
|
34
|
+
*/
|
|
20
35
|
export interface PoolOptions {
|
|
21
|
-
/** Maximum connections per origin. */
|
|
22
36
|
maxConnectionsPerOrigin?: number;
|
|
23
|
-
/** Maximum total connections across all origins. */
|
|
24
37
|
maxTotalConnections?: number;
|
|
25
|
-
/** Connection idle timeout in milliseconds. */
|
|
26
38
|
idleTimeout?: number;
|
|
27
|
-
/** Connection max age in milliseconds. */
|
|
28
39
|
maxAge?: number;
|
|
29
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Maintains a pool of reusable TLS connections, keyed by origin, supporting
|
|
43
|
+
* both HTTP/1.1 (exclusive per-request access) and HTTP/2 (multiplexed)
|
|
44
|
+
* connections. Idle and expired connections are evicted automatically on a
|
|
45
|
+
* 30-second timer.
|
|
46
|
+
*/
|
|
30
47
|
export declare class ConnectionPool {
|
|
31
48
|
private readonly options;
|
|
32
49
|
private readonly connections;
|
|
33
50
|
private totalConnections;
|
|
34
51
|
private cleanupTimer;
|
|
52
|
+
/**
|
|
53
|
+
* Creates a new ConnectionPool.
|
|
54
|
+
*
|
|
55
|
+
* @param {PoolOptions} [options={}] - Pool configuration; unset fields use their defaults.
|
|
56
|
+
*/
|
|
35
57
|
constructor(options?: PoolOptions);
|
|
36
58
|
/**
|
|
37
|
-
*
|
|
59
|
+
* Checks out a usable connection for the given origin. For HTTP/2 returns
|
|
60
|
+
* any live connection (multiplexing); for HTTP/1.1 returns a non-busy,
|
|
61
|
+
* non-expired connection and marks it busy.
|
|
38
62
|
*
|
|
39
|
-
*
|
|
63
|
+
* @param {string} origin - Origin key in `protocol://hostname:port` form.
|
|
64
|
+
* @returns {PoolEntry|undefined} A ready connection entry, or `undefined` if none is available.
|
|
40
65
|
*/
|
|
41
66
|
get(origin: string): PoolEntry | undefined;
|
|
42
67
|
/**
|
|
43
|
-
*
|
|
68
|
+
* Registers a new connection in the pool. If adding the entry would exceed
|
|
69
|
+
* the per-origin limit, the oldest entry for that origin is evicted. If the
|
|
70
|
+
* total connection limit has been reached, the globally least-recently-used
|
|
71
|
+
* idle connection is evicted first.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} origin - Origin key.
|
|
74
|
+
* @param {TLSSocket} socket - Connected socket to pool.
|
|
75
|
+
* @param {'h1'|'h2'} protocol - Negotiated HTTP protocol.
|
|
76
|
+
* @param {H2Profile} [h2Profile] - H2 profile used to configure the H2Client.
|
|
77
|
+
* @param {Array<[string,string]>} [defaultHeaders] - Default headers applied to every H2 request.
|
|
78
|
+
* @returns {PoolEntry} The newly created pool entry.
|
|
44
79
|
*/
|
|
45
80
|
put(origin: string, socket: TLSSocket, protocol: 'h1' | 'h2', h2Profile?: H2Profile, defaultHeaders?: Array<[string, string]>): PoolEntry;
|
|
46
81
|
/**
|
|
47
|
-
*
|
|
82
|
+
* Marks an HTTP/1.1 pool entry as available for reuse once a request has
|
|
83
|
+
* completed. Updates the `lastUsed` timestamp.
|
|
84
|
+
*
|
|
85
|
+
* @param {PoolEntry} entry - The connection entry to release.
|
|
48
86
|
*/
|
|
49
87
|
release(entry: PoolEntry): void;
|
|
50
88
|
/**
|
|
51
|
-
*
|
|
89
|
+
* Removes a connection entry from the pool and destroys its socket.
|
|
90
|
+
* Typically called when a connection error has occurred or the server
|
|
91
|
+
* sent a `Connection: close` header.
|
|
92
|
+
*
|
|
93
|
+
* @param {PoolEntry} entry - The connection entry to remove and destroy.
|
|
52
94
|
*/
|
|
53
95
|
remove(entry: PoolEntry): void;
|
|
54
96
|
/**
|
|
55
|
-
*
|
|
97
|
+
* Destroys all pooled connections and stops the idle-eviction timer.
|
|
98
|
+
* The pool must not be used after this call.
|
|
56
99
|
*/
|
|
57
100
|
close(): void;
|
|
101
|
+
/**
|
|
102
|
+
* Returns the total number of connections (both idle and busy) currently held
|
|
103
|
+
* in the pool across all origins.
|
|
104
|
+
*
|
|
105
|
+
* @returns {number} Total pooled connection count.
|
|
106
|
+
*/
|
|
58
107
|
get size(): number;
|
|
59
108
|
private isExpired;
|
|
60
109
|
private evictIdle;
|
package/dist/http/pool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../../src/http/pool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pool.d.ts","sourceRoot":"","sources":["../../src/http/pool.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAqB,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CA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AI,IAAI,IAAI,MAAM,CAEjB;IAED,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,SAAS;IAkBjB,OAAO,CAAC,WAAW;CAiBpB"}
|
package/dist/http/pool.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Connection pool.
|
|
3
|
-
*
|
|
4
|
-
* Manages TLS connections keyed by origin, supporting both HTTP/1.1
|
|
5
|
-
* keep-alive and HTTP/2 multiplexing. Handles connection reuse,
|
|
6
|
-
* eviction, and idle timeout.
|
|
7
|
-
*/
|
|
8
1
|
import { H2Client } from './h2/client.js';
|
|
9
2
|
const DEFAULT_POOL_OPTIONS = {
|
|
10
3
|
maxConnectionsPerOrigin: 6,
|
|
@@ -12,23 +5,36 @@ const DEFAULT_POOL_OPTIONS = {
|
|
|
12
5
|
idleTimeout: 60_000,
|
|
13
6
|
maxAge: 300_000,
|
|
14
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Maintains a pool of reusable TLS connections, keyed by origin, supporting
|
|
10
|
+
* both HTTP/1.1 (exclusive per-request access) and HTTP/2 (multiplexed)
|
|
11
|
+
* connections. Idle and expired connections are evicted automatically on a
|
|
12
|
+
* 30-second timer.
|
|
13
|
+
*/
|
|
15
14
|
export class ConnectionPool {
|
|
16
15
|
options;
|
|
17
16
|
connections = new Map();
|
|
18
17
|
totalConnections = 0;
|
|
19
18
|
cleanupTimer;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new ConnectionPool.
|
|
21
|
+
*
|
|
22
|
+
* @param {PoolOptions} [options={}] - Pool configuration; unset fields use their defaults.
|
|
23
|
+
*/
|
|
20
24
|
constructor(options = {}) {
|
|
21
25
|
this.options = { ...DEFAULT_POOL_OPTIONS, ...options };
|
|
22
|
-
// Periodic cleanup
|
|
23
26
|
this.cleanupTimer = setInterval(() => this.evictIdle(), 30_000);
|
|
24
27
|
if (this.cleanupTimer.unref) {
|
|
25
28
|
this.cleanupTimer.unref();
|
|
26
29
|
}
|
|
27
30
|
}
|
|
28
31
|
/**
|
|
29
|
-
*
|
|
32
|
+
* Checks out a usable connection for the given origin. For HTTP/2 returns
|
|
33
|
+
* any live connection (multiplexing); for HTTP/1.1 returns a non-busy,
|
|
34
|
+
* non-expired connection and marks it busy.
|
|
30
35
|
*
|
|
31
|
-
*
|
|
36
|
+
* @param {string} origin - Origin key in `protocol://hostname:port` form.
|
|
37
|
+
* @returns {PoolEntry|undefined} A ready connection entry, or `undefined` if none is available.
|
|
32
38
|
*/
|
|
33
39
|
get(origin) {
|
|
34
40
|
const entries = this.connections.get(origin);
|
|
@@ -36,12 +42,13 @@ export class ConnectionPool {
|
|
|
36
42
|
return undefined;
|
|
37
43
|
const now = Date.now();
|
|
38
44
|
for (const entry of entries) {
|
|
39
|
-
// H2 connections can be multiplexed (always reusable)
|
|
40
45
|
if (entry.protocol === 'h2' && !this.isExpired(entry, now)) {
|
|
46
|
+
if (entry.h2Client?.isClosed) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
41
49
|
entry.lastUsed = now;
|
|
42
50
|
return entry;
|
|
43
51
|
}
|
|
44
|
-
// H1 connections must be idle
|
|
45
52
|
if (entry.protocol === 'h1' && !entry.busy && !this.isExpired(entry, now)) {
|
|
46
53
|
entry.busy = true;
|
|
47
54
|
entry.lastUsed = now;
|
|
@@ -51,13 +58,21 @@ export class ConnectionPool {
|
|
|
51
58
|
return undefined;
|
|
52
59
|
}
|
|
53
60
|
/**
|
|
54
|
-
*
|
|
61
|
+
* Registers a new connection in the pool. If adding the entry would exceed
|
|
62
|
+
* the per-origin limit, the oldest entry for that origin is evicted. If the
|
|
63
|
+
* total connection limit has been reached, the globally least-recently-used
|
|
64
|
+
* idle connection is evicted first.
|
|
65
|
+
*
|
|
66
|
+
* @param {string} origin - Origin key.
|
|
67
|
+
* @param {TLSSocket} socket - Connected socket to pool.
|
|
68
|
+
* @param {'h1'|'h2'} protocol - Negotiated HTTP protocol.
|
|
69
|
+
* @param {H2Profile} [h2Profile] - H2 profile used to configure the H2Client.
|
|
70
|
+
* @param {Array<[string,string]>} [defaultHeaders] - Default headers applied to every H2 request.
|
|
71
|
+
* @returns {PoolEntry} The newly created pool entry.
|
|
55
72
|
*/
|
|
56
73
|
put(origin, socket, protocol, h2Profile, defaultHeaders) {
|
|
57
|
-
// Evict if at capacity
|
|
58
74
|
if (this.totalConnections >= this.options.maxTotalConnections) {
|
|
59
75
|
this.evictOldest();
|
|
60
|
-
// If eviction failed (all busy), allow temporary over-capacity
|
|
61
76
|
}
|
|
62
77
|
const entry = {
|
|
63
78
|
origin,
|
|
@@ -75,7 +90,6 @@ export class ConnectionPool {
|
|
|
75
90
|
entries = [];
|
|
76
91
|
this.connections.set(origin, entries);
|
|
77
92
|
}
|
|
78
|
-
// Check per-origin limit
|
|
79
93
|
while (entries.length >= this.options.maxConnectionsPerOrigin) {
|
|
80
94
|
const evicted = entries.shift();
|
|
81
95
|
if (evicted) {
|
|
@@ -88,14 +102,21 @@ export class ConnectionPool {
|
|
|
88
102
|
return entry;
|
|
89
103
|
}
|
|
90
104
|
/**
|
|
91
|
-
*
|
|
105
|
+
* Marks an HTTP/1.1 pool entry as available for reuse once a request has
|
|
106
|
+
* completed. Updates the `lastUsed` timestamp.
|
|
107
|
+
*
|
|
108
|
+
* @param {PoolEntry} entry - The connection entry to release.
|
|
92
109
|
*/
|
|
93
110
|
release(entry) {
|
|
94
111
|
entry.busy = false;
|
|
95
112
|
entry.lastUsed = Date.now();
|
|
96
113
|
}
|
|
97
114
|
/**
|
|
98
|
-
*
|
|
115
|
+
* Removes a connection entry from the pool and destroys its socket.
|
|
116
|
+
* Typically called when a connection error has occurred or the server
|
|
117
|
+
* sent a `Connection: close` header.
|
|
118
|
+
*
|
|
119
|
+
* @param {PoolEntry} entry - The connection entry to remove and destroy.
|
|
99
120
|
*/
|
|
100
121
|
remove(entry) {
|
|
101
122
|
const entries = this.connections.get(entry.origin);
|
|
@@ -112,7 +133,8 @@ export class ConnectionPool {
|
|
|
112
133
|
entry.socket.destroyTLS();
|
|
113
134
|
}
|
|
114
135
|
/**
|
|
115
|
-
*
|
|
136
|
+
* Destroys all pooled connections and stops the idle-eviction timer.
|
|
137
|
+
* The pool must not be used after this call.
|
|
116
138
|
*/
|
|
117
139
|
close() {
|
|
118
140
|
if (this.cleanupTimer) {
|
|
@@ -129,10 +151,15 @@ export class ConnectionPool {
|
|
|
129
151
|
this.connections.clear();
|
|
130
152
|
this.totalConnections = 0;
|
|
131
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Returns the total number of connections (both idle and busy) currently held
|
|
156
|
+
* in the pool across all origins.
|
|
157
|
+
*
|
|
158
|
+
* @returns {number} Total pooled connection count.
|
|
159
|
+
*/
|
|
132
160
|
get size() {
|
|
133
161
|
return this.totalConnections;
|
|
134
162
|
}
|
|
135
|
-
// ---- Internal ----
|
|
136
163
|
isExpired(entry, now) {
|
|
137
164
|
if (now - entry.createdAt > this.options.maxAge)
|
|
138
165
|
return true;
|
package/dist/http/pool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../src/http/pool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../src/http/pool.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AA0C1C,MAAM,oBAAoB,GAA0B;IAClD,uBAAuB,EAAE,CAAC;IAC1B,mBAAmB,EAAE,EAAE;IACvB,WAAW,EAAE,MAAM;IACnB,MAAM,EAAE,OAAO;CAChB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,OAAO,cAAc;IACR,OAAO,CAAwB;IAC/B,WAAW,GAAG,IAAI,GAAG,EAAuB,CAAC;IACtD,gBAAgB,GAAG,CAAC,CAAC;IACrB,YAAY,CAA6C;IAEjE;;;;OAIG;IACH,YAAY,UAAuB,EAAE;QACnC,IAAI,CAAC,OAAO,GAAG,EAAE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O,CAAC,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YACjD,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;gBAC5B,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;oBAChB,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;wBACtD,MAAM,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;oBAC7B,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAED,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* Zero runtime dependencies. Uses Node.js built-in modules exclusively.
|
|
2
|
+
* Public API entry point for the NLcURL library. Re-exports all types, classes,
|
|
3
|
+
* and functions that form the stable public surface of the package.
|
|
5
4
|
*
|
|
6
5
|
* @module nlcurl
|
|
7
6
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA;;;;;GAKG;AACH,YAAY,EACV,UAAU,EACV,aAAa,EACb,mBAAmB,EACnB,WAAW,EACX,cAAc,EACd,WAAW,EACX,aAAa,GACd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,EACL,WAAW,EACX,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,UAAU,EACV,UAAU,EACV,eAAe,EACf,aAAa,GACd,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACvE,OAAO,EACL,aAAa,EACb,OAAO,EACP,GAAG,EACH,IAAI,EACJ,GAAG,EACH,KAAK,EACL,GAAG,EACH,IAAI,GACL,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACL,UAAU,EACV,YAAY,EACZ,eAAe,GAChB,MAAM,4BAA4B,CAAC;AAEpC,YAAY,EACV,cAAc,EACd,UAAU,EACV,SAAS,EACT,aAAa,GACd,MAAM,yBAAyB,CAAC;AAEjC,YAAY,EACV,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,6BAA6B,CAAC;AAErC,YAAY,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAEpE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,eAAe,EAAE,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE9F,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* NLcURL -- Pure TypeScript HTTP client with browser fingerprint impersonation.
|
|
3
|
-
*
|
|
4
|
-
* Zero runtime dependencies. Uses Node.js built-in modules exclusively.
|
|
5
|
-
*
|
|
6
|
-
* @module nlcurl
|
|
7
|
-
*/
|
|
8
1
|
export { NLcURLResponse } from './core/response.js';
|
|
9
|
-
// ---- Error types ----
|
|
10
2
|
export { NLcURLError, TLSError, HTTPError, TimeoutError, ProxyError, AbortError, ConnectionError, ProtocolError, } from './core/errors.js';
|
|
11
|
-
// ---- Session and client ----
|
|
12
3
|
export { NLcURLSession } from './core/session.js';
|
|
13
4
|
export { createSession, request, get, post, put, patch, del, head, } from './core/client.js';
|
|
14
|
-
// ---- Fingerprints ----
|
|
15
5
|
export { getProfile, listProfiles, DEFAULT_PROFILE, } from './fingerprints/database.js';
|
|
16
|
-
// ---- Cookies ----
|
|
17
6
|
export { CookieJar } from './cookies/jar.js';
|
|
18
|
-
// ---- WebSocket ----
|
|
19
7
|
export { WebSocketClient } from './ws/client.js';
|
|
20
|
-
// ---- Utilities ----
|
|
21
8
|
export { ja3Hash, ja3String } from './fingerprints/ja3.js';
|
|
22
9
|
export { akamaiFingerprint } from './fingerprints/akamai.js';
|
|
23
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAGpD,OAAO,EACL,WAAW,EACX,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,UAAU,EACV,UAAU,EACV,eAAe,EACf,aAAa,GACd,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,aAAa,EAAuB,MAAM,mBAAmB,CAAC;AACvE,OAAO,EACL,aAAa,EACb,OAAO,EACP,GAAG,EACH,IAAI,EACJ,GAAG,EACH,KAAK,EACL,GAAG,EACH,IAAI,GACL,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EACL,UAAU,EACV,YAAY,EACZ,eAAe,GAChB,MAAM,4BAA4B,CAAC;AAgBpC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,EAAE,eAAe,EAA+C,MAAM,gBAAgB,CAAC;AAE9F,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC"}
|
|
@@ -1,27 +1,59 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Request/response interceptor middleware.
|
|
3
|
-
*/
|
|
4
1
|
import type { NLcURLRequest } from '../core/request.js';
|
|
5
2
|
import { NLcURLResponse } from '../core/response.js';
|
|
6
3
|
/**
|
|
7
|
-
* A function that
|
|
4
|
+
* A function that inspects and optionally transforms an outgoing request before
|
|
5
|
+
* it is sent. May be synchronous or asynchronous.
|
|
6
|
+
*
|
|
7
|
+
* @callback RequestInterceptor
|
|
8
|
+
* @param {NLcURLRequest} request - The outgoing request descriptor.
|
|
9
|
+
* @returns {NLcURLRequest | Promise<NLcURLRequest>} The (potentially modified) request.
|
|
8
10
|
*/
|
|
9
11
|
export type RequestInterceptor = (request: NLcURLRequest) => NLcURLRequest | Promise<NLcURLRequest>;
|
|
10
12
|
/**
|
|
11
|
-
* A function that
|
|
13
|
+
* A function that inspects and optionally transforms an incoming response before
|
|
14
|
+
* it is returned to the caller. May be synchronous or asynchronous.
|
|
15
|
+
*
|
|
16
|
+
* @callback ResponseInterceptor
|
|
17
|
+
* @param {NLcURLResponse} response - The received response.
|
|
18
|
+
* @returns {NLcURLResponse | Promise<NLcURLResponse>} The (potentially modified) response.
|
|
12
19
|
*/
|
|
13
20
|
export type ResponseInterceptor = (response: NLcURLResponse) => NLcURLResponse | Promise<NLcURLResponse>;
|
|
14
21
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* Interceptors are executed in FIFO order.
|
|
22
|
+
* Maintains ordered lists of request and response interceptors and applies
|
|
23
|
+
* them sequentially. Interceptors are invoked in the order they were added.
|
|
18
24
|
*/
|
|
19
25
|
export declare class InterceptorChain {
|
|
20
26
|
private readonly requestInterceptors;
|
|
21
27
|
private readonly responseInterceptors;
|
|
28
|
+
/**
|
|
29
|
+
* Registers a request interceptor. Interceptors are applied in registration order.
|
|
30
|
+
*
|
|
31
|
+
* @param {RequestInterceptor} fn - Interceptor function to add.
|
|
32
|
+
* @returns {this} This instance for chaining.
|
|
33
|
+
*/
|
|
22
34
|
addRequestInterceptor(fn: RequestInterceptor): this;
|
|
35
|
+
/**
|
|
36
|
+
* Registers a response interceptor. Interceptors are applied in registration order.
|
|
37
|
+
*
|
|
38
|
+
* @param {ResponseInterceptor} fn - Interceptor function to add.
|
|
39
|
+
* @returns {this} This instance for chaining.
|
|
40
|
+
*/
|
|
23
41
|
addResponseInterceptor(fn: ResponseInterceptor): this;
|
|
42
|
+
/**
|
|
43
|
+
* Passes `request` through all registered request interceptors in order and
|
|
44
|
+
* returns the final transformed request.
|
|
45
|
+
*
|
|
46
|
+
* @param {NLcURLRequest} request - Originating request descriptor.
|
|
47
|
+
* @returns {Promise<NLcURLRequest>} The request after all interceptors have run.
|
|
48
|
+
*/
|
|
24
49
|
processRequest(request: NLcURLRequest): Promise<NLcURLRequest>;
|
|
50
|
+
/**
|
|
51
|
+
* Passes `response` through all registered response interceptors in order and
|
|
52
|
+
* returns the final transformed response.
|
|
53
|
+
*
|
|
54
|
+
* @param {NLcURLResponse} response - Received response.
|
|
55
|
+
* @returns {Promise<NLcURLResponse>} The response after all interceptors have run.
|
|
56
|
+
*/
|
|
25
57
|
processResponse(response: NLcURLResponse): Promise<NLcURLResponse>;
|
|
26
58
|
}
|
|
27
59
|
//# sourceMappingURL=interceptor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../../src/middleware/interceptor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../../src/middleware/interceptor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;;;;;GAOG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAC/B,OAAO,EAAE,aAAa,KACnB,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;AAE5C;;;;;;;GAOG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAChC,QAAQ,EAAE,cAAc,KACrB,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;AAE9C;;;GAGG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA4B;IAChE,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAA6B;IAElE;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,EAAE,kBAAkB,GAAG,IAAI;IAKnD;;;;;OAKG;IACH,sBAAsB,CAAC,EAAE,EAAE,mBAAmB,GAAG,IAAI;IAKrD;;;;;;OAMG;IACG,cAAc,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAQpE;;;;;;OAMG;IACG,eAAe,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;CAOzE"}
|
|
@@ -1,22 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Middleware chain.
|
|
6
|
-
*
|
|
7
|
-
* Interceptors are executed in FIFO order.
|
|
2
|
+
* Maintains ordered lists of request and response interceptors and applies
|
|
3
|
+
* them sequentially. Interceptors are invoked in the order they were added.
|
|
8
4
|
*/
|
|
9
5
|
export class InterceptorChain {
|
|
10
6
|
requestInterceptors = [];
|
|
11
7
|
responseInterceptors = [];
|
|
8
|
+
/**
|
|
9
|
+
* Registers a request interceptor. Interceptors are applied in registration order.
|
|
10
|
+
*
|
|
11
|
+
* @param {RequestInterceptor} fn - Interceptor function to add.
|
|
12
|
+
* @returns {this} This instance for chaining.
|
|
13
|
+
*/
|
|
12
14
|
addRequestInterceptor(fn) {
|
|
13
15
|
this.requestInterceptors.push(fn);
|
|
14
16
|
return this;
|
|
15
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Registers a response interceptor. Interceptors are applied in registration order.
|
|
20
|
+
*
|
|
21
|
+
* @param {ResponseInterceptor} fn - Interceptor function to add.
|
|
22
|
+
* @returns {this} This instance for chaining.
|
|
23
|
+
*/
|
|
16
24
|
addResponseInterceptor(fn) {
|
|
17
25
|
this.responseInterceptors.push(fn);
|
|
18
26
|
return this;
|
|
19
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Passes `request` through all registered request interceptors in order and
|
|
30
|
+
* returns the final transformed request.
|
|
31
|
+
*
|
|
32
|
+
* @param {NLcURLRequest} request - Originating request descriptor.
|
|
33
|
+
* @returns {Promise<NLcURLRequest>} The request after all interceptors have run.
|
|
34
|
+
*/
|
|
20
35
|
async processRequest(request) {
|
|
21
36
|
let req = request;
|
|
22
37
|
for (const fn of this.requestInterceptors) {
|
|
@@ -24,6 +39,13 @@ export class InterceptorChain {
|
|
|
24
39
|
}
|
|
25
40
|
return req;
|
|
26
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Passes `response` through all registered response interceptors in order and
|
|
44
|
+
* returns the final transformed response.
|
|
45
|
+
*
|
|
46
|
+
* @param {NLcURLResponse} response - Received response.
|
|
47
|
+
* @returns {Promise<NLcURLResponse>} The response after all interceptors have run.
|
|
48
|
+
*/
|
|
27
49
|
async processResponse(response) {
|
|
28
50
|
let resp = response;
|
|
29
51
|
for (const fn of this.responseInterceptors) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptor.js","sourceRoot":"","sources":["../../src/middleware/interceptor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"interceptor.js","sourceRoot":"","sources":["../../src/middleware/interceptor.ts"],"names":[],"mappings":"AA4BA;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IACV,mBAAmB,GAAyB,EAAE,CAAC;IAC/C,oBAAoB,GAA0B,EAAE,CAAC;IAElE;;;;;OAKG;IACH,qBAAqB,CAAC,EAAsB;QAC1C,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAClC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,sBAAsB,CAAC,EAAuB;QAC5C,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACnC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,cAAc,CAAC,OAAsB;QACzC,IAAI,GAAG,GAAG,OAAO,CAAC;QAClB,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,mBAAmB,EAAE,CAAC;YAC1C,GAAG,GAAG,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,eAAe,CAAC,QAAwB;QAC5C,IAAI,IAAI,GAAG,QAAQ,CAAC;QACpB,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,oBAAoB,EAAE,CAAC;YAC3C,IAAI,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
|
@@ -1,23 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Configuration options for a token-bucket rate limiter.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* @typedef {Object} RateLimitConfig
|
|
5
|
+
* @property {number} maxRequests - Maximum number of requests allowed per `windowMs` interval.
|
|
6
|
+
* @property {number} windowMs - Duration of the rate-limiting window in milliseconds.
|
|
5
7
|
*/
|
|
6
8
|
export interface RateLimitConfig {
|
|
7
|
-
/** Maximum requests per window. */
|
|
8
9
|
maxRequests: number;
|
|
9
|
-
/** Window duration in milliseconds. */
|
|
10
10
|
windowMs: number;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Token-bucket rate limiter. Callers must call {@link RateLimiter.acquire} and
|
|
14
|
+
* await the returned promise before sending each request. Requests that would
|
|
15
|
+
* exceed the configured rate are queued and granted once the window refills.
|
|
16
|
+
*/
|
|
12
17
|
export declare class RateLimiter {
|
|
13
18
|
private readonly maxRequests;
|
|
14
19
|
private readonly windowMs;
|
|
15
20
|
private tokens;
|
|
16
21
|
private lastRefill;
|
|
17
22
|
private waitQueue;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new RateLimiter.
|
|
25
|
+
*
|
|
26
|
+
* @param {RateLimitConfig} config - Rate-limit parameters.
|
|
27
|
+
*/
|
|
18
28
|
constructor(config: RateLimitConfig);
|
|
19
29
|
/**
|
|
20
|
-
*
|
|
30
|
+
* Acquires a rate-limit token. Resolves immediately when a token is
|
|
31
|
+
* available, or waits until the current window refills.
|
|
32
|
+
*
|
|
33
|
+
* @returns {Promise<void>} Resolves once a token has been granted to the caller.
|
|
21
34
|
*/
|
|
22
35
|
acquire(): Promise<void>;
|
|
23
36
|
private drain;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limiter.d.ts","sourceRoot":"","sources":["../../src/middleware/rate-limiter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rate-limiter.d.ts","sourceRoot":"","sources":["../../src/middleware/rate-limiter.ts"],"names":[],"mappings":"AACA;;;;;;GAMG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;GAIG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,SAAS,CAAyB;IAE1C;;;;OAIG;gBACS,MAAM,EAAE,eAAe;IAOnC;;;;;OAKG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAgB9B,OAAO,CAAC,KAAK;IAeb,OAAO,CAAC,MAAM;CAQf"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Token-bucket rate limiter. Callers must call {@link RateLimiter.acquire} and
|
|
3
|
+
* await the returned promise before sending each request. Requests that would
|
|
4
|
+
* exceed the configured rate are queued and granted once the window refills.
|
|
5
5
|
*/
|
|
6
6
|
export class RateLimiter {
|
|
7
7
|
maxRequests;
|
|
@@ -9,6 +9,11 @@ export class RateLimiter {
|
|
|
9
9
|
tokens;
|
|
10
10
|
lastRefill;
|
|
11
11
|
waitQueue = [];
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new RateLimiter.
|
|
14
|
+
*
|
|
15
|
+
* @param {RateLimitConfig} config - Rate-limit parameters.
|
|
16
|
+
*/
|
|
12
17
|
constructor(config) {
|
|
13
18
|
this.maxRequests = config.maxRequests;
|
|
14
19
|
this.windowMs = config.windowMs;
|
|
@@ -16,7 +21,10 @@ export class RateLimiter {
|
|
|
16
21
|
this.lastRefill = Date.now();
|
|
17
22
|
}
|
|
18
23
|
/**
|
|
19
|
-
*
|
|
24
|
+
* Acquires a rate-limit token. Resolves immediately when a token is
|
|
25
|
+
* available, or waits until the current window refills.
|
|
26
|
+
*
|
|
27
|
+
* @returns {Promise<void>} Resolves once a token has been granted to the caller.
|
|
20
28
|
*/
|
|
21
29
|
async acquire() {
|
|
22
30
|
this.refill();
|
|
@@ -24,10 +32,8 @@ export class RateLimiter {
|
|
|
24
32
|
this.tokens--;
|
|
25
33
|
return;
|
|
26
34
|
}
|
|
27
|
-
// Enqueue and wait for a token to become available
|
|
28
35
|
await new Promise((resolve) => {
|
|
29
36
|
this.waitQueue.push(resolve);
|
|
30
|
-
// Schedule a refill check after the current window expires
|
|
31
37
|
const elapsed = Date.now() - this.lastRefill;
|
|
32
38
|
const waitMs = Math.max(1, this.windowMs - elapsed);
|
|
33
39
|
setTimeout(() => this.drain(), waitMs);
|
|
@@ -40,7 +46,6 @@ export class RateLimiter {
|
|
|
40
46
|
const next = this.waitQueue.shift();
|
|
41
47
|
next();
|
|
42
48
|
}
|
|
43
|
-
// If there are still waiters, schedule another drain
|
|
44
49
|
if (this.waitQueue.length > 0) {
|
|
45
50
|
const elapsed = Date.now() - this.lastRefill;
|
|
46
51
|
const waitMs = Math.max(1, this.windowMs - elapsed);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rate-limiter.js","sourceRoot":"","sources":["../../src/middleware/rate-limiter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rate-limiter.js","sourceRoot":"","sources":["../../src/middleware/rate-limiter.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,OAAO,WAAW;IACL,WAAW,CAAS;IACpB,QAAQ,CAAS;IAC1B,MAAM,CAAS;IACf,UAAU,CAAS;IACnB,SAAS,GAAsB,EAAE,CAAC;IAE1C;;;;OAIG;IACH,YAAY,MAAuB;QACjC,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;QACtC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC/B,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,MAAM,EAAE,CAAC;QAEd,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpB,IAAI,CAAC,MAAM,EAAE,CAAC;YACd,OAAO;QACT,CAAC;QAED,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YAClC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YAC7B,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC;YAC7C,MAAM,MAAM,GAAG,IAAI,CAAC,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