@kumiai/hub-tunnel 0.6.0 → 0.7.1

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.
@@ -164,9 +164,15 @@ export type HubTunnelTransportParams = {
164
164
  *
165
165
  * **Contract (relied on by callers):**
166
166
  * - `hub.subscribe(localDID, receiveTopicID)` and `hub.receive(localDID)` are each called
167
- * **exactly once** during construction.
167
+ * **exactly once** during construction. `hub.subscribe` is invoked synchronously (before
168
+ * `hub.receive`); its completion is captured, not awaited, so construction never blocks on it.
169
+ * - The first `write` (and any write still in flight when the subscribe is still in flight) gates
170
+ * on that subscribe completing before it publishes, so a caller that constructs a transport and
171
+ * immediately writes does not lose an inbound reply that lands before the subscribe has landed.
168
172
  * - On any teardown path (signal abort, idle timeout, encrypt failure, peer-side `session-end`,
169
173
  * manual `transport.dispose()`), it publishes a best-effort `session-end` frame to `sendTopicID`
170
- * and best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`.
174
+ * and best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`, the latter ordered **after**
175
+ * the subscribe completes (never before it) so a subscribe that is still in flight at teardown
176
+ * cannot land after — and resurrect — the unsubscribe.
171
177
  */
172
178
  export declare function createHubTunnelTransport<R, W>(params: HubTunnelTransportParams): TransportType<R, W>;
package/lib/transport.js CHANGED
@@ -10,19 +10,33 @@ const DEFAULT_INBOX_CAPACITY = 1024;
10
10
  *
11
11
  * **Contract (relied on by callers):**
12
12
  * - `hub.subscribe(localDID, receiveTopicID)` and `hub.receive(localDID)` are each called
13
- * **exactly once** during construction.
13
+ * **exactly once** during construction. `hub.subscribe` is invoked synchronously (before
14
+ * `hub.receive`); its completion is captured, not awaited, so construction never blocks on it.
15
+ * - The first `write` (and any write still in flight when the subscribe is still in flight) gates
16
+ * on that subscribe completing before it publishes, so a caller that constructs a transport and
17
+ * immediately writes does not lose an inbound reply that lands before the subscribe has landed.
14
18
  * - On any teardown path (signal abort, idle timeout, encrypt failure, peer-side `session-end`,
15
19
  * manual `transport.dispose()`), it publishes a best-effort `session-end` frame to `sendTopicID`
16
- * and best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`.
20
+ * and best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`, the latter ordered **after**
21
+ * the subscribe completes (never before it) so a subscribe that is still in flight at teardown
22
+ * cannot land after — and resurrect — the unsubscribe.
17
23
  */ export function createHubTunnelTransport(params) {
18
24
  const { hub, sessionID, localDID, sendTopicID, receiveTopicID, signal, idleTimeoutMs, reconnectTimeoutMs, onEvent, onSessionEnd } = params;
19
25
  const inboxCapacity = params.inboxCapacity ?? DEFAULT_INBOX_CAPACITY;
20
26
  let lockedSessionID = typeof sessionID === 'string' ? sessionID : null;
21
27
  let outboundSeq = 0;
22
28
  let expectedSeq = 0;
23
- // Best-effort subscribe; rejection is swallowed (the receive stream still
24
- // attaches, and a missing subscription simply yields no inbound frames).
25
- void Promise.resolve(hub.subscribe(localDID, receiveTopicID)).catch(()=>{});
29
+ // Async IIFE, not Promise.resolve(hub.subscribe(...)) (a sync throw would escape construction)
30
+ // and not .then(() => hub.subscribe(...)) (would defer the call, reversing subscribe-before-
31
+ // receive). `write` gates its first send on `subscribed`; a failed subscribe yields no inbound
32
+ // frames but still lets sends proceed.
33
+ const subscribed = (async ()=>{
34
+ try {
35
+ await hub.subscribe(localDID, receiveTopicID);
36
+ } catch {
37
+ // A failed subscribe yields no inbound frames; the send gate still proceeds.
38
+ }
39
+ })();
26
40
  const subscription = hub.receive(localDID, {
27
41
  topicID: receiveTopicID
28
42
  });
@@ -62,33 +76,52 @@ const DEFAULT_INBOX_CAPACITY = 1024;
62
76
  // ignore
63
77
  });
64
78
  };
