@peers-app/peers-sdk 0.27.3 → 0.27.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/contracts/contract-proxy.d.ts +7 -0
- package/dist/contracts/contract-proxy.js +19 -2
- package/dist/contracts/contract-transport.d.ts +2 -1
- package/dist/contracts/contract-transport.js +24 -2
- package/dist/contracts/create-local-consumer.js +16 -1
- package/dist/contracts/index.d.ts +2 -0
- package/dist/contracts/index.js +2 -0
- package/dist/contracts/system/index.d.ts +2 -0
- package/dist/contracts/system/index.js +2 -0
- package/dist/contracts/system/invites-contract.js +2 -1
- package/dist/contracts/system/sync-check-contract.d.ts +97 -0
- package/dist/contracts/system/sync-check-contract.js +115 -0
- package/dist/contracts/tool-arguments.d.ts +15 -0
- package/dist/contracts/tool-arguments.js +41 -0
- package/dist/contracts/tool-catalog-service.js +2 -12
- package/dist/contracts/tool-catalog.js +6 -2
- package/dist/data/change-tracking.d.ts +40 -0
- package/dist/data/change-tracking.js +89 -0
- package/dist/data/orm/types.d.ts +24 -7
- package/dist/data/orm/types.js +97 -24
- package/dist/define/manifest.js +6 -34
- package/dist/device/binary-peer-connection-v2.d.ts +7 -15
- package/dist/device/binary-peer-connection-v2.js +14 -30
- package/dist/device/connection.d.ts +34 -43
- package/dist/device/connection.js +188 -186
- package/dist/device/device.d.ts +1 -8
- package/dist/device/device.js +1 -30
- package/dist/device/socket.type.d.ts +6 -12
- package/dist/package-loader/package-loader.js +27 -22
- package/dist/system-ids.d.ts +5 -0
- package/dist/system-ids.js +6 -1
- package/package.json +4 -2
- package/dist/context/user-context-identity.test.d.ts +0 -1
- package/dist/context/user-context-identity.test.js +0 -220
- package/dist/contracts/__tests__/action-dispatch.test.d.ts +0 -1
- package/dist/contracts/__tests__/action-dispatch.test.js +0 -319
- package/dist/contracts/__tests__/also-implements.test.d.ts +0 -1
- package/dist/contracts/__tests__/also-implements.test.js +0 -110
- package/dist/contracts/__tests__/builder.test.d.ts +0 -1
- package/dist/contracts/__tests__/builder.test.js +0 -669
- package/dist/contracts/__tests__/contract-events.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-events.test.js +0 -199
- package/dist/contracts/__tests__/contract-observables.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-observables.test.js +0 -290
- package/dist/contracts/__tests__/contract-provider-router.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-provider-router.test.js +0 -315
- package/dist/contracts/__tests__/contract-proxy.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-proxy.test.js +0 -657
- package/dist/contracts/__tests__/contract-resolvers.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-resolvers.test.js +0 -758
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-subscription-lifecycle.test.js +0 -353
- package/dist/contracts/__tests__/contract-table-events.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-table-events.test.js +0 -209
- package/dist/contracts/__tests__/contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/contract-transport.test.js +0 -163
- package/dist/contracts/__tests__/extract.test.d.ts +0 -1
- package/dist/contracts/__tests__/extract.test.js +0 -145
- package/dist/contracts/__tests__/frozen-contracts.test.d.ts +0 -1
- package/dist/contracts/__tests__/frozen-contracts.test.js +0 -191
- package/dist/contracts/__tests__/generic-tool-dispatch.test.d.ts +0 -1
- package/dist/contracts/__tests__/generic-tool-dispatch.test.js +0 -130
- package/dist/contracts/__tests__/host-contract-client.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-client.test.js +0 -793
- package/dist/contracts/__tests__/host-contract-consumer.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-consumer.test.js +0 -163
- package/dist/contracts/__tests__/host-contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/host-contract-transport.test.js +0 -66
- package/dist/contracts/__tests__/integration.test.d.ts +0 -1
- package/dist/contracts/__tests__/integration.test.js +0 -348
- package/dist/contracts/__tests__/legacy-removal.test.d.ts +0 -1
- package/dist/contracts/__tests__/legacy-removal.test.js +0 -128
- package/dist/contracts/__tests__/persistent-registry.test.d.ts +0 -1
- package/dist/contracts/__tests__/persistent-registry.test.js +0 -109
- package/dist/contracts/__tests__/playground.test.d.ts +0 -1
- package/dist/contracts/__tests__/playground.test.js +0 -216
- package/dist/contracts/__tests__/registry.test.d.ts +0 -1
- package/dist/contracts/__tests__/registry.test.js +0 -238
- package/dist/contracts/__tests__/remote-contract-transport.test.d.ts +0 -1
- package/dist/contracts/__tests__/remote-contract-transport.test.js +0 -119
- package/dist/contracts/__tests__/system-contracts.test.d.ts +0 -1
- package/dist/contracts/__tests__/system-contracts.test.js +0 -247
- package/dist/contracts/__tests__/target-dispatch.d.ts +0 -11
- package/dist/contracts/__tests__/target-dispatch.js +0 -24
- package/dist/contracts/__tests__/tool-catalog-inventory.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog-inventory.test.js +0 -44
- package/dist/contracts/__tests__/tool-catalog-service.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog-service.test.js +0 -482
- package/dist/contracts/__tests__/tool-catalog.test.d.ts +0 -1
- package/dist/contracts/__tests__/tool-catalog.test.js +0 -1199
- package/dist/contracts/__tests__/validate.test.d.ts +0 -1
- package/dist/contracts/__tests__/validate.test.js +0 -991
- package/dist/contracts/__tests__/workflow-context.test.d.ts +0 -1
- package/dist/contracts/__tests__/workflow-context.test.js +0 -50
- package/dist/data/access-level.test.d.ts +0 -1
- package/dist/data/access-level.test.js +0 -53
- package/dist/data/change-tracking.test.d.ts +0 -1
- package/dist/data/change-tracking.test.js +0 -62
- package/dist/data/files/files.test.d.ts +0 -1
- package/dist/data/files/files.test.js +0 -770
- package/dist/data/group-members.test.d.ts +0 -1
- package/dist/data/group-members.test.js +0 -336
- package/dist/data/group-share.test.d.ts +0 -1
- package/dist/data/group-share.test.js +0 -110
- package/dist/data/groups.test.d.ts +0 -1
- package/dist/data/groups.test.js +0 -153
- package/dist/data/orm/cursor.test.d.ts +0 -1
- package/dist/data/orm/cursor.test.js +0 -314
- package/dist/data/orm/data-query.mongo.test.d.ts +0 -1
- package/dist/data/orm/data-query.mongo.test.js +0 -367
- package/dist/data/orm/data-query.sqlite.test.d.ts +0 -1
- package/dist/data/orm/data-query.sqlite.test.js +0 -365
- package/dist/data/orm/data-query.test.d.ts +0 -1
- package/dist/data/orm/data-query.test.js +0 -545
- package/dist/data/orm/dependency-injection.test.d.ts +0 -1
- package/dist/data/orm/dependency-injection.test.js +0 -171
- package/dist/data/orm/event-registry.test.d.ts +0 -1
- package/dist/data/orm/event-registry.test.js +0 -44
- package/dist/data/orm/multi-cursors.test.d.ts +0 -1
- package/dist/data/orm/multi-cursors.test.js +0 -463
- package/dist/data/orm/sql.data-source.test.d.ts +0 -1
- package/dist/data/orm/sql.data-source.test.js +0 -731
- package/dist/data/orm/table-container-events.test.d.ts +0 -1
- package/dist/data/orm/table-container-events.test.js +0 -101
- package/dist/data/orm/table-container.test.d.ts +0 -1
- package/dist/data/orm/table-container.test.js +0 -273
- package/dist/data/orm/table.event-source.test.d.ts +0 -1
- package/dist/data/orm/table.event-source.test.js +0 -331
- package/dist/data/orm/types.test.d.ts +0 -1
- package/dist/data/orm/types.test.js +0 -409
- package/dist/data/package-table-identity.test.d.ts +0 -1
- package/dist/data/package-table-identity.test.js +0 -46
- package/dist/data/package-version-permissions.test.d.ts +0 -1
- package/dist/data/package-version-permissions.test.js +0 -107
- package/dist/data/package-version-resolver.test.d.ts +0 -4
- package/dist/data/package-version-resolver.test.js +0 -543
- package/dist/data/package-versions.test.d.ts +0 -1
- package/dist/data/package-versions.test.js +0 -227
- package/dist/data/persistent-vars.conflict.test.d.ts +0 -1
- package/dist/data/persistent-vars.conflict.test.js +0 -126
- package/dist/data/persistent-vars.migration.test.d.ts +0 -1
- package/dist/data/persistent-vars.migration.test.js +0 -133
- package/dist/data/persistent-vars.package-scope.test.d.ts +0 -1
- package/dist/data/persistent-vars.package-scope.test.js +0 -27
- package/dist/data/sync-recovery-queue.test.d.ts +0 -1
- package/dist/data/sync-recovery-queue.test.js +0 -215
- package/dist/data/table-signing.test.d.ts +0 -1
- package/dist/data/table-signing.test.js +0 -118
- package/dist/data/tool-access.test.d.ts +0 -1
- package/dist/data/tool-access.test.js +0 -166
- package/dist/data/user-permissions.test.d.ts +0 -1
- package/dist/data/user-permissions.test.js +0 -305
- package/dist/data/user-trust-levels.test.d.ts +0 -1
- package/dist/data/user-trust-levels.test.js +0 -37
- package/dist/define/also-implements.test.d.ts +0 -1
- package/dist/define/also-implements.test.js +0 -123
- package/dist/define/declare-contract.test.d.ts +0 -1
- package/dist/define/declare-contract.test.js +0 -573
- package/dist/define/define.test.d.ts +0 -1
- package/dist/define/define.test.js +0 -1010
- package/dist/define/empty-fields.test.d.ts +0 -1
- package/dist/define/empty-fields.test.js +0 -55
- package/dist/define/events-observables.test.d.ts +0 -1
- package/dist/define/events-observables.test.js +0 -1013
- package/dist/define/serve-members.test.d.ts +0 -1
- package/dist/define/serve-members.test.js +0 -630
- package/dist/define/vars.test.d.ts +0 -1
- package/dist/define/vars.test.js +0 -39
- package/dist/device/binary-peer-connection-v2.test.d.ts +0 -1
- package/dist/device/binary-peer-connection-v2.test.js +0 -730
- package/dist/device/binary-peer-connection.d.ts +0 -58
- package/dist/device/binary-peer-connection.js +0 -516
- package/dist/device/binary-peer-connection.test.d.ts +0 -1
- package/dist/device/binary-peer-connection.test.js +0 -184
- package/dist/device/connection.test.d.ts +0 -1
- package/dist/device/connection.test.js +0 -613
- package/dist/device/device-election.test.d.ts +0 -1
- package/dist/device/device-election.test.js +0 -516
- package/dist/device/device.test.d.ts +0 -1
- package/dist/device/device.test.js +0 -243
- package/dist/device/frame-cipher.test.d.ts +0 -1
- package/dist/device/frame-cipher.test.js +0 -219
- package/dist/device/get-trust-level-fn.test.d.ts +0 -1
- package/dist/device/get-trust-level-fn.test.js +0 -685
- package/dist/device/streamed-socket.d.ts +0 -23
- package/dist/device/streamed-socket.js +0 -255
- package/dist/device/streamed-socket.test.d.ts +0 -1
- package/dist/device/streamed-socket.test.js +0 -216
- package/dist/device/tx-encoding.test.d.ts +0 -1
- package/dist/device/tx-encoding.test.js +0 -295
- package/dist/device-pairing/cpace.test.d.ts +0 -1
- package/dist/device-pairing/cpace.test.js +0 -116
- package/dist/device-pairing/device-pairing.test.d.ts +0 -1
- package/dist/device-pairing/device-pairing.test.js +0 -211
- package/dist/hlc.test.d.ts +0 -1
- package/dist/hlc.test.js +0 -45
- package/dist/host-credentials.test.d.ts +0 -1
- package/dist/host-credentials.test.js +0 -88
- package/dist/identity/account-email.test.d.ts +0 -1
- package/dist/identity/account-email.test.js +0 -59
- package/dist/identity/anchor-document.test.d.ts +0 -1
- package/dist/identity/anchor-document.test.js +0 -16
- package/dist/identity/anchor-resolution.test.d.ts +0 -1
- package/dist/identity/anchor-resolution.test.js +0 -130
- package/dist/identity/key-change.test.d.ts +0 -1
- package/dist/identity/key-change.test.js +0 -811
- package/dist/invites/group-approval.test.d.ts +0 -1
- package/dist/invites/group-approval.test.js +0 -223
- package/dist/invites/invite-token.test.d.ts +0 -1
- package/dist/invites/invite-token.test.js +0 -123
- package/dist/keys.test.d.ts +0 -1
- package/dist/keys.test.js +0 -336
- package/dist/logging/console-logger.test.d.ts +0 -1
- package/dist/logging/console-logger.test.js +0 -300
- package/dist/logging/console-logs.table.test.d.ts +0 -1
- package/dist/logging/console-logs.table.test.js +0 -156
- package/dist/observable.test.d.ts +0 -1
- package/dist/observable.test.js +0 -309
- package/dist/package-build/build-package.test.d.ts +0 -1
- package/dist/package-build/build-package.test.js +0 -204
- package/dist/package-build/contract-add.test.d.ts +0 -1
- package/dist/package-build/contract-add.test.js +0 -467
- package/dist/package-build/freeze-contracts.test.d.ts +0 -1
- package/dist/package-build/freeze-contracts.test.js +0 -355
- package/dist/package-installer/package-author-signing.test.d.ts +0 -1
- package/dist/package-installer/package-author-signing.test.js +0 -200
- package/dist/package-installer/package-cloner.test.d.ts +0 -1
- package/dist/package-installer/package-cloner.test.js +0 -278
- package/dist/package-installer/package-creator.test.d.ts +0 -1
- package/dist/package-installer/package-creator.test.js +0 -908
- package/dist/package-installer/package-dev-tools.test.d.ts +0 -1
- package/dist/package-installer/package-dev-tools.test.js +0 -64
- package/dist/package-installer/package-installer.test.d.ts +0 -1
- package/dist/package-installer/package-installer.test.js +0 -882
- package/dist/package-installer/package-propagation.test.d.ts +0 -1
- package/dist/package-installer/package-propagation.test.js +0 -1269
- package/dist/package-installer/package-publisher.test.d.ts +0 -1
- package/dist/package-installer/package-publisher.test.js +0 -153
- package/dist/package-installer/package-remote-checker.test.d.ts +0 -1
- package/dist/package-installer/package-remote-checker.test.js +0 -334
- package/dist/package-installer/package-seed-installer.test.d.ts +0 -1
- package/dist/package-installer/package-seed-installer.test.js +0 -123
- package/dist/package-installer/package-tarball.test.d.ts +0 -1
- package/dist/package-installer/package-tarball.test.js +0 -75
- package/dist/package-installer/peers-core-installer.test.d.ts +0 -1
- package/dist/package-installer/peers-core-installer.test.js +0 -49
- package/dist/package-installer/peers-core-trust-anchor.test.d.ts +0 -1
- package/dist/package-installer/peers-core-trust-anchor.test.js +0 -155
- package/dist/package-loader/contract-package-loader.test.d.ts +0 -1
- package/dist/package-loader/contract-package-loader.test.js +0 -274
- package/dist/package-loader/host-authority-loading.test.d.ts +0 -1
- package/dist/package-loader/host-authority-loading.test.js +0 -104
- package/dist/package-loader/host-dispatch-policy.test.d.ts +0 -1
- package/dist/package-loader/host-dispatch-policy.test.js +0 -56
- package/dist/package-loader/isolated-artifact.test.d.ts +0 -1
- package/dist/package-loader/isolated-artifact.test.js +0 -688
- package/dist/package-loader/isolated-persistent-var-gateway.test.d.ts +0 -1
- package/dist/package-loader/isolated-persistent-var-gateway.test.js +0 -56
- package/dist/package-loader/isolated-table-gateway.test.d.ts +0 -1
- package/dist/package-loader/isolated-table-gateway.test.js +0 -633
- package/dist/package-loader/package-loader.test.d.ts +0 -1
- package/dist/package-loader/package-loader.test.js +0 -1764
- package/dist/package-loader/resolve-consumed-contracts.test.d.ts +0 -1
- package/dist/package-loader/resolve-consumed-contracts.test.js +0 -166
- package/dist/serial-json.compat.test.d.ts +0 -1
- package/dist/serial-json.compat.test.js +0 -369
- package/dist/serial-json.test.d.ts +0 -1
- package/dist/serial-json.test.js +0 -401
- package/dist/testing/mock-host-contract.test.d.ts +0 -1
- package/dist/testing/mock-host-contract.test.js +0 -782
- package/dist/utils.test.d.ts +0 -1
- package/dist/utils.test.js +0 -232
|
@@ -37,6 +37,12 @@ exports.changeRecordSchema = zod_1.z.object({
|
|
|
37
37
|
.describe("Local apply / sync-visibility time; assigned at insert, incoming values replaced"),
|
|
38
38
|
supersededAt: zod_1.z.number().optional().describe("When this change was superseded; unset if active"),
|
|
39
39
|
});
|
|
40
|
+
/** Five id characters starting at `start`, read as one base-128 number. */
|
|
41
|
+
function idTailNumber(start) {
|
|
42
|
+
return [0, 1, 2, 3, 4]
|
|
43
|
+
.map((i) => `${128 ** (4 - i)} * unicode(substr(changeId, ${start + i}, 1))`)
|
|
44
|
+
.join(" + ");
|
|
45
|
+
}
|
|
40
46
|
const changeTrackingV2TableMetaData = {
|
|
41
47
|
name: "ChangeTrackingV2",
|
|
42
48
|
primaryKeyName: "changeId",
|
|
@@ -228,6 +234,89 @@ class ChangeTrackingTable extends orm_1.SQLDataSource {
|
|
|
228
234
|
`, changeIds);
|
|
229
235
|
return missingChangeIds.map((d) => d.changeId);
|
|
230
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Count active changes per table, or per table and change-id prefix, with a digest of their ids.
|
|
239
|
+
* Two devices holding the same active changes get the same results. One scan of the
|
|
240
|
+
* `(tableName, supersededAt)` index, which holds every change id.
|
|
241
|
+
*
|
|
242
|
+
* @param request - Which changes to count and how to group them
|
|
243
|
+
* @returns One row per table (and prefix) that has any active changes
|
|
244
|
+
*/
|
|
245
|
+
async countChanges(request) {
|
|
246
|
+
if (!request ||
|
|
247
|
+
typeof request.before !== "string" ||
|
|
248
|
+
!/^[0-9a-z]{1,25}$/i.test(request.before) ||
|
|
249
|
+
(request.tableName !== undefined &&
|
|
250
|
+
(typeof request.tableName !== "string" ||
|
|
251
|
+
!request.tableName ||
|
|
252
|
+
request.tableName.length > 256)) ||
|
|
253
|
+
(request.prefix !== undefined &&
|
|
254
|
+
(typeof request.prefix !== "string" || !/^[0-9a-z]{0,25}$/i.test(request.prefix))) ||
|
|
255
|
+
(request.prefixLength !== undefined &&
|
|
256
|
+
(!Number.isInteger(request.prefixLength) ||
|
|
257
|
+
request.prefixLength < 1 ||
|
|
258
|
+
request.prefixLength > 25 ||
|
|
259
|
+
!request.tableName ||
|
|
260
|
+
request.prefixLength <= (request.prefix?.length ?? 0))) ||
|
|
261
|
+
(request.afterPrefix !== undefined &&
|
|
262
|
+
(typeof request.afterPrefix !== "string" ||
|
|
263
|
+
!request.tableName ||
|
|
264
|
+
!/^[0-9a-z]{1,25}$/i.test(request.afterPrefix))) ||
|
|
265
|
+
(request.excludedTableNames !== undefined &&
|
|
266
|
+
(!Array.isArray(request.excludedTableNames) ||
|
|
267
|
+
request.excludedTableNames.some((name) => typeof name !== "string" || name.length > 256)))) {
|
|
268
|
+
throw new Error("Invalid countChanges request");
|
|
269
|
+
}
|
|
270
|
+
await this.initTable();
|
|
271
|
+
const { before, excludedTableNames = [], tableName, prefix, prefixLength, afterPrefix, } = request;
|
|
272
|
+
const conditions = ["supersededAt IS NULL", "changeId < ?"];
|
|
273
|
+
const params = [before];
|
|
274
|
+
// Without one table, list the table names with a seek per name. Without index statistics,
|
|
275
|
+
// SQLite would otherwise read every row through the (supersededAt, ...) index, which lacks
|
|
276
|
+
// tableName, instead of scanning the covering one.
|
|
277
|
+
let tableNames = "";
|
|
278
|
+
if (tableName !== undefined) {
|
|
279
|
+
conditions.push("tableName = ?");
|
|
280
|
+
params.push(tableName);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
tableNames = `WITH RECURSIVE names(name) AS (
|
|
284
|
+
SELECT MIN(tableName) FROM "${this.tableName}"
|
|
285
|
+
UNION ALL
|
|
286
|
+
SELECT (SELECT MIN(tableName) FROM "${this.tableName}" WHERE tableName > names.name)
|
|
287
|
+
FROM names WHERE names.name IS NOT NULL
|
|
288
|
+
)`;
|
|
289
|
+
conditions.push("tableName IN (SELECT name FROM names)");
|
|
290
|
+
}
|
|
291
|
+
if (excludedTableNames.length) {
|
|
292
|
+
conditions.push(`tableName NOT IN (${excludedTableNames.map(() => "?").join(", ")})`);
|
|
293
|
+
params.push(...excludedTableNames);
|
|
294
|
+
}
|
|
295
|
+
if (prefix) {
|
|
296
|
+
// Change ids are lowercase base36, so every id with this prefix sorts below `prefix~`.
|
|
297
|
+
conditions.push("changeId >= ?", "changeId < ?");
|
|
298
|
+
params.push(prefix, `${prefix}~`);
|
|
299
|
+
}
|
|
300
|
+
if (afterPrefix) {
|
|
301
|
+
conditions.push("changeId > ?");
|
|
302
|
+
params.push(`${afterPrefix}~`);
|
|
303
|
+
}
|
|
304
|
+
const length = prefixLength ?? 0;
|
|
305
|
+
const prefixColumn = length ? `, substr(changeId, 1, ${length}) AS prefix` : "";
|
|
306
|
+
const rows = await this.db.all(`${tableNames}
|
|
307
|
+
SELECT tableName${prefixColumn}, COUNT(*) AS count,
|
|
308
|
+
CAST(SUM(${idTailNumber(16)}) AS TEXT) || ':' || CAST(SUM(${idTailNumber(21)}) AS TEXT) AS sums
|
|
309
|
+
FROM "${this.tableName}"
|
|
310
|
+
WHERE ${conditions.join(" AND ")}
|
|
311
|
+
GROUP BY tableName${length ? ", prefix" : ""}
|
|
312
|
+
ORDER BY tableName${length ? ", prefix LIMIT 500" : ""}`, params);
|
|
313
|
+
return rows.map((row) => ({
|
|
314
|
+
tableName: row.tableName,
|
|
315
|
+
...(length ? { prefix: row.prefix } : {}),
|
|
316
|
+
count: Number(row.count),
|
|
317
|
+
sums: String(row.sums),
|
|
318
|
+
}));
|
|
319
|
+
}
|
|
231
320
|
/**
|
|
232
321
|
* Mark every still-active change for `recordId` whose `createdAt` is older
|
|
233
322
|
* than `supersededAt`. Used after a root snapshot, delete, or restore.
|
package/dist/data/orm/types.d.ts
CHANGED
|
@@ -26,24 +26,33 @@ export interface ITableMetaData {
|
|
|
26
26
|
versionNumber?: number;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
* Convert field metadata to the runtime Zod schema the Runtime validates
|
|
30
|
-
* tool arguments.
|
|
29
|
+
* Convert field metadata to the runtime Zod schema the Runtime validates table rows with.
|
|
31
30
|
*
|
|
32
31
|
* It checks types only: `type`, `isArray`, and `optional`. An object field accepts any object
|
|
33
|
-
* and keeps its keys; nested `fields` and `enumValues` aren't checked, so a row
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* {@link withoutEmptyFields}.
|
|
32
|
+
* and keeps its keys; nested `fields` and `enumValues` aren't checked, so a row from a package
|
|
33
|
+
* built against a newer shape isn't refused. A tool's arguments are checked with the stricter
|
|
34
|
+
* {@link fieldsToArgumentsSchema}, and {@link fieldsToShapeSchema} builds the full shape. An
|
|
35
|
+
* optional field refuses `null`: remove empty fields first with {@link withoutEmptyFields}.
|
|
37
36
|
*
|
|
38
37
|
* @param fields Serializable field definitions.
|
|
39
38
|
* @param codeSchema Optional author schema whose refinements are intersected.
|
|
40
39
|
*/
|
|
41
40
|
export declare function fieldsToSchema(fields: IField[], codeSchema?: z.AnyZodObject): z.AnyZodObject;
|
|
41
|
+
/**
|
|
42
|
+
* Convert a tool's input fields to the Zod schema the Runtime checks its arguments with before
|
|
43
|
+
* the provider runs. Like {@link fieldsToSchema}, and also: a required field must be present
|
|
44
|
+
* whatever its type (`any` included), and a field's `enumValues` are enforced. A contract
|
|
45
|
+
* version's fields don't change once it is stable, so a caller and its provider agree on them.
|
|
46
|
+
* Nested object `fields` still aren't checked, and keys no field names pass.
|
|
47
|
+
*
|
|
48
|
+
* @param fields A tool's input fields.
|
|
49
|
+
*/
|
|
50
|
+
export declare function fieldsToArgumentsSchema(fields: IField[]): z.AnyZodObject;
|
|
42
51
|
/**
|
|
43
52
|
* Convert field metadata to a Zod schema with every shape the IR carries: nested object
|
|
44
53
|
* `fields` and `enumValues`, as well as types. Objects keep keys their `fields` don't list. For
|
|
45
54
|
* describing a tool's arguments to an assistant's model; the Runtime validates with
|
|
46
|
-
* {@link
|
|
55
|
+
* {@link fieldsToArgumentsSchema}.
|
|
47
56
|
*
|
|
48
57
|
* @param fields Serializable field definitions.
|
|
49
58
|
*/
|
|
@@ -72,6 +81,14 @@ export declare function withoutEmptyFields<T>(fields: IField[], value: T): T;
|
|
|
72
81
|
* @param fields A table definition's fields.
|
|
73
82
|
*/
|
|
74
83
|
export declare function comparableTableFields(fields: IField[]): Partial<IField>[];
|
|
84
|
+
/**
|
|
85
|
+
* The scalar values an enum, a native enum, a literal, or a union of them allows; undefined for
|
|
86
|
+
* anything else.
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
* @param schema A field's Zod schema, unwrapped to its value type.
|
|
90
|
+
*/
|
|
91
|
+
export declare function zodEnumValues(schema: z.ZodTypeAny): (string | number | boolean)[] | undefined;
|
|
75
92
|
/**
|
|
76
93
|
* Convert a Zod object schema to serializable field metadata.
|
|
77
94
|
*
|
package/dist/data/orm/types.js
CHANGED
|
@@ -1,39 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fieldsToSchema = fieldsToSchema;
|
|
4
|
+
exports.fieldsToArgumentsSchema = fieldsToArgumentsSchema;
|
|
4
5
|
exports.fieldsToShapeSchema = fieldsToShapeSchema;
|
|
5
6
|
exports.withoutEmptyFields = withoutEmptyFields;
|
|
6
7
|
exports.comparableTableFields = comparableTableFields;
|
|
8
|
+
exports.zodEnumValues = zodEnumValues;
|
|
7
9
|
exports.schemaToFields = schemaToFields;
|
|
8
10
|
const zod_1 = require("zod");
|
|
9
11
|
const field_type_1 = require("../../types/field-type");
|
|
10
12
|
const utils_1 = require("../../utils");
|
|
11
13
|
/**
|
|
12
|
-
* Convert field metadata to the runtime Zod schema the Runtime validates
|
|
13
|
-
* tool arguments.
|
|
14
|
+
* Convert field metadata to the runtime Zod schema the Runtime validates table rows with.
|
|
14
15
|
*
|
|
15
16
|
* It checks types only: `type`, `isArray`, and `optional`. An object field accepts any object
|
|
16
|
-
* and keeps its keys; nested `fields` and `enumValues` aren't checked, so a row
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* {@link withoutEmptyFields}.
|
|
17
|
+
* and keeps its keys; nested `fields` and `enumValues` aren't checked, so a row from a package
|
|
18
|
+
* built against a newer shape isn't refused. A tool's arguments are checked with the stricter
|
|
19
|
+
* {@link fieldsToArgumentsSchema}, and {@link fieldsToShapeSchema} builds the full shape. An
|
|
20
|
+
* optional field refuses `null`: remove empty fields first with {@link withoutEmptyFields}.
|
|
20
21
|
*
|
|
21
22
|
* @param fields Serializable field definitions.
|
|
22
23
|
* @param codeSchema Optional author schema whose refinements are intersected.
|
|
23
24
|
*/
|
|
24
25
|
function fieldsToSchema(fields, codeSchema) {
|
|
25
|
-
return buildFieldsSchema(fields,
|
|
26
|
+
return buildFieldsSchema(fields, "rows", codeSchema);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Convert a tool's input fields to the Zod schema the Runtime checks its arguments with before
|
|
30
|
+
* the provider runs. Like {@link fieldsToSchema}, and also: a required field must be present
|
|
31
|
+
* whatever its type (`any` included), and a field's `enumValues` are enforced. A contract
|
|
32
|
+
* version's fields don't change once it is stable, so a caller and its provider agree on them.
|
|
33
|
+
* Nested object `fields` still aren't checked, and keys no field names pass.
|
|
34
|
+
*
|
|
35
|
+
* @param fields A tool's input fields.
|
|
36
|
+
*/
|
|
37
|
+
function fieldsToArgumentsSchema(fields) {
|
|
38
|
+
return buildFieldsSchema(fields, "arguments");
|
|
26
39
|
}
|
|
27
40
|
/**
|
|
28
41
|
* Convert field metadata to a Zod schema with every shape the IR carries: nested object
|
|
29
42
|
* `fields` and `enumValues`, as well as types. Objects keep keys their `fields` don't list. For
|
|
30
43
|
* describing a tool's arguments to an assistant's model; the Runtime validates with
|
|
31
|
-
* {@link
|
|
44
|
+
* {@link fieldsToArgumentsSchema}.
|
|
32
45
|
*
|
|
33
46
|
* @param fields Serializable field definitions.
|
|
34
47
|
*/
|
|
35
48
|
function fieldsToShapeSchema(fields) {
|
|
36
|
-
return buildFieldsSchema(fields,
|
|
49
|
+
return buildFieldsSchema(fields, "shapes");
|
|
37
50
|
}
|
|
38
51
|
function enumSchema(values) {
|
|
39
52
|
if (values.every((value) => typeof value === "string")) {
|
|
@@ -44,8 +57,8 @@ function enumSchema(values) {
|
|
|
44
57
|
? literals[0]
|
|
45
58
|
: zod_1.z.union(literals);
|
|
46
59
|
}
|
|
47
|
-
function fieldValueSchema(field,
|
|
48
|
-
if (
|
|
60
|
+
function fieldValueSchema(field, mode, open) {
|
|
61
|
+
if (mode !== "rows" && field.enumValues?.length) {
|
|
49
62
|
return enumSchema(field.enumValues);
|
|
50
63
|
}
|
|
51
64
|
switch (field.type) {
|
|
@@ -63,7 +76,9 @@ function fieldValueSchema(field, shapes, open) {
|
|
|
63
76
|
case "Date":
|
|
64
77
|
return zod_1.z.date();
|
|
65
78
|
case "object": {
|
|
66
|
-
const object = shapes && field.fields
|
|
79
|
+
const object = mode === "shapes" && field.fields
|
|
80
|
+
? buildFieldsSchema(field.fields, "shapes")
|
|
81
|
+
: zod_1.z.object({});
|
|
67
82
|
// An author schema intersected with this one parses the value itself; two parses of
|
|
68
83
|
// one object can't be merged, so only an open object keeps its keys.
|
|
69
84
|
return open ? object.passthrough() : object;
|
|
@@ -72,12 +87,12 @@ function fieldValueSchema(field, shapes, open) {
|
|
|
72
87
|
return zod_1.z.any();
|
|
73
88
|
}
|
|
74
89
|
}
|
|
75
|
-
function buildFieldsSchema(fields,
|
|
90
|
+
function buildFieldsSchema(fields, mode, codeSchema) {
|
|
76
91
|
let argsSchema = zod_1.z.object({});
|
|
77
92
|
for (const field of fields) {
|
|
78
93
|
const { name, description, isArray, defaultValue, optional } = field;
|
|
79
94
|
const shapeField = codeSchema?.shape?.[name];
|
|
80
|
-
let propertyType = fieldValueSchema(field,
|
|
95
|
+
let propertyType = fieldValueSchema(field, mode, !shapeField);
|
|
81
96
|
if (isArray) {
|
|
82
97
|
propertyType = propertyType.array();
|
|
83
98
|
}
|
|
@@ -87,6 +102,11 @@ function buildFieldsSchema(fields, shapes, codeSchema) {
|
|
|
87
102
|
if (defaultValue !== undefined) {
|
|
88
103
|
propertyType = propertyType.default(defaultValue);
|
|
89
104
|
}
|
|
105
|
+
else if (mode !== "rows" && !optional) {
|
|
106
|
+
// `z.any()` accepts a missing value; a required argument must be there, and a model
|
|
107
|
+
// describing it must see it listed as required.
|
|
108
|
+
propertyType = propertyType.refine((value) => value !== undefined, { message: "Required" });
|
|
109
|
+
}
|
|
90
110
|
propertyType = propertyType.describe(description ?? "");
|
|
91
111
|
if (shapeField) {
|
|
92
112
|
propertyType = zod_1.z.intersection(propertyType, shapeField);
|
|
@@ -189,6 +209,42 @@ function comparableTableFields(fields) {
|
|
|
189
209
|
return comparable;
|
|
190
210
|
});
|
|
191
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* The scalar values an enum, a native enum, a literal, or a union of them allows; undefined for
|
|
214
|
+
* anything else.
|
|
215
|
+
*
|
|
216
|
+
* @internal
|
|
217
|
+
* @param schema A field's Zod schema, unwrapped to its value type.
|
|
218
|
+
*/
|
|
219
|
+
function zodEnumValues(schema) {
|
|
220
|
+
const def = schema._def;
|
|
221
|
+
switch (def.typeName) {
|
|
222
|
+
case "ZodEnum":
|
|
223
|
+
return [...def.values];
|
|
224
|
+
case "ZodNativeEnum": {
|
|
225
|
+
// A numeric TypeScript enum maps each value back to its name too; skip those keys.
|
|
226
|
+
const values = def.values;
|
|
227
|
+
return Object.keys(values)
|
|
228
|
+
.filter((key) => typeof values[values[key]] !== "number")
|
|
229
|
+
.map((key) => values[key]);
|
|
230
|
+
}
|
|
231
|
+
case "ZodLiteral":
|
|
232
|
+
return ["string", "number", "boolean"].includes(typeof def.value) ? [def.value] : undefined;
|
|
233
|
+
case "ZodUnion": {
|
|
234
|
+
const values = [];
|
|
235
|
+
for (const option of def.options) {
|
|
236
|
+
const optionValues = zodEnumValues(option);
|
|
237
|
+
if (!optionValues) {
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
values.push(...optionValues);
|
|
241
|
+
}
|
|
242
|
+
return values;
|
|
243
|
+
}
|
|
244
|
+
default:
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
192
248
|
/**
|
|
193
249
|
* Convert a Zod object schema to serializable field metadata.
|
|
194
250
|
*
|
|
@@ -199,26 +255,30 @@ function schemaToFields(schema) {
|
|
|
199
255
|
for (const name of Object.keys(schema.shape)) {
|
|
200
256
|
const field = schema.shape[name];
|
|
201
257
|
let type = field_type_1.FieldType.any;
|
|
258
|
+
let valueSchema = field;
|
|
259
|
+
let nullable = false;
|
|
260
|
+
let enumElements = true;
|
|
202
261
|
let _def = field._def;
|
|
203
262
|
let isArray;
|
|
204
263
|
let brandedFieldType;
|
|
205
264
|
while (true) {
|
|
206
265
|
if (_def.typeName === "ZodArray") {
|
|
266
|
+
if (isArray)
|
|
267
|
+
enumElements = false;
|
|
207
268
|
isArray = true;
|
|
208
269
|
}
|
|
270
|
+
if (isArray && (_def.typeName === "ZodOptional" || _def.typeName === "ZodDefault"))
|
|
271
|
+
enumElements = false;
|
|
209
272
|
if (_def._fieldType) {
|
|
210
273
|
brandedFieldType = _def._fieldType;
|
|
211
274
|
}
|
|
212
|
-
if (_def.
|
|
213
|
-
|
|
214
|
-
continue;
|
|
215
|
-
}
|
|
216
|
-
if (_def.schema) {
|
|
217
|
-
_def = _def.schema._def;
|
|
218
|
-
continue;
|
|
275
|
+
if (_def.typeName === "ZodNullable") {
|
|
276
|
+
nullable = true;
|
|
219
277
|
}
|
|
220
|
-
|
|
221
|
-
|
|
278
|
+
const inner = _def.innerType ?? _def.schema ?? _def.type;
|
|
279
|
+
if (inner) {
|
|
280
|
+
valueSchema = inner;
|
|
281
|
+
_def = inner._def;
|
|
222
282
|
continue;
|
|
223
283
|
}
|
|
224
284
|
break;
|
|
@@ -255,7 +315,20 @@ function schemaToFields(schema) {
|
|
|
255
315
|
const optional = field._def.typeName === "ZodOptional" || undefined;
|
|
256
316
|
const description = field._def.description;
|
|
257
317
|
const defaultValue = field._def.defaultValue;
|
|
258
|
-
|
|
318
|
+
// The type stays as it was (an enum is `any`) so stable contract shapes don't change. A
|
|
319
|
+
// nullable enum also allows null, which a list of values can't say. The same is true of
|
|
320
|
+
// optional/defaulted elements and nested arrays: enumValues describes each immediate element.
|
|
321
|
+
const values = nullable || !enumElements ? undefined : zodEnumValues(valueSchema);
|
|
322
|
+
const enumValues = values?.length ? [...new Set(values)] : undefined;
|
|
323
|
+
const newField = {
|
|
324
|
+
name,
|
|
325
|
+
type,
|
|
326
|
+
isArray,
|
|
327
|
+
optional,
|
|
328
|
+
description,
|
|
329
|
+
defaultValue,
|
|
330
|
+
enumValues,
|
|
331
|
+
};
|
|
259
332
|
Object.keys(newField).forEach((k) => {
|
|
260
333
|
// @ts-expect-error
|
|
261
334
|
if (newField[k] === undefined)
|
package/dist/define/manifest.js
CHANGED
|
@@ -49,36 +49,6 @@ function literalType(schema) {
|
|
|
49
49
|
return field_type_1.FieldType.boolean;
|
|
50
50
|
return undefined;
|
|
51
51
|
}
|
|
52
|
-
/** The scalar values an enum, a literal, or a union of them allows; undefined for anything else. */
|
|
53
|
-
function enumValuesOf(schema) {
|
|
54
|
-
const def = schema._def;
|
|
55
|
-
switch (def.typeName) {
|
|
56
|
-
case "ZodEnum":
|
|
57
|
-
return [...def.values];
|
|
58
|
-
case "ZodNativeEnum": {
|
|
59
|
-
// A numeric TypeScript enum maps each value back to its name too; skip those keys.
|
|
60
|
-
const values = def.values;
|
|
61
|
-
return Object.keys(values)
|
|
62
|
-
.filter((key) => typeof values[values[key]] !== "number")
|
|
63
|
-
.map((key) => values[key]);
|
|
64
|
-
}
|
|
65
|
-
case "ZodLiteral":
|
|
66
|
-
return ["string", "number", "boolean"].includes(typeof def.value) ? [def.value] : undefined;
|
|
67
|
-
case "ZodUnion": {
|
|
68
|
-
const values = [];
|
|
69
|
-
for (const option of def.options) {
|
|
70
|
-
const optionValues = enumValuesOf(option);
|
|
71
|
-
if (!optionValues) {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
values.push(...optionValues);
|
|
75
|
-
}
|
|
76
|
-
return values;
|
|
77
|
-
}
|
|
78
|
-
default:
|
|
79
|
-
return undefined;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
52
|
/**
|
|
83
53
|
* What a field's schema says beyond its type: an object's own fields and the values an enum or
|
|
84
54
|
* literal allows. Walks the wrappers `schemaToFields` walks, into one array's elements, and
|
|
@@ -153,7 +123,7 @@ function fieldDetails(schema, shape, input, depth, path, event = false, wholeVal
|
|
|
153
123
|
? eventFields(current, depth + 1, path)
|
|
154
124
|
: irFields(current, input, depth + 1, path);
|
|
155
125
|
}
|
|
156
|
-
const values =
|
|
126
|
+
const values = (0, types_1.zodEnumValues)(current);
|
|
157
127
|
if (!emptyElement && values?.length && values.every((value) => (0, isolated_artifact_1.enumValueFits)(value, type))) {
|
|
158
128
|
details.enumValues = [...new Set(values)];
|
|
159
129
|
}
|
|
@@ -190,8 +160,10 @@ function irField(schema, schemaField, input, depth, path, wholeValue = false) {
|
|
|
190
160
|
const generated = (0, generated_field_1.generatedFieldMetadata)(schema);
|
|
191
161
|
if (generated)
|
|
192
162
|
return { name: schemaField.name, ...generated.shape };
|
|
193
|
-
|
|
194
|
-
const
|
|
163
|
+
// The IR records enum values itself (fieldDetails), only where they fit the field's type.
|
|
164
|
+
const { enumValues: _enumValues, ...plainField } = schemaField;
|
|
165
|
+
const literal = plainField.type === field_type_1.FieldType.any ? literalType(schema) : undefined;
|
|
166
|
+
const field = literal ? { ...plainField, type: literal } : plainField;
|
|
195
167
|
const { defaultValue: _default, ...rest } = field;
|
|
196
168
|
const result = typeof _default === "function" ? rest : field;
|
|
197
169
|
const details = fieldDetails(schema, field, input, depth, path, false, wholeValue);
|
|
@@ -500,7 +472,7 @@ function eventFields(schema, depth = 1, parent = "") {
|
|
|
500
472
|
const generated = (0, generated_field_1.generatedFieldMetadata)(schema.shape[field.name]);
|
|
501
473
|
if (generated)
|
|
502
474
|
return { name: field.name, ...generated.shape };
|
|
503
|
-
const { type: _type, optional: _optional, isArray: _array, defaultValue, ...metadata } = field;
|
|
475
|
+
const { type: _type, optional: _optional, isArray: _array, defaultValue, enumValues: _enumValues, ...metadata } = field;
|
|
504
476
|
const shape = eventValueShape(schema.shape[field.name]);
|
|
505
477
|
const path = parent ? `${parent}.${field.name}` : field.name;
|
|
506
478
|
return {
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* binary-peer-connection-v2.ts
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Binary transport layer under every device connection. Provides two cleanly
|
|
5
|
+
* separated modes:
|
|
6
6
|
*
|
|
7
7
|
* 1. **RPC mode** (emit/on): request/response interactions with automatic
|
|
8
8
|
* encoding (txEncode for objects, passthrough for Uint8Array) and transparent
|
|
9
9
|
* chunking for large payloads.
|
|
10
10
|
*
|
|
11
11
|
* 2. **Stream mode** (sendRawBytes/onRawBytes): continuous byte transfer for
|
|
12
|
-
* file syncing,
|
|
12
|
+
* file syncing, log streaming, etc. Each sendRawBytes
|
|
13
13
|
* call = exactly one onRawBytes callback (atomic delivery after reassembly).
|
|
14
14
|
*
|
|
15
15
|
* Both modes share a single sendFrame() function that handles backpressure
|
|
@@ -61,19 +61,12 @@ export declare const DEFAULT_BINARY_PEER_RECEIVE_LIMITS: Readonly<BinaryPeerRece
|
|
|
61
61
|
export interface WrapBinaryPeerOptions {
|
|
62
62
|
/** Protocol identifier used for server address (e.g., 'wrtc' or 'ws') */
|
|
63
63
|
protocol: "wrtc" | "ws";
|
|
64
|
-
/**
|
|
65
|
-
* Mark the transport as already secure (encrypted at transport layer).
|
|
66
|
-
* When true, sets secureLocal and secureRemote to skip the legacy per-message
|
|
67
|
-
* envelope against a legacy peer. Irrelevant once the frame cipher is armed.
|
|
68
|
-
* @deprecated Removed with the legacy path.
|
|
69
|
-
*/
|
|
70
|
-
markTransportSecure?: boolean;
|
|
71
64
|
/** Optional hard limits for untrusted inbound messages. */
|
|
72
65
|
receiveLimits?: Partial<BinaryPeerReceiveLimits>;
|
|
73
66
|
}
|
|
74
67
|
/**
|
|
75
68
|
* Creates an ISocket from a binary peer. This is the core v2 factory function.
|
|
76
|
-
*
|
|
69
|
+
* Used for every device connection.
|
|
77
70
|
*/
|
|
78
71
|
export declare function createBinaryPeerSocket(connectionId: string, peer: IBinaryPeer, protocol?: string, receiveLimitOverrides?: Partial<BinaryPeerReceiveLimits>): ISocket;
|
|
79
72
|
/**
|
|
@@ -82,10 +75,9 @@ export declare function createBinaryPeerSocket(connectionId: string, peer: IBina
|
|
|
82
75
|
*/
|
|
83
76
|
export declare function wrapBinaryPeer(connectionId: string, peer: IBinaryPeer, localDevice: Device, initiator: boolean, options: WrapBinaryPeerOptions, getTrustLevel?: ITrustLevelFn): Connection;
|
|
84
77
|
/**
|
|
85
|
-
* Wraps a WebRTC peer into a Connection.
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* classification.
|
|
78
|
+
* Wraps a WebRTC peer into a Connection. WebRTC is encrypted at the transport
|
|
79
|
+
* layer (DTLS-SRTP); the handshake arms the frame cipher on top of it, as on
|
|
80
|
+
* every transport.
|
|
89
81
|
*/
|
|
90
82
|
export declare function wrapWrtc(connectionId: string, peer: IWebRTCPeer, localDevice: Device, initiator: boolean, getTrustLevel?: ITrustLevelFn, receiveLimits?: Partial<BinaryPeerReceiveLimits>): Connection;
|
|
91
83
|
export {};
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* binary-peer-connection-v2.ts
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Binary transport layer under every device connection. Provides two cleanly
|
|
6
|
+
* separated modes:
|
|
7
7
|
*
|
|
8
8
|
* 1. **RPC mode** (emit/on): request/response interactions with automatic
|
|
9
9
|
* encoding (txEncode for objects, passthrough for Uint8Array) and transparent
|
|
10
10
|
* chunking for large payloads.
|
|
11
11
|
*
|
|
12
12
|
* 2. **Stream mode** (sendRawBytes/onRawBytes): continuous byte transfer for
|
|
13
|
-
* file syncing,
|
|
13
|
+
* file syncing, log streaming, etc. Each sendRawBytes
|
|
14
14
|
* call = exactly one onRawBytes callback (atomic delivery after reassembly).
|
|
15
15
|
*
|
|
16
16
|
* Both modes share a single sendFrame() function that handles backpressure
|
|
@@ -240,7 +240,7 @@ function buildStreamCont(sidBytes, chunkData) {
|
|
|
240
240
|
}
|
|
241
241
|
/**
|
|
242
242
|
* Creates an ISocket from a binary peer. This is the core v2 factory function.
|
|
243
|
-
*
|
|
243
|
+
* Used for every device connection.
|
|
244
244
|
*/
|
|
245
245
|
function createBinaryPeerSocket(connectionId, peer, protocol = "binary", receiveLimitOverrides = {}) {
|
|
246
246
|
const handlers = {};
|
|
@@ -265,7 +265,7 @@ function createBinaryPeerSocket(connectionId, peer, protocol = "binary", receive
|
|
|
265
265
|
let sendSealer = null;
|
|
266
266
|
let receiveOpener = null;
|
|
267
267
|
// Bare sockets (no Connection) deliver plaintext streams; Connection turns
|
|
268
|
-
// this off
|
|
268
|
+
// this off, so it receives only sealed stream frames.
|
|
269
269
|
let plaintextStreamsAllowed = true;
|
|
270
270
|
// Every inner frame must leave room for the cipher's 17 bytes so a sealed
|
|
271
271
|
// frame never exceeds the transport limit (64 KB SCTP on WebRTC). Reserved
|
|
@@ -503,7 +503,7 @@ function createBinaryPeerSocket(connectionId, peer, protocol = "binary", receive
|
|
|
503
503
|
else
|
|
504
504
|
responseMessage.result = result;
|
|
505
505
|
const encoded = encodeMessage(responseMessage);
|
|
506
|
-
sendRPC(encoded, sendOptions);
|
|
506
|
+
return sendRPC(encoded, sendOptions);
|
|
507
507
|
};
|
|
508
508
|
handler(message.args, callback);
|
|
509
509
|
}
|
|
@@ -730,17 +730,8 @@ function createBinaryPeerSocket(connectionId, peer, protocol = "binary", receive
|
|
|
730
730
|
break;
|
|
731
731
|
}
|
|
732
732
|
default: {
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
// legacy format (no discriminator), try to decode as RPC.
|
|
736
|
-
// This handles the transition period where v1 peers send raw RPC without
|
|
737
|
-
// a frame type byte.
|
|
738
|
-
if (bytes.length > receiveLimits.maxRpcMessageBytes) {
|
|
739
|
-
closeForProtocolViolation("legacy RPC exceeds the message-size limit");
|
|
740
|
-
return;
|
|
741
|
-
}
|
|
742
|
-
handleIncomingRPC(bytes);
|
|
743
|
-
break;
|
|
733
|
+
closeForProtocolViolation(`unknown frame type ${frameType}`);
|
|
734
|
+
return;
|
|
744
735
|
}
|
|
745
736
|
}
|
|
746
737
|
}
|
|
@@ -762,7 +753,6 @@ function createBinaryPeerSocket(connectionId, peer, protocol = "binary", receive
|
|
|
762
753
|
const socket = {
|
|
763
754
|
id: connectionId,
|
|
764
755
|
connected: true,
|
|
765
|
-
handlesOwnEncoding: true,
|
|
766
756
|
transportType,
|
|
767
757
|
stats,
|
|
768
758
|
armFrameCipher,
|
|
@@ -811,26 +801,20 @@ function createBinaryPeerSocket(connectionId, peer, protocol = "binary", receive
|
|
|
811
801
|
* This is the main implementation that handles all binary protocols.
|
|
812
802
|
*/
|
|
813
803
|
function wrapBinaryPeer(connectionId, peer, localDevice, initiator, options, getTrustLevel) {
|
|
814
|
-
const { protocol,
|
|
804
|
+
const { protocol, receiveLimits } = options;
|
|
815
805
|
const socket = createBinaryPeerSocket(connectionId, peer, protocol, receiveLimits);
|
|
816
806
|
const serverAddress = `${protocol}://${connectionId}`;
|
|
817
807
|
const localServerAddresses = initiator ? undefined : [serverAddress];
|
|
818
808
|
if (process.env.NODE_ENV !== "test") {
|
|
819
809
|
getTrustLevel ??= (0, get_trust_level_fn_1.getTrustLevelFn)(localDevice, serverAddress);
|
|
820
810
|
}
|
|
821
|
-
|
|
822
|
-
if (markTransportSecure) {
|
|
823
|
-
connection.secureLocal = true;
|
|
824
|
-
connection.secureRemote = true;
|
|
825
|
-
}
|
|
826
|
-
return connection;
|
|
811
|
+
return new connection_1.Connection(socket, localDevice, localServerAddresses, getTrustLevel);
|
|
827
812
|
}
|
|
828
813
|
/**
|
|
829
|
-
* Wraps a WebRTC peer into a Connection.
|
|
830
|
-
*
|
|
831
|
-
*
|
|
832
|
-
* classification.
|
|
814
|
+
* Wraps a WebRTC peer into a Connection. WebRTC is encrypted at the transport
|
|
815
|
+
* layer (DTLS-SRTP); the handshake arms the frame cipher on top of it, as on
|
|
816
|
+
* every transport.
|
|
833
817
|
*/
|
|
834
818
|
function wrapWrtc(connectionId, peer, localDevice, initiator, getTrustLevel, receiveLimits) {
|
|
835
|
-
return wrapBinaryPeer(connectionId, peer, localDevice, initiator, { protocol: "wrtc",
|
|
819
|
+
return wrapBinaryPeer(connectionId, peer, localDevice, initiator, { protocol: "wrtc", receiveLimits }, getTrustLevel);
|
|
836
820
|
}
|