bedrock-portal-nethernet 2.2.0 → 2.3.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.
@@ -1,29 +1,17 @@
1
1
  import { Connection } from './connection';
2
2
  import { Signal } from '../signaling/signal';
3
3
  import { SignalStructure } from '../signaling/struct';
4
- type PendingCandidate = {
5
- signal: SignalStructure;
6
- attempts: number;
7
- };
8
4
  export declare class Server {
9
5
  networkId: bigint;
10
6
  connectionId: bigint;
11
7
  signaling: Signal;
12
8
  connections: Map<bigint, Connection>;
13
- pendingCandidates: Map<bigint, PendingCandidate[]>;
14
- remoteDescriptionReady: Set<bigint>;
15
- pendingCandidateTimers: Map<bigint, NodeJS.Timeout>;
16
9
  onOpenConnection: (conn: Connection) => void;
17
10
  onCloseConnection: (id: bigint, reason: string) => void;
18
11
  onEncapsulated: (packet: Buffer, id: bigint) => void;
19
12
  constructor(signaling: Signal, networkId?: bigint, connectionId?: bigint);
20
13
  handleCandidate(signal: SignalStructure): Promise<void>;
21
- queueCandidate(signal: SignalStructure, reason?: string): void;
22
- schedulePendingCandidateFlush(connectionId: bigint): void;
23
- flushPendingCandidates(connectionId: bigint): void;
24
- requeueCandidate(candidate: PendingCandidate, remaining: PendingCandidate[], reason: string): void;
25
14
  handleOffer(signal: SignalStructure): Promise<void>;
26
15
  listen(): Promise<void>;
27
16
  close(): void;
28
17
  }
29
- export {};
@@ -5,8 +5,6 @@ const node_datachannel_1 = require("node-datachannel");
5
5
  const connection_1 = require("./connection");
6
6
  const struct_1 = require("../signaling/struct");
7
7
  const debugFn = require('debug')('bedrock-portal-nethernet');
