@syncular/server 0.0.6-90 → 0.0.6-93

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.
@@ -0,0 +1,318 @@
1
+ import type { ScopeValues } from '@syncular/core';
2
+ import { type Kysely, sql } from 'kysely';
3
+ import type { SyncServerAuth } from '../handlers/types';
4
+ import type { SyncCoreDb } from '../schema';
5
+
6
+ const DEFAULT_SCOPE_CACHE_PARTITION_ID = 'default';
7
+ const DEFAULT_MEMORY_SCOPE_CACHE_TTL_MS = 30_000;
8
+ const DEFAULT_MEMORY_SCOPE_CACHE_MAX_ENTRIES = 5_000;
9
+ const DEFAULT_DATABASE_SCOPE_CACHE_TTL_MS = 60_000;
10
+ const DEFAULT_DATABASE_SCOPE_CACHE_TABLE = 'sync_scope_cache';
11
+
12
+ export interface ScopeCacheContext<
13
+ DB extends SyncCoreDb = SyncCoreDb,
14
+ Auth extends SyncServerAuth = SyncServerAuth,
15
+ > {
16
+ db: Kysely<DB>;
17
+ auth: Auth;
18
+ table: string;
19
+ cacheKey: string;
20
+ }
21
+
22
+ export interface ScopeCacheSetContext<
23
+ DB extends SyncCoreDb = SyncCoreDb,
24
+ Auth extends SyncServerAuth = SyncServerAuth,
25
+ > extends ScopeCacheContext<DB, Auth> {
26
+ scopes: ScopeValues;
27
+ }
28
+
29
+ /**
30
+ * Shared cache contract for scope resolution results.
31
+ *
32
+ * Pull requests always apply request-local memoization first.
33
+ * This cache is used to share results across pulls.
34
+ */
35
+ export interface ScopeCacheBackend {
36
+ name: string;
37
+ get<DB extends SyncCoreDb, Auth extends SyncServerAuth>(
38
+ args: ScopeCacheContext<DB, Auth>
39
+ ): Promise<ScopeValues | null>;
40
+ set<DB extends SyncCoreDb, Auth extends SyncServerAuth>(
41
+ args: ScopeCacheSetContext<DB, Auth>
42
+ ): Promise<void>;
43
+ delete?<DB extends SyncCoreDb, Auth extends SyncServerAuth>(
44
+ args: ScopeCacheContext<DB, Auth>
45
+ ): Promise<void>;
46
+ }
47
+
48
+ export function createDefaultScopeCacheKey(args: {
49
+ table: string;
50
+ auth: SyncServerAuth;
51
+ }): string {
52
+ const partitionId = args.auth.partitionId ?? DEFAULT_SCOPE_CACHE_PARTITION_ID;
53
+ return `${partitionId}\u0000${args.auth.actorId}\u0000${args.table}`;
54
+ }
55
+
56
+ function cloneScopeValues(scopes: ScopeValues): ScopeValues {
57
+ const cloned: ScopeValues = {};
58
+ for (const [key, value] of Object.entries(scopes)) {
59
+ if (typeof value === 'string') {
60
+ cloned[key] = value;
61
+ continue;
62
+ }
63
+ cloned[key] = [...value];
64
+ }
65
+ return cloned;
66
+ }
67
+
68
+ function parseScopeValues(value: string): ScopeValues | null {
69
+ let parsed: unknown;
70
+ try {
71
+ parsed = JSON.parse(value);
72
+ } catch {
73
+ return null;
74
+ }
75
+
76
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
77
+ return null;
78
+ }
79
+
80
+ const out: ScopeValues = {};
81
+ for (const [scopeKey, scopeValue] of Object.entries(parsed)) {
82
+ if (typeof scopeValue === 'string') {
83
+ out[scopeKey] = scopeValue;
84
+ continue;
85
+ }
86
+
87
+ if (
88
+ Array.isArray(scopeValue) &&
89
+ scopeValue.every((item) => typeof item === 'string')
90
+ ) {
91
+ out[scopeKey] = [...scopeValue];
92
+ continue;
93
+ }
94
+
95
+ return null;
96
+ }
97
+
98
+ return out;
99
+ }
100
+
101
+ interface MemoryScopeCacheEntry {
102
+ scopes: ScopeValues;
103
+ expiresAt: number;
104
+ }
105
+
106
+ export interface MemoryScopeCacheOptions {
107
+ ttlMs?: number;
108
+ maxEntries?: number;
109
+ now?: () => number;
110
+ }
111
+
112
+ export function createMemoryScopeCache(
113
+ options?: MemoryScopeCacheOptions
114
+ ): ScopeCacheBackend {
115
+ const ttlMs = options?.ttlMs ?? DEFAULT_MEMORY_SCOPE_CACHE_TTL_MS;
116
+ const maxEntries =
117
+ options?.maxEntries ?? DEFAULT_MEMORY_SCOPE_CACHE_MAX_ENTRIES;
118
+ const now = options?.now ?? Date.now;
119
+ const buckets = new WeakMap<object, Map<string, MemoryScopeCacheEntry>>();
120
+
121
+ function getBucket(db: object): Map<string, MemoryScopeCacheEntry> {
122
+ const existing = buckets.get(db);
123
+ if (existing) {
124
+ return existing;
125
+ }
126
+ const created = new Map<string, MemoryScopeCacheEntry>();
127
+ buckets.set(db, created);
128
+ return created;
129
+ }
130
+
131
+ function evictOldest(bucket: Map<string, MemoryScopeCacheEntry>): void {
132
+ while (bucket.size > maxEntries) {
133
+ const oldestKey = bucket.keys().next().value;
134
+ if (!oldestKey) {
135
+ return;
136
+ }
137
+ bucket.delete(oldestKey);
138
+ }
139
+ }
140
+
141
+ return {
142
+ name: 'memory',
143
+ async get(args) {
144
+ const bucket = getBucket(args.db);
145
+ const entry = bucket.get(args.cacheKey);
146
+ if (!entry) {
147
+ return null;
148
+ }
149
+
150
+ const nowMs = now();
151
+ if (entry.expiresAt <= nowMs) {
152
+ bucket.delete(args.cacheKey);
153
+ return null;
154
+ }
155
+
156
+ return cloneScopeValues(entry.scopes);
157
+ },
158
+ async set(args) {
159
+ const bucket = getBucket(args.db);
160
+ if (ttlMs <= 0) {
161
+ bucket.delete(args.cacheKey);
162
+ return;
163
+ }
164
+
165
+ if (bucket.has(args.cacheKey)) {
166
+ bucket.delete(args.cacheKey);
167
+ }
168
+ bucket.set(args.cacheKey, {
169
+ scopes: cloneScopeValues(args.scopes),
170
+ expiresAt: now() + ttlMs,
171
+ });
172
+ evictOldest(bucket);
173
+ },
174
+ async delete(args) {
175
+ const bucket = getBucket(args.db);
176
+ bucket.delete(args.cacheKey);
177
+ },
178
+ };
179
+ }
180
+
181
+ export interface DatabaseScopeCacheOptions {
182
+ /**
183
+ * Scope cache table name.
184
+ * Default: `sync_scope_cache`
185
+ */
186
+ tableName?: string;
187
+ /**
188
+ * TTL for cache entries (milliseconds).
189
+ * Default: 60000
190
+ */
191
+ ttlMs?: number;
192
+ /**
193
+ * Automatically creates the cache table.
194
+ * Default: true
195
+ */
196
+ autoCreateTable?: boolean;
197
+ now?: () => Date;
198
+ }
199
+
200
+ export function createDatabaseScopeCache(
201
+ options?: DatabaseScopeCacheOptions
202
+ ): ScopeCacheBackend {
203
+ const tableName = options?.tableName ?? DEFAULT_DATABASE_SCOPE_CACHE_TABLE;
204
+ const ttlMs = options?.ttlMs ?? DEFAULT_DATABASE_SCOPE_CACHE_TTL_MS;
205
+ const autoCreateTable = options?.autoCreateTable ?? true;
206
+ const now = options?.now ?? (() => new Date());
207
+ const schemaReady = new WeakMap<object, Promise<void>>();
208
+
209
+ async function ensureTable<DB extends SyncCoreDb>(db: Kysely<DB>) {
210
+ if (!autoCreateTable) {
211
+ return;
212
+ }
213
+
214
+ const existing = schemaReady.get(db);
215
+ if (existing) {
216
+ await existing;
217
+ return;
218
+ }
219
+
220
+ const pending = sql`
221
+ CREATE TABLE IF NOT EXISTS ${sql.table(tableName)} (
222
+ cache_key TEXT PRIMARY KEY,
223
+ scope_values TEXT NOT NULL,
224
+ expires_at TEXT NOT NULL,
225
+ created_at TEXT NOT NULL
226
+ )
227
+ `
228
+ .execute(db)
229
+ .then(() => undefined);
230
+ schemaReady.set(db, pending);
231
+
232
+ try {
233
+ await pending;
234
+ } catch (error) {
235
+ schemaReady.delete(db);
236
+ throw error;
237
+ }
238
+ }
239
+
240
+ async function removeCacheRow<DB extends SyncCoreDb>(args: {
241
+ db: Kysely<DB>;
242
+ cacheKey: string;
243
+ }): Promise<void> {
244
+ await sql`
245
+ DELETE FROM ${sql.table(tableName)}
246
+ WHERE cache_key = ${args.cacheKey}
247
+ `.execute(args.db);
248
+ }
249
+
250
+ return {
251
+ name: 'database',
252
+ async get(args) {
253
+ await ensureTable(args.db);
254
+
255
+ const rows = await sql<{
256
+ scope_values: string;
257
+ expires_at: string;
258
+ }>`
259
+ SELECT scope_values, expires_at
260
+ FROM ${sql.table(tableName)}
261
+ WHERE cache_key = ${args.cacheKey}
262
+ LIMIT 1
263
+ `.execute(args.db);
264
+
265
+ const row = rows.rows[0];
266
+ if (!row) {
267
+ return null;
268
+ }
269
+
270
+ const nowIso = now().toISOString();
271
+ if (row.expires_at <= nowIso) {
272
+ await removeCacheRow({ db: args.db, cacheKey: args.cacheKey });
273
+ return null;
274
+ }
275
+
276
+ const parsed = parseScopeValues(row.scope_values);
277
+ if (!parsed) {
278
+ await removeCacheRow({ db: args.db, cacheKey: args.cacheKey });
279
+ return null;
280
+ }
281
+
282
+ return parsed;
283
+ },
284
+ async set(args) {
285
+ await ensureTable(args.db);
286
+
287
+ if (ttlMs <= 0) {
288
+ await removeCacheRow({ db: args.db, cacheKey: args.cacheKey });
289
+ return;
290
+ }
291
+
292
+ const createdAt = now();
293
+ const expiresAt = new Date(createdAt.getTime() + ttlMs);
294
+ await sql`
295
+ INSERT INTO ${sql.table(tableName)} (
296
+ cache_key,
297
+ scope_values,
298
+ expires_at,
299
+ created_at
300
+ )
301
+ VALUES (
302
+ ${args.cacheKey},
303
+ ${JSON.stringify(cloneScopeValues(args.scopes))},
304
+ ${expiresAt.toISOString()},
305
+ ${createdAt.toISOString()}
306
+ )
307
+ ON CONFLICT (cache_key) DO UPDATE SET
308
+ scope_values = EXCLUDED.scope_values,
309
+ expires_at = EXCLUDED.expires_at,
310
+ created_at = EXCLUDED.created_at
311
+ `.execute(args.db);
312
+ },
313
+ async delete(args) {
314
+ await ensureTable(args.db);
315
+ await removeCacheRow({ db: args.db, cacheKey: args.cacheKey });
316
+ },
317
+ };
318
+ }
@@ -1 +1,2 @@
1
+ export * from './cache';
1
2
  export * from './resolve';
