rollback-netcode 0.0.4

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.
Files changed (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +140 -0
  3. package/dist/debug.d.ts +29 -0
  4. package/dist/debug.d.ts.map +1 -0
  5. package/dist/debug.js +56 -0
  6. package/dist/debug.js.map +1 -0
  7. package/dist/index.d.ts +62 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +57 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/protocol/encoding.d.ts +80 -0
  12. package/dist/protocol/encoding.d.ts.map +1 -0
  13. package/dist/protocol/encoding.js +992 -0
  14. package/dist/protocol/encoding.js.map +1 -0
  15. package/dist/protocol/messages.d.ts +271 -0
  16. package/dist/protocol/messages.d.ts.map +1 -0
  17. package/dist/protocol/messages.js +114 -0
  18. package/dist/protocol/messages.js.map +1 -0
  19. package/dist/rollback/engine.d.ts +261 -0
  20. package/dist/rollback/engine.d.ts.map +1 -0
  21. package/dist/rollback/engine.js +543 -0
  22. package/dist/rollback/engine.js.map +1 -0
  23. package/dist/rollback/input-buffer.d.ts +225 -0
  24. package/dist/rollback/input-buffer.d.ts.map +1 -0
  25. package/dist/rollback/input-buffer.js +483 -0
  26. package/dist/rollback/input-buffer.js.map +1 -0
  27. package/dist/rollback/snapshot-buffer.d.ts +119 -0
  28. package/dist/rollback/snapshot-buffer.d.ts.map +1 -0
  29. package/dist/rollback/snapshot-buffer.js +256 -0
  30. package/dist/rollback/snapshot-buffer.js.map +1 -0
  31. package/dist/session/desync-manager.d.ts +106 -0
  32. package/dist/session/desync-manager.d.ts.map +1 -0
  33. package/dist/session/desync-manager.js +136 -0
  34. package/dist/session/desync-manager.js.map +1 -0
  35. package/dist/session/lag-monitor.d.ts +69 -0
  36. package/dist/session/lag-monitor.d.ts.map +1 -0
  37. package/dist/session/lag-monitor.js +74 -0
  38. package/dist/session/lag-monitor.js.map +1 -0
  39. package/dist/session/message-builders.d.ts +86 -0
  40. package/dist/session/message-builders.d.ts.map +1 -0
  41. package/dist/session/message-builders.js +199 -0
  42. package/dist/session/message-builders.js.map +1 -0
  43. package/dist/session/message-router.d.ts +61 -0
  44. package/dist/session/message-router.d.ts.map +1 -0
  45. package/dist/session/message-router.js +105 -0
  46. package/dist/session/message-router.js.map +1 -0
  47. package/dist/session/player-manager.d.ts +100 -0
  48. package/dist/session/player-manager.d.ts.map +1 -0
  49. package/dist/session/player-manager.js +160 -0
  50. package/dist/session/player-manager.js.map +1 -0
  51. package/dist/session/session.d.ts +379 -0
  52. package/dist/session/session.d.ts.map +1 -0
  53. package/dist/session/session.js +1294 -0
  54. package/dist/session/session.js.map +1 -0
  55. package/dist/session/topology.d.ts +66 -0
  56. package/dist/session/topology.d.ts.map +1 -0
  57. package/dist/session/topology.js +72 -0
  58. package/dist/session/topology.js.map +1 -0
  59. package/dist/transport/adapter.d.ts +99 -0
  60. package/dist/transport/adapter.d.ts.map +1 -0
  61. package/dist/transport/adapter.js +8 -0
  62. package/dist/transport/adapter.js.map +1 -0
  63. package/dist/transport/local.d.ts +192 -0
  64. package/dist/transport/local.d.ts.map +1 -0
  65. package/dist/transport/local.js +435 -0
  66. package/dist/transport/local.js.map +1 -0
  67. package/dist/transport/transforming.d.ts +177 -0
  68. package/dist/transport/transforming.d.ts.map +1 -0
  69. package/dist/transport/transforming.js +407 -0
  70. package/dist/transport/transforming.js.map +1 -0
  71. package/dist/transport/webrtc.d.ts +285 -0
  72. package/dist/transport/webrtc.d.ts.map +1 -0
  73. package/dist/transport/webrtc.js +734 -0
  74. package/dist/transport/webrtc.js.map +1 -0
  75. package/dist/types.d.ts +394 -0
  76. package/dist/types.d.ts.map +1 -0
  77. package/dist/types.js +256 -0
  78. package/dist/types.js.map +1 -0
  79. package/dist/utils/rate-limiter.d.ts +59 -0
  80. package/dist/utils/rate-limiter.d.ts.map +1 -0
  81. package/dist/utils/rate-limiter.js +93 -0
  82. package/dist/utils/rate-limiter.js.map +1 -0
  83. package/package.json +61 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 someusername6
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Rollback Netcode
2
+
3
+ [![npm version](https://img.shields.io/npm/v/rollback-netcode)](https://www.npmjs.com/package/rollback-netcode)
4
+ [![license](https://img.shields.io/npm/l/rollback-netcode)](https://github.com/someusername6/rollback-netcode/blob/main/LICENSE)
5
+
6
+ > **Note:** This library is under active development. APIs may change.
7
+
8
+ A TypeScript library for P2P rollback netcode in browser-based multiplayer games. Supports 4+ players with WebRTC—no dedicated server required.
9
+
10
+ **[Live Demo](https://someusername6.github.io/rollback-netcode)** | **[API Reference](docs/api.md)** | **[FAQ](docs/faq.md)**
11
+
12
+ ## Features
13
+
14
+ - **Rollback netcode** - Simulate optimistically, rollback and resimulate on misprediction
15
+ - **N-player support** - 4+ players, not limited to 2
16
+ - **P2P networking** - WebRTC DataChannels, works in any modern browser
17
+ - **Dynamic join/leave** - Players can join or leave during gameplay
18
+ - **Desync detection** - Periodic state hashing with automatic recovery
19
+ - **Transport-agnostic** - WebRTC included, or bring your own
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install rollback-netcode
25
+ ```
26
+
27
+ ## Quick Start
28
+
29
+ ### 1. Implement the Game Interface
30
+
31
+ Your game must implement four methods:
32
+
33
+ ```typescript
34
+ import type { Game, PlayerId } from 'rollback-netcode';
35
+
36
+ class MyGame implements Game {
37
+ private state = { x: 100, y: 100 };
38
+
39
+ serialize(): Uint8Array {
40
+ const buffer = new ArrayBuffer(8);
41
+ const view = new DataView(buffer);
42
+ view.setFloat32(0, this.state.x);
43
+ view.setFloat32(4, this.state.y);
44
+ return new Uint8Array(buffer);
45
+ }
46
+
47
+ deserialize(data: Uint8Array): void {
48
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
49
+ this.state.x = view.getFloat32(0);
50
+ this.state.y = view.getFloat32(4);
51
+ }
52
+
53
+ step(inputs: Map<PlayerId, Uint8Array>): void {
54
+ for (const [, input] of inputs) {
55
+ if (input[0] & 0x01) this.state.x -= 5; // left
56
+ if (input[0] & 0x02) this.state.x += 5; // right
57
+ if (input[0] & 0x04) this.state.y -= 5; // up
58
+ if (input[0] & 0x08) this.state.y += 5; // down
59
+ }
60
+ }
61
+
62
+ hash(): number {
63
+ return Math.floor(this.state.x * 1000 + this.state.y);
64
+ }
65
+ }
66
+ ```
67
+
68
+ ### 2. Create a Session
69
+
70
+ ```typescript
71
+ import { createSession, WebRTCTransport } from 'rollback-netcode';
72
+
73
+ const transport = new WebRTCTransport('my-player-id', {
74
+ onSignal: async (peerId, signal) => {
75
+ // Send signal to peer via your signaling server
76
+ await signalingServer.send(peerId, signal);
77
+ }
78
+ });
79
+
80
+ signalingServer.onSignal((fromPeerId, signal) => {
81
+ transport.handleSignal(fromPeerId, signal);
82
+ });
83
+
84
+ const session = createSession({ game: new MyGame(), transport });
85
+
86
+ session.on('playerJoined', (player) => console.log(`${player.id} joined`));
87
+ session.on('desync', () => console.log('Desync detected, recovering...'));
88
+ ```
89
+
90
+ ### 3. Host or Join
91
+
92
+ ```typescript
93
+ // Host creates a room
94
+ const roomId = await session.createRoom();
95
+
96
+ // Others join
97
+ await session.joinRoom(roomId, hostPeerId);
98
+ ```
99
+
100
+ ### 4. Game Loop
101
+
102
+ ```typescript
103
+ session.start(); // Host only
104
+
105
+ function gameLoop() {
106
+ const input = new Uint8Array([
107
+ (keys.left ? 0x01 : 0) |
108
+ (keys.right ? 0x02 : 0) |
109
+ (keys.up ? 0x04 : 0) |
110
+ (keys.down ? 0x08 : 0)
111
+ ]);
112
+
113
+ session.tick(input);
114
+ render(game);
115
+ requestAnimationFrame(gameLoop);
116
+ }
117
+ ```
118
+
119
+ ## Examples
120
+
121
+ See the [`examples/`](examples/) directory:
122
+
123
+ - **[local-transport](examples/local-transport/)** - Browser demo using simulated network (no server needed)
124
+ - **[webrtc](examples/webrtc/)** - Real WebRTC connections with a minimal signaling server
125
+
126
+ ## Documentation
127
+
128
+ - [API Reference](docs/api.md) - Full API documentation
129
+ - [FAQ](docs/faq.md) - Common questions answered
130
+ - [Architecture](docs/architecture.md) - Technical design and components
131
+ - [Configuration Tuning](docs/configuration-tuning.md) - Performance tuning guide
132
+
133
+ ## Requirements
134
+
135
+ - Node.js >= 22.0.0 (development)
136
+ - Modern browser with WebRTC (Chrome 56+, Firefox 44+, Safari 11+, Edge 79+)
137
+
138
+ ## License
139
+
140
+ [MIT](LICENSE)
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Debug logging utilities for the rollback netcode library.
3
+ *
4
+ * When debug mode is enabled, these utilities provide detailed logging
5
+ * of rollback operations, input handling, and network events.
6
+ */
7
+ /**
8
+ * Debug logger interface.
9
+ */
10
+ export interface DebugLogger {
11
+ /** Log general information */
12
+ log: (message: string, data?: unknown) => void;
13
+ /** Log warnings */
14
+ warn: (message: string, data?: unknown) => void;
15
+ /** Log detailed trace information */
16
+ trace: (message: string, data?: unknown) => void;
17
+ }
18
+ /**
19
+ * Create a debug logger that only outputs when enabled.
20
+ *
21
+ * @param enabled - Whether debug logging is enabled
22
+ * @returns A debug logger instance
23
+ */
24
+ export declare function createDebugLogger(enabled: boolean): DebugLogger;
25
+ /**
26
+ * A no-op logger for when debugging is disabled.
27
+ */
28
+ export declare const noopLogger: DebugLogger;
29
+ //# sourceMappingURL=debug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug.d.ts","sourceRoot":"","sources":["../src/debug.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,WAAW;IAC3B,8BAA8B;IAC9B,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAE/C,mBAAmB;IACnB,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAEhD,qCAAqC;IACrC,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACjD;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,WAAW,CAgC/D;AAED;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,WAIxB,CAAC"}
package/dist/debug.js ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Debug logging utilities for the rollback netcode library.
3
+ *
4
+ * When debug mode is enabled, these utilities provide detailed logging
5
+ * of rollback operations, input handling, and network events.
6
+ */
7
+ /**
8
+ * Create a debug logger that only outputs when enabled.
9
+ *
10
+ * @param enabled - Whether debug logging is enabled
11
+ * @returns A debug logger instance
12
+ */
13
+ export function createDebugLogger(enabled) {
14
+ if (!enabled) {
15
+ return {
16
+ log: () => { },
17
+ warn: () => { },
18
+ trace: () => { },
19
+ };
20
+ }
21
+ return {
22
+ log: (message, data) => {
23
+ if (data !== undefined) {
24
+ console.log("[rollback]", message, data);
25
+ }
26
+ else {
27
+ console.log("[rollback]", message);
28
+ }
29
+ },
30
+ warn: (message, data) => {
31
+ if (data !== undefined) {
32
+ console.warn("[rollback]", message, data);
33
+ }
34
+ else {
35
+ console.warn("[rollback]", message);
36
+ }
37
+ },
38
+ trace: (message, data) => {
39
+ if (data !== undefined) {
40
+ console.log("[rollback:trace]", message, data);
41
+ }
42
+ else {
43
+ console.log("[rollback:trace]", message);
44
+ }
45
+ },
46
+ };
47
+ }
48
+ /**
49
+ * A no-op logger for when debugging is disabled.
50
+ */
51
+ export const noopLogger = {
52
+ log: () => { },
53
+ warn: () => { },
54
+ trace: () => { },
55
+ };
56
+ //# sourceMappingURL=debug.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug.js","sourceRoot":"","sources":["../src/debug.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAgB;IACjD,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,OAAO;YACN,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;YACb,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;YACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;SACf,CAAC;IACH,CAAC;IAED,OAAO;QACN,GAAG,EAAE,CAAC,OAAe,EAAE,IAAc,EAAE,EAAE;YACxC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;YAC1C,CAAC;iBAAM,CAAC;gBACP,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;YACpC,CAAC;QACF,CAAC;QACD,IAAI,EAAE,CAAC,OAAe,EAAE,IAAc,EAAE,EAAE;YACzC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;YAC3C,CAAC;iBAAM,CAAC;gBACP,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;YACrC,CAAC;QACF,CAAC;QACD,KAAK,EAAE,CAAC,OAAe,EAAE,IAAc,EAAE,EAAE;YAC1C,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;YAChD,CAAC;iBAAM,CAAC;gBACP,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;YAC1C,CAAC;QACF,CAAC;KACD,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAgB;IACtC,GAAG,EAAE,GAAG,EAAE,GAAE,CAAC;IACb,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;CACf,CAAC"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Rollback Netcode Library
3
+ *
4
+ * A TypeScript library for P2P rollback netcode in browser-based multiplayer games.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { createSession, WebRTCTransport } from 'rollback-netcode';
9
+ *
10
+ * // Game implements this interface
11
+ * const game: Game = {
12
+ * serialize: () => { ... },
13
+ * deserialize: (data) => { ... },
14
+ * step: (inputs) => { ... },
15
+ * hash: () => { ... },
16
+ * };
17
+ *
18
+ * // Create session
19
+ * const session = createSession({
20
+ * game,
21
+ * transport: new WebRTCTransport('player-1'),
22
+ * });
23
+ *
24
+ * // Host creates room
25
+ * const roomId = await session.createRoom();
26
+ *
27
+ * // Others join
28
+ * await session.joinRoom(roomId, 'host-peer-id');
29
+ *
30
+ * // Game loop
31
+ * function tick() {
32
+ * const localInput = captureInput();
33
+ * session.tick(localInput);
34
+ * render(game);
35
+ * requestAnimationFrame(tick);
36
+ * }
37
+ * ```
38
+ *
39
+ * @packageDocumentation
40
+ */
41
+ export { createSession, Session } from "./session/session.js";
42
+ export type { CreateSessionOptions } from "./session/session.js";
43
+ export { StarTopology, MeshTopology, createTopologyStrategy, } from "./session/topology.js";
44
+ export type { TopologyStrategy } from "./session/topology.js";
45
+ export { RollbackEngine } from "./rollback/engine.js";
46
+ export type { RollbackEngineConfig } from "./rollback/engine.js";
47
+ export type { ConnectionMetrics, TransportAdapter, } from "./transport/adapter.js";
48
+ export { LocalTransport, createLocalTransportGroup, } from "./transport/local.js";
49
+ export type { LocalTransportConfig } from "./transport/local.js";
50
+ export { WebRTCTransport } from "./transport/webrtc.js";
51
+ export type { WebRTCTransportConfig, SignalingCallbacks, SignalMessage, } from "./transport/webrtc.js";
52
+ export { TransformingTransport, DEFAULT_TRANSFORMING_TRANSPORT_CONFIG, } from "./transport/transforming.js";
53
+ export type { TransformingTransportConfig } from "./transport/transforming.js";
54
+ export { encodeMessage, decodeMessage, DecodeError, DEFAULT_PROTOCOL_LIMITS, } from "./protocol/encoding.js";
55
+ export type { ProtocolLimits } from "./protocol/encoding.js";
56
+ export { MessageType } from "./protocol/messages.js";
57
+ export type { Message } from "./protocol/messages.js";
58
+ export type { Game, GameOperation, PlayerId, Tick, TickResult, SessionConfig, SessionEvents, PlayerInfo, InputPredictor, Snapshot, PlayerTimeline, ErrorContext, } from "./types.js";
59
+ export { Topology, DesyncAuthority, SessionState, PlayerRole, ErrorSource, PauseReason, PlayerConnectionState, asPlayerId, asTick, validatePlayerId, validateTick, playerIdToPeerId, TICK_MIN, DEFAULT_SESSION_CONFIG, DEFAULT_INPUT_PREDICTOR, MAX_PLAYERS_LIMIT, RollbackError, ValidationError, GameError, validateSessionConfig, } from "./types.js";
60
+ export { createDebugLogger, noopLogger } from "./debug.js";
61
+ export type { DebugLogger } from "./debug.js";
62
+ //# sourceMappingURL=index.d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J,UAAU,EACV,aAAa,EACb,aAAa,EACb,UAAU,EACV,cAAc,EACd,QAAQ,EACR,cAAc,EACd,YAAY,GACZ,MAAM,YAAY,CAAC;AAGpB,OAAO,EACN,QAAQ,EACR,eAAe,EACf,YAAY,EACZ,UAAU,EACV,WAAW,EACX,WAAW,EACX,qBAAqB,EACrB,UAAU,EACV,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,EAChB,QAAQ,EACR,sBAAsB,EACtB,uBAAuB,EACvB,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,SAAS,EACT,qBAAqB,GACrB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC3D,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Rollback Netcode Library
3
+ *
4
+ * A TypeScript library for P2P rollback netcode in browser-based multiplayer games.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { createSession, WebRTCTransport } from 'rollback-netcode';
9
+ *
10
+ * // Game implements this interface
11
+ * const game: Game = {
12
+ * serialize: () => { ... },
13
+ * deserialize: (data) => { ... },
14
+ * step: (inputs) => { ... },
15
+ * hash: () => { ... },
16
+ * };
17
+ *
18
+ * // Create session
19
+ * const session = createSession({
20
+ * game,
21
+ * transport: new WebRTCTransport('player-1'),
22
+ * });
23
+ *
24
+ * // Host creates room
25
+ * const roomId = await session.createRoom();
26
+ *
27
+ * // Others join
28
+ * await session.joinRoom(roomId, 'host-peer-id');
29
+ *
30
+ * // Game loop
31
+ * function tick() {
32
+ * const localInput = captureInput();
33
+ * session.tick(localInput);
34
+ * render(game);
35
+ * requestAnimationFrame(tick);
36
+ * }
37
+ * ```
38
+ *
39
+ * @packageDocumentation
40
+ */
41
+ // Core session management
42
+ export { createSession, Session } from "./session/session.js";
43
+ // Topology strategies
44
+ export { StarTopology, MeshTopology, createTopologyStrategy, } from "./session/topology.js";
45
+ // Rollback engine (for advanced use cases)
46
+ export { RollbackEngine } from "./rollback/engine.js";
47
+ export { LocalTransport, createLocalTransportGroup, } from "./transport/local.js";
48
+ export { WebRTCTransport } from "./transport/webrtc.js";
49
+ export { TransformingTransport, DEFAULT_TRANSFORMING_TRANSPORT_CONFIG, } from "./transport/transforming.js";
50
+ // Protocol encoding (for custom transport implementations)
51
+ export { encodeMessage, decodeMessage, DecodeError, DEFAULT_PROTOCOL_LIMITS, } from "./protocol/encoding.js";
52
+ export { MessageType } from "./protocol/messages.js";
53
+ // Enums and values from types
54
+ export { Topology, DesyncAuthority, SessionState, PlayerRole, ErrorSource, PauseReason, PlayerConnectionState, asPlayerId, asTick, validatePlayerId, validateTick, playerIdToPeerId, TICK_MIN, DEFAULT_SESSION_CONFIG, DEFAULT_INPUT_PREDICTOR, MAX_PLAYERS_LIMIT, RollbackError, ValidationError, GameError, validateSessionConfig, } from "./types.js";
55
+ // Debug utilities
56
+ export { createDebugLogger, noopLogger } from "./debug.js";
57
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,0BAA0B;AAC1B,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAG9D,sBAAsB;AACtB,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,sBAAsB,GACtB,MAAM,uBAAuB,CAAC;AAG/B,2CAA2C;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAQtD,OAAO,EACN,cAAc,EACd,yBAAyB,GACzB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAMxD,OAAO,EACN,qBAAqB,EACrB,qCAAqC,GACrC,MAAM,6BAA6B,CAAC;AAGrC,2DAA2D;AAC3D,OAAO,EACN,aAAa,EACb,aAAa,EACb,WAAW,EACX,uBAAuB,GACvB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAmBrD,8BAA8B;AAC9B,OAAO,EACN,QAAQ,EACR,eAAe,EACf,YAAY,EACZ,UAAU,EACV,WAAW,EACX,WAAW,EACX,qBAAqB,EACrB,UAAU,EACV,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,EAChB,QAAQ,EACR,sBAAsB,EACtB,uBAAuB,EACvB,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,SAAS,EACT,qBAAqB,GACrB,MAAM,YAAY,CAAC;AAEpB,kBAAkB;AAClB,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Binary encoding and decoding utilities for protocol messages.
3
+ *
4
+ * Message format:
5
+ * - Byte 0: Message type
6
+ * - Bytes 1+: Message payload (varies by type)
7
+ */
8
+ import { type Message } from "./messages.js";
9
+ /**
10
+ * Error thrown when decoding a message fails.
11
+ */
12
+ export declare class DecodeError extends Error {
13
+ readonly messageType: number | undefined;
14
+ readonly offset: number;
15
+ readonly expected: number;
16
+ readonly actual: number;
17
+ constructor(message: string, messageType: number | undefined, offset: number, expected: number, actual: number);
18
+ }
19
+ /**
20
+ * Error thrown when encoding a message fails due to invalid values.
21
+ */
22
+ export declare class EncodeError extends Error {
23
+ readonly field: string;
24
+ readonly maxValue: number;
25
+ readonly actualValue: number;
26
+ constructor(message: string, field: string, maxValue: number, actualValue: number);
27
+ }
28
+ /**
29
+ * Maximum size of a single input frame in bytes.
30
+ * This prevents malicious peers from sending excessively large inputs.
31
+ * 1KB is more than sufficient for any reasonable game input.
32
+ */
33
+ export declare const MAX_INPUT_SIZE_PER_FRAME = 1024;
34
+ /**
35
+ * Maximum total size of an InputMessage in bytes.
36
+ * This prevents memory exhaustion from malicious messages.
37
+ * 64KB allows for 255 frames × ~250 bytes each with overhead.
38
+ */
39
+ export declare const MAX_INPUT_MESSAGE_SIZE = 65536;
40
+ /**
41
+ * Configurable limits for protocol decoding.
42
+ * These limits prevent DoS attacks via oversized messages.
43
+ */
44
+ export interface ProtocolLimits {
45
+ /**
46
+ * Maximum length of a string (player ID, room ID, etc.) in bytes.
47
+ * Prevents memory exhaustion from malicious large strings.
48
+ * @default 1024
49
+ */
50
+ maxStringLength: number;
51
+ /**
52
+ * Maximum number of players in a player timeline.
53
+ * Prevents memory exhaustion from malicious large player lists.
54
+ * @default 256
55
+ */
56
+ maxPlayerCount: number;
57
+ /**
58
+ * Maximum size of game state in bytes.
59
+ * Prevents memory exhaustion from malicious large state payloads.
60
+ * @default 1000000 (1MB)
61
+ */
62
+ maxStateSize: number;
63
+ }
64
+ /**
65
+ * Default protocol limits.
66
+ * These values are suitable for most games and provide DoS protection.
67
+ */
68
+ export declare const DEFAULT_PROTOCOL_LIMITS: ProtocolLimits;
69
+ /**
70
+ * Encode a message to a binary format.
71
+ */
72
+ export declare function encodeMessage(message: Message): Uint8Array;
73
+ /**
74
+ * Decode a binary message.
75
+ * @param data - The binary data to decode
76
+ * @param limits - Optional protocol limits for DoS protection (defaults to DEFAULT_PROTOCOL_LIMITS)
77
+ * @throws DecodeError if the message is malformed, truncated, or exceeds limits
78
+ */
79
+ export declare function decodeMessage(data: Uint8Array, limits?: ProtocolLimits): Message;
80
+ //# sourceMappingURL=encoding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../../src/protocol/encoding.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAUH,OAAO,EAUN,KAAK,OAAO,EAYZ,MAAM,eAAe,CAAC;AAMvB;;GAEG;AACH,qBAAa,WAAY,SAAQ,KAAK;aAGpB,WAAW,EAAE,MAAM,GAAG,SAAS;aAC/B,MAAM,EAAE,MAAM;aACd,QAAQ,EA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