@margoengine/netcode 0.0.0-stage → 1.0.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.
Files changed (66) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1387 -2
  4. package/dist/client.d.ts +167 -0
  5. package/dist/clock.d.ts +29 -0
  6. package/dist/extrapolate.d.ts +42 -0
  7. package/dist/extrapolate.js +25 -0
  8. package/dist/game.d.ts +68 -0
  9. package/dist/generate-rust.d.ts +14 -0
  10. package/dist/generate-rust.js +184 -0
  11. package/dist/generate.d.ts +25 -0
  12. package/dist/generate.js +156 -0
  13. package/dist/hitboxes.d.ts +74 -0
  14. package/dist/hitboxes.js +124 -0
  15. package/dist/index.d.ts +9 -0
  16. package/dist/index.js +457 -0
  17. package/dist/interest.d.ts +101 -0
  18. package/dist/interest.js +166 -0
  19. package/dist/interpolation-delay.d.ts +114 -0
  20. package/dist/interpolation-delay.js +79 -0
  21. package/dist/interpolation-fields.d.ts +117 -0
  22. package/dist/interpolation-fields.js +147 -0
  23. package/dist/interpolation-timeline.d.ts +100 -0
  24. package/dist/interpolation-timeline.js +122 -0
  25. package/dist/interpolation.d.ts +39 -0
  26. package/dist/interpolation.js +61 -0
  27. package/dist/lag.d.ts +62 -0
  28. package/dist/lobby.d.ts +77 -0
  29. package/dist/lobby.js +96 -0
  30. package/dist/loopback.d.ts +92 -0
  31. package/dist/media.d.ts +215 -0
  32. package/dist/media.js +305 -0
  33. package/dist/migrate.d.ts +113 -0
  34. package/dist/migrate.js +289 -0
  35. package/dist/node.d.ts +101 -0
  36. package/dist/node.js +166 -0
  37. package/dist/physics.d.ts +133 -0
  38. package/dist/physics.js +169 -0
  39. package/dist/presence.d.ts +156 -0
  40. package/dist/presence.js +226 -0
  41. package/dist/protocol-asi_qndb.js +122 -0
  42. package/dist/protocol.d.ts +75 -0
  43. package/dist/relay.d.ts +152 -0
  44. package/dist/relay.js +235 -0
  45. package/dist/rollback.d.ts +131 -0
  46. package/dist/rollback.js +194 -0
  47. package/dist/rooms.d.ts +366 -0
  48. package/dist/rooms.js +479 -0
  49. package/dist/schema-CMfqK4Gu.js +616 -0
  50. package/dist/schema.d.ts +263 -0
  51. package/dist/server-DAVzHcWW.js +203 -0
  52. package/dist/server.d.ts +147 -0
  53. package/dist/shared/bytes.d.ts +38 -0
  54. package/dist/signal.d.ts +156 -0
  55. package/dist/signal.js +261 -0
  56. package/dist/smoothing.d.ts +76 -0
  57. package/dist/smoothing.js +155 -0
  58. package/dist/transport-DeQodwyv.js +49 -0
  59. package/dist/transport.d.ts +76 -0
  60. package/dist/webrtc.d.ts +32 -0
  61. package/dist/webrtc.js +64 -0
  62. package/dist/websocket.d.ts +27 -0
  63. package/dist/websocket.js +42 -0
  64. package/dist/webtransport.d.ts +83 -0
  65. package/dist/webtransport.js +120 -0
  66. package/package.json +138 -4
