-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsqliteRoutingStore.js
More file actions
320 lines (281 loc) · 8.24 KB
/
Copy pathsqliteRoutingStore.js
File metadata and controls
320 lines (281 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import fs from 'fs';
import path from 'path';
import Database from 'better-sqlite3';
export class SqliteRoutingStore {
constructor(dbPath, options = {}) {
this.dbPath = resolveDbPath(dbPath || options.dbPath || '.data/rtms-routing.sqlite');
ensureDbDir(this.dbPath);
this.db = new Database(this.dbPath);
configureDatabase(this.db, options);
this.prepareSchema();
this.prepareStatements();
}
health() {
return {
dbPath: this.dbPath,
journalMode: this.db.pragma('journal_mode', { simple: true })
};
}
acceptWebhookIdempotency(record = {}) {
const idempotencyKey = record.idempotencyKey || record.key;
const ttlMs = Number(record.ttlMs || 0);
if (!idempotencyKey || ttlMs <= 0) {
return { accepted: true, duplicate: false, disabled: true };
}
const nowMs = Date.now();
const expiresAtMs = nowMs + ttlMs;
this.cleanupExpiredWebhookIdempotency(nowMs);
const result = this.insertIdempotency.run({
idempotencyKey,
event: record.event || 'unknown',
streamId: record.streamId || null,
rtmsId: record.rtmsId || null,
acceptedAt: new Date(nowMs).toISOString(),
acceptedAtMs: nowMs,
expiresAtMs
});
return {
accepted: result.changes === 1,
duplicate: result.changes === 0
};
}
forgetWebhookIdempotency(idempotencyKey) {
if (!idempotencyKey) return;
this.deleteIdempotency.run(idempotencyKey);
}
cleanupExpiredWebhookIdempotency(nowMs = Date.now()) {
return this.deleteExpiredIdempotency.run(nowMs).changes;
}
countWebhookIdempotency() {
this.cleanupExpiredWebhookIdempotency();
return this.countIdempotency.get().count;
}
upsertStreamRoute(streamId, route = {}) {
if (!streamId) throw new Error('streamId is required');
const now = new Date().toISOString();
const existing = this.getStreamRoute(streamId);
const routeRecord = {
streamId,
regionCode: route.regionCode || existing?.regionCode || 'UNKNOWN',
spokeGroup: route.spokeGroup || existing?.spokeGroup || 'us',
productType: route.productType || existing?.productType || 'unknown',
rtmsId: route.rtmsId || existing?.rtmsId || null,
envelope: route.envelope || existing?.envelope || null,
createdAt: existing?.createdAt || now,
updatedAt: now
};
this.upsertRoute.run({
streamId,
regionCode: routeRecord.regionCode,
spokeGroup: routeRecord.spokeGroup,
productType: routeRecord.productType,
rtmsId: routeRecord.rtmsId,
envelopeJson: stringifyJson(routeRecord.envelope),
createdAt: routeRecord.createdAt,
updatedAt: routeRecord.updatedAt
});
return routeRecord;
}
getStreamRoute(streamId) {
if (!streamId) return null;
const row = this.getRoute.get(streamId);
if (!row) return null;
return {
streamId: row.stream_id,
regionCode: row.region_code,
spokeGroup: row.spoke_group,
productType: row.product_type,
rtmsId: row.rtms_id,
envelope: parseJson(row.envelope_json),
createdAt: row.created_at,
updatedAt: row.updated_at
};
}
writeStreamState(streamId, state = {}) {
if (!streamId) throw new Error('streamId is required');
const now = new Date().toISOString();
this.upsertState.run({
streamId,
stateJson: stringifyJson(state),
updatedAt: now
});
return { streamId, ...state, updatedAt: now };
}
appendStreamEvent(streamId, event = {}) {
if (!streamId) throw new Error('streamId is required');
const now = new Date().toISOString();
this.insertEvent.run({
streamId,
eventType: event.type || event.event || 'event',
eventJson: stringifyJson(event),
createdAt: now
});
return { streamId, event: { ...event, recordedAt: now } };
}
close() {
this.db.close();
}
prepareSchema() {
this.db.exec(`
CREATE TABLE IF NOT EXISTS webhook_idempotency (
idempotency_key TEXT PRIMARY KEY,
event TEXT NOT NULL,
stream_id TEXT,
rtms_id TEXT,
accepted_at TEXT NOT NULL,
accepted_at_ms INTEGER NOT NULL,
expires_at_ms INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS webhook_idempotency_expires_at_idx
ON webhook_idempotency (expires_at_ms);
CREATE TABLE IF NOT EXISTS stream_routes (
stream_id TEXT PRIMARY KEY,
region_code TEXT,
spoke_group TEXT NOT NULL,
product_type TEXT,
rtms_id TEXT,
envelope_json TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS stream_routes_rtms_id_idx
ON stream_routes (rtms_id);
CREATE INDEX IF NOT EXISTS stream_routes_spoke_group_idx
ON stream_routes (spoke_group, updated_at DESC);
CREATE TABLE IF NOT EXISTS stream_states (
stream_id TEXT PRIMARY KEY,
state_json TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS stream_events (
event_id INTEGER PRIMARY KEY AUTOINCREMENT,
stream_id TEXT NOT NULL,
event_type TEXT NOT NULL,
event_json TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS stream_events_stream_id_idx
ON stream_events (stream_id, created_at DESC);
`);
}
prepareStatements() {
this.insertIdempotency = this.db.prepare(`
INSERT OR IGNORE INTO webhook_idempotency (
idempotency_key,
event,
stream_id,
rtms_id,
accepted_at,
accepted_at_ms,
expires_at_ms
) VALUES (
@idempotencyKey,
@event,
@streamId,
@rtmsId,
@acceptedAt,
@acceptedAtMs,
@expiresAtMs
)
`);
this.deleteExpiredIdempotency = this.db.prepare(`
DELETE FROM webhook_idempotency
WHERE expires_at_ms <= ?
`);
this.deleteIdempotency = this.db.prepare(`
DELETE FROM webhook_idempotency
WHERE idempotency_key = ?
`);
this.countIdempotency = this.db.prepare(`
SELECT COUNT(*) AS count
FROM webhook_idempotency
`);
this.getRoute = this.db.prepare(`
SELECT *
FROM stream_routes
WHERE stream_id = ?
`);
this.upsertRoute = this.db.prepare(`
INSERT INTO stream_routes (
stream_id,
region_code,
spoke_group,
product_type,
rtms_id,
envelope_json,
created_at,
updated_at
) VALUES (
@streamId,
@regionCode,
@spokeGroup,
@productType,
@rtmsId,
@envelopeJson,
@createdAt,
@updatedAt
)
ON CONFLICT(stream_id) DO UPDATE SET
region_code = excluded.region_code,
spoke_group = excluded.spoke_group,
product_type = excluded.product_type,
rtms_id = excluded.rtms_id,
envelope_json = excluded.envelope_json,
updated_at = excluded.updated_at
`);
this.upsertState = this.db.prepare(`
INSERT INTO stream_states (
stream_id,
state_json,
updated_at
) VALUES (
@streamId,
@stateJson,
@updatedAt
)
ON CONFLICT(stream_id) DO UPDATE SET
state_json = excluded.state_json,
updated_at = excluded.updated_at
`);
this.insertEvent = this.db.prepare(`
INSERT INTO stream_events (
stream_id,
event_type,
event_json,
created_at
) VALUES (
@streamId,
@eventType,
@eventJson,
@createdAt
)
`);
}
}
export function resolveDbPath(dbPath) {
if (dbPath === ':memory:') return dbPath;
return path.resolve(dbPath || '.data/rtms.sqlite');
}
function ensureDbDir(dbPath) {
if (dbPath === ':memory:') return;
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
}
function configureDatabase(db, options = {}) {
db.pragma(`busy_timeout = ${Number(options.busyTimeoutMs || 5000)}`);
db.pragma('foreign_keys = ON');
if (options.wal !== false) {
db.pragma('journal_mode = WAL');
db.pragma('synchronous = NORMAL');
}
}
function stringifyJson(value) {
return JSON.stringify(value ?? null);
}
function parseJson(value) {
if (!value) return null;
try {
return JSON.parse(value);
} catch {
return null;
}
}