@mentra/miniapp 3.4.0-dev.1017 → 3.4.0-dev.1023
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/background/register.d.ts +3 -1
- package/dist/background/register.d.ts.map +1 -1
- package/dist/background/register.js +19 -2
- package/dist/background/register.js.map +1 -1
- package/dist/session.d.ts +43 -3
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +197 -30
- package/dist/session.js.map +1 -1
- package/dist/transport/auto.js +6 -0
- package/dist/transport/auto.js.map +1 -1
- package/dist/transport/dispatch.d.ts +2 -0
- package/dist/transport/dispatch.d.ts.map +1 -1
- package/dist/transport/dispatch.js +7 -2
- package/dist/transport/dispatch.js.map +1 -1
- package/dist/transport/local-socket.d.ts +6 -0
- package/dist/transport/local-socket.d.ts.map +1 -1
- package/dist/transport/local-socket.js +34 -2
- package/dist/transport/local-socket.js.map +1 -1
- package/dist/transport/types.d.ts +4 -1
- package/dist/transport/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/background/register.ts +24 -2
- package/src/session.ts +186 -35
- package/src/transport/auto.ts +5 -0
- package/src/transport/dispatch.ts +7 -2
- package/src/transport/local-socket.ts +30 -1
- package/src/transport/types.ts +4 -1
package/README.md
CHANGED
|
@@ -67,7 +67,7 @@ function App() {
|
|
|
67
67
|
|
|
68
68
|
```ts
|
|
69
69
|
const session = new MiniappSession(options?)
|
|
70
|
-
await session.connect() //
|
|
70
|
+
await session.connect() // same Promise while in flight or connected; a failed attempt can be retried
|
|
71
71
|
session.isConnected()
|
|
72
72
|
session.disconnect()
|
|
73
73
|
```
|
|
@@ -87,7 +87,7 @@ session.off("ready", handler)
|
|
|
87
87
|
|
|
88
88
|
| Event | Signature |
|
|
89
89
|
| -------------- | --------------------------------------------- |
|
|
90
|
-
| `ready` | `() => void` — fires
|
|
90
|
+
| `ready` | `() => void` — fires once, on the handshake |
|
|
91
91
|
| `disconnect` | `(reason: string) => void` |
|
|
92
92
|
| `error` | `(err: Error) => void` |
|
|
93
93
|
| `visibility` | `("foreground" \| "background") => void` |
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
* 2. Call the user-supplied handler synchronously so subscriptions
|
|
14
14
|
* are registered before any host events fan out.
|
|
15
15
|
* 3. Call `session.connect()` so the host receives CONNECT and the
|
|
16
|
-
* first CONNECT_ACK populates `userId`, `capabilities`, etc.
|
|
16
|
+
* first CONNECT_ACK populates `userId`, `capabilities`, etc. A host that
|
|
17
|
+
* has not answered within `connectTimeoutMs` gets CONNECT again; that is
|
|
18
|
+
* a stalled host, not a miniapp crash.
|
|
17
19
|
* 4. Send READY once the handler's returned promise settles. Hosts that
|
|
18
20
|
* advertise `hostFeatures.initReady` keep the UI closed until then.
|
|
19
21
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../../src/background/register.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../../src/background/register.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAC,cAAc,EAAqB,KAAK,qBAAqB,EAAC,MAAM,YAAY,CAAA;AAExF,MAAM,MAAM,mBAAmB,CAAC,SAAS,SAAS,MAAM,IAAI,cAAc,CAAC,SAAS,CAAC,CAAA;AAErF,MAAM,MAAM,kBAAkB,CAAC,SAAS,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CACnF,OAAO,EAAE,mBAAmB,CAAC,SAAS,CAAC,KACpC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;AAwBzB;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,eAAe,CAAC,SAAS,SAAS,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChF,OAAO,EAAE,kBAAkB,CAAC,SAAS,CAAC,EACtC,OAAO,GAAE,qBAA0B,GAClC,IAAI,CA2DN"}
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
* 2. Call the user-supplied handler synchronously so subscriptions
|
|
14
14
|
* are registered before any host events fan out.
|
|
15
15
|
* 3. Call `session.connect()` so the host receives CONNECT and the
|
|
16
|
-
* first CONNECT_ACK populates `userId`, `capabilities`, etc.
|
|
16
|
+
* first CONNECT_ACK populates `userId`, `capabilities`, etc. A host that
|
|
17
|
+
* has not answered within `connectTimeoutMs` gets CONNECT again; that is
|
|
18
|
+
* a stalled host, not a miniapp crash.
|
|
17
19
|
* 4. Send READY once the handler's returned promise settles. Hosts that
|
|
18
20
|
* advertise `hostFeatures.initReady` keep the UI closed until then.
|
|
19
21
|
*
|
|
@@ -21,7 +23,13 @@
|
|
|
21
23
|
* JSContext (crash recovery, dev reload), the polyfill + bundle are
|
|
22
24
|
* re-evaluated and the handler fires again with a fresh session.
|
|
23
25
|
*/
|
|
24
|
-
import { MiniappSession } from "../session";
|
|
26
|
+
import { MiniappSession, NotConnectedError } from "../session";
|
|
27
|
+
/** A later init replaces the session: disconnecting it ends its handshake, so it neither retries nor reports. */
|
|
28
|
+
function releaseBackgroundSession(g) {
|
|
29
|
+
const previous = g.__mentraBackgroundSession;
|
|
30
|
+
g.__mentraBackgroundSession = undefined;
|
|
31
|
+
previous?.disconnect();
|
|
32
|
+
}
|
|
25
33
|
/**
|
|
26
34
|
* Register the miniapp's startup handler. Call once at the top level of
|
|
27
35
|
* `src/background/index.ts`. Top-level side effects are fine, but the
|
|
@@ -41,8 +49,13 @@ import { MiniappSession } from "../session";
|
|
|
41
49
|
*/
|
|
42
50
|
export function registerMiniapp(handler, options = {}) {
|
|
43
51
|
const g = globalThis;
|
|
52
|
+
releaseBackgroundSession(g);
|
|
44
53
|
g.__mentraInitCallback = (sessionId) => {
|
|
54
|
+
releaseBackgroundSession(g);
|
|
45
55
|
const session = new MiniappSession(options);
|
|
56
|
+
g.__mentraBackgroundSession = session;
|
|
57
|
+
// Before the handler runs, so its own connect() or waitForReady() shares the retried handshake.
|
|
58
|
+
session.retryHandshakeOnTimeout();
|
|
46
59
|
// The handler's returned promise defines readiness: the host keeps the UI
|
|
47
60
|
// closed until it settles, so handlers registered after an await still
|
|
48
61
|
// exist before the first UI request arrives.
|
|
@@ -69,6 +82,10 @@ export function registerMiniapp(handler, options = {}) {
|
|
|
69
82
|
/* connect failure is reported below */
|
|
70
83
|
});
|
|
71
84
|
connected.catch((err) => {
|
|
85
|
+
// Replaced by a later init, or disconnected during the handshake: a
|
|
86
|
+
// deliberate teardown, not a crash.
|
|
87
|
+
if (g.__mentraBackgroundSession !== session || err instanceof NotConnectedError)
|
|
88
|
+
return;
|
|
72
89
|
// eslint-disable-next-line no-console
|
|
73
90
|
console.error("[mentra-miniapp] session.connect() rejected:", err);
|
|
74
91
|
// Surface to the host's crash controller as a structured uncaught
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"register.js","sourceRoot":"","sources":["../../src/background/register.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"register.js","sourceRoot":"","sources":["../../src/background/register.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAC,cAAc,EAAE,iBAAiB,EAA6B,MAAM,YAAY,CAAA;AAuBxF,iHAAiH;AACjH,SAAS,wBAAwB,CAAC,CAAc;IAC9C,MAAM,QAAQ,GAAG,CAAC,CAAC,yBAAyB,CAAA;IAC5C,CAAC,CAAC,yBAAyB,GAAG,SAAS,CAAA;IACvC,QAAQ,EAAE,UAAU,EAAE,CAAA;AACxB,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAsC,EACtC,UAAiC,EAAE;IAEnC,MAAM,CAAC,GAAG,UAAoC,CAAA;IAC9C,wBAAwB,CAAC,CAAC,CAAC,CAAA;IAC3B,CAAC,CAAC,oBAAoB,GAAG,CAAC,SA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AI,CAAC,CAAC,yBAAyB,KAAK,OAAO,IAAI,GAAG,YAAY,iBAAiB;gBAAE,OAAM;YACvF,sCAAsC;YACtC,OAAO,CAAC,KAAK,CAAC,8CAA8C,EAAE,GAAG,CAAC,CAAA;YAClE,kEAAkE;YAClE,kEAAkE;YAClE,2DAA2D;YAC3D,8DAA8D;YAC9D,qCAAqC;YACrC,IAAI,CAAC;gBACH,MAAM,OAAO,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBAChE,MAAM,KAAK,GAAG,GAAG,YAAY,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAA;gBAChE,CAAC,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,EAAC,OAAO,EAAE,6BAA6B,OAAO,EAAE,EAAE,KAAK,EAAC,CAAC,CAAC,CAAA;YAC3F,CAAC;YAAC,MAAM,CAAC;gBACP,2CAA2C;YAC7C,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC"}
|
package/dist/session.d.ts
CHANGED
|
@@ -134,6 +134,13 @@ export declare class NotConnectedError extends Error {
|
|
|
134
134
|
readonly code = MiniappErrorCode.NOT_CONNECTED;
|
|
135
135
|
constructor(message?: string);
|
|
136
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* @internal — `connect()` rejects with this when the host did not answer CONNECT
|
|
139
|
+
* within `connectTimeoutMs`. The session stays usable: `connect()` sends CONNECT again.
|
|
140
|
+
*/
|
|
141
|
+
export declare class ConnectAckTimeoutError extends Error {
|
|
142
|
+
constructor();
|
|
143
|
+
}
|
|
137
144
|
export interface MiniappRequestError {
|
|
138
145
|
code: string;
|
|
139
146
|
message: string;
|
|
@@ -240,11 +247,26 @@ export declare class MiniappSession<TChannels extends object = any> {
|
|
|
240
247
|
private authRefreshPromise;
|
|
241
248
|
/**
|
|
242
249
|
* Outbound queue for anything sent before CONNECT_ACK. Flushed in FIFO order
|
|
243
|
-
* once the phone responds with CONNECT_ACK.
|
|
250
|
+
* once the phone responds with CONNECT_ACK. A queued request carries its id so
|
|
251
|
+
* one that has already been rejected is dropped instead of sent.
|
|
244
252
|
*/
|
|
245
253
|
private readonly outboundQueue;
|
|
246
254
|
private readonly pendingRequests;
|
|
255
|
+
/** In flight or succeeded. A failed attempt clears it so `connect()` can send CONNECT again. */
|
|
247
256
|
private connectPromise;
|
|
257
|
+
/**
|
|
258
|
+
* Set by the first CONNECT_ACK and never cleared: the handshake completes once
|
|
259
|
+
* per session. The host answers every CONNECT, so after a retry the ACK for
|
|
260
|
+
* another CONNECT of this session can still arrive, in either order. Unlike
|
|
261
|
+
* `ready`, this survives a transport drop, which a session does not recover from.
|
|
262
|
+
*/
|
|
263
|
+
private handshakeComplete;
|
|
264
|
+
/** Rejects the handshake in flight. Set only while waiting for CONNECT_ACK. */
|
|
265
|
+
private failHandshake;
|
|
266
|
+
/** Set by `registerMiniapp`: a CONNECT_ACK timeout sends CONNECT again instead of failing `connect()`. */
|
|
267
|
+
private retryHandshake;
|
|
268
|
+
/** Ends the pause before CONNECT is sent again, with the failure that ended it if any. Set only during that pause. */
|
|
269
|
+
private endRetryPause;
|
|
248
270
|
private disposed;
|
|
249
271
|
/**
|
|
250
272
|
* Set by `registerMiniapp` to the host's id for this spawn: CONNECT tells the
|
|
@@ -292,10 +314,28 @@ export declare class MiniappSession<TChannels extends object = any> {
|
|
|
292
314
|
forceLocal?: boolean;
|
|
293
315
|
}): UnsubscribeFn;
|
|
294
316
|
/**
|
|
295
|
-
* Connect to LocalMiniappRuntime. Idempotent
|
|
296
|
-
*
|
|
317
|
+
* Connect to LocalMiniappRuntime. Idempotent while a handshake is in flight
|
|
318
|
+
* or has succeeded: repeat calls return the same Promise. A failed attempt
|
|
319
|
+
* (CONNECT_ACK timeout, transport failure) does not stick, so a later call
|
|
320
|
+
* sends CONNECT again. A session started by `registerMiniapp` keeps sending
|
|
321
|
+
* CONNECT after a timeout, so this Promise (and `waitForReady()`) settles
|
|
322
|
+
* only when the host answers, the transport fails, or the session ends.
|
|
297
323
|
*/
|
|
298
324
|
connect(): Promise<void>;
|
|
325
|
+
/**
|
|
326
|
+
* @internal — called by `registerMiniapp` before its handler runs. The host
|
|
327
|
+
* owns this context's lifetime, so a host that has not answered CONNECT is
|
|
328
|
+
* stalled rather than gone: send CONNECT again instead of failing.
|
|
329
|
+
*/
|
|
330
|
+
retryHandshakeOnTimeout(): void;
|
|
331
|
+
private connectUntilAcknowledged;
|
|
332
|
+
/**
|
|
333
|
+
* Resolves after CONNECT_RETRY_MS, or as soon as CONNECT_ACK lands or the
|
|
334
|
+
* session ends. Resolves with the error when the transport failed meanwhile.
|
|
335
|
+
*/
|
|
336
|
+
private pauseBeforeRetry;
|
|
337
|
+
/** Send one CONNECT and wait for its CONNECT_ACK. */
|
|
338
|
+
private sendConnect;
|
|
299
339
|
/**
|
|
300
340
|
* @internal — called by `registerMiniapp` before `connect()`. The host then
|
|
301
341
|
* keeps the UI closed until {@link reportInitReady}, so UI requests cannot
|
package/dist/session.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAKH,OAAO,EAAqB,kBAAkB,EAAC,MAAM,WAAW,CAAA;AAChE,OAAO,EAAC,gBAAgB,EAAsB,mBAAmB,EAAC,MAAM,YAAY,CAAA;AACpF,OAAO,EAAkB,sBAAsB,EAAC,MAAM,kBAAkB,CAAA;AAExE,OAAO,EAAC,YAAY,EAAC,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAC,UAAU,EAAC,MAAM,gBAAgB,CAAA;AACzC,OAAO,EAAC,WAAW,EAAC,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAC,mBA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E,OAAO,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAKhG,GAAG,CAAC,CAAC,SAAS,MAAM,oBAAoB,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,IAAI;IAI3F;;;;;;;OAOG;IACH,kBAAkB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI;IAIjE,kBAAkB,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,iBAAiB,KAAK,IAAI,GAAG,MAAM,IAAI;IAIvE,oBAAoB,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,mBAAmB,GAAG,IAAI,KAAK,IAAI,GAAG,MAAM,IAAI;IAIpF,mBAAmB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,IAAI,GAAG,MAAM,IAAI;IAQ9E,OAAO,CAAC,aAAa;IAarB,OAAO,CAAC,UAAU;IAclB,OAAO,CAAC,cAAc;IAoPtB,OAAO,CAAC,yBAAyB;IAWjC,OAAO,CAAC,cAAc;IAkBtB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,kBAAkB;IAwB1B,OAAO,CAAC,SAAS;IAWjB;;;;OAIG;IACH,OAAO,CAAC,gBAAgB;CAezB;AA6BD,kEAAkE;AAClE,wBAAgB,mBAAmB,IAAI,SAAS,cAAc,EAAE,CAE/D"}
|
package/dist/session.js
CHANGED
|
@@ -58,7 +58,19 @@ export class NotConnectedError extends Error {
|
|
|
58
58
|
this.name = "NotConnectedError";
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* @internal — `connect()` rejects with this when the host did not answer CONNECT
|
|
63
|
+
* within `connectTimeoutMs`. The session stays usable: `connect()` sends CONNECT again.
|
|
64
|
+
*/
|
|
65
|
+
export class ConnectAckTimeoutError extends Error {
|
|
66
|
+
constructor() {
|
|
67
|
+
super("MiniappSession: CONNECT_ACK timeout");
|
|
68
|
+
this.name = "ConnectAckTimeoutError";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
61
71
|
const DEFAULT_CONNECT_TIMEOUT_MS = 10000;
|
|
72
|
+
/** Pause before a `registerMiniapp` session sends CONNECT again after a CONNECT_ACK timeout. */
|
|
73
|
+
const CONNECT_RETRY_MS = 1000;
|
|
62
74
|
// Hard ceiling on how long a single bridge request waits for the host's
|
|
63
75
|
// REQUEST_RESULT. Without it, a host that never responds (a hung cloud call, a
|
|
64
76
|
// GPS fix that never arrives, a native handler that stalls) leaves the request
|
|
@@ -94,11 +106,26 @@ export class MiniappSession {
|
|
|
94
106
|
this.authRefreshPromise = null;
|
|
95
107
|
/**
|
|
96
108
|
* Outbound queue for anything sent before CONNECT_ACK. Flushed in FIFO order
|
|
97
|
-
* once the phone responds with CONNECT_ACK.
|
|
109
|
+
* once the phone responds with CONNECT_ACK. A queued request carries its id so
|
|
110
|
+
* one that has already been rejected is dropped instead of sent.
|
|
98
111
|
*/
|
|
99
112
|
this.outboundQueue = [];
|
|
100
113
|
this.pendingRequests = new Map();
|
|
114
|
+
/** In flight or succeeded. A failed attempt clears it so `connect()` can send CONNECT again. */
|
|
101
115
|
this.connectPromise = null;
|
|
116
|
+
/**
|
|
117
|
+
* Set by the first CONNECT_ACK and never cleared: the handshake completes once
|
|
118
|
+
* per session. The host answers every CONNECT, so after a retry the ACK for
|
|
119
|
+
* another CONNECT of this session can still arrive, in either order. Unlike
|
|
120
|
+
* `ready`, this survives a transport drop, which a session does not recover from.
|
|
121
|
+
*/
|
|
122
|
+
this.handshakeComplete = false;
|
|
123
|
+
/** Rejects the handshake in flight. Set only while waiting for CONNECT_ACK. */
|
|
124
|
+
this.failHandshake = null;
|
|
125
|
+
/** Set by `registerMiniapp`: a CONNECT_ACK timeout sends CONNECT again instead of failing `connect()`. */
|
|
126
|
+
this.retryHandshake = false;
|
|
127
|
+
/** Ends the pause before CONNECT is sent again, with the failure that ended it if any. Set only during that pause. */
|
|
128
|
+
this.endRetryPause = null;
|
|
102
129
|
this.disposed = false;
|
|
103
130
|
/**
|
|
104
131
|
* Set by `registerMiniapp` to the host's id for this spawn: CONNECT tells the
|
|
@@ -219,8 +246,12 @@ export class MiniappSession {
|
|
|
219
246
|
// Connection lifecycle
|
|
220
247
|
// -------------------------------------------------------------------------
|
|
221
248
|
/**
|
|
222
|
-
* Connect to LocalMiniappRuntime. Idempotent
|
|
223
|
-
*
|
|
249
|
+
* Connect to LocalMiniappRuntime. Idempotent while a handshake is in flight
|
|
250
|
+
* or has succeeded: repeat calls return the same Promise. A failed attempt
|
|
251
|
+
* (CONNECT_ACK timeout, transport failure) does not stick, so a later call
|
|
252
|
+
* sends CONNECT again. A session started by `registerMiniapp` keeps sending
|
|
253
|
+
* CONNECT after a timeout, so this Promise (and `waitForReady()`) settles
|
|
254
|
+
* only when the host answers, the transport fails, or the session ends.
|
|
224
255
|
*/
|
|
225
256
|
connect() {
|
|
226
257
|
if (this.disposed) {
|
|
@@ -228,38 +259,155 @@ export class MiniappSession {
|
|
|
228
259
|
}
|
|
229
260
|
if (this.connectPromise)
|
|
230
261
|
return this.connectPromise;
|
|
262
|
+
// A CONNECT_ACK that arrived after an earlier attempt timed out still completed the handshake.
|
|
263
|
+
if (this.handshakeComplete) {
|
|
264
|
+
this.connectPromise = Promise.resolve();
|
|
265
|
+
return this.connectPromise;
|
|
266
|
+
}
|
|
267
|
+
const attempt = this.retryHandshake ? this.connectUntilAcknowledged() : this.sendConnect();
|
|
268
|
+
this.connectPromise = attempt;
|
|
269
|
+
attempt.catch(() => {
|
|
270
|
+
// Registered before the caller's handlers, so a retry from a rejection handler sends CONNECT again.
|
|
271
|
+
if (this.connectPromise === attempt)
|
|
272
|
+
this.connectPromise = null;
|
|
273
|
+
});
|
|
274
|
+
return attempt;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* @internal — called by `registerMiniapp` before its handler runs. The host
|
|
278
|
+
* owns this context's lifetime, so a host that has not answered CONNECT is
|
|
279
|
+
* stalled rather than gone: send CONNECT again instead of failing.
|
|
280
|
+
*/
|
|
281
|
+
retryHandshakeOnTimeout() {
|
|
282
|
+
this.retryHandshake = true;
|
|
283
|
+
}
|
|
284
|
+
async connectUntilAcknowledged() {
|
|
285
|
+
for (;;) {
|
|
286
|
+
try {
|
|
287
|
+
await this.sendConnect();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
catch (err) {
|
|
291
|
+
if (this.disposed || !(err instanceof ConnectAckTimeoutError))
|
|
292
|
+
throw err;
|
|
293
|
+
}
|
|
294
|
+
// eslint-disable-next-line no-console
|
|
295
|
+
console.warn("[mentra-miniapp] CONNECT_ACK timed out; sending CONNECT again");
|
|
296
|
+
const failure = await this.pauseBeforeRetry();
|
|
297
|
+
if (failure)
|
|
298
|
+
throw failure;
|
|
299
|
+
// The CONNECT_ACK for an earlier CONNECT can still land during the pause.
|
|
300
|
+
if (this.handshakeComplete)
|
|
301
|
+
return;
|
|
302
|
+
if (this.disposed)
|
|
303
|
+
throw new NotConnectedError("MiniappSession: connect aborted");
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Resolves after CONNECT_RETRY_MS, or as soon as CONNECT_ACK lands or the
|
|
308
|
+
* session ends. Resolves with the error when the transport failed meanwhile.
|
|
309
|
+
*/
|
|
310
|
+
pauseBeforeRetry() {
|
|
311
|
+
return new Promise((resolve) => {
|
|
312
|
+
const end = (failure) => {
|
|
313
|
+
clearTimeout(timer);
|
|
314
|
+
this.emitter.off("ready", onReady);
|
|
315
|
+
if (this.endRetryPause === end)
|
|
316
|
+
this.endRetryPause = null;
|
|
317
|
+
resolve(failure);
|
|
318
|
+
};
|
|
319
|
+
const onReady = () => end();
|
|
320
|
+
const timer = setTimeout(end, CONNECT_RETRY_MS);
|
|
321
|
+
this.emitter.on("ready", onReady);
|
|
322
|
+
this.endRetryPause = end;
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
/** Send one CONNECT and wait for its CONNECT_ACK. */
|
|
326
|
+
sendConnect() {
|
|
327
|
+
// Only a host that received CONNECT can be waited out. Before that, the transport is what failed.
|
|
328
|
+
let connectSent = false;
|
|
329
|
+
// This attempt's own outcome. A slow open() can finish after a newer attempt
|
|
330
|
+
// took over, so nothing below acts through the session-wide failHandshake.
|
|
331
|
+
let attemptSettled = false;
|
|
332
|
+
let failAttempt = () => { };
|
|
231
333
|
// Register the readiness listener BEFORE any awaits so that a synchronous
|
|
232
334
|
// CONNECT_ACK delivery in tests (or the phone) can't race the subscription.
|
|
233
335
|
const readyPromise = new Promise((resolve, reject) => {
|
|
336
|
+
const settle = (error) => {
|
|
337
|
+
if (attemptSettled)
|
|
338
|
+
return;
|
|
339
|
+
attemptSettled = true;
|
|
340
|
+
clearTimeout(timer);
|
|
341
|
+
this.emitter.off("ready", onReady);
|
|
342
|
+
this.emitter.off("error", onError);
|
|
343
|
+
if (this.failHandshake === onError)
|
|
344
|
+
this.failHandshake = null;
|
|
345
|
+
if (error)
|
|
346
|
+
reject(error);
|
|
347
|
+
else
|
|
348
|
+
resolve();
|
|
349
|
+
};
|
|
350
|
+
const onReady = () => settle();
|
|
351
|
+
const onError = (error) => settle(error);
|
|
234
352
|
const timer = setTimeout(() => {
|
|
235
|
-
const err =
|
|
353
|
+
const err = connectSent
|
|
354
|
+
? new ConnectAckTimeoutError()
|
|
355
|
+
: new Error("MiniappSession: transport did not open before the CONNECT_ACK timeout");
|
|
356
|
+
// Settled before the public emit, so a throwing "error" listener cannot leave it pending.
|
|
357
|
+
settle(err);
|
|
358
|
+
// Requests queued for a retried handshake wait for it, bounded by their own timeouts.
|
|
359
|
+
if (connectSent && this.retryHandshake)
|
|
360
|
+
return;
|
|
236
361
|
this.failAllPending({ code: MiniappErrorCode.NOT_CONNECTED, message: err.message });
|
|
237
362
|
this.emitter.emit("error", err);
|
|
238
|
-
reject(err);
|
|
239
363
|
}, this.connectTimeoutMs);
|
|
240
|
-
this.emitter.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
this.emitter.once("error", (err) => {
|
|
245
|
-
clearTimeout(timer);
|
|
246
|
-
reject(err);
|
|
247
|
-
});
|
|
364
|
+
this.emitter.on("ready", onReady);
|
|
365
|
+
this.emitter.on("error", onError);
|
|
366
|
+
this.failHandshake = onError;
|
|
367
|
+
failAttempt = onError;
|
|
248
368
|
});
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
369
|
+
// Settles once CONNECT is sent, or rejects if opening or sending fails.
|
|
370
|
+
const sent = (async () => {
|
|
371
|
+
try {
|
|
372
|
+
this.transport.onMessage((raw) => this.handleIncoming(raw));
|
|
373
|
+
this.transport.onDisconnect((reason) => this.handleTransportDisconnect(reason));
|
|
374
|
+
// An open transport stays as it is: a retried handshake only sends CONNECT again.
|
|
375
|
+
const opening = this.transport.isOpen() ? Promise.resolve() : this.transport.open();
|
|
376
|
+
// An open that finishes after disconnect() must not leave the transport open.
|
|
377
|
+
opening.then(() => {
|
|
378
|
+
if (this.disposed)
|
|
379
|
+
this.transport.close();
|
|
380
|
+
}, () => { });
|
|
381
|
+
await opening;
|
|
382
|
+
// Settled while opening: failed (the timeout, disconnect(), a transport drop),
|
|
383
|
+
// or completed by an earlier CONNECT's ACK. Either way, send nothing.
|
|
384
|
+
if (attemptSettled)
|
|
385
|
+
return;
|
|
386
|
+
if (this.disposed)
|
|
387
|
+
throw new NotConnectedError("MiniappSession: connect aborted");
|
|
388
|
+
const requestId = makeRequestId();
|
|
389
|
+
const connectPayload = {
|
|
390
|
+
type: MiniappRequestType.CONNECT,
|
|
391
|
+
packageName: this.packageName,
|
|
392
|
+
...(this.initSessionId !== null ? { initReady: true, sessionId: this.initSessionId } : {}),
|
|
393
|
+
};
|
|
394
|
+
connectSent = true;
|
|
395
|
+
this.transport.send(serializeEnvelope({ payload: connectPayload, requestId }));
|
|
396
|
+
}
|
|
397
|
+
catch (err) {
|
|
398
|
+
// Already reported through readyPromise; a newer attempt may own the session now.
|
|
399
|
+
if (attemptSettled)
|
|
400
|
+
return;
|
|
401
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
402
|
+
if (!this.disposed)
|
|
403
|
+
this.failAllPending({ code: MiniappErrorCode.NOT_CONNECTED, message: error.message });
|
|
404
|
+
failAttempt(error);
|
|
405
|
+
throw error;
|
|
406
|
+
}
|
|
261
407
|
})();
|
|
262
|
-
|
|
408
|
+
// The handshake can fail while the transport is still opening (disconnect(),
|
|
409
|
+
// the timeout): the attempt settles then, without waiting for open().
|
|
410
|
+
return Promise.all([sent, readyPromise]).then(() => undefined);
|
|
263
411
|
}
|
|
264
412
|
/**
|
|
265
413
|
* @internal — called by `registerMiniapp` before `connect()`. The host then
|
|
@@ -288,6 +436,10 @@ export class MiniappSession {
|
|
|
288
436
|
if (this.disposed)
|
|
289
437
|
return;
|
|
290
438
|
this.disposed = true;
|
|
439
|
+
// End a handshake still waiting for CONNECT_ACK. Its timer would otherwise
|
|
440
|
+
// report a timeout for a session that no longer exists.
|
|
441
|
+
this.failHandshake?.(new NotConnectedError("MiniappSession: connect aborted"));
|
|
442
|
+
this.endRetryPause?.();
|
|
291
443
|
// Give listeners one synchronous chance to flush final messages
|
|
292
444
|
// (e.g. display.render([])) before we tear down the transport.
|
|
293
445
|
try {
|
|
@@ -355,7 +507,7 @@ export class MiniappSession {
|
|
|
355
507
|
reject,
|
|
356
508
|
timer,
|
|
357
509
|
});
|
|
358
|
-
this.enqueueOrSend(serializeEnvelope(envelope));
|
|
510
|
+
this.enqueueOrSend(serializeEnvelope(envelope), requestId);
|
|
359
511
|
});
|
|
360
512
|
}
|
|
361
513
|
// -------------------------------------------------------------------------
|
|
@@ -391,7 +543,7 @@ export class MiniappSession {
|
|
|
391
543
|
// -------------------------------------------------------------------------
|
|
392
544
|
// Internal — transport glue
|
|
393
545
|
// -------------------------------------------------------------------------
|
|
394
|
-
enqueueOrSend(raw) {
|
|
546
|
+
enqueueOrSend(raw, requestId) {
|
|
395
547
|
if (this.ready) {
|
|
396
548
|
try {
|
|
397
549
|
this.transport.send(raw);
|
|
@@ -402,11 +554,15 @@ export class MiniappSession {
|
|
|
402
554
|
}
|
|
403
555
|
return;
|
|
404
556
|
}
|
|
405
|
-
this.outboundQueue.push(raw);
|
|
557
|
+
this.outboundQueue.push(requestId === undefined ? { raw } : { raw, requestId });
|
|
406
558
|
}
|
|
407
559
|
flushQueue() {
|
|
408
560
|
const queue = this.outboundQueue.splice(0);
|
|
409
|
-
for (const raw of queue) {
|
|
561
|
+
for (const { raw, requestId } of queue) {
|
|
562
|
+
// Rejected while waiting for the handshake (a CONNECT_ACK timeout or its own
|
|
563
|
+
// timeout). Its caller has seen the failure, so the host must not run it.
|
|
564
|
+
if (requestId !== undefined && !this.pendingRequests.has(requestId))
|
|
565
|
+
continue;
|
|
410
566
|
try {
|
|
411
567
|
this.transport.send(raw);
|
|
412
568
|
}
|
|
@@ -423,6 +579,11 @@ export class MiniappSession {
|
|
|
423
579
|
const type = payload?.type;
|
|
424
580
|
switch (type) {
|
|
425
581
|
case MiniappResponseType.CONNECT_ACK: {
|
|
582
|
+
// The answer to another CONNECT of this session (a retry, or one whose
|
|
583
|
+
// attempt timed out). Applying it would run the handshake again: emit
|
|
584
|
+
// "ready" twice and replace state that later updates may have changed.
|
|
585
|
+
if (this.handshakeComplete)
|
|
586
|
+
return;
|
|
426
587
|
const ack = payload;
|
|
427
588
|
this.userId = ack.userId ?? "";
|
|
428
589
|
if (ack.packageName)
|
|
@@ -445,6 +606,7 @@ export class MiniappSession {
|
|
|
445
606
|
// — `hasPermission` getters will simply return false.
|
|
446
607
|
if (ack.permissions)
|
|
447
608
|
this.applyPermissions(ack.permissions);
|
|
609
|
+
this.handshakeComplete = true;
|
|
448
610
|
this.ready = true;
|
|
449
611
|
this.flushQueue();
|
|
450
612
|
this.emitter.emit("ready");
|
|
@@ -645,6 +807,11 @@ export class MiniappSession {
|
|
|
645
807
|
}
|
|
646
808
|
handleTransportDisconnect(reason) {
|
|
647
809
|
this.ready = false;
|
|
810
|
+
// A transport that drops during the handshake has failed it. Sending CONNECT
|
|
811
|
+
// again over a dead bridge would keep that failure from the host.
|
|
812
|
+
const failure = new Error(`MiniappSession: transport disconnected: ${reason}`);
|
|
813
|
+
this.failHandshake?.(failure);
|
|
814
|
+
this.endRetryPause?.(failure);
|
|
648
815
|
this.failAllPending({ code: MiniappErrorCode.NOT_CONNECTED, message: `Transport disconnected: ${reason}` });
|
|
649
816
|
this.emitter.emit("disconnect", reason);
|
|
650
817
|
}
|