maka-agent 0.2.0-dev.14.20260902 → 0.2.0-dev.16.20260903
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/cli-core.js +0 -1
- package/dist/pi-transcript.js +48 -0
- package/dist/pi-tui-form-interaction.js +497 -0
- package/dist/pi-tui-pickers.js +256 -55
- package/dist/pi-tui-runner.js +215 -12
- package/dist/runtime-host-cli.js +17 -42
- package/dist/runtime-host-lifecycle-transaction.js +31 -13
- package/dist/runtime-host-managed-lifecycle-manager.js +1 -1
- package/dist/runtime-host-onboarding.js +3 -1
- package/dist/runtime-host-openrc-service.js +469 -0
- package/dist/runtime-host-run-command.js +6 -2
- package/dist/runtime-host-service-command.js +3 -3
- package/dist/runtime-host-service-management-command.js +43 -15
- package/dist/runtime-host-session-channel.js +3 -0
- package/dist/runtime-host-session-driver.js +21 -6
- package/dist/runtime-host-setup-command.js +30 -31
- package/dist/tui-context-refresh.js +103 -0
- package/dist/tui-copy-catalog.js +73 -0
- package/native/runtime-host-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
- package/node_modules/@maka/core/dist/chat-model-choice.js +4 -0
- package/node_modules/@maka/core/dist/computer-use.js +27 -92
- package/node_modules/@maka/core/dist/interaction.js +577 -3
- package/node_modules/@maka/core/dist/llm-connections.js +4 -0
- package/node_modules/@maka/core/dist/local-memory.js +2 -2
- package/node_modules/@maka/core/dist/model-call-usage-projection.js +3 -0
- package/node_modules/@maka/core/dist/model-metadata.js +26 -7
- package/node_modules/@maka/core/dist/model-thinking.js +17 -0
- package/node_modules/@maka/core/dist/provider-registry.js +1 -0
- package/node_modules/@maka/core/dist/runtime-event.js +23 -1
- package/node_modules/@maka/core/dist/scheduled-task.js +4 -0
- package/node_modules/@maka/core/dist/session.js +95 -2
- package/node_modules/@maka/core/dist/text-sanitize.js +19 -0
- package/node_modules/@maka/core/dist/usage-ledger-merge.js +3 -0
- package/node_modules/@maka/core/dist/usage-record-schema.js +11 -5
- package/node_modules/@maka/core/dist/workhub-creation-intent.js +75 -9
- package/node_modules/@maka/core/dist/workhub-session-resolver.js +50 -0
- package/node_modules/@maka/core/package.json +2 -0
- package/node_modules/@maka/eval/dist/harbor-external-subject.js +2 -1
- package/node_modules/@maka/eval/dist/harness-executor.js +3 -2
- package/node_modules/@maka/eval/dist/install-preflight.js +39 -10
- package/node_modules/@maka/eval/dist/process-termination.js +83 -0
- package/node_modules/@maka/mcp/dist/sep-2243.js +3 -3
- package/node_modules/@maka/runtime/dist/agent-run.js +6 -3
- package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +332 -118
- package/node_modules/@maka/runtime/dist/ai-sdk-compaction-contract.js +6 -1
- package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +171 -341
- package/node_modules/@maka/runtime/dist/bots/telegram-bridge.js +3 -26
- package/node_modules/@maka/runtime/dist/bundled-skill-catalog.generated.js +1 -1
- package/node_modules/@maka/runtime/dist/computer-use-codec.js +1 -141
- package/node_modules/@maka/runtime/dist/computer-use-tools.js +23 -105
- package/node_modules/@maka/runtime/dist/context-budget-policy.js +20 -26
- package/node_modules/@maka/runtime/dist/context-budget.js +18 -20
- package/node_modules/@maka/runtime/dist/conversation-copy.js +23 -19
- package/node_modules/@maka/runtime/dist/cua-frame-state.js +1 -69
- package/node_modules/@maka/runtime/dist/github-copilot-oauth-enrollment.js +267 -0
- package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +6 -9
- package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +67 -23
- package/node_modules/@maka/runtime/dist/history-compact-summary-validation.js +11 -18
- package/node_modules/@maka/runtime/dist/history-compaction.js +66 -73
- package/node_modules/@maka/runtime/dist/interaction-authority.js +69 -0
- package/node_modules/@maka/runtime/dist/mcp-tools.js +13 -1
- package/node_modules/@maka/runtime/dist/memory-extraction.js +11 -11
- package/node_modules/@maka/runtime/dist/model-adapter.js +24 -6
- package/node_modules/@maka/runtime/dist/model-factory.js +14 -5
- package/node_modules/@maka/runtime/dist/oauth-provider-contracts.js +28 -1
- package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +7 -6
- package/node_modules/@maka/runtime/dist/opencode-session-header.js +29 -0
- package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +11 -1
- package/node_modules/@maka/runtime/dist/runtime-kernel.js +12 -11
- package/node_modules/@maka/runtime/dist/runtime-read-model.js +51 -13
- package/node_modules/@maka/runtime/dist/scheduled-task-tools.js +3 -0
- package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +26 -0
- package/node_modules/@maka/runtime/dist/session-manager.js +1 -0
- package/node_modules/@maka/runtime/dist/session-projection-helpers.js +16 -1
- package/node_modules/@maka/runtime/dist/stream-graph-projection.js +6 -0
- package/node_modules/@maka/runtime/dist/stream-graph-read-model.js +5 -0
- package/node_modules/@maka/runtime/dist/stream-usage-fallback-fetch.js +74 -0
- package/node_modules/@maka/runtime/dist/subscription-credentials.js +51 -4
- package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +47 -3
- package/node_modules/@maka/runtime/dist/system-prompt/main-session-prompt.js +20 -1
- package/node_modules/@maka/runtime/dist/test-connection.js +18 -11
- package/node_modules/@maka/runtime/dist/tool-runtime.js +183 -0
- package/node_modules/@maka/runtime/package.json +1 -0
- package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +14 -0
- package/node_modules/@maka/runtime-host/dist/client/client-capability-channel.js +84 -1
- package/node_modules/@maka/runtime-host/dist/client/host-profile.js +68 -51
- package/node_modules/@maka/runtime-host/dist/client/index.js +1 -1
- package/node_modules/@maka/runtime-host/dist/client/owner-connection-code.js +8 -5
- package/node_modules/@maka/runtime-host/dist/client/peer-client.js +342 -36
- package/node_modules/@maka/runtime-host/dist/client/reconnect-lifecycle.js +60 -4
- package/node_modules/@maka/runtime-host/dist/peer-mesh/index.js +3 -3
- package/node_modules/@maka/runtime-host/dist/peer-mesh/limits.js +1 -1
- package/node_modules/@maka/runtime-host/dist/peer-mesh/model.js +48 -60
- package/node_modules/@maka/runtime-host/dist/peer-mesh/node.js +847 -431
- package/node_modules/@maka/runtime-host/dist/peer-mesh/owner.js +33 -74
- package/node_modules/@maka/runtime-host/dist/peer-mesh/store.js +82 -69
- package/node_modules/@maka/runtime-host/dist/peer-reachability/index.js +21 -0
- package/node_modules/@maka/runtime-host/dist/peer-reachability/model.js +202 -0
- package/node_modules/@maka/runtime-host/dist/peer-reachability/owner.js +119 -0
- package/node_modules/@maka/runtime-host/dist/peer-reachability/publisher.js +245 -0
- package/node_modules/@maka/runtime-host/dist/protocol/agent-graph.js +4 -1
- package/node_modules/@maka/runtime-host/dist/protocol/client-capability.js +68 -0
- package/node_modules/@maka/runtime-host/dist/protocol/connection-effects.js +15 -8
- package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +7 -23
- package/node_modules/@maka/runtime-host/dist/protocol/index.js +47 -1
- package/node_modules/@maka/runtime-host/dist/protocol/oauth.js +25 -1
- package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
- package/node_modules/@maka/runtime-host/dist/protocol/peer-mesh.js +14 -31
- package/node_modules/@maka/runtime-host/dist/protocol/scheduled-task.js +19 -6
- package/node_modules/@maka/runtime-host/dist/protocol/session-collaboration.js +36 -1
- package/node_modules/@maka/runtime-host/dist/protocol/session-revision.js +11 -2
- package/node_modules/@maka/runtime-host/dist/protocol/usage-pricing.js +16 -2
- package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +51 -2
- package/node_modules/@maka/runtime-host/dist/server/access-authority.js +35 -2
- package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
- package/node_modules/@maka/runtime-host/dist/server/client-capability-coordinator.js +12 -10
- package/node_modules/@maka/runtime-host/dist/server/client-capability-invocation-broker.js +163 -30
- package/node_modules/@maka/runtime-host/dist/server/connection-effect-coordinator.js +38 -14
- package/node_modules/@maka/runtime-host/dist/server/daily-review-coordinator.js +45 -22
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +78 -20
- package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +4 -2
- package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +2 -1
- package/node_modules/@maka/runtime-host/dist/server/execution-service.js +49 -28
- package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +3 -6
- package/node_modules/@maka/runtime-host/dist/server/interaction-coordinator.js +130 -27
- package/node_modules/@maka/runtime-host/dist/server/interaction-projection.js +30 -1
- package/node_modules/@maka/runtime-host/dist/server/listener-set.js +2 -4
- package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +16 -3
- package/node_modules/@maka/runtime-host/dist/server/oauth-coordinator.js +66 -3
- package/node_modules/@maka/runtime-host/dist/server/oauth-execution-authority.js +20 -3
- package/node_modules/@maka/runtime-host/dist/server/peer-listener.js +8 -21
- package/node_modules/@maka/runtime-host/dist/server/peer-mesh-authority.js +1 -1
- package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +6 -3
- package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +72 -12
- package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +48 -18
- package/node_modules/@maka/runtime-host/dist/server/session-turn-access-request-coordinator.js +43 -26
- package/node_modules/@maka/runtime-host/dist/server/usage-pricing-coordinator.js +15 -1
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +229 -4
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +146 -8
- package/node_modules/@maka/runtime-host/dist/transport/peer-native.js +32 -6
- package/node_modules/@maka/runtime-host/package.json +1 -0
- package/node_modules/@maka/storage/dist/agent-run-store.js +18 -5
- package/node_modules/@maka/storage/dist/codex-session-adapter.js +14 -0
- package/node_modules/@maka/storage/dist/execution-stores.js +5 -0
- package/node_modules/@maka/storage/dist/interaction-store.js +13 -1
- package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +9 -3
- package/node_modules/@maka/storage/dist/runtime-policy/coordinator.js +49 -19
- package/node_modules/@maka/storage/dist/runtime-policy/oauth-login-receipt-document.js +3 -1
- package/node_modules/@maka/storage/dist/runtime-policy/onboarding-transaction.js +13 -5
- package/node_modules/@maka/storage/dist/session-copy-cleanup.js +4 -2
- package/node_modules/@maka/storage/dist/session-store.js +39 -6
- package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +16 -9
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-schema.js +30 -3
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +117 -0
- package/node_modules/@maka/storage/dist/sqlite-usage-store.js +29 -6
- package/node_modules/@maka/storage/dist/usage-stores.js +3 -1
- package/package.json +1 -1
|
@@ -16,103 +16,62 @@
|
|
|
16
16
|
* specific language governing permissions and limitations
|
|
17
17
|
* under the License.
|
|
18
18
|
*/
|
|
19
|
-
import { createRuntimeHostPeerClient } from '../client/peer-client.js';
|
|
20
|
-
import { chmod, mkdir } from 'node:fs/promises';
|
|
21
19
|
import { join } from 'node:path';
|
|
22
|
-
import { acquireFileLifetimeOwner, } from '@maka/storage/file-lifetime-owner';
|
|
23
20
|
import { openPeerMeshNode } from './node.js';
|
|
24
21
|
import { migrateLegacyPeerMeshState } from './store.js';
|
|
25
|
-
export async function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
await
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
...(input.coordinationRelays ? { coordinationRelays: input.coordinationRelays } : {}),
|
|
40
|
-
...(input.automaticRelayDiscovery === undefined
|
|
41
|
-
? {}
|
|
42
|
-
: { automaticRelayDiscovery: input.automaticRelayDiscovery }),
|
|
43
|
-
...(input.webRtcStunUrls === undefined ? {} : { webRtcStunUrls: input.webRtcStunUrls }),
|
|
44
|
-
routeResolver: {
|
|
45
|
-
resolveRoutes: (peerId) => resolverMesh?.resolveRoutes(peerId),
|
|
46
|
-
prepareRoutes: async (peerId, signal) => resolverMesh?.prepareRoutes(peerId, signal),
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
await rootOwner.close().catch(() => undefined);
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
22
|
+
export async function openRuntimeHostPeerMeshComponent(input) {
|
|
23
|
+
const peerId = input.endpoint.client.identity().peerId;
|
|
24
|
+
await migrateLegacyPeerMeshState(input.dataRoot, peerId);
|
|
25
|
+
const mesh = await openPeerMeshNode({
|
|
26
|
+
dataRoot: join(input.dataRoot, peerId),
|
|
27
|
+
peer: input.endpoint.client,
|
|
28
|
+
reachability: input.endpoint.reachability,
|
|
29
|
+
endpointKind: input.endpointKind,
|
|
30
|
+
...(input.onBackgroundReconcileError
|
|
31
|
+
? { onBackgroundReconcileError: input.onBackgroundReconcileError }
|
|
32
|
+
: {}),
|
|
33
|
+
});
|
|
34
|
+
let detachResolver;
|
|
35
|
+
let serving;
|
|
54
36
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
dataRoot: join(input.dataRoot, client.identity().peerId),
|
|
58
|
-
peer: client,
|
|
59
|
-
endpointKind: input.endpointKind,
|
|
60
|
-
...(input.onBackgroundReconcileError
|
|
61
|
-
? { onBackgroundReconcileError: input.onBackgroundReconcileError }
|
|
62
|
-
: {}),
|
|
63
|
-
});
|
|
64
|
-
resolverMesh = mesh;
|
|
37
|
+
detachResolver = input.endpoint.client.attachRouteResolver(mesh);
|
|
38
|
+
serving = mesh.serve();
|
|
65
39
|
}
|
|
66
40
|
catch (error) {
|
|
67
|
-
|
|
68
|
-
await
|
|
41
|
+
detachResolver?.();
|
|
42
|
+
await mesh.close().catch(() => undefined);
|
|
69
43
|
throw error;
|
|
70
44
|
}
|
|
71
|
-
const serving = mesh.serve();
|
|
72
45
|
let closeTask;
|
|
73
46
|
const close = () => {
|
|
74
|
-
|
|
75
|
-
closeTask ??= closeOwner(mesh, client, serving, rootOwner);
|
|
47
|
+
closeTask ??= closeMesh(mesh, serving, detachResolver);
|
|
76
48
|
return closeTask;
|
|
77
49
|
};
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
return stopUnexpectedOwner(mesh, error);
|
|
81
|
-
};
|
|
82
|
-
const closed = serving.then(() => closeTask ?? stopUnexpected(new Error('Runtime Host Peer Mesh owner stopped unexpectedly')), (error) => closeTask ?? stopUnexpected(error));
|
|
50
|
+
const closed = serving.then(() => closeTask ??
|
|
51
|
+
stopUnexpectedMesh(mesh, detachResolver, new Error('Peer Mesh stopped unexpectedly')), (error) => closeTask ?? stopUnexpectedMesh(mesh, detachResolver, error));
|
|
83
52
|
void closed.catch(() => undefined);
|
|
84
|
-
return Object.freeze({
|
|
85
|
-
client,
|
|
86
|
-
mesh,
|
|
87
|
-
closed,
|
|
88
|
-
close,
|
|
89
|
-
});
|
|
53
|
+
return Object.freeze({ mesh, closed, close });
|
|
90
54
|
}
|
|
91
|
-
async function
|
|
55
|
+
async function stopUnexpectedMesh(mesh, detachResolver, error) {
|
|
56
|
+
detachResolver();
|
|
92
57
|
try {
|
|
93
58
|
await mesh.close();
|
|
94
59
|
}
|
|
95
60
|
catch (closeError) {
|
|
96
|
-
throw new AggregateError([error, closeError], '
|
|
61
|
+
throw new AggregateError([error, closeError], 'Peer Mesh failed to stop');
|
|
97
62
|
}
|
|
98
63
|
throw error;
|
|
99
64
|
}
|
|
100
|
-
async function
|
|
65
|
+
async function closeMesh(mesh, serving, detachResolver) {
|
|
101
66
|
const errors = [];
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
await client.close().catch((error) => {
|
|
109
|
-
errors.push(error);
|
|
110
|
-
});
|
|
111
|
-
await rootOwner.close().catch((error) => {
|
|
112
|
-
errors.push(error);
|
|
113
|
-
});
|
|
67
|
+
detachResolver();
|
|
68
|
+
await mesh.close().catch((error) => errors.push(error));
|
|
69
|
+
await serving.catch((error) => errors.push(error));
|
|
70
|
+
throwCollected(errors, 'Unable to close Peer Mesh');
|
|
71
|
+
}
|
|
72
|
+
function throwCollected(errors, message) {
|
|
114
73
|
if (errors.length === 1)
|
|
115
74
|
throw errors[0];
|
|
116
75
|
if (errors.length > 1)
|
|
117
|
-
throw new AggregateError(errors,
|
|
76
|
+
throw new AggregateError(errors, message);
|
|
118
77
|
}
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
import { chmod, lstat, mkdir, open, readFile, rename, unlink } from 'node:fs/promises';
|
|
20
20
|
import { dirname, join } from 'node:path';
|
|
21
21
|
import { acquireFileLifetimeOwner, } from '@maka/storage/file-lifetime-owner';
|
|
22
|
-
import {
|
|
22
|
+
import { decodeSignedPeerMeshMemberAdvertisement, decodeSignedPeerMeshRoster, PEER_MESH_MAX_INVITATION_RECORDS, PEER_MESH_MAX_MEMBERS, PEER_MESH_MAX_MESHES, PEER_MESH_MAX_PENDING_INVITATIONS, validatePeerMeshInvitation, validatePeerMeshAuthorityKeyPair, } from './model.js';
|
|
23
|
+
import { decodeSignedPeerReachabilityLease, } from '../peer-reachability/index.js';
|
|
23
24
|
import { canonicalPeerMeshDisplayName } from './display-name.js';
|
|
24
25
|
const STATE_FILE = 'peer-mesh.json';
|
|
25
26
|
const LOCK_FILE = 'peer-mesh.owner';
|
|
@@ -75,6 +76,7 @@ class PeerMeshStateStoreImpl {
|
|
|
75
76
|
#failure;
|
|
76
77
|
#closeTask;
|
|
77
78
|
#closed = false;
|
|
79
|
+
#listeners = new Set();
|
|
78
80
|
constructor(dataRoot, localPeerId, owner, state) {
|
|
79
81
|
this.localPeerId = localPeerId;
|
|
80
82
|
this.owner = owner;
|
|
@@ -91,6 +93,11 @@ class PeerMeshStateStoreImpl {
|
|
|
91
93
|
this.#assertOpen();
|
|
92
94
|
return this.#state;
|
|
93
95
|
}
|
|
96
|
+
subscribe(listener) {
|
|
97
|
+
this.#assertOpen();
|
|
98
|
+
this.#listeners.add(listener);
|
|
99
|
+
return () => this.#listeners.delete(listener);
|
|
100
|
+
}
|
|
94
101
|
mutate(operation) {
|
|
95
102
|
this.#assertOpen();
|
|
96
103
|
const task = this.#tail.then(async () => {
|
|
@@ -99,12 +106,20 @@ class PeerMeshStateStoreImpl {
|
|
|
99
106
|
const updated = await operation(this.#state);
|
|
100
107
|
if (updated.state === this.#state)
|
|
101
108
|
return updated.result;
|
|
102
|
-
const candidate =
|
|
109
|
+
const candidate = pruneUnreferencedEvidence(updated.state, this.localPeerId);
|
|
103
110
|
const canonical = decodePeerMeshStoredState(candidate, this.localPeerId);
|
|
104
111
|
assertStateAdvance(this.#state.meshes, canonical.meshes, this.localPeerId);
|
|
105
112
|
try {
|
|
106
113
|
await writeState(this.#path, this.localPeerId, canonical);
|
|
107
114
|
this.#state = canonical;
|
|
115
|
+
for (const listener of this.#listeners) {
|
|
116
|
+
try {
|
|
117
|
+
listener();
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// Persisted state remains authoritative when an observer fails.
|
|
121
|
+
}
|
|
122
|
+
}
|
|
108
123
|
}
|
|
109
124
|
catch (error) {
|
|
110
125
|
if (error instanceof PeerMeshPostCommitError) {
|
|
@@ -126,6 +141,7 @@ class PeerMeshStateStoreImpl {
|
|
|
126
141
|
}
|
|
127
142
|
async #close() {
|
|
128
143
|
this.#closed = true;
|
|
144
|
+
this.#listeners.clear();
|
|
129
145
|
await this.#tail;
|
|
130
146
|
await this.owner.close();
|
|
131
147
|
}
|
|
@@ -136,16 +152,14 @@ class PeerMeshStateStoreImpl {
|
|
|
136
152
|
throw this.#failure;
|
|
137
153
|
}
|
|
138
154
|
}
|
|
139
|
-
export function decodePeerMeshState(value, localPeerId
|
|
155
|
+
export function decodePeerMeshState(value, localPeerId) {
|
|
140
156
|
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
141
157
|
throw new Error('Invalid Peer Mesh state');
|
|
142
158
|
}
|
|
143
159
|
const record = value;
|
|
144
160
|
const expectedKeys = record.role === 'authority'
|
|
145
161
|
? ['role', 'roster', 'authorityPrivateKey', 'invitations']
|
|
146
|
-
:
|
|
147
|
-
? ['role', 'roster', 'authority']
|
|
148
|
-
: ['role', 'roster', 'authority', 'desiredMembership'];
|
|
162
|
+
: ['role', 'roster', 'desiredMembership'];
|
|
149
163
|
if (Object.keys(record).length !== expectedKeys.length ||
|
|
150
164
|
expectedKeys.some((key) => !Object.hasOwn(record, key))) {
|
|
151
165
|
throw new Error('Invalid Peer Mesh state');
|
|
@@ -155,8 +169,8 @@ export function decodePeerMeshState(value, localPeerId, legacyReplica = false) {
|
|
|
155
169
|
}
|
|
156
170
|
const roster = decodeSignedPeerMeshRoster(record.roster);
|
|
157
171
|
if (record.role === 'authority') {
|
|
158
|
-
if (
|
|
159
|
-
throw new Error('Peer Mesh authority
|
|
172
|
+
if (roster.roster.authorityPeerId !== localPeerId) {
|
|
173
|
+
throw new Error('Peer Mesh authority identity does not match its roster');
|
|
160
174
|
}
|
|
161
175
|
const privateKey = boundedString(record.authorityPrivateKey, 'authorityPrivateKey', 256);
|
|
162
176
|
validatePeerMeshAuthorityKeyPair({
|
|
@@ -170,22 +184,17 @@ export function decodePeerMeshState(value, localPeerId, legacyReplica = false) {
|
|
|
170
184
|
invitations: Object.freeze(decodeInvitations(record.invitations)),
|
|
171
185
|
});
|
|
172
186
|
}
|
|
173
|
-
const authority = decodeAuthorityTarget(record.authority);
|
|
174
|
-
if (!roster.roster.members.includes(authority.peerId)) {
|
|
175
|
-
throw new Error('Peer Mesh authority is not present in its roster');
|
|
176
|
-
}
|
|
177
187
|
return Object.freeze({
|
|
178
188
|
role: 'replica',
|
|
179
|
-
authority,
|
|
180
189
|
roster,
|
|
181
|
-
desiredMembership:
|
|
190
|
+
desiredMembership: decodeDesiredMembership(record.desiredMembership),
|
|
182
191
|
});
|
|
183
192
|
}
|
|
184
|
-
function decodePeerMeshStates(value, localPeerId
|
|
193
|
+
function decodePeerMeshStates(value, localPeerId) {
|
|
185
194
|
if (!Array.isArray(value) || value.length > PEER_MESH_MAX_MESHES) {
|
|
186
195
|
throw new Error('Invalid Peer Mesh state collection');
|
|
187
196
|
}
|
|
188
|
-
const states = value.map((state) => decodePeerMeshState(state, localPeerId
|
|
197
|
+
const states = value.map((state) => decodePeerMeshState(state, localPeerId));
|
|
189
198
|
const meshIds = states.map(({ roster }) => roster.roster.meshId);
|
|
190
199
|
if (new Set(meshIds).size !== meshIds.length) {
|
|
191
200
|
throw new Error('Duplicate Peer Mesh state');
|
|
@@ -243,49 +252,28 @@ async function readState(path, expectedLocalPeerId) {
|
|
|
243
252
|
throw new Error('Invalid Peer Mesh state document');
|
|
244
253
|
}
|
|
245
254
|
const record = document;
|
|
246
|
-
|
|
247
|
-
Object.keys(record).length
|
|
248
|
-
Object.hasOwn(record, 'localPeerId')
|
|
249
|
-
Object.hasOwn(record, '
|
|
250
|
-
|
|
251
|
-
Object.
|
|
252
|
-
Object.hasOwn(record, '
|
|
253
|
-
Object.hasOwn(record, '
|
|
254
|
-
Object.hasOwn(record, '
|
|
255
|
-
const versionThree = record.version === 3 &&
|
|
256
|
-
Object.keys(record).length === 5 &&
|
|
257
|
-
Object.hasOwn(record, 'localPeerId') &&
|
|
258
|
-
Object.hasOwn(record, 'meshes') &&
|
|
259
|
-
Object.hasOwn(record, 'routes') &&
|
|
260
|
-
Object.hasOwn(record, 'transitMeshId');
|
|
261
|
-
const versionFour = record.version === 4 &&
|
|
262
|
-
Object.keys(record).length === 6 &&
|
|
263
|
-
Object.hasOwn(record, 'localPeerId') &&
|
|
264
|
-
Object.hasOwn(record, 'displayName') &&
|
|
265
|
-
Object.hasOwn(record, 'meshes') &&
|
|
266
|
-
Object.hasOwn(record, 'routes') &&
|
|
267
|
-
Object.hasOwn(record, 'transitMeshId');
|
|
268
|
-
const versionSix = record.version === 6 &&
|
|
269
|
-
Object.keys(record).length === 7 &&
|
|
270
|
-
Object.hasOwn(record, 'localPeerId') &&
|
|
271
|
-
Object.hasOwn(record, 'displayName') &&
|
|
272
|
-
Object.hasOwn(record, 'meshes') &&
|
|
273
|
-
Object.hasOwn(record, 'pendingJoins') &&
|
|
274
|
-
Object.hasOwn(record, 'routes') &&
|
|
275
|
-
Object.hasOwn(record, 'transitMeshId');
|
|
276
|
-
if (!versionOne && !versionTwo && !versionThree && !versionFour && !versionSix) {
|
|
255
|
+
if (record.version !== 7 ||
|
|
256
|
+
Object.keys(record).length !== 8 ||
|
|
257
|
+
!Object.hasOwn(record, 'localPeerId') ||
|
|
258
|
+
!Object.hasOwn(record, 'displayName') ||
|
|
259
|
+
!Object.hasOwn(record, 'meshes') ||
|
|
260
|
+
!Object.hasOwn(record, 'pendingJoins') ||
|
|
261
|
+
!Object.hasOwn(record, 'reachability') ||
|
|
262
|
+
!Object.hasOwn(record, 'advertisements') ||
|
|
263
|
+
!Object.hasOwn(record, 'transitMeshId')) {
|
|
277
264
|
throw new Error('Unsupported Peer Mesh state document');
|
|
278
265
|
}
|
|
279
266
|
if (boundedString(record.localPeerId, 'localPeerId', 256) !== expectedLocalPeerId) {
|
|
280
267
|
throw new Error('Peer Mesh state belongs to a different peer identity');
|
|
281
268
|
}
|
|
282
269
|
return decodePeerMeshStoredState({
|
|
283
|
-
displayName:
|
|
270
|
+
displayName: record.displayName,
|
|
284
271
|
meshes: record.meshes,
|
|
285
|
-
pendingJoins:
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
272
|
+
pendingJoins: record.pendingJoins,
|
|
273
|
+
reachability: record.reachability,
|
|
274
|
+
advertisements: record.advertisements,
|
|
275
|
+
transitMeshId: record.transitMeshId,
|
|
276
|
+
}, expectedLocalPeerId);
|
|
289
277
|
}
|
|
290
278
|
catch (error) {
|
|
291
279
|
if (isNodeError(error, 'ENOENT')) {
|
|
@@ -293,7 +281,8 @@ async function readState(path, expectedLocalPeerId) {
|
|
|
293
281
|
displayName: null,
|
|
294
282
|
meshes: Object.freeze([]),
|
|
295
283
|
pendingJoins: Object.freeze([]),
|
|
296
|
-
|
|
284
|
+
reachability: Object.freeze([]),
|
|
285
|
+
advertisements: Object.freeze([]),
|
|
297
286
|
transitMeshId: null,
|
|
298
287
|
});
|
|
299
288
|
}
|
|
@@ -312,7 +301,7 @@ async function pathExists(path) {
|
|
|
312
301
|
}
|
|
313
302
|
}
|
|
314
303
|
async function writeState(path, localPeerId, state) {
|
|
315
|
-
const document = `${JSON.stringify({ version:
|
|
304
|
+
const document = `${JSON.stringify({ version: 7, localPeerId, ...state }, null, 2)}\n`;
|
|
316
305
|
if (Buffer.byteLength(document) > MAX_STATE_BYTES)
|
|
317
306
|
throw new Error('Peer Mesh state is too large');
|
|
318
307
|
const temporary = `${path}.tmp`;
|
|
@@ -401,35 +390,53 @@ function decodeInvitations(value) {
|
|
|
401
390
|
}
|
|
402
391
|
return invitations;
|
|
403
392
|
}
|
|
404
|
-
function
|
|
393
|
+
function decodeReachability(value, meshes) {
|
|
405
394
|
if (!Array.isArray(value) || value.length > PEER_MESH_MAX_MESHES * PEER_MESH_MAX_MEMBERS) {
|
|
406
|
-
throw new Error('Invalid Peer Mesh
|
|
395
|
+
throw new Error('Invalid Peer Mesh reachability');
|
|
407
396
|
}
|
|
408
|
-
const
|
|
409
|
-
const peerIds =
|
|
397
|
+
const leases = value.map(decodeSignedPeerReachabilityLease);
|
|
398
|
+
const peerIds = leases.map(({ lease }) => lease.peerId);
|
|
410
399
|
const knownPeers = new Set(meshes
|
|
411
400
|
.filter(({ roster }) => !roster.roster.closed)
|
|
412
401
|
.flatMap(({ roster }) => roster.roster.members));
|
|
413
402
|
if (new Set(peerIds).size !== peerIds.length ||
|
|
414
403
|
peerIds.some((peerId) => !knownPeers.has(peerId))) {
|
|
415
|
-
throw new Error('Invalid Peer Mesh
|
|
404
|
+
throw new Error('Invalid Peer Mesh reachability');
|
|
416
405
|
}
|
|
417
|
-
return Object.freeze(
|
|
406
|
+
return Object.freeze(leases);
|
|
407
|
+
}
|
|
408
|
+
function decodeAdvertisements(value, meshes) {
|
|
409
|
+
if (!Array.isArray(value) || value.length > PEER_MESH_MAX_MESHES * PEER_MESH_MAX_MEMBERS) {
|
|
410
|
+
throw new Error('Invalid Peer Mesh member advertisements');
|
|
411
|
+
}
|
|
412
|
+
const advertisements = value.map(decodeSignedPeerMeshMemberAdvertisement);
|
|
413
|
+
const keys = advertisements.map(({ advertisement }) => `${advertisement.meshId}\n${advertisement.peerId}`);
|
|
414
|
+
if (new Set(keys).size !== keys.length ||
|
|
415
|
+
advertisements.some(({ advertisement }) => {
|
|
416
|
+
const mesh = meshes.find(({ roster }) => roster.roster.meshId === advertisement.meshId);
|
|
417
|
+
return (!mesh ||
|
|
418
|
+
mesh.roster.roster.closed ||
|
|
419
|
+
!mesh.roster.roster.members.includes(advertisement.peerId));
|
|
420
|
+
})) {
|
|
421
|
+
throw new Error('Invalid Peer Mesh member advertisements');
|
|
422
|
+
}
|
|
423
|
+
return Object.freeze(advertisements);
|
|
418
424
|
}
|
|
419
|
-
function decodePeerMeshStoredState(value, localPeerId
|
|
425
|
+
function decodePeerMeshStoredState(value, localPeerId) {
|
|
420
426
|
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
421
427
|
throw new Error('Invalid Peer Mesh state document');
|
|
422
428
|
}
|
|
423
429
|
const record = value;
|
|
424
|
-
if (Object.keys(record).length !==
|
|
430
|
+
if (Object.keys(record).length !== 6 ||
|
|
425
431
|
!Object.hasOwn(record, 'displayName') ||
|
|
426
432
|
!Object.hasOwn(record, 'meshes') ||
|
|
427
433
|
!Object.hasOwn(record, 'pendingJoins') ||
|
|
428
|
-
!Object.hasOwn(record, '
|
|
434
|
+
!Object.hasOwn(record, 'reachability') ||
|
|
435
|
+
!Object.hasOwn(record, 'advertisements') ||
|
|
429
436
|
!Object.hasOwn(record, 'transitMeshId')) {
|
|
430
437
|
throw new Error('Invalid Peer Mesh state document');
|
|
431
438
|
}
|
|
432
|
-
const meshes = decodePeerMeshStates(record.meshes, localPeerId
|
|
439
|
+
const meshes = decodePeerMeshStates(record.meshes, localPeerId);
|
|
433
440
|
const pendingJoins = decodePendingJoins(record.pendingJoins, meshes, localPeerId);
|
|
434
441
|
const displayName = record.displayName === null ? null : canonicalPeerMeshDisplayName(record.displayName);
|
|
435
442
|
const transitMeshId = record.transitMeshId === null
|
|
@@ -444,7 +451,8 @@ function decodePeerMeshStoredState(value, localPeerId, legacyReplica = false) {
|
|
|
444
451
|
displayName,
|
|
445
452
|
meshes,
|
|
446
453
|
pendingJoins,
|
|
447
|
-
|
|
454
|
+
reachability: decodeReachability(record.reachability, meshes),
|
|
455
|
+
advertisements: decodeAdvertisements(record.advertisements, meshes),
|
|
448
456
|
transitMeshId,
|
|
449
457
|
});
|
|
450
458
|
}
|
|
@@ -496,18 +504,23 @@ function decodePendingJoinPhase(value) {
|
|
|
496
504
|
}
|
|
497
505
|
return value;
|
|
498
506
|
}
|
|
499
|
-
function
|
|
507
|
+
function pruneUnreferencedEvidence(state, localPeerId) {
|
|
500
508
|
const knownPeers = new Set(state.meshes
|
|
501
509
|
.filter(({ roster }) => !roster.roster.closed)
|
|
502
510
|
.flatMap(({ roster }) => roster.roster.members));
|
|
503
|
-
const
|
|
511
|
+
const reachability = state.reachability.filter(({ lease }) => knownPeers.has(lease.peerId));
|
|
512
|
+
const advertisements = state.advertisements.filter(({ advertisement }) => state.meshes.some(({ roster }) => !roster.roster.closed &&
|
|
513
|
+
roster.roster.meshId === advertisement.meshId &&
|
|
514
|
+
roster.roster.members.includes(advertisement.peerId)));
|
|
504
515
|
const transitMeshId = state.meshes.some((mesh) => mesh.roster.roster.meshId === state.transitMeshId &&
|
|
505
516
|
isActivePeerMeshMembership(mesh, localPeerId))
|
|
506
517
|
? state.transitMeshId
|
|
507
518
|
: null;
|
|
508
|
-
return
|
|
519
|
+
return reachability.length === state.reachability.length &&
|
|
520
|
+
advertisements.length === state.advertisements.length &&
|
|
521
|
+
transitMeshId === state.transitMeshId
|
|
509
522
|
? state
|
|
510
|
-
: { ...state,
|
|
523
|
+
: { ...state, reachability, advertisements, transitMeshId };
|
|
511
524
|
}
|
|
512
525
|
function boundedString(value, label, max) {
|
|
513
526
|
if (typeof value !== 'string' || value.length === 0 || value.length > max) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
export { authenticateSignedPeerReachabilityLease, canonicalPeerReachabilityLease, decodeSignedPeerReachabilityLease, PEER_REACHABILITY_LEASE_TTL_MS, PEER_REACHABILITY_MAX_CLOCK_SKEW_MS, PEER_REACHABILITY_MAX_LIFETIME_MS, PEER_REACHABILITY_MAX_RECORD_BYTES, PEER_REACHABILITY_MAX_ROUTES_PER_CLASS, PEER_REACHABILITY_RECOVERY_HORIZON_MS, PEER_REACHABILITY_REFRESH_LEAD_MS, isPeerReachabilityLeaseCurrent, isPeerReachabilityLeaseRecoverable, peerReachabilityLeaseReceipt, peerReachabilityLeaseSigningBytes, samePeerReachabilityRoutes, verifySignedPeerReachabilityLease, } from './model.js';
|
|
20
|
+
export { PeerReachabilityPersistenceError, PeerReachabilityPostCommitError, } from './publisher.js';
|
|
21
|
+
export { openRuntimeHostPeerEndpointOwner, } from './owner.js';
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
export const PEER_REACHABILITY_LEASE_TTL_MS = 5 * 60 * 1_000;
|
|
20
|
+
export const PEER_REACHABILITY_REFRESH_LEAD_MS = 60 * 1_000;
|
|
21
|
+
export const PEER_REACHABILITY_MAX_LIFETIME_MS = 10 * 60 * 1_000;
|
|
22
|
+
export const PEER_REACHABILITY_MAX_CLOCK_SKEW_MS = 2 * 60 * 1_000;
|
|
23
|
+
export const PEER_REACHABILITY_RECOVERY_HORIZON_MS = 24 * 60 * 60 * 1_000;
|
|
24
|
+
export const PEER_REACHABILITY_MAX_ROUTES_PER_CLASS = 16;
|
|
25
|
+
export const PEER_REACHABILITY_MAX_RECORD_BYTES = 48 * 1_024;
|
|
26
|
+
const PEER_ID_MAX_BYTES = 256;
|
|
27
|
+
const ADDRESS_MAX_BYTES = 2 * 1_024;
|
|
28
|
+
const PROOF_MAX_BYTES = 256;
|
|
29
|
+
export function canonicalPeerReachabilityLease(value) {
|
|
30
|
+
const record = exactRecord(value, 'peer reachability lease', [
|
|
31
|
+
'version',
|
|
32
|
+
'peerId',
|
|
33
|
+
'revision',
|
|
34
|
+
'issuedAt',
|
|
35
|
+
'expiresAt',
|
|
36
|
+
'directRoutes',
|
|
37
|
+
'coordinationRoutes',
|
|
38
|
+
]);
|
|
39
|
+
if (record.version !== 1)
|
|
40
|
+
throw new Error('Unsupported peer reachability lease version');
|
|
41
|
+
const issuedAt = integer(record.issuedAt, 'issuedAt', 1);
|
|
42
|
+
const expiresAt = integer(record.expiresAt, 'expiresAt', issuedAt + 1);
|
|
43
|
+
if (expiresAt - issuedAt > PEER_REACHABILITY_MAX_LIFETIME_MS) {
|
|
44
|
+
throw new Error('Peer reachability lease lifetime is too long');
|
|
45
|
+
}
|
|
46
|
+
const lease = Object.freeze({
|
|
47
|
+
version: 1,
|
|
48
|
+
peerId: token(record.peerId, 'peerId', PEER_ID_MAX_BYTES),
|
|
49
|
+
revision: integer(record.revision, 'revision', 1),
|
|
50
|
+
issuedAt,
|
|
51
|
+
expiresAt,
|
|
52
|
+
directRoutes: Object.freeze(addresses(record.directRoutes, 'directRoutes')),
|
|
53
|
+
coordinationRoutes: Object.freeze(addresses(record.coordinationRoutes, 'coordinationRoutes')),
|
|
54
|
+
});
|
|
55
|
+
const directRoutes = new Set(lease.directRoutes);
|
|
56
|
+
if (lease.coordinationRoutes.some((route) => directRoutes.has(route))) {
|
|
57
|
+
throw new Error('Peer reachability route cannot belong to more than one class');
|
|
58
|
+
}
|
|
59
|
+
if (peerReachabilityLeaseSigningBytes(lease).byteLength > PEER_REACHABILITY_MAX_RECORD_BYTES) {
|
|
60
|
+
throw new Error('Peer reachability lease is too large');
|
|
61
|
+
}
|
|
62
|
+
return lease;
|
|
63
|
+
}
|
|
64
|
+
export function decodeSignedPeerReachabilityLease(value) {
|
|
65
|
+
const record = exactRecord(value, 'signed peer reachability lease', [
|
|
66
|
+
'lease',
|
|
67
|
+
'publicKey',
|
|
68
|
+
'signature',
|
|
69
|
+
]);
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
lease: canonicalPeerReachabilityLease(record.lease),
|
|
72
|
+
publicKey: proof(record.publicKey, 'publicKey'),
|
|
73
|
+
signature: proof(record.signature, 'signature'),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
export function verifySignedPeerReachabilityLease(input) {
|
|
77
|
+
const signed = authenticateSignedPeerReachabilityLease(input);
|
|
78
|
+
if (signed.lease.issuedAt > input.now + PEER_REACHABILITY_MAX_CLOCK_SKEW_MS) {
|
|
79
|
+
throw new Error('Peer reachability lease was issued too far in the future');
|
|
80
|
+
}
|
|
81
|
+
if (!input.allowExpired && signed.lease.expiresAt <= input.now) {
|
|
82
|
+
throw new Error('Peer reachability lease has expired');
|
|
83
|
+
}
|
|
84
|
+
return signed;
|
|
85
|
+
}
|
|
86
|
+
export function authenticateSignedPeerReachabilityLease(input) {
|
|
87
|
+
const signed = decodeSignedPeerReachabilityLease(input.value);
|
|
88
|
+
if (signed.lease.peerId !== input.expectedPeerId) {
|
|
89
|
+
throw new Error('Peer reachability lease belongs to a different peer');
|
|
90
|
+
}
|
|
91
|
+
if (!input.verifyIdentity(signed.lease.peerId, peerReachabilityLeaseSigningBytes(signed.lease), {
|
|
92
|
+
publicKey: Buffer.from(signed.publicKey, 'base64url'),
|
|
93
|
+
signature: Buffer.from(signed.signature, 'base64url'),
|
|
94
|
+
})) {
|
|
95
|
+
throw new Error('Peer reachability lease signature is invalid');
|
|
96
|
+
}
|
|
97
|
+
return signed;
|
|
98
|
+
}
|
|
99
|
+
export function peerReachabilityLeaseSigningBytes(lease) {
|
|
100
|
+
return Buffer.from(`maka.peer-reachability.lease.v1\n${JSON.stringify({
|
|
101
|
+
coordinationRoutes: lease.coordinationRoutes,
|
|
102
|
+
directRoutes: lease.directRoutes,
|
|
103
|
+
expiresAt: lease.expiresAt,
|
|
104
|
+
issuedAt: lease.issuedAt,
|
|
105
|
+
peerId: lease.peerId,
|
|
106
|
+
revision: lease.revision,
|
|
107
|
+
version: lease.version,
|
|
108
|
+
})}`);
|
|
109
|
+
}
|
|
110
|
+
export function samePeerReachabilityRoutes(lease, reachability) {
|
|
111
|
+
return (sameStrings(lease.directRoutes, reachability.listenAddresses) &&
|
|
112
|
+
sameStrings(lease.coordinationRoutes, reachability.activeCoordinationRelays));
|
|
113
|
+
}
|
|
114
|
+
export function peerReachabilityLeaseReceipt(input) {
|
|
115
|
+
const { signed, previous } = input;
|
|
116
|
+
if (previous?.peerId === signed.lease.peerId &&
|
|
117
|
+
previous.revision === signed.lease.revision &&
|
|
118
|
+
previous.signature === signed.signature) {
|
|
119
|
+
return previous;
|
|
120
|
+
}
|
|
121
|
+
if (!Number.isFinite(input.wallNow) || !Number.isFinite(input.monotonicNow)) {
|
|
122
|
+
throw new Error('Invalid peer reachability receipt clock');
|
|
123
|
+
}
|
|
124
|
+
const signedLifetime = signed.lease.expiresAt - signed.lease.issuedAt;
|
|
125
|
+
const wallRemaining = Math.max(0, signed.lease.expiresAt - input.wallNow);
|
|
126
|
+
return Object.freeze({
|
|
127
|
+
peerId: signed.lease.peerId,
|
|
128
|
+
revision: signed.lease.revision,
|
|
129
|
+
signature: signed.signature,
|
|
130
|
+
currentUntil: input.monotonicNow + Math.min(signedLifetime, wallRemaining),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
export function isPeerReachabilityLeaseCurrent(signed, receipt, monotonicNow) {
|
|
134
|
+
return Boolean(receipt &&
|
|
135
|
+
receipt.peerId === signed.lease.peerId &&
|
|
136
|
+
receipt.revision === signed.lease.revision &&
|
|
137
|
+
receipt.signature === signed.signature &&
|
|
138
|
+
receipt.currentUntil > monotonicNow);
|
|
139
|
+
}
|
|
140
|
+
export function isPeerReachabilityLeaseRecoverable(lease, now) {
|
|
141
|
+
return lease.expiresAt > now - PEER_REACHABILITY_RECOVERY_HORIZON_MS;
|
|
142
|
+
}
|
|
143
|
+
function exactRecord(value, label, keys) {
|
|
144
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
145
|
+
throw new Error(`Invalid ${label}`);
|
|
146
|
+
}
|
|
147
|
+
const record = value;
|
|
148
|
+
if (Object.keys(record).length !== keys.length ||
|
|
149
|
+
keys.some((key) => !Object.hasOwn(record, key))) {
|
|
150
|
+
throw new Error(`Invalid ${label}`);
|
|
151
|
+
}
|
|
152
|
+
return record;
|
|
153
|
+
}
|
|
154
|
+
function token(value, label, maxBytes) {
|
|
155
|
+
if (typeof value !== 'string' ||
|
|
156
|
+
value.length === 0 ||
|
|
157
|
+
Buffer.byteLength(value, 'utf8') > maxBytes ||
|
|
158
|
+
/\s|[\u0000-\u001f\u007f]/u.test(value)) {
|
|
159
|
+
throw new Error(`Invalid peer reachability ${label}`);
|
|
160
|
+
}
|
|
161
|
+
return value;
|
|
162
|
+
}
|
|
163
|
+
function integer(value, label, minimum) {
|
|
164
|
+
if (!Number.isSafeInteger(value) || value < minimum) {
|
|
165
|
+
throw new Error(`Invalid peer reachability ${label}`);
|
|
166
|
+
}
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
function addresses(value, label) {
|
|
170
|
+
if (!Array.isArray(value) || value.length > PEER_REACHABILITY_MAX_ROUTES_PER_CLASS) {
|
|
171
|
+
throw new Error(`Invalid peer reachability ${label}`);
|
|
172
|
+
}
|
|
173
|
+
const result = value.map((address) => {
|
|
174
|
+
if (typeof address !== 'string' ||
|
|
175
|
+
address.length === 0 ||
|
|
176
|
+
Buffer.byteLength(address, 'utf8') > ADDRESS_MAX_BYTES ||
|
|
177
|
+
!address.startsWith('/') ||
|
|
178
|
+
/\s|[\u0000-\u001f\u007f]/u.test(address)) {
|
|
179
|
+
throw new Error(`Invalid peer reachability ${label}`);
|
|
180
|
+
}
|
|
181
|
+
return address;
|
|
182
|
+
});
|
|
183
|
+
if (new Set(result).size !== result.length) {
|
|
184
|
+
throw new Error(`Duplicate peer reachability ${label}`);
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
function proof(value, label) {
|
|
189
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
190
|
+
throw new Error(`Invalid peer reachability ${label}`);
|
|
191
|
+
}
|
|
192
|
+
const bytes = Buffer.from(value, 'base64url');
|
|
193
|
+
if (bytes.length === 0 ||
|
|
194
|
+
bytes.length > PROOF_MAX_BYTES ||
|
|
195
|
+
bytes.toString('base64url') !== value) {
|
|
196
|
+
throw new Error(`Invalid peer reachability ${label}`);
|
|
197
|
+
}
|
|
198
|
+
return value;
|
|
199
|
+
}
|
|
200
|
+
function sameStrings(left, right) {
|
|
201
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
202
|
+
}
|