@syncular/relay 0.0.1-60

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.
Files changed (68) hide show
  1. package/dist/client-role/forward-engine.d.ts +63 -0
  2. package/dist/client-role/forward-engine.d.ts.map +1 -0
  3. package/dist/client-role/forward-engine.js +263 -0
  4. package/dist/client-role/forward-engine.js.map +1 -0
  5. package/dist/client-role/index.d.ts +9 -0
  6. package/dist/client-role/index.d.ts.map +1 -0
  7. package/dist/client-role/index.js +9 -0
  8. package/dist/client-role/index.js.map +1 -0
  9. package/dist/client-role/pull-engine.d.ts +70 -0
  10. package/dist/client-role/pull-engine.d.ts.map +1 -0
  11. package/dist/client-role/pull-engine.js +233 -0
  12. package/dist/client-role/pull-engine.js.map +1 -0
  13. package/dist/client-role/sequence-mapper.d.ts +65 -0
  14. package/dist/client-role/sequence-mapper.d.ts.map +1 -0
  15. package/dist/client-role/sequence-mapper.js +161 -0
  16. package/dist/client-role/sequence-mapper.js.map +1 -0
  17. package/dist/index.d.ts +37 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +44 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/migrate.d.ts +18 -0
  22. package/dist/migrate.d.ts.map +1 -0
  23. package/dist/migrate.js +99 -0
  24. package/dist/migrate.js.map +1 -0
  25. package/dist/mode-manager.d.ts +60 -0
  26. package/dist/mode-manager.d.ts.map +1 -0
  27. package/dist/mode-manager.js +114 -0
  28. package/dist/mode-manager.js.map +1 -0
  29. package/dist/realtime.d.ts +102 -0
  30. package/dist/realtime.d.ts.map +1 -0
  31. package/dist/realtime.js +305 -0
  32. package/dist/realtime.js.map +1 -0
  33. package/dist/relay.d.ts +188 -0
  34. package/dist/relay.d.ts.map +1 -0
  35. package/dist/relay.js +315 -0
  36. package/dist/relay.js.map +1 -0
  37. package/dist/schema.d.ts +158 -0
  38. package/dist/schema.d.ts.map +1 -0
  39. package/dist/schema.js +7 -0
  40. package/dist/schema.js.map +1 -0
  41. package/dist/server-role/index.d.ts +54 -0
  42. package/dist/server-role/index.d.ts.map +1 -0
  43. package/dist/server-role/index.js +198 -0
  44. package/dist/server-role/index.js.map +1 -0
  45. package/dist/server-role/pull.d.ts +25 -0
  46. package/dist/server-role/pull.d.ts.map +1 -0
  47. package/dist/server-role/pull.js +24 -0
  48. package/dist/server-role/pull.js.map +1 -0
  49. package/dist/server-role/push.d.ts +27 -0
  50. package/dist/server-role/push.d.ts.map +1 -0
  51. package/dist/server-role/push.js +98 -0
  52. package/dist/server-role/push.js.map +1 -0
  53. package/package.json +61 -0
  54. package/src/__tests__/relay.test.ts +464 -0
  55. package/src/bun-types.d.ts +50 -0
  56. package/src/client-role/forward-engine.ts +352 -0
  57. package/src/client-role/index.ts +9 -0
  58. package/src/client-role/pull-engine.ts +301 -0
  59. package/src/client-role/sequence-mapper.ts +201 -0
  60. package/src/index.ts +50 -0
  61. package/src/migrate.ts +113 -0
  62. package/src/mode-manager.ts +142 -0
  63. package/src/realtime.ts +370 -0
  64. package/src/relay.ts +421 -0
  65. package/src/schema.ts +171 -0
  66. package/src/server-role/index.ts +342 -0
  67. package/src/server-role/pull.ts +37 -0
  68. package/src/server-role/push.ts +130 -0
