sol-trade-sdk 0.1.0 → 0.1.2
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 +12 -5
- package/dist/cache/index.d.mts +70 -0
- package/dist/cache/index.d.ts +70 -0
- package/dist/cache/index.js +259 -0
- package/dist/cache/index.mjs +19 -0
- package/dist/chunk-64JY4YH6.mjs +227 -0
- package/dist/chunk-6BWS3CLP.mjs +16 -0
- package/dist/{chunk-NEZDFAYA.mjs → chunk-H4ZUMTTP.mjs} +864 -336
- package/dist/{chunk-MMQAMIKR.mjs → chunk-L6RZIXXN.mjs} +4 -11
- package/dist/{clients-VITWK7B6.mjs → chunk-V7QDWUXJ.mjs} +39 -39
- package/dist/clients-PSG35HN4.mjs +107 -0
- package/dist/index-hZh5hrGj.d.mts +3089 -0
- package/dist/index-yJ5AsNfm.d.ts +3089 -0
- package/dist/index.d.mts +2 -2658
- package/dist/index.d.ts +2 -2658
- package/dist/index.js +794 -278
- package/dist/index.mjs +21 -4
- package/dist/perf/index.mjs +2 -1
- package/dist/pool/index.d.mts +88 -0
- package/dist/pool/index.d.ts +88 -0
- package/dist/pool/index.js +262 -0
- package/dist/pool/index.mjs +233 -0
- package/dist/rpc/index.d.mts +77 -0
- package/dist/rpc/index.d.ts +77 -0
- package/dist/rpc/index.js +447 -0
- package/dist/rpc/index.mjs +223 -0
- package/dist/swqos/index.d.mts +416 -0
- package/dist/swqos/index.d.ts +416 -0
- package/dist/swqos/index.js +3450 -0
- package/dist/swqos/index.mjs +1375 -0
- package/dist/trading/index.d.mts +998 -0
- package/dist/trading/index.d.ts +998 -0
- package/dist/trading/index.js +4302 -0
- package/dist/trading/index.mjs +2097 -0
- package/package.json +5 -4
- package/src/__tests__/hotpath.test.ts +33 -0
- package/src/__tests__/parser-compat.test.ts +82 -0
- package/src/__tests__/protocol-parity.test.ts +242 -0
- package/src/__tests__/sdk.test.ts +16 -0
- package/src/cache/index.ts +1 -0
- package/src/hotpath/executor.ts +25 -18
- package/src/index.ts +330 -92
- package/src/instruction/meteora_damm_v2_builder.ts +88 -20
- package/src/instruction/pumpfun_builder.ts +133 -43
- package/src/instruction/raydium_amm_v4_builder.ts +244 -35
- package/src/instruction/raydium_cpmm_builder.ts +87 -69
- package/src/params/index.ts +38 -5
- package/src/pool/index.ts +1 -0
- package/src/rpc/index.ts +1 -0
- package/src/trading/factory.ts +10 -0
package/dist/index.mjs
CHANGED
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
DEFAULT_PRIORITY_FEE,
|
|
28
28
|
DEFAULT_SLIPPAGE,
|
|
29
29
|
DEFAULT_TIP_LAMPORTS,
|
|
30
|
-
DexType,
|
|
30
|
+
DexType2 as DexType,
|
|
31
31
|
ExecutionPath,
|
|
32
32
|
FilterMiddleware,
|
|
33
33
|
GasFeeStrategy,
|
|
@@ -40,12 +40,15 @@ import {
|
|
|
40
40
|
KNOWN_PROGRAM_IDS,
|
|
41
41
|
KeyNotAvailableError,
|
|
42
42
|
LoggingMiddleware,
|
|
43
|
+
MARKET_STATE_SIZE,
|
|
43
44
|
MAX_INSTRUCTIONS_WARN,
|
|
44
45
|
METEORA_DAMM_V2_AUTHORITY,
|
|
45
46
|
METEORA_DAMM_V2_EVENT_AUTHORITY_SEED,
|
|
46
47
|
METEORA_DAMM_V2_PROGRAM,
|
|
47
48
|
METEORA_DAMM_V2_PROGRAM_ID,
|
|
49
|
+
METEORA_DAMM_V2_SWAP2_DISCRIMINATOR,
|
|
48
50
|
METEORA_DAMM_V2_SWAP_DISCRIMINATOR,
|
|
51
|
+
METEORA_DAMM_V2_SWAP_MODE_PARTIAL_FILL,
|
|
49
52
|
METEORA_POOL_SIZE,
|
|
50
53
|
MeteoraDammV2Executor,
|
|
51
54
|
MeteoraDammV2Params,
|
|
@@ -148,7 +151,7 @@ import {
|
|
|
148
151
|
TradeError,
|
|
149
152
|
TradeExecutorFactory,
|
|
150
153
|
TradeTokenType,
|
|
151
|
-
TradeType,
|
|
154
|
+
TradeType2 as TradeType,
|
|
152
155
|
TradingClient,
|
|
153
156
|
TradingContext,
|
|
154
157
|
TransactionBuilder,
|
|
@@ -191,18 +194,21 @@ import {
|
|
|
191
194
|
createTradeConfig,
|
|
192
195
|
decodeAmmInfo,
|
|
193
196
|
decodeBonkPoolState,
|
|
197
|
+
decodeMarketState,
|
|
194
198
|
decodeMeteoraPool,
|
|
195
199
|
decodePool,
|
|
196
200
|
decodeRaydiumCPMMpoolState,
|
|
197
201
|
defaultExecuteOptions,
|
|
198
202
|
defaultHotPathConfig,
|
|
199
203
|
defaultTradeExecuteOptions,
|
|
204
|
+
deriveSerumVaultSigner,
|
|
200
205
|
extractHintsFromLogs,
|
|
201
206
|
fetchAddressLookupTableAccount,
|
|
202
207
|
fetchAmmInfo,
|
|
203
208
|
fetchBondingCurveAccount,
|
|
204
209
|
fetchBonkPoolState,
|
|
205
210
|
fetchDurableNonceInfo,
|
|
211
|
+
fetchMarketState,
|
|
206
212
|
fetchMeteoraPool,
|
|
207
213
|
fetchPool,
|
|
208
214
|
fetchRaydiumCPMMpoolState,
|
|
@@ -261,6 +267,8 @@ import {
|
|
|
261
267
|
nowMicroseconds,
|
|
262
268
|
parseAddressLookupTable,
|
|
263
269
|
pumpFunFeeRecipientMeta,
|
|
270
|
+
pumpFunParamsFromParserTrade,
|
|
271
|
+
pumpSwapParamsFromParserTrade,
|
|
264
272
|
recommendedSenderThreadCoreIndices,
|
|
265
273
|
retryWithBackoff,
|
|
266
274
|
signWithKeypair,
|
|
@@ -276,10 +284,11 @@ import {
|
|
|
276
284
|
validateTransactionSize,
|
|
277
285
|
withAllBuiltinMiddlewares,
|
|
278
286
|
withStandardMiddlewares
|
|
279
|
-
} from "./chunk-
|
|
287
|
+
} from "./chunk-H4ZUMTTP.mjs";
|
|
280
288
|
import {
|
|
281
289
|
perf_exports
|
|
282
|
-
} from "./chunk-
|
|
290
|
+
} from "./chunk-L6RZIXXN.mjs";
|
|
291
|
+
import "./chunk-6BWS3CLP.mjs";
|
|
283
292
|
export {
|
|
284
293
|
AMM_INFO_SIZE,
|
|
285
294
|
ASSOCIATED_TOKEN_PROGRAM,
|
|
@@ -322,12 +331,15 @@ export {
|
|
|
322
331
|
KNOWN_PROGRAM_IDS,
|
|
323
332
|
KeyNotAvailableError,
|
|
324
333
|
LoggingMiddleware,
|
|
334
|
+
MARKET_STATE_SIZE,
|
|
325
335
|
MAX_INSTRUCTIONS_WARN,
|
|
326
336
|
METEORA_DAMM_V2_AUTHORITY,
|
|
327
337
|
METEORA_DAMM_V2_EVENT_AUTHORITY_SEED,
|
|
328
338
|
METEORA_DAMM_V2_PROGRAM,
|
|
329
339
|
METEORA_DAMM_V2_PROGRAM_ID,
|
|
340
|
+
METEORA_DAMM_V2_SWAP2_DISCRIMINATOR,
|
|
330
341
|
METEORA_DAMM_V2_SWAP_DISCRIMINATOR,
|
|
342
|
+
METEORA_DAMM_V2_SWAP_MODE_PARTIAL_FILL,
|
|
331
343
|
METEORA_POOL_SIZE,
|
|
332
344
|
MeteoraDammV2Executor,
|
|
333
345
|
MeteoraDammV2Params,
|
|
@@ -473,18 +485,21 @@ export {
|
|
|
473
485
|
createTradeConfig,
|
|
474
486
|
decodeAmmInfo,
|
|
475
487
|
decodeBonkPoolState,
|
|
488
|
+
decodeMarketState,
|
|
476
489
|
decodeMeteoraPool,
|
|
477
490
|
decodePool,
|
|
478
491
|
decodeRaydiumCPMMpoolState,
|
|
479
492
|
defaultExecuteOptions,
|
|
480
493
|
defaultHotPathConfig,
|
|
481
494
|
defaultTradeExecuteOptions,
|
|
495
|
+
deriveSerumVaultSigner,
|
|
482
496
|
extractHintsFromLogs,
|
|
483
497
|
fetchAddressLookupTableAccount,
|
|
484
498
|
fetchAmmInfo,
|
|
485
499
|
fetchBondingCurveAccount,
|
|
486
500
|
fetchBonkPoolState,
|
|
487
501
|
fetchDurableNonceInfo,
|
|
502
|
+
fetchMarketState,
|
|
488
503
|
fetchMeteoraPool,
|
|
489
504
|
fetchPool,
|
|
490
505
|
fetchRaydiumCPMMpoolState,
|
|
@@ -544,6 +559,8 @@ export {
|
|
|
544
559
|
parseAddressLookupTable,
|
|
545
560
|
perf_exports as perf,
|
|
546
561
|
pumpFunFeeRecipientMeta,
|
|
562
|
+
pumpFunParamsFromParserTrade,
|
|
563
|
+
pumpSwapParamsFromParserTrade,
|
|
547
564
|
recommendedSenderThreadCoreIndices,
|
|
548
565
|
retryWithBackoff,
|
|
549
566
|
signWithKeypair,
|
package/dist/perf/index.mjs
CHANGED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-Performance Pool and Concurrency Utilities for Sol Trade SDK
|
|
3
|
+
*/
|
|
4
|
+
interface PoolStats {
|
|
5
|
+
activeTasks: number;
|
|
6
|
+
tasksCompleted: number;
|
|
7
|
+
queueSize: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* High-performance worker pool for parallel task execution.
|
|
11
|
+
*/
|
|
12
|
+
declare class WorkerPool {
|
|
13
|
+
private taskQueue;
|
|
14
|
+
private activeWorkers;
|
|
15
|
+
private tasksCompleted;
|
|
16
|
+
private maxWorkers;
|
|
17
|
+
private maxQueueSize;
|
|
18
|
+
private idleResolvers;
|
|
19
|
+
constructor(workers?: number, maxQueueSize?: number);
|
|
20
|
+
submit<T>(task: () => Promise<T>): Promise<T>;
|
|
21
|
+
submitBatch<T>(tasks: Array<() => Promise<T>>): Promise<T[]>;
|
|
22
|
+
private processQueue;
|
|
23
|
+
idle(): Promise<void>;
|
|
24
|
+
getStats(): PoolStats;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Token bucket rate limiter.
|
|
28
|
+
*/
|
|
29
|
+
declare class RateLimiter {
|
|
30
|
+
private rate;
|
|
31
|
+
private burst;
|
|
32
|
+
private tokens;
|
|
33
|
+
private lastUpdate;
|
|
34
|
+
constructor(rate: number, burst?: number);
|
|
35
|
+
allow(): boolean;
|
|
36
|
+
wait(): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Rate limiter with multiple keys.
|
|
40
|
+
*/
|
|
41
|
+
declare class MultiRateLimiter {
|
|
42
|
+
private rate;
|
|
43
|
+
private burst;
|
|
44
|
+
private limiters;
|
|
45
|
+
constructor(rate: number, burst?: number);
|
|
46
|
+
allow(key: string): boolean;
|
|
47
|
+
wait(key: string): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
interface Connection {
|
|
50
|
+
close(): void;
|
|
51
|
+
isAlive(): boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Generic connection pool with automatic management.
|
|
55
|
+
*/
|
|
56
|
+
declare class ConnectionPool<T extends Connection> {
|
|
57
|
+
private factory;
|
|
58
|
+
private maxSize;
|
|
59
|
+
private timeout;
|
|
60
|
+
private pool;
|
|
61
|
+
private created;
|
|
62
|
+
private waiting;
|
|
63
|
+
constructor(factory: () => T, maxSize?: number, timeout?: number);
|
|
64
|
+
get(): Promise<T>;
|
|
65
|
+
release(conn: T): void;
|
|
66
|
+
private create;
|
|
67
|
+
closeAll(): void;
|
|
68
|
+
getStats(): {
|
|
69
|
+
created: number;
|
|
70
|
+
available: number;
|
|
71
|
+
waiting: number;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Generic object pool for reusing expensive objects.
|
|
76
|
+
*/
|
|
77
|
+
declare class ObjectPool<T> {
|
|
78
|
+
private factory;
|
|
79
|
+
private resetFunc?;
|
|
80
|
+
private maxSize;
|
|
81
|
+
private pool;
|
|
82
|
+
constructor(factory: () => T, resetFunc?: ((obj: T) => void) | undefined, maxSize?: number);
|
|
83
|
+
acquire(): T;
|
|
84
|
+
release(obj: T): void;
|
|
85
|
+
with<R>(fn: (obj: T) => R): R;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export { type Connection, ConnectionPool, MultiRateLimiter, ObjectPool, RateLimiter, WorkerPool };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-Performance Pool and Concurrency Utilities for Sol Trade SDK
|
|
3
|
+
*/
|
|
4
|
+
interface PoolStats {
|
|
5
|
+
activeTasks: number;
|
|
6
|
+
tasksCompleted: number;
|
|
7
|
+
queueSize: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* High-performance worker pool for parallel task execution.
|
|
11
|
+
*/
|
|
12
|
+
declare class WorkerPool {
|
|
13
|
+
private taskQueue;
|
|
14
|
+
private activeWorkers;
|
|
15
|
+
private tasksCompleted;
|
|
16
|
+
private maxWorkers;
|
|
17
|
+
private maxQueueSize;
|
|
18
|
+
private idleResolvers;
|
|
19
|
+
constructor(workers?: number, maxQueueSize?: number);
|
|
20
|
+
submit<T>(task: () => Promise<T>): Promise<T>;
|
|
21
|
+
submitBatch<T>(tasks: Array<() => Promise<T>>): Promise<T[]>;
|
|
22
|
+
private processQueue;
|
|
23
|
+
idle(): Promise<void>;
|
|
24
|
+
getStats(): PoolStats;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Token bucket rate limiter.
|
|
28
|
+
*/
|
|
29
|
+
declare class RateLimiter {
|
|
30
|
+
private rate;
|
|
31
|
+
private burst;
|
|
32
|
+
private tokens;
|
|
33
|
+
private lastUpdate;
|
|
34
|
+
constructor(rate: number, burst?: number);
|
|
35
|
+
allow(): boolean;
|
|
36
|
+
wait(): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Rate limiter with multiple keys.
|
|
40
|
+
*/
|
|
41
|
+
declare class MultiRateLimiter {
|
|
42
|
+
private rate;
|
|
43
|
+
private burst;
|
|
44
|
+
private limiters;
|
|
45
|
+
constructor(rate: number, burst?: number);
|
|
46
|
+
allow(key: string): boolean;
|
|
47
|
+
wait(key: string): Promise<void>;
|
|
48
|
+
}
|
|
49
|
+
interface Connection {
|
|
50
|
+
close(): void;
|
|
51
|
+
isAlive(): boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Generic connection pool with automatic management.
|
|
55
|
+
*/
|
|
56
|
+
declare class ConnectionPool<T extends Connection> {
|
|
57
|
+
private factory;
|
|
58
|
+
private maxSize;
|
|
59
|
+
private timeout;
|
|
60
|
+
private pool;
|
|
61
|
+
private created;
|
|
62
|
+
private waiting;
|
|
63
|
+
constructor(factory: () => T, maxSize?: number, timeout?: number);
|
|
64
|
+
get(): Promise<T>;
|
|
65
|
+
release(conn: T): void;
|
|
66
|
+
private create;
|
|
67
|
+
closeAll(): void;
|
|
68
|
+
getStats(): {
|
|
69
|
+
created: number;
|
|
70
|
+
available: number;
|
|
71
|
+
waiting: number;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Generic object pool for reusing expensive objects.
|
|
76
|
+
*/
|
|
77
|
+
declare class ObjectPool<T> {
|
|
78
|
+
private factory;
|
|
79
|
+
private resetFunc?;
|
|
80
|
+
private maxSize;
|
|
81
|
+
private pool;
|
|
82
|
+
constructor(factory: () => T, resetFunc?: ((obj: T) => void) | undefined, maxSize?: number);
|
|
83
|
+
acquire(): T;
|
|
84
|
+
release(obj: T): void;
|
|
85
|
+
with<R>(fn: (obj: T) => R): R;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export { type Connection, ConnectionPool, MultiRateLimiter, ObjectPool, RateLimiter, WorkerPool };
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/pool/index.ts
|
|
21
|
+
var pool_exports = {};
|
|
22
|
+
__export(pool_exports, {
|
|
23
|
+
ConnectionPool: () => ConnectionPool,
|
|
24
|
+
MultiRateLimiter: () => MultiRateLimiter,
|
|
25
|
+
ObjectPool: () => ObjectPool,
|
|
26
|
+
RateLimiter: () => RateLimiter,
|
|
27
|
+
WorkerPool: () => WorkerPool
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(pool_exports);
|
|
30
|
+
|
|
31
|
+
// src/pool/pool.ts
|
|
32
|
+
var WorkerPool = class {
|
|
33
|
+
taskQueue = [];
|
|
34
|
+
activeWorkers = 0;
|
|
35
|
+
tasksCompleted = 0;
|
|
36
|
+
maxWorkers;
|
|
37
|
+
maxQueueSize;
|
|
38
|
+
idleResolvers = [];
|
|
39
|
+
constructor(workers = 4, maxQueueSize = 100) {
|
|
40
|
+
this.maxWorkers = workers;
|
|
41
|
+
this.maxQueueSize = maxQueueSize;
|
|
42
|
+
}
|
|
43
|
+
async submit(task) {
|
|
44
|
+
if (this.taskQueue.length >= this.maxQueueSize) {
|
|
45
|
+
throw new Error("Task queue is full");
|
|
46
|
+
}
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
this.taskQueue.push(async () => {
|
|
49
|
+
try {
|
|
50
|
+
const result = await task();
|
|
51
|
+
resolve(result);
|
|
52
|
+
return result;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
reject(error);
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
this.processQueue();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async submitBatch(tasks) {
|
|
62
|
+
return Promise.all(tasks.map((task) => this.submit(task)));
|
|
63
|
+
}
|
|
64
|
+
async processQueue() {
|
|
65
|
+
while (this.taskQueue.length > 0 && this.activeWorkers < this.maxWorkers) {
|
|
66
|
+
const task = this.taskQueue.shift();
|
|
67
|
+
this.activeWorkers++;
|
|
68
|
+
try {
|
|
69
|
+
await task();
|
|
70
|
+
} finally {
|
|
71
|
+
this.activeWorkers--;
|
|
72
|
+
this.tasksCompleted++;
|
|
73
|
+
this.processQueue();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (this.activeWorkers === 0 && this.taskQueue.length === 0) {
|
|
77
|
+
this.idleResolvers.forEach((resolve) => resolve());
|
|
78
|
+
this.idleResolvers = [];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async idle() {
|
|
82
|
+
if (this.activeWorkers === 0 && this.taskQueue.length === 0) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
return new Promise((resolve) => {
|
|
86
|
+
this.idleResolvers.push(resolve);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
getStats() {
|
|
90
|
+
return {
|
|
91
|
+
activeTasks: this.activeWorkers,
|
|
92
|
+
tasksCompleted: this.tasksCompleted,
|
|
93
|
+
queueSize: this.taskQueue.length
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var RateLimiter = class {
|
|
98
|
+
constructor(rate, burst = 10) {
|
|
99
|
+
this.rate = rate;
|
|
100
|
+
this.burst = burst;
|
|
101
|
+
this.tokens = burst;
|
|
102
|
+
this.lastUpdate = Date.now();
|
|
103
|
+
}
|
|
104
|
+
rate;
|
|
105
|
+
burst;
|
|
106
|
+
tokens;
|
|
107
|
+
lastUpdate;
|
|
108
|
+
allow() {
|
|
109
|
+
const now = Date.now();
|
|
110
|
+
const elapsed = (now - this.lastUpdate) / 1e3;
|
|
111
|
+
this.lastUpdate = now;
|
|
112
|
+
this.tokens += elapsed * this.rate;
|
|
113
|
+
if (this.tokens > this.burst) {
|
|
114
|
+
this.tokens = this.burst;
|
|
115
|
+
}
|
|
116
|
+
if (this.tokens >= 1) {
|
|
117
|
+
this.tokens--;
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
async wait() {
|
|
123
|
+
while (!this.allow()) {
|
|
124
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
var MultiRateLimiter = class {
|
|
129
|
+
constructor(rate, burst = 10) {
|
|
130
|
+
this.rate = rate;
|
|
131
|
+
this.burst = burst;
|
|
132
|
+
}
|
|
133
|
+
rate;
|
|
134
|
+
burst;
|
|
135
|
+
limiters = /* @__PURE__ */ new Map();
|
|
136
|
+
allow(key) {
|
|
137
|
+
let limiter = this.limiters.get(key);
|
|
138
|
+
if (!limiter) {
|
|
139
|
+
limiter = new RateLimiter(this.rate, this.burst);
|
|
140
|
+
this.limiters.set(key, limiter);
|
|
141
|
+
}
|
|
142
|
+
return limiter.allow();
|
|
143
|
+
}
|
|
144
|
+
async wait(key) {
|
|
145
|
+
while (!this.allow(key)) {
|
|
146
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
var ConnectionPool = class {
|
|
151
|
+
constructor(factory, maxSize = 10, timeout = 3e4) {
|
|
152
|
+
this.factory = factory;
|
|
153
|
+
this.maxSize = maxSize;
|
|
154
|
+
this.timeout = timeout;
|
|
155
|
+
}
|
|
156
|
+
factory;
|
|
157
|
+
maxSize;
|
|
158
|
+
timeout;
|
|
159
|
+
pool = [];
|
|
160
|
+
created = 0;
|
|
161
|
+
waiting = 0;
|
|
162
|
+
async get() {
|
|
163
|
+
if (this.pool.length > 0) {
|
|
164
|
+
const conn = this.pool.pop();
|
|
165
|
+
if (conn.isAlive()) {
|
|
166
|
+
return conn;
|
|
167
|
+
}
|
|
168
|
+
this.created--;
|
|
169
|
+
return this.create();
|
|
170
|
+
}
|
|
171
|
+
if (this.created < this.maxSize) {
|
|
172
|
+
return this.create();
|
|
173
|
+
}
|
|
174
|
+
this.waiting++;
|
|
175
|
+
const startTime = Date.now();
|
|
176
|
+
while (Date.now() - startTime < this.timeout) {
|
|
177
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
178
|
+
if (this.pool.length > 0) {
|
|
179
|
+
const conn = this.pool.pop();
|
|
180
|
+
if (conn.isAlive()) {
|
|
181
|
+
this.waiting--;
|
|
182
|
+
return conn;
|
|
183
|
+
}
|
|
184
|
+
this.created--;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
this.waiting--;
|
|
188
|
+
throw new Error("Connection timeout");
|
|
189
|
+
}
|
|
190
|
+
release(conn) {
|
|
191
|
+
if (!conn.isAlive()) {
|
|
192
|
+
this.created--;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (this.pool.length < this.maxSize) {
|
|
196
|
+
this.pool.push(conn);
|
|
197
|
+
} else {
|
|
198
|
+
conn.close();
|
|
199
|
+
this.created--;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
create() {
|
|
203
|
+
const conn = this.factory();
|
|
204
|
+
this.created++;
|
|
205
|
+
return conn;
|
|
206
|
+
}
|
|
207
|
+
closeAll() {
|
|
208
|
+
for (const conn of this.pool) {
|
|
209
|
+
conn.close();
|
|
210
|
+
}
|
|
211
|
+
this.pool = [];
|
|
212
|
+
this.created = 0;
|
|
213
|
+
}
|
|
214
|
+
getStats() {
|
|
215
|
+
return {
|
|
216
|
+
created: this.created,
|
|
217
|
+
available: this.pool.length,
|
|
218
|
+
waiting: this.waiting
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var ObjectPool = class {
|
|
223
|
+
constructor(factory, resetFunc, maxSize = 100) {
|
|
224
|
+
this.factory = factory;
|
|
225
|
+
this.resetFunc = resetFunc;
|
|
226
|
+
this.maxSize = maxSize;
|
|
227
|
+
}
|
|
228
|
+
factory;
|
|
229
|
+
resetFunc;
|
|
230
|
+
maxSize;
|
|
231
|
+
pool = [];
|
|
232
|
+
acquire() {
|
|
233
|
+
if (this.pool.length > 0) {
|
|
234
|
+
return this.pool.pop();
|
|
235
|
+
}
|
|
236
|
+
return this.factory();
|
|
237
|
+
}
|
|
238
|
+
release(obj) {
|
|
239
|
+
if (this.resetFunc) {
|
|
240
|
+
this.resetFunc(obj);
|
|
241
|
+
}
|
|
242
|
+
if (this.pool.length < this.maxSize) {
|
|
243
|
+
this.pool.push(obj);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
with(fn) {
|
|
247
|
+
const obj = this.acquire();
|
|
248
|
+
try {
|
|
249
|
+
return fn(obj);
|
|
250
|
+
} finally {
|
|
251
|
+
this.release(obj);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
256
|
+
0 && (module.exports = {
|
|
257
|
+
ConnectionPool,
|
|
258
|
+
MultiRateLimiter,
|
|
259
|
+
ObjectPool,
|
|
260
|
+
RateLimiter,
|
|
261
|
+
WorkerPool
|
|
262
|
+
});
|