1+ /// <reference lib="dom" />
12import EventEmitter from 'node:events'
23import { RemoteInfo , Socket } from 'node:dgram'
3- import { PeerConnection , DataChannel , IceServer } from 'node-datachannel'
44
55declare module 'nethernet' {
66
7+ // wrtc implements the standard WebRTC API. Use its standard interfaces without
8+ // importing wrtc's upstream declarations, which currently fail strict checking.
9+ export interface RTCPeerConnectionLike extends RTCPeerConnection { }
10+
11+ export interface RTCDataChannelLike extends RTCDataChannel { }
12+
13+ export interface IceServer {
14+ urls : string | string [ ]
15+ username ?: string
16+ credential ?: string
17+ }
18+
719 export interface ResponsePacket {
820 binary : number [ ]
921 buffer : Buffer
@@ -18,24 +30,29 @@ declare module 'nethernet' {
1830 export class Connection {
1931 nethernet : Client | Server ;
2032 address : bigint ;
21- rtcConnection : PeerConnection ;
22- reliable : DataChannel | null ;
23- unreliable : DataChannel | null ;
33+ rtcConnection : RTCPeerConnectionLike ;
34+ reliable : RTCDataChannelLike | null ;
35+ unreliable : RTCDataChannelLike | null ;
2436 promisedSegments : number ;
2537 buf : Buffer | null ;
26- sendQueue : Buffer [ ] ;
38+ sendQueue : Buffer [ ] ;
2739
28- constructor ( nethernet : Client | Server , address : bigint , rtcConnection : PeerConnection ) ;
29- setChannels ( reliable ?: DataChannel | null , unreliable ?: DataChannel | null ) : void ;
40+ closed : boolean ;
41+
42+ constructor ( nethernet : Client | Server , address : bigint , rtcConnection : RTCPeerConnectionLike ) ;
43+ setChannels ( reliable ?: RTCDataChannelLike | null , unreliable ?: RTCDataChannelLike | null ) : void ;
3044 handleMessage ( data : Buffer | string | ArrayBuffer ) : void ;
3145 send ( data : Buffer | string ) : number ;
3246 sendNow ( data : Buffer ) : number ;
3347 flushQueue ( ) : void ;
34- close ( ) : void ;
48+ close ( reason ?: string ) : void ;
3549 }
3650
3751 export interface ServerOptions {
3852 networkId ?: bigint ;
53+ credentials ?: ( string | IceServer ) [ ] ;
54+ iceServers ?: ( string | IceServer ) [ ] ;
55+ acceptTimeoutMs ?: number ;
3956 }
4057
4158 export interface ServerEvents {
@@ -47,15 +64,18 @@ declare module 'nethernet' {
4764
4865 export class Server extends EventEmitter {
4966 options : ServerOptions ;
67+ credentials : IceServer [ ] ;
68+ acceptTimeoutMs : number ;
5069 networkId : bigint ;
5170 connections : Map < bigint , Connection > ;
71+ acceptTimeouts : Map < bigint , NodeJS . Timeout > ;
5272 advertisement ?: Buffer ;
5373 socket : Socket ;
5474 serializer : any ;
5575 deserializer : any ;
5676
5777 constructor ( options ?: ServerOptions ) ;
58- handleCandidate ( signal : SignalStructure ) : Promise < void > ;
78+ handleCandidate ( signal : SignalStructure , respond ?: ( signal : SignalStructure ) => void ) : Promise < void > ;
5979 handleOffer ( signal : SignalStructure , respond : ( signal : SignalStructure ) => void , credentials ?: ( string | IceServer ) [ ] ) : Promise < void > ;
6080 processPacket ( buffer : Buffer , rinfo : RemoteInfo ) : void ;
6181 setAdvertisement ( buffer : Buffer ) : void ;
@@ -73,7 +93,17 @@ declare module 'nethernet' {
7393 disconnect : ( connectionId : bigint , reason : string ) => void ;
7494 encapsulated : ( data : Buffer , connectionId : bigint ) => void ;
7595 pong : ( packet : any ) => void ;
76- }
96+ error : ( error : Error ) => void ;
97+ }
98+
99+ export interface ClientOptions {
100+ networkId ?: bigint ;
101+ connectionId ?: bigint ;
102+ credentials ?: ( string | IceServer ) [ ] ;
103+ iceServers ?: ( string | IceServer ) [ ] ;
104+ responseTimeoutMs ?: number ;
105+ inactivityTimeoutMs ?: number ;
106+ }
77107
78108 export class Client extends EventEmitter {
79109 serverNetworkId : bigint ;
@@ -85,14 +115,17 @@ declare module 'nethernet' {
85115 deserializer : any ;
86116 responses : Map < bigint , any > ;
87117 addresses : Map < bigint , RemoteInfo > ;
88- credentials : ( string | IceServer ) [ ] ;
118+ credentials : IceServer [ ] ;
119+ responseTimeoutMs : number ;
120+ inactivityTimeoutMs : number ;
89121 signalHandler : ( signal : SignalStructure ) => void ;
90- connection ?: Connection ;
91- rtcConnection ?: PeerConnection ;
122+ connection ?: Connection | null ;
123+ rtcConnection ?: RTCPeerConnectionLike | null ;
92124 pingInterval ?: NodeJS . Timeout ;
93125 running : boolean ;
94-
126+
95127 constructor ( networkId : bigint , broadcastAddress ?: string ) ;
128+ constructor ( networkId : bigint , broadcastAddress : string | undefined , options : ClientOptions ) ;
96129 handleCandidate ( signal : SignalStructure ) : Promise < void > ;
97130 handleAnswer ( signal : SignalStructure ) : Promise < void > ;
98131 createOffer ( ) : Promise < void > ;
@@ -102,7 +135,7 @@ declare module 'nethernet' {
102135 handleSignal ( signal : SignalStructure ) : void ;
103136 sendDiscoveryRequest ( ) : void ;
104137 sendDiscoveryMessage ( signal : SignalStructure ) : void ;
105- connect ( ) : Promise < void > ;
138+ connect ( ) : void ;
106139 send ( buffer : Buffer ) : void ;
107140 ping ( ) : void ;
108141 close ( reason ?: string ) : void ;
@@ -118,6 +151,36 @@ declare module 'nethernet' {
118151 ConnectError = 'CONNECTERROR'
119152 }
120153
154+ export enum ErrorCode {
155+ None = 0 ,
156+ DestinationNotLoggedIn = 1 ,
157+ NegotiationTimeout = 2 ,
158+ WrongTransportVersion = 3 ,
159+ FailedToCreatePeerConnection = 4 ,
160+ ICE = 5 ,
161+ ConnectRequest = 6 ,
162+ ConnectResponse = 7 ,
163+ CandidateAdd = 8 ,
164+ InactivityTimeout = 9 ,
165+ FailedToCreateOffer = 10 ,
166+ FailedToCreateAnswer = 11 ,
167+ FailedToSetLocalDescription = 12 ,
168+ FailedToSetRemoteDescription = 13 ,
169+ NegotiationTimeoutWaitingForResponse = 14 ,
170+ NegotiationTimeoutWaitingForAccept = 15 ,
171+ IncomingConnectionIgnored = 16 ,
172+ SignalingParsingFailure = 17 ,
173+ SignalingUnknownError = 18 ,
174+ SignalingUnicastMessageDeliveryFailed = 19 ,
175+ SignalingBroadcastDeliveryFailed = 20 ,
176+ SignalingMessageDeliveryFailed = 21 ,
177+ SignalingTurnAuthFailed = 22 ,
178+ SignalingFallbackToBestEffortDelivery = 23 ,
179+ NoSignalingChannel = 24 ,
180+ NotLoggedIn = 25 ,
181+ SignalingFailedToSend = 26
182+ }
183+
121184 export class SignalStructure {
122185 type : SignalType ;
123186 connectionId : bigint ;
0 commit comments