@syncular/relay 0.0.1-100
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/dist/client-role/forward-engine.d.ts +63 -0
- package/dist/client-role/forward-engine.d.ts.map +1 -0
- package/dist/client-role/forward-engine.js +257 -0
- package/dist/client-role/forward-engine.js.map +1 -0
- package/dist/client-role/index.d.ts +9 -0
- package/dist/client-role/index.d.ts.map +1 -0
- package/dist/client-role/index.js +9 -0
- package/dist/client-role/index.js.map +1 -0
- package/dist/client-role/pull-engine.d.ts +70 -0
- package/dist/client-role/pull-engine.d.ts.map +1 -0
- package/dist/client-role/pull-engine.js +247 -0
- package/dist/client-role/pull-engine.js.map +1 -0
- package/dist/client-role/sequence-mapper.d.ts +65 -0
- package/dist/client-role/sequence-mapper.d.ts.map +1 -0
- package/dist/client-role/sequence-mapper.js +161 -0
- package/dist/client-role/sequence-mapper.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/migrate.d.ts +18 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +99 -0
- package/dist/migrate.js.map +1 -0
- package/dist/mode-manager.d.ts +60 -0
- package/dist/mode-manager.d.ts.map +1 -0
- package/dist/mode-manager.js +114 -0
- package/dist/mode-manager.js.map +1 -0
- package/dist/realtime.d.ts +102 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/realtime.js +305 -0
- package/dist/realtime.js.map +1 -0
- package/dist/relay.d.ts +189 -0
- package/dist/relay.d.ts.map +1 -0
- package/dist/relay.js +319 -0
- package/dist/relay.js.map +1 -0
- package/dist/schema.d.ts +158 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +7 -0
- package/dist/schema.js.map +1 -0
- package/dist/server-role/index.d.ts +54 -0
- package/dist/server-role/index.d.ts.map +1 -0
- package/dist/server-role/index.js +195 -0
- package/dist/server-role/index.js.map +1 -0
- package/dist/server-role/pull.d.ts +25 -0
- package/dist/server-role/pull.d.ts.map +1 -0
- package/dist/server-role/pull.js +24 -0
- package/dist/server-role/pull.js.map +1 -0
- package/dist/server-role/push.d.ts +27 -0
- package/dist/server-role/push.d.ts.map +1 -0
- package/dist/server-role/push.js +94 -0
- package/dist/server-role/push.js.map +1 -0
- package/package.json +61 -0
- package/src/__tests__/relay.test.ts +781 -0
- package/src/bun-types.d.ts +50 -0
- package/src/client-role/forward-engine.ts +343 -0
- package/src/client-role/index.ts +9 -0
- package/src/client-role/pull-engine.ts +321 -0
- package/src/client-role/sequence-mapper.ts +201 -0
- package/src/index.ts +50 -0
- package/src/migrate.ts +113 -0
- package/src/mode-manager.ts +142 -0
- package/src/realtime.ts +370 -0
- package/src/relay.ts +424 -0
- package/src/schema.ts +171 -0
- package/src/server-role/index.ts +339 -0
- package/src/server-role/pull.ts +37 -0
- package/src/server-role/push.ts +123 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/relay - Pull Engine
|
|
3
|
+
*
|
|
4
|
+
* Pulls changes from the main server and stores them locally
|
|
5
|
+
* on the relay for local clients to access.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
ScopeValues,
|
|
10
|
+
SyncCommit,
|
|
11
|
+
SyncPullResponse,
|
|
12
|
+
SyncSubscriptionRequest,
|
|
13
|
+
SyncTransport,
|
|
14
|
+
} from '@syncular/core';
|
|
15
|
+
import type { ServerSyncDialect, TableRegistry } from '@syncular/server';
|
|
16
|
+
import { pushCommit } from '@syncular/server';
|
|
17
|
+
import { type Kysely, sql } from 'kysely';
|
|
18
|
+
import type { RelayRealtime } from '../realtime';
|
|
19
|
+
import type { RelayDatabase } from '../schema';
|
|
20
|
+
import type { SequenceMapper } from './sequence-mapper';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Pull engine options.
|
|
24
|
+
*/
|
|
25
|
+
export interface PullEngineOptions<DB extends RelayDatabase = RelayDatabase> {
|
|
26
|
+
db: Kysely<DB>;
|
|
27
|
+
dialect: ServerSyncDialect;
|
|
28
|
+
transport: SyncTransport;
|
|
29
|
+
clientId: string;
|
|
30
|
+
/** Tables to subscribe to */
|
|
31
|
+
tables: string[];
|
|
32
|
+
/** Scope values for subscriptions */
|
|
33
|
+
scopes: ScopeValues;
|
|
34
|
+
shapes: TableRegistry<DB>;
|
|
35
|
+
sequenceMapper: SequenceMapper<DB>;
|
|
36
|
+
realtime: RelayRealtime;
|
|
37
|
+
intervalMs?: number;
|
|
38
|
+
onError?: (error: Error) => void;
|
|
39
|
+
onPullComplete?: () => Promise<void>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Pull engine for receiving changes from the main server.
|
|
44
|
+
*/
|
|
45
|
+
export class PullEngine<DB extends RelayDatabase = RelayDatabase> {
|
|
46
|
+
private readonly db: Kysely<DB>;
|
|
47
|
+
private readonly dialect: ServerSyncDialect;
|
|
48
|
+
private readonly transport: SyncTransport;
|
|
49
|
+
private readonly clientId: string;
|
|
50
|
+
private readonly tables: string[];
|
|
51
|
+
private readonly scopes: ScopeValues;
|
|
52
|
+
private readonly shapes: TableRegistry<DB>;
|
|
53
|
+
private readonly sequenceMapper: SequenceMapper<DB>;
|
|
54
|
+
private readonly realtime: RelayRealtime;
|
|
55
|
+
private readonly intervalMs: number;
|
|
56
|
+
private readonly onError?: (error: Error) => void;
|
|
57
|
+
private readonly onPullComplete?: () => Promise<void>;
|
|
58
|
+
|
|
59
|
+
private running = false;
|
|
60
|
+
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
61
|
+
private cursors = new Map<string, number>();
|
|
62
|
+
|
|
63
|
+
constructor(options: PullEngineOptions<DB>) {
|
|
64
|
+
this.db = options.db;
|
|
65
|
+
this.dialect = options.dialect;
|
|
66
|
+
this.transport = options.transport;
|
|
67
|
+
this.clientId = options.clientId;
|
|
68
|
+
this.tables = options.tables;
|
|
69
|
+
this.scopes = options.scopes;
|
|
70
|
+
this.shapes = options.shapes;
|
|
71
|
+
this.sequenceMapper = options.sequenceMapper;
|
|
72
|
+
this.realtime = options.realtime;
|
|
73
|
+
this.intervalMs = options.intervalMs ?? 10000;
|
|
74
|
+
this.onError = options.onError;
|
|
75
|
+
this.onPullComplete = options.onPullComplete;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Start the pull engine loop.
|
|
80
|
+
*/
|
|
81
|
+
start(): void {
|
|
82
|
+
if (this.running) return;
|
|
83
|
+
this.running = true;
|
|
84
|
+
void this.loadCursors()
|
|
85
|
+
.catch((error) => {
|
|
86
|
+
this.onError?.(
|
|
87
|
+
error instanceof Error ? error : new Error(String(error))
|
|
88
|
+
);
|
|
89
|
+
})
|
|
90
|
+
.finally(() => {
|
|
91
|
+
if (this.running) {
|
|
92
|
+
this.scheduleNext(0);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Stop the pull engine.
|
|
99
|
+
*/
|
|
100
|
+
stop(): void {
|
|
101
|
+
this.running = false;
|
|
102
|
+
if (this.timer) {
|
|
103
|
+
clearTimeout(this.timer);
|
|
104
|
+
this.timer = null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Pull once (for manual/testing use).
|
|
110
|
+
*/
|
|
111
|
+
async pullOnce(): Promise<boolean> {
|
|
112
|
+
return this.processOne();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private async loadCursors(): Promise<void> {
|
|
116
|
+
try {
|
|
117
|
+
// Load cursors from config
|
|
118
|
+
const rowResult = await sql<{ value_json: string }>`
|
|
119
|
+
select value_json
|
|
120
|
+
from ${sql.table('relay_config')}
|
|
121
|
+
where key = 'main_cursors'
|
|
122
|
+
limit 1
|
|
123
|
+
`.execute(this.db);
|
|
124
|
+
const row = rowResult.rows[0];
|
|
125
|
+
|
|
126
|
+
if (row?.value_json) {
|
|
127
|
+
const parsed = JSON.parse(row.value_json);
|
|
128
|
+
if (typeof parsed === 'object' && parsed !== null) {
|
|
129
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
130
|
+
if (typeof value === 'number') {
|
|
131
|
+
this.cursors.set(key, value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} catch {
|
|
137
|
+
// Ignore - start from scratch
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private async saveCursors(): Promise<void> {
|
|
142
|
+
const cursorObj: Record<string, number> = {};
|
|
143
|
+
for (const [key, value] of this.cursors) {
|
|
144
|
+
cursorObj[key] = value;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const valueJson = JSON.stringify(cursorObj);
|
|
148
|
+
await sql`
|
|
149
|
+
insert into ${sql.table('relay_config')} (key, value_json)
|
|
150
|
+
values ('main_cursors', ${valueJson})
|
|
151
|
+
on conflict (key)
|
|
152
|
+
do update set value_json = ${valueJson}
|
|
153
|
+
`.execute(this.db);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private scheduleNext(delayMs: number): void {
|
|
157
|
+
if (!this.running) return;
|
|
158
|
+
if (this.timer) return;
|
|
159
|
+
|
|
160
|
+
this.timer = setTimeout(async () => {
|
|
161
|
+
this.timer = null;
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const pulled = await this.processOne();
|
|
165
|
+
// If we pulled something, immediately try again
|
|
166
|
+
const nextDelay = pulled ? 0 : this.intervalMs;
|
|
167
|
+
this.scheduleNext(nextDelay);
|
|
168
|
+
} catch (err) {
|
|
169
|
+
this.onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
170
|
+
this.scheduleNext(this.intervalMs);
|
|
171
|
+
}
|
|
172
|
+
}, delayMs);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private async processOne(): Promise<boolean> {
|
|
176
|
+
// Build subscriptions for each table
|
|
177
|
+
const subscriptionRequests: SyncSubscriptionRequest[] = this.tables.map(
|
|
178
|
+
(table) => ({
|
|
179
|
+
id: table,
|
|
180
|
+
shape: table,
|
|
181
|
+
scopes: this.scopes,
|
|
182
|
+
cursor: this.cursors.get(table) ?? -1,
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
let response: SyncPullResponse;
|
|
187
|
+
try {
|
|
188
|
+
const combined = await this.transport.sync({
|
|
189
|
+
clientId: this.clientId,
|
|
190
|
+
pull: {
|
|
191
|
+
subscriptions: subscriptionRequests,
|
|
192
|
+
limitCommits: 100,
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
if (!combined.pull) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
response = combined.pull;
|
|
199
|
+
} catch {
|
|
200
|
+
// Network error - will retry
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (!response.ok) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let hasChanges = false;
|
|
209
|
+
const affectedTables = new Set<string>();
|
|
210
|
+
|
|
211
|
+
for (const sub of response.subscriptions) {
|
|
212
|
+
if (sub.status !== 'active') continue;
|
|
213
|
+
|
|
214
|
+
const table = sub.id;
|
|
215
|
+
|
|
216
|
+
// Process commits
|
|
217
|
+
let canAdvanceCursor = true;
|
|
218
|
+
for (const commit of sub.commits) {
|
|
219
|
+
const outcome = await this.applyCommitLocally(commit, table);
|
|
220
|
+
if (outcome === 'applied') {
|
|
221
|
+
hasChanges = true;
|
|
222
|
+
affectedTables.add(table);
|
|
223
|
+
}
|
|
224
|
+
if (outcome === 'rejected') {
|
|
225
|
+
canAdvanceCursor = false;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Update cursor
|
|
231
|
+
if (
|
|
232
|
+
canAdvanceCursor &&
|
|
233
|
+
sub.nextCursor > (this.cursors.get(table) ?? -1)
|
|
234
|
+
) {
|
|
235
|
+
this.cursors.set(table, sub.nextCursor);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Save updated cursors
|
|
240
|
+
await this.saveCursors();
|
|
241
|
+
|
|
242
|
+
// Notify local clients if we have changes
|
|
243
|
+
if (hasChanges && affectedTables.size > 0) {
|
|
244
|
+
const maxCursor = await this.dialect.readMaxCommitSeq(this.db);
|
|
245
|
+
this.realtime.notifyScopeKeys(Array.from(affectedTables), maxCursor);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Trigger rate-limited prune after successful pull
|
|
249
|
+
await this.onPullComplete?.();
|
|
250
|
+
|
|
251
|
+
return hasChanges;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Apply a commit from main server locally.
|
|
256
|
+
*
|
|
257
|
+
* This re-applies the commit through the local shape handlers
|
|
258
|
+
* to ensure proper indexing and scope assignment.
|
|
259
|
+
*/
|
|
260
|
+
private async applyCommitLocally(
|
|
261
|
+
commit: SyncCommit,
|
|
262
|
+
table: string
|
|
263
|
+
): Promise<'applied' | 'cached' | 'rejected'> {
|
|
264
|
+
if (commit.changes.length === 0) return 'cached';
|
|
265
|
+
|
|
266
|
+
// Convert changes to operations
|
|
267
|
+
const operations = commit.changes.map((change) => ({
|
|
268
|
+
table: change.table,
|
|
269
|
+
row_id: change.row_id,
|
|
270
|
+
op: change.op,
|
|
271
|
+
payload: change.row_json as Record<string, unknown> | null,
|
|
272
|
+
}));
|
|
273
|
+
|
|
274
|
+
// Generate a unique commit ID for this relay instance
|
|
275
|
+
const relayCommitId = `main:${commit.commitSeq}:${table}`;
|
|
276
|
+
|
|
277
|
+
// Push through local handler
|
|
278
|
+
const result = await pushCommit({
|
|
279
|
+
db: this.db,
|
|
280
|
+
dialect: this.dialect,
|
|
281
|
+
shapes: this.shapes,
|
|
282
|
+
actorId: commit.actorId,
|
|
283
|
+
request: {
|
|
284
|
+
clientId: `relay:${this.clientId}`,
|
|
285
|
+
clientCommitId: relayCommitId,
|
|
286
|
+
operations,
|
|
287
|
+
schemaVersion: 1,
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
if (
|
|
292
|
+
result.response.ok === true &&
|
|
293
|
+
result.response.status === 'applied' &&
|
|
294
|
+
typeof result.response.commitSeq === 'number'
|
|
295
|
+
) {
|
|
296
|
+
// Record sequence mapping
|
|
297
|
+
await this.sequenceMapper.createConfirmedMapping(
|
|
298
|
+
result.response.commitSeq,
|
|
299
|
+
commit.commitSeq
|
|
300
|
+
);
|
|
301
|
+
return 'applied';
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Already applied (cached) - that's fine
|
|
305
|
+
if (result.response.status === 'cached') {
|
|
306
|
+
return 'cached';
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Rejected - this shouldn't happen for pulls from main
|
|
310
|
+
// Do not advance cursor; signal error so caller can react.
|
|
311
|
+
const error = new Error(
|
|
312
|
+
`Relay: Failed to apply commit ${commit.commitSeq} locally (status=${result.response.status})`
|
|
313
|
+
);
|
|
314
|
+
console.warn(
|
|
315
|
+
`Relay: Failed to apply commit ${commit.commitSeq} locally:`,
|
|
316
|
+
result.response
|
|
317
|
+
);
|
|
318
|
+
this.onError?.(error);
|
|
319
|
+
return 'rejected';
|
|
320
|
+
}
|
|
321
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/relay - Sequence Mapper
|
|
3
|
+
*
|
|
4
|
+
* Tracks the mapping between relay's local commit_seq
|
|
5
|
+
* and the main server's global commit_seq.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { type Kysely, sql } from 'kysely';
|
|
9
|
+
import type { RelayDatabase, RelaySequenceMapStatus } from '../schema';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Sequence mapping entry.
|
|
13
|
+
*/
|
|
14
|
+
export interface SequenceMapping {
|
|
15
|
+
localCommitSeq: number;
|
|
16
|
+
mainCommitSeq: number | null;
|
|
17
|
+
status: RelaySequenceMapStatus;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sequence mapper for tracking local to main commit sequence mappings.
|
|
22
|
+
*/
|
|
23
|
+
export class SequenceMapper<DB extends RelayDatabase = RelayDatabase> {
|
|
24
|
+
private readonly db: Kysely<DB>;
|
|
25
|
+
|
|
26
|
+
constructor(options: { db: Kysely<DB> }) {
|
|
27
|
+
this.db = options.db;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Create a pending mapping for a local commit that will be forwarded.
|
|
32
|
+
*/
|
|
33
|
+
async createPendingMapping(localCommitSeq: number): Promise<void> {
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
await sql`
|
|
36
|
+
insert into ${sql.table('relay_sequence_map')} (
|
|
37
|
+
local_commit_seq,
|
|
38
|
+
main_commit_seq,
|
|
39
|
+
status,
|
|
40
|
+
created_at,
|
|
41
|
+
updated_at
|
|
42
|
+
)
|
|
43
|
+
values (${localCommitSeq}, ${null}, 'pending', ${now}, ${now})
|
|
44
|
+
on conflict (local_commit_seq) do nothing
|
|
45
|
+
`.execute(this.db);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Mark a mapping as forwarded with the main server's commit_seq.
|
|
50
|
+
*/
|
|
51
|
+
async markForwarded(
|
|
52
|
+
localCommitSeq: number,
|
|
53
|
+
mainCommitSeq: number
|
|
54
|
+
): Promise<void> {
|
|
55
|
+
const now = Date.now();
|
|
56
|
+
await sql`
|
|
57
|
+
update ${sql.table('relay_sequence_map')}
|
|
58
|
+
set
|
|
59
|
+
main_commit_seq = ${mainCommitSeq},
|
|
60
|
+
status = 'forwarded',
|
|
61
|
+
updated_at = ${now}
|
|
62
|
+
where local_commit_seq = ${localCommitSeq}
|
|
63
|
+
`.execute(this.db);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Mark a mapping as confirmed (main server acknowledged).
|
|
68
|
+
*/
|
|
69
|
+
async markConfirmed(localCommitSeq: number): Promise<void> {
|
|
70
|
+
const now = Date.now();
|
|
71
|
+
await sql`
|
|
72
|
+
update ${sql.table('relay_sequence_map')}
|
|
73
|
+
set status = 'confirmed', updated_at = ${now}
|
|
74
|
+
where local_commit_seq = ${localCommitSeq}
|
|
75
|
+
`.execute(this.db);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get the mapping for a local commit sequence.
|
|
80
|
+
*/
|
|
81
|
+
async getMapping(localCommitSeq: number): Promise<SequenceMapping | null> {
|
|
82
|
+
const rowResult = await sql<{
|
|
83
|
+
local_commit_seq: number;
|
|
84
|
+
main_commit_seq: number | null;
|
|
85
|
+
status: RelaySequenceMapStatus;
|
|
86
|
+
}>`
|
|
87
|
+
select local_commit_seq, main_commit_seq, status
|
|
88
|
+
from ${sql.table('relay_sequence_map')}
|
|
89
|
+
where local_commit_seq = ${localCommitSeq}
|
|
90
|
+
limit 1
|
|
91
|
+
`.execute(this.db);
|
|
92
|
+
const row = rowResult.rows[0];
|
|
93
|
+
|
|
94
|
+
if (!row) return null;
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
localCommitSeq: row.local_commit_seq,
|
|
98
|
+
mainCommitSeq: row.main_commit_seq,
|
|
99
|
+
status: row.status,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Get the local commit sequence for a main server commit sequence.
|
|
105
|
+
*/
|
|
106
|
+
async getLocalCommitSeq(mainCommitSeq: number): Promise<number | null> {
|
|
107
|
+
const rowResult = await sql<{ local_commit_seq: number }>`
|
|
108
|
+
select local_commit_seq
|
|
109
|
+
from ${sql.table('relay_sequence_map')}
|
|
110
|
+
where main_commit_seq = ${mainCommitSeq}
|
|
111
|
+
limit 1
|
|
112
|
+
`.execute(this.db);
|
|
113
|
+
|
|
114
|
+
return rowResult.rows[0]?.local_commit_seq ?? null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Get all pending mappings (commits not yet forwarded).
|
|
119
|
+
*/
|
|
120
|
+
async getPendingMappings(): Promise<SequenceMapping[]> {
|
|
121
|
+
const rowsResult = await sql<{
|
|
122
|
+
local_commit_seq: number;
|
|
123
|
+
main_commit_seq: number | null;
|
|
124
|
+
status: RelaySequenceMapStatus;
|
|
125
|
+
}>`
|
|
126
|
+
select local_commit_seq, main_commit_seq, status
|
|
127
|
+
from ${sql.table('relay_sequence_map')}
|
|
128
|
+
where status = 'pending'
|
|
129
|
+
order by local_commit_seq asc
|
|
130
|
+
`.execute(this.db);
|
|
131
|
+
const rows = rowsResult.rows;
|
|
132
|
+
|
|
133
|
+
return rows.map((row) => ({
|
|
134
|
+
localCommitSeq: row.local_commit_seq,
|
|
135
|
+
mainCommitSeq: row.main_commit_seq,
|
|
136
|
+
status: row.status,
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Create a mapping for commits pulled from main (assigned new local commit_seq).
|
|
142
|
+
*
|
|
143
|
+
* These mappings go directly to 'confirmed' status since they came from main.
|
|
144
|
+
*/
|
|
145
|
+
async createConfirmedMapping(
|
|
146
|
+
localCommitSeq: number,
|
|
147
|
+
mainCommitSeq: number
|
|
148
|
+
): Promise<void> {
|
|
149
|
+
const now = Date.now();
|
|
150
|
+
await sql`
|
|
151
|
+
insert into ${sql.table('relay_sequence_map')} (
|
|
152
|
+
local_commit_seq,
|
|
153
|
+
main_commit_seq,
|
|
154
|
+
status,
|
|
155
|
+
created_at,
|
|
156
|
+
updated_at
|
|
157
|
+
)
|
|
158
|
+
values (
|
|
159
|
+
${localCommitSeq},
|
|
160
|
+
${mainCommitSeq},
|
|
161
|
+
'confirmed',
|
|
162
|
+
${now},
|
|
163
|
+
${now}
|
|
164
|
+
)
|
|
165
|
+
on conflict (local_commit_seq)
|
|
166
|
+
do update set
|
|
167
|
+
main_commit_seq = ${mainCommitSeq},
|
|
168
|
+
status = 'confirmed',
|
|
169
|
+
updated_at = ${now}
|
|
170
|
+
`.execute(this.db);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Delete confirmed/forwarded sequence mappings older than the given age.
|
|
175
|
+
* Keeps pending mappings (they haven't been forwarded yet).
|
|
176
|
+
*/
|
|
177
|
+
async pruneOldMappings(maxAgeMs: number): Promise<number> {
|
|
178
|
+
const threshold = Date.now() - maxAgeMs;
|
|
179
|
+
const result = await sql`
|
|
180
|
+
delete from ${sql.table('relay_sequence_map')}
|
|
181
|
+
where status in ('confirmed', 'forwarded')
|
|
182
|
+
and updated_at < ${threshold}
|
|
183
|
+
`.execute(this.db);
|
|
184
|
+
|
|
185
|
+
return Number(result.numAffectedRows ?? 0);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Get the highest main_commit_seq we've seen (for tracking pull cursor).
|
|
190
|
+
*/
|
|
191
|
+
async getHighestMainCommitSeq(): Promise<number> {
|
|
192
|
+
const rowResult = await sql<{ max_seq: number | null }>`
|
|
193
|
+
select max(main_commit_seq) as max_seq
|
|
194
|
+
from ${sql.table('relay_sequence_map')}
|
|
195
|
+
where main_commit_seq is not null
|
|
196
|
+
limit 1
|
|
197
|
+
`.execute(this.db);
|
|
198
|
+
|
|
199
|
+
return rowResult.rows[0]?.max_seq ?? 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/relay - Edge Relay Server
|
|
3
|
+
*
|
|
4
|
+
* An edge relay server that acts as a local server to nearby clients
|
|
5
|
+
* while simultaneously acting as a client to the main server.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { createRelayServer } from '@syncular/relay';
|
|
10
|
+
* import { createSqliteServerDialect } from '@syncular/server-dialect-sqlite';
|
|
11
|
+
*
|
|
12
|
+
* const relay = createRelayServer({
|
|
13
|
+
* db: sqliteDb,
|
|
14
|
+
* dialect: createSqliteServerDialect(),
|
|
15
|
+
* mainServerTransport: createHttpTransport({ baseUrl: 'https://main.example.com/sync' }),
|
|
16
|
+
* mainServerClientId: 'relay-branch-001',
|
|
17
|
+
* mainServerActorId: 'relay-service',
|
|
18
|
+
* scopeKeys: ['client:acme'],
|
|
19
|
+
* shapes: shapeRegistry,
|
|
20
|
+
* subscriptions: subscriptionRegistry,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Mount routes for local clients
|
|
24
|
+
* app.route('/sync', await relay.getRoutes());
|
|
25
|
+
*
|
|
26
|
+
* // Start background sync with main
|
|
27
|
+
* await relay.start();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
// Client role (syncing with main server)
|
|
32
|
+
export * from './client-role';
|
|
33
|
+
|
|
34
|
+
// Migration
|
|
35
|
+
export * from './migrate';
|
|
36
|
+
|
|
37
|
+
// Mode manager
|
|
38
|
+
export * from './mode-manager';
|
|
39
|
+
|
|
40
|
+
// Realtime WebSocket manager
|
|
41
|
+
export * from './realtime';
|
|
42
|
+
|
|
43
|
+
// Main exports
|
|
44
|
+
export * from './relay';
|
|
45
|
+
|
|
46
|
+
// Schema types
|
|
47
|
+
export * from './schema';
|
|
48
|
+
|
|
49
|
+
// Server role (serving local clients)
|
|
50
|
+
export * from './server-role';
|
package/src/migrate.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/relay - Schema setup
|
|
3
|
+
*
|
|
4
|
+
* Creates relay-specific tables for edge relay functionality.
|
|
5
|
+
* Uses Kysely for dialect-agnostic schema creation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ServerSyncDialect } from '@syncular/server';
|
|
9
|
+
import type { Kysely } from 'kysely';
|
|
10
|
+
import { sql } from 'kysely';
|
|
11
|
+
import type { RelayDatabase } from './schema';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Ensures the relay schema exists in the database.
|
|
15
|
+
* Safe to call multiple times (idempotent).
|
|
16
|
+
*
|
|
17
|
+
* This creates relay-specific tables on top of the base sync schema.
|
|
18
|
+
* Call `ensureSyncSchema()` from @syncular/server first to create base tables.
|
|
19
|
+
*/
|
|
20
|
+
export async function ensureRelaySchema<
|
|
21
|
+
DB extends RelayDatabase = RelayDatabase,
|
|
22
|
+
>(db: Kysely<DB>, dialect: ServerSyncDialect): Promise<void> {
|
|
23
|
+
// Ensure base sync schema exists first
|
|
24
|
+
await dialect.ensureSyncSchema(db);
|
|
25
|
+
|
|
26
|
+
// Create relay-specific tables
|
|
27
|
+
const isSqlite = dialect.name === 'sqlite';
|
|
28
|
+
|
|
29
|
+
// Forward outbox table
|
|
30
|
+
await sql
|
|
31
|
+
.raw(`
|
|
32
|
+
CREATE TABLE IF NOT EXISTS relay_forward_outbox (
|
|
33
|
+
id TEXT PRIMARY KEY,
|
|
34
|
+
local_commit_seq INTEGER NOT NULL,
|
|
35
|
+
client_id TEXT NOT NULL,
|
|
36
|
+
client_commit_id TEXT NOT NULL,
|
|
37
|
+
operations_json TEXT NOT NULL,
|
|
38
|
+
schema_version INTEGER NOT NULL DEFAULT 1,
|
|
39
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
40
|
+
main_commit_seq INTEGER,
|
|
41
|
+
error TEXT,
|
|
42
|
+
last_response_json TEXT,
|
|
43
|
+
created_at INTEGER NOT NULL DEFAULT (${isSqlite ? "strftime('%s','now') * 1000" : 'EXTRACT(EPOCH FROM NOW()) * 1000'}),
|
|
44
|
+
updated_at INTEGER NOT NULL DEFAULT (${isSqlite ? "strftime('%s','now') * 1000" : 'EXTRACT(EPOCH FROM NOW()) * 1000'}),
|
|
45
|
+
attempt_count INTEGER NOT NULL DEFAULT 0
|
|
46
|
+
)
|
|
47
|
+
`)
|
|
48
|
+
.execute(db);
|
|
49
|
+
|
|
50
|
+
// Index for finding next sendable outbox entry
|
|
51
|
+
await sql
|
|
52
|
+
.raw(`
|
|
53
|
+
CREATE INDEX IF NOT EXISTS idx_relay_forward_outbox_status
|
|
54
|
+
ON relay_forward_outbox (status, created_at)
|
|
55
|
+
`)
|
|
56
|
+
.execute(db);
|
|
57
|
+
|
|
58
|
+
// Sequence map table
|
|
59
|
+
await sql
|
|
60
|
+
.raw(`
|
|
61
|
+
CREATE TABLE IF NOT EXISTS relay_sequence_map (
|
|
62
|
+
local_commit_seq INTEGER PRIMARY KEY,
|
|
63
|
+
main_commit_seq INTEGER,
|
|
64
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
65
|
+
created_at INTEGER NOT NULL DEFAULT (${isSqlite ? "strftime('%s','now') * 1000" : 'EXTRACT(EPOCH FROM NOW()) * 1000'}),
|
|
66
|
+
updated_at INTEGER NOT NULL DEFAULT (${isSqlite ? "strftime('%s','now') * 1000" : 'EXTRACT(EPOCH FROM NOW()) * 1000'})
|
|
67
|
+
)
|
|
68
|
+
`)
|
|
69
|
+
.execute(db);
|
|
70
|
+
|
|
71
|
+
// Index for looking up main_commit_seq
|
|
72
|
+
await sql
|
|
73
|
+
.raw(`
|
|
74
|
+
CREATE INDEX IF NOT EXISTS idx_relay_sequence_map_main
|
|
75
|
+
ON relay_sequence_map (main_commit_seq)
|
|
76
|
+
WHERE main_commit_seq IS NOT NULL
|
|
77
|
+
`)
|
|
78
|
+
.execute(db);
|
|
79
|
+
|
|
80
|
+
// Forward conflicts table
|
|
81
|
+
await sql
|
|
82
|
+
.raw(`
|
|
83
|
+
CREATE TABLE IF NOT EXISTS relay_forward_conflicts (
|
|
84
|
+
id TEXT PRIMARY KEY,
|
|
85
|
+
local_commit_seq INTEGER NOT NULL,
|
|
86
|
+
client_id TEXT NOT NULL,
|
|
87
|
+
client_commit_id TEXT NOT NULL,
|
|
88
|
+
response_json TEXT NOT NULL,
|
|
89
|
+
created_at INTEGER NOT NULL,
|
|
90
|
+
resolved_at INTEGER
|
|
91
|
+
)
|
|
92
|
+
`)
|
|
93
|
+
.execute(db);
|
|
94
|
+
|
|
95
|
+
// Index for finding unresolved conflicts
|
|
96
|
+
await sql
|
|
97
|
+
.raw(`
|
|
98
|
+
CREATE INDEX IF NOT EXISTS idx_relay_forward_conflicts_unresolved
|
|
99
|
+
ON relay_forward_conflicts (resolved_at)
|
|
100
|
+
WHERE resolved_at IS NULL
|
|
101
|
+
`)
|
|
102
|
+
.execute(db);
|
|
103
|
+
|
|
104
|
+
// Config table
|
|
105
|
+
await sql
|
|
106
|
+
.raw(`
|
|
107
|
+
CREATE TABLE IF NOT EXISTS relay_config (
|
|
108
|
+
key TEXT PRIMARY KEY,
|
|
109
|
+
value_json TEXT NOT NULL
|
|
110
|
+
)
|
|
111
|
+
`)
|
|
112
|
+
.execute(db);
|
|
113
|
+
}
|