65
- const teardown = (error)=>{
79
+ // Holds every teardown effect EXCEPT the voluntary-path extras (session-end frame, controller
80
+ // error) — every teardown path (voluntary dispose, idle, abort, readable-pull error,
81
+ // `result.done`, remote session-end) routes through this, not just `teardown`, so none of them
82
+ // can bypass it and leak the reconnect timer / hub-status listener / abort listener / unsubscribe.
83
+ const releaseResources = ()=>{
66
84
  if (torndown) return;
67
85
  torndown = true;
68
86
  clearIdleTimer();
69
87
  clearReconnectTimer();
70
88
  if (unsubscribeEvents != null) {
71
- unsubscribeEvents();
89
+ try {
90
+ unsubscribeEvents();
91
+ } catch {
92
+ // ignore
93
+ }
72
94
  unsubscribeEvents = undefined;
73
95
  }
74
96
  if (abortHandler != null && signal != null) {
75
97
  signal.removeEventListener('abort', abortHandler);
76
98
  abortHandler = undefined;
77
99
  }
78
- sendSessionEnd();
100
+ // Ordered after `subscribed`: unsubscribing before an in-flight subscribe lands would have
101
+ // the subscribe's later mutation resurrect a subscription this transport no longer wants.
102
+ // `releaseResources` itself stays synchronous — only the unsubscribe is deferred.
103
+ void subscribed.then(()=>hub.unsubscribe?.(localDID, receiveTopicID)).catch(()=>{});
79
104
  try {
80
- void Promise.resolve(hub.unsubscribe?.(localDID, receiveTopicID)).catch(()=>{});
105
+ // Fire-and-forget: awaiting `return()` deadlocks on the real wire hub, where it waits behind
106
+ // the drain loop's in-flight `next()` that never settles during teardown. No-op catch keeps a
107
+ // late rejection off the unhandled-rejection path.
108
+ const rawClose = iterator.return?.();
109
+ if (rawClose != null) void Promise.resolve(rawClose).catch(()=>{});
81
110
  } catch {
82
- // ignore
111
+ // synchronous throw from `return()` — nothing more to release
83
112
  }
113
+ };
114
+ const teardown = (error)=>{
115
+ if (torndown) return;
116
+ releaseResources();
117
+ sendSessionEnd();
84
118
  if (error !== undefined && readableController != null) {
85
119
  try {
86
120
  readableController.error(error);
87
121
  } catch {
88
- // controller may already be closed
122
+ // already closed
89
123
  }
90
124
  }
91
- iterator.return?.();
92
125
  };
93
126
  const scheduleIdleTimer = ()=>{
94
127
  if (idleTimeoutMs == null || torndown) return;
@@ -139,16 +172,14 @@ const DEFAULT_INBOX_CAPACITY = 1024;
139
172
  result = await iterator.next();
140
173
  } catch (error) {
141
174
  if (!torndown) {
142
- torndown = true;
143
- clearIdleTimer();
175
+ releaseResources();
144
176
  controller.error(error);
145
177
  }
146
178
  return;
147
179
  }
148
180
  if (torndown) return;
149
181
  if (result.done) {
150
- torndown = true;
151
- clearIdleTimer();
182
+ releaseResources();
152
183
  controller.close();
153
184
  return;
154
185
  }
@@ -186,18 +217,17 @@ const DEFAULT_INBOX_CAPACITY = 1024;
186
217
  continue;
187
218
  }
188
219
  if (frame.kind === 'session-end') {
189
- torndown = true;
190
- clearIdleTimer();
220
+ // Before `releaseResources()` (which closes the iterator): a subscription whose
221
+ // close abandons outstanding claims (hub-mux's mailbox facade does) can no longer
222
+ // honour an ack afterwards.
223
+ ackHandled(message.sequenceID);
191
224
  try {
192
225
  controller.close();
193
226
  } catch {
194
227
  // already closed
195
228
  }
196
- // Before `iterator.return?.()`: a subscription whose close abandons outstanding
197
- // claims (hub-mux's mailbox facade does) can no longer honour an ack afterwards.
198
- ackHandled(message.sequenceID);
229
+ releaseResources();
199
230
  handled = false;
200
- iterator.return?.();
201
231
  onSessionEnd?.();
202
232
  return;
203
233
  }
@@ -243,6 +273,10 @@ const DEFAULT_INBOX_CAPACITY = 1024;
243
273
  if (lockedSessionID == null) {
244
274
  throw new SessionNotEstablishedError('hub-tunnel: cannot send before session is established');
245
275
  }
276
+ await subscribed;
277
+ if (torndown) {
278
+ throw new Error('Hub tunnel transport torn down');
279
+ }
246
280
  const frame = {
247
281
  v: 1,
248
282
  sessionID: lockedSessionID,
@@ -283,7 +317,7 @@ const DEFAULT_INBOX_CAPACITY = 1024;
283
317
  transport.events.on('disposed', ()=>{
284
318
  teardown();
285
319
  });
286
- if (reconnectTimeoutMs != null && hub.events != null) {
320
+ if (!torndown && reconnectTimeoutMs != null && hub.events != null) {
287
321
  const armReconnectTimer = ()=>{
288
322
  if (torndown || reconnectTimer != null) return;
289
323
  reconnectTimer = setTimeout(()=>{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/hub-tunnel",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Peer-to-peer Enkaku transport tunneled through a hub relay",
5
5
  "keywords": [
6
6
  "enkaku",
@@ -27,18 +27,18 @@
27
27
  ],
28
28
  "dependencies": {
29
29
  "@enkaku/transport": "^0.21.0",
30
+ "@kumiai/hub-protocol": "^0.7.0",
30
31
  "@sozai/async": "^0.2.1",
31
32
  "@sozai/codec": "^0.4.0",
32
33
  "@sozai/event": "^0.1.3",
33
- "@sozai/schema": "^0.1.1",
34
- "@kumiai/hub-protocol": "^0.6.0"
34
+ "@sozai/schema": "^0.1.1"
35
35
  },
36
36
  "devDependencies": {
37
- "@enkaku/client": "^0.21.0",
37
+ "@enkaku/client": "^0.21.1",
38
38
  "@enkaku/protocol": "^0.21.0",
39
- "@enkaku/server": "^0.21.0",
40
- "@kokuin/token": "^0.4.0",
41
- "@kumiai/hub-conformance": "^0.6.0"
39
+ "@enkaku/server": "^0.21.2",
40
+ "@kokuin/token": "^0.5.0",
41
+ "@kumiai/hub-conformance": "^0.7.0"
42
42
  },
43
43
  "publishConfig": {
44
44
  "access": "public"