8
- const MAX_PENDING_CANDIDATE_ATTEMPTS = 50;
9
- const PENDING_CANDIDATE_RETRY_MS = 100;
10
8
  const getRandomUint64 = () => {
11
9
  const high = Math.floor(Math.random() * 0xFFFFFFFF);
12
10
  const low = Math.floor(Math.random() * 0xFFFFFFFF);
@@ -17,9 +15,6 @@ class Server {
17
15
  connectionId;
18
16
  signaling;
19
17
  connections;
20
- pendingCandidates;
21
- remoteDescriptionReady;
22
- pendingCandidateTimers;
23
18
  onOpenConnection;
24
19
  onCloseConnection;
25
20
  onEncapsulated;
@@ -28,78 +23,18 @@ class Server {
28
23
  this.networkId = networkId;
29
24
  this.connectionId = connectionId;
30
25
  this.connections = new Map();
31
- this.pendingCandidates = new Map();
32
- this.remoteDescriptionReady = new Set();
33
- this.pendingCandidateTimers = new Map();
34
26
  this.onOpenConnection = () => { };
35
27
  this.onCloseConnection = () => { };
36
28
  this.onEncapsulated = () => { };
37
29
  }
38
30
  async handleCandidate(signal) {
39
31
  const conn = this.connections.get(signal.connectionId);
40
- if (!conn || !this.remoteDescriptionReady.has(signal.connectionId)) {
41
- this.queueCandidate(signal);
42
- return;
43
- }
44
- try {
32
+ if (conn) {
45
33
  conn.rtcConnection.addRemoteCandidate(signal.data, '0');
46
34
  }
47
- catch (error) {
48
- this.queueCandidate(signal, formatCandidateError(error));
49
- }
50
- }
51
- queueCandidate(signal, reason = 'connection is not ready') {
52
- const candidates = this.pendingCandidates.get(signal.connectionId) || [];
53
- candidates.push({ signal, attempts: 0 });
54
- this.pendingCandidates.set(signal.connectionId, candidates);
55
- debugFn('Queued ICE candidate', signal.connectionId, reason);
56
- this.schedulePendingCandidateFlush(signal.connectionId);
57
- }
58
- schedulePendingCandidateFlush(connectionId) {
59
- if (this.pendingCandidateTimers.has(connectionId))
60
- return;
61
- const timer = setTimeout(() => {
62
- this.pendingCandidateTimers.delete(connectionId);
63
- this.flushPendingCandidates(connectionId);
64
- }, PENDING_CANDIDATE_RETRY_MS);
65
- timer.unref?.();
66
- this.pendingCandidateTimers.set(connectionId, timer);
67
- }
68
- flushPendingCandidates(connectionId) {
69
- const candidates = this.pendingCandidates.get(connectionId);
70
- if (!candidates?.length)
71
- return;
72
- const conn = this.connections.get(connectionId);
73
- const remaining = [];
74
- for (const candidate of candidates) {
75
- if (!conn || !this.remoteDescriptionReady.has(connectionId)) {
76
- this.requeueCandidate(candidate, remaining, 'connection is not ready');
77
- continue;
78
- }
79
- try {
80
- conn.rtcConnection.addRemoteCandidate(candidate.signal.data, '0');
81
- }
82
- catch (error) {
83
- this.requeueCandidate(candidate, remaining, formatCandidateError(error));
84
- }
85
- }
86
- if (remaining.length) {
87
- this.pendingCandidates.set(connectionId, remaining);
88
- this.schedulePendingCandidateFlush(connectionId);
89
- }
90
35
  else {
91
- this.pendingCandidates.delete(connectionId);
92
- }
93
- }
94
- requeueCandidate(candidate, remaining, reason) {
95
- if (candidate.attempts >= MAX_PENDING_CANDIDATE_ATTEMPTS) {
96
- debugFn('Dropping ICE candidate after retries', candidate.signal.connectionId, reason);
97
- return;
36
+ debugFn('Received candidate for unknown connection', signal);
98
37
  }
99
- remaining.push({
100
- signal: candidate.signal,
101
- attempts: candidate.attempts + 1,
102
- });
103
38
  }
104
39
  async handleOffer(signal) {
105
40
  if (!this.signaling.credentials) {
@@ -109,7 +44,7 @@ class Server {
109
44
  const connection = new connection_1.Connection(this, signal.connectionId, rtcConnection);
110
45
  this.connections.set(signal.connectionId, connection);
111
46
  rtcConnection.onLocalCandidate(candidate => {
112
- this.signaling.write(new struct_1.SignalStructure(struct_1.SignalType.CandidateAdd, signal.connectionId, candidate, signal.networkId));
47
+ this.signaling.write(new struct_1.SignalStructure(struct_1.SignalType.CandidateAdd, signal.connectionId, candidate, signal.networkId, signal.pmsgId));
113
48
  });
114
49
  rtcConnection.onDataChannel(channel => {
115
50
  if (channel.getLabel() === 'ReliableDataChannel')
@@ -120,34 +55,25 @@ class Server {
120
55
  rtcConnection.onIceStateChange(state => {
121
56
  if (state === 'connected')
122
57
  this.onOpenConnection(connection);
123
- if (state === 'disconnected') {
124
- this.remoteDescriptionReady.delete(signal.connectionId);
125
- this.pendingCandidates.delete(signal.connectionId);
58
+ if (state === 'disconnected')
126
59
  this.onCloseConnection(signal.connectionId, 'disconnected');
127
- }
128
60
  });
129
61
  rtcConnection.setRemoteDescription(signal.data, 'offer');
130
- this.remoteDescriptionReady.add(signal.connectionId);
131
- this.flushPendingCandidates(signal.connectionId);
132
62
  const answer = rtcConnection.localDescription();
133
63
  if (!answer) {
134
64
  throw new Error('No answer');
135
65
  }
136
- this.signaling.write(new struct_1.SignalStructure(struct_1.SignalType.ConnectResponse, signal.connectionId, answer.sdp, signal.networkId));
66
+ this.signaling.write(new struct_1.SignalStructure(struct_1.SignalType.ConnectResponse, signal.connectionId, answer.sdp, signal.networkId, signal.pmsgId));
137
67
  }
138
68
  async listen() {
139
69
  await this.signaling.connect();
140
70
  this.signaling.on('signal', (signal) => {
141
71
  switch (signal.type) {
142
72
  case struct_1.SignalType.ConnectRequest:
143
- this.handleOffer(signal).catch(error => {
144
- debugFn('Failed to handle connect offer', signal, error);
145
- });
73
+ this.handleOffer(signal);
146
74
  break;
147
75
  case struct_1.SignalType.CandidateAdd:
148
- this.handleCandidate(signal).catch(error => {
149
- debugFn('Failed to handle ICE candidate', signal, error);
150
- });
76
+ this.handleCandidate(signal);
151
77
  break;
152
78
  default:
153
79
  debugFn('Received signal for unknown type', signal);
@@ -155,18 +81,9 @@ class Server {
155
81
  });
156
82
  }
157
83
  close() {
158
- for (const timer of this.pendingCandidateTimers.values()) {
159
- clearTimeout(timer);
160
- }
161
- this.pendingCandidateTimers.clear();
162
- this.pendingCandidates.clear();
163
- this.remoteDescriptionReady.clear();
164
84
  for (const conn of this.connections.values()) {
165
85
  conn.close();
166
86
  }
167
87
  }
168
88
  }
169
89
  exports.Server = Server;
170
- function formatCandidateError(error) {
171
- return error instanceof Error ? error.message : String(error);
172
- }
package/dist/options.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { CompressionAlgorithm } from './transforms/framer';
2
2
  export type Options = {
3
3
  version: string;
4
- signalingMode: 'rpc' | 'legacy';
5
4
  autoInitPlayer: boolean;
6
5
  offline: boolean;
7
6
  connectTimeout: number;
@@ -11,4 +10,13 @@ export type Options = {
11
10
  protocolVersion: number;
12
11
  };
13
12
  export declare const CURRENT_VERSION = "1.26.20";
14
- export declare const defaultOptions: Options;
13
+ export declare const defaultOptions: {
14
+ version: string;
15
+ autoInitPlayer: boolean;
16
+ offline: boolean;
17
+ connectTimeout: number;
18
+ compressionAlgorithm: CompressionAlgorithm;
19
+ compressionLevel: number;
20
+ compressionThreshold: number;
21
+ protocolVersion: number;
22
+ };
package/dist/options.js CHANGED
@@ -7,7 +7,6 @@ exports.CURRENT_VERSION = '1.26.20';
7
7
  exports.defaultOptions = {
8
8
  // https://minecraft.wiki/w/Protocol_version#Bedrock_Edition_2
9
9
  version: exports.CURRENT_VERSION,
10
- signalingMode: 'rpc',
11
10
  // client: If we should send SetPlayerInitialized to the server after getting play_status spawn.
12
11
  // if this is disabled, no 'spawn' event will be emitted, you should manually set
13
12
  // client.status to ClientStatus.Initialized after sending the init packet.
@@ -241,6 +241,37 @@ declare const _default: {
241
241
  })[];
242
242
  name?: undefined;
243
243
  })[])[];
244
+ ItemNew: (string | ({
245
+ name: string;
246
+ type: string;
247
+ } | {
248
+ name: string;
249
+ type: (string | {
250
+ compareTo: string;
251
+ fields: {
252
+ true: (string | {
253
+ name: string;
254
+ type: string;
255
+ }[])[];
256
+ };
257
+ default: string;
258
+ })[];
259
+ } | {
260
+ name: string;
261
+ type: (string | {
262
+ compareTo: string;
263
+ fields: {
264
+ '/ShieldItemID': (string | {
265
+ lengthType: string;
266
+ type: string;
267
+ })[];
268
+ };
269
+ default: (string | {
270
+ lengthType: string;
271
+ type: string;
272
+ })[];
273
+ })[];
274
+ })[])[];
244
275
  Item: (string | ({
245
276
  name: string;
246
277
  type: string;
@@ -459,6 +490,8 @@ declare const _default: {
459
490
  139: string;
460
491
  140: string;
461
492
  141: string;
493
+ 142: string;
494
+ 143: string;
462
495
  };
463
496
  })[];
464
497
  } | {
@@ -2258,6 +2291,8 @@ declare const _default: {
2258
2291
  596: string;
2259
2292
  597: string;
2260
2293
  598: string;
2294
+ 599: string;
2295
+ 600: string;
2261
2296
  };
2262
2297
  })[];
2263
2298
  LegacyEntityType: (string | {
@@ -2775,6 +2810,10 @@ declare const _default: {
2775
2810
  130: string;
2776
2811
  131: string;
2777
2812
  132: string;
2813
+ 133: string;
2814
+ 134: string;
2815
+ 135: string;
2816
+ 136: string;
2778
2817
  };
2779
2818
  })[];
2780
2819
  FullContainerName: (string | ({
@@ -3024,6 +3063,9 @@ declare const _default: {
3024
3063
  46: string;
3025
3064
  47: string;
3026
3065
  48: string;
3066
+ 49: string;
3067
+ 50: string;
3068
+ 51: string;
3027
3069
  };
3028
3070
  })[];
3029
3071
  DebugMarkerData: (string | {
@@ -3250,6 +3292,76 @@ declare const _default: {
3250
3292
  type: string;
3251
3293
  })[];
3252
3294
  })[])[];
3295
+ EntityDiagnosticTimingInfo: (string | {
3296
+ name: string;
3297
+ type: string;
3298
+ }[])[];
3299
+ SystemDiagnosticTimingInfo: (string | {
3300
+ name: string;
3301
+ type: string;
3302
+ }[])[];
3303
+ PrimitiveShape: (string | ({
3304
+ name: string;
3305
+ type: string;
3306
+ } | {
3307
+ name: string;
3308
+ type: (string | (string | {
3309
+ type: string;
3310
+ mappings: {
3311
+ 0: string;
3312
+ 1: string;
3313
+ 2: string;
3314
+ 3: string;
3315
+ 4: string;
3316
+ 5: string;
3317
+ };
3318
+ })[])[];
3319
+ } | {
3320
+ name: string;
3321
+ type: (string | {
3322
+ compareTo: string;
3323
+ fields: {
3324
+ line: string;
3325
+ box: string;
3326
+ sphere: string;
3327
+ circle: string;
3328
+ text: string;
3329
+ arrow: string;
3330
+ };
3331
+ default: string;
3332
+ })[];
3333
+ })[])[];
3334
+ ShapeLine: (string | {
3335
+ name: string;
3336
+ type: string;
3337
+ }[])[];
3338
+ ShapeBox: (string | {
3339
+ name: string;
3340
+ type: string;
3341
+ }[])[];
3342
+ ShapeSphere: (string | {
3343
+ name: string;
3344
+ type: string[];
3345
+ }[])[];
3346
+ ShapeCircle: (string | {
3347
+ name: string;
3348
+ type: string[];
3349
+ }[])[];
3350
+ ShapeText: (string | ({
3351
+ name: string;
3352
+ type: string;
3353
+ } | {
3354
+ name: string;
3355
+ type: string[];
3356
+ })[])[];
3357
+ ShapeArrow: (string | {
3358
+ name: string;
3359
+ type: string;
3360
+ }[])[];
3361
+ PartyInfo: (string | {
3362
+ name: string;
3363
+ type: string;
3364
+ }[])[];
3253
3365
  mcpe_packet: (string | ({
3254
3366
  name: string;
3255
3367
  type: (string | {
@@ -3494,6 +3606,8 @@ declare const _default: {
3494
3606
  343: string;
3495
3607
  344: string;
3496
3608
  345: string;
3609
+ 346: string;
3610
+ 347: string;
3497
3611
  };
3498
3612
  })[];
3499
3613
  } | {
@@ -3658,7 +3772,7 @@ declare const _default: {
3658
3772
  correct_player_move_prediction: string;
3659
3773
  item_registry: string;
3660
3774
  filter_text_packet: string;
3661
- debug_renderer: string;
3775
+ primitive_shapes: string;
3662
3776
  sync_entity_property: string;
3663
3777
  add_volume_entity: string;
3664
3778
  remove_volume_entity: string;
@@ -3740,6 +3854,8 @@ declare const _default: {
3740
3854
  serverbound_data_driven_screen_closed: string;
3741
3855
  sync_world_clocks: string;
3742
3856
  clientbound_attribute_layer_sync: string;
3857
+ server_store_info: string;
3858
+ server_presence_info: string;
3743
3859
  };
3744
3860
  })[];
3745
3861
  })[])[];
@@ -4382,6 +4498,8 @@ declare const _default: {
4382
4498
  77: string;
4383
4499
  78: string;
4384
4500
  79: string;
4501
+ 80: string;
4502
+ 81: string;
4385
4503
  };
4386
4504
  })[];
4387
4505
  })[])[];
