@aztec/validator-ha-signer 0.0.1-commit.0208eb9
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 +187 -0
- package/dest/config.d.ts +101 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +92 -0
- package/dest/db/index.d.ts +4 -0
- package/dest/db/index.d.ts.map +1 -0
- package/dest/db/index.js +3 -0
- package/dest/db/migrations/1_initial-schema.d.ts +9 -0
- package/dest/db/migrations/1_initial-schema.d.ts.map +1 -0
- package/dest/db/migrations/1_initial-schema.js +20 -0
- package/dest/db/postgres.d.ts +84 -0
- package/dest/db/postgres.d.ts.map +1 -0
- package/dest/db/postgres.js +210 -0
- package/dest/db/schema.d.ts +95 -0
- package/dest/db/schema.d.ts.map +1 -0
- package/dest/db/schema.js +229 -0
- package/dest/db/test_helper.d.ts +10 -0
- package/dest/db/test_helper.d.ts.map +1 -0
- package/dest/db/test_helper.js +14 -0
- package/dest/db/types.d.ts +166 -0
- package/dest/db/types.d.ts.map +1 -0
- package/dest/db/types.js +49 -0
- package/dest/errors.d.ts +34 -0
- package/dest/errors.d.ts.map +1 -0
- package/dest/errors.js +34 -0
- package/dest/factory.d.ts +42 -0
- package/dest/factory.d.ts.map +1 -0
- package/dest/factory.js +70 -0
- package/dest/migrations.d.ts +15 -0
- package/dest/migrations.d.ts.map +1 -0
- package/dest/migrations.js +53 -0
- package/dest/slashing_protection_service.d.ts +84 -0
- package/dest/slashing_protection_service.d.ts.map +1 -0
- package/dest/slashing_protection_service.js +225 -0
- package/dest/test/pglite_pool.d.ts +92 -0
- package/dest/test/pglite_pool.d.ts.map +1 -0
- package/dest/test/pglite_pool.js +210 -0
- package/dest/types.d.ts +152 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +21 -0
- package/dest/validator_ha_signer.d.ts +71 -0
- package/dest/validator_ha_signer.d.ts.map +1 -0
- package/dest/validator_ha_signer.js +132 -0
- package/package.json +106 -0
- package/src/config.ts +149 -0
- package/src/db/index.ts +3 -0
- package/src/db/migrations/1_initial-schema.ts +26 -0
- package/src/db/postgres.ts +284 -0
- package/src/db/schema.ts +266 -0
- package/src/db/test_helper.ts +17 -0
- package/src/db/types.ts +206 -0
- package/src/errors.ts +47 -0
- package/src/factory.ts +82 -0
- package/src/migrations.ts +75 -0
- package/src/slashing_protection_service.ts +287 -0
- package/src/test/pglite_pool.ts +256 -0
- package/src/types.ts +226 -0
- package/src/validator_ha_signer.ts +162 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slashing Protection Service
|
|
3
|
+
*
|
|
4
|
+
* Provides distributed locking and slashing protection for validator duties.
|
|
5
|
+
* Uses an external database to coordinate across multiple validator nodes.
|
|
6
|
+
*/ import { createLogger } from '@aztec/foundation/log';
|
|
7
|
+
import { RunningPromise } from '@aztec/foundation/promise';
|
|
8
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
9
|
+
import { DutyStatus, getBlockIndexFromDutyIdentifier } from './db/types.js';
|
|
10
|
+
import { DutyAlreadySignedError, SlashingProtectionError } from './errors.js';
|
|
11
|
+
/**
|
|
12
|
+
* Slashing Protection Service
|
|
13
|
+
*
|
|
14
|
+
* This service ensures that a validator only signs one block/attestation per slot,
|
|
15
|
+
* even when running multiple redundant nodes (HA setup).
|
|
16
|
+
*
|
|
17
|
+
* All nodes in the HA setup try to sign - the first one wins, others get
|
|
18
|
+
* DutyAlreadySignedError (normal) or SlashingProtectionError (if different data).
|
|
19
|
+
*
|
|
20
|
+
* Flow:
|
|
21
|
+
* 1. checkAndRecord() - Atomically try to acquire lock via tryInsertOrGetExisting
|
|
22
|
+
* 2. Caller performs the signing operation
|
|
23
|
+
* 3. recordSuccess() - Update to 'signed' status with signature
|
|
24
|
+
* OR deleteDuty() - Delete the record to allow retry
|
|
25
|
+
*/ export class SlashingProtectionService {
|
|
26
|
+
db;
|
|
27
|
+
config;
|
|
28
|
+
log;
|
|
29
|
+
pollingIntervalMs;
|
|
30
|
+
signingTimeoutMs;
|
|
31
|
+
maxStuckDutiesAgeMs;
|
|
32
|
+
cleanupRunningPromise;
|
|
33
|
+
lastOldDutiesCleanupAtMs;
|
|
34
|
+
constructor(db, config){
|
|
35
|
+
this.db = db;
|
|
36
|
+
this.config = config;
|
|
37
|
+
this.log = createLogger('slashing-protection');
|
|
38
|
+
this.pollingIntervalMs = config.pollingIntervalMs;
|
|
39
|
+
this.signingTimeoutMs = config.signingTimeoutMs;
|
|
40
|
+
// Default to 144s (2x 72s Aztec slot duration) if not explicitly configured
|
|
41
|
+
this.maxStuckDutiesAgeMs = config.maxStuckDutiesAgeMs ?? 144_000;
|
|
42
|
+
this.cleanupRunningPromise = new RunningPromise(this.cleanup.bind(this), this.log, this.maxStuckDutiesAgeMs);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Check if a duty can be performed and acquire the lock if so.
|
|
46
|
+
*
|
|
47
|
+
* This method uses an atomic insert-or-get operation.
|
|
48
|
+
* It will:
|
|
49
|
+
* 1. Try to insert a new record with 'signing' status
|
|
50
|
+
* 2. If insert succeeds, we acquired the lock - return the lockToken
|
|
51
|
+
* 3. If a record exists, handle based on status:
|
|
52
|
+
* - SIGNED: Throw appropriate error (already signed or slashing protection)
|
|
53
|
+
* - SIGNING: Wait and poll until status changes, then handle result
|
|
54
|
+
*
|
|
55
|
+
* @returns The lockToken that must be used for recordSuccess/deleteDuty
|
|
56
|
+
* @throws DutyAlreadySignedError if the duty was already completed
|
|
57
|
+
* @throws SlashingProtectionError if attempting to sign different data for same slot/duty
|
|
58
|
+
*/ async checkAndRecord(params) {
|
|
59
|
+
const { validatorAddress, slot, dutyType, messageHash, nodeId } = params;
|
|
60
|
+
const startTime = Date.now();
|
|
61
|
+
this.log.debug(`Checking duty: ${dutyType} for slot ${slot}`, {
|
|
62
|
+
validatorAddress: validatorAddress.toString(),
|
|
63
|
+
nodeId
|
|
64
|
+
});
|
|
65
|
+
while(true){
|
|
66
|
+
// insert if not present, get existing if present
|
|
67
|
+
const { isNew, record } = await this.db.tryInsertOrGetExisting(params);
|
|
68
|
+
if (isNew) {
|
|
69
|
+
// We successfully acquired the lock
|
|
70
|
+
this.log.info(`Acquired lock for duty ${dutyType} at slot ${slot}`, {
|
|
71
|
+
validatorAddress: validatorAddress.toString(),
|
|
72
|
+
nodeId
|
|
73
|
+
});
|
|
74
|
+
return record.lockToken;
|
|
75
|
+
}
|
|
76
|
+
// Record already exists - handle based on status
|
|
77
|
+
if (record.status === DutyStatus.SIGNED) {
|
|
78
|
+
// Duty was already signed - check if same or different data
|
|
79
|
+
if (record.messageHash !== messageHash) {
|
|
80
|
+
this.log.verbose(`Slashing protection triggered for duty ${dutyType} at slot ${slot}`, {
|
|
81
|
+
validatorAddress: validatorAddress.toString(),
|
|
82
|
+
existingMessageHash: record.messageHash,
|
|
83
|
+
attemptedMessageHash: messageHash,
|
|
84
|
+
existingNodeId: record.nodeId,
|
|
85
|
+
attemptingNodeId: nodeId
|
|
86
|
+
});
|
|
87
|
+
throw new SlashingProtectionError(slot, dutyType, record.blockIndexWithinCheckpoint, record.messageHash, messageHash, record.nodeId);
|
|
88
|
+
}
|
|
89
|
+
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, record.nodeId);
|
|
90
|
+
} else if (record.status === DutyStatus.SIGNING) {
|
|
91
|
+
// Another node is currently signing - check for timeout
|
|
92
|
+
if (Date.now() - startTime > this.signingTimeoutMs) {
|
|
93
|
+
this.log.warn(`Timeout waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
|
|
94
|
+
validatorAddress: validatorAddress.toString(),
|
|
95
|
+
timeoutMs: this.signingTimeoutMs,
|
|
96
|
+
signingNodeId: record.nodeId
|
|
97
|
+
});
|
|
98
|
+
throw new DutyAlreadySignedError(slot, dutyType, record.blockIndexWithinCheckpoint, 'unknown (timeout)');
|
|
99
|
+
}
|
|
100
|
+
// Wait and poll
|
|
101
|
+
this.log.debug(`Waiting for signing to complete for duty ${dutyType} at slot ${slot}`, {
|
|
102
|
+
validatorAddress: validatorAddress.toString(),
|
|
103
|
+
signingNodeId: record.nodeId
|
|
104
|
+
});
|
|
105
|
+
await sleep(this.pollingIntervalMs);
|
|
106
|
+
// Loop continues - next iteration will check status again
|
|
107
|
+
} else {
|
|
108
|
+
throw new Error(`Unknown duty status: ${record.status}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Record a successful signing operation.
|
|
114
|
+
* Updates the duty status to 'signed' and stores the signature.
|
|
115
|
+
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
116
|
+
*
|
|
117
|
+
* @returns true if the update succeeded, false if token didn't match
|
|
118
|
+
*/ async recordSuccess(params) {
|
|
119
|
+
const { rollupAddress, validatorAddress, slot, dutyType, signature, nodeId, lockToken } = params;
|
|
120
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
121
|
+
const success = await this.db.updateDutySigned(rollupAddress, validatorAddress, slot, dutyType, signature.toString(), lockToken, blockIndexWithinCheckpoint);
|
|
122
|
+
if (success) {
|
|
123
|
+
this.log.info(`Recorded successful signing for duty ${dutyType} at slot ${slot}`, {
|
|
124
|
+
validatorAddress: validatorAddress.toString(),
|
|
125
|
+
nodeId
|
|
126
|
+
});
|
|
127
|
+
} else {
|
|
128
|
+
this.log.warn(`Failed to record successful signing for duty ${dutyType} at slot ${slot}: invalid token`, {
|
|
129
|
+
validatorAddress: validatorAddress.toString(),
|
|
130
|
+
nodeId
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return success;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Delete a duty record after a failed signing operation.
|
|
137
|
+
* Removes the record to allow another node/attempt to retry.
|
|
138
|
+
* Only succeeds if the lockToken matches (caller must be the one who created the duty).
|
|
139
|
+
*
|
|
140
|
+
* @returns true if the delete succeeded, false if token didn't match
|
|
141
|
+
*/ async deleteDuty(params) {
|
|
142
|
+
const { rollupAddress, validatorAddress, slot, dutyType, lockToken } = params;
|
|
143
|
+
const blockIndexWithinCheckpoint = getBlockIndexFromDutyIdentifier(params);
|
|
144
|
+
const success = await this.db.deleteDuty(rollupAddress, validatorAddress, slot, dutyType, lockToken, blockIndexWithinCheckpoint);
|
|
145
|
+
if (success) {
|
|
146
|
+
this.log.info(`Deleted duty ${dutyType} at slot ${slot} to allow retry`, {
|
|
147
|
+
validatorAddress: validatorAddress.toString()
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
this.log.warn(`Failed to delete duty ${dutyType} at slot ${slot}: invalid token`, {
|
|
151
|
+
validatorAddress: validatorAddress.toString()
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return success;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Get the node ID for this service
|
|
158
|
+
*/ get nodeId() {
|
|
159
|
+
return this.config.nodeId;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Start running tasks.
|
|
163
|
+
* Cleanup runs immediately on start to recover from any previous crashes.
|
|
164
|
+
*/ /**
|
|
165
|
+
* Start the background cleanup task.
|
|
166
|
+
* Also performs one-time cleanup of duties with outdated rollup addresses.
|
|
167
|
+
*/ async start() {
|
|
168
|
+
// One-time cleanup at startup: remove duties from previous rollup versions
|
|
169
|
+
const numOutdatedRollupDuties = await this.db.cleanupOutdatedRollupDuties(this.config.l1Contracts.rollupAddress);
|
|
170
|
+
if (numOutdatedRollupDuties > 0) {
|
|
171
|
+
this.log.info(`Cleaned up ${numOutdatedRollupDuties} duties with outdated rollup address at startup`, {
|
|
172
|
+
currentRollupAddress: this.config.l1Contracts.rollupAddress.toString()
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
this.cleanupRunningPromise.start();
|
|
176
|
+
this.log.info('Slashing protection service started', {
|
|
177
|
+
nodeId: this.config.nodeId
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Stop the background cleanup task.
|
|
182
|
+
*/ async stop() {
|
|
183
|
+
await this.cleanupRunningPromise.stop();
|
|
184
|
+
this.log.info('Slashing protection service stopped', {
|
|
185
|
+
nodeId: this.config.nodeId
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Close the database connection.
|
|
190
|
+
* Should be called after stop() during graceful shutdown.
|
|
191
|
+
*/ async close() {
|
|
192
|
+
await this.db.close();
|
|
193
|
+
this.log.info('Slashing protection database connection closed');
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Periodic cleanup of stuck duties and optionally old signed duties.
|
|
197
|
+
* Runs in the background via RunningPromise.
|
|
198
|
+
*/ async cleanup() {
|
|
199
|
+
// 1. Clean up stuck duties (our own node's duties that got stuck in 'signing' status)
|
|
200
|
+
const numStuckDuties = await this.db.cleanupOwnStuckDuties(this.config.nodeId, this.maxStuckDutiesAgeMs);
|
|
201
|
+
if (numStuckDuties > 0) {
|
|
202
|
+
this.log.verbose(`Cleaned up ${numStuckDuties} stuck duties`, {
|
|
203
|
+
nodeId: this.config.nodeId,
|
|
204
|
+
maxStuckDutiesAgeMs: this.maxStuckDutiesAgeMs
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
// 2. Clean up old signed duties if configured
|
|
208
|
+
// we shouldn't run this as often as stuck duty cleanup.
|
|
209
|
+
if (this.config.cleanupOldDutiesAfterHours !== undefined) {
|
|
210
|
+
const maxAgeMs = this.config.cleanupOldDutiesAfterHours * 60 * 60 * 1000;
|
|
211
|
+
const nowMs = Date.now();
|
|
212
|
+
const shouldRun = this.lastOldDutiesCleanupAtMs === undefined || nowMs - this.lastOldDutiesCleanupAtMs >= maxAgeMs;
|
|
213
|
+
if (shouldRun) {
|
|
214
|
+
const numOldDuties = await this.db.cleanupOldDuties(maxAgeMs);
|
|
215
|
+
this.lastOldDutiesCleanupAtMs = nowMs;
|
|
216
|
+
if (numOldDuties > 0) {
|
|
217
|
+
this.log.verbose(`Cleaned up ${numOldDuties} old signed duties`, {
|
|
218
|
+
cleanupOldDutiesAfterHours: this.config.cleanupOldDutiesAfterHours,
|
|
219
|
+
maxAgeMs
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vendored pg-compatible Pool/Client wrapper for PGlite.
|
|
3
|
+
*
|
|
4
|
+
* Copied from @middle-management/pglite-pg-adapter v0.0.3
|
|
5
|
+
* https://www.npmjs.com/package/@middle-management/pglite-pg-adapter
|
|
6
|
+
*
|
|
7
|
+
* Modifications:
|
|
8
|
+
* - Converted to ESM and TypeScript
|
|
9
|
+
* - Uses PGliteInterface instead of PGlite class to avoid TypeScript
|
|
10
|
+
* type mismatches from ESM/CJS dual package resolution with private fields
|
|
11
|
+
* - Simplified rowCount calculation to handle CTEs properly
|
|
12
|
+
*/
|
|
13
|
+
import type { PGliteInterface } from '@electric-sql/pglite';
|
|
14
|
+
import { EventEmitter } from 'events';
|
|
15
|
+
import type { QueryResult, QueryResultRow } from 'pg';
|
|
16
|
+
import { Readable, Writable } from 'stream';
|
|
17
|
+
export interface PoolConfig {
|
|
18
|
+
pglite: PGliteInterface;
|
|
19
|
+
max?: number;
|
|
20
|
+
min?: number;
|
|
21
|
+
}
|
|
22
|
+
interface ClientConfig {
|
|
23
|
+
pglite: PGliteInterface;
|
|
24
|
+
host?: string;
|
|
25
|
+
port?: number;
|
|
26
|
+
ssl?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare class Client extends EventEmitter {
|
|
29
|
+
protected pglite: PGliteInterface;
|
|
30
|
+
protected _connected: boolean;
|
|
31
|
+
readonly host: string;
|
|
32
|
+
readonly port: number;
|
|
33
|
+
readonly ssl: boolean;
|
|
34
|
+
readonly connection: object;
|
|
35
|
+
readonly copyFrom: () => Writable;
|
|
36
|
+
readonly copyTo: () => Readable;
|
|
37
|
+
readonly pauseDrain: () => void;
|
|
38
|
+
readonly resumeDrain: () => void;
|
|
39
|
+
readonly escapeLiteral: (str: string) => string;
|
|
40
|
+
readonly escapeIdentifier: (str: string) => string;
|
|
41
|
+
readonly setTypeParser: () => void;
|
|
42
|
+
readonly getTypeParser: () => (value: string) => unknown;
|
|
43
|
+
constructor(config: ClientConfig);
|
|
44
|
+
connect(): Promise<void>;
|
|
45
|
+
end(): Promise<void>;
|
|
46
|
+
query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>>;
|
|
47
|
+
protected convertPGliteResult<R extends QueryResultRow>(result: {
|
|
48
|
+
rows: R[];
|
|
49
|
+
fields: Array<{
|
|
50
|
+
name: string;
|
|
51
|
+
dataTypeID: number;
|
|
52
|
+
}>;
|
|
53
|
+
affectedRows?: number;
|
|
54
|
+
}): QueryResult<R>;
|
|
55
|
+
get connected(): boolean;
|
|
56
|
+
}
|
|
57
|
+
export declare class Pool extends EventEmitter {
|
|
58
|
+
private clients;
|
|
59
|
+
private availableClients;
|
|
60
|
+
private waitingQueue;
|
|
61
|
+
private _ended;
|
|
62
|
+
private pglite;
|
|
63
|
+
private _config;
|
|
64
|
+
readonly expiredCount = 0;
|
|
65
|
+
readonly options: PoolConfig;
|
|
66
|
+
constructor(config: PoolConfig);
|
|
67
|
+
get totalCount(): number;
|
|
68
|
+
get idleCount(): number;
|
|
69
|
+
get waitingCount(): number;
|
|
70
|
+
get ending(): boolean;
|
|
71
|
+
get ended(): boolean;
|
|
72
|
+
connect(): Promise<PoolClient>;
|
|
73
|
+
query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>>;
|
|
74
|
+
releaseClient(client: PoolClient): void;
|
|
75
|
+
end(): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
export declare class PoolClient extends Client {
|
|
78
|
+
private pool;
|
|
79
|
+
private _released;
|
|
80
|
+
private _inUse;
|
|
81
|
+
private _userReleased;
|
|
82
|
+
constructor(pglite: PGliteInterface, pool: Pool);
|
|
83
|
+
query<R extends QueryResultRow = any>(text: string, values?: any[]): Promise<QueryResult<R>>;
|
|
84
|
+
release(): void;
|
|
85
|
+
end(): Promise<void>;
|
|
86
|
+
_markInUse(): void;
|
|
87
|
+
_markAvailable(): void;
|
|
88
|
+
_markReleased(): void;
|
|
89
|
+
get connected(): boolean;
|
|
90
|
+
}
|
|
91
|
+
export {};
|
|
92
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGdsaXRlX3Bvb2wuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90ZXN0L3BnbGl0ZV9wb29sLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7Ozs7OztHQVdHO0FBQ0gsT0FBTyxLQUFLLEVBQUUsZUFBZSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDNUQsT0FBTyxFQUFFLFlBQVksRUFBRSxNQUFNLFFBQVEsQ0FBQztBQUN0QyxPQUFPLEtBQUssRUFBRSxXQUFXLEVBQUUsY0FBYyxFQUFFLE1BQU0sSUFBSSxDQUFDO0FBQ3RELE9BQU8sRUFBRSxRQUFRLEVBQUUsUUFBUSxFQUFFLE1BQU0sUUFBUSxDQUFDO0FBRTVDLE1BQU0sV0FBVyxVQUFVO0lBQ3pCLE1BQU0sRUFBRSxlQUFlLENBQUM7SUFDeEIsR0FBRyxDQUFDLEVBQUUsTUFBTSxDQUFDO0lBQ2IsR0FBRyxDQUFDLEVBQUUsTUFBTSxDQUFDO0NBQ2Q7QUFFRCxVQUFVLFlBQVk7SUFDcEIsTUFBTSxFQUFFLGVBQWUsQ0FBQztJQUN4QixJQUFJLENBQUMsRUFBRSxNQUFNLENBQUM7SUFDZCxJQUFJLENBQUMsRUFBRSxNQUFNLENBQUM7SUFDZCxHQUFHLENBQUMsRUFBRSxPQUFPLENBQUM7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UAAW,SAAQ,MAAM;IACpC,OAAO,CAAC,IAAI,CAAO;IACnB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,aAAa,CAAS;IAE9B,YAAY,MAAM,EAAE,eAAe,EAAE,IAAI,EAAE,IAAI,EAI9C;IAEc,KAAK,CAAC,CAAC,SAAS,cAAc,GAAG,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAM1G;IAED,OAAO,IAAI,IAAI,CAMd;IAEQ,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAG5B;IAED,UAAU,IAAI,IAAI,CAGjB;IAED,cAAc,IAAI,IAAI,CAGrB;IAED,aAAa,IAAI,IAAI,CAIpB;IAED,IAAa,SAAS,IAAI,OAAO,CAEhC;CACF"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vendored pg-compatible Pool/Client wrapper for PGlite.
|
|
3
|
+
*
|
|
4
|
+
* Copied from @middle-management/pglite-pg-adapter v0.0.3
|
|
5
|
+
* https://www.npmjs.com/package/@middle-management/pglite-pg-adapter
|
|
6
|
+
*
|
|
7
|
+
* Modifications:
|
|
8
|
+
* - Converted to ESM and TypeScript
|
|
9
|
+
* - Uses PGliteInterface instead of PGlite class to avoid TypeScript
|
|
10
|
+
* type mismatches from ESM/CJS dual package resolution with private fields
|
|
11
|
+
* - Simplified rowCount calculation to handle CTEs properly
|
|
12
|
+
*/ import { EventEmitter } from 'events';
|
|
13
|
+
import { Readable, Writable } from 'stream';
|
|
14
|
+
export class Client extends EventEmitter {
|
|
15
|
+
pglite;
|
|
16
|
+
_connected = false;
|
|
17
|
+
host;
|
|
18
|
+
port;
|
|
19
|
+
ssl;
|
|
20
|
+
connection;
|
|
21
|
+
// Stub implementations for pg compatibility
|
|
22
|
+
copyFrom = ()=>new Writable();
|
|
23
|
+
copyTo = ()=>new Readable();
|
|
24
|
+
pauseDrain = ()=>{};
|
|
25
|
+
resumeDrain = ()=>{};
|
|
26
|
+
escapeLiteral = (str)=>`'${str.replace(/'/g, "''")}'`;
|
|
27
|
+
escapeIdentifier = (str)=>`"${str.replace(/"/g, '""')}"`;
|
|
28
|
+
setTypeParser = ()=>{};
|
|
29
|
+
getTypeParser = ()=>(value)=>value;
|
|
30
|
+
constructor(config){
|
|
31
|
+
super();
|
|
32
|
+
this.pglite = config.pglite;
|
|
33
|
+
this.host = config.host || 'localhost';
|
|
34
|
+
this.port = config.port || 5432;
|
|
35
|
+
this.ssl = typeof config.ssl === 'boolean' ? config.ssl : !!config.ssl;
|
|
36
|
+
this.connection = {};
|
|
37
|
+
}
|
|
38
|
+
connect() {
|
|
39
|
+
if (this._connected) {
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
}
|
|
42
|
+
this._connected = true;
|
|
43
|
+
this.emit('connect');
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
}
|
|
46
|
+
end() {
|
|
47
|
+
if (!this._connected) {
|
|
48
|
+
return Promise.resolve();
|
|
49
|
+
}
|
|
50
|
+
this._connected = false;
|
|
51
|
+
this.emit('end');
|
|
52
|
+
return Promise.resolve();
|
|
53
|
+
}
|
|
54
|
+
async query(text, values) {
|
|
55
|
+
if (!this._connected) {
|
|
56
|
+
throw new Error('Client is not connected');
|
|
57
|
+
}
|
|
58
|
+
const result = await this.pglite.query(text, values);
|
|
59
|
+
return this.convertPGliteResult(result);
|
|
60
|
+
}
|
|
61
|
+
convertPGliteResult(result) {
|
|
62
|
+
return {
|
|
63
|
+
command: '',
|
|
64
|
+
rowCount: 'affectedRows' in result ? result.affectedRows ?? 0 : result.rows.length,
|
|
65
|
+
oid: 0,
|
|
66
|
+
fields: result.fields.map((field)=>({
|
|
67
|
+
name: field.name,
|
|
68
|
+
tableID: 0,
|
|
69
|
+
columnID: 0,
|
|
70
|
+
dataTypeID: field.dataTypeID,
|
|
71
|
+
dataTypeSize: -1,
|
|
72
|
+
dataTypeModifier: -1,
|
|
73
|
+
format: 'text'
|
|
74
|
+
})),
|
|
75
|
+
rows: result.rows
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
get connected() {
|
|
79
|
+
return this._connected;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export class Pool extends EventEmitter {
|
|
83
|
+
clients = [];
|
|
84
|
+
availableClients = [];
|
|
85
|
+
waitingQueue = [];
|
|
86
|
+
_ended = false;
|
|
87
|
+
pglite;
|
|
88
|
+
_config;
|
|
89
|
+
expiredCount = 0;
|
|
90
|
+
options;
|
|
91
|
+
constructor(config){
|
|
92
|
+
super();
|
|
93
|
+
this._config = {
|
|
94
|
+
max: 10,
|
|
95
|
+
min: 0,
|
|
96
|
+
...config
|
|
97
|
+
};
|
|
98
|
+
this.pglite = config.pglite;
|
|
99
|
+
this.options = config;
|
|
100
|
+
}
|
|
101
|
+
get totalCount() {
|
|
102
|
+
return this.clients.length;
|
|
103
|
+
}
|
|
104
|
+
get idleCount() {
|
|
105
|
+
return this.availableClients.length;
|
|
106
|
+
}
|
|
107
|
+
get waitingCount() {
|
|
108
|
+
return this.waitingQueue.length;
|
|
109
|
+
}
|
|
110
|
+
get ending() {
|
|
111
|
+
return this._ended;
|
|
112
|
+
}
|
|
113
|
+
get ended() {
|
|
114
|
+
return this._ended;
|
|
115
|
+
}
|
|
116
|
+
connect() {
|
|
117
|
+
if (this._ended) {
|
|
118
|
+
return Promise.reject(new Error('Pool is ended'));
|
|
119
|
+
}
|
|
120
|
+
if (this.availableClients.length > 0) {
|
|
121
|
+
const client = this.availableClients.pop();
|
|
122
|
+
client._markInUse();
|
|
123
|
+
return Promise.resolve(client);
|
|
124
|
+
}
|
|
125
|
+
if (this.clients.length < (this._config.max || 10)) {
|
|
126
|
+
const client = new PoolClient(this.pglite, this);
|
|
127
|
+
this.clients.push(client);
|
|
128
|
+
return Promise.resolve(client);
|
|
129
|
+
}
|
|
130
|
+
return new Promise((resolve)=>{
|
|
131
|
+
this.waitingQueue.push(resolve);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async query(text, values) {
|
|
135
|
+
const client = await this.connect();
|
|
136
|
+
try {
|
|
137
|
+
return await client.query(text, values);
|
|
138
|
+
} finally{
|
|
139
|
+
client.release();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
releaseClient(client) {
|
|
143
|
+
const index = this.clients.indexOf(client);
|
|
144
|
+
if (index !== -1) {
|
|
145
|
+
client._markAvailable();
|
|
146
|
+
if (this.waitingQueue.length > 0) {
|
|
147
|
+
const resolve = this.waitingQueue.shift();
|
|
148
|
+
client._markInUse();
|
|
149
|
+
resolve(client);
|
|
150
|
+
} else {
|
|
151
|
+
this.availableClients.push(client);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
end() {
|
|
156
|
+
this._ended = true;
|
|
157
|
+
this.clients.forEach((client)=>client._markReleased());
|
|
158
|
+
this.clients = [];
|
|
159
|
+
this.availableClients = [];
|
|
160
|
+
this.emit('end');
|
|
161
|
+
return Promise.resolve();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export class PoolClient extends Client {
|
|
165
|
+
pool;
|
|
166
|
+
_released = false;
|
|
167
|
+
_inUse = true;
|
|
168
|
+
_userReleased = false;
|
|
169
|
+
constructor(pglite, pool){
|
|
170
|
+
super({
|
|
171
|
+
pglite
|
|
172
|
+
});
|
|
173
|
+
this.pool = pool;
|
|
174
|
+
this._connected = true;
|
|
175
|
+
}
|
|
176
|
+
async query(text, values) {
|
|
177
|
+
if (this._userReleased && !this._inUse) {
|
|
178
|
+
throw new Error('Client has been released back to the pool');
|
|
179
|
+
}
|
|
180
|
+
const result = await this.pglite.query(text, values);
|
|
181
|
+
return this.convertPGliteResult(result);
|
|
182
|
+
}
|
|
183
|
+
release() {
|
|
184
|
+
if (this._released || this._userReleased) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this._userReleased = true;
|
|
188
|
+
this.pool.releaseClient(this);
|
|
189
|
+
}
|
|
190
|
+
end() {
|
|
191
|
+
this.release();
|
|
192
|
+
return Promise.resolve();
|
|
193
|
+
}
|
|
194
|
+
_markInUse() {
|
|
195
|
+
this._inUse = true;
|
|
196
|
+
this._userReleased = false;
|
|
197
|
+
}
|
|
198
|
+
_markAvailable() {
|
|
199
|
+
this._inUse = false;
|
|
200
|
+
this._userReleased = false;
|
|
201
|
+
}
|
|
202
|
+
_markReleased() {
|
|
203
|
+
this._released = true;
|
|
204
|
+
this._inUse = false;
|
|
205
|
+
this._userReleased = true;
|
|
206
|
+
}
|
|
207
|
+
get connected() {
|
|
208
|
+
return this._connected && !this._released;
|
|
209
|
+
}
|
|
210
|
+
}
|