@kumiai/hub-tunnel 0.12.2 → 0.13.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.
- package/lib/encrypted-transport.js +16 -6
- package/lib/index.d.ts +1 -0
- package/lib/transport.d.ts +16 -3
- package/lib/transport.js +15 -8
- package/package.json +10 -10
|
@@ -35,14 +35,24 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, onExpired,
|
|
|
35
35
|
remember(result.sequenceID);
|
|
36
36
|
return result;
|
|
37
37
|
},
|
|
38
|
-
subscribe (subscriberDID, topicID, options) {
|
|
39
|
-
return hub.subscribe(
|
|
38
|
+
subscribe ({ subscriberDID, topicID, ...options }) {
|
|
39
|
+
return hub.subscribe({
|
|
40
|
+
subscriberDID,
|
|
41
|
+
topicID,
|
|
42
|
+
...options
|
|
43
|
+
});
|
|
40
44
|
},
|
|
41
|
-
unsubscribe (subscriberDID, topicID) {
|
|
42
|
-
return hub.unsubscribe?.(
|
|
45
|
+
unsubscribe ({ subscriberDID, topicID }) {
|
|
46
|
+
return hub.unsubscribe?.({
|
|
47
|
+
subscriberDID,
|
|
48
|
+
topicID
|
|
49
|
+
});
|
|
43
50
|
},
|
|
44
|
-
receive (subscriberDID, options) {
|
|
45
|
-
const inner = hub.receive(
|
|
51
|
+
receive ({ subscriberDID, ...options }) {
|
|
52
|
+
const inner = hub.receive({
|
|
53
|
+
subscriberDID,
|
|
54
|
+
...options
|
|
55
|
+
});
|
|
46
56
|
const innerIterator = inner[Symbol.asyncIterator]();
|
|
47
57
|
const ackedOnDecrypt = new Set();
|
|
48
58
|
const inFlightDecrypts = new Set();
|
package/lib/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export { BackpressureError, DecryptError, DuplicateFrameError, EncryptError, Env
|
|
|
5
5
|
export type { FrameDroppedReason, ObservabilityEvent, ObservabilityEventListener, } from './events.js';
|
|
6
6
|
export { decodeFrame, encodeFrame, HUB_FRAME_VERSION, type HubFrame, type HubFrameMessageBody, hubFrameSchema, } from './frame.js';
|
|
7
7
|
export { precedesSequenceID } from './sequence.js';
|
|
8
|
+
export type { HubBaseReceiveParams, HubBaseSubscribeParams, HubBaseUnsubscribeParams, } from './transport.js';
|
|
8
9
|
export { createHubTunnelTransport, type HubBase, type HubFetchTopicParams, type HubFetchTopicResult, type HubPublishParams, type HubReceiveOptions, type HubReceiveSubscription, type HubSubscribeOptions, type HubTunnelSessionID, type HubTunnelTransportParams, type LogHub, type MailboxHub, type MailboxHubEvent, type MailboxHubEvents, type MailboxPublishParams, } from './transport.js';
|
package/lib/transport.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { type TransportType } from '@enkaku/transport';
|
|
|
2
2
|
import type { StoredMessage } from '@kumiai/hub-protocol';
|
|
3
3
|
import type { EventEmitter } from '@sozai/event';
|
|
4
4
|
import type { ObservabilityEventListener } from './events.js';
|
|
5
|
+
export type Now = () => number;
|
|
5
6
|
export type HubReceiveSubscription = AsyncIterable<StoredMessage> & {
|
|
6
7
|
return?: () => void;
|
|
7
8
|
/**
|
|
@@ -91,6 +92,17 @@ export type HubFetchTopicResult = {
|
|
|
91
92
|
/** Removed log frames after the exclusive cursor, from the same snapshot as messages and head. */
|
|
92
93
|
gap: boolean;
|
|
93
94
|
};
|
|
95
|
+
export type HubBaseSubscribeParams = {
|
|
96
|
+
subscriberDID: string;
|
|
97
|
+
topicID: string;
|
|
98
|
+
} & HubSubscribeOptions;
|
|
99
|
+
export type HubBaseUnsubscribeParams = {
|
|
100
|
+
subscriberDID: string;
|
|
101
|
+
topicID: string;
|
|
102
|
+
};
|
|
103
|
+
export type HubBaseReceiveParams = {
|
|
104
|
+
subscriberDID: string;
|
|
105
|
+
} & HubReceiveOptions;
|
|
94
106
|
/**
|
|
95
107
|
* The APIs both hub kinds share — everything except how you publish. `events` are
|
|
96
108
|
* connection-lifecycle (reconnect/connect/disconnect), emitted by a mailbox and a log hub alike, so
|
|
@@ -98,9 +110,9 @@ export type HubFetchTopicResult = {
|
|
|
98
110
|
* own `publish` on top; neither is derived from the other.
|
|
99
111
|
*/
|
|
100
112
|
export type HubBase = {
|
|
101
|
-
subscribe: (
|
|
102
|
-
unsubscribe?: (
|
|
103
|
-
receive: (
|
|
113
|
+
subscribe: (params: HubBaseSubscribeParams) => Promise<void> | void;
|
|
114
|
+
unsubscribe?: (params: HubBaseUnsubscribeParams) => Promise<void> | void;
|
|
115
|
+
receive: (params: HubBaseReceiveParams) => HubReceiveSubscription;
|
|
104
116
|
readonly events?: MailboxHubEvents;
|
|
105
117
|
};
|
|
106
118
|
/**
|
|
@@ -148,6 +160,7 @@ export type HubTunnelTransportParams = {
|
|
|
148
160
|
/** Topic this transport subscribes to and accepts inbound frames from. */
|
|
149
161
|
receiveTopicID: string;
|
|
150
162
|
inboxCapacity?: number;
|
|
163
|
+
monotonicNow?: Now;
|
|
151
164
|
idleTimeoutMs?: number;
|
|
152
165
|
reconnectTimeoutMs?: number;
|
|
153
166
|
signal?: AbortSignal;
|
package/lib/transport.js
CHANGED
|
@@ -21,7 +21,7 @@ const DEFAULT_INBOX_CAPACITY = 1024;
|
|
|
21
21
|
* the subscribe completes (never before it) so a subscribe that is still in flight at teardown
|
|
22
22
|
* cannot land after — and resurrect — the unsubscribe.
|
|
23
23
|
*/ export function createHubTunnelTransport(params) {
|
|
24
|
-
const { hub, sessionID, localDID, sendTopicID, receiveTopicID, signal, idleTimeoutMs, reconnectTimeoutMs, onEvent, onSessionEnd } = params;
|
|
24
|
+
const { hub, monotonicNow = ()=>performance.now(), sessionID, localDID, sendTopicID, receiveTopicID, signal, idleTimeoutMs, reconnectTimeoutMs, onEvent, onSessionEnd } = params;
|
|
25
25
|
const inboxCapacity = params.inboxCapacity ?? DEFAULT_INBOX_CAPACITY;
|
|
26
26
|
let lockedSessionID = typeof sessionID === 'string' ? sessionID : null;
|
|
27
27
|
let outboundSeq = 0;
|
|
@@ -32,19 +32,23 @@ const DEFAULT_INBOX_CAPACITY = 1024;
|
|
|
32
32
|
// frames but still lets sends proceed.
|
|
33
33
|
const subscribed = (async ()=>{
|
|
34
34
|
try {
|
|
35
|
-
await hub.subscribe(
|
|
35
|
+
await hub.subscribe({
|
|
36
|
+
subscriberDID: localDID,
|
|
37
|
+
topicID: receiveTopicID
|
|
38
|
+
});
|
|
36
39
|
} catch {
|
|
37
40
|
// A failed subscribe yields no inbound frames; the send gate still proceeds.
|
|
38
41
|
}
|
|
39
42
|
})();
|
|
40
|
-
const subscription = hub.receive(
|
|
43
|
+
const subscription = hub.receive({
|
|
44
|
+
subscriberDID: localDID,
|
|
41
45
|
topicID: receiveTopicID
|
|
42
46
|
});
|
|
43
47
|
const iterator = subscription[Symbol.asyncIterator]();
|
|
44
48
|
let abortHandler;
|
|
45
49
|
let torndown = false;
|
|
46
50
|
let readableController;
|
|
47
|
-
let lastActivity =
|
|
51
|
+
let lastActivity = monotonicNow();
|
|
48
52
|
let idleTimer;
|
|
49
53
|
let reconnectTimer;
|
|
50
54
|
let unsubscribeEvents;
|
|
@@ -100,7 +104,10 @@ const DEFAULT_INBOX_CAPACITY = 1024;
|
|
|
100
104
|
// Ordered after `subscribed`: unsubscribing before an in-flight subscribe lands would have
|
|
101
105
|
// the subscribe's later mutation resurrect a subscription this transport no longer wants.
|
|
102
106
|
// `releaseResources` itself stays synchronous — only the unsubscribe is deferred.
|
|
103
|
-
void subscribed.then(()=>hub.unsubscribe?.(
|
|
107
|
+
void subscribed.then(()=>hub.unsubscribe?.({
|
|
108
|
+
subscriberDID: localDID,
|
|
109
|
+
topicID: receiveTopicID
|
|
110
|
+
})).catch(()=>{});
|
|
104
111
|
try {
|
|
105
112
|
// Fire-and-forget: awaiting `return()` deadlocks on the real wire hub, where it waits behind
|
|
106
113
|
// the drain loop's in-flight `next()` that never settles during teardown. No-op catch keeps a
|
|
@@ -126,13 +133,13 @@ const DEFAULT_INBOX_CAPACITY = 1024;
|
|
|
126
133
|
const scheduleIdleTimer = ()=>{
|
|
127
134
|
if (idleTimeoutMs == null || torndown) return;
|
|
128
135
|
clearIdleTimer();
|
|
129
|
-
const elapsed =
|
|
136
|
+
const elapsed = monotonicNow() - lastActivity;
|
|
130
137
|
const remaining = idleTimeoutMs - elapsed;
|
|
131
138
|
const delay = Math.max(remaining, 0);
|
|
132
139
|
idleTimer = setTimeout(()=>{
|
|
133
140
|
idleTimer = undefined;
|
|
134
141
|
if (torndown) return;
|
|
135
|
-
const sinceActivity =
|
|
142
|
+
const sinceActivity = monotonicNow() - lastActivity;
|
|
136
143
|
if (sinceActivity >= idleTimeoutMs) {
|
|
137
144
|
teardown(new TimeoutInterruption({
|
|
138
145
|
message: 'idle timeout'
|
|
@@ -143,7 +150,7 @@ const DEFAULT_INBOX_CAPACITY = 1024;
|
|
|
143
150
|
}, delay);
|
|
144
151
|
};
|
|
145
152
|
const markActivity = ()=>{
|
|
146
|
-
lastActivity =
|
|
153
|
+
lastActivity = monotonicNow();
|
|
147
154
|
};
|
|
148
155
|
// A hub's ack may be synchronous and throw synchronously, before `Promise.resolve` sees it — so
|
|
149
156
|
// the rejection guard alone is not enough (same fix as `ackUpstream` in rpc/src/hub-mux.ts). An
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/hub-tunnel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"description": "Peer-to-peer Enkaku transport tunneled through a hub relay",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"enkaku",
|
|
@@ -26,19 +26,19 @@
|
|
|
26
26
|
"lib/*"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@enkaku/transport": "^0.
|
|
30
|
-
"@kumiai/hub-protocol": "^0.
|
|
31
|
-
"@sozai/async": "^0.4.
|
|
32
|
-
"@sozai/codec": "^0.4.
|
|
29
|
+
"@enkaku/transport": "^0.23.3",
|
|
30
|
+
"@kumiai/hub-protocol": "^0.13.2",
|
|
31
|
+
"@sozai/async": "^0.4.4",
|
|
32
|
+
"@sozai/codec": "^0.4.4",
|
|
33
33
|
"@sozai/event": "^0.1.6",
|
|
34
34
|
"@sozai/schema": "^0.1.6"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@enkaku/client": "^0.
|
|
38
|
-
"@enkaku/protocol": "^0.
|
|
39
|
-
"@enkaku/server": "^0.
|
|
40
|
-
"@kokuin/token": "^0.
|
|
41
|
-
"@kumiai/hub-conformance": "^0.
|
|
37
|
+
"@enkaku/client": "^0.23.7",
|
|
38
|
+
"@enkaku/protocol": "^0.23.4",
|
|
39
|
+
"@enkaku/server": "^0.23.4",
|
|
40
|
+
"@kokuin/token": "^0.7.1",
|
|
41
|
+
"@kumiai/hub-conformance": "^0.13.1"
|
|
42
42
|
},
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|