@@ -4528,10 +4646,13 @@ declare const _default: {
4528
4646
  name: string;
4529
4647
  type: string;
4530
4648
  }[])[];
4531
- packet_inventory_slot: (string | {
4649
+ packet_inventory_slot: (string | ({
4532
4650
  name: string;
4533
4651
  type: string;
4534
- }[])[];
4652
+ } | {
4653
+ name: string;
4654
+ type: string[];
4655
+ })[])[];
4535
4656
  packet_container_set_data: (string | {
4536
4657
  name: string;
4537
4658
  type: string;
@@ -4924,7 +5045,7 @@ declare const _default: {
4924
5045
  58: string;
4925
5046
  62: string;
4926
5047
  71: string;
4927
- 74: string;
5048
+ 75: string;
4928
5049
  };
4929
5050
  })[];
4930
5051
  } | {
@@ -5076,10 +5197,13 @@ declare const _default: {
5076
5197
  name: string;
5077
5198
  type: string;
5078
5199
  }[])[];
5079
- packet_play_sound: (string | {
5200
+ packet_play_sound: (string | ({
5080
5201
  name: string;
5081
5202
  type: string;
5082
- }[])[];
5203
+ } | {
5204
+ name: string;
5205
+ type: string[];
5206
+ })[])[];
5083
5207
  packet_stop_sound: (string | {
5084
5208
  name: string;
5085
5209
  type: string;
@@ -5482,10 +5606,13 @@ declare const _default: {
5482
5606
  type: string;
5483
5607
  })[];
5484
5608
  }[])[];
