@minip2p/node 0.7.3 → 0.7.5
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 +2 -0
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +117 -232
- package/dist/adapter.js.map +1 -1
- package/dist/addon.d.ts +154 -0
- package/dist/native-shape.d.ts +24 -0
- package/dist/native-shape.d.ts.map +1 -0
- package/dist/native-shape.js +11 -0
- package/dist/native-shape.js.map +1 -0
- package/dist/native.d.ts +4 -71
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js.map +1 -1
- package/package.json +13 -12
- package/src/adapter.ts +156 -263
- package/src/addon.d.ts +154 -0
- package/src/native-shape.ts +41 -0
- package/src/native.ts +6 -81
package/src/addon.d.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/* auto-generated by NAPI-RS from crates/nodejs; regenerate with `pnpm native:build` */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import type {
|
|
4
|
+
NativeConnectionInfo,
|
|
5
|
+
NativeEvent,
|
|
6
|
+
NativeIdentifyInfo,
|
|
7
|
+
NativeKnownPeerInfo,
|
|
8
|
+
NativeOpenStream,
|
|
9
|
+
NativePathKind,
|
|
10
|
+
NativeRelayReservationInfo,
|
|
11
|
+
Reachability,
|
|
12
|
+
} from "./native-shape.js";
|
|
13
|
+
|
|
14
|
+
/** A native minip2p endpoint owned by Node.js. */
|
|
15
|
+
export declare class NodeEndpoint {
|
|
16
|
+
/** Binds a native endpoint without starting its driver. */
|
|
17
|
+
constructor(secretKey: Uint8Array, config: NodeEndpointConfig)
|
|
18
|
+
/** Starts the detached driver with a strong event-loop doorbell. */
|
|
19
|
+
start(doorbell: () => void): void
|
|
20
|
+
/** Requests shutdown without waiting for the driver thread. */
|
|
21
|
+
close(): void
|
|
22
|
+
/** Returns the local peer ID. */
|
|
23
|
+
peerId(): string
|
|
24
|
+
/** Returns bound peer addresses. */
|
|
25
|
+
listenAddrs(): Array<string>
|
|
26
|
+
/** Returns whether the driver accepts commands. */
|
|
27
|
+
isRunning(): boolean
|
|
28
|
+
/** Pulls a bounded batch of native events. */
|
|
29
|
+
drainEvents(limit: number): Array<NativeEvent>
|
|
30
|
+
/** Returns connected peer IDs. */
|
|
31
|
+
connectedPeers(): Array<string>
|
|
32
|
+
/** Returns whether Identify completed for a peer. */
|
|
33
|
+
isPeerReady(peerId: string): boolean
|
|
34
|
+
/** Returns the latest Identify snapshot. */
|
|
35
|
+
peerInfo(peerId: string): NativeIdentifyInfo | null
|
|
36
|
+
/** Accepted for compatibility; has no effect, since the driver sleeps until the endpoint's next deadline. */
|
|
37
|
+
setActive(active: boolean): void
|
|
38
|
+
/** Subscribes to a pubsub topic. */
|
|
39
|
+
subscribe(topic: string): boolean
|
|
40
|
+
/** Unsubscribes from a pubsub topic. */
|
|
41
|
+
unsubscribe(topic: string): boolean
|
|
42
|
+
/** Publishes one pubsub payload. */
|
|
43
|
+
publish(topic: string, data: Uint8Array): void
|
|
44
|
+
/** Starts one ping operation. */
|
|
45
|
+
ping(peerId: string): void
|
|
46
|
+
/** Registers an application protocol. */
|
|
47
|
+
addProtocol(protocolId: string): void
|
|
48
|
+
/** Starts opening an application stream. */
|
|
49
|
+
openStream(peerId: string, protocolId: string): NativeOpenStream
|
|
50
|
+
/**
|
|
51
|
+
* Sends bytes on an application stream, named by its connection and
|
|
52
|
+
* stream ids. Returns `true` once every byte is accepted, or `false`
|
|
53
|
+
* when the rest is held: `StreamWriteAccepted` follows once it is
|
|
54
|
+
* accepted, unless the write side ends first (`StreamWriteStopped`,
|
|
55
|
+
* `StreamClosed`, a reset, or the connection closing or being replaced),
|
|
56
|
+
* which drops the tail.
|
|
57
|
+
*/
|
|
58
|
+
sendStream(peerId: string, connId: bigint, streamId: bigint, data: Uint8Array): boolean
|
|
59
|
+
/** Half-closes the local stream write side. */
|
|
60
|
+
closeStreamWrite(peerId: string, connId: bigint, streamId: bigint): void
|
|
61
|
+
/** Resets an application stream. */
|
|
62
|
+
resetStream(peerId: string, connId: bigint, streamId: bigint): void
|
|
63
|
+
/** Resets and relinquishes an application stream. */
|
|
64
|
+
abandonStream(peerId: string, connId: bigint, streamId: bigint): void
|
|
65
|
+
/**
|
|
66
|
+
* Acknowledges `bytes` of a stream's received data as consumed,
|
|
67
|
+
* letting its sender continue. Every registered protocol needs this.
|
|
68
|
+
*/
|
|
69
|
+
streamConsumed(connId: bigint, streamId: bigint, bytes: number): void
|
|
70
|
+
/**
|
|
71
|
+
* Starts one Connection attempt: a peer ID string, or an array of
|
|
72
|
+
* complete peer addresses naming one peer. Returns the Connect ID.
|
|
73
|
+
*/
|
|
74
|
+
connect(target: string | Array<string>): bigint
|
|
75
|
+
/** Cancels a connection attempt. */
|
|
76
|
+
cancelConnect(id: bigint): void
|
|
77
|
+
/** Disconnects one peer. */
|
|
78
|
+
disconnect(peerId: string): void
|
|
79
|
+
/** Returns the current path to a peer. */
|
|
80
|
+
path(peerId: string): NativePathKind | null
|
|
81
|
+
/** Returns the transport connection selected for a peer. */
|
|
82
|
+
connectionInfo(peerId: string): NativeConnectionInfo | null
|
|
83
|
+
/** Returns the discovery address book. */
|
|
84
|
+
knownPeers(): Array<NativeKnownPeerInfo>
|
|
85
|
+
/** Returns the discovery clock. */
|
|
86
|
+
discoveryNowMs(): bigint | null
|
|
87
|
+
/** Returns the current reachability verdict. */
|
|
88
|
+
reachability(): Reachability
|
|
89
|
+
/** Returns the active relay reservation. */
|
|
90
|
+
activeReservation(): NativeRelayReservationInfo | null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Builds a circuit address through a direct relay address. */
|
|
94
|
+
export declare function circuitAddress(relayAddress: string, peerId: string): string
|
|
95
|
+
|
|
96
|
+
/** Generates a 32-byte Ed25519 secret key. */
|
|
97
|
+
export declare function generateSecretKey(): Uint8Array
|
|
98
|
+
|
|
99
|
+
/** Signed-discovery configuration accepted from Node.js. */
|
|
100
|
+
export interface NodeDiscoveryOptions {
|
|
101
|
+
/** Pubsub topic carrying signed beacons. */
|
|
102
|
+
topic: string
|
|
103
|
+
/** Beacon interval in milliseconds. */
|
|
104
|
+
beaconIntervalMs: bigint
|
|
105
|
+
/** Peer expiry interval in milliseconds. */
|
|
106
|
+
peerTtlMs: bigint
|
|
107
|
+
/** Whether observations may trigger dials. */
|
|
108
|
+
autoDial: boolean
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Endpoint configuration accepted from Node.js. */
|
|
112
|
+
export interface NodeEndpointConfig {
|
|
113
|
+
/** Identify agent version. */
|
|
114
|
+
agentVersion?: string
|
|
115
|
+
/** Relay peer addresses. */
|
|
116
|
+
relays: Array<string>
|
|
117
|
+
/** AutoNAT server peer addresses. */
|
|
118
|
+
autonatServers: Array<string>
|
|
119
|
+
/** Listen multiaddresses, or the QUIC dual-stack defaults when absent. */
|
|
120
|
+
listen?: Array<string>
|
|
121
|
+
/** Whether all outbound paths must remain relayed. */
|
|
122
|
+
forceRelay: boolean
|
|
123
|
+
/** Whether unsigned pubsub messages are accepted. */
|
|
124
|
+
allowUnsigned: boolean
|
|
125
|
+
/** Application protocol identifiers. */
|
|
126
|
+
protocols: Array<string>
|
|
127
|
+
/** Signed discovery configuration. */
|
|
128
|
+
discovery?: NodeDiscoveryOptions
|
|
129
|
+
/** mDNS configuration. */
|
|
130
|
+
mdns?: NodeMdnsOptions
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** mDNS configuration accepted from Node.js. */
|
|
134
|
+
export interface NodeMdnsOptions {
|
|
135
|
+
/** Whether IPv6 multicast is enabled. */
|
|
136
|
+
enableIpv6: boolean
|
|
137
|
+
/** Advertised record lifetime in milliseconds. */
|
|
138
|
+
ttlMs: bigint
|
|
139
|
+
/** Query interval in milliseconds. */
|
|
140
|
+
queryIntervalMs: bigint
|
|
141
|
+
/** Maximum encoded packet size. */
|
|
142
|
+
maxPacketBytes: number
|
|
143
|
+
/** Maximum addresses per response. */
|
|
144
|
+
maxAnnouncedAddrs: number
|
|
145
|
+
/** Interface refresh interval in milliseconds. */
|
|
146
|
+
interfaceRefreshMs: bigint
|
|
147
|
+
/** Socket polling interval in milliseconds. */
|
|
148
|
+
socketPollIntervalMs: bigint
|
|
149
|
+
/** Whether observations may trigger dials. */
|
|
150
|
+
autoDial: boolean
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Derives a peer ID from raw Ed25519 secret key material. */
|
|
154
|
+
export declare function peerIdFromSecretKey(secretKey: Uint8Array): string
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types of the values the native addon returns. `minip2p-ffi-core` derives
|
|
3
|
+
* them with serde (its `js_shape` module pins the representation), and the
|
|
4
|
+
* SDK's backend types describe the same shape, so each native type is the
|
|
5
|
+
* SDK type with napi-rs's value encoding: `u64` arrives as `number`, or as a
|
|
6
|
+
* `bigint` above `u32::MAX`, and bytes arrive as a `Buffer`.
|
|
7
|
+
*
|
|
8
|
+
* The generated `addon.d.ts` imports these names through its header.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ConnectionInfo,
|
|
13
|
+
IdentifyInfo,
|
|
14
|
+
KnownPeerInfo,
|
|
15
|
+
RelayReservationInfo,
|
|
16
|
+
} from "@minip2p/core";
|
|
17
|
+
import type {
|
|
18
|
+
BackendOpenStream,
|
|
19
|
+
P2pEvent,
|
|
20
|
+
PathKind,
|
|
21
|
+
} from "@minip2p/core/backend";
|
|
22
|
+
|
|
23
|
+
/** One SDK backend value as the addon encodes it. */
|
|
24
|
+
export type NativeShape<Value> = Value extends ArrayBuffer
|
|
25
|
+
? Uint8Array
|
|
26
|
+
: Value extends number
|
|
27
|
+
? number | bigint
|
|
28
|
+
: Value extends string | boolean
|
|
29
|
+
? Value
|
|
30
|
+
: Value extends readonly (infer Item)[]
|
|
31
|
+
? readonly NativeShape<Item>[]
|
|
32
|
+
: { readonly [Key in keyof Value]: NativeShape<Value[Key]> };
|
|
33
|
+
|
|
34
|
+
export type NativeEvent = NativeShape<P2pEvent>;
|
|
35
|
+
export type NativeIdentifyInfo = NativeShape<IdentifyInfo>;
|
|
36
|
+
export type NativeKnownPeerInfo = NativeShape<KnownPeerInfo>;
|
|
37
|
+
export type NativeRelayReservationInfo = NativeShape<RelayReservationInfo>;
|
|
38
|
+
export type NativeConnectionInfo = NativeShape<ConnectionInfo>;
|
|
39
|
+
export type NativeOpenStream = NativeShape<BackendOpenStream>;
|
|
40
|
+
export type NativePathKind = NativeShape<PathKind>;
|
|
41
|
+
export type { Reachability } from "@minip2p/core";
|
package/src/native.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
|
|
5
|
+
import type * as Addon from "./addon.js";
|
|
6
|
+
|
|
5
7
|
const require = createRequire(import.meta.url);
|
|
6
8
|
|
|
7
9
|
const supportedTargets = [
|
|
@@ -44,88 +46,11 @@ function unsupportedTarget(target: string, cause?: unknown): Error {
|
|
|
44
46
|
);
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
readonly allowUnsigned: boolean;
|
|
50
|
-
readonly autonatServers: string[];
|
|
51
|
-
readonly forceRelay: boolean;
|
|
52
|
-
readonly discovery?: {
|
|
53
|
-
readonly autoDial: boolean;
|
|
54
|
-
readonly beaconIntervalMs: bigint;
|
|
55
|
-
readonly peerTtlMs: bigint;
|
|
56
|
-
readonly topic: string;
|
|
57
|
-
};
|
|
58
|
-
readonly listen?: string[];
|
|
59
|
-
readonly mdns?: {
|
|
60
|
-
readonly autoDial: boolean;
|
|
61
|
-
readonly enableIpv6: boolean;
|
|
62
|
-
readonly interfaceRefreshMs: bigint;
|
|
63
|
-
readonly maxAnnouncedAddrs: number;
|
|
64
|
-
readonly maxPacketBytes: number;
|
|
65
|
-
readonly queryIntervalMs: bigint;
|
|
66
|
-
readonly socketPollIntervalMs: bigint;
|
|
67
|
-
readonly ttlMs: bigint;
|
|
68
|
-
};
|
|
69
|
-
readonly protocols: string[];
|
|
70
|
-
readonly relays: string[];
|
|
71
|
-
}
|
|
49
|
+
/** The addon's exports, typed by the `addon.d.ts` that `native:build` generates. */
|
|
50
|
+
type NativeBinding = typeof Addon;
|
|
72
51
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
activeReservation: () => unknown;
|
|
76
|
-
addProtocol: (protocolId: string) => void;
|
|
77
|
-
cancelConnect: (id: bigint) => void;
|
|
78
|
-
close: () => void;
|
|
79
|
-
closeStreamWrite: (peerId: string, connId: bigint, streamId: bigint) => void;
|
|
80
|
-
connectedPeers: () => string[];
|
|
81
|
-
connectionInfo: (peerId: string) =>
|
|
82
|
-
| {
|
|
83
|
-
readonly connId: bigint;
|
|
84
|
-
readonly remoteAddr?: string | null;
|
|
85
|
-
readonly readyProtocols?: string[] | null;
|
|
86
|
-
}
|
|
87
|
-
| null
|
|
88
|
-
| undefined;
|
|
89
|
-
connect: (target: string | string[]) => bigint;
|
|
90
|
-
disconnect: (peerId: string) => void;
|
|
91
|
-
discoveryNowMs: () => bigint | null | undefined;
|
|
92
|
-
drainEvents: (limit: number) => unknown[];
|
|
93
|
-
isRunning: () => boolean;
|
|
94
|
-
isPeerReady: (peerId: string) => boolean;
|
|
95
|
-
knownPeers: () => unknown[];
|
|
96
|
-
listenAddrs: () => string[];
|
|
97
|
-
openStream: (
|
|
98
|
-
peerId: string,
|
|
99
|
-
protocolId: string
|
|
100
|
-
) => { readonly connId: bigint; readonly streamId: bigint };
|
|
101
|
-
path: (peerId: string) => unknown;
|
|
102
|
-
peerId: () => string;
|
|
103
|
-
peerInfo: (peerId: string) => unknown;
|
|
104
|
-
ping: (peerId: string) => void;
|
|
105
|
-
publish: (topic: string, data: Uint8Array) => void;
|
|
106
|
-
reachability: () => number;
|
|
107
|
-
resetStream: (peerId: string, connId: bigint, streamId: bigint) => void;
|
|
108
|
-
sendStream: (
|
|
109
|
-
peerId: string,
|
|
110
|
-
connId: bigint,
|
|
111
|
-
streamId: bigint,
|
|
112
|
-
data: Uint8Array
|
|
113
|
-
) => void;
|
|
114
|
-
setActive: (active: boolean) => void;
|
|
115
|
-
start: (doorbell: () => void) => void;
|
|
116
|
-
subscribe: (topic: string) => boolean;
|
|
117
|
-
unsubscribe: (topic: string) => boolean;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
interface NativeBinding {
|
|
121
|
-
readonly NodeEndpoint: new (
|
|
122
|
-
secretKey: Uint8Array,
|
|
123
|
-
config: NativeEndpointConfig
|
|
124
|
-
) => NativeEndpoint;
|
|
125
|
-
circuitAddress: (relayAddress: string, peerId: string) => string;
|
|
126
|
-
generateSecretKey: () => Uint8Array;
|
|
127
|
-
peerIdFromSecretKey: (secretKey: Uint8Array) => string;
|
|
128
|
-
}
|
|
52
|
+
/** A native endpoint instance. */
|
|
53
|
+
export type NativeEndpoint = Addon.NodeEndpoint;
|
|
129
54
|
|
|
130
55
|
function loadNativeBinding(target: string): NativeBinding {
|
|
131
56
|
if (!supportedTargets.includes(target as SupportedTarget)) {
|