@minip2p/core 0.9.1 → 0.10.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/README.md CHANGED
@@ -39,11 +39,11 @@ for await (const event of endpoint.events({ signal })) {
39
39
  }
40
40
  ```
41
41
 
42
- A peer holds one connection at a time. `connectionEstablished` and `connectionClosed` mark the peer connecting and disconnecting; when a newer connection takes the peer's slot, a single `connectionReplaced` (`{ peerId, oldConnId, newConnId }`) stands in for both, the peer stays connected, and streams and pending opens on `oldConnId` end with `StreamClosedError`. `peerReady` carries the `connId` that completed Identify and fires once per connection. `waitPeerReady` follows the peer's current connection, so a stale `peerReady` for a replaced connection never resolves it; it resolves at once when `connectionInfo(peerId).readyProtocols` shows the current connection is already ready, and rejects with `PeerDisconnectedError` when that connection closes.
42
+ A peer holds one connection at a time. `connectionEstablished` and `connectionClosed` mark the peer connecting and disconnecting, and `connectionClosed` carries a `reason` (`ConnectionCloseReason`: `local`, `remote` with the peer's `code`, `timedOut`, `stalled`, or `error` with a `message`); when a newer connection takes the peer's slot, a single `connectionReplaced` (`{ peerId, oldConnId, newConnId }`) stands in for both, the peer stays connected, and streams and pending opens on `oldConnId` end with `StreamClosedError`. `peerReady` carries the `connId` that completed Identify and fires once per connection. `waitPeerReady` follows the peer's current connection, so a stale `peerReady` for a replaced connection never resolves it; it resolves at once when `connectionInfo(peerId).readyProtocols` shows the current connection is already ready, and rejects with `PeerDisconnectedError` when that connection closes.
43
43
 
44
44
  `connect` accepts a Connection target: a peer ID, one complete peer multiaddress, or a list of complete addresses naming one peer. For a Connection attempt, a timeout ends only the local wait (`cancelOnTimeout: true` opts into cancelling), an aborted `signal` cancels the attempt (the wait settles from its terminal), and a terminal lost to event overflow rejects with `ConnectResultLostError`. Waits never stop unrelated events from reaching other subscribers.
45
45
 
46
- Use `endpoint.on("stream", handler)` to claim inbound streams. Operations accept `{ timeoutMs, signal }`; the default timeout is 65 seconds and `timeoutMs: 0` disables it. Both endpoints and streams implement `Symbol.dispose`, including the fallback used by `await using`; neither implements `Symbol.asyncDispose`.
46
+ Use `endpoint.on("stream", handler)` to claim inbound streams. Operations accept `{ timeoutMs, signal }`; the default timeout is 65 seconds and `timeoutMs: 0` disables it. Streams implement `Symbol.dispose`. `endpoint.close(timeoutMs = 500)` rejects outstanding operations at once, then resolves with an `EndpointCloseReport` (`{ reason, outcome?, unfinished }`) once native has disconnected peers and drained for up to `timeoutMs` (`0` tears down at once); `onClose` observers receive the same report then. The deadline bounds teardown time, not delivery of accepted writes. Endpoints implement `Symbol.asyncDispose` (awaits `close()`) and `Symbol.dispose` (starts it).
47
47
 
48
48
  `create` throws `InvalidConfigError` when native rejects the configuration. Its `capability` (`Gossipsub`, `Mdns`, `Discovery`, `Transport`, or `Options`, from `ConfigCapability`) names the part to change, and `detail` carries the native diagnostic. A malformed listen address and a socket the platform refused are not config errors; they keep their native error codes.
49
49
 
package/dist/backend.d.ts CHANGED
@@ -1,4 +1,40 @@
1
+ import { CloseOutcome } from "./types.js";
1
2
  import type { Bytes, ConnectionInfo, IdentifyInfo, KnownPeerInfo, Minip2pConfig, PathKind, P2pEvent, Reachability, RelayReservationInfo } from "./types.js";
3
+ /** How the native endpoint's close drain ended (`EndpointClosed`). */
4
+ export interface BackendClosed {
5
+ readonly outcome: CloseOutcome;
6
+ /** Peers whose connections were still tracked, each once. */
7
+ readonly unfinished: readonly string[];
8
+ }
9
+ /**
10
+ * The peers `EndpointClosed`'s unfinished connections name, each once. An
11
+ * inbound handshake that had not identified its peer names none.
12
+ */
13
+ export declare const unfinishedPeers: (unfinished: readonly {
14
+ readonly peerId?: string | undefined;
15
+ }[]) => string[];
16
+ /**
17
+ * A native adapter's side of {@link Minip2pBackend.close}: stops native once,
18
+ * then resolves with the native `EndpointClosed` the adapter hands to
19
+ * {@link settle}, or at once when no driver started.
20
+ */
21
+ export declare class NativeCloseLatch {
22
+ #private;
23
+ constructor();
24
+ /** Whether `close` was called; later native events can be ignored. */
25
+ get requested(): boolean;
26
+ /** Records the native `EndpointClosed`, which may precede `close`. */
27
+ settle(closed: BackendClosed): void;
28
+ /**
29
+ * Runs `stop` on the first call, then resolves once native closed and
30
+ * `release`, if any, ran. Later calls return the same promise.
31
+ */
32
+ close(started: boolean, stop: () => void, release?: () => void): Promise<BackendClosed>;
33
+ }
34
+ /** A native connection snapshot: {@link ConnectionInfo} with the raw path. */
35
+ export interface BackendConnectionInfo extends Omit<ConnectionInfo, "path"> {
36
+ readonly path: PathKind;
37
+ }
2
38
  /** Full native identity allocated for an outbound stream. */
3
39
  export interface BackendOpenStream {
4
40
  /** Endpoint-local connection carrying the stream. */
@@ -31,8 +67,14 @@ export interface Minip2pBackend {
31
67
  * elided, dropped, or coalesced into an earlier one.
32
68
  */
33
69
  eventHandled?: (event: P2pEvent) => void;
34
- /** Idempotently releases the native endpoint. */
35
- close: () => void;
70
+ /**
71
+ * Requests native shutdown, draining for up to `timeoutMs` (`0` tears down
72
+ * at once), and resolves with the native `EndpointClosed`, the driver's
73
+ * last event, once the native endpoint is released. Idempotent: later
74
+ * calls return the same promise and their timeout is ignored. Resolves at
75
+ * once for a backend whose `start` failed. Never rejects.
76
+ */
77
+ close: (timeoutMs: number) => Promise<BackendClosed>;
36
78
  /** Returns the local peer ID. */
37
79
  peerId: () => string;
38
80
  /** Returns bound peer multiaddresses. */
@@ -49,10 +91,10 @@ export interface Minip2pBackend {
49
91
  discoveryNowMs: () => number | undefined;
50
92
  /** Returns the active relay reservation. */
51
93
  activeReservation: () => RelayReservationInfo | undefined;
52
- /** Returns the authoritative native path to a peer. */
94
+ /** Returns the path of a peer's established connection. */
53
95
  path: (peerId: string) => PathKind | undefined;
54
- /** Returns the transport connection selected for a peer. */
55
- connectionInfo: (peerId: string) => ConnectionInfo | undefined;
96
+ /** Returns a snapshot of a peer's established connection. */
97
+ connectionInfo: (peerId: string) => BackendConnectionInfo | undefined;
56
98
  /** Builds a circuit address through a relay. */
57
99
  circuitAddress: (relayAddress: string, peerId: string) => string;
58
100
  /** Returns the native reachability state. */
@@ -113,7 +155,7 @@ export { resolveEndpointConfig, type BackendEndpointConfig } from "./config.js";
113
155
  export { EventDrain } from "./event-drain.js";
114
156
  export { ConnectionIdMap, u64ToNumber } from "./native-ids.js";
115
157
  export { SettleHandles, eventStream } from "./settle-handles.js";
116
- export { P2pEvent_Tags, PathKind_Tags, type P2pEvent, type P2pEventByTag, type PathKind, } from "./types.js";
158
+ export { ConnectionCloseReason_Tags, P2pEvent_Tags, PathKind_Tags, type P2pEvent, type P2pEventByTag, type PathKind, type RawConnectionCloseReason, } from "./types.js";
117
159
  /**
118
160
  * The parts of a native `FfiError` an adapter reads off its toolchain's error
119
161
  * value: the variant name, its detail field, and an `InvalidConfig`'s
@@ -1 +1 @@
1
- {"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EACV,KAAK,EACL,cAAc,EACd,YAAY,EACZ,aAAa,EACb,aAAa,EACb,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB,6DAA6D;AAC7D,MAAM,WAAW,iBAAiB;IAChC,qDAAqD;IACrD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,kDAAkD;IAClD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAC5B;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAClD;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,CAAC;AAE1E;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,iDAAiD;IACjD,KAAK,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,QAAQ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KAAK,IAAI,CAAC;IACpC,8CAA8C;IAC9C,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACtC;AAED,OAAO,EAAE,qBAAqB,EAAE,KAAK,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACjE,OAAO,EACL,0BAA0B,EAC1B,aAAa,EACb,aAAa,EACb,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,wBAAwB,GAC9B,MAAM,YAAY,CAAC;AAEpB;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C;AAMD;;;;;GAKG;AACH,eAAO,MAAM,aAAa,iCAIvB,iBAAiB,KAAG,KAAK,GAAG,SAoB9B,CAAC;AAEF,gFAAgF;AAChF,MAAM,WAAW,qBAAqB;IACpC,MAAM,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,cAAc,CAAC;IAClD,iBAAiB,EAAE,MAAM,UAAU,CAAC;IACpC,mBAAmB,EAAE,CAAC,SAAS,EAAE,KAAK,KAAK,MAAM,CAAC;IAClD,cAAc,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;CAClE"}
package/dist/backend.js CHANGED
@@ -1,10 +1,63 @@
1
1
  import { BackpressureError, InvalidConfigError, MessageTooLargeError, NotPermittedError, } from "./errors.js";
2
- import { ConfigCapability } from "./types.js";
2
+ import { CloseOutcome, ConfigCapability } from "./types.js";
3
+ /**
4
+ * The peers `EndpointClosed`'s unfinished connections name, each once. An
5
+ * inbound handshake that had not identified its peer names none.
6
+ */
7
+ export const unfinishedPeers = (unfinished) => [
8
+ ...new Set(unfinished.flatMap(({ peerId }) => (peerId === undefined ? [] : [peerId]))),
9
+ ];
10
+ /**
11
+ * A native adapter's side of {@link Minip2pBackend.close}: stops native once,
12
+ * then resolves with the native `EndpointClosed` the adapter hands to
13
+ * {@link settle}, or at once when no driver started.
14
+ */
15
+ export class NativeCloseLatch {
16
+ #closed;
17
+ #resolve;
18
+ #closing;
19
+ constructor() {
20
+ // oxlint-disable-next-line promise/avoid-new -- Settled by a native event, not a callback API.
21
+ this.#closed = new Promise((resolve) => {
22
+ this.#resolve = resolve;
23
+ });
24
+ }
25
+ /** Whether `close` was called; later native events can be ignored. */
26
+ get requested() {
27
+ return this.#closing !== undefined;
28
+ }
29
+ /** Records the native `EndpointClosed`, which may precede `close`. */
30
+ settle(closed) {
31
+ this.#resolve?.(closed);
32
+ }
33
+ /**
34
+ * Runs `stop` on the first call, then resolves once native closed and
35
+ * `release`, if any, ran. Later calls return the same promise.
36
+ */
37
+ close(started, stop, release) {
38
+ this.#closing ??= this.#finish(started, stop, release);
39
+ return this.#closing;
40
+ }
41
+ async #finish(started, stop, release) {
42
+ stop();
43
+ // Without a driver, native released the endpoint at once.
44
+ const closed = started
45
+ ? await this.#closed
46
+ : { outcome: CloseOutcome.Drained, unfinished: [] };
47
+ try {
48
+ release?.();
49
+ }
50
+ catch {
51
+ // The endpoint is closed either way; the report still stands.
52
+ }
53
+ return closed;
54
+ }
55
+ }
3
56
  export { resolveEndpointConfig } from "./config.js";
4
57
  export { EventDrain } from "./event-drain.js";
5
58
  export { ConnectionIdMap, u64ToNumber } from "./native-ids.js";
6
59
  export { SettleHandles, eventStream } from "./settle-handles.js";
7
- export { P2pEvent_Tags, PathKind_Tags, } from "./types.js";
60
+ export { ConnectionCloseReason_Tags, P2pEvent_Tags, PathKind_Tags, } from "./types.js";
8
61
  // Each capability's name is its value, so an own key is a capability.
9
62
  const isConfigCapability = (name) => Object.hasOwn(ConfigCapability, name);
10
63
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"backend.js","sourceRoot":"","sources":["../src/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AA0H9C,OAAO,EAAE,qBAAqB,EAA8B,MAAM,aAAa,CAAC;AAChF,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACjE,OAAO,EACL,aAAa,EACb,aAAa,GAId,MAAM,YAAY,CAAC;AAapB,sEAAsE;AACtE,MAAM,kBAAkB,GAAG,CAAC,IAAY,EAA4B,EAAE,CACpE,MAAM,CAAC,MAAM,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAC;AAExC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,EAC5B,UAAU,EACV,MAAM,EACN,GAAG,GACe,EAAqB,EAAE;IACzC,QAAQ,GAAG,EAAE,CAAC;QACZ,KAAK,cAAc,EAAE,CAAC;YACpB,OAAO,IAAI,iBAAiB,EAAE,CAAC;QACjC,CAAC;QACD,KAAK,iBAAiB,EAAE,CAAC;YACvB,OAAO,IAAI,oBAAoB,EAAE,CAAC;QACpC,CAAC;QACD,KAAK,cAAc,EAAE,CAAC;YACpB,OAAO,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC;QACvC,CAAC;QACD,KAAK,eAAe,EAAE,CAAC;YACrB,OAAO,UAAU,KAAK,SAAS,IAAI,kBAAk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C;QACjC,CAAC;QACD,KAAK,iBAAiB,EAAE,CAAC;YACvB,OAAO,IAAI,oBAAoB,EAAE,CAAC;QACpC,CAAC;QACD,KAAK,cAAc,EAAE,CAAC;YACpB,OAAO,IAAI,iBAAiB,CAAC,MAAM,CAAC,CAAC;QACvC,CAAC;QACD,KAAK,eAAe,EAAE,CAAC;YACrB,OAAO,UAAU,KAAK,SAAS,IAAI,kBAAkB,CAAC,UAAU,CAAC;gBAC/D,CAAC,CAAC,IAAI,kBAAkB,CAAC,UAAU,EAAE,MAAM,IAAI,EAAE,CAAC;gBAClD,CAAC,CAAC,SAAS,CAAC;QAChB,CAAC;QACD,SAAS,CAAC;YACR,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;AACH,CAAC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { AbortError, BackpressureError, ClosedError, ConnectCancelledError, ConnectFailedError, ConnectResultLostError, ConnectResultUnavailableError, DriverFailedError, EventQueueOverflowError, InvalidConfigError, MessageTooLargeError, NotPermittedError, OpenStreamError, PeerDisconnectedError, StreamClosedError, TimeoutError, WriteBufferFullError, } from "./errors.js";
2
2
  export { Minip2pBase, Stream, type StreamEventMap } from "./sdk.js";
3
- export { ConfigCapability, DiscoverySource, DriverFailureKind, EndpointErrorKind, MdnsFailureKind, NatErrorKind, Reachability, type Bytes, type CloseReason, type ConnectionInfo, type ConnectOptions, type ConnectResult, type ConnectTarget, type EventsOptions, type IdentifyInfo, type InboundStreamMeta, type KnownPeerInfo, type Minip2pCatchAllEventMap, type Minip2pConfig, type Minip2pDiscoveryOptions, type Minip2pEvent, type Minip2pMdnsOptions, type Minip2pNamedEventMap, type OnceOptions, type OpOptions, type Path, type RelayReservationInfo, type Unsubscribe, type WaitForOptions, } from "./types.js";
3
+ export { CloseOutcome, ConfigCapability, DiscoverySource, DriverFailureKind, EndpointErrorKind, MdnsFailureKind, NatErrorKind, Reachability, type Bytes, type ConnectionCloseReason, type ConnectionInfo, type ConnectOptions, type ConnectResult, type ConnectTarget, type EndpointCloseReason, type EndpointCloseReport, type EventsOptions, type IdentifyInfo, type InboundStreamMeta, type KnownPeerInfo, type Minip2pCatchAllEventMap, type Minip2pConfig, type Minip2pDiscoveryOptions, type Minip2pEvent, type Minip2pMdnsOptions, type Minip2pNamedEventMap, type OnceOptions, type OpOptions, type Path, type RelayReservationInfo, type Unsubscribe, type WaitForOptions, } from "./types.js";
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,WAAW,EACX,qBAAqB,EACrB,kBAAkB,EAClB,sBAAsB,EACtB,6BAA6B,EAC7B,iBAAiB,EACjB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,eAAe,EACf,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AACpE,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,KAAK,KAAK,EACV,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAClB,KAAK,uBAAuB,EAC5B,KAAK,aAAa,EAClB,KAAK,uBAAuB,EAC5B,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,WAAW,EACX,qBAAqB,EACrB,kBAAkB,EAClB,sBAAsB,EACtB,6BAA6B,EAC7B,iBAAiB,EACjB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,eAAe,EACf,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AACpE,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,KAAK,KAAK,EACV,KAAK,qBAAqB,EAC1B,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,aAAa,EAClB,KAAK,uBAAuB,EAC5B,KAAK,aAAa,EAClB,KAAK,uBAAuB,EAC5B,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { AbortError, BackpressureError, ClosedError, ConnectCancelledError, ConnectFailedError, ConnectResultLostError, ConnectResultUnavailableError, DriverFailedError, EventQueueOverflowError, InvalidConfigError, MessageTooLargeError, NotPermittedError, OpenStreamError, PeerDisconnectedError, StreamClosedError, TimeoutError, WriteBufferFullError, } from "./errors.js";
2
2
  export { Minip2pBase, Stream } from "./sdk.js";
3
- export { ConfigCapability, DiscoverySource, DriverFailureKind, EndpointErrorKind, MdnsFailureKind, NatErrorKind, Reachability, } from "./types.js";
3
+ export { CloseOutcome, ConfigCapability, DiscoverySource, DriverFailureKind, EndpointErrorKind, MdnsFailureKind, NatErrorKind, Reachability, } from "./types.js";
4
4
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,WAAW,EACX,qBAAqB,EACrB,kBAAkB,EAClB,sBAAsB,EACtB,6BAA6B,EAC7B,iBAAiB,EACjB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,eAAe,EACf,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAuB,MAAM,UAAU,CAAC;AACpE,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,YAAY,GAuBb,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,WAAW,EACX,qBAAqB,EACrB,kBAAkB,EAClB,sBAAsB,EACtB,6BAA6B,EAC7B,iBAAiB,EACjB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,eAAe,EACf,qBAAqB,EACrB,iBAAiB,EACjB,YAAY,EACZ,oBAAoB,GACrB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAuB,MAAM,UAAU,CAAC;AACpE,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,YAAY,EACZ,YAAY,GAyBb,MAAM,YAAY,CAAC"}
package/dist/sdk.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Minip2pBackend } from "./backend.js";
2
- import type { Bytes, CloseReason, ConnectionInfo, ConnectOptions, ConnectResult, ConnectTarget, EventsOptions, IdentifyInfo, InboundStreamMeta, KnownPeerInfo, Minip2pEvent, Minip2pNamedEventMap, OnceOptions, OpOptions, Path, Reachability, RelayReservationInfo, Unsubscribe, WaitForOptions } from "./types.js";
2
+ import type { Bytes, ConnectionInfo, ConnectOptions, ConnectResult, ConnectTarget, EndpointCloseReport, EventsOptions, IdentifyInfo, InboundStreamMeta, KnownPeerInfo, Minip2pEvent, Minip2pNamedEventMap, OnceOptions, OpOptions, Path, Reachability, RelayReservationInfo, Unsubscribe, WaitForOptions } from "./types.js";
3
3
  type EventKind = keyof Minip2pNamedEventMap;
4
4
  type NamedHandler<Kind extends EventKind> = (payload: Minip2pNamedEventMap[Kind]) => void;
5
5
  type CatchAllHandler = (event: Minip2pEvent) => void;
@@ -145,11 +145,27 @@ export declare class Minip2pBase {
145
145
  } & Minip2pNamedEventMap[Kind]>): Promise<{
146
146
  readonly type: Kind;
147
147
  } & Minip2pNamedEventMap[Kind]>;
148
- /** Registers a one-shot endpoint shutdown observer. */
149
- onClose(handler: (reason: CloseReason) => void): Unsubscribe;
150
- /** Idempotently stops the endpoint and rejects outstanding operations. */
151
- close(): void;
148
+ /**
149
+ * Registers a one-shot endpoint shutdown observer. It stays registered
150
+ * through the native close drain and fires once that finishes, with the
151
+ * same report {@link close} resolves with. A no-op once it has fired.
152
+ */
153
+ onClose(handler: (report: EndpointCloseReport) => void): Unsubscribe;
154
+ /**
155
+ * Stops the endpoint: rejects outstanding operations and drops listeners
156
+ * at once, then has native disconnect peers and drain for up to
157
+ * `timeoutMs` (default 500; `0` tears down at once, best effort). Resolves
158
+ * with the close report once the drain finished, after `onClose`
159
+ * observers ran; it never rejects. The deadline bounds teardown time only,
160
+ * not delivery of accepted writes: QUIC discards queued writes and TCP may
161
+ * abort. Idempotent: a later call returns the same promise, including
162
+ * after a driver failure closed the endpoint.
163
+ */
164
+ close(timeoutMs?: number): Promise<EndpointCloseReport>;
165
+ /** Starts {@link close} with the default drain, without waiting for it. */
152
166
  [Symbol.dispose](): void;
167
+ /** {@link close} with the default drain, for `await using`. */
168
+ [Symbol.asyncDispose](): Promise<void>;
153
169
  /** Returns this endpoint's base58 peer ID. */
154
170
  peerId(): string;
155
171
  /** Returns the currently bound peer multiaddresses. */
@@ -170,9 +186,16 @@ export declare class Minip2pBase {
170
186
  get circuitAddress(): string | undefined;
171
187
  /** Returns the latest AutoNAT reachability verdict. */
172
188
  reachability(): Reachability;
173
- /** Returns the authoritative active path to `peerId`, if connected. */
189
+ /**
190
+ * Returns the path of `peerId`'s established connection; `undefined` only
191
+ * while the peer is not connected.
192
+ */
174
193
  path(peerId: string): Path | undefined;
175
- /** Returns the transport connection selected for `peerId`, if connected. */
194
+ /**
195
+ * Returns a snapshot of `peerId`'s established connection: its connection
196
+ * ID, remote address, path, and Identify protocols once ready;
197
+ * `undefined` while the peer is not connected.
198
+ */
176
199
  connectionInfo(peerId: string): ConnectionInfo | undefined;
177
200
  /** Returns whether the native driver is accepting work. */
178
201
  isRunning(): boolean;
package/dist/sdk.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"sdk.d.ts","sourceRoot":"","sources":["../src/sdk.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAwB,cAAc,EAAE,MAAM,cAAc,CAAC;AAiBzE,OAAO,KAAK,EACV,KAAK,EACL,WAAW,EACX,cAAc,EACd,cAAc,EACd,aAAa,EACb,aAAa,EACb,aAAa,EACb,YAAY,EACZ,iBAAiB,EACjB,aAAa,EAEb,YAAY,EACZ,oBAAoB,EACpB,WAAW,EACX,SAA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GAAG,IAAI,CAG/B;CA6mBF"}
package/dist/sdk.js CHANGED
@@ -1,7 +1,12 @@
1
1
  /* oxlint-disable default-case, func-style, max-classes-per-file, no-await-in-loop, no-empty-function, no-loop-func, no-use-before-define, promise/avoid-new, promise/prefer-await-to-callbacks, promise/prefer-await-to-then, typescript/no-invalid-void-type, unicorn/consistent-function-scoping, unicorn/no-new-array, unicorn/no-useless-spread, unicorn/no-useless-undefined -- Promise adapters, sequential async iterators, void event payloads, snapshot fan-out, and bounded queues are deliberate SDK internals. */
2
2
  import { AbortError, ClosedError, ConnectCancelledError, ConnectFailedError, ConnectResultLostError, ConnectResultUnavailableError, DriverFailedError, EventQueueOverflowError, OpenStreamError, PeerDisconnectedError, StreamClosedError, TimeoutError, WriteBufferFullError, } from "./errors.js";
3
- import { P2pEvent_Tags, PathKind_Tags } from "./types.js";
3
+ import { CloseOutcome, ConnectionCloseReason_Tags, P2pEvent_Tags, PathKind_Tags, } from "./types.js";
4
4
  const DEFAULT_TIMEOUT_MS = 65_000;
5
+ /**
6
+ * Default close drain: the Rust `minip2p::DEFAULT_CLOSE_TIMEOUT` (500 ms)
7
+ * that `Endpoint::close` and native's `DEFAULT_STOP_TIMEOUT_MS` use.
8
+ */
9
+ const DEFAULT_CLOSE_TIMEOUT_MS = 500;
5
10
  const EVENT_QUEUE_CAP = 4096;
6
11
  const EVENT_FLUSH_BATCH = 256;
7
12
  const CONNECT_TERMINAL_CAP = 1024;
@@ -873,6 +878,9 @@ export class Minip2pBase {
873
878
  #dropped = 0;
874
879
  #flushScheduled = false;
875
880
  #closed = false;
881
+ /** Settles once the native drain finished and close observers ran. */
882
+ #closing;
883
+ #closeReported = false;
876
884
  constructor(backend, relayAddresses) {
877
885
  this.#backend = backend;
878
886
  this.#relayAddrs = relayAddresses;
@@ -882,7 +890,7 @@ export class Minip2pBase {
882
890
  });
883
891
  }
884
892
  catch (error) {
885
- backend.close();
893
+ void backend.close(0);
886
894
  throw error;
887
895
  }
888
896
  }
@@ -1006,19 +1014,38 @@ export class Minip2pBase {
1006
1014
  this.#waiters.add(waiter);
1007
1015
  });
1008
1016
  }
1009
- /** Registers a one-shot endpoint shutdown observer. */
1017
+ /**
1018
+ * Registers a one-shot endpoint shutdown observer. It stays registered
1019
+ * through the native close drain and fires once that finishes, with the
1020
+ * same report {@link close} resolves with. A no-op once it has fired.
1021
+ */
1010
1022
  onClose(handler) {
1011
- if (this.#closed) {
1023
+ if (this.#closeReported) {
1012
1024
  return () => { };
1013
1025
  }
1014
1026
  return subscribe(this.#closeHandlers, handler);
1015
1027
  }
1016
- /** Idempotently stops the endpoint and rejects outstanding operations. */
1017
- close() {
1018
- this.#teardown({ reason: "close" }, new ClosedError());
1028
+ /**
1029
+ * Stops the endpoint: rejects outstanding operations and drops listeners
1030
+ * at once, then has native disconnect peers and drain for up to
1031
+ * `timeoutMs` (default 500; `0` tears down at once, best effort). Resolves
1032
+ * with the close report once the drain finished, after `onClose`
1033
+ * observers ran; it never rejects. The deadline bounds teardown time only,
1034
+ * not delivery of accepted writes: QUIC discards queued writes and TCP may
1035
+ * abort. Idempotent: a later call returns the same promise, including
1036
+ * after a driver failure closed the endpoint.
1037
+ */
1038
+ close(timeoutMs = DEFAULT_CLOSE_TIMEOUT_MS) {
1039
+ assertTimeout(timeoutMs);
1040
+ return this.#teardown(new ClosedError(), timeoutMs);
1019
1041
  }
1042
+ /** Starts {@link close} with the default drain, without waiting for it. */
1020
1043
  [Symbol.dispose]() {
1021
- this.close();
1044
+ void this.close();
1045
+ }
1046
+ /** {@link close} with the default drain, for `await using`. */
1047
+ async [Symbol.asyncDispose]() {
1048
+ await this.close();
1022
1049
  }
1023
1050
  /** Returns this endpoint's base58 peer ID. */
1024
1051
  peerId() {
@@ -1076,16 +1103,26 @@ export class Minip2pBase {
1076
1103
  this.#assertOpen();
1077
1104
  return this.#backend.reachability();
1078
1105
  }
1079
- /** Returns the authoritative active path to `peerId`, if connected. */
1106
+ /**
1107
+ * Returns the path of `peerId`'s established connection; `undefined` only
1108
+ * while the peer is not connected.
1109
+ */
1080
1110
  path(peerId) {
1081
1111
  this.#assertOpen();
1082
1112
  const path = this.#backend.path(peerId);
1083
1113
  return path === undefined ? undefined : normalizePath(path);
1084
1114
  }
1085
- /** Returns the transport connection selected for `peerId`, if connected. */
1115
+ /**
1116
+ * Returns a snapshot of `peerId`'s established connection: its connection
1117
+ * ID, remote address, path, and Identify protocols once ready;
1118
+ * `undefined` while the peer is not connected.
1119
+ */
1086
1120
  connectionInfo(peerId) {
1087
1121
  this.#assertOpen();
1088
- return this.#backend.connectionInfo(peerId);
1122
+ const info = this.#backend.connectionInfo(peerId);
1123
+ return info === undefined
1124
+ ? undefined
1125
+ : { ...info, path: normalizePath(info.path) };
1089
1126
  }
1090
1127
  /** Returns whether the native driver is accepting work. */
1091
1128
  isRunning() {
@@ -1504,10 +1541,14 @@ export class Minip2pBase {
1504
1541
  }
1505
1542
  }
1506
1543
  #dispatchNative(event) {
1544
+ // Backends resolve `close` with it instead of delivering it.
1545
+ if (event.tag === P2pEvent_Tags.EndpointClosed) {
1546
+ return;
1547
+ }
1507
1548
  if (event.tag === P2pEvent_Tags.DriverFailed) {
1508
1549
  const failure = new DriverFailedError(event.inner.kind, event.inner.detail);
1509
1550
  this.#dispatch("driverFailed", event.inner, event.inner);
1510
- this.#teardown({ error: failure, reason: "driverFailed" }, failure);
1551
+ void this.#teardown(failure, 0);
1511
1552
  return;
1512
1553
  }
1513
1554
  if (event.tag === P2pEvent_Tags.StreamReady) {
@@ -1816,49 +1857,70 @@ export class Minip2pBase {
1816
1857
  item = this.#queue.shift();
1817
1858
  }
1818
1859
  }
1819
- #teardown(reason, error) {
1820
- if (this.#closed) {
1821
- return;
1860
+ /**
1861
+ * Closes the endpoint once, rejecting what is outstanding with `cause`;
1862
+ * later calls return the first call's promise. A
1863
+ * {@link DriverFailedError} cause reports a `driverFailed` close.
1864
+ */
1865
+ #teardown(cause, timeoutMs) {
1866
+ if (this.#closing !== undefined) {
1867
+ return this.#closing;
1822
1868
  }
1823
1869
  this.#closed = true;
1824
- const closeHandlers = [...this.#closeHandlers];
1825
- this.#closeHandlers.clear();
1826
1870
  this.#clearQueue();
1827
1871
  this.#dropped = 0;
1828
1872
  this.#named.clear();
1829
1873
  this.#catchAll.clear();
1874
+ // Native is asked to close before anything below can call back in: a
1875
+ // re-entrant close returns this promise.
1876
+ let closed;
1877
+ try {
1878
+ closed = this.#backend.close(timeoutMs);
1879
+ }
1880
+ catch {
1881
+ closed = Promise.resolve({
1882
+ outcome: CloseOutcome.Failed,
1883
+ unfinished: [],
1884
+ });
1885
+ }
1886
+ const closing = closed.then(({ outcome, unfinished }) => {
1887
+ const report = cause instanceof DriverFailedError
1888
+ ? { error: cause, reason: "driverFailed", unfinished }
1889
+ : { outcome, reason: "close", unfinished };
1890
+ this.#closeReported = true;
1891
+ const closeHandlers = [...this.#closeHandlers];
1892
+ this.#closeHandlers.clear();
1893
+ for (const handler of closeHandlers) {
1894
+ try {
1895
+ handler(report);
1896
+ }
1897
+ catch {
1898
+ // Close notification is outside the now-closed event queue.
1899
+ }
1900
+ }
1901
+ return report;
1902
+ });
1903
+ this.#closing = closing;
1830
1904
  for (const waiter of [...this.#waiters]) {
1831
- this.#settleWaiter(waiter, error);
1905
+ this.#settleWaiter(waiter, cause);
1832
1906
  }
1833
1907
  for (const attempt of this.#connects.values()) {
1834
- attempt.settle?.({ error, ok: false });
1908
+ attempt.settle?.({ error: cause, ok: false });
1835
1909
  }
1836
1910
  this.#connects.clear();
1837
1911
  this.#terminalConnects.clear();
1838
1912
  for (const pending of this.#pendingOpens.takeAll()) {
1839
1913
  clearPendingOpen(pending);
1840
- pending.reject(error);
1914
+ pending.reject(cause);
1841
1915
  }
1842
1916
  for (const stream of this.#streams.takeAll()) {
1843
- stream.terminal(error);
1917
+ stream.terminal(cause);
1844
1918
  }
1845
1919
  for (const ping of this.#pings.values()) {
1846
- ping.cancel(error);
1920
+ ping.cancel(cause);
1847
1921
  }
1848
1922
  this.#pings.clear();
1849
- try {
1850
- this.#backend.close();
1851
- }
1852
- finally {
1853
- for (const handler of closeHandlers) {
1854
- try {
1855
- handler(reason);
1856
- }
1857
- catch {
1858
- // Close notification is outside the now-closed event queue.
1859
- }
1860
- }
1861
- }
1923
+ return closing;
1862
1924
  }
1863
1925
  #assertOpen() {
1864
1926
  if (this.#closed) {
@@ -1935,15 +1997,40 @@ function normalizeEvent(event) {
1935
1997
  to: normalizePath(event.inner.to),
1936
1998
  };
1937
1999
  }
2000
+ else if (event.tag === P2pEvent_Tags.ConnectionClosed) {
2001
+ payload = {
2002
+ ...event.inner,
2003
+ reason: normalizeCloseReason(event.inner.reason),
2004
+ };
2005
+ }
1938
2006
  return { payload: payload, type };
1939
2007
  }
2008
+ function normalizeCloseReason(reason) {
2009
+ switch (reason.tag) {
2010
+ case ConnectionCloseReason_Tags.Local: {
2011
+ return { kind: "local" };
2012
+ }
2013
+ case ConnectionCloseReason_Tags.Remote: {
2014
+ return { code: reason.inner.code, kind: "remote" };
2015
+ }
2016
+ case ConnectionCloseReason_Tags.TimedOut: {
2017
+ return { kind: "timedOut" };
2018
+ }
2019
+ case ConnectionCloseReason_Tags.Stalled: {
2020
+ return { kind: "stalled" };
2021
+ }
2022
+ case ConnectionCloseReason_Tags.Error: {
2023
+ return { kind: "error", message: reason.inner.message };
2024
+ }
2025
+ }
2026
+ }
1940
2027
  function normalizePath(path) {
1941
2028
  switch (path.tag) {
1942
- case PathKind_Tags.DirectDialed: {
1943
- return { kind: "directDialed" };
2029
+ case PathKind_Tags.Direct: {
2030
+ return { kind: "direct" };
1944
2031
  }
1945
- case PathKind_Tags.DirectPunched: {
1946
- return { kind: "directPunched" };
2032
+ case PathKind_Tags.Punched: {
2033
+ return { kind: "punched" };
1947
2034
  }
1948
2035
  case PathKind_Tags.Relayed: {
1949
2036
  return { kind: "relayed", relayPeerId: path.inner.relayPeerId };