@kumiai/hub-tunnel 0.12.0 → 0.12.2

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.
@@ -1,8 +1,15 @@
1
1
  import { fromB64, toB64 } from '@sozai/codec';
2
2
  import { decodeEnvelope, encodeEnvelope } from './envelope.js';
3
- import { DecryptError, 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,11 +27,13 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, drainTimeou
20
27
  groupID,
21
28
  ciphertext: toB64(ciphertextBytes)
22
29
  };
23
- return await hub.publish({
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
38
  subscribe (subscriberDID, topicID, options) {
30
39
  return hub.subscribe(subscriberDID, topicID, options);
@@ -132,6 +141,32 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, drainTimeou
132
141
  try {
133
142
  plaintext = await decryptWork;
134
143
  } catch (cause) {
144
+ if (cause instanceof DuplicateFrameError) {
145
+ // Opened once already: the redelivery of a frame whose ack was lost. Acked so the
146
+ // hub stops redelivering it.
147
+ onEvent?.({
148
+ type: 'frame-dropped',
149
+ reason: 'dedup'
150
+ });
151
+ ackHandled(message.sequenceID);
152
+ continue;
153
+ }
154
+ if (cause instanceof ExpiredFrameError) {
155
+ // Meant for this reader, but its key is gone: no later key opens it either.
156
+ onEvent?.({
157
+ type: 'frame-expired',
158
+ error: cause
159
+ });
160
+ onEvent?.({
161
+ type: 'frame-dropped',
162
+ reason: 'expired'
163
+ });
164
+ ackHandled(message.sequenceID);
165
+ if (anchor != null && precedesSequenceID(anchor, message.sequenceID)) {
166
+ onExpired(cause);
167
+ }
168
+ continue;
169
+ }
135
170
  const err = new DecryptError('decrypt failed', {
136
171
  cause
137
172
  });
@@ -159,6 +194,7 @@ function wrapHub({ hub, encryptor, groupID, onEvent, onEncryptError, drainTimeou
159
194
  done: true
160
195
  };
161
196
  }
197
+ remember(message.sequenceID);
162
198
  const decrypted = {
163
199
  sequenceID: message.sequenceID,
164
200
  senderDID: message.senderDID,
@@ -264,6 +300,10 @@ export function createEncryptedHubTunnelTransport(params) {
264
300
  drains,
265
301
  onEncryptError: (err)=>{
266
302
  internalController.abort(err);
303
+ },
304
+ // Aborting tears the session down with a session-end, so the peer fails promptly too.
305
+ onExpired: (err)=>{
306
+ internalController.abort(err);
267
307
  }
268
308
  });
269
309
  const transport = createHubTunnelTransport({
@@ -1,4 +1,8 @@
1
1
  export type Encryptor = {
2
2
  encrypt(plaintext: Uint8Array): Promise<Uint8Array>;
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
+ */
3
7
  decrypt(ciphertext: Uint8Array): Promise<Uint8Array>;
4
8
  };
package/lib/errors.d.ts CHANGED
@@ -7,6 +7,23 @@ export declare class EnvelopeDecodeError extends Error {
7
7
  export declare class DecryptError extends Error {
8
8
  name: string;
9
9
  }
10
+ /**
11
+ * Thrown by an `Encryptor.decrypt` for a ciphertext this reader has already opened. A hub
12
+ * redelivers a frame whose ack was lost, and a ratcheting cipher cannot open it twice: that is a
13
+ * duplicate, not a lost frame.
14
+ */
15
+ export declare class DuplicateFrameError extends Error {
16
+ name: string;
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
+ }
10
27
  export declare class EncryptError extends Error {
11
28
  name: string;
12
29
  }
package/lib/errors.js CHANGED
@@ -7,6 +7,21 @@ export class EnvelopeDecodeError extends Error {
7
7
  export class DecryptError extends Error {
8
8
  name = 'DecryptError';
9
9
  }
10
+ /**
11
+ * Thrown by an `Encryptor.decrypt` for a ciphertext this reader has already opened. A hub
12
+ * redelivers a frame whose ack was lost, and a ratcheting cipher cannot open it twice: that is a
13
+ * duplicate, not a lost frame.
14
+ */ export class DuplicateFrameError extends Error {
15
+ name = 'DuplicateFrameError';
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
+ }
10
25
  export class EncryptError extends Error {
11
26
  name = 'EncryptError';
12
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' | 'group-mismatch' | 'topic-mismatch' | 'session-mismatch' | 'stale-session-end' | 'dedup';
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,8 @@
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, 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';
7
8
  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, 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;
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/hub-tunnel",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "Peer-to-peer Enkaku transport tunneled through a hub relay",
5
5
  "keywords": [
6
6
  "enkaku",
@@ -26,18 +26,18 @@
26
26
  "lib/*"
27
27
  ],
28
28
  "dependencies": {
29
- "@enkaku/transport": "^0.22.2",
29
+ "@enkaku/transport": "^0.22.3",
30
30
  "@kumiai/hub-protocol": "^0.12.0",
31
- "@sozai/async": "^0.4.1",
31
+ "@sozai/async": "^0.4.3",
32
32
  "@sozai/codec": "^0.4.3",
33
33
  "@sozai/event": "^0.1.6",
34
34
  "@sozai/schema": "^0.1.6"
35
35
  },
36
36
  "devDependencies": {
37
- "@enkaku/client": "^0.22.2",
38
- "@enkaku/protocol": "^0.22.2",
39
- "@enkaku/server": "^0.22.2",
40
- "@kokuin/token": "^0.6.2",
37
+ "@enkaku/client": "^0.22.3",
38
+ "@enkaku/protocol": "^0.22.3",
39
+ "@enkaku/server": "^0.22.3",
40
+ "@kokuin/token": "^0.6.4",
41
41
  "@kumiai/hub-conformance": "^0.12.0"
42
42
  },
43
43
  "publishConfig": {