@@ -0,0 +1,180 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
2
+ import { createDatabase } from '@syncular/core';
3
+ import type { Kysely } from 'kysely';
4
+ import { createBunSqliteDialect } from '../../../dialect-bun-sqlite/src';
5
+ import {
6
+ createServerHandler,
7
+ createServerHandlerCollection,
8
+ } from '../handlers';
9
+ import type { SyncCoreDb } from '../schema';
10
+ import { createDatabaseScopeCache, createDefaultScopeCacheKey } from './cache';
11
+ import { resolveEffectiveScopesForSubscriptions } from './resolve';
12
+
13
+ interface TasksTable {
14
+ id: string;
15
+ user_id: string;
16
+ server_version: number;
17
+ }
18
+
19
+ interface TestDb extends SyncCoreDb {
20
+ tasks: TasksTable;
21
+ }
22
+
23
+ interface ClientDb {
24
+ tasks: TasksTable;
25
+ }
26
+
27
+ describe('resolveEffectiveScopesForSubscriptions cache behavior', () => {
28
+ let db: Kysely<TestDb>;
29
+
30
+ beforeEach(() => {
31
+ db = createDatabase<TestDb>({
32
+ dialect: createBunSqliteDialect({ path: ':memory:' }),
33
+ family: 'sqlite',
34
+ });
35
+ });
36
+
37
+ afterEach(async () => {
38
+ await db.destroy();
39
+ });
40
+
41
+ it('memoizes resolveScopes calls inside a single request', async () => {
42
+ let resolveCalls = 0;
43
+ const handler = createServerHandler<TestDb, ClientDb, 'tasks'>({
44
+ table: 'tasks',
45
+ scopes: ['user:{user_id}'],
46
+ resolveScopes: async (ctx) => {
47
+ resolveCalls += 1;
48
+ return { user_id: [ctx.actorId] };
49
+ },
50
+ });
51
+ const handlers = createServerHandlerCollection<TestDb>([handler]);
52
+
53
+ const resolved = await resolveEffectiveScopesForSubscriptions({
54
+ db,
55
+ auth: { actorId: 'u1' },
56
+ handlers,
57
+ subscriptions: [
58
+ { id: 'sub-1', table: 'tasks', scopes: { user_id: 'u1' }, cursor: -1 },
59
+ { id: 'sub-2', table: 'tasks', scopes: { user_id: 'u1' }, cursor: -1 },
60
+ ],
61
+ });
62
+
63
+ expect(resolveCalls).toBe(1);
64
+ expect(resolved).toHaveLength(2);
65
+ expect(resolved.every((entry) => entry.status === 'active')).toBe(true);
66
+ });
67
+
68
+ it('uses shared cache hit and skips resolveScopes', async () => {
69
+ let resolveCalls = 0;
70
+ let cacheGetCalls = 0;
71
+ let cacheSetCalls = 0;
72
+ const handler = createServerHandler<TestDb, ClientDb, 'tasks'>({
73
+ table: 'tasks',
74
+ scopes: ['user:{user_id}'],
75
+ resolveScopes: async (ctx) => {
76
+ resolveCalls += 1;
77
+ return { user_id: [ctx.actorId] };
78
+ },
79
+ });
80
+ const handlers = createServerHandlerCollection<TestDb>([handler]);
81
+
82
+ const resolved = await resolveEffectiveScopesForSubscriptions({
83
+ db,
84
+ auth: { actorId: 'u1' },
85
+ handlers,
86
+ scopeCache: {
87
+ name: 'test-cache',
88
+ async get() {
89
+ cacheGetCalls += 1;
90
+ return { user_id: ['u1'] };
91
+ },
92
+ async set() {
93
+ cacheSetCalls += 1;
94
+ },
95
+ },
96
+ subscriptions: [
97
+ { id: 'sub-1', table: 'tasks', scopes: { user_id: 'u1' }, cursor: -1 },
98
+ ],
99
+ });
100
+
101
+ expect(resolveCalls).toBe(0);
102
+ expect(cacheGetCalls).toBe(1);
103
+ expect(cacheSetCalls).toBe(0);
104
+ expect(resolved[0]?.status).toBe('active');
105
+ });
106
+
107
+ it('uses request-local memoization even with shared cache misses', async () => {
108
+ let resolveCalls = 0;
109
+ let cacheGetCalls = 0;
110
+ let cacheSetCalls = 0;
111
+ const handler = createServerHandler<TestDb, ClientDb, 'tasks'>({
112
+ table: 'tasks',
113
+ scopes: ['user:{user_id}'],
114
+ resolveScopes: async (ctx) => {
115
+ resolveCalls += 1;
116
+ return { user_id: [ctx.actorId] };
117
+ },
118
+ });
119
+ const handlers = createServerHandlerCollection<TestDb>([handler]);
120
+
121
+ const resolved = await resolveEffectiveScopesForSubscriptions({
122
+ db,
123
+ auth: { actorId: 'u1' },
124
+ handlers,
125
+ scopeCache: {
126
+ name: 'test-cache',
127
+ async get() {
128
+ cacheGetCalls += 1;
129
+ return null;
130
+ },
131
+ async set() {
132
+ cacheSetCalls += 1;
133
+ },
134
+ },
135
+ subscriptions: [
136
+ { id: 'sub-1', table: 'tasks', scopes: { user_id: 'u1' }, cursor: -1 },
137
+ { id: 'sub-2', table: 'tasks', scopes: { user_id: 'u1' }, cursor: -1 },
138
+ ],
139
+ });
140
+
141
+ expect(resolveCalls).toBe(1);
142
+ expect(cacheGetCalls).toBe(1);
143
+ expect(cacheSetCalls).toBe(1);
144
+ expect(resolved).toHaveLength(2);
145
+ expect(resolved.every((entry) => entry.status === 'active')).toBe(true);
146
+ });
147
+
148
+ it('roundtrips values through database scope cache', async () => {
149
+ const scopeCache = createDatabaseScopeCache();
150
+ const auth = { actorId: 'u1', partitionId: 'tenant-1' };
151
+ const cacheKey = createDefaultScopeCacheKey({ auth, table: 'tasks' });
152
+ const context = { db, auth, table: 'tasks', cacheKey };
153
+ const scopes = { user_id: ['u1', 'u2'] };
154
+
155
+ await scopeCache.set({ ...context, scopes });
156
+ const cachedScopes = await scopeCache.get(context);
157
+
158
+ expect(cachedScopes).toEqual(scopes);
159
+ });
160
+
161
+ it('expires entries in database scope cache', async () => {
162
+ let nowMs = Date.now();
163
+ const scopeCache = createDatabaseScopeCache({
164
+ ttlMs: 25,
165
+ now: () => new Date(nowMs),
166
+ });
167
+ const auth = { actorId: 'u1', partitionId: 'tenant-1' };
168
+ const cacheKey = createDefaultScopeCacheKey({ auth, table: 'tasks' });
169
+ const context = { db, auth, table: 'tasks', cacheKey };
170
+
171
+ await scopeCache.set({
172
+ ...context,
173
+ scopes: { user_id: ['u1'] },
174
+ });
175
+
176
+ nowMs += 26;
177
+ const cachedScopes = await scopeCache.get(context);
178
+ expect(cachedScopes).toBeNull();
179
+ });
180
+ });
@@ -10,6 +10,7 @@ import {
10
10
  } from '../handlers/collection';