@@ -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', 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
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * @syncular/relay - Mode Manager
3
+ *
4
+ * State machine for tracking relay online/offline status.
5
+ */
6
+
7
+ /**
8
+ * Relay operating modes.
9
+ */
10
+ export type RelayMode = 'online' | 'offline' | 'reconnecting';
11
+
12
+ /**
13
+ * Mode manager options.
14
+ */
15
+ export interface ModeManagerOptions {
16
+ healthCheckIntervalMs?: number;
17
+ reconnectBackoffMs?: number;
18
+ maxReconnectBackoffMs?: number;
19
+ onModeChange?: (mode: RelayMode) => void;
20
+ }
21
+
22
+ /**
23
+ * Mode manager for tracking relay online/offline state.
24
+ *
25
+ * Uses health checks to detect connectivity to the main server
26
+ * and manages reconnection with exponential backoff.
27
+ */
28
+ export class ModeManager {
29
+ private mode: RelayMode = 'offline';
30
+ private healthCheckIntervalMs: number;
31
+ private reconnectBackoffMs: number;
32
+ private maxReconnectBackoffMs: number;
33
+ private currentBackoffMs: number;
34
+ private onModeChange?: (mode: RelayMode) => void;
35
+
36
+ private running = false;
37
+ private timer: ReturnType<typeof setTimeout> | null = null;
38
+ private healthCheckFn: (() => Promise<boolean>) | null = null;
39
+ // biome-ignore lint/correctness/noUnusedPrivateClassMembers: assigned and incremented in healthCheck
40
+ private consecutiveFailures = 0;
41
+
42
+ constructor(options: ModeManagerOptions = {}) {
43
+ this.healthCheckIntervalMs = options.healthCheckIntervalMs ?? 30000;
44
+ this.reconnectBackoffMs = options.reconnectBackoffMs ?? 1000;
45
+ this.maxReconnectBackoffMs = options.maxReconnectBackoffMs ?? 60000;
46
+ this.currentBackoffMs = this.reconnectBackoffMs;
47
+ this.onModeChange = options.onModeChange;
48
+ }
49
+
50
+ /**
51
+ * Get the current mode.
52
+ */
53
+ getMode(): RelayMode {
54
+ return this.mode;
55
+ }
56
+
57
+ /**
58
+ * Start the mode manager with a health check function.
59
+ */
60
+ start(healthCheckFn: () => Promise<boolean>): void {
61
+ if (this.running) return;
62
+ this.running = true;
63
+ this.healthCheckFn = healthCheckFn;
64
+
65
+ // Start with immediate health check
66
+ this.scheduleHealthCheck(0);
67
+ }
68
+
69
+ /**
70
+ * Stop the mode manager.
71
+ */
72
+ stop(): void {
73
+ this.running = false;
74
+ if (this.timer) {
75
+ clearTimeout(this.timer);
76
+ this.timer = null;
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Manually report a successful operation (resets backoff).
82
+ */
83
+ reportSuccess(): void {
84
+ if (this.mode !== 'online') {
85
+ this.setMode('online');
86
+ }
87
+ this.consecutiveFailures = 0;
88
+ this.currentBackoffMs = this.reconnectBackoffMs;
89
+ }
90
+
91
+ /**
92
+ * Manually report a failed operation.
93
+ */
94
+ reportFailure(): void {
95
+ this.consecutiveFailures++;
96
+
97
+ if (this.mode === 'online') {
98
+ this.setMode('reconnecting');
99
+ }
100
+
101
+ // Increase backoff for next attempt
102
+ this.currentBackoffMs = Math.min(
103
+ this.currentBackoffMs * 2,
104
+ this.maxReconnectBackoffMs
105
+ );
106
+ }
107
+
108
+ private setMode(newMode: RelayMode): void {
109
+ if (this.mode === newMode) return;
110
+ this.mode = newMode;
111
+ this.onModeChange?.(newMode);
112
+ }
113
+
114
+ private scheduleHealthCheck(delayMs: number): void {
115
+ if (!this.running) return;
116
+ if (this.timer) return;
117
+
118
+ this.timer = setTimeout(async () => {
119
+ this.timer = null;
120
+
121
+ if (!this.healthCheckFn) {
122
+ this.scheduleHealthCheck(this.healthCheckIntervalMs);
123
+ return;
124
+ }
125
+
126
+ try {
127
+ const healthy = await this.healthCheckFn();
128
+
129
+ if (healthy) {
130
+ this.reportSuccess();
131
+ this.scheduleHealthCheck(this.healthCheckIntervalMs);
132
+ } else {
133
+ this.reportFailure();
134
+ this.scheduleHealthCheck(this.currentBackoffMs);
135
+ }
136
+ } catch {
137
+ this.reportFailure();
138
+ this.scheduleHealthCheck(this.currentBackoffMs);
139
+ }
140
+ }, delayMs);
141
+ }
142
+ }