@coasys/ad4m 0.13.1-staging.5 → 0.13.1-staging.7
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/lib/index.cjs +1518 -739
- package/lib/index.js +1515 -740
- package/lib/src/Ad4mClient.d.ts +9 -0
- package/lib/src/Ad4mClient.js +17 -2
- package/lib/src/agent/Agent.d.ts +5 -1
- package/lib/src/agent/Agent.js +3 -0
- package/lib/src/agent/AgentClient.d.ts +3 -0
- package/lib/src/agent/AgentClient.js +3 -0
- package/lib/src/agent/AgentStatus.d.ts +2 -0
- package/lib/src/agent/AgentStatus.js +2 -0
- package/lib/src/apiClient.d.ts +73 -0
- package/lib/src/apiClient.js +269 -13
- package/lib/src/generated/api/AgentStatus.d.ts +5 -0
- package/lib/src/generated/api/AgentWithIdentity.d.ts +14 -0
- package/lib/src/generated/api/AgentWithIdentity.js +1 -0
- package/lib/src/generated/api/BotView.d.ts +7 -0
- package/lib/src/generated/api/BotView.js +2 -0
- package/lib/src/generated/api/ClaimOfferParams.d.ts +13 -0
- package/lib/src/generated/api/ClaimOfferParams.js +2 -0
- package/lib/src/generated/api/ClaimOfferResult.d.ts +14 -0
- package/lib/src/generated/api/ClaimOfferResult.js +1 -0
- package/lib/src/generated/api/ClaimView.d.ts +11 -0
- package/lib/src/generated/api/ClaimView.js +2 -0
- package/lib/src/generated/api/CosignInceptParams.d.ts +10 -0
- package/lib/src/generated/api/CosignInceptParams.js +1 -0
- package/lib/src/generated/api/DeviceView.d.ts +17 -0
- package/lib/src/generated/api/DeviceView.js +1 -0
- package/lib/src/generated/api/DidBotScope.d.ts +7 -0
- package/lib/src/generated/api/DidBotScope.js +2 -0
- package/lib/src/generated/api/DidDocument.d.ts +16 -0
- package/lib/src/generated/api/DidDocument.js +1 -0
- package/lib/src/generated/api/DidDocumentMetadata.d.ts +18 -0
- package/lib/src/generated/api/DidDocumentMetadata.js +2 -0
- package/lib/src/generated/api/DidResolutionMetadata.d.ts +51 -0
- package/lib/src/generated/api/DidResolutionMetadata.js +1 -0
- package/lib/src/generated/api/DidResolutionResult.d.ts +16 -0
- package/lib/src/generated/api/DidResolutionResult.js +1 -0
- package/lib/src/generated/api/DidVerificationMethod.d.ts +17 -0
- package/lib/src/generated/api/DidVerificationMethod.js +1 -0
- package/lib/src/generated/api/Events.d.ts +2 -0
- package/lib/src/generated/api/ForkRecord.d.ts +11 -0
- package/lib/src/generated/api/ForkRecord.js +2 -0
- package/lib/src/generated/api/IdentityForkEvent.d.ts +10 -0
- package/lib/src/generated/api/IdentityForkEvent.js +2 -0
- package/lib/src/generated/api/IdentityLog.d.ts +18 -0
- package/lib/src/generated/api/IdentityLog.js +1 -0
- package/lib/src/generated/api/IdentityStatus.d.ts +48 -0
- package/lib/src/generated/api/IdentityStatus.js +1 -0
- package/lib/src/generated/api/JoinStatus.d.ts +10 -0
- package/lib/src/generated/api/JoinStatus.js +2 -0
- package/lib/src/generated/api/JoinStatusParams.d.ts +6 -0
- package/lib/src/generated/api/JoinStatusParams.js +2 -0
- package/lib/src/generated/api/LogParams.d.ts +17 -0
- package/lib/src/generated/api/LogParams.js +2 -0
- package/lib/src/generated/api/Offer.d.ts +14 -0
- package/lib/src/generated/api/Offer.js +1 -0
- package/lib/src/generated/api/OfferKind.d.ts +4 -0
- package/lib/src/generated/api/OfferKind.js +2 -0
- package/lib/src/generated/api/OfferListing.d.ts +9 -0
- package/lib/src/generated/api/OfferListing.js +1 -0
- package/lib/src/generated/api/OfferOpened.d.ts +6 -0
- package/lib/src/generated/api/OfferOpened.js +2 -0
- package/lib/src/generated/api/OfferState.d.ts +21 -0
- package/lib/src/generated/api/OfferState.js +1 -0
- package/lib/src/generated/api/OffersParams.d.ts +11 -0
- package/lib/src/generated/api/OffersParams.js +1 -0
- package/lib/src/generated/api/OpenOfferParams.d.ts +7 -0
- package/lib/src/generated/api/OpenOfferParams.js +1 -0
- package/lib/src/generated/api/PublishParams.d.ts +11 -0
- package/lib/src/generated/api/PublishParams.js +1 -0
- package/lib/src/generated/api/RequestBody.d.ts +15 -0
- package/lib/src/generated/api/RequestBody.js +2 -0
- package/lib/src/generated/api/ResetLogParams.d.ts +9 -0
- package/lib/src/generated/api/ResetLogParams.js +2 -0
- package/lib/src/generated/api/ResolveParams.d.ts +18 -0
- package/lib/src/generated/api/ResolveParams.js +2 -0
- package/lib/src/generated/api/RpcMethods.d.ts +68 -1
- package/lib/src/generated/api/RpcMethods.js +6 -0
- package/lib/src/generated/api/SignedBy.d.ts +8 -0
- package/lib/src/generated/api/SignedBy.js +2 -0
- package/lib/src/generated/api/SignedHead.d.ts +14 -0
- package/lib/src/generated/api/SignedHead.js +2 -0
- package/lib/src/generated/api/SignedOffer.d.ts +10 -0
- package/lib/src/generated/api/SignedOffer.js +1 -0
- package/lib/src/generated/api/SignedRequest.d.ts +8 -0
- package/lib/src/generated/api/SignedRequest.js +1 -0
- package/lib/src/generated/api/SkippedSource.d.ts +7 -0
- package/lib/src/generated/api/SkippedSource.js +2 -0
- package/lib/src/generated/api/StoreBehind.d.ts +13 -0
- package/lib/src/generated/api/StoreBehind.js +2 -0
- package/lib/src/generated/api/StoreInfo.d.ts +15 -0
- package/lib/src/generated/api/StoreInfo.js +2 -0
- package/lib/src/generated/api/index.d.ts +38 -0
- package/lib/src/identity/IdentityClient.d.ts +70 -0
- package/lib/src/identity/IdentityClient.js +97 -0
- package/lib/src/index.d.ts +1 -0
- package/lib/src/index.js +1 -0
- package/lib/src/model/Ad4mModel.d.ts +11 -0
- package/lib/src/model/Ad4mModel.js +28 -14
- package/lib/src/model/ModelQueryBuilder.d.ts +2 -0
- package/lib/src/model/ModelQueryBuilder.js +36 -57
- package/lib/src/neighbourhood/MeshManager.d.ts +24 -0
- package/lib/src/neighbourhood/MeshManager.js +42 -1
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +10 -0
- package/lib/src/neighbourhood/NeighbourhoodClient.js +16 -0
- package/lib/src/neighbourhood/Session.js +2 -0
- package/lib/src/neighbourhood/SfuManager.d.ts +30 -3
- package/lib/src/neighbourhood/SfuManager.js +78 -41
- package/lib/src/perspectives/FlowInstance.js +6 -4
- package/lib/src/perspectives/LiveQuery.d.ts +40 -4
- package/lib/src/perspectives/LiveQuery.js +94 -10
- package/lib/src/perspectives/PerspectiveClient.d.ts +4 -2
- package/lib/src/perspectives/PerspectiveClient.js +6 -4
- package/lib/src/perspectives/PerspectiveProxy.d.ts +15 -3
- package/lib/src/perspectives/PerspectiveProxy.js +33 -16
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +35 -2
- package/lib/src/perspectives/WakerSubscriptionManager.js +93 -9
- package/lib/src/shacl/SHACLFlow.d.ts +5 -3
- package/lib/src/shacl/SHACLFlow.js +121 -5
- package/package.json +2 -2
- package/src/Ad4mClient.test.ts +64 -4
- package/src/Ad4mClient.ts +17 -0
- package/src/agent/Agent.ts +9 -1
- package/src/agent/AgentClient.ts +5 -1
- package/src/agent/AgentStatus.ts +4 -0
- package/src/apiClient.test.ts +769 -3
- package/src/apiClient.ts +268 -13
- package/src/generated/api/AgentStatus.ts +6 -1
- package/src/generated/api/AgentWithIdentity.ts +12 -0
- package/src/generated/api/BotView.ts +6 -0
- package/src/generated/api/ClaimOfferParams.ts +14 -0
- package/src/generated/api/ClaimOfferResult.ts +11 -0
- package/src/generated/api/ClaimView.ts +6 -0
- package/src/generated/api/CosignInceptParams.ts +11 -0
- package/src/generated/api/DeviceView.ts +12 -0
- package/src/generated/api/DidBotScope.ts +6 -0
- package/src/generated/api/DidDocument.ts +8 -0
- package/src/generated/api/DidDocumentMetadata.ts +19 -0
- package/src/generated/api/DidResolutionMetadata.ts +52 -0
- package/src/generated/api/DidResolutionResult.ts +13 -0
- package/src/generated/api/DidVerificationMethod.ts +15 -0
- package/src/generated/api/Events.ts +2 -0
- package/src/generated/api/ForkRecord.ts +9 -0
- package/src/generated/api/IdentityForkEvent.ts +7 -0
- package/src/generated/api/IdentityLog.ts +17 -0
- package/src/generated/api/IdentityStatus.ts +49 -0
- package/src/generated/api/JoinStatus.ts +7 -0
- package/src/generated/api/JoinStatusParams.ts +6 -0
- package/src/generated/api/LogParams.ts +18 -0
- package/src/generated/api/Offer.ts +8 -0
- package/src/generated/api/OfferKind.ts +6 -0
- package/src/generated/api/OfferListing.ts +8 -0
- package/src/generated/api/OfferOpened.ts +6 -0
- package/src/generated/api/OfferState.ts +7 -0
- package/src/generated/api/OffersParams.ts +11 -0
- package/src/generated/api/OpenOfferParams.ts +7 -0
- package/src/generated/api/PublishParams.ts +12 -0
- package/src/generated/api/RequestBody.ts +7 -0
- package/src/generated/api/ResetLogParams.ts +10 -0
- package/src/generated/api/ResolveParams.ts +19 -0
- package/src/generated/api/RpcMethods.ts +38 -1
- package/src/generated/api/SignedBy.ts +6 -0
- package/src/generated/api/SignedHead.ts +10 -0
- package/src/generated/api/SignedOffer.ts +8 -0
- package/src/generated/api/SignedRequest.ts +7 -0
- package/src/generated/api/SkippedSource.ts +6 -0
- package/src/generated/api/StoreBehind.ts +14 -0
- package/src/generated/api/StoreInfo.ts +16 -0
- package/src/generated/api/index.ts +39 -0
- package/src/identity/IdentityClient.ts +111 -0
- package/src/index.ts +1 -0
- package/src/model/Ad4mModel.test.ts +128 -364
- package/src/model/Ad4mModel.ts +27 -18
- package/src/model/ModelQueryBuilder.ts +39 -59
- package/src/model/write-round-trips.test.ts +324 -0
- package/src/neighbourhood/MeshManager.test.ts +87 -0
- package/src/neighbourhood/MeshManager.ts +50 -1
- package/src/neighbourhood/NeighbourhoodClient.test.ts +27 -0
- package/src/neighbourhood/NeighbourhoodClient.ts +19 -0
- package/src/neighbourhood/Session.test.ts +5 -0
- package/src/neighbourhood/Session.ts +2 -0
- package/src/neighbourhood/SfuManager.test.ts +293 -0
- package/src/neighbourhood/SfuManager.ts +92 -45
- package/src/perspectives/FlowInstance.ts +6 -4
- package/src/perspectives/LiveQuery.test.ts +173 -1
- package/src/perspectives/LiveQuery.ts +110 -10
- package/src/perspectives/PerspectiveClient.ts +6 -4
- package/src/perspectives/PerspectiveProxy.test.ts +24 -2
- package/src/perspectives/PerspectiveProxy.ts +35 -16
- package/src/perspectives/WakerSubscriptionManager.test.ts +225 -1
- package/src/perspectives/WakerSubscriptionManager.ts +97 -10
- package/src/shacl/SHACLFlow.test.ts +221 -0
- package/src/shacl/SHACLFlow.ts +121 -5
package/lib/src/Ad4mClient.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { PerspectiveClient } from './perspectives/PerspectiveClient';
|
|
|
5
5
|
import { RuntimeClient } from './runtime/RuntimeClient';
|
|
6
6
|
import { ExpressionClient } from './expression/ExpressionClient';
|
|
7
7
|
import { AIClient } from './ai/AIClient';
|
|
8
|
+
import { IdentityClient } from './identity/IdentityClient';
|
|
8
9
|
import { EventFilter } from './apiClient';
|
|
9
10
|
import type { EventMap, EventName } from './generated/api/Events';
|
|
10
11
|
/**
|
|
@@ -31,6 +32,8 @@ export declare class Ad4mClient {
|
|
|
31
32
|
get perspective(): PerspectiveClient;
|
|
32
33
|
get runtime(): RuntimeClient;
|
|
33
34
|
get ai(): AIClient;
|
|
35
|
+
/** The executor's identity calls (`did:scid:coasys:1`). */
|
|
36
|
+
get identity(): IdentityClient;
|
|
34
37
|
/**
|
|
35
38
|
* Call `handler` with every `type` event, or only those about
|
|
36
39
|
* `filter.perspective`. The payload is typed by the executor's event
|
|
@@ -45,6 +48,12 @@ export declare class Ad4mClient {
|
|
|
45
48
|
* new client. Returns a function that removes the callback.
|
|
46
49
|
*/
|
|
47
50
|
onAuthFailed(callback: (reason: string) => void): () => void;
|
|
51
|
+
/**
|
|
52
|
+
* Reconnect now if the connection to the executor dropped and the client is
|
|
53
|
+
* waiting out its backoff: for a host that knows the executor is back (it
|
|
54
|
+
* restarted it, or the device woke). A no-op while connected or connecting.
|
|
55
|
+
*/
|
|
56
|
+
reconnectNow(): void;
|
|
48
57
|
/** Close all event connections and clear in-memory caches */
|
|
49
58
|
close(): void;
|
|
50
59
|
}
|
package/lib/src/Ad4mClient.js
CHANGED
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _Ad4mClient_baseUrl, _Ad4mClient_token, _Ad4mClient_apiClient, _Ad4mClient_agentClient, _Ad4mClient_expressionClient, _Ad4mClient_languageClient, _Ad4mClient_neighbourhoodClient, _Ad4mClient_perspectiveClient, _Ad4mClient_runtimeClient, _Ad4mClient_aiClient;
|
|
12
|
+
var _Ad4mClient_baseUrl, _Ad4mClient_token, _Ad4mClient_apiClient, _Ad4mClient_agentClient, _Ad4mClient_expressionClient, _Ad4mClient_languageClient, _Ad4mClient_neighbourhoodClient, _Ad4mClient_perspectiveClient, _Ad4mClient_runtimeClient, _Ad4mClient_aiClient, _Ad4mClient_identityClient;
|
|
13
13
|
import { AgentClient } from './agent/AgentClient';
|
|
14
14
|
import { LanguageClient } from './language/LanguageClient';
|
|
15
15
|
import { NeighbourhoodClient } from './neighbourhood/NeighbourhoodClient';
|
|
@@ -17,6 +17,7 @@ import { PerspectiveClient } from './perspectives/PerspectiveClient';
|
|
|
17
17
|
import { RuntimeClient } from './runtime/RuntimeClient';
|
|
18
18
|
import { ExpressionClient } from './expression/ExpressionClient';
|
|
19
19
|
import { AIClient } from './ai/AIClient';
|
|
20
|
+
import { IdentityClient } from './identity/IdentityClient';
|
|
20
21
|
import { ApiClient } from './apiClient';
|
|
21
22
|
import { Ad4mModel } from './model/Ad4mModel';
|
|
22
23
|
/**
|
|
@@ -42,6 +43,7 @@ export class Ad4mClient {
|
|
|
42
43
|
_Ad4mClient_perspectiveClient.set(this, void 0);
|
|
43
44
|
_Ad4mClient_runtimeClient.set(this, void 0);
|
|
44
45
|
_Ad4mClient_aiClient.set(this, void 0);
|
|
46
|
+
_Ad4mClient_identityClient.set(this, void 0);
|
|
45
47
|
__classPrivateFieldSet(this, _Ad4mClient_baseUrl, baseUrl, "f");
|
|
46
48
|
__classPrivateFieldSet(this, _Ad4mClient_token, token, "f");
|
|
47
49
|
__classPrivateFieldSet(this, _Ad4mClient_apiClient, new ApiClient(baseUrl, token, options?.webSocketImpl, options?.fetchImpl), "f");
|
|
@@ -55,6 +57,7 @@ export class Ad4mClient {
|
|
|
55
57
|
__classPrivateFieldGet(this, _Ad4mClient_perspectiveClient, "f").setNeighbourhoodClient(__classPrivateFieldGet(this, _Ad4mClient_neighbourhoodClient, "f"));
|
|
56
58
|
__classPrivateFieldGet(this, _Ad4mClient_perspectiveClient, "f").setAIClient(__classPrivateFieldGet(this, _Ad4mClient_aiClient, "f"));
|
|
57
59
|
__classPrivateFieldSet(this, _Ad4mClient_runtimeClient, new RuntimeClient(baseUrl, token, __classPrivateFieldGet(this, _Ad4mClient_apiClient, "f")), "f");
|
|
60
|
+
__classPrivateFieldSet(this, _Ad4mClient_identityClient, new IdentityClient(baseUrl, token, __classPrivateFieldGet(this, _Ad4mClient_apiClient, "f")), "f");
|
|
58
61
|
// Register with AD4M DevTools if the bridge is installed (e.g. browser extension)
|
|
59
62
|
try {
|
|
60
63
|
const dt = globalThis.__AD4M_DEVTOOLS__;
|
|
@@ -86,6 +89,10 @@ export class Ad4mClient {
|
|
|
86
89
|
get ai() {
|
|
87
90
|
return __classPrivateFieldGet(this, _Ad4mClient_aiClient, "f");
|
|
88
91
|
}
|
|
92
|
+
/** The executor's identity calls (`did:scid:coasys:1`). */
|
|
93
|
+
get identity() {
|
|
94
|
+
return __classPrivateFieldGet(this, _Ad4mClient_identityClient, "f");
|
|
95
|
+
}
|
|
89
96
|
/**
|
|
90
97
|
* Call `handler` with every `type` event, or only those about
|
|
91
98
|
* `filter.perspective`. The payload is typed by the executor's event
|
|
@@ -104,10 +111,18 @@ export class Ad4mClient {
|
|
|
104
111
|
onAuthFailed(callback) {
|
|
105
112
|
return __classPrivateFieldGet(this, _Ad4mClient_apiClient, "f").onAuthFailed(callback);
|
|
106
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Reconnect now if the connection to the executor dropped and the client is
|
|
116
|
+
* waiting out its backoff: for a host that knows the executor is back (it
|
|
117
|
+
* restarted it, or the device woke). A no-op while connected or connecting.
|
|
118
|
+
*/
|
|
119
|
+
reconnectNow() {
|
|
120
|
+
__classPrivateFieldGet(this, _Ad4mClient_apiClient, "f").reconnectNow();
|
|
121
|
+
}
|
|
107
122
|
/** Close all event connections and clear in-memory caches */
|
|
108
123
|
close() {
|
|
109
124
|
__classPrivateFieldGet(this, _Ad4mClient_agentClient, "f").clearByDidCache();
|
|
110
125
|
__classPrivateFieldGet(this, _Ad4mClient_apiClient, "f").closeAll();
|
|
111
126
|
}
|
|
112
127
|
}
|
|
113
|
-
_Ad4mClient_baseUrl = new WeakMap(), _Ad4mClient_token = new WeakMap(), _Ad4mClient_apiClient = new WeakMap(), _Ad4mClient_agentClient = new WeakMap(), _Ad4mClient_expressionClient = new WeakMap(), _Ad4mClient_languageClient = new WeakMap(), _Ad4mClient_neighbourhoodClient = new WeakMap(), _Ad4mClient_perspectiveClient = new WeakMap(), _Ad4mClient_runtimeClient = new WeakMap(), _Ad4mClient_aiClient = new WeakMap();
|
|
128
|
+
_Ad4mClient_baseUrl = new WeakMap(), _Ad4mClient_token = new WeakMap(), _Ad4mClient_apiClient = new WeakMap(), _Ad4mClient_agentClient = new WeakMap(), _Ad4mClient_expressionClient = new WeakMap(), _Ad4mClient_languageClient = new WeakMap(), _Ad4mClient_neighbourhoodClient = new WeakMap(), _Ad4mClient_perspectiveClient = new WeakMap(), _Ad4mClient_runtimeClient = new WeakMap(), _Ad4mClient_aiClient = new WeakMap(), _Ad4mClient_identityClient = new WeakMap();
|
package/lib/src/agent/Agent.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Perspective } from "../perspectives/Perspective";
|
|
2
2
|
import type { Agent as WireAgent } from "../generated/api/Agent";
|
|
3
|
+
import type { AgentWithIdentity } from "../generated/api/AgentWithIdentity";
|
|
3
4
|
/** AD4M's representation of an Agent
|
|
4
5
|
*
|
|
5
6
|
* AD4M Agents are build around DIDs, which are used to identify and authenticate the Agent.
|
|
@@ -30,9 +31,12 @@ export declare class Agent {
|
|
|
30
31
|
perspective?: Perspective;
|
|
31
32
|
/** Address of the Language by which the Agent will receive DMs */
|
|
32
33
|
directMessageLanguage?: string;
|
|
34
|
+
/** The `did:scid` this agent speaks for (`agent.me()`); absent when it speaks for no one,
|
|
35
|
+
* and on agents read from the agent language. `did` stays the device `did:key`. */
|
|
36
|
+
identity?: string;
|
|
33
37
|
constructor(did: string, perspective?: Perspective);
|
|
34
38
|
/** Build an Agent (with its perspective's query helpers) from the executor's wire shape. */
|
|
35
|
-
static fromWire(wire: WireAgent): Agent;
|
|
39
|
+
static fromWire(wire: WireAgent | AgentWithIdentity): Agent;
|
|
36
40
|
}
|
|
37
41
|
declare const AgentExpression_base: abstract new (author?: string, timestamp?: string, data?: Agent, proof?: import("../expression/Expression").ExpressionProof) => import("../expression/Expression").ExpressionShape<Agent, import("../expression/Expression").ExpressionProof>;
|
|
38
42
|
export declare class AgentExpression extends AgentExpression_base {
|
package/lib/src/agent/Agent.js
CHANGED
|
@@ -30,6 +30,9 @@ export class Agent {
|
|
|
30
30
|
static fromWire(wire) {
|
|
31
31
|
const agent = new Agent(wire.did, Perspective.fromWire(wire.perspective));
|
|
32
32
|
agent.directMessageLanguage = wire.directMessageLanguage;
|
|
33
|
+
if ("identity" in wire && wire.identity) {
|
|
34
|
+
agent.identity = wire.identity;
|
|
35
|
+
}
|
|
33
36
|
return agent;
|
|
34
37
|
}
|
|
35
38
|
}
|
|
@@ -22,6 +22,9 @@ export declare class AgentClient {
|
|
|
22
22
|
constructor(baseUrl: string, token?: string, sharedApiClient?: ApiClient);
|
|
23
23
|
me(): Promise<Agent>;
|
|
24
24
|
status(): Promise<AgentStatus>;
|
|
25
|
+
/** Creates the agent's keys and DID on first run. The executor refuses (409)
|
|
26
|
+
* an agent that is already initialised — {@link unlock} it instead; a second
|
|
27
|
+
* `generate` never replaces the existing keys and DID. */
|
|
25
28
|
generate(passphrase: string, options?: CallOptions): Promise<AgentStatus>;
|
|
26
29
|
import(args: InitializeArgs): Promise<AgentStatus>;
|
|
27
30
|
lock(passphrase: string): Promise<AgentStatus>;
|
|
@@ -50,6 +50,9 @@ export class AgentClient {
|
|
|
50
50
|
const agentStatus = await __classPrivateFieldGet(this, _AgentClient_apiClient, "f").call('agent.status', {});
|
|
51
51
|
return new AgentStatus(agentStatus);
|
|
52
52
|
}
|
|
53
|
+
/** Creates the agent's keys and DID on first run. The executor refuses (409)
|
|
54
|
+
* an agent that is already initialised — {@link unlock} it instead; a second
|
|
55
|
+
* `generate` never replaces the existing keys and DID. */
|
|
53
56
|
async generate(passphrase, options) {
|
|
54
57
|
const result = await __classPrivateFieldGet(this, _AgentClient_apiClient, "f").call('agent.generate', { passphrase }, options);
|
|
55
58
|
return new AgentStatus(result);
|
package/lib/src/apiClient.d.ts
CHANGED
|
@@ -56,23 +56,81 @@ export declare class ApiClient {
|
|
|
56
56
|
private _wsReconnectTimer;
|
|
57
57
|
private _wsReconnectDelay;
|
|
58
58
|
private _wsPingTimer;
|
|
59
|
+
/** When the open socket was first pinged since it last delivered a message; null while none is unanswered. */
|
|
60
|
+
private _wsPingedAt;
|
|
61
|
+
/** When that ping could have reached the executor, which reads it only after what the socket sent before it. */
|
|
62
|
+
private _wsPingArrivesBy;
|
|
63
|
+
/** When everything the open socket sent so far could have crossed a SLOWEST_LINK_BYTES_PER_MS link.
|
|
64
|
+
* A message from the socket does not move it back: hearing from the executor says nothing
|
|
65
|
+
* about how much of a large upload has reached it. */
|
|
66
|
+
private _wsSentBy;
|
|
67
|
+
/** A `checkConnection` probe waiting for the open socket to say anything; null when none. */
|
|
68
|
+
private _wsProbeTimer;
|
|
59
69
|
private _watching;
|
|
60
70
|
private _watchScheduled;
|
|
61
71
|
private _watchDone;
|
|
62
72
|
private _watchSent;
|
|
63
73
|
private _watchRetries;
|
|
64
74
|
private _watchRetryTimer;
|
|
75
|
+
private _networkCallbacks;
|
|
76
|
+
/** Removes the network-change listeners; null while not listening. */
|
|
77
|
+
private _unlistenNetwork;
|
|
78
|
+
/** A network change waits for its socket to open, then tells `onNetworkChange`. */
|
|
79
|
+
private _networkChanged;
|
|
80
|
+
/** The socket dialled for the last network change. */
|
|
81
|
+
private _networkDial;
|
|
65
82
|
private _getWsUrl;
|
|
66
83
|
private _ensureWs;
|
|
67
84
|
private _onOpen;
|
|
85
|
+
/**
|
|
86
|
+
* Hear of network changes where the platform says so: `online` on the
|
|
87
|
+
* global object (browsers, Deno) redials at once, and `change` on
|
|
88
|
+
* `navigator.connection` (Chromium) runs `checkConnection`. Elsewhere,
|
|
89
|
+
* e.g. Node, nothing is heard and the ping deadline notices a dead
|
|
90
|
+
* socket on its own.
|
|
91
|
+
*/
|
|
92
|
+
private _listenForNetworkChanges;
|
|
93
|
+
/**
|
|
94
|
+
* The network changed: the open socket most likely runs over a path that
|
|
95
|
+
* is gone, and nothing would say so before the ping deadline. Drop it as
|
|
96
|
+
* a silent socket is dropped and dial the next one now, without backoff.
|
|
97
|
+
* `onNetworkChange` callbacks fire once that socket opens.
|
|
98
|
+
*/
|
|
99
|
+
private _onNetworkChange;
|
|
100
|
+
private _notifyNetworkChange;
|
|
68
101
|
/** `reason` is set when the executor asked to try again later; calls that fail carry it. */
|
|
69
102
|
private _onClose;
|
|
70
103
|
/** The executor refused the token: fail every call with 401, stop
|
|
71
104
|
* reconnecting and tell the `onAuthFailed` callbacks, once. */
|
|
72
105
|
private _failAuth;
|
|
73
106
|
private _startPing;
|
|
107
|
+
/** Send on `ws`, counting how long what the open socket sent could take to cross the slowest link. */
|
|
108
|
+
private _send;
|
|
109
|
+
/** The socket answered nothing for a whole ping interval: treat it as
|
|
110
|
+
* closed now. Its close event can take minutes on a dead connection,
|
|
111
|
+
* since the close frame gets no answer either; `_onClose` ignores it then. */
|
|
112
|
+
private _dropSilentSocket;
|
|
74
113
|
private _stopPing;
|
|
114
|
+
private _endProbe;
|
|
75
115
|
private _scheduleReconnect;
|
|
116
|
+
/** End the wait for the next dial, if one is pending, without dialling. */
|
|
117
|
+
private _cancelReconnect;
|
|
118
|
+
/**
|
|
119
|
+
* Dial now if the client is waiting out its reconnect backoff (up to 30 s)
|
|
120
|
+
* instead of letting the wait run out. For the moments the host knows the
|
|
121
|
+
* executor may be reachable again: an executor it restarted is up, the
|
|
122
|
+
* device woke. In a browser the client does this itself on `online` and when
|
|
123
|
+
* the page becomes visible. Does nothing while a socket is open or
|
|
124
|
+
* connecting, after `closeAll()` with nothing to reconnect, or once the
|
|
125
|
+
* executor refused the token.
|
|
126
|
+
*/
|
|
127
|
+
reconnectNow(): void;
|
|
128
|
+
private _wakeListening;
|
|
129
|
+
/** `online` always; `visibilitychange` only when the page became visible. */
|
|
130
|
+
private readonly _onWake;
|
|
131
|
+
/** Listen for `online` and `visibilitychange` exactly while a reconnect
|
|
132
|
+
* timer is pending. Outside a browser (no `window`) it registers nothing. */
|
|
133
|
+
private _listenForWake;
|
|
76
134
|
/**
|
|
77
135
|
* Call an executor method over the WebSocket, connecting first if needed.
|
|
78
136
|
* Params and result are typed by the executor's method table
|
|
@@ -110,6 +168,21 @@ export declare class ApiClient {
|
|
|
110
168
|
* Does NOT fire on the initial connection — only on reconnects.
|
|
111
169
|
* Returns an unsubscribe function. */
|
|
112
170
|
onReconnect(callback: () => void): () => void;
|
|
171
|
+
/** Register a callback that fires after the network changed (`online`,
|
|
172
|
+
* or a socket that failed `checkConnection`), once the socket dialled for the
|
|
173
|
+
* new network is open and its event watch sent. Calls made from it go
|
|
174
|
+
* out on that socket. Returns an unsubscribe function. */
|
|
175
|
+
onNetworkChange(callback: () => void): () => void;
|
|
176
|
+
/**
|
|
177
|
+
* Check the open socket now rather than at the next ping: send a ping,
|
|
178
|
+
* and if nothing at all arrives on that socket within `deadlineMs`,
|
|
179
|
+
* treat it as a network change (drop it, dial the next one at once,
|
|
180
|
+
* `onNetworkChange` callbacks fire when it opens). Any message keeps it.
|
|
181
|
+
* For a caller that has seen its network path break, e.g. ICE going
|
|
182
|
+
* disconnected, where the platform fired no `online` event. Without an
|
|
183
|
+
* open socket, or while a check runs, this does nothing.
|
|
184
|
+
*/
|
|
185
|
+
checkConnection(deadlineMs?: number): void;
|
|
113
186
|
/** Register a callback that fires once when the executor refuses this
|
|
114
187
|
* client's token: a 401 reply, or a socket closed with code 1008. Every
|
|
115
188
|
* call then fails with a 401 `RpcError` and the client does not reconnect;
|
package/lib/src/apiClient.js
CHANGED
|
@@ -18,6 +18,40 @@ const MAX_RECONNECT_DELAY_MS = 30000;
|
|
|
18
18
|
* MAX_WATCH_RETRIES times in a row. */
|
|
19
19
|
const WATCH_RETRY_DELAY_MS = 1000;
|
|
20
20
|
const MAX_WATCH_RETRIES = 5;
|
|
21
|
+
/** An open socket sends a ping this often, and must deliver some message
|
|
22
|
+
* (the pong will do) within an interval of the first ping it left
|
|
23
|
+
* unanswered. After a network switch a socket can stay OPEN for minutes with
|
|
24
|
+
* no close event, since no FIN or RST ever arrives; this notices it within
|
|
25
|
+
* two intervals. */
|
|
26
|
+
const PING_INTERVAL_MS = 10000;
|
|
27
|
+
/** The slowest link a socket is given time for. The executor reads a ping
|
|
28
|
+
* only after the frames sent before it, so a ping left unanswered counts as
|
|
29
|
+
* silence only once everything the socket sent before that ping could have
|
|
30
|
+
* crossed this link and PONG_RETURN_MS more have passed, if that takes longer
|
|
31
|
+
* than PING_INTERVAL_MS. Messages that come in meanwhile do not shorten this:
|
|
32
|
+
* they say nothing about how far an upload has got. */
|
|
33
|
+
const SLOWEST_LINK_BYTES_PER_MS = 16; // 16 kB/s
|
|
34
|
+
/** How long a pong gets to come back after its ping could have been read
|
|
35
|
+
* behind a large write. Shorter than PING_INTERVAL_MS, so the few ms a small
|
|
36
|
+
* frame adds before a ping on an idle socket never cost a ping interval. */
|
|
37
|
+
const PONG_RETURN_MS = 5000;
|
|
38
|
+
/** How long `checkConnection` waits for the open socket to say anything
|
|
39
|
+
* after its ping. If a large write sent before it may still be on the way,
|
|
40
|
+
* the wait lasts until that write could have crossed, as for the periodic
|
|
41
|
+
* ping, and half of this more for the pong. On a working path the pong is
|
|
42
|
+
* one round trip away. */
|
|
43
|
+
const CHECK_CONNECTION_DEADLINE_MS = 2000;
|
|
44
|
+
/** The bytes `text` takes as UTF-8, as a WebSocket text frame sends it. */
|
|
45
|
+
function utf8Length(text) {
|
|
46
|
+
let bytes = text.length;
|
|
47
|
+
for (let i = 0; i < text.length; i++) {
|
|
48
|
+
const unit = text.charCodeAt(i);
|
|
49
|
+
// A surrogate is half of a 4-byte pair: 2 units, 2 extra bytes.
|
|
50
|
+
if (unit >= 0x80)
|
|
51
|
+
bytes += unit < 0x800 || (unit >= 0xd800 && unit <= 0xdfff) ? 1 : 2;
|
|
52
|
+
}
|
|
53
|
+
return bytes;
|
|
54
|
+
}
|
|
21
55
|
let _idCounter = 0;
|
|
22
56
|
function nextId() {
|
|
23
57
|
return String(++_idCounter);
|
|
@@ -52,6 +86,16 @@ export class ApiClient {
|
|
|
52
86
|
this._wsReconnectTimer = null;
|
|
53
87
|
this._wsReconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
54
88
|
this._wsPingTimer = null;
|
|
89
|
+
/** When the open socket was first pinged since it last delivered a message; null while none is unanswered. */
|
|
90
|
+
this._wsPingedAt = null;
|
|
91
|
+
/** When that ping could have reached the executor, which reads it only after what the socket sent before it. */
|
|
92
|
+
this._wsPingArrivesBy = 0;
|
|
93
|
+
/** When everything the open socket sent so far could have crossed a SLOWEST_LINK_BYTES_PER_MS link.
|
|
94
|
+
* A message from the socket does not move it back: hearing from the executor says nothing
|
|
95
|
+
* about how much of a large upload has reached it. */
|
|
96
|
+
this._wsSentBy = 0;
|
|
97
|
+
/** A `checkConnection` probe waiting for the open socket to say anything; null when none. */
|
|
98
|
+
this._wsProbeTimer = null;
|
|
55
99
|
// The executor sends a socket only the events it asked for (`events.watch`).
|
|
56
100
|
// What the executor has for the open socket, as sent; a new socket has `{}`.
|
|
57
101
|
this._watching = '{}';
|
|
@@ -61,6 +105,20 @@ export class ApiClient {
|
|
|
61
105
|
this._watchSent = Promise.resolve();
|
|
62
106
|
this._watchRetries = 0;
|
|
63
107
|
this._watchRetryTimer = null;
|
|
108
|
+
this._networkCallbacks = new Set();
|
|
109
|
+
/** Removes the network-change listeners; null while not listening. */
|
|
110
|
+
this._unlistenNetwork = null;
|
|
111
|
+
/** A network change waits for its socket to open, then tells `onNetworkChange`. */
|
|
112
|
+
this._networkChanged = false;
|
|
113
|
+
/** The socket dialled for the last network change. */
|
|
114
|
+
this._networkDial = null;
|
|
115
|
+
this._wakeListening = false;
|
|
116
|
+
/** `online` always; `visibilitychange` only when the page became visible. */
|
|
117
|
+
this._onWake = (event) => {
|
|
118
|
+
if (event.type === 'visibilitychange' && typeof document !== 'undefined' && document.visibilityState !== 'visible')
|
|
119
|
+
return;
|
|
120
|
+
this.reconnectNow();
|
|
121
|
+
};
|
|
64
122
|
this.baseUrl = baseUrl;
|
|
65
123
|
this.token = token;
|
|
66
124
|
this._webSocketImpl = webSocketImpl;
|
|
@@ -84,6 +142,10 @@ export class ApiClient {
|
|
|
84
142
|
return;
|
|
85
143
|
if (this._authFailed !== null)
|
|
86
144
|
throw new RpcError(401, this._authFailed);
|
|
145
|
+
this._listenForNetworkChanges();
|
|
146
|
+
// A dial ends any wait for one: a call or subscriber that dials during the backoff
|
|
147
|
+
// leaves no timer behind to swallow the retry after this socket's own drop.
|
|
148
|
+
this._cancelReconnect();
|
|
87
149
|
const WsImpl = this._webSocketImpl ?? globalThis.WebSocket;
|
|
88
150
|
const ws = new WsImpl(this._getWsUrl());
|
|
89
151
|
this._ws = ws;
|
|
@@ -108,6 +170,10 @@ export class ApiClient {
|
|
|
108
170
|
// Only watchApplied() awaits this; a close before open is not an error otherwise.
|
|
109
171
|
this._wsOpen.catch(() => { });
|
|
110
172
|
ws.onmessage = (event) => {
|
|
173
|
+
if (this._ws === ws) {
|
|
174
|
+
this._wsPingedAt = null;
|
|
175
|
+
this._endProbe();
|
|
176
|
+
}
|
|
111
177
|
let parsed;
|
|
112
178
|
try {
|
|
113
179
|
parsed = JSON.parse(event.data);
|
|
@@ -133,7 +199,8 @@ export class ApiClient {
|
|
|
133
199
|
this._failAuth(rpcError.body);
|
|
134
200
|
return;
|
|
135
201
|
}
|
|
136
|
-
|
|
202
|
+
// A dropped socket's events are not this client's any more: its successor watches again.
|
|
203
|
+
if (parsed.type === 'pong' || this._ws !== ws)
|
|
137
204
|
return;
|
|
138
205
|
const perspective = parsed.perspectiveUuid;
|
|
139
206
|
const regs = this._handlers.get(parsed.type);
|
|
@@ -156,14 +223,14 @@ export class ApiClient {
|
|
|
156
223
|
if (this._ws !== ws)
|
|
157
224
|
return;
|
|
158
225
|
this._wsReconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
159
|
-
this._startPing();
|
|
226
|
+
this._startPing(ws);
|
|
160
227
|
// The watch goes out before the queued calls, so the events they cause reach listeners.
|
|
161
228
|
this._watching = '{}';
|
|
162
229
|
this._watchRetries = 0;
|
|
163
230
|
this._flushWatch();
|
|
164
231
|
for (const pending of this._pendingCalls.values()) {
|
|
165
232
|
if (!pending.sent) {
|
|
166
|
-
|
|
233
|
+
this._send(ws, pending.message);
|
|
167
234
|
pending.sent = true;
|
|
168
235
|
}
|
|
169
236
|
}
|
|
@@ -178,6 +245,70 @@ export class ApiClient {
|
|
|
178
245
|
}
|
|
179
246
|
}
|
|
180
247
|
this._hasConnectedOnce = true;
|
|
248
|
+
if (this._networkChanged) {
|
|
249
|
+
this._networkChanged = false;
|
|
250
|
+
this._notifyNetworkChange();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Hear of network changes where the platform says so: `online` on the
|
|
255
|
+
* global object (browsers, Deno) redials at once, and `change` on
|
|
256
|
+
* `navigator.connection` (Chromium) runs `checkConnection`. Elsewhere,
|
|
257
|
+
* e.g. Node, nothing is heard and the ping deadline notices a dead
|
|
258
|
+
* socket on its own.
|
|
259
|
+
*/
|
|
260
|
+
_listenForNetworkChanges() {
|
|
261
|
+
if (this._unlistenNetwork)
|
|
262
|
+
return;
|
|
263
|
+
const handler = () => this._onNetworkChange();
|
|
264
|
+
const removers = [];
|
|
265
|
+
const target = globalThis;
|
|
266
|
+
if (typeof target.addEventListener === 'function') {
|
|
267
|
+
target.addEventListener('online', handler);
|
|
268
|
+
removers.push(() => target.removeEventListener?.('online', handler));
|
|
269
|
+
}
|
|
270
|
+
const connection = globalThis.navigator?.connection;
|
|
271
|
+
if (typeof connection?.addEventListener === 'function') {
|
|
272
|
+
// `change` also fires when Chromium's downlink or rtt estimate
|
|
273
|
+
// moves, as a busy call makes it do: probe rather than drop.
|
|
274
|
+
const probe = () => this.checkConnection();
|
|
275
|
+
connection.addEventListener('change', probe);
|
|
276
|
+
removers.push(() => connection.removeEventListener?.('change', probe));
|
|
277
|
+
}
|
|
278
|
+
this._unlistenNetwork = () => { for (const remove of removers)
|
|
279
|
+
remove(); };
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* The network changed: the open socket most likely runs over a path that
|
|
283
|
+
* is gone, and nothing would say so before the ping deadline. Drop it as
|
|
284
|
+
* a silent socket is dropped and dial the next one now, without backoff.
|
|
285
|
+
* `onNetworkChange` callbacks fire once that socket opens.
|
|
286
|
+
*/
|
|
287
|
+
_onNetworkChange() {
|
|
288
|
+
if (this._authFailed !== null)
|
|
289
|
+
return;
|
|
290
|
+
const ws = this._ws;
|
|
291
|
+
// A socket dialled for an earlier change is on the new network already.
|
|
292
|
+
if (ws && ws === this._networkDial && ws.readyState === 0 /* CONNECTING */)
|
|
293
|
+
return;
|
|
294
|
+
if (ws) {
|
|
295
|
+
console.info('Network changed; reconnecting the WebSocket');
|
|
296
|
+
this._onClose(ws);
|
|
297
|
+
ws.close();
|
|
298
|
+
}
|
|
299
|
+
this._cancelReconnect();
|
|
300
|
+
this._wsReconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
301
|
+
if (this._handlers.size === 0 && this._pendingCalls.size === 0) {
|
|
302
|
+
this._notifyNetworkChange();
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
this._networkChanged = true;
|
|
306
|
+
this._ensureWs();
|
|
307
|
+
this._networkDial = this._ws;
|
|
308
|
+
}
|
|
309
|
+
_notifyNetworkChange() {
|
|
310
|
+
for (const cb of [...this._networkCallbacks])
|
|
311
|
+
callSafely(cb, 'Error in network-change callback:');
|
|
181
312
|
}
|
|
182
313
|
/** `reason` is set when the executor asked to try again later; calls that fail carry it. */
|
|
183
314
|
_onClose(ws, reason) {
|
|
@@ -214,19 +345,56 @@ export class ApiClient {
|
|
|
214
345
|
for (const cb of [...this._authFailedCallbacks])
|
|
215
346
|
callSafely(cb, 'Error in auth-failed callback:', reason);
|
|
216
347
|
}
|
|
217
|
-
_startPing() {
|
|
348
|
+
_startPing(ws) {
|
|
218
349
|
this._stopPing();
|
|
350
|
+
this._wsPingedAt = null;
|
|
351
|
+
this._wsSentBy = 0;
|
|
219
352
|
this._wsPingTimer = setInterval(() => {
|
|
220
|
-
if (this._ws
|
|
221
|
-
|
|
353
|
+
if (this._ws !== ws)
|
|
354
|
+
return;
|
|
355
|
+
// A probe has its own, shorter deadline and decides for this ping.
|
|
356
|
+
if (this._wsProbeTimer)
|
|
357
|
+
return;
|
|
358
|
+
const ping = JSON.stringify({ type: 'ping' });
|
|
359
|
+
if (this._wsPingedAt === null) {
|
|
360
|
+
this._wsPingedAt = performance.now();
|
|
361
|
+
this._send(ws, ping);
|
|
362
|
+
this._wsPingArrivesBy = this._wsSentBy;
|
|
363
|
+
return;
|
|
222
364
|
}
|
|
223
|
-
|
|
365
|
+
if (performance.now() >= Math.max(this._wsPingedAt + PING_INTERVAL_MS, this._wsPingArrivesBy + PONG_RETURN_MS)) {
|
|
366
|
+
this._dropSilentSocket(ws);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
this._send(ws, ping);
|
|
370
|
+
}, PING_INTERVAL_MS);
|
|
371
|
+
}
|
|
372
|
+
/** Send on `ws`, counting how long what the open socket sent could take to cross the slowest link. */
|
|
373
|
+
_send(ws, text) {
|
|
374
|
+
if (this._ws === ws)
|
|
375
|
+
this._wsSentBy = Math.max(this._wsSentBy, performance.now()) + utf8Length(text) / SLOWEST_LINK_BYTES_PER_MS;
|
|
376
|
+
ws.send(text);
|
|
377
|
+
}
|
|
378
|
+
/** The socket answered nothing for a whole ping interval: treat it as
|
|
379
|
+
* closed now. Its close event can take minutes on a dead connection,
|
|
380
|
+
* since the close frame gets no answer either; `_onClose` ignores it then. */
|
|
381
|
+
_dropSilentSocket(ws) {
|
|
382
|
+
console.warn(`WebSocket silent for ${performance.now() - (this._wsPingedAt ?? performance.now())} ms after a ping; reconnecting`);
|
|
383
|
+
this._onClose(ws);
|
|
384
|
+
ws.close();
|
|
224
385
|
}
|
|
225
386
|
_stopPing() {
|
|
226
387
|
if (this._wsPingTimer) {
|
|
227
388
|
clearInterval(this._wsPingTimer);
|
|
228
389
|
this._wsPingTimer = null;
|
|
229
390
|
}
|
|
391
|
+
this._endProbe();
|
|
392
|
+
}
|
|
393
|
+
_endProbe() {
|
|
394
|
+
if (this._wsProbeTimer) {
|
|
395
|
+
clearTimeout(this._wsProbeTimer);
|
|
396
|
+
this._wsProbeTimer = null;
|
|
397
|
+
}
|
|
230
398
|
}
|
|
231
399
|
_scheduleReconnect() {
|
|
232
400
|
if (this._wsReconnectTimer || this._authFailed !== null)
|
|
@@ -235,8 +403,54 @@ export class ApiClient {
|
|
|
235
403
|
this._wsReconnectDelay = Math.min(this._wsReconnectDelay * 2, MAX_RECONNECT_DELAY_MS);
|
|
236
404
|
this._wsReconnectTimer = setTimeout(() => {
|
|
237
405
|
this._wsReconnectTimer = null;
|
|
406
|
+
this._listenForWake(false);
|
|
238
407
|
this._ensureWs();
|
|
239
408
|
}, delay);
|
|
409
|
+
this._listenForWake(true);
|
|
410
|
+
}
|
|
411
|
+
/** End the wait for the next dial, if one is pending, without dialling. */
|
|
412
|
+
_cancelReconnect() {
|
|
413
|
+
if (this._wsReconnectTimer) {
|
|
414
|
+
clearTimeout(this._wsReconnectTimer);
|
|
415
|
+
this._wsReconnectTimer = null;
|
|
416
|
+
}
|
|
417
|
+
this._listenForWake(false);
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Dial now if the client is waiting out its reconnect backoff (up to 30 s)
|
|
421
|
+
* instead of letting the wait run out. For the moments the host knows the
|
|
422
|
+
* executor may be reachable again: an executor it restarted is up, the
|
|
423
|
+
* device woke. In a browser the client does this itself on `online` and when
|
|
424
|
+
* the page becomes visible. Does nothing while a socket is open or
|
|
425
|
+
* connecting, after `closeAll()` with nothing to reconnect, or once the
|
|
426
|
+
* executor refused the token.
|
|
427
|
+
*/
|
|
428
|
+
reconnectNow() {
|
|
429
|
+
if (!this._wsReconnectTimer || this._ws || this._authFailed !== null)
|
|
430
|
+
return;
|
|
431
|
+
this._cancelReconnect();
|
|
432
|
+
// The executor is likely back, so a failure now starts the backoff over.
|
|
433
|
+
this._wsReconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
|
434
|
+
this._ensureWs();
|
|
435
|
+
}
|
|
436
|
+
/** Listen for `online` and `visibilitychange` exactly while a reconnect
|
|
437
|
+
* timer is pending. Outside a browser (no `window`) it registers nothing. */
|
|
438
|
+
_listenForWake(on) {
|
|
439
|
+
if (on === this._wakeListening)
|
|
440
|
+
return;
|
|
441
|
+
const win = typeof window !== 'undefined' ? window : undefined;
|
|
442
|
+
if (typeof win?.addEventListener !== 'function')
|
|
443
|
+
return;
|
|
444
|
+
const doc = typeof document !== 'undefined' && typeof document.addEventListener === 'function' ? document : undefined;
|
|
445
|
+
this._wakeListening = on;
|
|
446
|
+
if (on) {
|
|
447
|
+
win.addEventListener('online', this._onWake);
|
|
448
|
+
doc?.addEventListener('visibilitychange', this._onWake);
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
win.removeEventListener('online', this._onWake);
|
|
452
|
+
doc?.removeEventListener('visibilitychange', this._onWake);
|
|
453
|
+
}
|
|
240
454
|
}
|
|
241
455
|
// ── RPC call method ─────────────────────────────────────────────────────
|
|
242
456
|
/**
|
|
@@ -282,7 +496,7 @@ export class ApiClient {
|
|
|
282
496
|
}, timeoutMs);
|
|
283
497
|
const onAbort = () => {
|
|
284
498
|
if (pending.sent && this._ws?.readyState === 1 /* OPEN */) {
|
|
285
|
-
this._ws
|
|
499
|
+
this._send(this._ws, JSON.stringify({ id: nextId(), type: 'request.cancel', params: { targetId: id } }));
|
|
286
500
|
}
|
|
287
501
|
settle(() => reject(new DOMException('Aborted', 'AbortError')));
|
|
288
502
|
};
|
|
@@ -305,7 +519,7 @@ export class ApiClient {
|
|
|
305
519
|
return;
|
|
306
520
|
}
|
|
307
521
|
if (this._ws.readyState === 1 /* OPEN */) {
|
|
308
|
-
this._ws
|
|
522
|
+
this._send(this._ws, message);
|
|
309
523
|
pending.sent = true;
|
|
310
524
|
}
|
|
311
525
|
});
|
|
@@ -426,10 +640,7 @@ export class ApiClient {
|
|
|
426
640
|
clearTimeout(this._watchRetryTimer);
|
|
427
641
|
this._watchRetryTimer = null;
|
|
428
642
|
}
|
|
429
|
-
|
|
430
|
-
clearTimeout(this._wsReconnectTimer);
|
|
431
|
-
this._wsReconnectTimer = null;
|
|
432
|
-
}
|
|
643
|
+
this._cancelReconnect();
|
|
433
644
|
const ws = this._ws;
|
|
434
645
|
this._ws = null;
|
|
435
646
|
this._wsOpen = null;
|
|
@@ -442,6 +653,46 @@ export class ApiClient {
|
|
|
442
653
|
this._reconnectCallbacks.add(callback);
|
|
443
654
|
return () => { this._reconnectCallbacks.delete(callback); };
|
|
444
655
|
}
|
|
656
|
+
/** Register a callback that fires after the network changed (`online`,
|
|
657
|
+
* or a socket that failed `checkConnection`), once the socket dialled for the
|
|
658
|
+
* new network is open and its event watch sent. Calls made from it go
|
|
659
|
+
* out on that socket. Returns an unsubscribe function. */
|
|
660
|
+
onNetworkChange(callback) {
|
|
661
|
+
this._networkCallbacks.add(callback);
|
|
662
|
+
this._listenForNetworkChanges();
|
|
663
|
+
return () => { this._networkCallbacks.delete(callback); };
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Check the open socket now rather than at the next ping: send a ping,
|
|
667
|
+
* and if nothing at all arrives on that socket within `deadlineMs`,
|
|
668
|
+
* treat it as a network change (drop it, dial the next one at once,
|
|
669
|
+
* `onNetworkChange` callbacks fire when it opens). Any message keeps it.
|
|
670
|
+
* For a caller that has seen its network path break, e.g. ICE going
|
|
671
|
+
* disconnected, where the platform fired no `online` event. Without an
|
|
672
|
+
* open socket, or while a check runs, this does nothing.
|
|
673
|
+
*/
|
|
674
|
+
checkConnection(deadlineMs = CHECK_CONNECTION_DEADLINE_MS) {
|
|
675
|
+
const ws = this._ws;
|
|
676
|
+
if (!ws || ws.readyState !== 1 /* OPEN */ || this._wsProbeTimer)
|
|
677
|
+
return;
|
|
678
|
+
const first = this._wsPingedAt === null;
|
|
679
|
+
if (first)
|
|
680
|
+
this._wsPingedAt = performance.now();
|
|
681
|
+
this._send(ws, JSON.stringify({ type: 'ping' }));
|
|
682
|
+
if (first)
|
|
683
|
+
this._wsPingArrivesBy = this._wsSentBy;
|
|
684
|
+
// Any message from the socket ends the probe (`onmessage` → `_endProbe`).
|
|
685
|
+
// The ping reaches the executor only after what was sent before it,
|
|
686
|
+
// and its pong gets half the deadline to come back after that.
|
|
687
|
+
const wait = Math.max(deadlineMs, this._wsSentBy - performance.now() + deadlineMs / 2);
|
|
688
|
+
this._wsProbeTimer = setTimeout(() => {
|
|
689
|
+
this._wsProbeTimer = null;
|
|
690
|
+
if (this._ws !== ws)
|
|
691
|
+
return;
|
|
692
|
+
console.warn(`WebSocket silent for ${wait} ms after a check; reconnecting`);
|
|
693
|
+
this._onNetworkChange();
|
|
694
|
+
}, wait);
|
|
695
|
+
}
|
|
445
696
|
/** Register a callback that fires once when the executor refuses this
|
|
446
697
|
* client's token: a 401 reply, or a socket closed with code 1008. Every
|
|
447
698
|
* call then fails with a 401 `RpcError` and the client does not reconnect;
|
|
@@ -459,6 +710,11 @@ export class ApiClient {
|
|
|
459
710
|
this._handlers.clear();
|
|
460
711
|
this._reconnectCallbacks.clear();
|
|
461
712
|
this._authFailedCallbacks.clear();
|
|
713
|
+
this._networkCallbacks.clear();
|
|
714
|
+
this._unlistenNetwork?.();
|
|
715
|
+
this._unlistenNetwork = null;
|
|
716
|
+
this._networkChanged = false;
|
|
717
|
+
this._networkDial = null;
|
|
462
718
|
// A reused client must not fire onReconnect on its next first open.
|
|
463
719
|
this._hasConnectedOnce = false;
|
|
464
720
|
}
|