@syncular/server 0.82.3 → 0.84.0

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/storage.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { asNumber } from './pg-executor.js';
1
2
  /**
2
3
  * Evaluate the RFC 0007 predicate against a gate token. A process that
3
4
  * declares the pending checkpoint may serve while it backfills; any other
@@ -87,6 +88,210 @@ export function clientRoundState(lastRoundAtMs, lastErrorCode, consecutiveFailur
87
88
  ...(consecutiveFailures > 0 ? { consecutiveFailures } : {}),
88
89
  };
89
90
  }
91
+ /** Runtime-neutral base64 (no `Buffer`, Workers-safe) for stored push results. */
92
+ function bytesToBase64(bytes) {
93
+ let binary = '';
94
+ for (const byte of bytes)
95
+ binary += String.fromCharCode(byte);
96
+ return btoa(binary);
97
+ }
98
+ function base64ToBytes(text) {
99
+ const binary = atob(text);
100
+ const bytes = new Uint8Array(binary.length);
101
+ for (let i = 0; i < binary.length; i++)
102
+ bytes[i] = binary.charCodeAt(i);
103
+ return bytes;
104
+ }
105
+ /**
106
+ * Some drivers return a JSON column already parsed (pglite, node-postgres);
107
+ * SQLite returns the `TEXT`.
108
+ */
109
+ export function asJson(value) {
110
+ if (typeof value === 'string')
111
+ return JSON.parse(value);
112
+ return value;
113
+ }
114
+ /** Native SQLite and Postgres return `Uint8Array`; D1 returns `ArrayBuffer` for BLOBs. */
115
+ export function asUint8Array(value) {
116
+ if (value instanceof Uint8Array)
117
+ return value;
118
+ if (value instanceof ArrayBuffer)
119
+ return new Uint8Array(value);
120
+ if (Array.isArray(value))
121
+ return new Uint8Array(value);
122
+ throw new Error(`expected a BLOB column, got ${typeof value}`);
123
+ }
124
+ /**
125
+ * Convert a push result to the JSON-able object both SQL backends store.
126
+ * `serverRow` bytes are base64-encoded: JSON cannot hold raw bytes.
127
+ */
128
+ export function pushResultToJson(result) {
129
+ return {
130
+ status: result.status,
131
+ ...(result.commitSeq !== undefined ? { commitSeq: result.commitSeq } : {}),
132
+ ...(result.recordedAtMs !== undefined
133
+ ? { recordedAtMs: result.recordedAtMs }
134
+ : {}),
135
+ ...(result.cacheIdentity !== undefined
136
+ ? { cacheIdentity: result.cacheIdentity }
137
+ : {}),
138
+ ...(result.conflictAuthorization !== undefined
139
+ ? { conflictAuthorization: result.conflictAuthorization }
140
+ : {}),
141
+ results: result.results.map((record) => {
142
+ if (record.status === 'conflict') {
143
+ return {
144
+ opIndex: record.opIndex,
145
+ status: record.status,
146
+ code: record.code,
147
+ message: record.message,
148
+ serverVersion: record.serverVersion,
149
+ serverRow: bytesToBase64(record.serverRow),
150
+ conflictColumns: bytesToBase64(record.conflictColumns),
151
+ };
152
+ }
153
+ if (record.status === 'error') {
154
+ return {
155
+ opIndex: record.opIndex,
156
+ status: record.status,
157
+ code: record.code,
158
+ message: record.message,
159
+ retryable: record.retryable,
160
+ ...(record.details !== undefined ? { details: record.details } : {}),
161
+ };
162
+ }
163
+ return { opIndex: record.opIndex, status: record.status };
164
+ }),
165
+ };
166
+ }
167
+ export function pushResultFromJson(value) {
168
+ const parsed = value;
169
+ const results = parsed.results.map((record) => {
170
+ if (record.status === 'conflict') {
171
+ return {
172
+ opIndex: record.opIndex,
173
+ status: 'conflict',
174
+ code: record.code ?? '',
175
+ message: record.message ?? '',
176
+ serverVersion: record.serverVersion ?? 0,
177
+ serverRow: base64ToBytes(record.serverRow ?? ''),
178
+ conflictColumns: base64ToBytes(record.conflictColumns ?? ''),
179
+ };
180
+ }
181
+ if (record.status === 'error') {
182
+ return {
183
+ opIndex: record.opIndex,
184
+ status: 'error',
185
+ code: record.code ?? '',
186
+ message: record.message ?? '',
187
+ retryable: record.retryable ?? false,
188
+ ...(record.details !== undefined ? { details: record.details } : {}),
189
+ };
190
+ }
191
+ return { opIndex: record.opIndex, status: 'applied' };
192
+ });
193
+ return {
194
+ status: parsed.status,
195
+ ...(parsed.commitSeq !== undefined ? { commitSeq: parsed.commitSeq } : {}),
196
+ ...(parsed.recordedAtMs !== undefined
197
+ ? { recordedAtMs: parsed.recordedAtMs }
198
+ : {}),
199
+ ...(parsed.cacheIdentity !== undefined
200
+ ? { cacheIdentity: parsed.cacheIdentity }
201
+ : {}),
202
+ ...(parsed.conflictAuthorization !== undefined
203
+ ? { conflictAuthorization: parsed.conflictAuthorization }
204
+ : {}),
205
+ results,
206
+ };
207
+ }
208
+ export function toStoredRow(record) {
209
+ return {
210
+ rowId: record.row_id,
211
+ serverVersion: asNumber(record.server_version),
212
+ scopes: asJson(record.scopes),
213
+ payload: asUint8Array(record.payload),
214
+ ...(record.column_versions === null || record.column_versions === undefined
215
+ ? {}
216
+ : { columnVersions: asUint8Array(record.column_versions) }),
217
+ };
218
+ }
219
+ export function toTombstone(record) {
220
+ return {
221
+ commitSeq: asNumber(record.commit_seq),
222
+ ...(record.server_version === null || record.server_version === undefined
223
+ ? {}
224
+ : { serverVersion: asNumber(record.server_version) }),
225
+ ...(record.scopes === null || record.scopes === undefined
226
+ ? {}
227
+ : { scopes: asJson(record.scopes) }),
228
+ };
229
+ }
230
+ export function toStoredChange(record) {
231
+ return {
232
+ table: record.tbl,
233
+ rowId: record.row_id,
234
+ op: asNumber(record.op) === 1 ? 'upsert' : 'delete',
235
+ ...(record.row_version !== null && record.row_version !== undefined
236
+ ? { rowVersion: asNumber(record.row_version) }
237
+ : {}),
238
+ scopes: asJson(record.scopes),
239
+ ...(record.payload !== null && record.payload !== undefined
240
+ ? { payload: asUint8Array(record.payload) }
241
+ : {}),
242
+ };
243
+ }
244
+ export function toStoredReaction(record) {
245
+ return {
246
+ idempotencyKey: record.idempotency_key,
247
+ type: record.type,
248
+ version: asNumber(record.version),
249
+ payload: asJson(record.payload),
250
+ sourceClientId: record.source_client_id,
251
+ sourceClientCommitId: record.source_client_commit_id,
252
+ sourceCommitSeq: asNumber(record.source_commit_seq),
253
+ createdAtMs: asNumber(record.created_at_ms),
254
+ maxAttempts: asNumber(record.max_attempts),
255
+ status: record.status,
256
+ attempts: asNumber(record.attempts),
257
+ availableAtMs: asNumber(record.available_at_ms),
258
+ ...(record.lease_owner !== null ? { leaseOwner: record.lease_owner } : {}),
259
+ ...(record.lease_expires_at_ms !== null
260
+ ? { leaseExpiresAtMs: asNumber(record.lease_expires_at_ms) }
261
+ : {}),
262
+ ...(record.completed_at_ms !== null
263
+ ? { completedAtMs: asNumber(record.completed_at_ms) }
264
+ : {}),
265
+ ...(record.last_failure !== null
266
+ ? { lastFailure: asJson(record.last_failure) }
267
+ : {}),
268
+ };
269
+ }
270
+ export const CLIENT_RECORD_COLUMNS = 'client_id, actor_id, wire_version, cursor, updated_at_ms, schema_version, units, registration_digest, catching_up, last_round_at_ms, last_error_code, consecutive_failures';
271
+ export function toClientRecord(record) {
272
+ return {
273
+ clientId: record.client_id,
274
+ actorId: record.actor_id,
275
+ wireVersion: asNumber(record.wire_version),
276
+ cursor: asNumber(record.cursor),
277
+ updatedAtMs: asNumber(record.updated_at_ms),
278
+ ...(record.schema_version === null
279
+ ? {}
280
+ : { schemaVersion: asNumber(record.schema_version) }),
281
+ ...(record.units === null || record.registration_digest === null
282
+ ? {}
283
+ : {
284
+ registration: {
285
+ units: asJson(record.units),
286
+ digest: asUint8Array(record.registration_digest),
287
+ },
288
+ }),
289
+ catchingUp: asJson(record.catching_up),
290
+ ...clientRoundState(record.last_round_at_ms === null
291
+ ? null
292
+ : asNumber(record.last_round_at_ms), record.last_error_code, asNumber(record.consecutive_failures)),
293
+ };
294
+ }
90
295
  /** Durable partition identity refreshed after host authentication (§2.1). */
