velocious 1.0.475 → 1.0.477
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/README.md +3 -0
- package/build/configuration-types.js +4 -0
- package/build/configuration.js +29 -0
- package/build/database/drivers/base.js +16 -0
- package/build/database/drivers/mssql/index.js +13 -0
- package/build/database/drivers/mssql/sql/remove-index.js +6 -0
- package/build/database/drivers/mysql/index.js +13 -0
- package/build/database/drivers/mysql/sql/remove-index.js +6 -0
- package/build/database/drivers/pgsql/index.js +13 -0
- package/build/database/drivers/pgsql/sql/remove-index.js +6 -0
- package/build/database/drivers/sqlite/base.js +13 -0
- package/build/database/drivers/sqlite/sql/remove-index.js +6 -0
- package/build/database/migration/index.js +44 -0
- package/build/database/pool/base-methods-forward.js +1 -0
- package/build/database/query/remove-index-base.js +39 -0
- package/build/database/record/index.js +25 -9
- package/build/environment-handlers/node/cli/commands/generate/base-models.js +36 -0
- package/build/frontend-model-controller.js +632 -1
- package/build/frontend-models/base.js +5 -1
- package/build/frontend-models/resource-definition.js +20 -3
- package/build/http-server/client/websocket-session.js +93 -0
- package/build/routes/hooks/frontend-model-command-route-hook.js +24 -0
- package/build/src/configuration-types.d.ts +20 -0
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +5 -1
- package/build/src/configuration.d.ts +13 -0
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +28 -1
- package/build/src/database/drivers/base.d.ts +20 -0
- package/build/src/database/drivers/base.d.ts.map +1 -1
- package/build/src/database/drivers/base.js +16 -1
- package/build/src/database/drivers/mssql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/index.js +12 -1
- package/build/src/database/drivers/mssql/sql/remove-index.d.ts +4 -0
- package/build/src/database/drivers/mssql/sql/remove-index.d.ts.map +1 -0
- package/build/src/database/drivers/mssql/sql/remove-index.js +5 -0
- package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/index.js +12 -1
- package/build/src/database/drivers/mysql/sql/remove-index.d.ts +4 -0
- package/build/src/database/drivers/mysql/sql/remove-index.d.ts.map +1 -0
- package/build/src/database/drivers/mysql/sql/remove-index.js +5 -0
- package/build/src/database/drivers/pgsql/index.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/index.js +12 -1
- package/build/src/database/drivers/pgsql/sql/remove-index.d.ts +4 -0
- package/build/src/database/drivers/pgsql/sql/remove-index.d.ts.map +1 -0
- package/build/src/database/drivers/pgsql/sql/remove-index.js +5 -0
- package/build/src/database/drivers/sqlite/base.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/base.js +12 -1
- package/build/src/database/drivers/sqlite/sql/remove-index.d.ts +4 -0
- package/build/src/database/drivers/sqlite/sql/remove-index.d.ts.map +1 -0
- package/build/src/database/drivers/sqlite/sql/remove-index.js +5 -0
- package/build/src/database/migration/index.d.ts +25 -0
- package/build/src/database/migration/index.d.ts.map +1 -1
- package/build/src/database/migration/index.js +39 -1
- package/build/src/database/pool/base-methods-forward.d.ts.map +1 -1
- package/build/src/database/pool/base-methods-forward.js +2 -1
- package/build/src/database/query/remove-index-base.d.ts +35 -0
- package/build/src/database/query/remove-index-base.d.ts.map +1 -0
- package/build/src/database/query/remove-index-base.js +34 -0
- package/build/src/database/record/index.d.ts +11 -0
- package/build/src/database/record/index.d.ts.map +1 -1
- package/build/src/database/record/index.js +26 -11
- package/build/src/environment-handlers/node/cli/commands/generate/base-models.d.ts +15 -0
- package/build/src/environment-handlers/node/cli/commands/generate/base-models.d.ts.map +1 -1
- package/build/src/environment-handlers/node/cli/commands/generate/base-models.js +33 -1
- package/build/src/frontend-model-controller.d.ts +156 -0
- package/build/src/frontend-model-controller.d.ts.map +1 -1
- package/build/src/frontend-model-controller.js +555 -2
- package/build/src/frontend-models/base.d.ts +5 -0
- package/build/src/frontend-models/base.d.ts.map +1 -1
- package/build/src/frontend-models/base.js +6 -2
- package/build/src/frontend-models/resource-definition.d.ts.map +1 -1
- package/build/src/frontend-models/resource-definition.js +20 -4
- package/build/src/http-server/client/websocket-session.d.ts +39 -0
- package/build/src/http-server/client/websocket-session.d.ts.map +1 -1
- package/build/src/http-server/client/websocket-session.js +85 -1
- package/build/src/routes/hooks/frontend-model-command-route-hook.d.ts.map +1 -1
- package/build/src/routes/hooks/frontend-model-command-route-hook.js +22 -1
- package/build/src/sync/conflict-strategy.d.ts +83 -0
- package/build/src/sync/conflict-strategy.d.ts.map +1 -0
- package/build/src/sync/conflict-strategy.js +215 -0
- package/build/src/sync/peer-mutation-bundle.d.ts +97 -0
- package/build/src/sync/peer-mutation-bundle.d.ts.map +1 -0
- package/build/src/sync/peer-mutation-bundle.js +215 -0
- package/build/src/sync/server-change-feed.d.ts +265 -0
- package/build/src/sync/server-change-feed.d.ts.map +1 -0
- package/build/src/sync/server-change-feed.js +475 -0
- package/build/sync/conflict-strategy.js +225 -0
- package/build/sync/peer-mutation-bundle.js +232 -0
- package/build/sync/server-change-feed.js +524 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/configuration-types.js +4 -0
- package/src/configuration.js +29 -0
- package/src/database/drivers/base.js +16 -0
- package/src/database/drivers/mssql/index.js +13 -0
- package/src/database/drivers/mssql/sql/remove-index.js +6 -0
- package/src/database/drivers/mysql/index.js +13 -0
- package/src/database/drivers/mysql/sql/remove-index.js +6 -0
- package/src/database/drivers/pgsql/index.js +13 -0
- package/src/database/drivers/pgsql/sql/remove-index.js +6 -0
- package/src/database/drivers/sqlite/base.js +13 -0
- package/src/database/drivers/sqlite/sql/remove-index.js +6 -0
- package/src/database/migration/index.js +44 -0
- package/src/database/pool/base-methods-forward.js +1 -0
- package/src/database/query/remove-index-base.js +39 -0
- package/src/database/record/index.js +25 -9
- package/src/environment-handlers/node/cli/commands/generate/base-models.js +36 -0
- package/src/frontend-model-controller.js +632 -1
- package/src/frontend-models/base.js +5 -1
- package/src/frontend-models/resource-definition.js +20 -3
- package/src/http-server/client/websocket-session.js +93 -0
- package/src/routes/hooks/frontend-model-command-route-hook.js +24 -0
- package/src/sync/conflict-strategy.js +225 -0
- package/src/sync/peer-mutation-bundle.js +232 -0
- package/src/sync/server-change-feed.js +524 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import {createSignedMutation, mutationIdempotencyKey, verifySignedMutation} from "./device-identity.js"
|
|
4
|
+
|
|
5
|
+
const PEER_MUTATION_BUNDLE_FORMAT = "velocious.sync.peer-mutation-bundle.v1"
|
|
6
|
+
const EXPORTABLE_STATUSES = new Set(["pending", "applied-locally", "conflict"])
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Peer mutation bundle exported by a device for offline/P2P transfer.
|
|
10
|
+
* @typedef {object} PeerMutationBundle
|
|
11
|
+
* @property {string} exportedAt - ISO timestamp when the bundle was exported.
|
|
12
|
+
* @property {"velocious.sync.peer-mutation-bundle.v1"} format - Bundle format identifier.
|
|
13
|
+
* @property {PeerMutationBundleEntry[]} mutations - Signed mutations in local sequence order.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* One peer mutation bundle entry.
|
|
18
|
+
* @typedef {object} PeerMutationBundleEntry
|
|
19
|
+
* @property {string} [localRecordId] - Exporting device's local mutation record id.
|
|
20
|
+
* @property {number} [localSequence] - Exporting device's local mutation sequence.
|
|
21
|
+
* @property {import("./device-identity.js").SignedSyncMutation} signedMutation - Device-signed mutation envelope.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Exports local non-terminal mutations as a signed peer-transfer bundle.
|
|
26
|
+
* @param {object} args - Arguments.
|
|
27
|
+
* @param {import("./device-identity.js").DeviceCertificate} args.deviceCertificate - Device certificate for signing records.
|
|
28
|
+
* @param {import("./device-identity.js").SyncJsonWebKey} args.devicePrivateKey - Device private key for signing records.
|
|
29
|
+
* @param {import("./local-mutation-log.js").default} args.mutationLog - Local mutation log.
|
|
30
|
+
* @param {() => Date} [args.now] - Export clock.
|
|
31
|
+
* @param {import("./local-mutation-log.js").LocalMutationStatus[]} [args.statuses] - Statuses to export.
|
|
32
|
+
* @returns {Promise<PeerMutationBundle>} - Signed peer mutation bundle.
|
|
33
|
+
*/
|
|
34
|
+
export async function exportPeerMutationBundle({deviceCertificate, devicePrivateKey, mutationLog, now = () => new Date(), statuses}) {
|
|
35
|
+
const selectedStatuses = normalizeExportStatuses(statuses)
|
|
36
|
+
const timestamp = isoTimestamp(now(), "exportedAt")
|
|
37
|
+
const records = (await mutationLog.records())
|
|
38
|
+
.filter((record) => selectedStatuses.has(record.status))
|
|
39
|
+
.sort((left, right) => left.sequence - right.sequence)
|
|
40
|
+
const mutations = []
|
|
41
|
+
|
|
42
|
+
for (const record of records) {
|
|
43
|
+
mutations.push({
|
|
44
|
+
localRecordId: record.id,
|
|
45
|
+
localSequence: record.sequence,
|
|
46
|
+
signedMutation: await createSignedMutation({
|
|
47
|
+
deviceCertificate,
|
|
48
|
+
devicePrivateKey,
|
|
49
|
+
mutation: record.mutation
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
exportedAt: timestamp,
|
|
56
|
+
format: PEER_MUTATION_BUNDLE_FORMAT,
|
|
57
|
+
mutations
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Imports verified peer mutations into the local mutation log.
|
|
63
|
+
* @param {object} args - Arguments.
|
|
64
|
+
* @param {import("./device-identity.js").SyncJsonWebKey} args.backendPublicKey - Backend public key for verifying peer certificates.
|
|
65
|
+
* @param {PeerMutationBundle} args.bundle - Peer bundle to import.
|
|
66
|
+
* @param {import("./local-mutation-log.js").default} args.mutationLog - Local mutation log.
|
|
67
|
+
* @param {Date} [args.now] - Verification time.
|
|
68
|
+
* @returns {Promise<{imported: {clientMutationId: string, idempotencyKey: string, localRecordId: string}[], rejected: {errorMessage: string, index: number}[], skipped: {clientMutationId: string, idempotencyKey: string, localRecordId: string, reason: "duplicate"}[]}>} - Import result.
|
|
69
|
+
*/
|
|
70
|
+
export async function importPeerMutationBundle({backendPublicKey, bundle, mutationLog, now = new Date()}) {
|
|
71
|
+
const normalizedBundle = normalizePeerMutationBundle(bundle)
|
|
72
|
+
const existingRecords = await mutationLog.records()
|
|
73
|
+
const existingByIdempotencyKey = new Map(existingRecords.map((record) => [mutationIdempotencyKey({mutation: record.mutation}), record]))
|
|
74
|
+
/** @type {{clientMutationId: string, idempotencyKey: string, localRecordId: string}[]} */
|
|
75
|
+
const imported = []
|
|
76
|
+
/** @type {{errorMessage: string, index: number}[]} */
|
|
77
|
+
const rejected = []
|
|
78
|
+
/** @type {{clientMutationId: string, idempotencyKey: string, localRecordId: string, reason: "duplicate"}[]} */
|
|
79
|
+
const skipped = []
|
|
80
|
+
|
|
81
|
+
for (const [index, entry] of normalizedBundle.mutations.entries()) {
|
|
82
|
+
const mutation = await verifiedBundleMutation({backendPublicKey, entry, index, now, rejected})
|
|
83
|
+
|
|
84
|
+
if (!mutation) continue
|
|
85
|
+
|
|
86
|
+
const idempotencyKey = mutationIdempotencyKey(entry.signedMutation)
|
|
87
|
+
const duplicateRecord = existingByIdempotencyKey.get(idempotencyKey)
|
|
88
|
+
|
|
89
|
+
if (duplicateRecord) {
|
|
90
|
+
skipped.push({
|
|
91
|
+
clientMutationId: mutation.clientMutationId,
|
|
92
|
+
idempotencyKey,
|
|
93
|
+
localRecordId: duplicateRecord.id,
|
|
94
|
+
reason: /** @type {"duplicate"} */ ("duplicate")
|
|
95
|
+
})
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const record = await mutationLog.append({mutation})
|
|
100
|
+
const updated = await mutationLog.updateStatus({id: record.id, status: "peer-applied"})
|
|
101
|
+
existingByIdempotencyKey.set(idempotencyKey, updated)
|
|
102
|
+
imported.push({clientMutationId: mutation.clientMutationId, idempotencyKey, localRecordId: updated.id})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {imported, rejected, skipped}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Verifies a bundle mutation and records normal per-entry verification rejections.
|
|
110
|
+
* Storage writes intentionally happen outside this helper so storage failures escape import.
|
|
111
|
+
* @param {object} args - Arguments.
|
|
112
|
+
* @param {import("./device-identity.js").SyncJsonWebKey} args.backendPublicKey - Backend public key.
|
|
113
|
+
* @param {PeerMutationBundleEntry} args.entry - Bundle entry.
|
|
114
|
+
* @param {number} args.index - Entry index.
|
|
115
|
+
* @param {Date} args.now - Verification time.
|
|
116
|
+
* @param {{errorMessage: string, index: number}[]} args.rejected - Rejection accumulator.
|
|
117
|
+
* @returns {Promise<import("./device-identity.js").SyncMutation | null>} - Verified mutation, or null when rejected.
|
|
118
|
+
*/
|
|
119
|
+
async function verifiedBundleMutation({backendPublicKey, entry, index, now, rejected}) {
|
|
120
|
+
try {
|
|
121
|
+
return await verifySignedMutation({backendPublicKey, now, signedMutation: entry.signedMutation})
|
|
122
|
+
} catch (error) {
|
|
123
|
+
rejected.push({errorMessage: errorMessage(error), index})
|
|
124
|
+
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Normalizes export status filters.
|
|
131
|
+
* @param {import("./local-mutation-log.js").LocalMutationStatus[] | undefined} statuses - Optional statuses.
|
|
132
|
+
* @returns {Set<import("./local-mutation-log.js").LocalMutationStatus>} - Status set.
|
|
133
|
+
*/
|
|
134
|
+
function normalizeExportStatuses(statuses) {
|
|
135
|
+
if (statuses === undefined) return /** @type {Set<import("./local-mutation-log.js").LocalMutationStatus>} */ (new Set(EXPORTABLE_STATUSES))
|
|
136
|
+
if (!Array.isArray(statuses)) throw new Error("Expected peer mutation export statuses array")
|
|
137
|
+
|
|
138
|
+
const normalized = new Set()
|
|
139
|
+
|
|
140
|
+
for (const status of statuses) {
|
|
141
|
+
if (!EXPORTABLE_STATUSES.has(status)) throw new Error(`Unsupported peer mutation export status '${String(status)}'`)
|
|
142
|
+
normalized.add(status)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return /** @type {Set<import("./local-mutation-log.js").LocalMutationStatus>} */ (normalized)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Normalizes a peer mutation bundle.
|
|
150
|
+
* @param {PeerMutationBundle} bundle - Bundle value.
|
|
151
|
+
* @returns {PeerMutationBundle} - Normalized bundle.
|
|
152
|
+
*/
|
|
153
|
+
function normalizePeerMutationBundle(bundle) {
|
|
154
|
+
if (!bundle || typeof bundle !== "object" || Array.isArray(bundle)) throw new Error("Expected peer mutation bundle object")
|
|
155
|
+
if (bundle.format !== PEER_MUTATION_BUNDLE_FORMAT) throw new Error(`Unsupported peer mutation bundle format '${String(bundle.format)}'`)
|
|
156
|
+
isoTimestamp(new Date(requiredString(bundle.exportedAt, "exportedAt")), "exportedAt")
|
|
157
|
+
if (!Array.isArray(bundle.mutations)) throw new Error("Expected peer mutation bundle mutations array")
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
exportedAt: bundle.exportedAt,
|
|
161
|
+
format: bundle.format,
|
|
162
|
+
mutations: bundle.mutations.map((entry, index) => normalizePeerMutationBundleEntry(entry, index))
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Normalizes one peer mutation bundle entry.
|
|
168
|
+
* @param {PeerMutationBundleEntry} entry - Bundle entry.
|
|
169
|
+
* @param {number} index - Entry index.
|
|
170
|
+
* @returns {PeerMutationBundleEntry} - Normalized entry.
|
|
171
|
+
*/
|
|
172
|
+
function normalizePeerMutationBundleEntry(entry, index) {
|
|
173
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) throw new Error(`Expected peer mutation bundle entry ${index} object`)
|
|
174
|
+
if (!entry.signedMutation || typeof entry.signedMutation !== "object" || Array.isArray(entry.signedMutation)) throw new Error(`Expected peer mutation bundle entry ${index} signedMutation object`)
|
|
175
|
+
|
|
176
|
+
/** @type {PeerMutationBundleEntry} */
|
|
177
|
+
const normalized = {
|
|
178
|
+
signedMutation: entry.signedMutation
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (entry.localRecordId !== undefined) normalized.localRecordId = requiredString(entry.localRecordId, `entry ${index} localRecordId`)
|
|
182
|
+
if (entry.localSequence !== undefined) normalized.localSequence = positiveInteger(entry.localSequence, `entry ${index} localSequence`)
|
|
183
|
+
|
|
184
|
+
return normalized
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Requires a non-empty string.
|
|
189
|
+
* @param {unknown} value - Value.
|
|
190
|
+
* @param {string} label - Label.
|
|
191
|
+
* @returns {string} - String.
|
|
192
|
+
*/
|
|
193
|
+
function requiredString(value, label) {
|
|
194
|
+
if (typeof value !== "string" || value.length < 1) throw new Error(`Expected peer mutation bundle ${label}`)
|
|
195
|
+
|
|
196
|
+
return value
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Requires a positive integer.
|
|
201
|
+
* @param {unknown} value - Value.
|
|
202
|
+
* @param {string} label - Label.
|
|
203
|
+
* @returns {number} - Integer.
|
|
204
|
+
*/
|
|
205
|
+
function positiveInteger(value, label) {
|
|
206
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 1) throw new Error(`Expected peer mutation bundle ${label} positive integer`)
|
|
207
|
+
|
|
208
|
+
return value
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Returns an ISO timestamp from a valid Date.
|
|
213
|
+
* @param {Date} date - Date.
|
|
214
|
+
* @param {string} label - Label.
|
|
215
|
+
* @returns {string} - ISO timestamp.
|
|
216
|
+
*/
|
|
217
|
+
function isoTimestamp(date, label) {
|
|
218
|
+
if (!(date instanceof Date) || Number.isNaN(date.getTime())) throw new Error(`Invalid peer mutation bundle ${label}`)
|
|
219
|
+
|
|
220
|
+
return date.toISOString()
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Returns a safe error message.
|
|
225
|
+
* @param {unknown} error - Error.
|
|
226
|
+
* @returns {string} - Message.
|
|
227
|
+
*/
|
|
228
|
+
function errorMessage(error) {
|
|
229
|
+
if (error instanceof Error) return error.message
|
|
230
|
+
|
|
231
|
+
return String(error)
|
|
232
|
+
}
|