@g9tm/syncdata 1.5.12 → 1.5.14

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/PARITY.md CHANGED
@@ -45,9 +45,10 @@ it). This file maps every .NET piece to its twin and lists every deliberate diff
45
45
  | `ExportPendingChangesAsync(path)` | `exportPendingChanges(writeLine)` | the caller writes the lines where it wants |
46
46
  | `CheckIntegrityAsync` | `checkIntegrity()` | |
47
47
  | `KeepLostConflicts` (LES-0072) | `keepLostConflicts` | same SQL, same rejection row (code, lost columns, server values as JSON); `G9ApplyReport.conflictsKept`; `test/keepLostConflicts.test.ts` |
48
+ | Lost = pushed columns only; timestamps as instants (LES-0086) | `pushedMask`, `sameValue(…, type)` / `instant` | `PushedMask` / `SameValue(type, …)` / `Instant`: the dirty mask restricted to the batch's columns; DateTime/DateTimeOffset text in either form equal within one microsecond (TS: no BigInt - milliseconds plus 100 ns units); `HeldValueTests` / `test/heldValues.test.ts` |
48
49
  | `SaveHelloAsync(hello, localMidpointUtc, ct)` (LES-0075) | `saveHello(hello, signal, localMidpointMs)` | the engine measures the Hello round trip in both clients and stores `serverTime - midpoint` |
49
50
  | `G9CSqliteException` (`IG9SyncLocalStoreFailure`) | `G9SqliteStoreError` (`localStoreFailure`) | classified from the driver's code |
50
- | `G9CSignalRSyncTransport`, options | `G9SignalRSyncTransport` (`@g9tm/syncdata/signalr`) | over `G9Client` of `@g9tm/signalr-supernetcore-client`; Lynx: `createLynxSyncTransport`. Limits (ADR-0074): `CallTimeout` / `PlanTimeout` / `PushTimeout` / `StreamIdleTimeout` = `callTimeoutMs` / `planTimeoutMs` / `pushTimeoutMs` / `streamIdleTimeoutMs`, same defaults, same retryable `G9S-NET-FAILURE`, same connection drop (`SignalRTimeoutTests` / `test/transportTimeouts.test.ts`) |
51
+ | `G9CSignalRSyncTransport`, options | `G9SignalRSyncTransport` (`@g9tm/syncdata/signalr`) | over `G9Client` of `@g9tm/signalr-supernetcore-client`; Lynx: `createLynxSyncTransport`. Limits (ADR-0074): `CallTimeout` / `PlanTimeout` / `PushTimeout` / `StreamIdleTimeout` = `callTimeoutMs` / `planTimeoutMs` / `pushTimeoutMs` / `streamIdleTimeoutMs`, same defaults, same retryable `G9S-NET-FAILURE`, same connection drop (`SignalRTimeoutTests` / `test/transportTimeouts.test.ts`). After an automatic reconnect both resend the last accepted Hello so the new connection rejoins its poke groups (LES-0087: `OnReconnectedAsync` / `_rejoin`; `SignalRTransportTests.After_the_connection_reconnects_by_itself_…` / `test/transportRejoin.test.ts`) |
51
52
  | `G9CInMemorySyncTransport`, `G9CTransportFaults` | `G9FaultInjectingTransport`, `G9TransportFaults` (`@g9tm/syncdata/testing`) | wraps a real transport (no in-process server engine in JavaScript) |
52
53
 
53
54
  ## Deliberate differences