11
11
  import type { SyncServerAuth } from '../handlers/types';
12
12
  import type { SyncCoreDb } from '../schema';
13
+ import { createDefaultScopeCacheKey, type ScopeCacheBackend } from './cache';
13
14
 
14
15
  export class InvalidSubscriptionScopeError extends Error {
15
16
  constructor(message: string) {
@@ -142,9 +143,11 @@ export async function resolveEffectiveScopesForSubscriptions<
142
143
  auth: Auth;
143
144
  subscriptions: SyncSubscriptionRequest[];
144
145
  handlers: ServerHandlerCollection<DB, Auth>;
146
+ scopeCache?: ScopeCacheBackend;
145
147
  }): Promise<ResolvedSubscription[]> {
146
148
  const out: ResolvedSubscription[] = [];
147
149
  const seenIds = new Set<string>();
150
+ const requestScopeCache = new Map<string, ScopeValues | null>();
148
151
 
149
152
  for (const sub of args.subscriptions) {
150
153
  if (!sub.id || typeof sub.id !== 'string') {
@@ -180,21 +183,88 @@ export async function resolveEffectiveScopesForSubscriptions<
180
183
  table: sub.table,
181
184
  });
182
185
 
183
- // Get allowed scopes from the handler
184
- let allowed: ScopeValues;
185
- try {
186
- allowed = await handler.resolveScopes({
187
- db: args.db,
188
- actorId: args.auth.actorId,
189
- auth: args.auth,
190
- });
191
- } catch (resolveErr) {
192
- // Scope resolution failed - mark subscription as revoked
193
- // rather than failing the entire pull
194
- console.error(
195
- `[resolveScopes] Failed for table ${sub.table}, subscription ${sub.id}:`,
196
- resolveErr
197
- );
186
+ // Resolve allowed scopes with request-local memoization first, then
187
+ // optional shared cache backend, then table handler.
188
+ const scopeCacheKey = createDefaultScopeCacheKey({
189
+ auth: args.auth,
190
+ table: sub.table,
191
+ });
192
+ let allowed: ScopeValues | null;
193
+ if (requestScopeCache.has(scopeCacheKey)) {
194
+ allowed = requestScopeCache.get(scopeCacheKey) ?? null;
195
+ } else {
196
+ allowed = null;
197
+ let sharedCacheHit = false;
198
+
199
+ if (args.scopeCache) {
200
+ try {
201
+ const cachedAllowed = await args.scopeCache.get({
202
+ db: args.db,
203
+ auth: args.auth,
204
+ table: sub.table,
205
+ cacheKey: scopeCacheKey,
206
+ });
207
+ if (cachedAllowed !== null) {
208
+ allowed = cachedAllowed;
209
+ sharedCacheHit = true;
210
+ }
211
+ } catch (cacheErr) {
212
+ console.error(
213
+ `[scopeCache.get] Failed for table ${sub.table}, subscription ${sub.id}:`,
214
+ cacheErr
215
+ );
216
+ }
217
+ }
218
+
219
+ if (!sharedCacheHit) {
220
+ try {
221
+ allowed = await handler.resolveScopes({
222
+ db: args.db,
223
+ actorId: args.auth.actorId,
224
+ auth: args.auth,
225
+ });
226
+ } catch (resolveErr) {
227
+ // Scope resolution failed - mark subscription as revoked
228
+ // rather than failing the entire pull
229
+ console.error(
230
+ `[resolveScopes] Failed for table ${sub.table}, subscription ${sub.id}:`,
231
+ resolveErr
232
+ );
233
+ requestScopeCache.set(scopeCacheKey, null);
234
+ out.push({
235
+ id: sub.id,
236
+ table: sub.table,
237
+ scopes: {},
238
+ params: sub.params,
239
+ cursor: sub.cursor,
240
+ bootstrapState: sub.bootstrapState ?? null,
241
+ status: 'revoked',
242
+ });
243
+ continue;
244
+ }
245
+
246
+ if (args.scopeCache && allowed !== null) {
247
+ try {
248
+ await args.scopeCache.set({
249
+ db: args.db,
250
+ auth: args.auth,
251
+ table: sub.table,
252
+ cacheKey: scopeCacheKey,
253
+ scopes: allowed,
254
+ });
255
+ } catch (cacheErr) {
256
+ console.error(
257
+ `[scopeCache.set] Failed for table ${sub.table}, subscription ${sub.id}:`,
258
+ cacheErr
259
+ );
260
+ }
261
+ }
262
+ }
263
+
264
+ requestScopeCache.set(scopeCacheKey, allowed);
265
+ }
266
+
267
+ if (!allowed) {
198
268
  out.push({
199
269
  id: sub.id,
200
270
  table: sub.table,