@kumiai/hub-tunnel 0.1.0 → 0.3.0
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 +2 -2
- package/lib/index.d.ts +1 -1
- package/lib/transport.d.ts +104 -21
- package/lib/transport.js +8 -10
- package/package.json +10 -10
|
@@ -26,8 +26,8 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError }) {
|
|
|
26
26
|
payload: encodeEnvelope(envelope)
|
|
27
27
|
});
|
|
28
28
|
},
|
|
29
|
-
subscribe (subscriberDID, topicID) {
|
|
30
|
-
return hub.subscribe(subscriberDID, topicID);
|
|
29
|
+
subscribe (subscriberDID, topicID, options) {
|
|
30
|
+
return hub.subscribe(subscriberDID, topicID, options);
|
|
31
31
|
},
|
|
32
32
|
unsubscribe (subscriberDID, topicID) {
|
|
33
33
|
return hub.unsubscribe?.(subscriberDID, topicID);
|
package/lib/index.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export { decodeEnvelope, encodeEnvelope, TUNNEL_ENVELOPE_VERSION, type TunnelEnv
|
|
|
4
4
|
export { BackpressureError, DecryptError, EncryptError, EnvelopeDecodeError, FrameDecodeError, HubReconnectingError, SessionNotEstablishedError, } from './errors.js';
|
|
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
|
-
export { createHubTunnelTransport, type
|
|
7
|
+
export { createHubTunnelTransport, type HubBase, type HubFetchTopicParams, type HubFetchTopicResult, type HubPublishParams, type HubReceiveSubscription, type HubSubscribeOptions, type HubTunnelSessionID, type HubTunnelTransportParams, type LogHub, type MailboxHub, type MailboxHubEvent, type MailboxHubEventListener, type MailboxHubEvents, type MailboxPublishParams, } from './transport.js';
|
package/lib/transport.d.ts
CHANGED
|
@@ -11,36 +11,121 @@ export type HubReceiveSubscription = AsyncIterable<StoredMessage> & {
|
|
|
11
11
|
*/
|
|
12
12
|
ack?: (sequenceID: string) => void | Promise<void>;
|
|
13
13
|
};
|
|
14
|
-
export type
|
|
14
|
+
export type MailboxHubEvent = {
|
|
15
15
|
type: 'reconnecting';
|
|
16
16
|
} | {
|
|
17
17
|
type: 'connected';
|
|
18
18
|
} | {
|
|
19
19
|
type: 'disconnected';
|
|
20
20
|
};
|
|
21
|
-
export type
|
|
22
|
-
export type
|
|
23
|
-
subscribe: (listener:
|
|
21
|
+
export type MailboxHubEventListener = (event: MailboxHubEvent) => void;
|
|
22
|
+
export type MailboxHubEvents = {
|
|
23
|
+
subscribe: (listener: MailboxHubEventListener) => () => void;
|
|
24
24
|
};
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* A mailbox-class publish — the only kind a {@link MailboxHub} accepts. No retention class, no
|
|
27
|
+
* compare-and-set, no idempotency key: the lanes a MailboxHub fronts (directed tunnel, encrypting
|
|
28
|
+
* wrapper, session hub) carry app traffic never CAS'd against a log head. Keeping those fields OFF
|
|
29
|
+
* this type makes the restriction structural — a caller cannot hand a conditional publish to a
|
|
30
|
+
* mailbox lane, so a wrapper fronting one has nothing to silently drop. A lane that needs the CAS
|
|
31
|
+
* uses a {@link LogHub} instead.
|
|
32
|
+
*/
|
|
33
|
+
export type MailboxPublishParams = {
|
|
26
34
|
senderDID: string;
|
|
27
35
|
topicID: string;
|
|
28
36
|
payload: Uint8Array;
|
|
29
37
|
};
|
|
30
|
-
export type
|
|
38
|
+
export type HubPublishParams = MailboxPublishParams & {
|
|
39
|
+
/**
|
|
40
|
+
* Retention class. 'mailbox' (default): removed once every delivery is acked, or when it ages
|
|
41
|
+
* out. 'log': retained unconditionally, removed only by trim (a subscriber that must read it may
|
|
42
|
+
* not exist when it is published). Only a 'log' publish moves the topic's head.
|
|
43
|
+
*/
|
|
44
|
+
retain?: 'log' | 'mailbox';
|
|
45
|
+
/**
|
|
46
|
+
* Compare-and-set on the topic's head. Absent: append unconditionally. Present: append only if
|
|
47
|
+
* the head is exactly this value, where `null` means "the topic has never had an accepted log
|
|
48
|
+
* publish". A loser gets HeadMismatchError and nothing is stored (no entry, no delivery, no
|
|
49
|
+
* sequenceID consumed).
|
|
50
|
+
*
|
|
51
|
+
* `null` and absent are different requests: a forwarding implementation must check for the key,
|
|
52
|
+
* not for a non-null value.
|
|
53
|
+
*/
|
|
54
|
+
expectedHead?: string | null;
|
|
55
|
+
/**
|
|
56
|
+
* Idempotency key. Republishing an already-accepted `publishID` returns its original sequenceID
|
|
57
|
+
* and appends nothing — letting a peer that crashed between publishing a commit and recording
|
|
58
|
+
* the outcome ask "did my publish land?" with no responder or network peer involved.
|
|
59
|
+
*/
|
|
60
|
+
publishID?: string;
|
|
61
|
+
};
|
|
62
|
+
export type HubSubscribeOptions = {
|
|
63
|
+
/**
|
|
64
|
+
* Requested retention in seconds for this subscriber's view. Absent: the hub's default. Above
|
|
65
|
+
* the hub's maximum the subscribe is refused, never clamped — a silent downgrade strands a peer
|
|
66
|
+
* that believed it had asked for more.
|
|
67
|
+
*/
|
|
68
|
+
retention?: number;
|
|
69
|
+
};
|
|
70
|
+
export type HubFetchTopicParams = {
|
|
71
|
+
subscriberDID: string;
|
|
72
|
+
topicID: string;
|
|
73
|
+
/** Exclusive cursor: entries after this sequenceID. Absent: from the oldest retained. */
|
|
74
|
+
after?: string;
|
|
75
|
+
limit?: number;
|
|
76
|
+
};
|
|
77
|
+
export type HubFetchTopicResult = {
|
|
78
|
+
messages: Array<StoredMessage>;
|
|
79
|
+
/** The sequenceID of the last accepted log publish, or null. Survives a trim. */
|
|
80
|
+
head: string | null;
|
|
81
|
+
/** The oldest sequenceID still retained, or null if the log is empty. */
|
|
82
|
+
oldest: string | null;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* The APIs both hub kinds share — everything except how you publish. `events` are
|
|
86
|
+
* connection-lifecycle (reconnect/connect/disconnect), emitted by a mailbox and a log hub alike, so
|
|
87
|
+
* they belong here rather than on either shape. {@link MailboxHub} and {@link LogHub} each add their
|
|
88
|
+
* own `publish` on top; neither is derived from the other.
|
|
89
|
+
*/
|
|
90
|
+
export type HubBase = {
|
|
91
|
+
subscribe: (subscriberDID: string, topicID: string, options?: HubSubscribeOptions) => Promise<void> | void;
|
|
92
|
+
unsubscribe?: (subscriberDID: string, topicID: string) => Promise<void> | void;
|
|
93
|
+
receive: (subscriberDID: string) => HubReceiveSubscription;
|
|
94
|
+
events?: MailboxHubEvents;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Publish and push-delivery over topics. A subscriber sees only what is published after it
|
|
98
|
+
* subscribes — no history to read.
|
|
99
|
+
*
|
|
100
|
+
* The shape of the adapter views over a hub (directed tunnel, session hub, encrypting wrapper) and
|
|
101
|
+
* of a hub used for mailbox traffic alone. A lane that must be readable by a peer not subscribed at
|
|
102
|
+
* publish time needs a {@link LogHub} instead.
|
|
103
|
+
*/
|
|
104
|
+
export type MailboxHub = HubBase & {
|
|
105
|
+
publish: (params: MailboxPublishParams) => Promise<{
|
|
106
|
+
sequenceID: string;
|
|
107
|
+
}>;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* A hub that also retains a readable per-topic log. A pull-driven lane needs one: commits must be
|
|
111
|
+
* readable by a peer not subscribed when they were published — a member invited tomorrow has to
|
|
112
|
+
* apply commits that land today, which no push will bring it.
|
|
113
|
+
*
|
|
114
|
+
* Its `publish` widens the mailbox one to {@link HubPublishParams}: only through a LogHub can a
|
|
115
|
+
* caller drive the CAS (`expectedHead`), pin a frame to the log (`retain: 'log'`), or carry an
|
|
116
|
+
* idempotency key (`publishID`). A MailboxHub deliberately cannot.
|
|
117
|
+
*/
|
|
118
|
+
export type LogHub = HubBase & {
|
|
31
119
|
publish: (params: HubPublishParams) => Promise<{
|
|
32
120
|
sequenceID: string;
|
|
33
121
|
}>;
|
|
34
|
-
|
|
35
|
-
unsubscribe?: (subscriberDID: string, topicID: string) => Promise<void> | void;
|
|
36
|
-
receive: (subscriberDID: string) => HubReceiveSubscription;
|
|
37
|
-
events?: HubLikeEvents;
|
|
122
|
+
fetchTopic: (params: HubFetchTopicParams) => Promise<HubFetchTopicResult>;
|
|
38
123
|
};
|
|
39
124
|
export type HubTunnelSessionID = string | {
|
|
40
125
|
auto: true;
|
|
41
126
|
};
|
|
42
127
|
export type HubTunnelTransportParams = {
|
|
43
|
-
hub:
|
|
128
|
+
hub: MailboxHub;
|
|
44
129
|
sessionID: HubTunnelSessionID;
|
|
45
130
|
/**
|
|
46
131
|
* The authenticated DID used to drain the receive stream (`hub.receive`) and
|
|
@@ -65,17 +150,15 @@ export type HubTunnelTransportParams = {
|
|
|
65
150
|
onSessionEnd?: () => void;
|
|
66
151
|
};
|
|
67
152
|
/**
|
|
68
|
-
* Build a hub-tunnel transport over the pub/sub hub API. The returned
|
|
69
|
-
*
|
|
70
|
-
* subscription (filtering to that topic), and writes to the hub via
|
|
153
|
+
* Build a hub-tunnel transport over the pub/sub hub API. The returned `TransportType` subscribes
|
|
154
|
+
* to `receiveTopicID`, reads a single inbox subscription filtered to that topic, and writes via
|
|
71
155
|
* `hub.publish` on `sendTopicID`.
|
|
72
156
|
*
|
|
73
|
-
* **Contract
|
|
74
|
-
* - `hub.subscribe(localDID, receiveTopicID)` and `hub.receive(localDID)` are
|
|
75
|
-
*
|
|
76
|
-
* - On any teardown path (signal abort, idle timeout, encrypt failure,
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`.
|
|
157
|
+
* **Contract (relied on by callers):**
|
|
158
|
+
* - `hub.subscribe(localDID, receiveTopicID)` and `hub.receive(localDID)` are each called
|
|
159
|
+
* **exactly once** during construction.
|
|
160
|
+
* - On any teardown path (signal abort, idle timeout, encrypt failure, peer-side `session-end`,
|
|
161
|
+
* manual `transport.dispose()`), it publishes a best-effort `session-end` frame to `sendTopicID`
|
|
162
|
+
* and best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`.
|
|
80
163
|
*/
|
|
81
164
|
export declare function createHubTunnelTransport<R, W>(params: HubTunnelTransportParams): TransportType<R, W>;
|
package/lib/transport.js
CHANGED
|
@@ -4,18 +4,16 @@ import { BackpressureError, FrameDecodeError, HubReconnectingError, SessionNotEs
|
|
|
4
4
|
import { decodeFrame, encodeFrame } from './frame.js';
|
|
5
5
|
const DEFAULT_INBOX_CAPACITY = 1024;
|
|
6
6
|
/**
|
|
7
|
-
* Build a hub-tunnel transport over the pub/sub hub API. The returned
|
|
8
|
-
*
|
|
9
|
-
* subscription (filtering to that topic), and writes to the hub via
|
|
7
|
+
* Build a hub-tunnel transport over the pub/sub hub API. The returned `TransportType` subscribes
|
|
8
|
+
* to `receiveTopicID`, reads a single inbox subscription filtered to that topic, and writes via
|
|
10
9
|
* `hub.publish` on `sendTopicID`.
|
|
11
10
|
*
|
|
12
|
-
* **Contract
|
|
13
|
-
* - `hub.subscribe(localDID, receiveTopicID)` and `hub.receive(localDID)` are
|
|
14
|
-
*
|
|
15
|
-
* - On any teardown path (signal abort, idle timeout, encrypt failure,
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`.
|
|
11
|
+
* **Contract (relied on by callers):**
|
|
12
|
+
* - `hub.subscribe(localDID, receiveTopicID)` and `hub.receive(localDID)` are each called
|
|
13
|
+
* **exactly once** during construction.
|
|
14
|
+
* - On any teardown path (signal abort, idle timeout, encrypt failure, peer-side `session-end`,
|
|
15
|
+
* manual `transport.dispose()`), it publishes a best-effort `session-end` frame to `sendTopicID`
|
|
16
|
+
* and best-effort `hub.unsubscribe?.(localDID, receiveTopicID)`.
|
|
19
17
|
*/ export function createHubTunnelTransport(params) {
|
|
20
18
|
const { hub, sessionID, localDID, sendTopicID, receiveTopicID, signal, idleTimeoutMs, reconnectTimeoutMs, onEvent, onSessionEnd } = params;
|
|
21
19
|
const inboxCapacity = params.inboxCapacity ?? DEFAULT_INBOX_CAPACITY;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/hub-tunnel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Peer-to-peer Enkaku transport tunneled through a hub relay",
|
|
6
6
|
"keywords": [
|
|
@@ -26,17 +26,17 @@
|
|
|
26
26
|
],
|
|
27
27
|
"sideEffects": false,
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@sozai/async": "^0.1
|
|
30
|
-
"@sozai/codec": "^0.
|
|
31
|
-
"@sozai/schema": "^0.1.
|
|
32
|
-
"@enkaku/transport": "^0.
|
|
33
|
-
"@kumiai/hub-protocol": "^0.
|
|
29
|
+
"@sozai/async": "^0.2.1",
|
|
30
|
+
"@sozai/codec": "^0.2.0",
|
|
31
|
+
"@sozai/schema": "^0.1.1",
|
|
32
|
+
"@enkaku/transport": "^0.19.1",
|
|
33
|
+
"@kumiai/hub-protocol": "^0.3.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@enkaku/client": "^0.
|
|
37
|
-
"@enkaku/protocol": "^0.
|
|
38
|
-
"@enkaku/server": "^0.
|
|
39
|
-
"@kokuin/token": "^0.
|
|
36
|
+
"@enkaku/client": "^0.19.0",
|
|
37
|
+
"@enkaku/protocol": "^0.19.0",
|
|
38
|
+
"@enkaku/server": "^0.19.0",
|
|
39
|
+
"@kokuin/token": "^0.2.0"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"build:clean": "del lib",
|