@@ -159,6 +159,8 @@ export declare class G9ApplyReport {
159
159
  conflicts: number;
160
160
  /** Conflicts the device lost that were kept on it as rejections (LES-0072). */
161
161
  conflictsKept: number;
162
+ /** Conflicts the device lost on paper only: the server already held its values for every lost column (LES-0085). */
163
+ conflictsSameValue: number;
162
164
  commandsCompleted: number;
163
165
  revokedDeleted: number;
164
166
  revokedQuarantined: number;
@@ -145,6 +145,8 @@ export class G9ApplyReport {
145
145
  this.conflicts = 0;
146
146
  /** Conflicts the device lost that were kept on it as rejections (LES-0072). */
147
147
  this.conflictsKept = 0;
148
+ /** Conflicts the device lost on paper only: the server already held its values for every lost column (LES-0085). */
149
+ this.conflictsSameValue = 0;
148
150
  this.commandsCompleted = 0;
149
151
  this.revokedDeleted = 0;
150
152
  this.revokedQuarantined = 0;
@@ -171,6 +173,7 @@ export class G9ApplyReport {
171
173
  this.rejected += other.rejected;
172
174
  this.conflicts += other.conflicts;
173
175
  this.conflictsKept += other.conflictsKept;
176
+ this.conflictsSameValue += other.conflictsSameValue;
174
177
  this.commandsCompleted += other.commandsCompleted;
175
178
  this.revokedDeleted += other.revokedDeleted;
176
179
  this.revokedQuarantined += other.revokedQuarantined;
@@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,CAAC;QAC9F,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAuBD,4EAA4E;AAE5E,0GAA0G;AAC1G,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,KAAK,EAAE,CAAC;IACR,OAAO,EAAE,CAAC;IACV,QAAQ,EAAE,CAAC;IACX,IAAI,EAAE,CAAC;CACC,CAAC;AAQX,MAAM,UAAU,mBAAmB,CAAC,KAAc;IAChD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAQ,KAAyC,CAAC,iBAAiB,KAAK,QAAQ,CAAC;AACzI,CAAC","sourcesContent":["// Twins of G9SyncData.Client/Storage/*.cs: the client store contract, its records, local commands, revocation rules\r\n// and apply diagnostics. The engine is store-agnostic; `@g9tm/syncdata/sqlite` is the implementation.\r\nimport type { G9EMoveOutPolicy } from '../abstractions/enums.js';\r\nimport type { G9Long } from '../abstractions/long.js';\r\nimport type { G9SyncParameters } from '../abstractions/parameters.js';\r\nimport type {\r\n G9DtBackfillRequest,\r\n G9DtCommand,\r\n G9DtHelloResult,\r\n G9DtNegotiatedTable,\r\n G9DtPullContinuation,\r\n G9DtPullPlan,\r\n G9DtPushResult,\r\n G9DtSnapshotFileRequest,\r\n G9DtSubscriptionDefinition,\r\n G9DtSubscriptionRef,\r\n G9DtSyncChunk,\r\n G9DtTableColumns,\r\n G9DtTableDigest,\r\n} from '../protocol/dtos.js';\r\nimport type { G9SyncClientModel } from './model.js';\r\n\r\n// ---------------------------------------------------------------- server state and subscriptions\r\n\r\n/** What the client last learned from the server (`G9CClientServerState`). */\r\nexport interface G9ClientServerState {\r\n epoch: string | null;\r\n lastHelloUtcMs: number | null;\r\n /** Server clock minus device clock, ms. */\r\n serverClockOffsetMs: number;\r\n tables: G9DtNegotiatedTable[];\r\n definitions: G9DtSubscriptionDefinition[];\r\n}\r\n\r\n/** `G9ESubscriptionState`. */\r\nexport const G9ESubscriptionState = {\r\n Active: 0,\r\n NeedsReconcile: 1,\r\n Failed: 2,\r\n} as const;\r\nexport type G9ESubscriptionState = (typeof G9ESubscriptionState)[keyof typeof G9ESubscriptionState];\r\n\r\n/** A local subscription (`G9CSyncSubscriptionRecord`). Times are epoch milliseconds UTC. */\r\nexport interface G9SyncSubscriptionRecord {\r\n key: string;\r\n name: string;\r\n parameters: G9SyncParameters;\r\n /** Membership slot (bit) for persistent subscriptions; the shared ephemeral slot otherwise. */\r\n slot: number;\r\n ephemeral: boolean;\r\n ttlMs: number | null;\r\n cursor: string | null;\r\n /** The move-out policy the server last announced; kept so it still applies once access is gone. */\r\n moveOutPolicy: G9EMoveOutPolicy | null;\r\n definitionHash: string | null;\r\n authHash: string | null;\r\n state: G9ESubscriptionState;\r\n lastErrorCode: string | null;\r\n createdUtcMs: number;\r\n lastSuccessUtcMs: number | null;\r\n lastReconcileUtcMs: number | null;\r\n lastAccessUtcMs: number | null;\r\n}\r\n\r\n/** `G9CSyncSubscriptionRecord.ToReference()`. */\r\nexport function toSubscriptionRef(record: G9SyncSubscriptionRecord): G9DtSubscriptionRef {\r\n return {\r\n name: record.name,\r\n parameters: record.parameters.values,\r\n cursor: record.cursor,\r\n definitionHash: record.definitionHash,\r\n authHash: record.authHash,\r\n };\r\n}\r\n\r\n/** A partial update of a subscription record; absent members are left unchanged (`G9CSubscriptionUpdate`). */\r\nexport interface G9SubscriptionUpdate {\r\n cursor?: string | null;\r\n definitionHash?: string | null;\r\n authHash?: string | null;\r\n state?: G9ESubscriptionState | null;\r\n errorCode?: string | null;\r\n clearError?: boolean;\r\n succeeded?: boolean;\r\n reconciled?: boolean;\r\n accessed?: boolean;\r\n clearCursor?: boolean;\r\n}\r\n\r\n// ---------------------------------------------------------------- push and pull\r\n\r\n/** A push batch built from dirty rows (`G9CPushBatch`). */\r\nexport interface G9PushBatch {\r\n batchId: number;\r\n /** Highest local change sequence included; rows changed after it stay dirty after the acknowledgement. */\r\n maxSequence: number;\r\n frames: Uint8Array[];\r\n tables: G9DtTableColumns[];\r\n rowCount: number;\r\n /** True when the byte budget ended the batch early: more pending rows follow. */\r\n truncated: boolean;\r\n /** Rows rejected on the device while building the batch (G9S-REJ-TOO-LARGE). */\r\n rejectedLocally: number;\r\n /** Commands carried by this batch, in device order. */\r\n commands: G9DtCommand[];\r\n}\r\n\r\n/** A pull being staged, persisted across restarts (`G9CStagedPull`). */\r\nexport interface G9StagedPull {\r\n runId: string;\r\n plan: G9DtPullPlan;\r\n createdUtcMs: number;\r\n backfills: G9DtBackfillRequest[];\r\n /** What the world looked like when the plan was made (ADR-0046); a run only resumes a stage whose generation matches. */\r\n generation: string | null;\r\n /** The cursor each planned subscription had when the plan was made, by subscription key (ADR-0060). */\r\n plannedFromCursors: { [subscriptionKey: string]: string | null };\r\n /** The device clock when the stage began, assigned by the store. */\r\n stageSequence: number;\r\n}\r\n\r\n/** One committed slice of a staged pull (`G9CApplySlice`). */\r\nexport interface G9ApplySlice {\r\n report: G9ApplyReport;\r\n rowsApplied: number;\r\n rowsAppliedTotal: number;\r\n rowsPlanned: number;\r\n isFinal: boolean;\r\n}\r\n\r\n/** `G9CResumePoint`. */\r\nexport interface G9ResumePoint {\r\n continuation: G9DtPullContinuation | null;\r\n stagedRows: number;\r\n stagedBytes: number;\r\n complete: boolean;\r\n}\r\n\r\n// ---------------------------------------------------------------- reports\r\n\r\n/** Why a table large enough for index deferral kept its indexes live. */\r\nexport const REASON_TABLE_NOT_EMPTY = 'table-not-empty';\r\nconst MAX_FOREIGN_TRIGGER_NAMES = 10;\r\n\r\n/** How an apply ran, as opposed to what it changed (`G9CApplyDiagnostics`, ADR-0058). */\r\n/** A file table copied record for record into an empty table with identical columns (the transfer optimisation). */\r\nexport const FILE_MODE_COPY = 'copy';\r\n/** A file table copied into an empty table by column name. */\r\nexport const FILE_MODE_COLUMNS = 'columns';\r\n/** A file table merged into a table that held rows or protected rows: changed rows only. */\r\nexport const FILE_MODE_MERGE = 'merge';\r\n/** A file table inserted into a table holding only OTHER rows (a second site's first load): every row is new, no per-row diff. */\r\nexport const FILE_MODE_APPEND = 'append';\r\n\r\nexport class G9ApplyDiagnostics {\r\n transactions = 0;\r\n transactionsWithTriggersSkipped = 0;\r\n readonly foreignTriggers: string[] = [];\r\n readonly deferredIndexes: { [table: string]: number } = {};\r\n readonly liveIndexReasons: { [table: string]: string } = {};\r\n readonly rowsByTable: { [table: string]: number } = {};\r\n readonly msByTable: { [table: string]: number } = {};\r\n indexRebuildMs = 0;\r\n elapsedMs = 0;\r\n /** Snapshot files copied in (ADR-0072). */\r\n snapshotFiles = 0;\r\n /** Rows that arrived through snapshot files. */\r\n fileRows = 0;\r\n /** Milliseconds spent copying snapshot files, metadata included. */\r\n fileCopyMs = 0;\r\n /** How each table came out of a snapshot file: `copy`, `columns` or `merge` (see FILE_MODE_*). */\r\n readonly fileTables: { [table: string]: string } = {};\r\n\r\n /** True when every transaction ran with trigger execution off, false when any ran with it on, null when nothing was applied. */\r\n get triggersSkipped(): boolean | null {\r\n return this.transactions === 0 ? null : this.transactionsWithTriggersSkipped === this.transactions;\r\n }\r\n\r\n get deferredIndexCount(): number {\r\n let total = 0;\r\n for (const key of Object.keys(this.deferredIndexes)) total += this.deferredIndexes[key] as number;\r\n return total;\r\n }\r\n\r\n merge(other: G9ApplyDiagnostics): void {\r\n this.transactions += other.transactions;\r\n this.transactionsWithTriggersSkipped += other.transactionsWithTriggersSkipped;\r\n this.indexRebuildMs += other.indexRebuildMs;\r\n this.elapsedMs += other.elapsedMs;\r\n this.snapshotFiles += other.snapshotFiles;\r\n this.fileRows += other.fileRows;\r\n this.fileCopyMs += other.fileCopyMs;\r\n for (const key of Object.keys(other.fileTables)) this.fileTables[key] = other.fileTables[key] as string;\r\n for (const name of other.foreignTriggers) {\r\n if (this.foreignTriggers.length >= MAX_FOREIGN_TRIGGER_NAMES) break;\r\n if (!this.foreignTriggers.some((n) => n.toLowerCase() === name.toLowerCase())) this.foreignTriggers.push(name);\r\n }\r\n addAll(this.deferredIndexes, other.deferredIndexes);\r\n for (const key of Object.keys(other.liveIndexReasons)) this.liveIndexReasons[key] = other.liveIndexReasons[key] as string;\r\n addAll(this.rowsByTable, other.rowsByTable);\r\n addAll(this.msByTable, other.msByTable);\r\n }\r\n\r\n /** One line for a log: what engaged, what did not and why, and the tables that took the time. */\r\n toString(): string {\r\n if (this.transactions === 0) return 'nothing applied';\r\n let text = `${this.transactions} transaction(s) in ${this.elapsedMs} ms; triggers `;\r\n text +=\r\n this.triggersSkipped === true\r\n ? 'skipped'\r\n : this.transactionsWithTriggersSkipped === 0\r\n ? 'LIVE'\r\n : `skipped in ${this.transactionsWithTriggersSkipped} of ${this.transactions}`;\r\n if (this.foreignTriggers.length > 0) text += ' (foreign: ' + this.foreignTriggers.join(', ') + ')';\r\n const deferred = Object.keys(this.deferredIndexes);\r\n if (deferred.length > 0) {\r\n text += `; ${this.deferredIndexCount} index(es) rebuilt after the load in ${this.indexRebuildMs} ms (` + deferred.map((t) => t + ':' + this.deferredIndexes[t]).join(', ') + ')';\r\n }\r\n const live = Object.keys(this.liveIndexReasons);\r\n if (live.length > 0) text += '; live indexes on ' + live.map((t) => t + ' (' + this.liveIndexReasons[t] + ')').join(', ');\r\n if (this.snapshotFiles > 0) {\r\n text +=\r\n `; ${this.snapshotFiles} snapshot file(s), ${this.fileRows} rows copied in ${this.fileCopyMs} ms (` +\r\n Object.keys(this.fileTables)\r\n .map((t) => t + ':' + this.fileTables[t])\r\n .join(', ') +\r\n ')';\r\n }\r\n const slowest = Object.keys(this.msByTable)\r\n .sort((a, b) => (this.msByTable[b] as number) - (this.msByTable[a] as number))\r\n .slice(0, 4);\r\n if (slowest.length > 0) {\r\n text +=\r\n '; ' +\r\n slowest\r\n .map((t) => {\r\n const ms = this.msByTable[t] as number;\r\n const rows = this.rowsByTable[t] ?? 0;\r\n const rate = ms > 0 ? Math.floor((rows * 1000) / ms) : rows;\r\n return `${t} ${rows} rows in ${ms} ms (${rate}/s)`;\r\n })\r\n .join(', ');\r\n }\r\n return text;\r\n }\r\n}\r\n\r\nfunction addAll(target: { [key: string]: number }, source: { [key: string]: number }): void {\r\n for (const key of Object.keys(source)) target[key] = (target[key] ?? 0) + (source[key] as number);\r\n}\r\n\r\n/** What an apply changed (`G9CApplyReport`). */\r\nexport class G9ApplyReport {\r\n upserts = 0;\r\n deletes = 0;\r\n moveOuts = 0;\r\n membershipAdded = 0;\r\n /** Rows skipped because they carry a pending local change. */\r\n skippedDirty = 0;\r\n accepted = 0;\r\n rejected = 0;\r\n conflicts = 0;\r\n /** Conflicts the device lost that were kept on it as rejections (LES-0072). */\r\n conflictsKept = 0;\r\n commandsCompleted = 0;\r\n revokedDeleted = 0;\r\n revokedQuarantined = 0;\r\n revokedRetained = 0;\r\n /** Changed keys per local table (capped per table). */\r\n readonly changedKeys: { [localTable: string]: string[] } = {};\r\n /** Upserts whose row already held exactly the incoming values (ADR-0058). */\r\n unchangedRows = 0;\r\n readonly diagnostics = new G9ApplyDiagnostics();\r\n\r\n get total(): number {\r\n return this.upserts + this.deletes + this.moveOuts;\r\n }\r\n\r\n merge(other: G9ApplyReport): void {\r\n this.unchangedRows += other.unchangedRows;\r\n this.diagnostics.merge(other.diagnostics);\r\n this.upserts += other.upserts;\r\n this.deletes += other.deletes;\r\n this.moveOuts += other.moveOuts;\r\n this.membershipAdded += other.membershipAdded;\r\n this.skippedDirty += other.skippedDirty;\r\n this.accepted += other.accepted;\r\n this.rejected += other.rejected;\r\n this.conflicts += other.conflicts;\r\n this.conflictsKept += other.conflictsKept;\r\n this.commandsCompleted += other.commandsCompleted;\r\n this.revokedDeleted += other.revokedDeleted;\r\n this.revokedQuarantined += other.revokedQuarantined;\r\n this.revokedRetained += other.revokedRetained;\r\n for (const table of Object.keys(other.changedKeys)) {\r\n const list = this.changedKeys[table] ?? (this.changedKeys[table] = []);\r\n for (const key of other.changedKeys[table] as string[]) list.push(key);\r\n }\r\n }\r\n}\r\n\r\n/** A local row as seen by reconciliation (`G9CLocalRowEntry`). */\r\nexport interface G9LocalRowEntry {\r\n key: string;\r\n bucket: number;\r\n version: number;\r\n hash: G9Long;\r\n member: boolean;\r\n dirty: boolean;\r\n}\r\n\r\nexport interface G9PendingTable {\r\n localTable: string;\r\n upserts: number;\r\n deletes: number;\r\n rejected: number;\r\n}\r\n\r\nexport interface G9PendingSummary {\r\n totalRows: number;\r\n rejectedRows: number;\r\n /** Commands recorded on the device and not yet executed by the server. */\r\n pendingCommands: number;\r\n tables: G9PendingTable[];\r\n}\r\n\r\n/** One entry of the local change feed (`G9CChangeFeedEntry`). */\r\nexport interface G9ChangeFeedEntry {\r\n sequence: number;\r\n key: string;\r\n deleted: boolean;\r\n localPending: boolean;\r\n}\r\n\r\n/**\r\n * Narrows a change-feed read to the keys a query of the app's own tables selects (`G9CChangeFeedKeyFilter`): a projection of one\r\n * site no longer walks the entries of every other site. Deletions are always returned - their rows are gone, so no query can place\r\n * them. `keySql` is one SELECT of one column, embedded as `pk IN (...)`; parameters are bound by name (`$name`, `@name`, `:name`)\r\n * and `$g9t`, `$g9a`, `$g9l` belong to the read.\r\n */\r\nexport interface G9ChangeFeedKeyFilter {\r\n keySql: string;\r\n parameters?: { [name: string]: string | number | null };\r\n}\r\n\r\nconst RESERVED_FEED_PARAMETERS = ['$g9t', '$g9a', '$g9l'];\r\n\r\n/** Throws when the filter cannot be embedded safely (`G9CChangeFeedKeyFilter.Validate`). */\r\nexport function validateChangeFeedKeyFilter(filter: G9ChangeFeedKeyFilter): void {\r\n const sql = (filter.keySql ?? '').trim();\r\n if (sql.slice(0, 6).toUpperCase() !== 'SELECT' || sql.indexOf(';') >= 0) {\r\n throw new Error('A change-feed key filter is a single SELECT statement of one column.');\r\n }\r\n for (const name of Object.keys(filter.parameters ?? {})) {\r\n if (name.length < 2 || '$@:'.indexOf(name[0] as string) < 0) throw new Error(`Change-feed key filter parameter '${name}' must be named ($name, @name or :name).`);\r\n if (RESERVED_FEED_PARAMETERS.indexOf(name.toLowerCase()) >= 0) throw new Error(`Change-feed key filter parameter '${name}' is reserved by the read.`);\r\n }\r\n}\r\n\r\nexport interface G9SyncRejection {\r\n localTable: string;\r\n key: string;\r\n code: string;\r\n message: string | null;\r\n sqlState: string | null;\r\n constraint: string | null;\r\n column: string | null;\r\n details: string | null;\r\n atUtcMs: number;\r\n}\r\n\r\n/** A persisted summary of one run (`G9CSyncRunReport`); serialized with the .NET JSON names into g9_runs. */\r\nexport interface G9SyncRunReport {\r\n runId: string;\r\n startedUtcMs: number;\r\n durationMs: number;\r\n outcome: string;\r\n priority: string;\r\n errorCode: string | null;\r\n errorMessage: string | null;\r\n pushed: number;\r\n rejected: number;\r\n conflicts: number;\r\n downloaded: number;\r\n applied: number;\r\n downloadedBytes: number;\r\n subscriptions: string[];\r\n phaseMs: { [phase: string]: number };\r\n triggersSkipped: boolean | null;\r\n foreignTriggers: string[];\r\n applyTransactions: number;\r\n indexesDeferred: number;\r\n indexRebuildMs: number;\r\n liveIndexReasons: { [table: string]: string };\r\n unchangedRows: number;\r\n}\r\n\r\n// ---------------------------------------------------------------- local commands (ADR-0025)\r\n\r\n/** `G9ELocalCommandState`. */\r\nexport const G9ELocalCommandState = {\r\n Pending: 0,\r\n Ok: 1,\r\n Rejected: 2,\r\n Superseded: 3,\r\n} as const;\r\nexport type G9ELocalCommandState = (typeof G9ELocalCommandState)[keyof typeof G9ELocalCommandState];\r\n\r\n/** A command recorded on this device (`G9CLocalCommand`). */\r\nexport interface G9LocalCommand {\r\n commandId: string;\r\n sequence: number;\r\n name: string;\r\n version: number;\r\n payload: string;\r\n createdUtcMs: number;\r\n state: G9ELocalCommandState;\r\n code: string | null;\r\n message: string | null;\r\n result: string | null;\r\n completedUtcMs: number | null;\r\n}\r\n\r\n/** What to record (`G9CCommandRequest`). */\r\nexport interface G9CommandRequest {\r\n name: string;\r\n version?: number;\r\n /** JSON payload for the server's handler. */\r\n payload: string;\r\n}\r\n\r\n/** A value an app binds through {@link G9LocalCommandWriter}: the SQLite driver's value forms, plus booleans. */\r\nexport type G9LocalSqlArgument = null | number | string | boolean | ArrayBuffer | Uint8Array | { int64: string } | G9Long;\r\n\r\n/**\r\n * Writes a command's local effects in the transaction that records it (`IG9LocalCommandWriter`). Rows written here are\r\n * not uploaded as row changes; they are protected from pulls until the server's result arrives.\r\n */\r\nexport interface G9LocalCommandWriter {\r\n /** Executes SQL; parameters bind positionally as `$p0`, `$p1`, … */\r\n execute(sql: string, ...args: G9LocalSqlArgument[]): Promise<number>;\r\n scalar(sql: string, ...args: G9LocalSqlArgument[]): Promise<unknown>;\r\n}\r\n\r\n// ---------------------------------------------------------------- revocation (ADR-0026)\r\n\r\n/** `G9ERevocationReason`. */\r\nexport const G9ERevocationReason = {\r\n LeftScope: 0,\r\n Unsubscribed: 1,\r\n Expired: 2,\r\n AccessRevoked: 3,\r\n} as const;\r\nexport type G9ERevocationReason = (typeof G9ERevocationReason)[keyof typeof G9ERevocationReason];\r\n\r\n/** `G9ERevocationDisposition`. */\r\nexport const G9ERevocationDisposition = {\r\n Delete: 0,\r\n MarkRevoked: 1,\r\n Quarantine: 2,\r\n Retain: 3,\r\n} as const;\r\nexport type G9ERevocationDisposition = (typeof G9ERevocationDisposition)[keyof typeof G9ERevocationDisposition];\r\n\r\n/** A row no subscription holds any more, as a revocation rule sees it (`G9CRevokedRow`). */\r\nexport interface G9RevokedRow {\r\n localTable: string;\r\n key: string;\r\n reason: G9ERevocationReason;\r\n subscriptionKey: string | null;\r\n hasPendingChanges: boolean;\r\n isRejected: boolean;\r\n /** Column values (key included) as stored locally; empty when the row no longer exists locally. */\r\n values: { [column: string]: unknown };\r\n}\r\n\r\n/** Lets a custom rule change the row before its disposition applies. */\r\nexport interface G9RevokedRowEditor {\r\n set(column: string, value: unknown): void;\r\n}\r\n\r\nexport interface G9RevocationRule {\r\n readonly name: string;\r\n readonly condition: (row: G9RevokedRow) => boolean;\r\n readonly decide: (row: G9RevokedRow, editor: G9RevokedRowEditor) => G9ERevocationDisposition;\r\n readonly retainWhile: ((row: G9RevokedRow) => boolean) | null;\r\n}\r\n\r\n/** Ordered revocation rules (`G9CRevocationPolicy`): the first matching rule decides. */\r\nexport class G9RevocationPolicy {\r\n readonly rules: readonly G9RevocationRule[];\r\n\r\n private constructor(rules: readonly G9RevocationRule[]) {\r\n this.rules = rules;\r\n }\r\n\r\n /** No rules: delete clean rows, quarantine rows with pending changes. */\r\n static readonly default = new G9RevocationPolicy([]);\r\n\r\n static create(configure: (builder: G9RevocationPolicyBuilder) => void): G9RevocationPolicy {\r\n const builder = new G9RevocationPolicyBuilder();\r\n configure(builder);\r\n return new G9RevocationPolicy(builder.rules.slice());\r\n }\r\n\r\n decide(row: G9RevokedRow, editor: G9RevokedRowEditor): { rule: G9RevocationRule | null; disposition: G9ERevocationDisposition } {\r\n for (const rule of this.rules) {\r\n if (rule.condition(row)) return { rule, disposition: rule.decide(row, editor) };\r\n }\r\n return { rule: null, disposition: row.hasPendingChanges ? G9ERevocationDisposition.Quarantine : G9ERevocationDisposition.Delete };\r\n }\r\n\r\n find(name: string): G9RevocationRule | null {\r\n for (const rule of this.rules) if (rule.name === name) return rule;\r\n return null;\r\n }\r\n}\r\n\r\nexport class G9RevocationPolicyBuilder {\r\n readonly rules: G9RevocationRule[] = [];\r\n\r\n when(name: string, condition: (row: G9RevokedRow) => boolean): G9RevocationActionBuilder {\r\n if (typeof name !== 'string' || name.trim() === '') throw new TypeError('A rule needs a name.');\r\n if (this.rules.some((r) => r.name === name)) throw new RangeError(`Revocation rule '${name}' is declared twice.`);\r\n return new G9RevocationActionBuilder(this, name, condition);\r\n }\r\n\r\n /** Rows of one local table. */\r\n forTable(name: string, localTable: string, condition?: (row: G9RevokedRow) => boolean): G9RevocationActionBuilder {\r\n const table = localTable.toLowerCase();\r\n return this.when(name, (row) => row.localTable.toLowerCase() === table && (condition ? condition(row) : true));\r\n }\r\n}\r\n\r\nexport class G9RevocationActionBuilder {\r\n private readonly _owner: G9RevocationPolicyBuilder;\r\n private readonly _name: string;\r\n private readonly _condition: (row: G9RevokedRow) => boolean;\r\n\r\n constructor(owner: G9RevocationPolicyBuilder, name: string, condition: (row: G9RevokedRow) => boolean) {\r\n this._owner = owner;\r\n this._name = name;\r\n this._condition = condition;\r\n }\r\n\r\n delete(): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.Delete, null);\r\n }\r\n\r\n markRevoked(): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.MarkRevoked, null);\r\n }\r\n\r\n quarantine(): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.Quarantine, null);\r\n }\r\n\r\n retainWhile(retainWhile: (row: G9RevokedRow) => boolean): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.Retain, retainWhile);\r\n }\r\n\r\n custom(decide: (row: G9RevokedRow, editor: G9RevokedRowEditor) => G9ERevocationDisposition): G9RevocationPolicyBuilder {\r\n return this._add(decide, null);\r\n }\r\n\r\n private _add(\r\n decide: (row: G9RevokedRow, editor: G9RevokedRowEditor) => G9ERevocationDisposition,\r\n retainWhile: ((row: G9RevokedRow) => boolean) | null,\r\n ): G9RevocationPolicyBuilder {\r\n this._owner.rules.push({ name: this._name, condition: this._condition, decide, retainWhile });\r\n return this._owner;\r\n }\r\n}\r\n\r\n/** A row in the holding area (`G9CQuarantinedRow`). */\r\nexport interface G9QuarantinedRow {\r\n localTable: string;\r\n key: string;\r\n reason: G9ERevocationReason;\r\n rule: string | null;\r\n hadPendingChanges: boolean;\r\n values: { [column: string]: unknown };\r\n atUtcMs: number;\r\n}\r\n\r\n/** A row kept in the app's table after revocation (`G9CRetainedRow`). */\r\nexport interface G9RetainedRow {\r\n localTable: string;\r\n key: string;\r\n disposition: G9ERevocationDisposition;\r\n reason: G9ERevocationReason;\r\n rule: string | null;\r\n atUtcMs: number;\r\n}\r\n\r\n// ---------------------------------------------------------------- failures\r\n\r\n/** What went wrong inside a client store, in terms an application can act on (`G9ELocalStoreFailure`). */\r\nexport const G9ELocalStoreFailure = {\r\n Other: 0,\r\n Corrupt: 1,\r\n DiskFull: 2,\r\n Busy: 3,\r\n} as const;\r\nexport type G9ELocalStoreFailure = (typeof G9ELocalStoreFailure)[keyof typeof G9ELocalStoreFailure];\r\n\r\n/** Implemented by a store's errors so the engine can give a run's failure a stable code (`IG9SyncLocalStoreFailure`). */\r\nexport interface G9SyncLocalStoreFailure {\r\n readonly localStoreFailure: G9ELocalStoreFailure;\r\n}\r\n\r\nexport function isLocalStoreFailure(error: unknown): error is G9SyncLocalStoreFailure {\r\n return typeof error === 'object' && error !== null && typeof (error as { localStoreFailure?: unknown }).localStoreFailure === 'number';\r\n}\r\n\r\n// ---------------------------------------------------------------- the store contract\r\n\r\n/**\r\n * The client storage provider (`IG9SyncClientStore`): local tables, change capture, subscription membership,\r\n * staging, apply and metadata. Every method takes an optional AbortSignal (the CancellationToken twin).\r\n */\r\nexport interface G9SyncClientStore {\r\n initialize(model: G9SyncClientModel, signal?: AbortSignal): Promise<void>;\r\n getClientId(signal?: AbortSignal): Promise<string>;\r\n getServerState(signal?: AbortSignal): Promise<G9ClientServerState>;\r\n /**\r\n * Stores the Hello answer. `localMidpointMs`: the local time the server's clock reading corresponds to - the midpoint of the round\r\n * trip (LES-0075); the offset is then exact up to the asymmetry of the two legs. Omitted: the time of this call.\r\n */\r\n saveHello(hello: G9DtHelloResult, signal?: AbortSignal, localMidpointMs?: number): Promise<void>;\r\n getPendingBackfills(signal?: AbortSignal): Promise<G9DtBackfillRequest[]>;\r\n\r\n getSubscriptions(signal?: AbortSignal): Promise<G9SyncSubscriptionRecord[]>;\r\n ensureSubscription(\r\n name: string,\r\n parameters: G9SyncParameters,\r\n ephemeral: boolean,\r\n ttlMs: number | null,\r\n adoptExistingRows: boolean,\r\n signal?: AbortSignal,\r\n ): Promise<G9SyncSubscriptionRecord>;\r\n removeSubscription(key: string, policy: G9EMoveOutPolicy, signal?: AbortSignal): Promise<void>;\r\n releaseSubscriptionRows(key: string, signal?: AbortSignal): Promise<G9ApplyReport>;\r\n updateSubscription(key: string, update: G9SubscriptionUpdate, signal?: AbortSignal): Promise<void>;\r\n collectExpiredEphemeral(utcNowMs: number, signal?: AbortSignal): Promise<number>;\r\n\r\n buildPushBatch(tables: readonly G9DtNegotiatedTable[], maxRows: number, maxBytes: number, signal?: AbortSignal): Promise<G9PushBatch | null>;\r\n forgetUploadedRows(localTable: string, keys: readonly string[] | null, signal?: AbortSignal): Promise<number>;\r\n applyPushResult(batch: G9PushBatch, result: G9DtPushResult, signal?: AbortSignal): Promise<G9ApplyReport>;\r\n\r\n getStagedPull(signal?: AbortSignal): Promise<G9StagedPull | null>;\r\n /** The newest staged pull planned under exactly this generation, or null (ADR-0060). */\r\n getStagedPullForGeneration(generation: string, signal?: AbortSignal): Promise<G9StagedPull | null>;\r\n beginStagedPull(pull: G9StagedPull, signal?: AbortSignal): Promise<void>;\r\n pruneStagedPulls(olderThanUtcMs: number, maxBytes: number, keepGeneration: string | null, signal?: AbortSignal): Promise<number>;\r\n discardAllStagedPulls(signal?: AbortSignal): Promise<number>;\r\n stageChunk(runId: string, chunk: G9DtSyncChunk, signal?: AbortSignal): Promise<void>;\r\n /**\r\n * The store's offer to receive large first downloads as SQLite snapshot files (ADR-0072), or null for rows only. A store that\r\n * makes an offer stages file segments (`chunk.file`) in `stageChunk` and copies the files in `applyStagedPull`.\r\n */\r\n describeSnapshotFiles?(negotiated: readonly G9DtNegotiatedTable[]): G9DtSnapshotFileRequest | null;\r\n getResumePoint(runId: string, signal?: AbortSignal): Promise<G9ResumePoint>;\r\n applyStagedPull(\r\n runId: string,\r\n rowsApplied: ((rows: number) => void) | null,\r\n slices: ((slice: G9ApplySlice) => void) | null,\r\n signal?: AbortSignal,\r\n ): Promise<G9ApplyReport>;\r\n discardStagedPull(runId: string, signal?: AbortSignal): Promise<void>;\r\n\r\n computeDigest(subscriptionKey: string, table: G9DtNegotiatedTable, signal?: AbortSignal): Promise<G9DtTableDigest>;\r\n readLocalEntries(\r\n subscriptionKey: string,\r\n table: G9DtNegotiatedTable,\r\n buckets: ReadonlySet<number>,\r\n serverKeys: ReadonlySet<string>,\r\n signal?: AbortSignal,\r\n ): Promise<G9LocalRowEntry[]>;\r\n applyReconcile(\r\n subscriptionKey: string,\r\n table: G9DtNegotiatedTable,\r\n fetchedFrames: readonly Uint8Array[],\r\n addMembership: readonly string[],\r\n moveOut: readonly string[],\r\n policy: G9EMoveOutPolicy,\r\n signal?: AbortSignal,\r\n ): Promise<G9ApplyReport>;\r\n\r\n getPendingSummary(signal?: AbortSignal): Promise<G9PendingSummary>;\r\n enqueueCommand(\r\n request: G9CommandRequest,\r\n applyLocally: ((writer: G9LocalCommandWriter, signal?: AbortSignal) => Promise<void>) | null,\r\n signal?: AbortSignal,\r\n ): Promise<G9LocalCommand>;\r\n getQuarantined(signal?: AbortSignal): Promise<G9QuarantinedRow[]>;\r\n restoreQuarantined(localTable: string, key: string, signal?: AbortSignal): Promise<number>;\r\n discardQuarantined(localTable: string | null, key: string | null, signal?: AbortSignal): Promise<number>;\r\n getRetained(signal?: AbortSignal): Promise<G9RetainedRow[]>;\r\n reevaluateRetained(signal?: AbortSignal): Promise<number>;\r\n getCommands(state: G9ELocalCommandState | null, limit: number, signal?: AbortSignal): Promise<G9LocalCommand[]>;\r\n readChangeFeed(localTable: string, afterSequence: number, limit: number, signal?: AbortSignal): Promise<G9ChangeFeedEntry[]>;\r\n /** The feed narrowed to the keys `keyFilter` selects, plus every deletion; fewer than `limit` entries means the read is complete. */\r\n readChangeFeedFiltered(localTable: string, afterSequence: number, limit: number, keyFilter: G9ChangeFeedKeyFilter, signal?: AbortSignal): Promise<G9ChangeFeedEntry[]>;\r\n getChangeSequence(signal?: AbortSignal): Promise<number>;\r\n getChangeFeedFloor(signal?: AbortSignal): Promise<number>;\r\n getRejections(signal?: AbortSignal): Promise<G9SyncRejection[]>;\r\n retryRejected(localTable: string | null, key: string | null, signal?: AbortSignal): Promise<number>;\r\n /** Gives up the local change of rejected rows and takes the server's copy (LES-0070); run a forced reconcile after it. */\r\n revertRejected(localTable: string | null, key: string | null, signal?: AbortSignal): Promise<number>;\r\n markDirty(localTable: string, keys: readonly string[], signal?: AbortSignal): Promise<number>;\r\n adoptExistingRows(signal?: AbortSignal): Promise<number>;\r\n saveRunReport(report: G9SyncRunReport, keep: number, signal?: AbortSignal): Promise<void>;\r\n getRecentRunReports(count: number, signal?: AbortSignal): Promise<G9SyncRunReport[]>;\r\n getPhaseStatistics(signal?: AbortSignal): Promise<{ [key: string]: number }>;\r\n recordPhaseStatistics(samples: { [key: string]: number }, signal?: AbortSignal): Promise<void>;\r\n purgeDeletedFeed(keep: number, signal?: AbortSignal): Promise<number>;\r\n dispose(): Promise<void>;\r\n}\r\n"]}
1
+ {"version":3,"file":"storeTypes.js","sourceRoot":"","sources":["../../src/client/storeTypes.ts"],"names":[],"mappings":"AAkCA,8BAA8B;AAC9B,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,MAAM,EAAE,CAAC;IACT,cAAc,EAAE,CAAC;IACjB,MAAM,EAAE,CAAC;CACD,CAAC;AAyBX,iDAAiD;AACjD,MAAM,UAAU,iBAAiB,CAAC,MAAgC;IAChE,OAAO;QACL,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC,MAAM;QACpC,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,QAAQ,EAAE,MAAM,CAAC,QAAQ;KAC1B,CAAC;AACJ,CAAC;AAiED,2EAA2E;AAE3E,yEAAyE;AACzE,MAAM,CAAC,MAAM,sBAAsB,GAAG,iBAAiB,CAAC;AACxD,MAAM,yBAAyB,GAAG,EAAE,CAAC;AAErC,yFAAyF;AACzF,oHAAoH;AACpH,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC;AACrC,8DAA8D;AAC9D,MAAM,CAAC,MAAM,iBAAiB,GAAG,SAAS,CAAC;AAC3C,4FAA4F;A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;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,wBAAwB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IACpE,CAAC;IAED,WAAW,CAAC,WAA2C;QACrD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,wBAAwB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACvE,CAAC;IAED,MAAM,CAAC,MAAmF;QACxF,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;IAEO,IAAI,CACV,MAAmF,EACnF,WAAoD;QAEpD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC,CAAC;QAC9F,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAuBD,4EAA4E;AAE5E,0GAA0G;AAC1G,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,KAAK,EAAE,CAAC;IACR,OAAO,EAAE,CAAC;IACV,QAAQ,EAAE,CAAC;IACX,IAAI,EAAE,CAAC;CACC,CAAC;AAQX,MAAM,UAAU,mBAAmB,CAAC,KAAc;IAChD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,OAAQ,KAAyC,CAAC,iBAAiB,KAAK,QAAQ,CAAC;AACzI,CAAC","sourcesContent":["// Twins of G9SyncData.Client/Storage/*.cs: the client store contract, its records, local commands, revocation rules\r\n// and apply diagnostics. The engine is store-agnostic; `@g9tm/syncdata/sqlite` is the implementation.\r\nimport type { G9EMoveOutPolicy } from '../abstractions/enums.js';\r\nimport type { G9Long } from '../abstractions/long.js';\r\nimport type { G9SyncParameters } from '../abstractions/parameters.js';\r\nimport type {\r\n G9DtBackfillRequest,\r\n G9DtCommand,\r\n G9DtHelloResult,\r\n G9DtNegotiatedTable,\r\n G9DtPullContinuation,\r\n G9DtPullPlan,\r\n G9DtPushResult,\r\n G9DtSnapshotFileRequest,\r\n G9DtSubscriptionDefinition,\r\n G9DtSubscriptionRef,\r\n G9DtSyncChunk,\r\n G9DtTableColumns,\r\n G9DtTableDigest,\r\n} from '../protocol/dtos.js';\r\nimport type { G9SyncClientModel } from './model.js';\r\n\r\n// ---------------------------------------------------------------- server state and subscriptions\r\n\r\n/** What the client last learned from the server (`G9CClientServerState`). */\r\nexport interface G9ClientServerState {\r\n epoch: string | null;\r\n lastHelloUtcMs: number | null;\r\n /** Server clock minus device clock, ms. */\r\n serverClockOffsetMs: number;\r\n tables: G9DtNegotiatedTable[];\r\n definitions: G9DtSubscriptionDefinition[];\r\n}\r\n\r\n/** `G9ESubscriptionState`. */\r\nexport const G9ESubscriptionState = {\r\n Active: 0,\r\n NeedsReconcile: 1,\r\n Failed: 2,\r\n} as const;\r\nexport type G9ESubscriptionState = (typeof G9ESubscriptionState)[keyof typeof G9ESubscriptionState];\r\n\r\n/** A local subscription (`G9CSyncSubscriptionRecord`). Times are epoch milliseconds UTC. */\r\nexport interface G9SyncSubscriptionRecord {\r\n key: string;\r\n name: string;\r\n parameters: G9SyncParameters;\r\n /** Membership slot (bit) for persistent subscriptions; the shared ephemeral slot otherwise. */\r\n slot: number;\r\n ephemeral: boolean;\r\n ttlMs: number | null;\r\n cursor: string | null;\r\n /** The move-out policy the server last announced; kept so it still applies once access is gone. */\r\n moveOutPolicy: G9EMoveOutPolicy | null;\r\n definitionHash: string | null;\r\n authHash: string | null;\r\n state: G9ESubscriptionState;\r\n lastErrorCode: string | null;\r\n createdUtcMs: number;\r\n lastSuccessUtcMs: number | null;\r\n lastReconcileUtcMs: number | null;\r\n lastAccessUtcMs: number | null;\r\n}\r\n\r\n/** `G9CSyncSubscriptionRecord.ToReference()`. */\r\nexport function toSubscriptionRef(record: G9SyncSubscriptionRecord): G9DtSubscriptionRef {\r\n return {\r\n name: record.name,\r\n parameters: record.parameters.values,\r\n cursor: record.cursor,\r\n definitionHash: record.definitionHash,\r\n authHash: record.authHash,\r\n };\r\n}\r\n\r\n/** A partial update of a subscription record; absent members are left unchanged (`G9CSubscriptionUpdate`). */\r\nexport interface G9SubscriptionUpdate {\r\n cursor?: string | null;\r\n definitionHash?: string | null;\r\n authHash?: string | null;\r\n state?: G9ESubscriptionState | null;\r\n errorCode?: string | null;\r\n clearError?: boolean;\r\n succeeded?: boolean;\r\n reconciled?: boolean;\r\n accessed?: boolean;\r\n clearCursor?: boolean;\r\n}\r\n\r\n// ---------------------------------------------------------------- push and pull\r\n\r\n/** A push batch built from dirty rows (`G9CPushBatch`). */\r\nexport interface G9PushBatch {\r\n batchId: number;\r\n /** Highest local change sequence included; rows changed after it stay dirty after the acknowledgement. */\r\n maxSequence: number;\r\n frames: Uint8Array[];\r\n tables: G9DtTableColumns[];\r\n rowCount: number;\r\n /** True when the byte budget ended the batch early: more pending rows follow. */\r\n truncated: boolean;\r\n /** Rows rejected on the device while building the batch (G9S-REJ-TOO-LARGE). */\r\n rejectedLocally: number;\r\n /** Commands carried by this batch, in device order. */\r\n commands: G9DtCommand[];\r\n}\r\n\r\n/** A pull being staged, persisted across restarts (`G9CStagedPull`). */\r\nexport interface G9StagedPull {\r\n runId: string;\r\n plan: G9DtPullPlan;\r\n createdUtcMs: number;\r\n backfills: G9DtBackfillRequest[];\r\n /** What the world looked like when the plan was made (ADR-0046); a run only resumes a stage whose generation matches. */\r\n generation: string | null;\r\n /** The cursor each planned subscription had when the plan was made, by subscription key (ADR-0060). */\r\n plannedFromCursors: { [subscriptionKey: string]: string | null };\r\n /** The device clock when the stage began, assigned by the store. */\r\n stageSequence: number;\r\n}\r\n\r\n/** One committed slice of a staged pull (`G9CApplySlice`). */\r\nexport interface G9ApplySlice {\r\n report: G9ApplyReport;\r\n rowsApplied: number;\r\n rowsAppliedTotal: number;\r\n rowsPlanned: number;\r\n isFinal: boolean;\r\n}\r\n\r\n/** `G9CResumePoint`. */\r\nexport interface G9ResumePoint {\r\n continuation: G9DtPullContinuation | null;\r\n stagedRows: number;\r\n stagedBytes: number;\r\n complete: boolean;\r\n}\r\n\r\n// ---------------------------------------------------------------- reports\r\n\r\n/** Why a table large enough for index deferral kept its indexes live. */\r\nexport const REASON_TABLE_NOT_EMPTY = 'table-not-empty';\r\nconst MAX_FOREIGN_TRIGGER_NAMES = 10;\r\n\r\n/** How an apply ran, as opposed to what it changed (`G9CApplyDiagnostics`, ADR-0058). */\r\n/** A file table copied record for record into an empty table with identical columns (the transfer optimisation). */\r\nexport const FILE_MODE_COPY = 'copy';\r\n/** A file table copied into an empty table by column name. */\r\nexport const FILE_MODE_COLUMNS = 'columns';\r\n/** A file table merged into a table that held rows or protected rows: changed rows only. */\r\nexport const FILE_MODE_MERGE = 'merge';\r\n/** A file table inserted into a table holding only OTHER rows (a second site's first load): every row is new, no per-row diff. */\r\nexport const FILE_MODE_APPEND = 'append';\r\n\r\nexport class G9ApplyDiagnostics {\r\n transactions = 0;\r\n transactionsWithTriggersSkipped = 0;\r\n readonly foreignTriggers: string[] = [];\r\n readonly deferredIndexes: { [table: string]: number } = {};\r\n readonly liveIndexReasons: { [table: string]: string } = {};\r\n readonly rowsByTable: { [table: string]: number } = {};\r\n readonly msByTable: { [table: string]: number } = {};\r\n indexRebuildMs = 0;\r\n elapsedMs = 0;\r\n /** Snapshot files copied in (ADR-0072). */\r\n snapshotFiles = 0;\r\n /** Rows that arrived through snapshot files. */\r\n fileRows = 0;\r\n /** Milliseconds spent copying snapshot files, metadata included. */\r\n fileCopyMs = 0;\r\n /** How each table came out of a snapshot file: `copy`, `columns` or `merge` (see FILE_MODE_*). */\r\n readonly fileTables: { [table: string]: string } = {};\r\n\r\n /** True when every transaction ran with trigger execution off, false when any ran with it on, null when nothing was applied. */\r\n get triggersSkipped(): boolean | null {\r\n return this.transactions === 0 ? null : this.transactionsWithTriggersSkipped === this.transactions;\r\n }\r\n\r\n get deferredIndexCount(): number {\r\n let total = 0;\r\n for (const key of Object.keys(this.deferredIndexes)) total += this.deferredIndexes[key] as number;\r\n return total;\r\n }\r\n\r\n merge(other: G9ApplyDiagnostics): void {\r\n this.transactions += other.transactions;\r\n this.transactionsWithTriggersSkipped += other.transactionsWithTriggersSkipped;\r\n this.indexRebuildMs += other.indexRebuildMs;\r\n this.elapsedMs += other.elapsedMs;\r\n this.snapshotFiles += other.snapshotFiles;\r\n this.fileRows += other.fileRows;\r\n this.fileCopyMs += other.fileCopyMs;\r\n for (const key of Object.keys(other.fileTables)) this.fileTables[key] = other.fileTables[key] as string;\r\n for (const name of other.foreignTriggers) {\r\n if (this.foreignTriggers.length >= MAX_FOREIGN_TRIGGER_NAMES) break;\r\n if (!this.foreignTriggers.some((n) => n.toLowerCase() === name.toLowerCase())) this.foreignTriggers.push(name);\r\n }\r\n addAll(this.deferredIndexes, other.deferredIndexes);\r\n for (const key of Object.keys(other.liveIndexReasons)) this.liveIndexReasons[key] = other.liveIndexReasons[key] as string;\r\n addAll(this.rowsByTable, other.rowsByTable);\r\n addAll(this.msByTable, other.msByTable);\r\n }\r\n\r\n /** One line for a log: what engaged, what did not and why, and the tables that took the time. */\r\n toString(): string {\r\n if (this.transactions === 0) return 'nothing applied';\r\n let text = `${this.transactions} transaction(s) in ${this.elapsedMs} ms; triggers `;\r\n text +=\r\n this.triggersSkipped === true\r\n ? 'skipped'\r\n : this.transactionsWithTriggersSkipped === 0\r\n ? 'LIVE'\r\n : `skipped in ${this.transactionsWithTriggersSkipped} of ${this.transactions}`;\r\n if (this.foreignTriggers.length > 0) text += ' (foreign: ' + this.foreignTriggers.join(', ') + ')';\r\n const deferred = Object.keys(this.deferredIndexes);\r\n if (deferred.length > 0) {\r\n text += `; ${this.deferredIndexCount} index(es) rebuilt after the load in ${this.indexRebuildMs} ms (` + deferred.map((t) => t + ':' + this.deferredIndexes[t]).join(', ') + ')';\r\n }\r\n const live = Object.keys(this.liveIndexReasons);\r\n if (live.length > 0) text += '; live indexes on ' + live.map((t) => t + ' (' + this.liveIndexReasons[t] + ')').join(', ');\r\n if (this.snapshotFiles > 0) {\r\n text +=\r\n `; ${this.snapshotFiles} snapshot file(s), ${this.fileRows} rows copied in ${this.fileCopyMs} ms (` +\r\n Object.keys(this.fileTables)\r\n .map((t) => t + ':' + this.fileTables[t])\r\n .join(', ') +\r\n ')';\r\n }\r\n const slowest = Object.keys(this.msByTable)\r\n .sort((a, b) => (this.msByTable[b] as number) - (this.msByTable[a] as number))\r\n .slice(0, 4);\r\n if (slowest.length > 0) {\r\n text +=\r\n '; ' +\r\n slowest\r\n .map((t) => {\r\n const ms = this.msByTable[t] as number;\r\n const rows = this.rowsByTable[t] ?? 0;\r\n const rate = ms > 0 ? Math.floor((rows * 1000) / ms) : rows;\r\n return `${t} ${rows} rows in ${ms} ms (${rate}/s)`;\r\n })\r\n .join(', ');\r\n }\r\n return text;\r\n }\r\n}\r\n\r\nfunction addAll(target: { [key: string]: number }, source: { [key: string]: number }): void {\r\n for (const key of Object.keys(source)) target[key] = (target[key] ?? 0) + (source[key] as number);\r\n}\r\n\r\n/** What an apply changed (`G9CApplyReport`). */\r\nexport class G9ApplyReport {\r\n upserts = 0;\r\n deletes = 0;\r\n moveOuts = 0;\r\n membershipAdded = 0;\r\n /** Rows skipped because they carry a pending local change. */\r\n skippedDirty = 0;\r\n accepted = 0;\r\n rejected = 0;\r\n conflicts = 0;\r\n /** Conflicts the device lost that were kept on it as rejections (LES-0072). */\r\n conflictsKept = 0;\r\n /** Conflicts the device lost on paper only: the server already held its values for every lost column (LES-0085). */\r\n conflictsSameValue = 0;\r\n commandsCompleted = 0;\r\n revokedDeleted = 0;\r\n revokedQuarantined = 0;\r\n revokedRetained = 0;\r\n /** Changed keys per local table (capped per table). */\r\n readonly changedKeys: { [localTable: string]: string[] } = {};\r\n /** Upserts whose row already held exactly the incoming values (ADR-0058). */\r\n unchangedRows = 0;\r\n readonly diagnostics = new G9ApplyDiagnostics();\r\n\r\n get total(): number {\r\n return this.upserts + this.deletes + this.moveOuts;\r\n }\r\n\r\n merge(other: G9ApplyReport): void {\r\n this.unchangedRows += other.unchangedRows;\r\n this.diagnostics.merge(other.diagnostics);\r\n this.upserts += other.upserts;\r\n this.deletes += other.deletes;\r\n this.moveOuts += other.moveOuts;\r\n this.membershipAdded += other.membershipAdded;\r\n this.skippedDirty += other.skippedDirty;\r\n this.accepted += other.accepted;\r\n this.rejected += other.rejected;\r\n this.conflicts += other.conflicts;\r\n this.conflictsKept += other.conflictsKept;\r\n this.conflictsSameValue += other.conflictsSameValue;\r\n this.commandsCompleted += other.commandsCompleted;\r\n this.revokedDeleted += other.revokedDeleted;\r\n this.revokedQuarantined += other.revokedQuarantined;\r\n this.revokedRetained += other.revokedRetained;\r\n for (const table of Object.keys(other.changedKeys)) {\r\n const list = this.changedKeys[table] ?? (this.changedKeys[table] = []);\r\n for (const key of other.changedKeys[table] as string[]) list.push(key);\r\n }\r\n }\r\n}\r\n\r\n/** A local row as seen by reconciliation (`G9CLocalRowEntry`). */\r\nexport interface G9LocalRowEntry {\r\n key: string;\r\n bucket: number;\r\n version: number;\r\n hash: G9Long;\r\n member: boolean;\r\n dirty: boolean;\r\n}\r\n\r\nexport interface G9PendingTable {\r\n localTable: string;\r\n upserts: number;\r\n deletes: number;\r\n rejected: number;\r\n}\r\n\r\nexport interface G9PendingSummary {\r\n totalRows: number;\r\n rejectedRows: number;\r\n /** Commands recorded on the device and not yet executed by the server. */\r\n pendingCommands: number;\r\n tables: G9PendingTable[];\r\n}\r\n\r\n/** One entry of the local change feed (`G9CChangeFeedEntry`). */\r\nexport interface G9ChangeFeedEntry {\r\n sequence: number;\r\n key: string;\r\n deleted: boolean;\r\n localPending: boolean;\r\n}\r\n\r\n/**\r\n * Narrows a change-feed read to the keys a query of the app's own tables selects (`G9CChangeFeedKeyFilter`): a projection of one\r\n * site no longer walks the entries of every other site. Deletions are always returned - their rows are gone, so no query can place\r\n * them. `keySql` is one SELECT of one column, embedded as `pk IN (...)`; parameters are bound by name (`$name`, `@name`, `:name`)\r\n * and `$g9t`, `$g9a`, `$g9l` belong to the read.\r\n */\r\nexport interface G9ChangeFeedKeyFilter {\r\n keySql: string;\r\n parameters?: { [name: string]: string | number | null };\r\n}\r\n\r\nconst RESERVED_FEED_PARAMETERS = ['$g9t', '$g9a', '$g9l'];\r\n\r\n/** Throws when the filter cannot be embedded safely (`G9CChangeFeedKeyFilter.Validate`). */\r\nexport function validateChangeFeedKeyFilter(filter: G9ChangeFeedKeyFilter): void {\r\n const sql = (filter.keySql ?? '').trim();\r\n if (sql.slice(0, 6).toUpperCase() !== 'SELECT' || sql.indexOf(';') >= 0) {\r\n throw new Error('A change-feed key filter is a single SELECT statement of one column.');\r\n }\r\n for (const name of Object.keys(filter.parameters ?? {})) {\r\n if (name.length < 2 || '$@:'.indexOf(name[0] as string) < 0) throw new Error(`Change-feed key filter parameter '${name}' must be named ($name, @name or :name).`);\r\n if (RESERVED_FEED_PARAMETERS.indexOf(name.toLowerCase()) >= 0) throw new Error(`Change-feed key filter parameter '${name}' is reserved by the read.`);\r\n }\r\n}\r\n\r\nexport interface G9SyncRejection {\r\n localTable: string;\r\n key: string;\r\n code: string;\r\n message: string | null;\r\n sqlState: string | null;\r\n constraint: string | null;\r\n column: string | null;\r\n details: string | null;\r\n atUtcMs: number;\r\n}\r\n\r\n/** A persisted summary of one run (`G9CSyncRunReport`); serialized with the .NET JSON names into g9_runs. */\r\nexport interface G9SyncRunReport {\r\n runId: string;\r\n startedUtcMs: number;\r\n durationMs: number;\r\n outcome: string;\r\n priority: string;\r\n errorCode: string | null;\r\n errorMessage: string | null;\r\n pushed: number;\r\n rejected: number;\r\n conflicts: number;\r\n downloaded: number;\r\n applied: number;\r\n downloadedBytes: number;\r\n subscriptions: string[];\r\n phaseMs: { [phase: string]: number };\r\n triggersSkipped: boolean | null;\r\n foreignTriggers: string[];\r\n applyTransactions: number;\r\n indexesDeferred: number;\r\n indexRebuildMs: number;\r\n liveIndexReasons: { [table: string]: string };\r\n unchangedRows: number;\r\n}\r\n\r\n// ---------------------------------------------------------------- local commands (ADR-0025)\r\n\r\n/** `G9ELocalCommandState`. */\r\nexport const G9ELocalCommandState = {\r\n Pending: 0,\r\n Ok: 1,\r\n Rejected: 2,\r\n Superseded: 3,\r\n} as const;\r\nexport type G9ELocalCommandState = (typeof G9ELocalCommandState)[keyof typeof G9ELocalCommandState];\r\n\r\n/** A command recorded on this device (`G9CLocalCommand`). */\r\nexport interface G9LocalCommand {\r\n commandId: string;\r\n sequence: number;\r\n name: string;\r\n version: number;\r\n payload: string;\r\n createdUtcMs: number;\r\n state: G9ELocalCommandState;\r\n code: string | null;\r\n message: string | null;\r\n result: string | null;\r\n completedUtcMs: number | null;\r\n}\r\n\r\n/** What to record (`G9CCommandRequest`). */\r\nexport interface G9CommandRequest {\r\n name: string;\r\n version?: number;\r\n /** JSON payload for the server's handler. */\r\n payload: string;\r\n}\r\n\r\n/** A value an app binds through {@link G9LocalCommandWriter}: the SQLite driver's value forms, plus booleans. */\r\nexport type G9LocalSqlArgument = null | number | string | boolean | ArrayBuffer | Uint8Array | { int64: string } | G9Long;\r\n\r\n/**\r\n * Writes a command's local effects in the transaction that records it (`IG9LocalCommandWriter`). Rows written here are\r\n * not uploaded as row changes; they are protected from pulls until the server's result arrives.\r\n */\r\nexport interface G9LocalCommandWriter {\r\n /** Executes SQL; parameters bind positionally as `$p0`, `$p1`, … */\r\n execute(sql: string, ...args: G9LocalSqlArgument[]): Promise<number>;\r\n scalar(sql: string, ...args: G9LocalSqlArgument[]): Promise<unknown>;\r\n}\r\n\r\n// ---------------------------------------------------------------- revocation (ADR-0026)\r\n\r\n/** `G9ERevocationReason`. */\r\nexport const G9ERevocationReason = {\r\n LeftScope: 0,\r\n Unsubscribed: 1,\r\n Expired: 2,\r\n AccessRevoked: 3,\r\n} as const;\r\nexport type G9ERevocationReason = (typeof G9ERevocationReason)[keyof typeof G9ERevocationReason];\r\n\r\n/** `G9ERevocationDisposition`. */\r\nexport const G9ERevocationDisposition = {\r\n Delete: 0,\r\n MarkRevoked: 1,\r\n Quarantine: 2,\r\n Retain: 3,\r\n} as const;\r\nexport type G9ERevocationDisposition = (typeof G9ERevocationDisposition)[keyof typeof G9ERevocationDisposition];\r\n\r\n/** A row no subscription holds any more, as a revocation rule sees it (`G9CRevokedRow`). */\r\nexport interface G9RevokedRow {\r\n localTable: string;\r\n key: string;\r\n reason: G9ERevocationReason;\r\n subscriptionKey: string | null;\r\n hasPendingChanges: boolean;\r\n isRejected: boolean;\r\n /** Column values (key included) as stored locally; empty when the row no longer exists locally. */\r\n values: { [column: string]: unknown };\r\n}\r\n\r\n/** Lets a custom rule change the row before its disposition applies. */\r\nexport interface G9RevokedRowEditor {\r\n set(column: string, value: unknown): void;\r\n}\r\n\r\nexport interface G9RevocationRule {\r\n readonly name: string;\r\n readonly condition: (row: G9RevokedRow) => boolean;\r\n readonly decide: (row: G9RevokedRow, editor: G9RevokedRowEditor) => G9ERevocationDisposition;\r\n readonly retainWhile: ((row: G9RevokedRow) => boolean) | null;\r\n}\r\n\r\n/** Ordered revocation rules (`G9CRevocationPolicy`): the first matching rule decides. */\r\nexport class G9RevocationPolicy {\r\n readonly rules: readonly G9RevocationRule[];\r\n\r\n private constructor(rules: readonly G9RevocationRule[]) {\r\n this.rules = rules;\r\n }\r\n\r\n /** No rules: delete clean rows, quarantine rows with pending changes. */\r\n static readonly default = new G9RevocationPolicy([]);\r\n\r\n static create(configure: (builder: G9RevocationPolicyBuilder) => void): G9RevocationPolicy {\r\n const builder = new G9RevocationPolicyBuilder();\r\n configure(builder);\r\n return new G9RevocationPolicy(builder.rules.slice());\r\n }\r\n\r\n decide(row: G9RevokedRow, editor: G9RevokedRowEditor): { rule: G9RevocationRule | null; disposition: G9ERevocationDisposition } {\r\n for (const rule of this.rules) {\r\n if (rule.condition(row)) return { rule, disposition: rule.decide(row, editor) };\r\n }\r\n return { rule: null, disposition: row.hasPendingChanges ? G9ERevocationDisposition.Quarantine : G9ERevocationDisposition.Delete };\r\n }\r\n\r\n find(name: string): G9RevocationRule | null {\r\n for (const rule of this.rules) if (rule.name === name) return rule;\r\n return null;\r\n }\r\n}\r\n\r\nexport class G9RevocationPolicyBuilder {\r\n readonly rules: G9RevocationRule[] = [];\r\n\r\n when(name: string, condition: (row: G9RevokedRow) => boolean): G9RevocationActionBuilder {\r\n if (typeof name !== 'string' || name.trim() === '') throw new TypeError('A rule needs a name.');\r\n if (this.rules.some((r) => r.name === name)) throw new RangeError(`Revocation rule '${name}' is declared twice.`);\r\n return new G9RevocationActionBuilder(this, name, condition);\r\n }\r\n\r\n /** Rows of one local table. */\r\n forTable(name: string, localTable: string, condition?: (row: G9RevokedRow) => boolean): G9RevocationActionBuilder {\r\n const table = localTable.toLowerCase();\r\n return this.when(name, (row) => row.localTable.toLowerCase() === table && (condition ? condition(row) : true));\r\n }\r\n}\r\n\r\nexport class G9RevocationActionBuilder {\r\n private readonly _owner: G9RevocationPolicyBuilder;\r\n private readonly _name: string;\r\n private readonly _condition: (row: G9RevokedRow) => boolean;\r\n\r\n constructor(owner: G9RevocationPolicyBuilder, name: string, condition: (row: G9RevokedRow) => boolean) {\r\n this._owner = owner;\r\n this._name = name;\r\n this._condition = condition;\r\n }\r\n\r\n delete(): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.Delete, null);\r\n }\r\n\r\n markRevoked(): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.MarkRevoked, null);\r\n }\r\n\r\n quarantine(): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.Quarantine, null);\r\n }\r\n\r\n retainWhile(retainWhile: (row: G9RevokedRow) => boolean): G9RevocationPolicyBuilder {\r\n return this._add(() => G9ERevocationDisposition.Retain, retainWhile);\r\n }\r\n\r\n custom(decide: (row: G9RevokedRow, editor: G9RevokedRowEditor) => G9ERevocationDisposition): G9RevocationPolicyBuilder {\r\n return this._add(decide, null);\r\n }\r\n\r\n private _add(\r\n decide: (row: G9RevokedRow, editor: G9RevokedRowEditor) => G9ERevocationDisposition,\r\n retainWhile: ((row: G9RevokedRow) => boolean) | null,\r\n ): G9RevocationPolicyBuilder {\r\n this._owner.rules.push({ name: this._name, condition: this._condition, decide, retainWhile });\r\n return this._owner;\r\n }\r\n}\r\n\r\n/** A row in the holding area (`G9CQuarantinedRow`). */\r\nexport interface G9QuarantinedRow {\r\n localTable: string;\r\n key: string;\r\n reason: G9ERevocationReason;\r\n rule: string | null;\r\n hadPendingChanges: boolean;\r\n values: { [column: string]: unknown };\r\n atUtcMs: number;\r\n}\r\n\r\n/** A row kept in the app's table after revocation (`G9CRetainedRow`). */\r\nexport interface G9RetainedRow {\r\n localTable: string;\r\n key: string;\r\n disposition: G9ERevocationDisposition;\r\n reason: G9ERevocationReason;\r\n rule: string | null;\r\n atUtcMs: number;\r\n}\r\n\r\n// ---------------------------------------------------------------- failures\r\n\r\n/** What went wrong inside a client store, in terms an application can act on (`G9ELocalStoreFailure`). */\r\nexport const G9ELocalStoreFailure = {\r\n Other: 0,\r\n Corrupt: 1,\r\n DiskFull: 2,\r\n Busy: 3,\r\n} as const;\r\nexport type G9ELocalStoreFailure = (typeof G9ELocalStoreFailure)[keyof typeof G9ELocalStoreFailure];\r\n\r\n/** Implemented by a store's errors so the engine can give a run's failure a stable code (`IG9SyncLocalStoreFailure`). */\r\nexport interface G9SyncLocalStoreFailure {\r\n readonly localStoreFailure: G9ELocalStoreFailure;\r\n}\r\n\r\nexport function isLocalStoreFailure(error: unknown): error is G9SyncLocalStoreFailure {\r\n return typeof error === 'object' && error !== null && typeof (error as { localStoreFailure?: unknown }).localStoreFailure === 'number';\r\n}\r\n\r\n// ---------------------------------------------------------------- the store contract\r\n\r\n/**\r\n * The client storage provider (`IG9SyncClientStore`): local tables, change capture, subscription membership,\r\n * staging, apply and metadata. Every method takes an optional AbortSignal (the CancellationToken twin).\r\n */\r\nexport interface G9SyncClientStore {\r\n initialize(model: G9SyncClientModel, signal?: AbortSignal): Promise<void>;\r\n getClientId(signal?: AbortSignal): Promise<string>;\r\n getServerState(signal?: AbortSignal): Promise<G9ClientServerState>;\r\n /**\r\n * Stores the Hello answer. `localMidpointMs`: the local time the server's clock reading corresponds to - the midpoint of the round\r\n * trip (LES-0075); the offset is then exact up to the asymmetry of the two legs. Omitted: the time of this call.\r\n */\r\n saveHello(hello: G9DtHelloResult, signal?: AbortSignal, localMidpointMs?: number): Promise<void>;\r\n getPendingBackfills(signal?: AbortSignal): Promise<G9DtBackfillRequest[]>;\r\n\r\n getSubscriptions(signal?: AbortSignal): Promise<G9SyncSubscriptionRecord[]>;\r\n ensureSubscription(\r\n name: string,\r\n parameters: G9SyncParameters,\r\n ephemeral: boolean,\r\n ttlMs: number | null,\r\n adoptExistingRows: boolean,\r\n signal?: AbortSignal,\r\n ): Promise<G9SyncSubscriptionRecord>;\r\n removeSubscription(key: string, policy: G9EMoveOutPolicy, signal?: AbortSignal): Promise<void>;\r\n releaseSubscriptionRows(key: string, signal?: AbortSignal): Promise<G9ApplyReport>;\r\n updateSubscription(key: string, update: G9SubscriptionUpdate, signal?: AbortSignal): Promise<void>;\r\n collectExpiredEphemeral(utcNowMs: number, signal?: AbortSignal): Promise<number>;\r\n\r\n buildPushBatch(tables: readonly G9DtNegotiatedTable[], maxRows: number, maxBytes: number, signal?: AbortSignal): Promise<G9PushBatch | null>;\r\n forgetUploadedRows(localTable: string, keys: readonly string[] | null, signal?: AbortSignal): Promise<number>;\r\n applyPushResult(batch: G9PushBatch, result: G9DtPushResult, signal?: AbortSignal): Promise<G9ApplyReport>;\r\n\r\n getStagedPull(signal?: AbortSignal): Promise<G9StagedPull | null>;\r\n /** The newest staged pull planned under exactly this generation, or null (ADR-0060). */\r\n getStagedPullForGeneration(generation: string, signal?: AbortSignal): Promise<G9StagedPull | null>;\r\n beginStagedPull(pull: G9StagedPull, signal?: AbortSignal): Promise<void>;\r\n pruneStagedPulls(olderThanUtcMs: number, maxBytes: number, keepGeneration: string | null, signal?: AbortSignal): Promise<number>;\r\n discardAllStagedPulls(signal?: AbortSignal): Promise<number>;\r\n stageChunk(runId: string, chunk: G9DtSyncChunk, signal?: AbortSignal): Promise<void>;\r\n /**\r\n * The store's offer to receive large first downloads as SQLite snapshot files (ADR-0072), or null for rows only. A store that\r\n * makes an offer stages file segments (`chunk.file`) in `stageChunk` and copies the files in `applyStagedPull`.\r\n */\r\n describeSnapshotFiles?(negotiated: readonly G9DtNegotiatedTable[]): G9DtSnapshotFileRequest | null;\r\n getResumePoint(runId: string, signal?: AbortSignal): Promise<G9ResumePoint>;\r\n applyStagedPull(\r\n runId: string,\r\n rowsApplied: ((rows: number) => void) | null,\r\n slices: ((slice: G9ApplySlice) => void) | null,\r\n signal?: AbortSignal,\r\n ): Promise<G9ApplyReport>;\r\n discardStagedPull(runId: string, signal?: AbortSignal): Promise<void>;\r\n\r\n computeDigest(subscriptionKey: string, table: G9DtNegotiatedTable, signal?: AbortSignal): Promise<G9DtTableDigest>;\r\n readLocalEntries(\r\n subscriptionKey: string,\r\n table: G9DtNegotiatedTable,\r\n buckets: ReadonlySet<number>,\r\n serverKeys: ReadonlySet<string>,\r\n signal?: AbortSignal,\r\n ): Promise<G9LocalRowEntry[]>;\r\n applyReconcile(\r\n subscriptionKey: string,\r\n table: G9DtNegotiatedTable,\r\n fetchedFrames: readonly Uint8Array[],\r\n addMembership: readonly string[],\r\n moveOut: readonly string[],\r\n policy: G9EMoveOutPolicy,\r\n signal?: AbortSignal,\r\n ): Promise<G9ApplyReport>;\r\n\r\n getPendingSummary(signal?: AbortSignal): Promise<G9PendingSummary>;\r\n enqueueCommand(\r\n request: G9CommandRequest,\r\n applyLocally: ((writer: G9LocalCommandWriter, signal?: AbortSignal) => Promise<void>) | null,\r\n signal?: AbortSignal,\r\n ): Promise<G9LocalCommand>;\r\n getQuarantined(signal?: AbortSignal): Promise<G9QuarantinedRow[]>;\r\n restoreQuarantined(localTable: string, key: string, signal?: AbortSignal): Promise<number>;\r\n discardQuarantined(localTable: string | null, key: string | null, signal?: AbortSignal): Promise<number>;\r\n getRetained(signal?: AbortSignal): Promise<G9RetainedRow[]>;\r\n reevaluateRetained(signal?: AbortSignal): Promise<number>;\r\n getCommands(state: G9ELocalCommandState | null, limit: number, signal?: AbortSignal): Promise<G9LocalCommand[]>;\r\n readChangeFeed(localTable: string, afterSequence: number, limit: number, signal?: AbortSignal): Promise<G9ChangeFeedEntry[]>;\r\n /** The feed narrowed to the keys `keyFilter` selects, plus every deletion; fewer than `limit` entries means the read is complete. */\r\n readChangeFeedFiltered(localTable: string, afterSequence: number, limit: number, keyFilter: G9ChangeFeedKeyFilter, signal?: AbortSignal): Promise<G9ChangeFeedEntry[]>;\r\n getChangeSequence(signal?: AbortSignal): Promise<number>;\r\n getChangeFeedFloor(signal?: AbortSignal): Promise<number>;\r\n getRejections(signal?: AbortSignal): Promise<G9SyncRejection[]>;\r\n retryRejected(localTable: string | null, key: string | null, signal?: AbortSignal): Promise<number>;\r\n /** Gives up the local change of rejected rows and takes the server's copy (LES-0070); run a forced reconcile after it. */\r\n revertRejected(localTable: string | null, key: string | null, signal?: AbortSignal): Promise<number>;\r\n markDirty(localTable: string, keys: readonly string[], signal?: AbortSignal): Promise<number>;\r\n adoptExistingRows(signal?: AbortSignal): Promise<number>;\r\n saveRunReport(report: G9SyncRunReport, keep: number, signal?: AbortSignal): Promise<void>;\r\n getRecentRunReports(count: number, signal?: AbortSignal): Promise<G9SyncRunReport[]>;\r\n getPhaseStatistics(signal?: AbortSignal): Promise<{ [key: string]: number }>;\r\n recordPhaseStatistics(samples: { [key: string]: number }, signal?: AbortSignal): Promise<void>;\r\n purgeDeletedFeed(keep: number, signal?: AbortSignal): Promise<number>;\r\n dispose(): Promise<void>;\r\n}\r\n"]}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /** The package version; equal to the .NET packages' (G9SyncDataVersion) — one release, both sides. */
2
- export declare const VERSION = "1.5.12";
2
+ export declare const VERSION = "1.5.14";
3
3
  export { G9EConflictPolicy, G9EMoveOutPolicy, G9ESyncColumnType, G9ESyncDirection, columnTypeName, isColumnType } from './abstractions/enums.js';
4
4
  export { G9SyncErrorCodes, G9SyncException, isRetryableCode, isSyncException, type G9SyncExceptionOptions } from './abstractions/errors.js';
5
5
  export { G9Long, parseJson, stringifyJson } from './abstractions/long.js';
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  // doubles `@g9tm/syncdata/testing`, the pure-JavaScript Brotli decoder `@g9tm/syncdata/brotli-js`; Lynx wiring is the
4
4
  // separate package `@g9tm/syncdata-lynx`. Every public type keeps its .NET name without the G9C prefix (PARITY.md).
5
5
  /** The package version; equal to the .NET packages' (G9SyncDataVersion) — one release, both sides. */
6
- export const VERSION = '1.5.12';
6
+ export const VERSION = '1.5.14';
7
7
  // abstractions
8
8
  export { G9EConflictPolicy, G9EMoveOutPolicy, G9ESyncColumnType, G9ESyncDirection, columnTypeName, isColumnType } from './abstractions/enums.js';
9
9
  export { G9SyncErrorCodes, G9SyncException, isRetryableCode, isSyncException } from './abstractions/errors.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wHAAwH;AACxH,uHAAuH;AACvH,sHAAsH;AACtH,oHAAoH;AAEpH,sGAAsG;AACtG,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,CAAC;AAEhC,eAAe;AACf,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACjJ,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAE,eAAe,EAA+B,MAAM,0BAA0B,CAAC;AAC5I,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,OAAO,E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AChB,cAAc,EACd,WAAW,EACX,YAAY,EACZ,oBAAoB,GAYrB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEnE,2CAA2C;AAC3C,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC","sourcesContent":["// @g9tm/syncdata — the TypeScript twin of the G9SyncData .NET client (abstractions, protocol, engine). The SQLite store\r\n// is `@g9tm/syncdata/sqlite`, the SignalR transport `@g9tm/syncdata/signalr`, Node helpers `@g9tm/syncdata/node`, test\r\n// doubles `@g9tm/syncdata/testing`, the pure-JavaScript Brotli decoder `@g9tm/syncdata/brotli-js`; Lynx wiring is the\r\n// separate package `@g9tm/syncdata-lynx`. Every public type keeps its .NET name without the G9C prefix (PARITY.md).\r\n\r\n/** The package version; equal to the .NET packages' (G9SyncDataVersion) — one release, both sides. */\r\nexport const VERSION = '1.5.12';\r\n\r\n// abstractions\r\nexport { G9EConflictPolicy, G9EMoveOutPolicy, G9ESyncColumnType, G9ESyncDirection, columnTypeName, isColumnType } from './abstractions/enums.js';\r\nexport { G9SyncErrorCodes, G9SyncException, isRetryableCode, isSyncException, type G9SyncExceptionOptions } from './abstractions/errors.js';\r\nexport { G9Long, parseJson, stringifyJson } from './abstractions/long.js';\r\nexport { EMPTY_GUID, guidToN, newGuid, normalizeGuid, tryNormalizeGuid } from './abstractions/guid.js';\r\nexport { G9SyncKey } from './abstractions/key.js';\r\nexport { G9SyncDigest } from './abstractions/digest.js';\r\nexport { G9SyncParameters, escapeDataString } from './abstractions/parameters.js';\r\n\r\n// protocol\r\nexport * from './protocol/dtos.js';\r\nexport { G9SyncHubErrors, G9SyncProtocol, negotiateFrameVersion, tableGroup } from './protocol/protocol.js';\r\nexport {\r\n bytesField,\r\n dateTimeMs,\r\n fromMessagePackNames,\r\n G9WireGuid,\r\n GUID_EXTENSION_TYPE,\r\n readChangesAvailable,\r\n readContinuation,\r\n readHelloResult,\r\n readPullPlan,\r\n readPushResult,\r\n readReconcileSummary,\r\n readSyncChunk,\r\n toMessagePackNames,\r\n type G9WireFormat,\r\n} from './protocol/wire.js';\r\nexport { G9ECompression, G9EPushRowOp, G9ERowBatchOp, G9RowBatchFormat, G9RowBatchFormatError } from './protocol/rowBatch/format.js';\r\nexport { G9RowBatchWriter, type G9DateTimeOffsetValue, type G9DateTimeValue, type G9FrameCompressor } from './protocol/rowBatch/writer.js';\r\nexport {\r\n G9RowBatchReader,\r\n getDefaultFrameDecompressor,\r\n openRowBatch,\r\n openRowBatchSync,\r\n readFrameHeader,\r\n setDefaultFrameDecompressor,\r\n type G9FrameDecompressor,\r\n type G9FrameHeader,\r\n type G9OpenRowBatchOptions,\r\n} from './protocol/rowBatch/reader.js';\r\nexport { decimalToDouble, formatDecimal, formatDouble, parseDecimal, type DecimalParts } from './internal/dotnetFormat.js';\r\n\r\n// client\r\nexport { G9SyncClientModel, G9SyncClientModelBuilder, G9SyncClientTable, G9SyncClientTableBuilder, type G9SyncClientColumn, type G9SyncClientIndex } from './client/model.js';\r\nexport * from './client/storeTypes.js';\r\nexport type { G9SyncTransport } from './client/transport.js';\r\nexport { G9ESyncPhase, G9SyncProgressTracker, phaseName, type G9DtSyncProgress } from './client/progress.js';\r\nexport {\r\n G9ESyncOutcome,\r\n G9ESyncPriority,\r\n G9SyncPostStep,\r\n G9SyncRun,\r\n describeModelProblem,\r\n isTransientError,\r\n normalizeError,\r\n outcomeName,\r\n priorityName,\r\n resolveEngineOptions,\r\n type G9RowsAppliedEvent,\r\n type G9SubscriptionOptions,\r\n type G9SyncEngineOptions,\r\n type G9SyncHealth,\r\n type G9SyncHostHooks,\r\n type G9SyncLogger,\r\n type G9SyncLogLevel,\r\n type G9SyncModelProblem,\r\n type G9SyncPostStepContext,\r\n type G9SyncRequest,\r\n type G9SyncRunResult,\r\n} from './client/runs.js';\r\nexport { G9SyncEngine, stageGeneration } from './client/engine.js';\r\n\r\n// helpers the Lynx package and tests share\r\nexport { base64ToBytes, bytesToBase64 } from './internal/base64.js';\r\nexport { utf8Decode, utf8Encode } from './internal/utf8.js';\r\nexport { md5 } from './internal/md5.js';\r\nexport { sha256, toHex } from './internal/sha256.js';\r\nexport { cancellationError, createAbortController, delay, linkSignals, onAbort, yieldToEventLoop } from './internal/abort.js';\r\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wHAAwH;AACxH,uHAAuH;AACvH,sHAAsH;AACtH,oHAAoH;AAEpH,sGAAsG;AACtG,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,CAAC;AAEhC,eAAe;AACf,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACjJ,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAE,eAAe,EAA+B,MAAM,0BAA0B,CAAC;AAC5I,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACvG,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,gBAA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,sBAAsB,CAAC;AACpE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC","sourcesContent":["// @g9tm/syncdata — the TypeScript twin of the G9SyncData .NET client (abstractions, protocol, engine). The SQLite store\r\n// is `@g9tm/syncdata/sqlite`, the SignalR transport `@g9tm/syncdata/signalr`, Node helpers `@g9tm/syncdata/node`, test\r\n// doubles `@g9tm/syncdata/testing`, the pure-JavaScript Brotli decoder `@g9tm/syncdata/brotli-js`; Lynx wiring is the\r\n// separate package `@g9tm/syncdata-lynx`. Every public type keeps its .NET name without the G9C prefix (PARITY.md).\r\n\r\n/** The package version; equal to the .NET packages' (G9SyncDataVersion) — one release, both sides. */\r\nexport const VERSION = '1.5.14';\r\n\r\n// abstractions\r\nexport { G9EConflictPolicy, G9EMoveOutPolicy, G9ESyncColumnType, G9ESyncDirection, columnTypeName, isColumnType } from './abstractions/enums.js';\r\nexport { G9SyncErrorCodes, G9SyncException, isRetryableCode, isSyncException, type G9SyncExceptionOptions } from './abstractions/errors.js';\r\nexport { G9Long, parseJson, stringifyJson } from './abstractions/long.js';\r\nexport { EMPTY_GUID, guidToN, newGuid, normalizeGuid, tryNormalizeGuid } from './abstractions/guid.js';\r\nexport { G9SyncKey } from './abstractions/key.js';\r\nexport { G9SyncDigest } from './abstractions/digest.js';\r\nexport { G9SyncParameters, escapeDataString } from './abstractions/parameters.js';\r\n\r\n// protocol\r\nexport * from './protocol/dtos.js';\r\nexport { G9SyncHubErrors, G9SyncProtocol, negotiateFrameVersion, tableGroup } from './protocol/protocol.js';\r\nexport {\r\n bytesField,\r\n dateTimeMs,\r\n fromMessagePackNames,\r\n G9WireGuid,\r\n GUID_EXTENSION_TYPE,\r\n readChangesAvailable,\r\n readContinuation,\r\n readHelloResult,\r\n readPullPlan,\r\n readPushResult,\r\n readReconcileSummary,\r\n readSyncChunk,\r\n toMessagePackNames,\r\n type G9WireFormat,\r\n} from './protocol/wire.js';\r\nexport { G9ECompression, G9EPushRowOp, G9ERowBatchOp, G9RowBatchFormat, G9RowBatchFormatError } from './protocol/rowBatch/format.js';\r\nexport { G9RowBatchWriter, type G9DateTimeOffsetValue, type G9DateTimeValue, type G9FrameCompressor } from './protocol/rowBatch/writer.js';\r\nexport {\r\n G9RowBatchReader,\r\n getDefaultFrameDecompressor,\r\n openRowBatch,\r\n openRowBatchSync,\r\n readFrameHeader,\r\n setDefaultFrameDecompressor,\r\n type G9FrameDecompressor,\r\n type G9FrameHeader,\r\n type G9OpenRowBatchOptions,\r\n} from './protocol/rowBatch/reader.js';\r\nexport { decimalToDouble, formatDecimal, formatDouble, parseDecimal, type DecimalParts } from './internal/dotnetFormat.js';\r\n\r\n// client\r\nexport { G9SyncClientModel, G9SyncClientModelBuilder, G9SyncClientTable, G9SyncClientTableBuilder, type G9SyncClientColumn, type G9SyncClientIndex } from './client/model.js';\r\nexport * from './client/storeTypes.js';\r\nexport type { G9SyncTransport } from './client/transport.js';\r\nexport { G9ESyncPhase, G9SyncProgressTracker, phaseName, type G9DtSyncProgress } from './client/progress.js';\r\nexport {\r\n G9ESyncOutcome,\r\n G9ESyncPriority,\r\n G9SyncPostStep,\r\n G9SyncRun,\r\n describeModelProblem,\r\n isTransientError,\r\n normalizeError,\r\n outcomeName,\r\n priorityName,\r\n resolveEngineOptions,\r\n type G9RowsAppliedEvent,\r\n type G9SubscriptionOptions,\r\n type G9SyncEngineOptions,\r\n type G9SyncHealth,\r\n type G9SyncHostHooks,\r\n type G9SyncLogger,\r\n type G9SyncLogLevel,\r\n type G9SyncModelProblem,\r\n type G9SyncPostStepContext,\r\n type G9SyncRequest,\r\n type G9SyncRunResult,\r\n} from './client/runs.js';\r\nexport { G9SyncEngine, stageGeneration } from './client/engine.js';\r\n\r\n// helpers the Lynx package and tests share\r\nexport { base64ToBytes, bytesToBase64 } from './internal/base64.js';\r\nexport { utf8Decode, utf8Encode } from './internal/utf8.js';\r\nexport { md5 } from './internal/md5.js';\r\nexport { sha256, toHex } from './internal/sha256.js';\r\nexport { cancellationError, createAbortController, delay, linkSignals, onAbort, yieldToEventLoop } from './internal/abort.js';\r\n"]}
@@ -57,6 +57,7 @@ export declare class G9SignalRSyncTransport implements G9SyncTransport {
57
57
  private readonly _options;
58
58
  private readonly _handlers;
59
59
  private _connecting;
60
+ private _lastHello;
60
61
  constructor(options: G9SignalRSyncTransportOptions);
61
62
  get isConnected(): boolean;
62
63
  onChangesAvailable(handler: (changes: G9DtChangesAvailable) => void): () => void;
@@ -64,6 +65,11 @@ export declare class G9SignalRSyncTransport implements G9SyncTransport {
64
65
  connect(signal?: AbortSignal): Promise<void>;
65
66
  private _connectCore;
66
67
  hello(request: G9DtHelloRequest, signal?: AbortSignal): Promise<G9DtHelloResult>;
68
+ /**
69
+ * After an automatic reconnect the last accepted Hello is sent again, so the new connection joins the table groups pokes go to
70
+ * (S-2, LES-0087). Fire and forget, bounded by the call timeout; the answer is not used and a failure only delays a hint.
71
+ */
72
+ private _rejoin;
67
73
  push(request: G9DtPushRequest, chunks: AsyncIterable<G9DtSyncChunk>, signal?: AbortSignal): Promise<G9DtPushResult>;
68
74
  planPull(request: G9DtPullRequest, signal?: AbortSignal): Promise<G9DtPullPlan>;
69
75
  pull(token: string, continuation: G9DtPullContinuation | null, signal?: AbortSignal): AsyncIterable<G9DtSyncChunk>;
@@ -22,6 +22,7 @@ export class G9SignalRSyncTransport {
22
22
  var _a, _b, _c, _d;
23
23
  this._handlers = [];
24
24
  this._connecting = null;
25
+ this._lastHello = null;
25
26
  if (!options || typeof options.url !== 'string' || options.url.trim() === '')
26
27
  throw new TypeError('A hub URL is required.');
27
28
  this._options = options;
@@ -40,6 +41,10 @@ export class G9SignalRSyncTransport {
40
41
  clientOptions.accessTokenFactory = options.accessTokenFactory;
41
42
  this.client = options.createClient ? options.createClient(clientOptions) : new G9Client(clientOptions);
42
43
  this.client.on(G9SyncProtocol.Listeners.OnChangesAvailable, (payload) => this._onChanges(payload));
44
+ // Twin of .NET OnReconnectedAsync (S-2, LES-0087): a connection that reconnected by itself is a new connection id in no table group.
45
+ const connection = this.client.connection;
46
+ if (typeof connection.onreconnected === 'function')
47
+ connection.onreconnected(() => this._rejoin());
43
48
  }
44
49
  get isConnected() {
45
50
  return this.client.connection.state === HubConnectionState.Connected;
@@ -97,8 +102,21 @@ export class G9SignalRSyncTransport {
97
102
  throw new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync connection is ${String(this.client.connection.state)}.`, { retryable: true });
98
103
  }
99
104
  }
100
- hello(request, signal) {
101
- return this._invoke(G9SyncProtocol.Methods.Hello, [this._wire(request)], readHelloResult, this._callTimeoutMs(), signal);
105
+ async hello(request, signal) {
106
+ const result = await this._invoke(G9SyncProtocol.Methods.Hello, [this._wire(request)], readHelloResult, this._callTimeoutMs(), signal);
107
+ if (!result.error)
108
+ this._lastHello = request;
109
+ return result;
110
+ }
111
+ /**
112
+ * After an automatic reconnect the last accepted Hello is sent again, so the new connection joins the table groups pokes go to
113
+ * (S-2, LES-0087). Fire and forget, bounded by the call timeout; the answer is not used and a failure only delays a hint.
114
+ */
115
+ _rejoin() {
116
+ const hello = this._lastHello;
117
+ if (!hello)
118
+ return;
119
+ this._invoke(G9SyncProtocol.Methods.Hello, [this._wire(hello)], readHelloResult, this._callTimeoutMs()).catch(() => undefined);
102
120
  }
103
121
  push(request, chunks, signal) {
104
122
  var _a;
@@ -1 +1 @@
1
- {"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/signalr/transport.ts"],"names":[],"mappings":"AAAA,yHAAyH;AACzH,wHAAwH;AACxH,wGAAwG;AACxG,2DAA2D;AAC3D,OAAO,EAAE,QA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eAAe,CAAC,gBAAgB,CAAC,QAAQ,EAAE,wBAAwB,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IAChH,MAAM,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,eAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACjD,IAAI,MAAM;QAAE,OAAO,MAAM,CAAC;IAC1B,MAAM,MAAM,GAAI,KAAkC,CAAC,UAAU,CAAC;IAC9D,IAAI,MAAM,KAAK,GAAG,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,eAAe,CAAC,gBAAgB,CAAC,YAAY,EAAE,0CAA0C,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IAC9J,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY;QAAE,OAAO,IAAI,eAAe,CAAC,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5I,+GAA+G;IAC/G,IAAI,6BAA6B,CAAC,IAAI,CAAC,OAAO,CAAC;QAAE,OAAO,IAAI,eAAe,CAAC,gBAAgB,CAAC,WAAW,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACtJ,OAAO,IAAI,eAAe,CAAC,gBAAgB,CAAC,cAAc,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;AAC1G,CAAC","sourcesContent":["// Twin of G9SyncData.Client.SignalR/G9CSignalRSyncTransport.cs: the sync transport over the G9SignalRSuperNetCore client\r\n// (its jittered reconnect policy and connection states), every failure surfacing as a G9SyncException with the server's\r\n// stable code. Lynx hosts build the client with the native WebSocket of @g9tm/signalr-supernetcore-lynx\r\n// (`createLynxSyncTransport` in @g9tm/syncdata-lynx does).\r\nimport { G9Client, type G9ClientOptions } from '@g9tm/signalr-supernetcore-client';\r\nimport { HubConnectionState, Subject, type IHubProtocol, type IStreamResult, type ISubscription } from '@microsoft/signalr';\r\nimport { G9SyncErrorCodes, G9SyncException, isSyncException } from '../abstractions/errors.js';\r\nimport { createAbortController, onAbort } from '../internal/abort.js';\r\nimport type { G9SyncTransport } from '../client/transport.js';\r\nimport type {\r\n G9DtChangesAvailable,\r\n G9DtFetchRowsRequest,\r\n G9DtHelloRequest,\r\n G9DtHelloResult,\r\n G9DtPullContinuation,\r\n G9DtPullPlan,\r\n G9DtPullRequest,\r\n G9DtPushRequest,\r\n G9DtPushResult,\r\n G9DtReconcileRequest,\r\n G9DtSyncChunk,\r\n} from '../protocol/dtos.js';\r\nimport { G9SyncHubErrors, G9SyncProtocol } from '../protocol/protocol.js';\r\nimport {\r\n fromMessagePackNames,\r\n readChangesAvailable,\r\n readHelloResult,\r\n readPullPlan,\r\n readPushResult,\r\n readSyncChunk,\r\n toMessagePackNames,\r\n type G9WireFormat,\r\n} from '../protocol/wire.js';\r\nimport { createSyncMessagePackProtocol, G9SyncJsonHubProtocol } from './protocols.js';\r\n\r\n/** The SignalR hub protocol a client speaks (`G9ESyncHubProtocol`). The server accepts both. */\r\nexport const G9ESyncHubProtocol = {\r\n /** Text frames; payloads base64-encoded (+33%). Readable in traces. */\r\n Json: 0,\r\n /** Binary frames: payloads as raw bytes. The default. */\r\n MessagePack: 1,\r\n} as const;\r\nexport type G9ESyncHubProtocol = (typeof G9ESyncHubProtocol)[keyof typeof G9ESyncHubProtocol];\r\n\r\n/** Options of {@link G9SignalRSyncTransport} (`G9CSignalRSyncTransportOptions`). Durations are milliseconds. */\r\nexport interface G9SignalRSyncTransportOptions {\r\n /** Absolute hub URL, e.g. `https://sync.example.com/g9sync`. */\r\n url: string;\r\n /** Hub protocol; MessagePack by default. */\r\n protocol?: G9ESyncHubProtocol;\r\n /** Supplies the bearer token for every (re)connect. */\r\n accessTokenFactory?: () => string | Promise<string>;\r\n /** SignalR stateful reconnect (ADR-0069); off by default, the server must allow it too. */\r\n statefulReconnect?: boolean;\r\n /** How long one connect may take before it counts as a network failure. */\r\n connectTimeoutMs?: number;\r\n serverTimeoutMs?: number;\r\n keepAliveIntervalMs?: number;\r\n /**\r\n * How long a Hello waits for its answer before it is a retryable G9S-NET-FAILURE (ADR-0074). Default 60000; 0 = no limit.\r\n * A live connection does not guarantee an answer: pings keep it up while the call's message was lost on the way.\r\n */\r\n callTimeoutMs?: number;\r\n /**\r\n * How long a PlanPull waits for its plan. Default 300000 (5 minutes); 0 = no limit (ADR-0074, amended in 1.5.1): a site's first\r\n * plan builds key sets and plans a snapshot file - 16 s for a 450,000-row farm on a desktop, several times that on a busy server.\r\n */\r\n planTimeoutMs?: number;\r\n /** How long a push may take from its first chunk to its result; a safety net, not a pace. Default 600000; 0 = no limit. */\r\n pushTimeoutMs?: number;\r\n /**\r\n * How long a server stream (Pull, Reconcile, FetchRows) may go without a chunk before it is a retryable G9S-NET-FAILURE; the\r\n * engine resumes from its last continuation. Default 120000 (a snapshot file may still be building); 0 = no limit (ADR-0074).\r\n */\r\n streamIdleTimeoutMs?: number;\r\n /**\r\n * Everything else the G9 client takes (transport, headers, WebSocket constructor, skipNegotiation, reconnect policy, log\r\n * level…) — the twin of ConfigureHttpConnection/ConfigureBuilder. `url`, `protocol` and the timeouts above win.\r\n */\r\n client?: Partial<G9ClientOptions>;\r\n /** Builds the client (tests, hosts with their own wiring); `new G9Client(options)` by default. */\r\n createClient?: (options: G9ClientOptions) => G9Client;\r\n}\r\n\r\n/** The SignalR implementation of {@link G9SyncTransport}. */\r\nexport class G9SignalRSyncTransport implements G9SyncTransport {\r\n readonly client: G9Client;\r\n readonly format: G9WireFormat;\r\n private readonly _options: G9SignalRSyncTransportOptions;\r\n private readonly _handlers: ((changes: G9DtChangesAvailable) => void)[] = [];\r\n private _connecting: Promise<void> | null = null;\r\n\r\n constructor(options: G9SignalRSyncTransportOptions) {\r\n if (!options || typeof options.url !== 'string' || options.url.trim() === '') throw new TypeError('A hub URL is required.');\r\n this._options = options;\r\n const json = options.protocol === G9ESyncHubProtocol.Json;\r\n this.format = json ? 'json' : 'messagepack';\r\n const protocol: IHubProtocol = json ? new G9SyncJsonHubProtocol() : createSyncMessagePackProtocol();\r\n const clientOptions: G9ClientOptions = {\r\n ...(options.client ?? {}),\r\n url: options.url,\r\n protocol,\r\n statefulReconnect: options.statefulReconnect ?? false,\r\n serverTimeoutMs: options.serverTimeoutMs ?? 30000,\r\n keepAliveIntervalMs: options.keepAliveIntervalMs ?? 10000,\r\n };\r\n if (options.accessTokenFactory) clientOptions.accessTokenFactory = options.accessTokenFactory;\r\n this.client = options.createClient ? options.createClient(clientOptions) : new G9Client(clientOptions);\r\n this.client.on(G9SyncProtocol.Listeners.OnChangesAvailable, (payload: unknown) => this._onChanges(payload));\r\n }\r\n\r\n get isConnected(): boolean {\r\n return this.client.connection.state === HubConnectionState.Connected;\r\n }\r\n\r\n onChangesAvailable(handler: (changes: G9DtChangesAvailable) => void): () => void {\r\n this._handlers.push(handler);\r\n return () => {\r\n const index = this._handlers.indexOf(handler);\r\n if (index >= 0) this._handlers.splice(index, 1);\r\n };\r\n }\r\n\r\n /** Connects (joins a connect in progress); a connect that takes longer than connectTimeoutMs is a network failure. */\r\n connect(signal?: AbortSignal): Promise<void> {\r\n if (this.isConnected) return Promise.resolve();\r\n if (!this._connecting) {\r\n const connecting = this._connectCore().finally(() => {\r\n if (this._connecting === connecting) this._connecting = null;\r\n });\r\n this._connecting = connecting;\r\n }\r\n const connecting = this._connecting;\r\n if (!signal) return connecting;\r\n return new Promise<void>((resolve, reject) => {\r\n const unsubscribe = onAbort(signal, () => reject(new G9SyncException(G9SyncErrorCodes.Canceled, 'The connect was canceled.')));\r\n connecting.then(\r\n () => {\r\n unsubscribe();\r\n resolve();\r\n },\r\n (error) => {\r\n unsubscribe();\r\n reject(error);\r\n },\r\n );\r\n });\r\n }\r\n\r\n private async _connectCore(): Promise<void> {\r\n const timeout = createAbortController();\r\n const timer = setTimeout(() => timeout.abort(), this._options.connectTimeoutMs ?? 20000);\r\n try {\r\n await this.client.start(timeout.signal);\r\n } catch (error) {\r\n const mapped = mapError(error, false) ?? new G9SyncException(G9SyncErrorCodes.NetworkFailure, messageOf(error), { retryable: true, cause: error });\r\n throw mapped.code === G9SyncErrorCodes.Canceled\r\n ? new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync connection to ${this._options.url} did not open in time.`, { retryable: true, cause: error })\r\n : mapped;\r\n } finally {\r\n clearTimeout(timer);\r\n }\r\n if (!this.isConnected) {\r\n throw new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync connection is ${String(this.client.connection.state)}.`, { retryable: true });\r\n }\r\n }\r\n\r\n hello(request: G9DtHelloRequest, signal?: AbortSignal): Promise<G9DtHelloResult> {\r\n return this._invoke(G9SyncProtocol.Methods.Hello, [this._wire(request)], readHelloResult, this._callTimeoutMs(), signal);\r\n }\r\n\r\n push(request: G9DtPushRequest, chunks: AsyncIterable<G9DtSyncChunk>, signal?: AbortSignal): Promise<G9DtPushResult> {\r\n const subject = new Subject<unknown>();\r\n const result = this._invoke(G9SyncProtocol.Methods.Push, [this._wire(request), subject], readPushResult, this._options.pushTimeoutMs ?? 600000, signal);\r\n void (async () => {\r\n try {\r\n for await (const chunk of chunks) subject.next(this._wire(chunk));\r\n subject.complete();\r\n } catch (error) {\r\n subject.error(error);\r\n }\r\n })();\r\n return result;\r\n }\r\n\r\n planPull(request: G9DtPullRequest, signal?: AbortSignal): Promise<G9DtPullPlan> {\r\n return this._invoke(G9SyncProtocol.Methods.PlanPull, [this._wire(request)], readPullPlan, this._options.planTimeoutMs ?? 300000, signal);\r\n }\r\n\r\n pull(token: string, continuation: G9DtPullContinuation | null, signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n return this._stream(G9SyncProtocol.Methods.Pull, [token, continuation === null ? null : this._wire(continuation)], signal);\r\n }\r\n\r\n reconcile(request: G9DtReconcileRequest, signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n return this._stream(G9SyncProtocol.Methods.Reconcile, [this._wire(request)], signal);\r\n }\r\n\r\n fetchRows(request: G9DtFetchRowsRequest, signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n return this._stream(G9SyncProtocol.Methods.FetchRows, [this._wire(request)], signal);\r\n }\r\n\r\n async dispose(): Promise<void> {\r\n this._handlers.length = 0;\r\n await this.client.stop();\r\n }\r\n\r\n // ================================================================ plumbing\r\n\r\n private _wire(value: unknown): unknown {\r\n return this.format === 'messagepack' ? toMessagePackNames(value) : value;\r\n }\r\n\r\n private _read(value: unknown): unknown {\r\n return this.format === 'messagepack' ? fromMessagePackNames(value) : value;\r\n }\r\n\r\n private _callTimeoutMs(): number {\r\n return this._options.callTimeoutMs ?? 60000;\r\n }\r\n\r\n private async _invoke<T>(method: string, args: unknown[], read: (value: unknown) => T, timeoutMs: number, signal?: AbortSignal): Promise<T> {\r\n if (signal && signal.aborted) throw new G9SyncException(G9SyncErrorCodes.Canceled, 'The call was canceled.');\r\n let unsubscribe: () => void = () => undefined;\r\n let timer: ReturnType<typeof setTimeout> | null = null;\r\n let timedOut = false;\r\n try {\r\n const call = this.client.connection.invoke(method, ...args);\r\n const raw =\r\n signal || timeoutMs > 0\r\n ? await new Promise<unknown>((resolve, reject) => {\r\n if (signal) unsubscribe = onAbort(signal, () => reject(new G9SyncException(G9SyncErrorCodes.Canceled, 'The call was canceled.')));\r\n if (timeoutMs > 0) {\r\n timer = setTimeout(() => {\r\n timedOut = true;\r\n reject(\r\n new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync server did not answer ${method} within ${Math.round(timeoutMs / 1000)} s.`, {\r\n retryable: true,\r\n }),\r\n );\r\n }, timeoutMs);\r\n }\r\n call.then(resolve, reject);\r\n })\r\n : await call;\r\n return read(this._read(raw));\r\n } catch (error) {\r\n // ADR-0074: the answer did not come in time. The connection is not trusted any more (the server runs one invocation per\r\n // connection at a time, and whatever lost this answer may lose the next): drop it, so the retry runs on a fresh one.\r\n if (timedOut) await this._dropConnection();\r\n throw mapError(error, signal ? signal.aborted : false) ?? error;\r\n } finally {\r\n if (timer !== null) clearTimeout(timer);\r\n unsubscribe();\r\n }\r\n }\r\n\r\n /** Ends the connection after a call or stream ran out of time (ADR-0074); the next call connects afresh. Never throws. */\r\n private async _dropConnection(): Promise<void> {\r\n try {\r\n await this.client.connection.stop();\r\n } catch {\r\n // Stopping a connection that is already gone may throw; the next call connects afresh either way.\r\n }\r\n }\r\n\r\n private _stream(method: string, args: unknown[], signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n // eslint-disable-next-line @typescript-eslint/no-this-alias\r\n const self = this;\r\n return {\r\n [Symbol.asyncIterator](): AsyncIterator<G9DtSyncChunk> {\r\n const queue: G9DtSyncChunk[] = [];\r\n let done = false;\r\n let failure: unknown = null;\r\n let wake: (() => void) | null = null;\r\n let subscription: ISubscription<unknown> | null = null;\r\n const notify = (): void => {\r\n const w = wake;\r\n wake = null;\r\n if (w) w();\r\n };\r\n let unsubscribeAbort: () => void = () => undefined;\r\n // ADR-0074: the idle limit runs only while the iterator waits for the next chunk, never while the caller processes one.\r\n const idleMs = self._options.streamIdleTimeoutMs ?? 120000;\r\n let idleTimer: ReturnType<typeof setTimeout> | null = null;\r\n let idleExpired = false;\r\n const stopIdle = (): void => {\r\n if (idleTimer !== null) clearTimeout(idleTimer);\r\n idleTimer = null;\r\n };\r\n const armIdle = (): void => {\r\n stopIdle();\r\n if (idleMs <= 0 || done) return;\r\n idleTimer = setTimeout(() => {\r\n idleTimer = null;\r\n if (done) return;\r\n idleExpired = true;\r\n failure = new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync server sent nothing on ${method} for ${Math.round(idleMs / 1000)} s.`, {\r\n retryable: true,\r\n });\r\n done = true;\r\n subscription?.dispose();\r\n notify();\r\n }, idleMs);\r\n };\r\n const start = (): void => {\r\n if (subscription || done) return;\r\n let stream: IStreamResult<unknown>;\r\n try {\r\n stream = self.client.connection.stream(method, ...args);\r\n } catch (error) {\r\n failure = error;\r\n done = true;\r\n return;\r\n }\r\n subscription = stream.subscribe({\r\n next: (item) => {\r\n queue.push(readSyncChunk(self._read(item)));\r\n notify();\r\n },\r\n error: (error) => {\r\n failure = error;\r\n done = true;\r\n notify();\r\n },\r\n complete: () => {\r\n done = true;\r\n notify();\r\n },\r\n });\r\n unsubscribeAbort = onAbort(signal, () => {\r\n failure = new G9SyncException(G9SyncErrorCodes.Canceled, 'The stream was canceled.');\r\n done = true;\r\n subscription?.dispose();\r\n notify();\r\n });\r\n };\r\n const finish = (): void => {\r\n stopIdle();\r\n unsubscribeAbort();\r\n if (subscription && !done) subscription.dispose();\r\n done = true;\r\n };\r\n return {\r\n async next(): Promise<IteratorResult<G9DtSyncChunk>> {\r\n start();\r\n for (;;) {\r\n const item = queue.shift();\r\n if (item) {\r\n stopIdle();\r\n return { value: item, done: false };\r\n }\r\n if (done) {\r\n stopIdle();\r\n unsubscribeAbort();\r\n if (idleExpired) await self._dropConnection();\r\n if (failure !== null) throw mapError(failure, signal ? signal.aborted : false) ?? failure;\r\n return { value: undefined as unknown as G9DtSyncChunk, done: true };\r\n }\r\n if (idleTimer === null) armIdle();\r\n await new Promise<void>((resolve) => {\r\n wake = resolve;\r\n });\r\n }\r\n },\r\n async return(): Promise<IteratorResult<G9DtSyncChunk>> {\r\n finish();\r\n return { value: undefined as unknown as G9DtSyncChunk, done: true };\r\n },\r\n };\r\n },\r\n };\r\n }\r\n\r\n private _onChanges(payload: unknown): void {\r\n const changes = readChangesAvailable(this._read(payload));\r\n for (const handler of this._handlers.slice()) {\r\n try {\r\n handler(changes);\r\n } catch {\r\n // A ChangesAvailable handler threw (logged by the .NET transport); it must not break the connection.\r\n }\r\n }\r\n }\r\n}\r\n\r\nfunction messageOf(error: unknown): string {\r\n return error instanceof Error ? error.message : String(error);\r\n}\r\n\r\n/**\r\n * The sync exception for a transport failure (`Map`): the server's `[G9S|code|retryable]` marker wins; any other hub\r\n * error is a retryable server error; 401/403 is Unauthorized; the rest is a retryable network failure. Null leaves the\r\n * error as it is (an already mapped one).\r\n */\r\nexport function mapError(error: unknown, canceled: boolean): G9SyncException | null {\r\n if (isSyncException(error)) return null;\r\n if (canceled) return new G9SyncException(G9SyncErrorCodes.Canceled, 'The call was canceled.', { cause: error });\r\n const message = messageOf(error);\r\n const marked = G9SyncHubErrors.tryParse(message);\r\n if (marked) return marked;\r\n const status = (error as { statusCode?: unknown }).statusCode;\r\n if (status === 401 || status === 403) return new G9SyncException(G9SyncErrorCodes.Unauthorized, 'The sync server refused the credentials.', { cause: error });\r\n if (error instanceof Error && error.name === 'AbortError') return new G9SyncException(G9SyncErrorCodes.Canceled, message, { cause: error });\r\n // A completion error from the hub: SignalR words it \"An unexpected error occurred invoking '…' on the server.\"\r\n if (/on the server|HubException/i.test(message)) return new G9SyncException(G9SyncErrorCodes.ServerError, message, { retryable: true, cause: error });\r\n return new G9SyncException(G9SyncErrorCodes.NetworkFailure, message, { retryable: true, cause: error });\r\n}\r\n"]}
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../../src/signalr/transport.ts"],"names":[],"mappings":"AAAA,yHAAyH;AACzH,wHAAwH;AACxH,wGAAwG;AACxG,2DAA2D;AAC3D,OAAO,EAAE,QAAQ,EAAwB,MAAM,mCAAmC,CAAC;AACnF,OAAO,EAAE,kBAAkB,EAAE,OAAO,EAA6D,MAAM,oBAAoB,CAAC;AAC5H,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC/F,OAAO,EAAE,qBAAqB,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAetE,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC1E,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EACf,YAAY,EACZ,cAAc,EACd,aAAa,E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,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;AAC1G,CAAC","sourcesContent":["// Twin of G9SyncData.Client.SignalR/G9CSignalRSyncTransport.cs: the sync transport over the G9SignalRSuperNetCore client\r\n// (its jittered reconnect policy and connection states), every failure surfacing as a G9SyncException with the server's\r\n// stable code. Lynx hosts build the client with the native WebSocket of @g9tm/signalr-supernetcore-lynx\r\n// (`createLynxSyncTransport` in @g9tm/syncdata-lynx does).\r\nimport { G9Client, type G9ClientOptions } from '@g9tm/signalr-supernetcore-client';\r\nimport { HubConnectionState, Subject, type IHubProtocol, type IStreamResult, type ISubscription } from '@microsoft/signalr';\r\nimport { G9SyncErrorCodes, G9SyncException, isSyncException } from '../abstractions/errors.js';\r\nimport { createAbortController, onAbort } from '../internal/abort.js';\r\nimport type { G9SyncTransport } from '../client/transport.js';\r\nimport type {\r\n G9DtChangesAvailable,\r\n G9DtFetchRowsRequest,\r\n G9DtHelloRequest,\r\n G9DtHelloResult,\r\n G9DtPullContinuation,\r\n G9DtPullPlan,\r\n G9DtPullRequest,\r\n G9DtPushRequest,\r\n G9DtPushResult,\r\n G9DtReconcileRequest,\r\n G9DtSyncChunk,\r\n} from '../protocol/dtos.js';\r\nimport { G9SyncHubErrors, G9SyncProtocol } from '../protocol/protocol.js';\r\nimport {\r\n fromMessagePackNames,\r\n readChangesAvailable,\r\n readHelloResult,\r\n readPullPlan,\r\n readPushResult,\r\n readSyncChunk,\r\n toMessagePackNames,\r\n type G9WireFormat,\r\n} from '../protocol/wire.js';\r\nimport { createSyncMessagePackProtocol, G9SyncJsonHubProtocol } from './protocols.js';\r\n\r\n/** The SignalR hub protocol a client speaks (`G9ESyncHubProtocol`). The server accepts both. */\r\nexport const G9ESyncHubProtocol = {\r\n /** Text frames; payloads base64-encoded (+33%). Readable in traces. */\r\n Json: 0,\r\n /** Binary frames: payloads as raw bytes. The default. */\r\n MessagePack: 1,\r\n} as const;\r\nexport type G9ESyncHubProtocol = (typeof G9ESyncHubProtocol)[keyof typeof G9ESyncHubProtocol];\r\n\r\n/** Options of {@link G9SignalRSyncTransport} (`G9CSignalRSyncTransportOptions`). Durations are milliseconds. */\r\nexport interface G9SignalRSyncTransportOptions {\r\n /** Absolute hub URL, e.g. `https://sync.example.com/g9sync`. */\r\n url: string;\r\n /** Hub protocol; MessagePack by default. */\r\n protocol?: G9ESyncHubProtocol;\r\n /** Supplies the bearer token for every (re)connect. */\r\n accessTokenFactory?: () => string | Promise<string>;\r\n /** SignalR stateful reconnect (ADR-0069); off by default, the server must allow it too. */\r\n statefulReconnect?: boolean;\r\n /** How long one connect may take before it counts as a network failure. */\r\n connectTimeoutMs?: number;\r\n serverTimeoutMs?: number;\r\n keepAliveIntervalMs?: number;\r\n /**\r\n * How long a Hello waits for its answer before it is a retryable G9S-NET-FAILURE (ADR-0074). Default 60000; 0 = no limit.\r\n * A live connection does not guarantee an answer: pings keep it up while the call's message was lost on the way.\r\n */\r\n callTimeoutMs?: number;\r\n /**\r\n * How long a PlanPull waits for its plan. Default 300000 (5 minutes); 0 = no limit (ADR-0074, amended in 1.5.1): a site's first\r\n * plan builds key sets and plans a snapshot file - 16 s for a 450,000-row farm on a desktop, several times that on a busy server.\r\n */\r\n planTimeoutMs?: number;\r\n /** How long a push may take from its first chunk to its result; a safety net, not a pace. Default 600000; 0 = no limit. */\r\n pushTimeoutMs?: number;\r\n /**\r\n * How long a server stream (Pull, Reconcile, FetchRows) may go without a chunk before it is a retryable G9S-NET-FAILURE; the\r\n * engine resumes from its last continuation. Default 120000 (a snapshot file may still be building); 0 = no limit (ADR-0074).\r\n */\r\n streamIdleTimeoutMs?: number;\r\n /**\r\n * Everything else the G9 client takes (transport, headers, WebSocket constructor, skipNegotiation, reconnect policy, log\r\n * level…) — the twin of ConfigureHttpConnection/ConfigureBuilder. `url`, `protocol` and the timeouts above win.\r\n */\r\n client?: Partial<G9ClientOptions>;\r\n /** Builds the client (tests, hosts with their own wiring); `new G9Client(options)` by default. */\r\n createClient?: (options: G9ClientOptions) => G9Client;\r\n}\r\n\r\n/** The SignalR implementation of {@link G9SyncTransport}. */\r\nexport class G9SignalRSyncTransport implements G9SyncTransport {\r\n readonly client: G9Client;\r\n readonly format: G9WireFormat;\r\n private readonly _options: G9SignalRSyncTransportOptions;\r\n private readonly _handlers: ((changes: G9DtChangesAvailable) => void)[] = [];\r\n private _connecting: Promise<void> | null = null;\r\n private _lastHello: G9DtHelloRequest | null = null;\r\n\r\n constructor(options: G9SignalRSyncTransportOptions) {\r\n if (!options || typeof options.url !== 'string' || options.url.trim() === '') throw new TypeError('A hub URL is required.');\r\n this._options = options;\r\n const json = options.protocol === G9ESyncHubProtocol.Json;\r\n this.format = json ? 'json' : 'messagepack';\r\n const protocol: IHubProtocol = json ? new G9SyncJsonHubProtocol() : createSyncMessagePackProtocol();\r\n const clientOptions: G9ClientOptions = {\r\n ...(options.client ?? {}),\r\n url: options.url,\r\n protocol,\r\n statefulReconnect: options.statefulReconnect ?? false,\r\n serverTimeoutMs: options.serverTimeoutMs ?? 30000,\r\n keepAliveIntervalMs: options.keepAliveIntervalMs ?? 10000,\r\n };\r\n if (options.accessTokenFactory) clientOptions.accessTokenFactory = options.accessTokenFactory;\r\n this.client = options.createClient ? options.createClient(clientOptions) : new G9Client(clientOptions);\r\n this.client.on(G9SyncProtocol.Listeners.OnChangesAvailable, (payload: unknown) => this._onChanges(payload));\r\n // Twin of .NET OnReconnectedAsync (S-2, LES-0087): a connection that reconnected by itself is a new connection id in no table group.\r\n const connection = this.client.connection as { onreconnected?: (callback: (connectionId?: string) => void) => void };\r\n if (typeof connection.onreconnected === 'function') connection.onreconnected(() => this._rejoin());\r\n }\r\n\r\n get isConnected(): boolean {\r\n return this.client.connection.state === HubConnectionState.Connected;\r\n }\r\n\r\n onChangesAvailable(handler: (changes: G9DtChangesAvailable) => void): () => void {\r\n this._handlers.push(handler);\r\n return () => {\r\n const index = this._handlers.indexOf(handler);\r\n if (index >= 0) this._handlers.splice(index, 1);\r\n };\r\n }\r\n\r\n /** Connects (joins a connect in progress); a connect that takes longer than connectTimeoutMs is a network failure. */\r\n connect(signal?: AbortSignal): Promise<void> {\r\n if (this.isConnected) return Promise.resolve();\r\n if (!this._connecting) {\r\n const connecting = this._connectCore().finally(() => {\r\n if (this._connecting === connecting) this._connecting = null;\r\n });\r\n this._connecting = connecting;\r\n }\r\n const connecting = this._connecting;\r\n if (!signal) return connecting;\r\n return new Promise<void>((resolve, reject) => {\r\n const unsubscribe = onAbort(signal, () => reject(new G9SyncException(G9SyncErrorCodes.Canceled, 'The connect was canceled.')));\r\n connecting.then(\r\n () => {\r\n unsubscribe();\r\n resolve();\r\n },\r\n (error) => {\r\n unsubscribe();\r\n reject(error);\r\n },\r\n );\r\n });\r\n }\r\n\r\n private async _connectCore(): Promise<void> {\r\n const timeout = createAbortController();\r\n const timer = setTimeout(() => timeout.abort(), this._options.connectTimeoutMs ?? 20000);\r\n try {\r\n await this.client.start(timeout.signal);\r\n } catch (error) {\r\n const mapped = mapError(error, false) ?? new G9SyncException(G9SyncErrorCodes.NetworkFailure, messageOf(error), { retryable: true, cause: error });\r\n throw mapped.code === G9SyncErrorCodes.Canceled\r\n ? new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync connection to ${this._options.url} did not open in time.`, { retryable: true, cause: error })\r\n : mapped;\r\n } finally {\r\n clearTimeout(timer);\r\n }\r\n if (!this.isConnected) {\r\n throw new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync connection is ${String(this.client.connection.state)}.`, { retryable: true });\r\n }\r\n }\r\n\r\n async hello(request: G9DtHelloRequest, signal?: AbortSignal): Promise<G9DtHelloResult> {\r\n const result = await this._invoke(G9SyncProtocol.Methods.Hello, [this._wire(request)], readHelloResult, this._callTimeoutMs(), signal);\r\n if (!result.error) this._lastHello = request;\r\n return result;\r\n }\r\n\r\n /**\r\n * After an automatic reconnect the last accepted Hello is sent again, so the new connection joins the table groups pokes go to\r\n * (S-2, LES-0087). Fire and forget, bounded by the call timeout; the answer is not used and a failure only delays a hint.\r\n */\r\n private _rejoin(): void {\r\n const hello = this._lastHello;\r\n if (!hello) return;\r\n this._invoke(G9SyncProtocol.Methods.Hello, [this._wire(hello)], readHelloResult, this._callTimeoutMs()).catch(() => undefined);\r\n }\r\n\r\n push(request: G9DtPushRequest, chunks: AsyncIterable<G9DtSyncChunk>, signal?: AbortSignal): Promise<G9DtPushResult> {\r\n const subject = new Subject<unknown>();\r\n const result = this._invoke(G9SyncProtocol.Methods.Push, [this._wire(request), subject], readPushResult, this._options.pushTimeoutMs ?? 600000, signal);\r\n void (async () => {\r\n try {\r\n for await (const chunk of chunks) subject.next(this._wire(chunk));\r\n subject.complete();\r\n } catch (error) {\r\n subject.error(error);\r\n }\r\n })();\r\n return result;\r\n }\r\n\r\n planPull(request: G9DtPullRequest, signal?: AbortSignal): Promise<G9DtPullPlan> {\r\n return this._invoke(G9SyncProtocol.Methods.PlanPull, [this._wire(request)], readPullPlan, this._options.planTimeoutMs ?? 300000, signal);\r\n }\r\n\r\n pull(token: string, continuation: G9DtPullContinuation | null, signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n return this._stream(G9SyncProtocol.Methods.Pull, [token, continuation === null ? null : this._wire(continuation)], signal);\r\n }\r\n\r\n reconcile(request: G9DtReconcileRequest, signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n return this._stream(G9SyncProtocol.Methods.Reconcile, [this._wire(request)], signal);\r\n }\r\n\r\n fetchRows(request: G9DtFetchRowsRequest, signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n return this._stream(G9SyncProtocol.Methods.FetchRows, [this._wire(request)], signal);\r\n }\r\n\r\n async dispose(): Promise<void> {\r\n this._handlers.length = 0;\r\n await this.client.stop();\r\n }\r\n\r\n // ================================================================ plumbing\r\n\r\n private _wire(value: unknown): unknown {\r\n return this.format === 'messagepack' ? toMessagePackNames(value) : value;\r\n }\r\n\r\n private _read(value: unknown): unknown {\r\n return this.format === 'messagepack' ? fromMessagePackNames(value) : value;\r\n }\r\n\r\n private _callTimeoutMs(): number {\r\n return this._options.callTimeoutMs ?? 60000;\r\n }\r\n\r\n private async _invoke<T>(method: string, args: unknown[], read: (value: unknown) => T, timeoutMs: number, signal?: AbortSignal): Promise<T> {\r\n if (signal && signal.aborted) throw new G9SyncException(G9SyncErrorCodes.Canceled, 'The call was canceled.');\r\n let unsubscribe: () => void = () => undefined;\r\n let timer: ReturnType<typeof setTimeout> | null = null;\r\n let timedOut = false;\r\n try {\r\n const call = this.client.connection.invoke(method, ...args);\r\n const raw =\r\n signal || timeoutMs > 0\r\n ? await new Promise<unknown>((resolve, reject) => {\r\n if (signal) unsubscribe = onAbort(signal, () => reject(new G9SyncException(G9SyncErrorCodes.Canceled, 'The call was canceled.')));\r\n if (timeoutMs > 0) {\r\n timer = setTimeout(() => {\r\n timedOut = true;\r\n reject(\r\n new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync server did not answer ${method} within ${Math.round(timeoutMs / 1000)} s.`, {\r\n retryable: true,\r\n }),\r\n );\r\n }, timeoutMs);\r\n }\r\n call.then(resolve, reject);\r\n })\r\n : await call;\r\n return read(this._read(raw));\r\n } catch (error) {\r\n // ADR-0074: the answer did not come in time. The connection is not trusted any more (the server runs one invocation per\r\n // connection at a time, and whatever lost this answer may lose the next): drop it, so the retry runs on a fresh one.\r\n if (timedOut) await this._dropConnection();\r\n throw mapError(error, signal ? signal.aborted : false) ?? error;\r\n } finally {\r\n if (timer !== null) clearTimeout(timer);\r\n unsubscribe();\r\n }\r\n }\r\n\r\n /** Ends the connection after a call or stream ran out of time (ADR-0074); the next call connects afresh. Never throws. */\r\n private async _dropConnection(): Promise<void> {\r\n try {\r\n await this.client.connection.stop();\r\n } catch {\r\n // Stopping a connection that is already gone may throw; the next call connects afresh either way.\r\n }\r\n }\r\n\r\n private _stream(method: string, args: unknown[], signal?: AbortSignal): AsyncIterable<G9DtSyncChunk> {\r\n // eslint-disable-next-line @typescript-eslint/no-this-alias\r\n const self = this;\r\n return {\r\n [Symbol.asyncIterator](): AsyncIterator<G9DtSyncChunk> {\r\n const queue: G9DtSyncChunk[] = [];\r\n let done = false;\r\n let failure: unknown = null;\r\n let wake: (() => void) | null = null;\r\n let subscription: ISubscription<unknown> | null = null;\r\n const notify = (): void => {\r\n const w = wake;\r\n wake = null;\r\n if (w) w();\r\n };\r\n let unsubscribeAbort: () => void = () => undefined;\r\n // ADR-0074: the idle limit runs only while the iterator waits for the next chunk, never while the caller processes one.\r\n const idleMs = self._options.streamIdleTimeoutMs ?? 120000;\r\n let idleTimer: ReturnType<typeof setTimeout> | null = null;\r\n let idleExpired = false;\r\n const stopIdle = (): void => {\r\n if (idleTimer !== null) clearTimeout(idleTimer);\r\n idleTimer = null;\r\n };\r\n const armIdle = (): void => {\r\n stopIdle();\r\n if (idleMs <= 0 || done) return;\r\n idleTimer = setTimeout(() => {\r\n idleTimer = null;\r\n if (done) return;\r\n idleExpired = true;\r\n failure = new G9SyncException(G9SyncErrorCodes.NetworkFailure, `The sync server sent nothing on ${method} for ${Math.round(idleMs / 1000)} s.`, {\r\n retryable: true,\r\n });\r\n done = true;\r\n subscription?.dispose();\r\n notify();\r\n }, idleMs);\r\n };\r\n const start = (): void => {\r\n if (subscription || done) return;\r\n let stream: IStreamResult<unknown>;\r\n try {\r\n stream = self.client.connection.stream(method, ...args);\r\n } catch (error) {\r\n failure = error;\r\n done = true;\r\n return;\r\n }\r\n subscription = stream.subscribe({\r\n next: (item) => {\r\n queue.push(readSyncChunk(self._read(item)));\r\n notify();\r\n },\r\n error: (error) => {\r\n failure = error;\r\n done = true;\r\n notify();\r\n },\r\n complete: () => {\r\n done = true;\r\n notify();\r\n },\r\n });\r\n unsubscribeAbort = onAbort(signal, () => {\r\n failure = new G9SyncException(G9SyncErrorCodes.Canceled, 'The stream was canceled.');\r\n done = true;\r\n subscription?.dispose();\r\n notify();\r\n });\r\n };\r\n const finish = (): void => {\r\n stopIdle();\r\n unsubscribeAbort();\r\n if (subscription && !done) subscription.dispose();\r\n done = true;\r\n };\r\n return {\r\n async next(): Promise<IteratorResult<G9DtSyncChunk>> {\r\n start();\r\n for (;;) {\r\n const item = queue.shift();\r\n if (item) {\r\n stopIdle();\r\n return { value: item, done: false };\r\n }\r\n if (done) {\r\n stopIdle();\r\n unsubscribeAbort();\r\n if (idleExpired) await self._dropConnection();\r\n if (failure !== null) throw mapError(failure, signal ? signal.aborted : false) ?? failure;\r\n return { value: undefined as unknown as G9DtSyncChunk, done: true };\r\n }\r\n if (idleTimer === null) armIdle();\r\n await new Promise<void>((resolve) => {\r\n wake = resolve;\r\n });\r\n }\r\n },\r\n async return(): Promise<IteratorResult<G9DtSyncChunk>> {\r\n finish();\r\n return { value: undefined as unknown as G9DtSyncChunk, done: true };\r\n },\r\n };\r\n },\r\n };\r\n }\r\n\r\n private _onChanges(payload: unknown): void {\r\n const changes = readChangesAvailable(this._read(payload));\r\n for (const handler of this._handlers.slice()) {\r\n try {\r\n handler(changes);\r\n } catch {\r\n // A ChangesAvailable handler threw (logged by the .NET transport); it must not break the connection.\r\n }\r\n }\r\n }\r\n}\r\n\r\nfunction messageOf(error: unknown): string {\r\n return error instanceof Error ? error.message : String(error);\r\n}\r\n\r\n/**\r\n * The sync exception for a transport failure (`Map`): the server's `[G9S|code|retryable]` marker wins; any other hub\r\n * error is a retryable server error; 401/403 is Unauthorized; the rest is a retryable network failure. Null leaves the\r\n * error as it is (an already mapped one).\r\n */\r\nexport function mapError(error: unknown, canceled: boolean): G9SyncException | null {\r\n if (isSyncException(error)) return null;\r\n if (canceled) return new G9SyncException(G9SyncErrorCodes.Canceled, 'The call was canceled.', { cause: error });\r\n const message = messageOf(error);\r\n const marked = G9SyncHubErrors.tryParse(message);\r\n if (marked) return marked;\r\n const status = (error as { statusCode?: unknown }).statusCode;\r\n if (status === 401 || status === 403) return new G9SyncException(G9SyncErrorCodes.Unauthorized, 'The sync server refused the credentials.', { cause: error });\r\n if (error instanceof Error && error.name === 'AbortError') return new G9SyncException(G9SyncErrorCodes.Canceled, message, { cause: error });\r\n // A completion error from the hub: SignalR words it \"An unexpected error occurred invoking '…' on the server.\"\r\n if (/on the server|HubException/i.test(message)) return new G9SyncException(G9SyncErrorCodes.ServerError, message, { retryable: true, cause: error });\r\n return new G9SyncException(G9SyncErrorCodes.NetworkFailure, message, { retryable: true, cause: error });\r\n}\r\n"]}
@@ -1,7 +1,7 @@
1
1
  // Twin of G9SyncData.Client.Sqlite/G9CSqliteSyncStore.Push.cs: dirty rows in local change order become push frames; the
2
2
  // result clears exactly the rows that were sent (rows edited meanwhile stay dirty), flags rejections and applies canonical
3
3
  // server rows.
4
- import { G9ESyncDirection } from '../abstractions/enums.js';
4
+ import { G9ESyncColumnType, G9ESyncDirection } from '../abstractions/enums.js';
5
5
  import { G9SyncErrorCodes } from '../abstractions/errors.js';
6
6
  import { G9Long } from '../abstractions/long.js';
7
7
  import { utf8Encode } from '../internal/utf8.js';
@@ -300,8 +300,12 @@ async function readLostConflicts(store, batch, result) {
300
300
  const serverDeleted = conflict.code === G9SyncErrorCodes.ConflictDeleted;
301
301
  if (dirty === 2 && serverDeleted)
302
302
  continue; // both sides deleted it: nothing was lost
303
- let lostMask = mask;
303
+ // Only what this push CARRIED can have been lost (LES-0086): a column the device changed but never sends - read-only on the
304
+ // server, or not negotiated - was not part of the conflict, and the canonical row simply replaces it.
304
305
  const names = pushOrder.get(conflict.tableId);
306
+ let lostMask = names ? mask.and(pushedMask(table, names)) : mask;
307
+ if (dirty === 1 && lostMask.isZero())
308
+ continue;
305
309
  if (dirty === 1 && conflict.partiallyApplied && !conflict.serverChangedMask.isNegative() && conflict.code === G9SyncErrorCodes.ConflictColumns && names) {
306
310
  // The server's bits are in push column order; the device's are model columns.
307
311
  lostMask = G9Long.ZERO;
@@ -319,6 +323,50 @@ async function readLostConflicts(store, batch, result) {
319
323
  }
320
324
  return lost;
321
325
  }
326
+ /** The device's column bits of the columns a push sent for this table (push column order → model order). Twin of .NET PushedMask. */
327
+ function pushedMask(table, pushed) {
328
+ let mask = G9Long.ZERO;
329
+ for (const name of pushed) {
330
+ const local = table.model.indexOf(name);
331
+ if (local >= 0 && local < 63)
332
+ mask = mask.or(G9Long.ONE.shiftLeft(local));
333
+ }
334
+ return mask;
335
+ }
336
+ /** Two SQLite values are the same value (a blob by its bytes); a different type counts as different. Twin of .NET SameValue. */
337
+ function sameValue(a, b, type) {
338
+ if (a instanceof Uint8Array && b instanceof Uint8Array)
339
+ return a.length === b.length && a.every((x, i) => x === b[i]);
340
+ if (typeof a === 'bigint' || typeof b === 'bigint')
341
+ return typeof a === typeof b && a === b;
342
+ if (a === b || (a == null && b == null))
343
+ return true;
344
+ // A timestamp is the same INSTANT to the microsecond (LES-0086): the device keeps 100 ns, the server microseconds.
345
+ if ((type === G9ESyncColumnType.DateTime || type === G9ESyncColumnType.DateTimeOffset) && typeof a === 'string' && typeof b === 'string') {
346
+ const first = instant(a);
347
+ const second = instant(b);
348
+ if (first && second)
349
+ return Math.abs((first[0] - second[0]) * 10000 + (first[1] - second[1])) < 10;
350
+ }
351
+ return false;
352
+ }
353
+ const instantPattern = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,7}))?(Z|[+-]\d{2}:?\d{2})?$/;
354
+ /** A timestamp text as [Unix milliseconds, 100 ns units within that millisecond]; no BigInt (Lynx). Twin of .NET Instant. */
355
+ function instant(text) {
356
+ var _a;
357
+ const m = instantPattern.exec(text.trim());
358
+ if (!m)
359
+ return null;
360
+ const fraction = (((_a = m[7]) !== null && _a !== void 0 ? _a : '') + '0000000').slice(0, 7);
361
+ let ms = Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6]), Number(fraction.slice(0, 3)));
362
+ const zone = m[8];
363
+ if (zone && zone !== 'Z') {
364
+ const sign = zone[0] === '-' ? -1 : 1;
365
+ const digits = zone.slice(1).replace(':', '');
366
+ ms -= sign * (Number(digits.slice(0, 2)) * 60 + Number(digits.slice(2, 4))) * 60000;
367
+ }
368
+ return [ms, Number(fraction.slice(3))];
369
+ }
322
370
  /** Twin of .NET KeepLostConflicts: the device's lost values back over the canonical rows, parked as rejections (LES-0072). */
323
371
  async function keepLostConflicts(store, context, lost) {
324
372
  var _a;
@@ -329,6 +377,7 @@ async function keepLostConflicts(store, context, lost) {
329
377
  const now = await readLocalValues(connection, table, item.key);
330
378
  let columnNames = null;
331
379
  let serverValues = null;
380
+ let lostMask = item.lostMask;
332
381
  if (item.dirty === 2) {
333
382
  if (Object.keys(now).length > 0)
334
383
  await connection.exec(`DELETE FROM ${table.quoted} WHERE ${table.keyQuoted} = $k`, { $k: key });
@@ -343,7 +392,16 @@ async function keepLostConflicts(store, context, lost) {
343
392
  await connection.exec(`INSERT INTO ${table.quoted} (${all.map(quote).join(', ')}) VALUES (${all.map((_, i) => '$v' + i).join(', ')})`, params);
344
393
  }
345
394
  else {
346
- const columns = table.model.columns.map((c, i) => ({ name: c.name, i })).filter((c) => item.lostMask.testBit(c.i) && c.name in item.values).map((c) => c.name);
395
+ const lostColumns = table.model.columns.map((c, i) => ({ name: c.name, i })).filter((c) => item.lostMask.testBit(c.i) && c.name in item.values);
396
+ // LES-0085: a column the server already holds with the device's own value lost nothing; when none differs, the row is in sync.
397
+ const differing = lostColumns.filter((c) => { var _a; return !sameValue(item.values[c.name], now[c.name], (_a = table.model.columns[c.i]) === null || _a === void 0 ? void 0 : _a.type); });
398
+ if (lostColumns.length > 0 && differing.length === 0) {
399
+ context.report.conflictsSameValue++;
400
+ continue;
401
+ }
402
+ if (differing.length > 0)
403
+ lostMask = differing.reduce((m, c) => m.or(G9Long.ONE.shiftLeft(c.i)), G9Long.ZERO);
404
+ const columns = differing.map((c) => c.name);
347
405
  if (columns.length > 0) {
348
406
  columnNames = columns.join(',');
349
407
  const server = {};
@@ -355,7 +413,7 @@ async function keepLostConflicts(store, context, lost) {
355
413
  await connection.exec(`UPDATE ${table.quoted} SET ${columns.map((c, i) => quote(c) + ' = $v' + i).join(', ')} WHERE ${table.keyQuoted} = $k`, params);
356
414
  }
357
415
  }
358
- const mask = item.dirty === 2 ? G9Long.ZERO : item.lostMask;
416
+ const mask = item.dirty === 2 ? G9Long.ZERO : lostMask;
359
417
  await connection.exec('INSERT INTO g9_rows (tid, pk, server_ver, local_seq, subs, dirty, dirty_mask, rejected, deleted) ' +
360
418
  'VALUES ($t, $pk, $v, (SELECT seq FROM g9_clock WHERE id = 1) + 1, $subs, $d, $m, 1, 0) ' +
361
419
  'ON CONFLICT(tid, pk) DO UPDATE SET dirty = excluded.dirty, dirty_mask = excluded.dirty_mask, rejected = 1, deleted = 0, ' +
@@ -1 +1 @@
1
- {"version":3,"file":"push.js","sourceRoot":"","sources":["../../src/sqlite/push.ts"],"names":[],"mappings":"AAAA,wHAAwH;AACxH,2HAA2H;AAC3H,eAAe;AACf,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAoB,MAAM,yBAAyB,CAAC;AAE1E,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAC7E,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE7D,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,QAAQ,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GAAG,CAAC,CAAC;IAC1G,CAAC;AACH,CAAC","sourcesContent":["// Twin of G9SyncData.Client.Sqlite/G9CSqliteSyncStore.Push.cs: dirty rows in local change order become push frames; the\r\n// result clears exactly the rows that were sent (rows edited meanwhile stay dirty), flags rejections and applies canonical\r\n// server rows.\r\nimport { G9ESyncDirection } from '../abstractions/enums.js';\r\nimport { G9SyncErrorCodes } from '../abstractions/errors.js';\r\nimport { G9Long } from '../abstractions/long.js';\r\nimport { utf8Encode } from '../internal/utf8.js';\r\nimport { G9ApplyReport, type G9PushBatch } from '../client/storeTypes.js';\r\nimport type { G9DtNegotiatedTable, G9DtPushResult, G9DtTableColumns } from '../protocol/dtos.js';\r\nimport { G9EPushRowOp, G9ERowBatchOp } from '../protocol/rowBatch/format.js';\r\nimport { G9RowBatchWriter } from '../protocol/rowBatch/writer.js';\r\nimport { applyFrame, beginApply, endApply } from './apply.js';\r\nimport { completeCommands, readPendingCommands } from './commands.js';\r\nimport { asLong, asNumber, asString } from './connection.js';\r\nimport type { G9SqlValue } from './driver.js';\r\nimport { findByServerName, getLocal, keyValue, meta, nowMs, readLocalValues, serializeValues, setMeta, type LocalTable, type StoreInternals } from './internals.js';\r\nimport { quote } from './sql.js';\r\nimport { estimateEncodedBytes, pushSelectExpression, writeLocalValue } from './values.js';\r\n\r\ninterface PushColumn {\r\n name: string;\r\n type: number;\r\n local: number;\r\n}\r\n\r\ninterface PushSection {\r\n table: LocalTable;\r\n negotiated: G9DtNegotiatedTable;\r\n columns: PushColumn[];\r\n writer: G9RowBatchWriter;\r\n select: string;\r\n rows: number;\r\n}\r\n\r\nconst ROW_OVERHEAD = 48;\r\n\r\n/** `BuildPushBatchAsync`: the next batch of pending changes, oldest first, within the row and byte budgets. */\r\nexport async function buildPushBatch(store: StoreInternals, tables: readonly G9DtNegotiatedTable[], maxRows: number, maxBytes: number): Promise<G9PushBatch | null> {\r\n store.ensureInitialized();\r\n const uploadable = new Map<number, { table: LocalTable; negotiated: G9DtNegotiatedTable; columns: PushColumn[] }>();\r\n for (const negotiated of tables) {\r\n if (negotiated.direction === G9ESyncDirection.DownloadOnly) continue;\r\n const local = findByServerName(store, negotiated.name);\r\n if (!local) continue;\r\n const readOnly = new Set<string>();\r\n for (const c of negotiated.readOnlyColumns) readOnly.add(c.toLowerCase());\r\n for (const c of negotiated.computedColumns) readOnly.add(c.toLowerCase());\r\n const columns = negotiated.columns\r\n .filter((c) => !readOnly.has(c.name.toLowerCase()) && local.model.indexOf(c.name) >= 0)\r\n .map((c) => ({ name: c.name, type: c.type, local: local.model.indexOf(c.name) }));\r\n uploadable.set(local.tid, { table: local, negotiated, columns });\r\n }\r\n\r\n return store.writeLock.run(() =>\r\n store.writer().transaction('immediate', async () => {\r\n const connection = store.writer();\r\n const inflightBatch = await meta(connection, 'inflight_batch_id');\r\n const inflightMax = await meta(connection, 'inflight_max_seq');\r\n const offsetText = await meta(connection, 'server_offset_ms');\r\n const offsetMs = offsetText !== null && /^[+-]?\\d+$/.test(offsetText) ? Number(offsetText) : 0;\r\n\r\n const tids = Array.from(uploadable.keys());\r\n // `+tid`: the unary plus keeps the tid list from driving the plan. Without it SQLite searches the primary key by\r\n // tid — every row of every uploadable table (486k on a farm device, 16 s on the bridge) — and sorts them, instead\r\n // of walking the partial g9_rows_dirty index, which holds only the pending rows and is already in local_seq order.\r\n const sql =\r\n 'SELECT tid, pk, dirty, dirty_mask, server_ver, local_seq, coalesce(hlc_ms, 0) FROM g9_rows WHERE dirty <> 0 AND rejected = 0 AND +tid IN (' +\r\n tids.join(',') +\r\n ')' +\r\n (inflightMax === null ? '' : ' AND local_seq <= $imax') +\r\n ' ORDER BY local_seq LIMIT $max';\r\n const commands = await readPendingCommands(store, connection, inflightBatch, Math.max(1, maxRows));\r\n const pending =\r\n uploadable.size === 0\r\n ? []\r\n : await connection.query(sql, inflightMax === null ? { $max: Math.max(1, maxRows) } : { $max: Math.max(1, maxRows), $imax: Number(inflightMax) });\r\n const rows = pending.map((r) => ({\r\n tid: asNumber(r[0]),\r\n key: asString(r[1]),\r\n dirty: asNumber(r[2]),\r\n mask: asLong(r[3]),\r\n base: asNumber(r[4]),\r\n seq: asNumber(r[5]),\r\n hlc: asNumber(r[6]),\r\n }));\r\n\r\n if (rows.length === 0 && commands.length === 0) {\r\n if (inflightBatch !== null) {\r\n // Everything the in-flight batch carried has been resolved another way; forget it.\r\n await connection.exec(\"DELETE FROM g9_meta WHERE key IN ('inflight_batch_id', 'inflight_max_seq')\");\r\n }\r\n return null;\r\n }\r\n\r\n // Rows are encoded in local_seq order and the batch ends at the first row past the byte budget, so a batch is always\r\n // a PREFIX of the pending changes. One section (frame) per table.\r\n const sections = new Map<number, PushSection>();\r\n let included = 0;\r\n let includedMaxSequence = 0;\r\n let budgetUsed = 0;\r\n let truncated = false;\r\n const tooLarge: { tid: number; key: string; bytes: number }[] = [];\r\n for (const row of rows) {\r\n const entry = uploadable.get(row.tid);\r\n if (!entry) continue;\r\n let section = sections.get(row.tid);\r\n if (!section) {\r\n const select =\r\n entry.columns.length === 0\r\n ? `SELECT 1 FROM ${entry.table.quoted} WHERE ${entry.table.keyQuoted} = $k`\r\n : `SELECT ${entry.columns.map((c) => pushSelectExpression(quote(c.name), c.type)).join(', ')} FROM ${entry.table.quoted} WHERE ${entry.table.keyQuoted} = $k`;\r\n const writer = new G9RowBatchWriter();\r\n writer.beginSection(\r\n G9ERowBatchOp.Push,\r\n entry.negotiated.tableId,\r\n entry.table.model.keyType,\r\n entry.columns.map((c) => c.type),\r\n );\r\n section = { table: entry.table, negotiated: entry.negotiated, columns: entry.columns, writer, select, rows: 0 };\r\n sections.set(row.tid, section);\r\n }\r\n\r\n const hlc = row.hlc === 0 ? 0 : row.hlc + offsetMs;\r\n const keyBytes = utf8Encode(row.key).length;\r\n if (row.dirty === 2) {\r\n const deleteBytes = ROW_OVERHEAD + keyBytes;\r\n if (included > 0 && budgetUsed + deleteBytes > maxBytes) {\r\n truncated = true;\r\n break;\r\n }\r\n section.writer.beginPushRow(row.key, G9EPushRowOp.Delete, row.seq, row.base, G9Long.ZERO, hlc);\r\n section.writer.endRow();\r\n section.rows++;\r\n included++;\r\n includedMaxSequence = row.seq;\r\n budgetUsed += deleteBytes;\r\n continue;\r\n }\r\n\r\n const found = await connection.query(section.select, { $k: keyValue(section.table, row.key) });\r\n const values = found[0];\r\n if (!values) {\r\n section.writer.beginPushRow(row.key, G9EPushRowOp.Delete, row.seq, row.base, G9Long.ZERO, hlc);\r\n section.writer.endRow();\r\n section.rows++;\r\n included++;\r\n includedMaxSequence = row.seq;\r\n budgetUsed += ROW_OVERHEAD + keyBytes;\r\n continue;\r\n }\r\n\r\n let rowBytes = ROW_OVERHEAD + keyBytes;\r\n for (let j = 0; j < section.columns.length; j++) rowBytes += estimateEncodedBytes(values[j] as G9SqlValue, (section.columns[j] as PushColumn).type);\r\n if (rowBytes > maxBytes) {\r\n // Can never fit, whatever else the batch carries: reject it here so it does not block every later push.\r\n tooLarge.push({ tid: row.tid, key: row.key, bytes: rowBytes });\r\n continue;\r\n }\r\n if (included > 0 && budgetUsed + rowBytes > maxBytes) {\r\n truncated = true;\r\n break;\r\n }\r\n\r\n let pushMask = G9Long.ZERO;\r\n for (let j = 0; j < section.columns.length; j++) {\r\n if (row.mask.testBit((section.columns[j] as PushColumn).local)) pushMask = pushMask.or(G9Long.ONE.shiftLeft(j));\r\n }\r\n section.writer.beginPushRow(row.key, G9EPushRowOp.Upsert, row.seq, row.base, pushMask, hlc);\r\n for (let j = 0; j < section.columns.length; j++) writeLocalValue(section.writer, values[j] as G9SqlValue, (section.columns[j] as PushColumn).type, store.options);\r\n section.writer.endRow();\r\n section.rows++;\r\n included++;\r\n includedMaxSequence = row.seq;\r\n budgetUsed += rowBytes;\r\n }\r\n\r\n for (const { tid, key, bytes } of tooLarge) {\r\n await connection.exec('UPDATE g9_rows SET rejected = 1 WHERE tid = $t AND pk = $pk', { $t: tid, $pk: key });\r\n await connection.exec(\r\n 'INSERT OR REPLACE INTO g9_rejections (tid, pk, code, message, sql_state, constraint_name, column_name, details, at_utc) ' +\r\n 'VALUES ($t, $pk, $code, $message, NULL, NULL, NULL, NULL, $at)',\r\n {\r\n $t: tid,\r\n $pk: key,\r\n $code: G9SyncErrorCodes.RejectTooLarge,\r\n $message: `The row needs about ${bytes} bytes; the server accepts at most ${maxBytes} bytes per push.`,\r\n $at: nowMs(),\r\n },\r\n );\r\n }\r\n\r\n if (included === 0 && commands.length === 0) {\r\n if (tooLarge.length === 0) return null;\r\n return { batchId: 0, maxSequence: 0, frames: [], tables: [], rowCount: 0, truncated: false, rejectedLocally: tooLarge.length, commands: [] };\r\n }\r\n\r\n const frames: Uint8Array[] = [];\r\n const usedTables: G9DtTableColumns[] = [];\r\n sections.forEach((section) => {\r\n if (section.rows === 0) return;\r\n section.writer.endSection();\r\n frames.push(section.writer.toFrame({ compressor: store.options.frameCompressor }));\r\n usedTables.push({ tableId: section.negotiated.tableId, columns: section.columns.map((c) => c.name) });\r\n });\r\n\r\n let batchId: number;\r\n let maxSequence: number;\r\n if (inflightBatch !== null) {\r\n batchId = Number(inflightBatch);\r\n maxSequence = Number(inflightMax);\r\n } else {\r\n batchId = Number((await meta(connection, 'next_batch_id')) ?? '1');\r\n maxSequence = includedMaxSequence;\r\n await setMeta(connection, 'next_batch_id', String(batchId + 1));\r\n await setMeta(connection, 'inflight_batch_id', String(batchId));\r\n await setMeta(connection, 'inflight_max_seq', String(maxSequence));\r\n }\r\n for (const item of commands) {\r\n await connection.exec('UPDATE g9_commands SET batch_id = $b WHERE command_id = $id', { $b: batchId, $id: item.commandId });\r\n }\r\n return { batchId, maxSequence, frames, tables: usedTables, rowCount: included, truncated, rejectedLocally: tooLarge.length, commands };\r\n }),\r\n );\r\n}\r\n\r\n/** `ForgetUploadedRowsAsync`: removes uploaded rows of an UPLOAD-ONLY table without uploading a delete. */\r\nexport async function forgetUploadedRows(store: StoreInternals, localTable: string, keys: readonly string[] | null): Promise<number> {\r\n store.ensureInitialized();\r\n const table = getLocal(store, localTable);\r\n if (!table.negotiated || table.negotiated.direction !== G9ESyncDirection.UploadOnly) {\r\n throw new Error(`'${localTable}' is not upload-only: deleting its rows locally is a change the server must receive.`);\r\n }\r\n return store.writeLock.run(() =>\r\n store.writer().transaction('immediate', async () => {\r\n const connection = store.writer();\r\n await connection.exec('UPDATE g9_ctx SET applying = 1 WHERE id = 1'); // a library write: not captured\r\n await connection.exec('CREATE TEMP TABLE IF NOT EXISTS g9_forget (pk TEXT PRIMARY KEY); DELETE FROM g9_forget');\r\n if (keys === null) {\r\n await connection.exec('INSERT INTO g9_forget (pk) SELECT pk FROM g9_rows WHERE tid = $t AND dirty = 0', { $t: table.tid });\r\n } else {\r\n for (const key of keys) {\r\n await connection.exec('INSERT OR IGNORE INTO g9_forget (pk) SELECT pk FROM g9_rows WHERE tid = $t AND pk = $pk AND dirty = 0', { $t: table.tid, $pk: key });\r\n }\r\n }\r\n const forgotten = asNumber(await connection.scalar('SELECT count(*) FROM g9_forget'));\r\n await connection.exec(`DELETE FROM ${table.quoted} WHERE CAST(${table.keyQuoted} AS TEXT) IN (SELECT pk FROM g9_forget)`);\r\n await connection.exec('DELETE FROM g9_rows WHERE tid = $t AND pk IN (SELECT pk FROM g9_forget); DELETE FROM g9_forget', { $t: table.tid });\r\n await connection.exec('UPDATE g9_ctx SET applying = 0 WHERE id = 1');\r\n return forgotten;\r\n }),\r\n );\r\n}\r\n\r\n/** `ApplyPushResultAsync`. */\r\nexport async function applyPushResult(store: StoreInternals, batch: G9PushBatch, result: G9DtPushResult, signal?: AbortSignal): Promise<G9ApplyReport> {\r\n store.ensureInitialized();\r\n return store.writeLock.run(() =>\r\n store.writer().transaction('immediate', async () => {\r\n const connection = store.writer();\r\n // Before BeginApply: finished commands release their rows, so the canonical rows below replace the optimistic ones.\r\n const completed = await completeCommands(connection, result.commandResults);\r\n // Read BEFORE anything below touches the rows: what the device sent for each conflict it may have lost (LES-0072).\r\n const lost = await readLostConflicts(store, batch, result);\r\n const context = await beginApply(store, connection, { dirtyAfterSequence: batch.maxSequence, backfills: [], signal });\r\n context.report.commandsCompleted = completed;\r\n\r\n const acceptSql =\r\n 'UPDATE g9_rows SET server_ver = $v, ' +\r\n 'dirty = CASE WHEN local_seq <= $m THEN 0 ELSE dirty END, ' +\r\n 'dirty_mask = CASE WHEN local_seq <= $m THEN 0 ELSE dirty_mask END, ' +\r\n 'deleted = CASE WHEN local_seq <= $m AND dirty = 2 THEN 1 ELSE deleted END, ' +\r\n 'rejected = 0 WHERE tid = $t AND pk = $pk; ' +\r\n 'DELETE FROM g9_rejections WHERE tid = $t AND pk = $pk';\r\n for (const accepted of result.accepted) {\r\n const table = store.byServerId.get(accepted.tableId);\r\n if (!table) continue;\r\n for (let i = 0; i < accepted.keys.length; i++) {\r\n await context.queue(acceptSql, { $v: i < accepted.versions.length ? (accepted.versions[i] as number) : 0, $m: batch.maxSequence, $t: table.tid, $pk: accepted.keys[i] as string });\r\n context.report.accepted++;\r\n }\r\n }\r\n\r\n const rejectSql =\r\n 'UPDATE g9_rows SET rejected = 1 WHERE tid = $t AND pk = $pk AND local_seq <= $m; ' +\r\n 'INSERT OR REPLACE INTO g9_rejections (tid, pk, code, message, sql_state, constraint_name, column_name, details, at_utc) ' +\r\n 'VALUES ($t, $pk, $code, $message, $state, $constraint, $column, $details, $now)';\r\n const now = nowMs();\r\n for (const rejection of result.rejections) {\r\n const table = store.byServerId.get(rejection.tableId);\r\n if (!table) continue;\r\n await context.queue(rejectSql, {\r\n $t: table.tid,\r\n $pk: rejection.key,\r\n $m: batch.maxSequence,\r\n $code: rejection.code,\r\n $message: rejection.message ?? null,\r\n $state: rejection.sqlState ?? null,\r\n $constraint: rejection.constraint ?? null,\r\n $column: rejection.column ?? null,\r\n $details: rejection.details ?? null,\r\n $now: now,\r\n });\r\n context.report.rejected++;\r\n }\r\n\r\n context.report.conflicts = result.conflicts.length;\r\n await context.flush();\r\n for (const frame of result.canonicalFrames) await applyFrame(store, context, frame, null);\r\n await context.flush();\r\n await keepLostConflicts(store, context, lost);\r\n\r\n await endApply(context);\r\n await connection.exec(\"DELETE FROM g9_meta WHERE key IN ('inflight_batch_id', 'inflight_max_seq')\");\r\n return context.report;\r\n }),\r\n );\r\n}\r\n\r\n/** A conflict the device lost, with its row as it was sent. */\r\ninterface LostConflict {\r\n table: LocalTable;\r\n key: string;\r\n code: string;\r\n dirty: number;\r\n lostMask: G9Long;\r\n subs: G9Long;\r\n version: G9Long;\r\n values: { [column: string]: G9SqlValue };\r\n}\r\n\r\n/**\r\n * Twin of .NET ReadLostConflicts (LES-0072): the conflicts of this push in which the server kept its own values for something the\r\n * device changed - the whole change, or the overlapping columns of a column merge - each with the device's row as it was sent.\r\n */\r\nasync function readLostConflicts(store: StoreInternals, batch: G9PushBatch, result: G9DtPushResult): Promise<LostConflict[]> {\r\n const lost: LostConflict[] = [];\r\n if (!store.options.keepLostConflicts || result.conflicts.length === 0) return lost;\r\n const connection = store.writer();\r\n const pushOrder = new Map(batch.tables.map((t) => [t.tableId, t.columns]));\r\n for (const conflict of result.conflicts) {\r\n const table = store.byServerId.get(conflict.tableId);\r\n if (!table) continue;\r\n const row = (await connection.query('SELECT dirty, dirty_mask, subs, server_ver, local_seq FROM g9_rows WHERE tid = $t AND pk = $pk', { $t: table.tid, $pk: conflict.key }))[0];\r\n if (!row) continue;\r\n const dirty = asNumber(row[0]);\r\n const mask = asLong(row[1]);\r\n if (dirty === 0 || asNumber(row[4]) > batch.maxSequence) continue;\r\n const serverDeleted = conflict.code === G9SyncErrorCodes.ConflictDeleted;\r\n if (dirty === 2 && serverDeleted) continue; // both sides deleted it: nothing was lost\r\n\r\n let lostMask = mask;\r\n const names = pushOrder.get(conflict.tableId);\r\n if (dirty === 1 && conflict.partiallyApplied && !conflict.serverChangedMask.isNegative() && conflict.code === G9SyncErrorCodes.ConflictColumns && names) {\r\n // The server's bits are in push column order; the device's are model columns.\r\n lostMask = G9Long.ZERO;\r\n for (let j = 0; j < names.length && j < 63; j++) {\r\n if (!conflict.serverChangedMask.testBit(j)) continue;\r\n const local = table.model.indexOf(names[j] as string);\r\n if (local >= 0 && mask.testBit(local)) lostMask = lostMask.or(G9Long.ONE.shiftLeft(local));\r\n }\r\n if (lostMask.isZero()) continue;\r\n }\r\n\r\n lost.push({ table, key: conflict.key, code: conflict.code, dirty, lostMask, subs: asLong(row[2]), version: asLong(row[3]), values: await readLocalValues(connection, table, conflict.key) });\r\n }\r\n return lost;\r\n}\r\n\r\n/** Twin of .NET KeepLostConflicts: the device's lost values back over the canonical rows, parked as rejections (LES-0072). */\r\nasync function keepLostConflicts(store: StoreInternals, context: Awaited<ReturnType<typeof beginApply>>, lost: LostConflict[]): Promise<void> {\r\n const connection = context.connection;\r\n for (const item of lost) {\r\n const table = item.table;\r\n const key = keyValue(table, item.key);\r\n const now = await readLocalValues(connection, table, item.key);\r\n let columnNames: string | null = null;\r\n let serverValues: string | null = null;\r\n if (item.dirty === 2) {\r\n if (Object.keys(now).length > 0) await connection.exec(`DELETE FROM ${table.quoted} WHERE ${table.keyQuoted} = $k`, { $k: key });\r\n } else if (Object.keys(now).length === 0) {\r\n // The server deleted it: the device's row comes back as it was sent.\r\n const all = Object.keys(item.values);\r\n if (all.length === 0) continue;\r\n const params: { [name: string]: G9SqlValue } = {};\r\n all.forEach((column, i) => (params['$v' + i] = item.values[column] as G9SqlValue));\r\n await connection.exec(`INSERT INTO ${table.quoted} (${all.map(quote).join(', ')}) VALUES (${all.map((_, i) => '$v' + i).join(', ')})`, params);\r\n } else {\r\n const columns = table.model.columns.map((c, i) => ({ name: c.name, i })).filter((c) => item.lostMask.testBit(c.i) && c.name in item.values).map((c) => c.name);\r\n if (columns.length > 0) {\r\n columnNames = columns.join(',');\r\n const server: { [column: string]: unknown } = {};\r\n for (const column of columns) server[column] = now[column] ?? null;\r\n serverValues = serializeValues(server);\r\n const params: { [name: string]: G9SqlValue } = { $k: key };\r\n columns.forEach((column, i) => (params['$v' + i] = item.values[column] as G9SqlValue));\r\n await connection.exec(`UPDATE ${table.quoted} SET ${columns.map((c, i) => quote(c) + ' = $v' + i).join(', ')} WHERE ${table.keyQuoted} = $k`, params);\r\n }\r\n }\r\n\r\n const mask = item.dirty === 2 ? G9Long.ZERO : item.lostMask;\r\n await connection.exec(\r\n 'INSERT INTO g9_rows (tid, pk, server_ver, local_seq, subs, dirty, dirty_mask, rejected, deleted) ' +\r\n 'VALUES ($t, $pk, $v, (SELECT seq FROM g9_clock WHERE id = 1) + 1, $subs, $d, $m, 1, 0) ' +\r\n 'ON CONFLICT(tid, pk) DO UPDATE SET dirty = excluded.dirty, dirty_mask = excluded.dirty_mask, rejected = 1, deleted = 0, ' +\r\n 'subs = CASE WHEN g9_rows.subs = 0 THEN excluded.subs ELSE g9_rows.subs END, local_seq = excluded.local_seq; ' +\r\n 'UPDATE g9_clock SET seq = seq + 1 WHERE id = 1',\r\n { $t: table.tid, $pk: item.key, $v: item.version.toSqlValue(), $subs: item.subs.toSqlValue(), $d: item.dirty, $m: mask.toSqlValue() },\r\n );\r\n await connection.exec(\r\n 'INSERT OR REPLACE INTO g9_rejections (tid, pk, code, message, sql_state, constraint_name, column_name, details, at_utc) ' +\r\n 'VALUES ($t, $pk, $code, $message, NULL, NULL, $column, $details, $now)',\r\n {\r\n $t: table.tid,\r\n $pk: item.key,\r\n $code: item.code,\r\n $message:\r\n item.dirty === 2\r\n ? \"The server changed this row after the device last saw it; the device's delete was kept here, not applied.\"\r\n : `The server changed ${columnNames ?? 'this row'} after the device last saw it; the device's values were kept here, not applied.`,\r\n $column: columnNames,\r\n $details: serverValues,\r\n $now: nowMs(),\r\n },\r\n );\r\n context.report.conflictsKept++;\r\n if (context.wantsChangedKey(table.model.localName)) context.addChanged(table.model.localName, item.key);\r\n }\r\n}\r\n"]}
1
+ {"version":3,"file":"push.js","sourceRoot":"","sources":["../../src/sqlite/push.ts"],"names":[],"mappings":"AAAA,wHAAwH;AACxH,2HAA2H;AAC3H,eAAe;AACf,OAAO,EAAE,iBAAiB,EAAE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,CAAC;QACvD,MAAM,UAAU,CAAC,IAAI,CACnB,mGAAmG;YACjG,yFAAyF;YACzF,0HAA0H;YAC1H,8GAA8G;YAC9G,gDAAgD,EAClD,EAAE,EAAE,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,EAAE,IAAI,CAAC,UAAU,EAAE,EAAE,CACtI,CAAC;QACF,MAAM,UAAU,CAAC,IAAI,CACnB,0HAA0H;YACxH,wEAAwE,EAC1E;YACE,EAAE,EAAE,KAAK,CAAC,GAAG;YACb,GAAG,EAAE,IAAI,CAAC,GAAG;YACb,KAAK,EAAE,IAAI,CAAC,IAAI;YAChB,QAAQ,EACN,IAAI,CAAC,KAAK,KAAK,CAAC;gBACd,CAAC,CAAC,2GAA2G;gBAC7G,CAAC,CAAC,sBAAsB,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,UAAU,iFAAiF;YACtI,OAAO,EAAE,WAAW;YACpB,QAAQ,EAAE,YAAY;YACtB,IAAI,EAAE,KAAK,EAAE;SACd,CACF,CAAC;QACF,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC;QAC/B,IAAI,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC;YAAE,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1G,CAAC;AACH,CAAC","sourcesContent":["// Twin of G9SyncData.Client.Sqlite/G9CSqliteSyncStore.Push.cs: dirty rows in local change order become push frames; the\r\n// result clears exactly the rows that were sent (rows edited meanwhile stay dirty), flags rejections and applies canonical\r\n// server rows.\r\nimport { G9ESyncColumnType, G9ESyncDirection } from '../abstractions/enums.js';\r\nimport { G9SyncErrorCodes } from '../abstractions/errors.js';\r\nimport { G9Long } from '../abstractions/long.js';\r\nimport { utf8Encode } from '../internal/utf8.js';\r\nimport { G9ApplyReport, type G9PushBatch } from '../client/storeTypes.js';\r\nimport type { G9DtNegotiatedTable, G9DtPushResult, G9DtTableColumns } from '../protocol/dtos.js';\r\nimport { G9EPushRowOp, G9ERowBatchOp } from '../protocol/rowBatch/format.js';\r\nimport { G9RowBatchWriter } from '../protocol/rowBatch/writer.js';\r\nimport { applyFrame, beginApply, endApply } from './apply.js';\r\nimport { completeCommands, readPendingCommands } from './commands.js';\r\nimport { asLong, asNumber, asString } from './connection.js';\r\nimport type { G9SqlValue } from './driver.js';\r\nimport { findByServerName, getLocal, keyValue, meta, nowMs, readLocalValues, serializeValues, setMeta, type LocalTable, type StoreInternals } from './internals.js';\r\nimport { quote } from './sql.js';\r\nimport { estimateEncodedBytes, pushSelectExpression, writeLocalValue } from './values.js';\r\n\r\ninterface PushColumn {\r\n name: string;\r\n type: number;\r\n local: number;\r\n}\r\n\r\ninterface PushSection {\r\n table: LocalTable;\r\n negotiated: G9DtNegotiatedTable;\r\n columns: PushColumn[];\r\n writer: G9RowBatchWriter;\r\n select: string;\r\n rows: number;\r\n}\r\n\r\nconst ROW_OVERHEAD = 48;\r\n\r\n/** `BuildPushBatchAsync`: the next batch of pending changes, oldest first, within the row and byte budgets. */\r\nexport async function buildPushBatch(store: StoreInternals, tables: readonly G9DtNegotiatedTable[], maxRows: number, maxBytes: number): Promise<G9PushBatch | null> {\r\n store.ensureInitialized();\r\n const uploadable = new Map<number, { table: LocalTable; negotiated: G9DtNegotiatedTable; columns: PushColumn[] }>();\r\n for (const negotiated of tables) {\r\n if (negotiated.direction === G9ESyncDirection.DownloadOnly) continue;\r\n const local = findByServerName(store, negotiated.name);\r\n if (!local) continue;\r\n const readOnly = new Set<string>();\r\n for (const c of negotiated.readOnlyColumns) readOnly.add(c.toLowerCase());\r\n for (const c of negotiated.computedColumns) readOnly.add(c.toLowerCase());\r\n const columns = negotiated.columns\r\n .filter((c) => !readOnly.has(c.name.toLowerCase()) && local.model.indexOf(c.name) >= 0)\r\n .map((c) => ({ name: c.name, type: c.type, local: local.model.indexOf(c.name) }));\r\n uploadable.set(local.tid, { table: local, negotiated, columns });\r\n }\r\n\r\n return store.writeLock.run(() =>\r\n store.writer().transaction('immediate', async () => {\r\n const connection = store.writer();\r\n const inflightBatch = await meta(connection, 'inflight_batch_id');\r\n const inflightMax = await meta(connection, 'inflight_max_seq');\r\n const offsetText = await meta(connection, 'server_offset_ms');\r\n const offsetMs = offsetText !== null && /^[+-]?\\d+$/.test(offsetText) ? Number(offsetText) : 0;\r\n\r\n const tids = Array.from(uploadable.keys());\r\n // `+tid`: the unary plus keeps the tid list from driving the plan. Without it SQLite searches the primary key by\r\n // tid — every row of every uploadable table (486k on a farm device, 16 s on the bridge) — and sorts them, instead\r\n // of walking the partial g9_rows_dirty index, which holds only the pending rows and is already in local_seq order.\r\n const sql =\r\n 'SELECT tid, pk, dirty, dirty_mask, server_ver, local_seq, coalesce(hlc_ms, 0) FROM g9_rows WHERE dirty <> 0 AND rejected = 0 AND +tid IN (' +\r\n tids.join(',') +\r\n ')' +\r\n (inflightMax === null ? '' : ' AND local_seq <= $imax') +\r\n ' ORDER BY local_seq LIMIT $max';\r\n const commands = await readPendingCommands(store, connection, inflightBatch, Math.max(1, maxRows));\r\n const pending =\r\n uploadable.size === 0\r\n ? []\r\n : await connection.query(sql, inflightMax === null ? { $max: Math.max(1, maxRows) } : { $max: Math.max(1, maxRows), $imax: Number(inflightMax) });\r\n const rows = pending.map((r) => ({\r\n tid: asNumber(r[0]),\r\n key: asString(r[1]),\r\n dirty: asNumber(r[2]),\r\n mask: asLong(r[3]),\r\n base: asNumber(r[4]),\r\n seq: asNumber(r[5]),\r\n hlc: asNumber(r[6]),\r\n }));\r\n\r\n if (rows.length === 0 && commands.length === 0) {\r\n if (inflightBatch !== null) {\r\n // Everything the in-flight batch carried has been resolved another way; forget it.\r\n await connection.exec(\"DELETE FROM g9_meta WHERE key IN ('inflight_batch_id', 'inflight_max_seq')\");\r\n }\r\n return null;\r\n }\r\n\r\n // Rows are encoded in local_seq order and the batch ends at the first row past the byte budget, so a batch is always\r\n // a PREFIX of the pending changes. One section (frame) per table.\r\n const sections = new Map<number, PushSection>();\r\n let included = 0;\r\n let includedMaxSequence = 0;\r\n let budgetUsed = 0;\r\n let truncated = false;\r\n const tooLarge: { tid: number; key: string; bytes: number }[] = [];\r\n for (const row of rows) {\r\n const entry = uploadable.get(row.tid);\r\n if (!entry) continue;\r\n let section = sections.get(row.tid);\r\n if (!section) {\r\n const select =\r\n entry.columns.length === 0\r\n ? `SELECT 1 FROM ${entry.table.quoted} WHERE ${entry.table.keyQuoted} = $k`\r\n : `SELECT ${entry.columns.map((c) => pushSelectExpression(quote(c.name), c.type)).join(', ')} FROM ${entry.table.quoted} WHERE ${entry.table.keyQuoted} = $k`;\r\n const writer = new G9RowBatchWriter();\r\n writer.beginSection(\r\n G9ERowBatchOp.Push,\r\n entry.negotiated.tableId,\r\n entry.table.model.keyType,\r\n entry.columns.map((c) => c.type),\r\n );\r\n section = { table: entry.table, negotiated: entry.negotiated, columns: entry.columns, writer, select, rows: 0 };\r\n sections.set(row.tid, section);\r\n }\r\n\r\n const hlc = row.hlc === 0 ? 0 : row.hlc + offsetMs;\r\n const keyBytes = utf8Encode(row.key).length;\r\n if (row.dirty === 2) {\r\n const deleteBytes = ROW_OVERHEAD + keyBytes;\r\n if (included > 0 && budgetUsed + deleteBytes > maxBytes) {\r\n truncated = true;\r\n break;\r\n }\r\n section.writer.beginPushRow(row.key, G9EPushRowOp.Delete, row.seq, row.base, G9Long.ZERO, hlc);\r\n section.writer.endRow();\r\n section.rows++;\r\n included++;\r\n includedMaxSequence = row.seq;\r\n budgetUsed += deleteBytes;\r\n continue;\r\n }\r\n\r\n const found = await connection.query(section.select, { $k: keyValue(section.table, row.key) });\r\n const values = found[0];\r\n if (!values) {\r\n section.writer.beginPushRow(row.key, G9EPushRowOp.Delete, row.seq, row.base, G9Long.ZERO, hlc);\r\n section.writer.endRow();\r\n section.rows++;\r\n included++;\r\n includedMaxSequence = row.seq;\r\n budgetUsed += ROW_OVERHEAD + keyBytes;\r\n continue;\r\n }\r\n\r\n let rowBytes = ROW_OVERHEAD + keyBytes;\r\n for (let j = 0; j < section.columns.length; j++) rowBytes += estimateEncodedBytes(values[j] as G9SqlValue, (section.columns[j] as PushColumn).type);\r\n if (rowBytes > maxBytes) {\r\n // Can never fit, whatever else the batch carries: reject it here so it does not block every later push.\r\n tooLarge.push({ tid: row.tid, key: row.key, bytes: rowBytes });\r\n continue;\r\n }\r\n if (included > 0 && budgetUsed + rowBytes > maxBytes) {\r\n truncated = true;\r\n break;\r\n }\r\n\r\n let pushMask = G9Long.ZERO;\r\n for (let j = 0; j < section.columns.length; j++) {\r\n if (row.mask.testBit((section.columns[j] as PushColumn).local)) pushMask = pushMask.or(G9Long.ONE.shiftLeft(j));\r\n }\r\n section.writer.beginPushRow(row.key, G9EPushRowOp.Upsert, row.seq, row.base, pushMask, hlc);\r\n for (let j = 0; j < section.columns.length; j++) writeLocalValue(section.writer, values[j] as G9SqlValue, (section.columns[j] as PushColumn).type, store.options);\r\n section.writer.endRow();\r\n section.rows++;\r\n included++;\r\n includedMaxSequence = row.seq;\r\n budgetUsed += rowBytes;\r\n }\r\n\r\n for (const { tid, key, bytes } of tooLarge) {\r\n await connection.exec('UPDATE g9_rows SET rejected = 1 WHERE tid = $t AND pk = $pk', { $t: tid, $pk: key });\r\n await connection.exec(\r\n 'INSERT OR REPLACE INTO g9_rejections (tid, pk, code, message, sql_state, constraint_name, column_name, details, at_utc) ' +\r\n 'VALUES ($t, $pk, $code, $message, NULL, NULL, NULL, NULL, $at)',\r\n {\r\n $t: tid,\r\n $pk: key,\r\n $code: G9SyncErrorCodes.RejectTooLarge,\r\n $message: `The row needs about ${bytes} bytes; the server accepts at most ${maxBytes} bytes per push.`,\r\n $at: nowMs(),\r\n },\r\n );\r\n }\r\n\r\n if (included === 0 && commands.length === 0) {\r\n if (tooLarge.length === 0) return null;\r\n return { batchId: 0, maxSequence: 0, frames: [], tables: [], rowCount: 0, truncated: false, rejectedLocally: tooLarge.length, commands: [] };\r\n }\r\n\r\n const frames: Uint8Array[] = [];\r\n const usedTables: G9DtTableColumns[] = [];\r\n sections.forEach((section) => {\r\n if (section.rows === 0) return;\r\n section.writer.endSection();\r\n frames.push(section.writer.toFrame({ compressor: store.options.frameCompressor }));\r\n usedTables.push({ tableId: section.negotiated.tableId, columns: section.columns.map((c) => c.name) });\r\n });\r\n\r\n let batchId: number;\r\n let maxSequence: number;\r\n if (inflightBatch !== null) {\r\n batchId = Number(inflightBatch);\r\n maxSequence = Number(inflightMax);\r\n } else {\r\n batchId = Number((await meta(connection, 'next_batch_id')) ?? '1');\r\n maxSequence = includedMaxSequence;\r\n await setMeta(connection, 'next_batch_id', String(batchId + 1));\r\n await setMeta(connection, 'inflight_batch_id', String(batchId));\r\n await setMeta(connection, 'inflight_max_seq', String(maxSequence));\r\n }\r\n for (const item of commands) {\r\n await connection.exec('UPDATE g9_commands SET batch_id = $b WHERE command_id = $id', { $b: batchId, $id: item.commandId });\r\n }\r\n return { batchId, maxSequence, frames, tables: usedTables, rowCount: included, truncated, rejectedLocally: tooLarge.length, commands };\r\n }),\r\n );\r\n}\r\n\r\n/** `ForgetUploadedRowsAsync`: removes uploaded rows of an UPLOAD-ONLY table without uploading a delete. */\r\nexport async function forgetUploadedRows(store: StoreInternals, localTable: string, keys: readonly string[] | null): Promise<number> {\r\n store.ensureInitialized();\r\n const table = getLocal(store, localTable);\r\n if (!table.negotiated || table.negotiated.direction !== G9ESyncDirection.UploadOnly) {\r\n throw new Error(`'${localTable}' is not upload-only: deleting its rows locally is a change the server must receive.`);\r\n }\r\n return store.writeLock.run(() =>\r\n store.writer().transaction('immediate', async () => {\r\n const connection = store.writer();\r\n await connection.exec('UPDATE g9_ctx SET applying = 1 WHERE id = 1'); // a library write: not captured\r\n await connection.exec('CREATE TEMP TABLE IF NOT EXISTS g9_forget (pk TEXT PRIMARY KEY); DELETE FROM g9_forget');\r\n if (keys === null) {\r\n await connection.exec('INSERT INTO g9_forget (pk) SELECT pk FROM g9_rows WHERE tid = $t AND dirty = 0', { $t: table.tid });\r\n } else {\r\n for (const key of keys) {\r\n await connection.exec('INSERT OR IGNORE INTO g9_forget (pk) SELECT pk FROM g9_rows WHERE tid = $t AND pk = $pk AND dirty = 0', { $t: table.tid, $pk: key });\r\n }\r\n }\r\n const forgotten = asNumber(await connection.scalar('SELECT count(*) FROM g9_forget'));\r\n await connection.exec(`DELETE FROM ${table.quoted} WHERE CAST(${table.keyQuoted} AS TEXT) IN (SELECT pk FROM g9_forget)`);\r\n await connection.exec('DELETE FROM g9_rows WHERE tid = $t AND pk IN (SELECT pk FROM g9_forget); DELETE FROM g9_forget', { $t: table.tid });\r\n await connection.exec('UPDATE g9_ctx SET applying = 0 WHERE id = 1');\r\n return forgotten;\r\n }),\r\n );\r\n}\r\n\r\n/** `ApplyPushResultAsync`. */\r\nexport async function applyPushResult(store: StoreInternals, batch: G9PushBatch, result: G9DtPushResult, signal?: AbortSignal): Promise<G9ApplyReport> {\r\n store.ensureInitialized();\r\n return store.writeLock.run(() =>\r\n store.writer().transaction('immediate', async () => {\r\n const connection = store.writer();\r\n // Before BeginApply: finished commands release their rows, so the canonical rows below replace the optimistic ones.\r\n const completed = await completeCommands(connection, result.commandResults);\r\n // Read BEFORE anything below touches the rows: what the device sent for each conflict it may have lost (LES-0072).\r\n const lost = await readLostConflicts(store, batch, result);\r\n const context = await beginApply(store, connection, { dirtyAfterSequence: batch.maxSequence, backfills: [], signal });\r\n context.report.commandsCompleted = completed;\r\n\r\n const acceptSql =\r\n 'UPDATE g9_rows SET server_ver = $v, ' +\r\n 'dirty = CASE WHEN local_seq <= $m THEN 0 ELSE dirty END, ' +\r\n 'dirty_mask = CASE WHEN local_seq <= $m THEN 0 ELSE dirty_mask END, ' +\r\n 'deleted = CASE WHEN local_seq <= $m AND dirty = 2 THEN 1 ELSE deleted END, ' +\r\n 'rejected = 0 WHERE tid = $t AND pk = $pk; ' +\r\n 'DELETE FROM g9_rejections WHERE tid = $t AND pk = $pk';\r\n for (const accepted of result.accepted) {\r\n const table = store.byServerId.get(accepted.tableId);\r\n if (!table) continue;\r\n for (let i = 0; i < accepted.keys.length; i++) {\r\n await context.queue(acceptSql, { $v: i < accepted.versions.length ? (accepted.versions[i] as number) : 0, $m: batch.maxSequence, $t: table.tid, $pk: accepted.keys[i] as string });\r\n context.report.accepted++;\r\n }\r\n }\r\n\r\n const rejectSql =\r\n 'UPDATE g9_rows SET rejected = 1 WHERE tid = $t AND pk = $pk AND local_seq <= $m; ' +\r\n 'INSERT OR REPLACE INTO g9_rejections (tid, pk, code, message, sql_state, constraint_name, column_name, details, at_utc) ' +\r\n 'VALUES ($t, $pk, $code, $message, $state, $constraint, $column, $details, $now)';\r\n const now = nowMs();\r\n for (const rejection of result.rejections) {\r\n const table = store.byServerId.get(rejection.tableId);\r\n if (!table) continue;\r\n await context.queue(rejectSql, {\r\n $t: table.tid,\r\n $pk: rejection.key,\r\n $m: batch.maxSequence,\r\n $code: rejection.code,\r\n $message: rejection.message ?? null,\r\n $state: rejection.sqlState ?? null,\r\n $constraint: rejection.constraint ?? null,\r\n $column: rejection.column ?? null,\r\n $details: rejection.details ?? null,\r\n $now: now,\r\n });\r\n context.report.rejected++;\r\n }\r\n\r\n context.report.conflicts = result.conflicts.length;\r\n await context.flush();\r\n for (const frame of result.canonicalFrames) await applyFrame(store, context, frame, null);\r\n await context.flush();\r\n await keepLostConflicts(store, context, lost);\r\n\r\n await endApply(context);\r\n await connection.exec(\"DELETE FROM g9_meta WHERE key IN ('inflight_batch_id', 'inflight_max_seq')\");\r\n return context.report;\r\n }),\r\n );\r\n}\r\n\r\n/** A conflict the device lost, with its row as it was sent. */\r\ninterface LostConflict {\r\n table: LocalTable;\r\n key: string;\r\n code: string;\r\n dirty: number;\r\n lostMask: G9Long;\r\n subs: G9Long;\r\n version: G9Long;\r\n values: { [column: string]: G9SqlValue };\r\n}\r\n\r\n/**\r\n * Twin of .NET ReadLostConflicts (LES-0072): the conflicts of this push in which the server kept its own values for something the\r\n * device changed - the whole change, or the overlapping columns of a column merge - each with the device's row as it was sent.\r\n */\r\nasync function readLostConflicts(store: StoreInternals, batch: G9PushBatch, result: G9DtPushResult): Promise<LostConflict[]> {\r\n const lost: LostConflict[] = [];\r\n if (!store.options.keepLostConflicts || result.conflicts.length === 0) return lost;\r\n const connection = store.writer();\r\n const pushOrder = new Map(batch.tables.map((t) => [t.tableId, t.columns]));\r\n for (const conflict of result.conflicts) {\r\n const table = store.byServerId.get(conflict.tableId);\r\n if (!table) continue;\r\n const row = (await connection.query('SELECT dirty, dirty_mask, subs, server_ver, local_seq FROM g9_rows WHERE tid = $t AND pk = $pk', { $t: table.tid, $pk: conflict.key }))[0];\r\n if (!row) continue;\r\n const dirty = asNumber(row[0]);\r\n const mask = asLong(row[1]);\r\n if (dirty === 0 || asNumber(row[4]) > batch.maxSequence) continue;\r\n const serverDeleted = conflict.code === G9SyncErrorCodes.ConflictDeleted;\r\n if (dirty === 2 && serverDeleted) continue; // both sides deleted it: nothing was lost\r\n\r\n // Only what this push CARRIED can have been lost (LES-0086): a column the device changed but never sends - read-only on the\r\n // server, or not negotiated - was not part of the conflict, and the canonical row simply replaces it.\r\n const names = pushOrder.get(conflict.tableId);\r\n let lostMask = names ? mask.and(pushedMask(table, names)) : mask;\r\n if (dirty === 1 && lostMask.isZero()) continue;\r\n if (dirty === 1 && conflict.partiallyApplied && !conflict.serverChangedMask.isNegative() && conflict.code === G9SyncErrorCodes.ConflictColumns && names) {\r\n // The server's bits are in push column order; the device's are model columns.\r\n lostMask = G9Long.ZERO;\r\n for (let j = 0; j < names.length && j < 63; j++) {\r\n if (!conflict.serverChangedMask.testBit(j)) continue;\r\n const local = table.model.indexOf(names[j] as string);\r\n if (local >= 0 && mask.testBit(local)) lostMask = lostMask.or(G9Long.ONE.shiftLeft(local));\r\n }\r\n if (lostMask.isZero()) continue;\r\n }\r\n\r\n lost.push({ table, key: conflict.key, code: conflict.code, dirty, lostMask, subs: asLong(row[2]), version: asLong(row[3]), values: await readLocalValues(connection, table, conflict.key) });\r\n }\r\n return lost;\r\n}\r\n\r\n/** The device's column bits of the columns a push sent for this table (push column order → model order). Twin of .NET PushedMask. */\r\nfunction pushedMask(table: { model: { indexOf(name: string): number } }, pushed: readonly string[]): G9Long {\r\n let mask = G9Long.ZERO;\r\n for (const name of pushed) {\r\n const local = table.model.indexOf(name);\r\n if (local >= 0 && local < 63) mask = mask.or(G9Long.ONE.shiftLeft(local));\r\n }\r\n return mask;\r\n}\r\n\r\n/** Two SQLite values are the same value (a blob by its bytes); a different type counts as different. Twin of .NET SameValue. */\r\nfunction sameValue(a: unknown, b: unknown, type?: G9ESyncColumnType): boolean {\r\n if (a instanceof Uint8Array && b instanceof Uint8Array) return a.length === b.length && a.every((x, i) => x === b[i]);\r\n if (typeof a === 'bigint' || typeof b === 'bigint') return typeof a === typeof b && a === b;\r\n if (a === b || (a == null && b == null)) return true;\r\n // A timestamp is the same INSTANT to the microsecond (LES-0086): the device keeps 100 ns, the server microseconds.\r\n if ((type === G9ESyncColumnType.DateTime || type === G9ESyncColumnType.DateTimeOffset) && typeof a === 'string' && typeof b === 'string') {\r\n const first = instant(a);\r\n const second = instant(b);\r\n if (first && second) return Math.abs((first[0] - second[0]) * 10000 + (first[1] - second[1])) < 10;\r\n }\r\n return false;\r\n}\r\n\r\nconst instantPattern = /^(\\d{4})-(\\d{2})-(\\d{2})[T ](\\d{2}):(\\d{2}):(\\d{2})(?:\\.(\\d{1,7}))?(Z|[+-]\\d{2}:?\\d{2})?$/;\r\n\r\n/** A timestamp text as [Unix milliseconds, 100 ns units within that millisecond]; no BigInt (Lynx). Twin of .NET Instant. */\r\nfunction instant(text: string): [number, number] | null {\r\n const m = instantPattern.exec(text.trim());\r\n if (!m) return null;\r\n const fraction = ((m[7] ?? '') + '0000000').slice(0, 7);\r\n let ms = Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6]), Number(fraction.slice(0, 3)));\r\n const zone = m[8];\r\n if (zone && zone !== 'Z') {\r\n const sign = zone[0] === '-' ? -1 : 1;\r\n const digits = zone.slice(1).replace(':', '');\r\n ms -= sign * (Number(digits.slice(0, 2)) * 60 + Number(digits.slice(2, 4))) * 60000;\r\n }\r\n return [ms, Number(fraction.slice(3))];\r\n}\r\n\r\n/** Twin of .NET KeepLostConflicts: the device's lost values back over the canonical rows, parked as rejections (LES-0072). */\r\nasync function keepLostConflicts(store: StoreInternals, context: Awaited<ReturnType<typeof beginApply>>, lost: LostConflict[]): Promise<void> {\r\n const connection = context.connection;\r\n for (const item of lost) {\r\n const table = item.table;\r\n const key = keyValue(table, item.key);\r\n const now = await readLocalValues(connection, table, item.key);\r\n let columnNames: string | null = null;\r\n let serverValues: string | null = null;\r\n let lostMask = item.lostMask;\r\n if (item.dirty === 2) {\r\n if (Object.keys(now).length > 0) await connection.exec(`DELETE FROM ${table.quoted} WHERE ${table.keyQuoted} = $k`, { $k: key });\r\n } else if (Object.keys(now).length === 0) {\r\n // The server deleted it: the device's row comes back as it was sent.\r\n const all = Object.keys(item.values);\r\n if (all.length === 0) continue;\r\n const params: { [name: string]: G9SqlValue } = {};\r\n all.forEach((column, i) => (params['$v' + i] = item.values[column] as G9SqlValue));\r\n await connection.exec(`INSERT INTO ${table.quoted} (${all.map(quote).join(', ')}) VALUES (${all.map((_, i) => '$v' + i).join(', ')})`, params);\r\n } else {\r\n const lostColumns = table.model.columns.map((c, i) => ({ name: c.name, i })).filter((c) => item.lostMask.testBit(c.i) && c.name in item.values);\r\n // LES-0085: a column the server already holds with the device's own value lost nothing; when none differs, the row is in sync.\r\n const differing = lostColumns.filter((c) => !sameValue(item.values[c.name], now[c.name], table.model.columns[c.i]?.type));\r\n if (lostColumns.length > 0 && differing.length === 0) {\r\n context.report.conflictsSameValue++;\r\n continue;\r\n }\r\n if (differing.length > 0) lostMask = differing.reduce((m, c) => m.or(G9Long.ONE.shiftLeft(c.i)), G9Long.ZERO);\r\n const columns = differing.map((c) => c.name);\r\n if (columns.length > 0) {\r\n columnNames = columns.join(',');\r\n const server: { [column: string]: unknown } = {};\r\n for (const column of columns) server[column] = now[column] ?? null;\r\n serverValues = serializeValues(server);\r\n const params: { [name: string]: G9SqlValue } = { $k: key };\r\n columns.forEach((column, i) => (params['$v' + i] = item.values[column] as G9SqlValue));\r\n await connection.exec(`UPDATE ${table.quoted} SET ${columns.map((c, i) => quote(c) + ' = $v' + i).join(', ')} WHERE ${table.keyQuoted} = $k`, params);\r\n }\r\n }\r\n\r\n const mask = item.dirty === 2 ? G9Long.ZERO : lostMask;\r\n await connection.exec(\r\n 'INSERT INTO g9_rows (tid, pk, server_ver, local_seq, subs, dirty, dirty_mask, rejected, deleted) ' +\r\n 'VALUES ($t, $pk, $v, (SELECT seq FROM g9_clock WHERE id = 1) + 1, $subs, $d, $m, 1, 0) ' +\r\n 'ON CONFLICT(tid, pk) DO UPDATE SET dirty = excluded.dirty, dirty_mask = excluded.dirty_mask, rejected = 1, deleted = 0, ' +\r\n 'subs = CASE WHEN g9_rows.subs = 0 THEN excluded.subs ELSE g9_rows.subs END, local_seq = excluded.local_seq; ' +\r\n 'UPDATE g9_clock SET seq = seq + 1 WHERE id = 1',\r\n { $t: table.tid, $pk: item.key, $v: item.version.toSqlValue(), $subs: item.subs.toSqlValue(), $d: item.dirty, $m: mask.toSqlValue() },\r\n );\r\n await connection.exec(\r\n 'INSERT OR REPLACE INTO g9_rejections (tid, pk, code, message, sql_state, constraint_name, column_name, details, at_utc) ' +\r\n 'VALUES ($t, $pk, $code, $message, NULL, NULL, $column, $details, $now)',\r\n {\r\n $t: table.tid,\r\n $pk: item.key,\r\n $code: item.code,\r\n $message:\r\n item.dirty === 2\r\n ? \"The server changed this row after the device last saw it; the device's delete was kept here, not applied.\"\r\n : `The server changed ${columnNames ?? 'this row'} after the device last saw it; the device's values were kept here, not applied.`,\r\n $column: columnNames,\r\n $details: serverValues,\r\n $now: nowMs(),\r\n },\r\n );\r\n context.report.conflictsKept++;\r\n if (context.wantsChangedKey(table.model.localName)) context.addChanged(table.model.localName, item.key);\r\n }\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@g9tm/syncdata",
3
- "version": "1.5.12",
3
+ "version": "1.5.14",
4
4
  "description": "TypeScript twin of the G9SyncData .NET client for Lynx, Node and browsers: the sync engine, the G9RowBatch protocol, the SQLite client store (same schema as the .NET store) and the SignalR transport.",
5
5
  "license": "MIT",
6
6
  "author": "Iman Kari (G9TM)",