5485
- packet_level_sound_event: (string | {
5609
+ packet_level_sound_event: (string | ({
5486
5610
  name: string;
5487
5611
  type: string;
5488
- }[])[];
5612
+ } | {
5613
+ name: string;
5614
+ type: string[];
5615
+ })[])[];
5489
5616
  packet_level_event_generic: (string | {
5490
5617
  name: string;
5491
5618
  type: string;
@@ -5991,18 +6118,13 @@ declare const _default: {
5991
6118
  name: string;
5992
6119
  type: string;
5993
6120
  }[])[];
5994
- packet_debug_renderer: (string | ({
5995
- name: string;
5996
- type: string;
5997
- } | {
6121
+ packet_primitive_shapes: (string | {
5998
6122
  name: string;
5999
6123
  type: (string | {
6000
- compareTo: string;
6001
- fields: {
6002
- true: string;
6003
- };
6124
+ countType: string;
6125
+ type: string;
6004
6126
  })[];
6005
- })[])[];
6127
+ }[])[];
6006
6128
  packet_sync_entity_property: (string | {
6007
6129
  name: string;
6008
6130
  type: string;
@@ -6962,7 +7084,7 @@ declare const _default: {
6962
7084
  }[])[];
6963
7085
  packet_party_changed: (string | {
6964
7086
  name: string;
6965
- type: string;
7087
+ type: string[];
6966
7088
  }[])[];
6967
7089
  packet_serverbound_data_driven_screen_closed: (string | {
6968
7090
  name: string;
@@ -7084,6 +7206,14 @@ declare const _default: {
7084
7206
  })[];
7085
7207
  name?: undefined;
7086
7208
  })[])[];
7209
+ packet_server_store_info: (string | {
7210
+ name: string;
7211
+ type: string[];
7212
+ }[])[];
7213
+ packet_server_presence_info: (string | {
7214
+ name: string;
7215
+ type: string[];
7216
+ }[])[];
7087
7217
  };
7088
7218
  };
7089
7219
  export default _default;