@kumiai/hub-tunnel 0.12.1 → 0.13.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 +49 -9
- package/lib/encryptor.d.ts +4 -1
- package/lib/errors.d.ts +9 -0
- package/lib/errors.js +8 -0
- package/lib/events.d.ts +7 -2
- package/lib/index.d.ts +3 -1
- package/lib/index.js +2 -1
- package/lib/sequence.d.ts +6 -0
- package/lib/sequence.js +8 -0
- package/lib/transport.d.ts +14 -3
- package/lib/transport.js +10 -3
- package/package.json +9 -9
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { fromB64, toB64 } from '@sozai/codec';
|
|
2
2
|
import { decodeEnvelope, encodeEnvelope } from './envelope.js';
|
|
3
|
-
import { DecryptError, DuplicateFrameError, EncryptError, EnvelopeDecodeError } from './errors.js';
|
|
3
|
+
import { DecryptError, DuplicateFrameError, EncryptError, EnvelopeDecodeError, ExpiredFrameError } from './errors.js';
|
|
4
|
+
import { precedesSequenceID } from './sequence.js';
|
|
4
5
|
import { createHubTunnelTransport } from './transport.js';
|
|
5
|
-
function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, drainTimeoutMs, drains }) {
|
|
6
|
+
function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, onExpired, drainTimeoutMs, drains }) {
|
|
7
|
+
// The earliest hub sequence ID this session is known by: its first publish or first delivered
|
|
8
|
+
// frame. An expired frame the hub sequenced before it belongs to an earlier session.
|
|
9
|
+
let anchor;
|
|
10
|
+
const remember = (sequenceID)=>{
|
|
11
|
+
if (anchor == null || precedesSequenceID(sequenceID, anchor)) anchor = sequenceID;
|
|
12
|
+
};
|
|
6
13
|
const wrapped = {
|
|
7
14
|
async publish (params) {
|
|
8
15
|
let ciphertextBytes;
|
|
@@ -20,20 +27,32 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, drainTimeou
|
|
|
20
27
|
groupID,
|
|
21
28
|
ciphertext: toB64(ciphertextBytes)
|
|
22
29
|
};
|
|
23
|
-
|
|
30
|
+
const result = await hub.publish({
|
|
24
31
|
senderDID: params.senderDID,
|
|
25
32
|
topicID: params.topicID,
|
|
26
33
|
payload: encodeEnvelope(envelope)
|
|
27
34
|
});
|
|
35
|
+
remember(result.sequenceID);
|
|
36
|
+
return result;
|
|
28
37
|
},
|
|
29
|
-
subscribe (subscriberDID, topicID, options) {
|
|
30
|
-
return hub.subscribe(
|
|
38
|
+
subscribe ({ subscriberDID, topicID, ...options }) {
|
|
39
|
+
return hub.subscribe({
|
|
40
|
+
subscriberDID,
|
|
41
|
+
topicID,
|
|
42
|
+
...options
|
|
43
|
+
});
|
|
31
44
|
},
|
|
32
|
-
unsubscribe (subscriberDID, topicID) {
|
|
33
|
-
return hub.unsubscribe?.(
|
|
45
|
+
unsubscribe ({ subscriberDID, topicID }) {
|
|
46
|
+
return hub.unsubscribe?.({
|
|
47
|
+
subscriberDID,
|
|
48
|
+
topicID
|
|
49
|
+
});
|
|
34
50
|
},
|
|
35
|
-
receive (subscriberDID, options) {
|
|
36
|
-
const inner = hub.receive(
|
|
51
|
+
receive ({ subscriberDID, ...options }) {
|
|
52
|
+
const inner = hub.receive({
|
|
53
|
+
subscriberDID,
|
|
54
|
+
...options
|
|
55
|
+
});
|
|
37
56
|
const innerIterator = inner[Symbol.asyncIterator]();
|
|
38
57
|
const ackedOnDecrypt = new Set();
|
|
39
58
|
const inFlightDecrypts = new Set();
|
|
@@ -142,6 +161,22 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, drainTimeou
|
|
|
142
161
|
ackHandled(message.sequenceID);
|
|
143
162
|
continue;
|
|
144
163
|
}
|
|
164
|
+
if (cause instanceof ExpiredFrameError) {
|
|
165
|
+
// Meant for this reader, but its key is gone: no later key opens it either.
|
|
166
|
+
onEvent?.({
|
|
167
|
+
type: 'frame-expired',
|
|
168
|
+
error: cause
|
|
169
|
+
});
|
|
170
|
+
onEvent?.({
|
|
171
|
+
type: 'frame-dropped',
|
|
172
|
+
reason: 'expired'
|
|
173
|
+
});
|
|
174
|
+
ackHandled(message.sequenceID);
|
|
175
|
+
if (anchor != null && precedesSequenceID(anchor, message.sequenceID)) {
|
|
176
|
+
onExpired(cause);
|
|
177
|
+
}
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
145
180
|
const err = new DecryptError('decrypt failed', {
|
|
146
181
|
cause
|
|
147
182
|
});
|
|
@@ -169,6 +204,7 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, drainTimeou
|
|
|
169
204
|
done: true
|
|
170
205
|
};
|
|
171
206
|
}
|
|
207
|
+
remember(message.sequenceID);
|
|
172
208
|
const decrypted = {
|
|
173
209
|
sequenceID: message.sequenceID,
|
|
174
210
|
senderDID: message.senderDID,
|
|
@@ -274,6 +310,10 @@ export function createEncryptedHubTunnelTransport(params) {
|
|
|
274
310
|
drains,
|
|
275
311
|
onEncryptError: (err)=>{
|
|
276
312
|
internalController.abort(err);
|
|
313
|
+
},
|
|
314
|
+
// Aborting tears the session down with a session-end, so the peer fails promptly too.
|
|
315
|
+
onExpired: (err)=>{
|
|
316
|
+
internalController.abort(err);
|
|
277
317
|
}
|
|
278
318
|
});
|
|
279
319
|
const transport = createHubTunnelTransport({
|
package/lib/encryptor.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export type Encryptor = {
|
|
2
2
|
encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Rejects with `DuplicateFrameError` for a ciphertext this reader has already opened, and with
|
|
5
|
+
* `ExpiredFrameError` for one whose key it no longer holds.
|
|
6
|
+
*/
|
|
4
7
|
decrypt(ciphertext: Uint8Array): Promise<Uint8Array>;
|
|
5
8
|
};
|
package/lib/errors.d.ts
CHANGED
|
@@ -15,6 +15,15 @@ export declare class DecryptError extends Error {
|
|
|
15
15
|
export declare class DuplicateFrameError extends Error {
|
|
16
16
|
name: string;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Thrown by an `Encryptor.decrypt` for a ciphertext this reader can never open: its key was
|
|
20
|
+
* spent or discarded (a ratchet that moved past the frame's generation without retaining its
|
|
21
|
+
* key). Unlike a plain decrypt failure the frame is not corrupt or foreign: it was meant for
|
|
22
|
+
* this reader and is lost, so whatever depended on it should fail now rather than wait.
|
|
23
|
+
*/
|
|
24
|
+
export declare class ExpiredFrameError extends Error {
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
18
27
|
export declare class EncryptError extends Error {
|
|
19
28
|
name: string;
|
|
20
29
|
}
|
package/lib/errors.js
CHANGED
|
@@ -14,6 +14,14 @@ export class DecryptError extends Error {
|
|
|
14
14
|
*/ export class DuplicateFrameError extends Error {
|
|
15
15
|
name = 'DuplicateFrameError';
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Thrown by an `Encryptor.decrypt` for a ciphertext this reader can never open: its key was
|
|
19
|
+
* spent or discarded (a ratchet that moved past the frame's generation without retaining its
|
|
20
|
+
* key). Unlike a plain decrypt failure the frame is not corrupt or foreign: it was meant for
|
|
21
|
+
* this reader and is lost, so whatever depended on it should fail now rather than wait.
|
|
22
|
+
*/ export class ExpiredFrameError extends Error {
|
|
23
|
+
name = 'ExpiredFrameError';
|
|
24
|
+
}
|
|
17
25
|
export class EncryptError extends Error {
|
|
18
26
|
name = 'EncryptError';
|
|
19
27
|
}
|
package/lib/events.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import type { DecryptError, EnvelopeDecodeError } from './errors.js';
|
|
2
|
-
export type FrameDroppedReason = 'envelope-decode' | 'decrypt'
|
|
1
|
+
import type { DecryptError, EnvelopeDecodeError, ExpiredFrameError } from './errors.js';
|
|
2
|
+
export type FrameDroppedReason = 'envelope-decode' | 'decrypt'
|
|
3
|
+
/** Meant for this reader, but its key is gone: see `ExpiredFrameError`. */
|
|
4
|
+
| 'expired' | 'group-mismatch' | 'topic-mismatch' | 'session-mismatch' | 'stale-session-end' | 'dedup';
|
|
3
5
|
export type ObservabilityEvent = {
|
|
4
6
|
type: 'decrypt-failed';
|
|
5
7
|
error: DecryptError;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'frame-expired';
|
|
10
|
+
error: ExpiredFrameError;
|
|
6
11
|
} | {
|
|
7
12
|
type: 'envelope-decode-failed';
|
|
8
13
|
error: EnvelopeDecodeError;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { createEncryptedHubTunnelTransport, type EncryptedHubTunnelTransportParams, } from './encrypted-transport.js';
|
|
2
2
|
export type { Encryptor } from './encryptor.js';
|
|
3
3
|
export { decodeEnvelope, encodeEnvelope, TUNNEL_ENVELOPE_VERSION, type TunnelEnvelope, tunnelEnvelopeSchema, } from './envelope.js';
|
|
4
|
-
export { BackpressureError, DecryptError, DuplicateFrameError, EncryptError, EnvelopeDecodeError, FrameDecodeError, HubReconnectingError, SessionNotEstablishedError, } from './errors.js';
|
|
4
|
+
export { BackpressureError, DecryptError, DuplicateFrameError, EncryptError, EnvelopeDecodeError, ExpiredFrameError, 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 { precedesSequenceID } from './sequence.js';
|
|
8
|
+
export type { HubBaseReceiveParams, HubBaseSubscribeParams, HubBaseUnsubscribeParams, } from './transport.js';
|
|
7
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/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @kumiai/hub-tunnel — peer-to-peer Enkaku transport over the hub relay
|
|
2
2
|
export { createEncryptedHubTunnelTransport } from './encrypted-transport.js';
|
|
3
3
|
export { decodeEnvelope, encodeEnvelope, TUNNEL_ENVELOPE_VERSION, tunnelEnvelopeSchema } from './envelope.js';
|
|
4
|
-
export { BackpressureError, DecryptError, DuplicateFrameError, EncryptError, EnvelopeDecodeError, FrameDecodeError, HubReconnectingError, SessionNotEstablishedError } from './errors.js';
|
|
4
|
+
export { BackpressureError, DecryptError, DuplicateFrameError, EncryptError, EnvelopeDecodeError, ExpiredFrameError, FrameDecodeError, HubReconnectingError, SessionNotEstablishedError } from './errors.js';
|
|
5
5
|
export { decodeFrame, encodeFrame, HUB_FRAME_VERSION, hubFrameSchema } from './frame.js';
|
|
6
|
+
export { precedesSequenceID } from './sequence.js';
|
|
6
7
|
export { createHubTunnelTransport } from './transport.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether hub sequence ID `a` precedes `b`. A hub mints them from one counter across topics,
|
|
3
|
+
* fixed-width and zero-padded, so they compare as strings; a pair of plain decimals of differing
|
|
4
|
+
* width compares by value.
|
|
5
|
+
*/
|
|
6
|
+
export declare function precedesSequenceID(a: string, b: string): boolean;
|
package/lib/sequence.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether hub sequence ID `a` precedes `b`. A hub mints them from one counter across topics,
|
|
3
|
+
* fixed-width and zero-padded, so they compare as strings; a pair of plain decimals of differing
|
|
4
|
+
* width compares by value.
|
|
5
|
+
*/ export function precedesSequenceID(a, b) {
|
|
6
|
+
if (a.length !== b.length && /^\d+$/.test(a) && /^\d+$/.test(b)) return BigInt(a) < BigInt(b);
|
|
7
|
+
return a < b;
|
|
8
|
+
}
|
package/lib/transport.d.ts
CHANGED
|
@@ -91,6 +91,17 @@ export type HubFetchTopicResult = {
|
|
|
91
91
|
/** Removed log frames after the exclusive cursor, from the same snapshot as messages and head. */
|
|
92
92
|
gap: boolean;
|
|
93
93
|
};
|
|
94
|
+
export type HubBaseSubscribeParams = {
|
|
95
|
+
subscriberDID: string;
|
|
96
|
+
topicID: string;
|
|
97
|
+
} & HubSubscribeOptions;
|
|
98
|
+
export type HubBaseUnsubscribeParams = {
|
|
99
|
+
subscriberDID: string;
|
|
100
|
+
topicID: string;
|
|
101
|
+
};
|
|
102
|
+
export type HubBaseReceiveParams = {
|
|
103
|
+
subscriberDID: string;
|
|
104
|
+
} & HubReceiveOptions;
|
|
94
105
|
/**
|
|
95
106
|
* The APIs both hub kinds share — everything except how you publish. `events` are
|
|
96
107
|
* connection-lifecycle (reconnect/connect/disconnect), emitted by a mailbox and a log hub alike, so
|
|
@@ -98,9 +109,9 @@ export type HubFetchTopicResult = {
|
|
|
98
109
|
* own `publish` on top; neither is derived from the other.
|
|
99
110
|
*/
|
|
100
111
|
export type HubBase = {
|
|
101
|
-
subscribe: (
|
|
102
|
-
unsubscribe?: (
|
|
103
|
-
receive: (
|
|
112
|
+
subscribe: (params: HubBaseSubscribeParams) => Promise<void> | void;
|
|
113
|
+
unsubscribe?: (params: HubBaseUnsubscribeParams) => Promise<void> | void;
|
|
114
|
+
receive: (params: HubBaseReceiveParams) => HubReceiveSubscription;
|
|
104
115
|
readonly events?: MailboxHubEvents;
|
|
105
116
|
};
|
|
106
117
|
/**
|
package/lib/transport.js
CHANGED
|
@@ -32,12 +32,16 @@ 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]();
|
|
@@ -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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/hub-tunnel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
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.
|
|
29
|
+
"@enkaku/transport": "^0.23.0",
|
|
30
|
+
"@kumiai/hub-protocol": "^0.13.0",
|
|
31
31
|
"@sozai/async": "^0.4.3",
|
|
32
|
-
"@sozai/codec": "^0.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.0",
|
|
38
|
+
"@enkaku/protocol": "^0.23.0",
|
|
39
|
+
"@enkaku/server": "^0.23.0",
|
|
40
|
+
"@kokuin/token": "^0.7.0",
|
|
41
|
+
"@kumiai/hub-conformance": "^0.13.0"
|
|
42
42
|
},
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|