@@ -0,0 +1,76 @@
1
+ import { MargoError } from '@margoengine/math/errors';
2
+ /**
3
+ * How a message travels. `reliable`: arrives once, in order (retransmitted under loss). `unreliable`: may be lost,
4
+ * duplicated or reordered, never delayed by an earlier lost message. A transport without an unreliable path (a
5
+ * WebSocket) sends both kinds reliably.
6
+ */
7
+ export type Delivery = 'reliable' | 'unreliable';
8
+ /** Close codes the netcode uses, in the WebSocket application range (4000 to 4999) plus 1000 for a normal close. */
9
+ export declare const CloseCode: {
10
+ /** Closed on purpose. */
11
+ readonly normal: 1000;
12
+ /** The peer sent bytes that do not decode, or a message out of turn. */
13
+ readonly protocol: 4001;
14
+ /** The peer sent more messages or bytes than the server's limits allow. */
15
+ readonly rateLimit: 4002;
16
+ /** The two ends run different protocol versions or schemas. */
17
+ readonly mismatch: 4003;
18
+ /** The server holds its maximum number of players. */
19
+ readonly full: 4004;
20
+ /** The server removed the player (kick, or it shut down). */
21
+ readonly removed: 4005;
22
+ /** The peer stopped answering. */
23
+ readonly timeout: 4006;
24
+ /** The server failed while handling this connection (a room hook or message handler threw). */
25
+ readonly error: 4007;
26
+ };
27
+ /**
28
+ * @internal `reason` cut to at most `maxBytes` UTF-8 bytes on a character boundary: a WebSocket close frame holds 125
29
+ * bytes, 2 of them the code, so a reason gets 123.
30
+ */
31
+ export declare function closeReason(reason: string, maxBytes?: number): string;
32
+ /**
33
+ * One end of a two-way message connection. GameServer and GameClient set `onmessage` and `onclose`; anything else
34
+ * that implements this shape is a transport.
35
+ */
36
+ export interface Connection {
37
+ /** False once either end closed. */
38
+ readonly open: boolean;
39
+ /** Sends one message; does nothing once closed. `channel` defaults to `'reliable'`. */
40
+ send(data: Uint8Array, channel?: Delivery): void;
41
+ /** Closes both ends; the other end's `onclose` gets `code` and `reason`. Closing twice does nothing. */
42
+ close(code?: number, reason?: string): void;
43
+ /** Called for each message that arrives. */
44
+ onmessage: ((data: Uint8Array, channel: Delivery) => void) | null;
45
+ /** Called once when the connection closes, from either end. */
46
+ onclose: ((code: number, reason: string) => void) | null;
47
+ }
48
+ /** A server end that accepts connections. */
49
+ export interface Listener {
50
+ /** Called for each new connection. */
51
+ onconnection: ((connection: Connection) => void) | null;
52
+ /** Stops accepting connections (open ones stay open). */
53
+ close(): void;
54
+ }
55
+ /** One message from {@link messages}. */
56
+ export interface ConnectionMessage {
57
+ /** The message bytes. */
58
+ data: Uint8Array;
59
+ /** The channel it came on. */
60
+ channel: Delivery;
61
+ }
62
+ /**
63
+ * The messages of `connection` as an async iterator, for code that reads a connection directly (GameServer and
64
+ * GameClient do not need it). The iterator ends when the connection closes or `signal` aborts; it takes over
65
+ * `onmessage` and `onclose`.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * for await (const { data } of messages(connection, { signal })) console.log(data.length);
70
+ * ```
71
+ */
72
+ export declare function messages(connection: Connection, { signal }?: {
73
+ signal?: AbortSignal;
74
+ }): AsyncGenerator<ConnectionMessage, void, undefined>;
75
+ /** A coded MargoError. */
76
+ export declare const fault: (code: 'state' | 'failed' | 'disposed' | 'unsupported', message: string) => MargoError;
@@ -0,0 +1,32 @@
1
+ import { type Connection } from './transport.js';
2
+ /** The two data channels of one peer connection, as {@link createDataChannels} makes them. */
3
+ export interface DataChannels {
4
+ /** Ordered, reliable. */
5
+ reliable: RTCDataChannel;
6
+ /** Unordered, `maxRetransmits: 0`. Optional: without it unreliable messages go on the reliable channel. */
7
+ unreliable?: RTCDataChannel;
8
+ }
9
+ /**
10
+ * Creates the two netcode data channels on `peer`, pre-negotiated (ids 0 and 1, `negotiated: true`), so both sides
11
+ * call this with the same arguments and no extra signalling is needed for them.
12
+ */
13
+ export declare function createDataChannels(peer: RTCPeerConnection, { ids }?: {
14
+ ids?: [number, number];
15
+ }): DataChannels;
16
+ /**
17
+ * A {@link Connection} over data channels that are open (or opening: messages sent before then are dropped, so wait
18
+ * for the reliable channel's `open` event first). Closing it closes both channels; the far end sees a normal close.
19
+ * The close code and reason are not carried over WebRTC; send a game message first if the other side needs them.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const peer = new RTCPeerConnection();
24
+ * const channels = createDataChannels(peer);
25
+ * // ... exchange offer, answer and ICE candidates through your signalling ...
26
+ * await new Promise((open) => channels.reliable.addEventListener('open', open, { once: true }));
27
+ * const connection = dataChannelConnection(channels);
28
+ * ```
29
+ */
30
+ export declare function dataChannelConnection(channels: DataChannels, { dropUnreliableAbove }?: {
31
+ dropUnreliableAbove?: number;
32
+ }): Connection;
package/dist/webrtc.js ADDED
@@ -0,0 +1,64 @@
1
+ import { t as e } from "./transport-DeQodwyv.js";
2
+ //#region src/webrtc.ts
3
+ function t(e, { ids: t = [0, 1] } = {}) {
4
+ return {
5
+ reliable: e.createDataChannel("netcode-reliable", {
6
+ negotiated: !0,
7
+ id: t[0],
8
+ ordered: !0
9
+ }),
10
+ unreliable: e.createDataChannel("netcode-unreliable", {
11
+ negotiated: !0,
12
+ id: t[1],
13
+ ordered: !1,
14
+ maxRetransmits: 0
15
+ })
16
+ };
17
+ }
18
+ var n = class {
19
+ constructor(t, n) {
20
+ this.channels = t, this.dropAbove = n, this.onmessage = null, this.onclose = null, this.closed = !1, this.listening = [];
21
+ for (let [n, r] of [["reliable", t.reliable], ["unreliable", t.unreliable]]) {
22
+ if (!r) continue;
23
+ r.binaryType = "arraybuffer";
24
+ let t = (t) => {
25
+ if (this.closed) return;
26
+ let r = t.data;
27
+ if (!(r instanceof ArrayBuffer)) return this.close(e.protocol, "text message");
28
+ this.onmessage?.(new Uint8Array(r), n);
29
+ }, i = () => this.close(e.normal, "data channel closed");
30
+ r.addEventListener("message", t), r.addEventListener("close", i), this.listening.push([
31
+ r,
32
+ "message",
33
+ t
34
+ ], [
35
+ r,
36
+ "close",
37
+ i
38
+ ]);
39
+ }
40
+ }
41
+ get open() {
42
+ return !this.closed && this.channels.reliable.readyState === "open";
43
+ }
44
+ send(e, t = "reliable") {
45
+ if (!this.open) return;
46
+ let n = this.channels.unreliable;
47
+ t === "unreliable" && n?.readyState === "open" ? n.bufferedAmount <= this.dropAbove && n.send(e) : this.channels.reliable.send(e);
48
+ }
49
+ close(t = e.normal, n = "") {
50
+ this.closed || (this.release(), this.channels.unreliable?.close(), this.channels.reliable.close(), this.finish(t, n));
51
+ }
52
+ finish(e, t) {
53
+ this.closed || (this.closed = !0, this.release(), this.onclose?.(e, t));
54
+ }
55
+ release() {
56
+ for (let [e, t, n] of this.listening.splice(0)) e.removeEventListener(t, n);
57
+ }
58
+ };
59
+ function r(e, { dropUnreliableAbove: t = 65536 } = {}) {
60
+ if (!e?.reliable || typeof e.reliable.send != "function") throw TypeError("dataChannelConnection needs { reliable, unreliable? } RTCDataChannels; pass createDataChannels(peer)");
61
+ return new n(e, t);
62
+ }
63
+ //#endregion
64
+ export { t as createDataChannels, r as dataChannelConnection };
@@ -0,0 +1,27 @@
1
+ import { type Connection } from './transport.js';
2
+ /** Options of {@link openWebSocket}. */
3
+ export interface OpenWebSocketOptions {
4
+ /** Subprotocols to offer (default none). */
5
+ protocols?: string | string[];
6
+ /** Aborts the connect (the promise rejects with the signal's reason). */
7
+ signal?: AbortSignal;
8
+ /**
9
+ * Bytes buffered in the socket above which 'unreliable' messages are dropped instead of queued (default 64 KiB):
10
+ * a stale snapshot queued behind others is worse than none.
11
+ */
12
+ dropUnreliableAbove?: number;
13
+ /** The WebSocket class to use (default the global one). */
14
+ WebSocket?: typeof WebSocket;
15
+ }
16
+ /**
17
+ * Opens a WebSocket to `url` and resolves with a {@link Connection} once it is open.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * const connection = await openWebSocket('wss://game.example.com/play');
22
+ * const client = await GameClient.join(game, connection, { input: readKeys });
23
+ * ```
24
+ * @throws MargoError `unsupported` (as a rejection) when there is no WebSocket class; `failed` when the socket closes
25
+ * before it opens; the signal's reason when `signal` aborts.
26
+ */
27
+ export declare function openWebSocket(url: string | URL, options?: OpenWebSocketOptions): Promise<Connection>;
@@ -0,0 +1,42 @@
1
+ import { n as e, r as t, t as n } from "./transport-DeQodwyv.js";
2
+ //#region src/websocket.ts
3
+ var r = class {
4
+ constructor(e, t) {
5
+ this.socket = e, this.dropAbove = t, this.onmessage = null, this.onclose = null, this.closed = !1, this.message = (e) => {
6
+ this.closed || (e.data instanceof ArrayBuffer ? this.onmessage?.(new Uint8Array(e.data), "reliable") : this.close(n.protocol, "text frame"));
7
+ }, this.closing = (e) => this.finish(e.code, e.reason), e.binaryType = "arraybuffer", e.addEventListener("message", this.message), e.addEventListener("close", this.closing);
8
+ }
9
+ get open() {
10
+ return !this.closed && this.socket.readyState === 1;
11
+ }
12
+ send(e, t = "reliable") {
13
+ this.open && (t === "unreliable" && this.socket.bufferedAmount > this.dropAbove || this.socket.send(e));
14
+ }
15
+ close(t = n.normal, r = "") {
16
+ this.closed || (this.socket.close(t === 1e3 || t >= 3e3 && t <= 4999 ? t : 1e3, e(r)), this.finish(t, r));
17
+ }
18
+ finish(e, t) {
19
+ this.closed || (this.closed = !0, this.socket.removeEventListener("message", this.message), this.socket.removeEventListener("close", this.closing), this.onclose?.(e, t));
20
+ }
21
+ };
22
+ function i(e, n = {}) {
23
+ let i = n.WebSocket ?? globalThis.WebSocket, a = n.signal, o = n.dropUnreliableAbove ?? 65536;
24
+ return typeof i == "function" ? a?.aborted ? Promise.reject(a.reason) : new Promise((s, c) => {
25
+ let l = new i(e, n.protocols);
26
+ l.binaryType = "arraybuffer";
27
+ let u = () => {
28
+ l.close(), c(a?.reason);
29
+ }, d = () => {
30
+ a?.removeEventListener("abort", u), l.removeEventListener("close", f), l.removeEventListener("error", p);
31
+ }, f = (n) => {
32
+ d(), c(t("failed", `WebSocket to ${e} closed before it opened (${n.code}); check the URL and server`));
33
+ }, p = () => {
34
+ d(), l.close(), c(t("failed", `WebSocket to ${e} failed before it opened; check the URL and server`));
35
+ };
36
+ a?.addEventListener("abort", u, { once: !0 }), l.addEventListener("close", f, { once: !0 }), l.addEventListener("error", p, { once: !0 }), l.addEventListener("open", () => {
37
+ d(), s(new r(l, o));
38
+ }, { once: !0 });
39
+ }) : Promise.reject(t("unsupported", "No WebSocket class here; pass options.WebSocket or use Node 22+"));
40
+ }
41
+ //#endregion
42
+ export { i as openWebSocket };
@@ -0,0 +1,83 @@
1
+ import { type Connection } from './transport.js';
2
+ /** The part of a WebTransport session this connection uses: the W3C class, or a server's session of the same shape. */
3
+ export interface WebTransportSession {
4
+ /** Resolves once the session is up. */
5
+ readonly ready: Promise<unknown>;
6
+ /** Resolves with the close code and reason when the session ends; rejects when it ends in failure. */
7
+ readonly closed: Promise<{
8
+ closeCode?: number;
9
+ reason?: string;
10
+ }>;
11
+ /** Ends the session. */
12
+ close(info?: {
13
+ closeCode?: number;
14
+ reason?: string;
15
+ }): void;
16
+ /** The datagram channel. */
17
+ readonly datagrams: {
18
+ readonly readable: ReadableStream<Uint8Array>;
19
+ /** How big a datagram can be; the spec drops a bigger one without telling anyone. */
20
+ readonly maxDatagramSize?: number;
21
+ /** The spec's way to write. */
22
+ createWritable?(): WritableStream<Uint8Array>;
23
+ /** Older browsers' way to write. */
24
+ readonly writable?: WritableStream<Uint8Array>;
25
+ };
26
+ /** Opens a bidirectional stream. */
27
+ createBidirectionalStream(): Promise<WebTransportStreamPair>;
28
+ /** Bidirectional streams the other end opened. */
29
+ readonly incomingBidirectionalStreams: ReadableStream<WebTransportStreamPair>;
30
+ }
31
+ /** One bidirectional stream of a {@link WebTransportSession}. */
32
+ export interface WebTransportStreamPair {
33
+ /** Bytes from the other end. */
34
+ readonly readable: ReadableStream<Uint8Array>;
35
+ /** Bytes to the other end. */
36
+ readonly writable: WritableStream<Uint8Array>;
37
+ }
38
+ /** Options of {@link webTransportConnection}. */
39
+ export interface WebTransportConnectionOptions {
40
+ /** Unreliable messages are dropped while this many bytes wait on the reliable stream (default 65,536). */
41
+ dropUnreliableAbove?: number;
42
+ /** Largest reliable message taken from the other end, in bytes (default 16 MiB); bigger closes the connection. */
43
+ maxMessageBytes?: number;
44
+ }
45
+ /** Options of {@link openWebTransport}. */
46
+ export interface OpenWebTransportOptions extends WebTransportConnectionOptions {
47
+ /** Aborts the connect (the promise rejects with the signal's reason). */
48
+ signal?: AbortSignal;
49
+ /** Hashes of a self-signed certificate of at most 14 days, for a server with no public certificate. */
50
+ serverCertificateHashes?: readonly {
51
+ algorithm: 'sha-256';
52
+ value: BufferSource;
53
+ }[];
54
+ /** `'low-latency'` (the default here) or `'throughput'`. */
55
+ congestionControl?: 'default' | 'throughput' | 'low-latency';
56
+ /** The WebTransport class (default the global one). */
57
+ WebTransport?: new (url: string, options?: object) => WebTransportSession;
58
+ }
59
+ /**
60
+ * A {@link Connection} over a session that is up and one bidirectional stream of it. Use {@link openWebTransport} on a
61
+ * client and {@link acceptWebTransport} on a server; this is for a stream you opened yourself.
62
+ * @throws TypeError when the session cannot write datagrams.
63
+ */
64
+ export declare function webTransportConnection(session: WebTransportSession, stream: WebTransportStreamPair, options?: WebTransportConnectionOptions): Connection;
65
+ /**
66
+ * The server end: waits for the session and for the stream the client opens, and resolves with a {@link Connection} for
67
+ * `GameServer.accept`. Pass a session of the W3C shape, as the HTTP/3 server you run gives you.
68
+ * @throws MargoError `failed` (as a rejection) when the session ends before the client opened its stream.
69
+ */
70
+ export declare function acceptWebTransport(session: WebTransportSession, options?: WebTransportConnectionOptions): Promise<Connection>;
71
+ /**
72
+ * Opens a {@link Connection} to an `https://` HTTP/3 server, where the WebTransport class exists.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const connection = await openWebTransport('https://game.example.com/play');
77
+ * const client = await GameClient.join(game, connection, { input: readInput });
78
+ * ```
79
+ *
80
+ * @throws MargoError `unsupported` where there is no WebTransport, `failed` (as a rejection) when the session does not
81
+ * come up; the signal's reason when `signal` aborts.
82
+ */
83
+ export declare function openWebTransport(url: string, options?: OpenWebTransportOptions): Promise<Connection>;
@@ -0,0 +1,120 @@
1
+ import { n as e, r as t, t as n } from "./transport-DeQodwyv.js";
2
+ //#region src/webtransport.ts
3
+ var r = class {
4
+ constructor(e, t, r) {
5
+ this.session = e, this.onmessage = null, this.onclose = null, this.closed = !1, this.waiting = 0, this.dropAbove = r.dropUnreliableAbove ?? 65536, this.maxBytes = r.maxMessageBytes ?? 1 << 24;
6
+ let i = e.datagrams.createWritable?.() ?? e.datagrams.writable;
7
+ if (!i) throw TypeError("WebTransport session has no way to write datagrams; update the browser or server");
8
+ this.datagrams = i.getWriter(), this.stream = t.writable.getWriter(), e.closed.then((e) => this.finish(e?.closeCode ?? n.normal, e?.reason ?? ""), (e) => this.finish(n.timeout, String(e?.message ?? e))), this.readDatagrams(), this.readStream(t.readable);
9
+ }
10
+ get open() {
11
+ return !this.closed;
12
+ }
13
+ async readDatagrams() {
14
+ let e = this.session.datagrams.readable.getReader();
15
+ for (;;) {
16
+ let { done: t, value: n } = await e.read().catch(() => ({
17
+ done: !0,
18
+ value: void 0
19
+ }));
20
+ if (t || this.closed) return;
21
+ this.onmessage?.(n, "unreliable");
22
+ }
23
+ }
24
+ async readStream(e) {
25
+ let t = e.getReader(), r = /* @__PURE__ */ new Uint8Array(4096), i = 0;
26
+ for (;;) {
27
+ let { done: e, value: a } = await t.read().catch(() => ({
28
+ done: !0,
29
+ value: void 0
30
+ }));
31
+ if (this.closed) return;
32
+ if (e) {
33
+ setTimeout(() => this.close(n.protocol, "reliable stream ended"), 0);
34
+ return;
35
+ }
36
+ let o = a;
37
+ if (i + o.length > r.length) {
38
+ let e = new Uint8Array(Math.max(r.length * 2, i + o.length));
39
+ e.set(r.subarray(0, i)), r = e;
40
+ }
41
+ r.set(o, i), i += o.length;
42
+ let s = 0;
43
+ for (; i - s >= 4;) {
44
+ let e = new DataView(r.buffer, s, 4).getUint32(0, !0);
45
+ if (e > this.maxBytes) return this.close(n.rateLimit, "message too large");
46
+ if (i - s - 4 < e) break;
47
+ if (this.onmessage?.(r.slice(s + 4, s + 4 + e), "reliable"), this.closed) return;
48
+ s += 4 + e;
49
+ }
50
+ s > 0 && r.copyWithin(0, s, i), i -= s;
51
+ }
52
+ }
53
+ send(e, t = "reliable") {
54
+ if (this.closed) return;
55
+ if (t === "unreliable") {
56
+ let t = this.session.datagrams.maxDatagramSize ?? Infinity;
57
+ if (e.length <= t) {
58
+ (this.datagrams.desiredSize ?? 1) > 0 && this.datagrams.write(e).catch(() => {});
59
+ return;
60
+ }
61
+ if (this.waiting > this.dropAbove) return;
62
+ }
63
+ let n = new Uint8Array(4 + e.length);
64
+ new DataView(n.buffer).setUint32(0, e.length, !0), n.set(e, 4), this.waiting += n.length, this.stream.write(n).then(() => this.waiting -= n.length, () => {});
65
+ }
66
+ close(t = n.normal, r = "") {
67
+ this.closed || (this.session.close({
68
+ closeCode: t,
69
+ reason: e(r, 1024)
70
+ }), this.finish(t, r));
71
+ }
72
+ finish(e, t) {
73
+ this.closed || (this.closed = !0, this.onclose?.(e, t));
74
+ }
75
+ };
76
+ function i(e, t, n = {}) {
77
+ return new r(e, t, n);
78
+ }
79
+ async function a(e, r = {}) {
80
+ await e.ready;
81
+ let a = e.incomingBidirectionalStreams.getReader(), { done: o, value: s } = await a.read();
82
+ if (a.releaseLock(), o || !s) throw t("failed", "WebTransport session ended before its stream opened; check the client");
83
+ try {
84
+ return i(e, s, r);
85
+ } catch (t) {
86
+ throw e.close({
87
+ closeCode: n.error,
88
+ reason: "no connection"
89
+ }), t;
90
+ }
91
+ }
92
+ async function o(e, r = {}) {
93
+ let { signal: a, serverCertificateHashes: o, congestionControl: s = "low-latency" } = r, c = r.WebTransport ?? globalThis.WebTransport;
94
+ if (a?.throwIfAborted(), !c) throw t("unsupported", "WebTransport is not available here; use openWebSocket instead");
95
+ let l = new c(e, {
96
+ congestionControl: s,
97
+ requireUnreliable: !0,
98
+ ...o ? { serverCertificateHashes: o } : {}
99
+ });
100
+ l.closed.catch(() => {});
101
+ let u = () => l.close({
102
+ closeCode: n.normal,
103
+ reason: "aborted"
104
+ });
105
+ a?.addEventListener("abort", u, { once: !0 });
106
+ try {
107
+ await l.ready;
108
+ let e = await l.createBidirectionalStream();
109
+ return a?.throwIfAborted(), i(l, e, r);
110
+ } catch (r) {
111
+ throw l.close({
112
+ closeCode: n.normal,
113
+ reason: a?.aborted ? "aborted" : "open failed"
114
+ }), a?.aborted ? a.reason : t("failed", `WebTransport to ${e} did not open (${r.message}); check the server`);
115
+ } finally {
116
+ a?.removeEventListener("abort", u);
117
+ }
118
+ }
119
+ //#endregion
120
+ export { a as acceptWebTransport, o as openWebTransport, i as webTransportConnection };
package/package.json CHANGED
@@ -1,6 +1,140 @@
1
1
  {
2
2
  "name": "@margoengine/netcode",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Game networking for any game: schema-first binary codec with delta snapshots, an authoritative fixed-timestep server, client prediction and reconciliation, interpolation and a loopback network for tests.",
5
+ "keywords": [
6
+ "margoengine",
7
+ "netcode",
8
+ "multiplayer",
9
+ "networking",
10
+ "prediction"
11
+ ],
12
+ "homepage": "https://margoengine.com",
13
+ "bugs": {
14
+ "url": "https://github.com/scottcampbelldata/margoengine/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/scottcampbelldata/margoengine.git",
19
+ "directory": "packages/netcode"
20
+ },
21
+ "license": "Apache-2.0",
22
+ "author": "Scott Campbell",
23
+ "type": "module",
24
+ "sideEffects": false,
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ },
30
+ "./rollback": {
31
+ "types": "./dist/rollback.d.ts",
32
+ "import": "./dist/rollback.js"
33
+ },
34
+ "./physics": {
35
+ "types": "./dist/physics.d.ts",
36
+ "import": "./dist/physics.js"
37
+ },
38
+ "./websocket": {
39
+ "types": "./dist/websocket.d.ts",
40
+ "import": "./dist/websocket.js"
41
+ },
42
+ "./webrtc": {
43
+ "types": "./dist/webrtc.d.ts",
44
+ "import": "./dist/webrtc.js"
45
+ },
46
+ "./node": {
47
+ "types": "./dist/node.d.ts",
48
+ "import": "./dist/node.js"
49
+ },
50
+ "./rooms": {
51
+ "types": "./dist/rooms.d.ts",
52
+ "import": "./dist/rooms.js"
53
+ },
54
+ "./smoothing": {
55
+ "types": "./dist/smoothing.d.ts",
56
+ "import": "./dist/smoothing.js"
57
+ },
58
+ "./webtransport": {
59
+ "types": "./dist/webtransport.d.ts",
60
+ "import": "./dist/webtransport.js"
61
+ },
62
+ "./signal": {
63
+ "types": "./dist/signal.d.ts",
64
+ "import": "./dist/signal.js"
65
+ },
66
+ "./extrapolate": {
67
+ "types": "./dist/extrapolate.d.ts",
68
+ "import": "./dist/extrapolate.js"
69
+ },
70
+ "./relay": {
71
+ "types": "./dist/relay.d.ts",
72
+ "import": "./dist/relay.js"
73
+ },
74
+ "./hitboxes": {
75
+ "types": "./dist/hitboxes.d.ts",
76
+ "import": "./dist/hitboxes.js"
77
+ },
78
+ "./interest": {
79
+ "types": "./dist/interest.d.ts",
80
+ "import": "./dist/interest.js"
81
+ },
82
+ "./generate": {
83
+ "types": "./dist/generate.d.ts",
84
+ "import": "./dist/generate.js"
85
+ },
86
+ "./lobby": {
87
+ "types": "./dist/lobby.d.ts",
88
+ "import": "./dist/lobby.js"
89
+ },
90
+ "./media": {
91
+ "types": "./dist/media.d.ts",
92
+ "import": "./dist/media.js"
93
+ },
94
+ "./presence": {
95
+ "types": "./dist/presence.d.ts",
96
+ "import": "./dist/presence.js"
97
+ },
98
+ "./generate-rust": {
99
+ "types": "./dist/generate-rust.d.ts",
100
+ "import": "./dist/generate-rust.js"
101
+ },
102
+ "./migrate": {
103
+ "types": "./dist/migrate.d.ts",
104
+ "import": "./dist/migrate.js"
105
+ },
106
+ "./interpolation": {
107
+ "types": "./dist/interpolation.d.ts",
108
+ "import": "./dist/interpolation.js"
109
+ },
110
+ "./interpolation-delay": {
111
+ "types": "./dist/interpolation-delay.d.ts",
112
+ "import": "./dist/interpolation-delay.js"
113
+ },
114
+ "./interpolation-fields": {
115
+ "types": "./dist/interpolation-fields.d.ts",
116
+ "import": "./dist/interpolation-fields.js"
117
+ },
118
+ "./interpolation-timeline": {
119
+ "types": "./dist/interpolation-timeline.d.ts",
120
+ "import": "./dist/interpolation-timeline.js"
121
+ }
122
+ },
123
+ "files": [
124
+ "dist",
125
+ "LICENSE",
126
+ "NOTICE"
127
+ ],
128
+ "publishConfig": {
129
+ "access": "public"
130
+ },
131
+ "dependencies": {
132
+ "@margoengine/math": "^1.0.0"
133
+ },
134
+ "scripts": {
135
+ "build": "node ../../scripts/build-optional.js netcode",
136
+ "test": "npm run build && node --test tests/*.test.js",
137
+ "test:browser": "npm run build && node tests/browser/signal.mjs && node tests/browser/media.mjs",
138
+ "prepack": "npm run build"
139
+ }
140
+ }