91
296
  /** Longest age of `lastAuthenticatedAtMs` that `touchPartition` leaves unwritten. */
92
297
  export const PARTITION_TOUCH_INTERVAL_MS = 60_000;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/server",
3
- "version": "0.82.3",
3
+ "version": "0.84.0",
4
4
  "description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Benjamin Kniffler",
@@ -68,7 +68,7 @@
68
68
  "verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
69
69
  },
70
70
  "dependencies": {
71
- "@syncular/core": "0.82.3"
71
+ "@syncular/core": "0.84.0"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@electric-sql/pglite": "^0.5.4"
package/src/d1-storage.ts CHANGED
@@ -79,7 +79,6 @@ import {
79
79
  import type { CompiledSchema, CompiledTable } from './schema';
80
80
  import { matchesEffective } from './scopes';
81
81
  import {
82
- asUint8Array,
83
82
  deserializePushResult,
84
83
  placeholders,
85
84
  type SqliteRowRecord,
@@ -90,8 +89,6 @@ import {
90
89
  sqliteDdlStatements,
91
90
  SQLITE_UPGRADE_PROBE,
92
91
  sqliteUpgradeDdl,
93
- toStoredRow,
94
- toTombstone,
95
92
  } from './sqlite-dialect';
96
93
  import type { SqliteValue } from './sqlite-driver';
97
94
  import type {
@@ -116,7 +113,12 @@ import type {
116
113
  StoredRow,
117
114
  Tombstone,
118
115
  } from './storage';
119
- import { commitDeleteFloor } from './storage';
116
+ import {
117
+ asUint8Array,
118
+ commitDeleteFloor,
119
+ toStoredRow,
120
+ toTombstone,
121
+ } from './storage';
120
122
  import {
121
123
  isD1ConstraintError,
122
124
  StorageConstraintError,