@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1387 -2
- package/dist/client.d.ts +167 -0
- package/dist/clock.d.ts +29 -0
- package/dist/extrapolate.d.ts +42 -0
- package/dist/extrapolate.js +25 -0
- package/dist/game.d.ts +68 -0
- package/dist/generate-rust.d.ts +14 -0
- package/dist/generate-rust.js +184 -0
- package/dist/generate.d.ts +25 -0
- package/dist/generate.js +156 -0
- package/dist/hitboxes.d.ts +74 -0
- package/dist/hitboxes.js +124 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +457 -0
- package/dist/interest.d.ts +101 -0
- package/dist/interest.js +166 -0
- package/dist/interpolation-delay.d.ts +114 -0
- package/dist/interpolation-delay.js +79 -0
- package/dist/interpolation-fields.d.ts +117 -0
- package/dist/interpolation-fields.js +147 -0
- package/dist/interpolation-timeline.d.ts +100 -0
- package/dist/interpolation-timeline.js +122 -0
- package/dist/interpolation.d.ts +39 -0
- package/dist/interpolation.js +61 -0
- package/dist/lag.d.ts +62 -0
- package/dist/lobby.d.ts +77 -0
- package/dist/lobby.js +96 -0
- package/dist/loopback.d.ts +92 -0
- package/dist/media.d.ts +215 -0
- package/dist/media.js +305 -0
- package/dist/migrate.d.ts +113 -0
- package/dist/migrate.js +289 -0
- package/dist/node.d.ts +101 -0
- package/dist/node.js +166 -0
- package/dist/physics.d.ts +133 -0
- package/dist/physics.js +169 -0
- package/dist/presence.d.ts +156 -0
- package/dist/presence.js +226 -0
- package/dist/protocol-asi_qndb.js +122 -0
- package/dist/protocol.d.ts +75 -0
- package/dist/relay.d.ts +152 -0
- package/dist/relay.js +235 -0
- package/dist/rollback.d.ts +131 -0
- package/dist/rollback.js +194 -0
- package/dist/rooms.d.ts +366 -0
- package/dist/rooms.js +479 -0
- package/dist/schema-CMfqK4Gu.js +616 -0
- package/dist/schema.d.ts +263 -0
- package/dist/server-DAVzHcWW.js +203 -0
- package/dist/server.d.ts +147 -0
- package/dist/shared/bytes.d.ts +38 -0
- package/dist/signal.d.ts +156 -0
- package/dist/signal.js +261 -0
- package/dist/smoothing.d.ts +76 -0
- package/dist/smoothing.js +155 -0
- package/dist/transport-DeQodwyv.js +49 -0
- package/dist/transport.d.ts +76 -0
- package/dist/webrtc.d.ts +32 -0
- package/dist/webrtc.js +64 -0
- package/dist/websocket.d.ts +27 -0
- package/dist/websocket.js +42 -0
- package/dist/webtransport.d.ts +83 -0
- package/dist/webtransport.js +120 -0
- package/package.json +138 -4
package/dist/rooms.js
ADDED
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { n as e, t } from "./schema-CMfqK4Gu.js";
|
|
2
|
+
import { r as n, t as r } from "./transport-DeQodwyv.js";
|
|
3
|
+
//#region src/rooms.ts
|
|
4
|
+
var i = 48, a = 49, o = 50, s = 56, c = 57, l = 58, u = 59, d = 60, f = [
|
|
5
|
+
"create",
|
|
6
|
+
"join",
|
|
7
|
+
"joinOrCreate",
|
|
8
|
+
"joinById",
|
|
9
|
+
"reconnect"
|
|
10
|
+
], p = /* @__PURE__ */ new Set([
|
|
11
|
+
r.normal,
|
|
12
|
+
r.protocol,
|
|
13
|
+
r.rateLimit,
|
|
14
|
+
r.mismatch,
|
|
15
|
+
r.removed,
|
|
16
|
+
r.error
|
|
17
|
+
]), m = new TextEncoder(), h = new TextDecoder("utf-8", { fatal: !0 }), g = (e, t) => {
|
|
18
|
+
let n = m.encode(t);
|
|
19
|
+
e.varint(n.length), e.raw(n);
|
|
20
|
+
}, _ = (e, t) => {
|
|
21
|
+
let n = e.count(t);
|
|
22
|
+
e.need(n);
|
|
23
|
+
let r = e.at;
|
|
24
|
+
e.at += n;
|
|
25
|
+
try {
|
|
26
|
+
return h.decode(e.bytes.subarray(r, r + n));
|
|
27
|
+
} catch {
|
|
28
|
+
throw e.fail(`holds text that is not UTF-8 at byte ${r}`);
|
|
29
|
+
}
|
|
30
|
+
}, v = (e) => {
|
|
31
|
+
let t = e.bytes.slice(e.at);
|
|
32
|
+
return e.at = e.bytes.length, t;
|
|
33
|
+
}, y = (e) => JSON.stringify(e ?? null), b = class {
|
|
34
|
+
constructor(e, t, n) {
|
|
35
|
+
this.connection = t, this.limit = n, this.userData = void 0, this.auth = void 0, this.secret = "", this.sessionId = e;
|
|
36
|
+
}
|
|
37
|
+
send(t, n) {
|
|
38
|
+
let r = new e();
|
|
39
|
+
if (r.u8(d), g(r, t), g(r, y(n)), r.at > this.limit) throw RangeError(`Room message '${t}' is ${r.at} bytes; keep it under ${this.limit}`);
|
|
40
|
+
this.connection.send(r.done(), "reliable");
|
|
41
|
+
}
|
|
42
|
+
leave(e = r.removed, t = "removed") {
|
|
43
|
+
this.connection.close(e, t);
|
|
44
|
+
}
|
|
45
|
+
}, x = class {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.maxClients = 64, this.autoDispose = !0, this.maxMessageBytes = 4096, this.reconnectWindow = 0, this.maxMessagesPerSecond = void 0, this.messageBurst = void 0, this.roomId = "", this.roomName = "", this.clients = [], this.held = /* @__PURE__ */ new Map(), this.secrets = /* @__PURE__ */ new Map(), this.sent = /* @__PURE__ */ new Map(), this.handlers = /* @__PURE__ */ new Map(), this.patchRate = -1, this.lockedByHand = !1, this.disposed = !1, this.ids = /* @__PURE__ */ new WeakMap(), this.buckets = /* @__PURE__ */ new WeakMap(), this.nextId = 1, this.hooks = /* @__PURE__ */ new WeakMap(), this.onEmpty = null, this.report = (e) => console.error(e);
|
|
48
|
+
}
|
|
49
|
+
get state() {
|
|
50
|
+
return this.current;
|
|
51
|
+
}
|
|
52
|
+
get locked() {
|
|
53
|
+
return this.lockedByHand || this.clients.length + this.held.size >= this.maxClients;
|
|
54
|
+
}
|
|
55
|
+
get dropped() {
|
|
56
|
+
return [...this.held.keys()];
|
|
57
|
+
}
|
|
58
|
+
get empty() {
|
|
59
|
+
return !this.clients.length && !this.held.size;
|
|
60
|
+
}
|
|
61
|
+
claimSeat(e) {
|
|
62
|
+
return !this.locked;
|
|
63
|
+
}
|
|
64
|
+
idOf(e) {
|
|
65
|
+
return this.ids.get(e) ?? 0;
|
|
66
|
+
}
|
|
67
|
+
setState(e) {
|
|
68
|
+
this.current = e, this.patchRate < 0 && this.setPatchRate(50);
|
|
69
|
+
}
|
|
70
|
+
setPatchRate(e) {
|
|
71
|
+
this.patchTimer !== void 0 && clearInterval(this.patchTimer), this.patchRate = e, this.patchTimer = e > 0 ? setInterval(() => this.guard(() => this.patch()), e) : void 0;
|
|
72
|
+
}
|
|
73
|
+
setSimulationInterval(e, t = 1e3 / 60) {
|
|
74
|
+
this.simulationTimer !== void 0 && clearInterval(this.simulationTimer);
|
|
75
|
+
let n = performance.now();
|
|
76
|
+
this.simulationTimer = setInterval(() => {
|
|
77
|
+
let t = performance.now(), r = t - n;
|
|
78
|
+
n = t, this.guard(() => e(r));
|
|
79
|
+
}, t);
|
|
80
|
+
}
|
|
81
|
+
guard(e) {
|
|
82
|
+
if (!this.disposed) try {
|
|
83
|
+
let t = e();
|
|
84
|
+
t instanceof Promise && t.catch((e) => this.stop(e));
|
|
85
|
+
} catch (e) {
|
|
86
|
+
this.stop(e);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
stop(e) {
|
|
90
|
+
if (!this.disposed) {
|
|
91
|
+
this.report(e);
|
|
92
|
+
for (let e of [...this.clients]) e.connection.close(r.error, "room stopped by an error");
|
|
93
|
+
this.release();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
onMessage(e, t) {
|
|
97
|
+
this.handlers.set(e, t);
|
|
98
|
+
}
|
|
99
|
+
broadcast(e, t, { except: n } = {}) {
|
|
100
|
+
let r = new Set(Array.isArray(n) ? n : n ? [n] : []);
|
|
101
|
+
for (let n of this.clients) r.has(n) || n.send(e, t);
|
|
102
|
+
}
|
|
103
|
+
lock() {
|
|
104
|
+
this.lockedByHand = !0;
|
|
105
|
+
}
|
|
106
|
+
unlock() {
|
|
107
|
+
this.lockedByHand = !1;
|
|
108
|
+
}
|
|
109
|
+
patch() {
|
|
110
|
+
if (this.current === void 0 || this.disposed) return;
|
|
111
|
+
let t = this.schema, n = this.stateType, r = t.encode(n, this.current), i = t.decode(n, r);
|
|
112
|
+
for (let a of this.clients) {
|
|
113
|
+
let o = this.sent.get(a);
|
|
114
|
+
if (o && S(o, r)) continue;
|
|
115
|
+
let s = new e(r.length + 8);
|
|
116
|
+
o ? (s.u8(u), s.raw(t.encodeDelta(n, i, t.decode(n, o)))) : (s.u8(l), s.raw(r)), this.sent.set(a, r), a.connection.send(s.done(), "reliable");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
disconnect() {
|
|
120
|
+
for (let e of [...this.clients]) e.leave(r.removed, "room closed");
|
|
121
|
+
this.release();
|
|
122
|
+
}
|
|
123
|
+
[Symbol.dispose]() {
|
|
124
|
+
this.disconnect();
|
|
125
|
+
}
|
|
126
|
+
onCreate(e) {}
|
|
127
|
+
onAuth(e, t) {
|
|
128
|
+
return !0;
|
|
129
|
+
}
|
|
130
|
+
onJoin(e, t) {}
|
|
131
|
+
onLeave(e, t) {}
|
|
132
|
+
onDrop(e) {}
|
|
133
|
+
onReconnect(e) {}
|
|
134
|
+
onDispose() {}
|
|
135
|
+
async admit(e, t, n) {
|
|
136
|
+
let r = new b(t, e, this.maxMessageBytes), i;
|
|
137
|
+
try {
|
|
138
|
+
i = await this.onAuth(r, n);
|
|
139
|
+
} catch {
|
|
140
|
+
i = !1;
|
|
141
|
+
}
|
|
142
|
+
if (i === !1 || i == null || this.disposed || !e.open || !this.claimSeat(n)) return "refused";
|
|
143
|
+
r.auth = i, this.ids.set(r, this.nextId++), this.buckets.set(r, {
|
|
144
|
+
tokens: this.messageBurst ?? 60,
|
|
145
|
+
at: performance.now()
|
|
146
|
+
}), this.clients.push(r), this.seat(r, e);
|
|
147
|
+
try {
|
|
148
|
+
await this.during(r, () => this.onJoin(r, n)), this.patch();
|
|
149
|
+
} catch (e) {
|
|
150
|
+
return this.fail(r, e, "onJoin failed"), "failed";
|
|
151
|
+
}
|
|
152
|
+
return "joined";
|
|
153
|
+
}
|
|
154
|
+
seat(t, n) {
|
|
155
|
+
t.connection = n, n.onmessage = (e) => this.receive(t, e);
|
|
156
|
+
let i = n.onclose;
|
|
157
|
+
n.onclose = (e, a) => {
|
|
158
|
+
i?.(e, a), t.connection === n && this.drop(t, e === r.normal, e).catch((e) => this.report(e));
|
|
159
|
+
}, this.secrets.delete(t.secret), t.secret = this.reconnectWindow > 0 ? E() : "", t.secret && this.secrets.set(t.secret, t);
|
|
160
|
+
let a = new e(64);
|
|
161
|
+
a.u8(s), g(a, this.roomId), g(a, t.sessionId), a.u32(this.schema.hash), g(a, t.secret && `${this.roomId}.${t.secret}`), n.send(a.done(), "reliable");
|
|
162
|
+
}
|
|
163
|
+
async resume(e, t) {
|
|
164
|
+
let n = this.secrets.get(t);
|
|
165
|
+
if (!n || this.disposed || !e.open) return "refused";
|
|
166
|
+
let i = this.held.get(n), a = n.connection;
|
|
167
|
+
this.sent.delete(n), i !== void 0 && (clearTimeout(i), this.held.delete(n), this.clients.push(n)), this.seat(n, e), i === void 0 && a.close(r.removed, "reconnected on a new connection");
|
|
168
|
+
try {
|
|
169
|
+
await this.during(n, () => this.onReconnect(n)), this.patch();
|
|
170
|
+
} catch (e) {
|
|
171
|
+
return this.fail(n, e, "onReconnect failed"), "failed";
|
|
172
|
+
}
|
|
173
|
+
return "joined";
|
|
174
|
+
}
|
|
175
|
+
during(e, t) {
|
|
176
|
+
let n = t();
|
|
177
|
+
if (!(n instanceof Promise)) return n;
|
|
178
|
+
let r = n.then(() => this.forget(e, r), () => this.forget(e, r));
|
|
179
|
+
return this.hooks.set(e, r), n;
|
|
180
|
+
}
|
|
181
|
+
forget(e, t) {
|
|
182
|
+
this.hooks.get(e) === t && this.hooks.delete(e);
|
|
183
|
+
}
|
|
184
|
+
fail(e, t, n) {
|
|
185
|
+
this.report(t), e.connection.close(r.error, n);
|
|
186
|
+
}
|
|
187
|
+
receive(e, n) {
|
|
188
|
+
let i = this.buckets.get(e);
|
|
189
|
+
if (i) {
|
|
190
|
+
let t = performance.now(), n = this.maxMessagesPerSecond ?? 120;
|
|
191
|
+
if (i.tokens = Math.min(this.messageBurst ?? 60, i.tokens + (t - i.at) * n / 1e3), i.at = t, i.tokens < 1) return e.connection.close(r.rateLimit, `over ${n} messages per second`);
|
|
192
|
+
i.tokens--;
|
|
193
|
+
}
|
|
194
|
+
let s, c;
|
|
195
|
+
try {
|
|
196
|
+
if (n.length > this.maxMessageBytes) throw RangeError("Room message is over maxMessageBytes; send smaller messages or raise the limit");
|
|
197
|
+
let i = new t(n, "Room message", "send what the room client sends"), l = i.u8();
|
|
198
|
+
if (l === o) return e.connection.close(r.normal, "left");
|
|
199
|
+
if (l !== a) throw i.fail(`has kind ${l}, not a message`);
|
|
200
|
+
let u = _(i, 256);
|
|
201
|
+
c = JSON.parse(_(i, this.maxMessageBytes)), i.end(), s = this.handlers.get(u) ?? this.handlers.get("*");
|
|
202
|
+
} catch (t) {
|
|
203
|
+
return t instanceof RangeError || t instanceof SyntaxError || t instanceof TypeError ? e.connection.close(r.protocol, t.message) : this.fail(e, t, "room error");
|
|
204
|
+
}
|
|
205
|
+
if (s) try {
|
|
206
|
+
let t = s(e, c);
|
|
207
|
+
t instanceof Promise && t.catch((t) => this.fail(e, t, "message handler failed"));
|
|
208
|
+
} catch (t) {
|
|
209
|
+
this.fail(e, t, "message handler failed");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async drop(e, t, n) {
|
|
213
|
+
let r = e.connection, i = this.hooks.get(e);
|
|
214
|
+
if (i && (await i, e.connection !== r)) return;
|
|
215
|
+
let a = this.clients.indexOf(e);
|
|
216
|
+
if (!(a < 0)) {
|
|
217
|
+
if (this.clients.splice(a, 1), this.sent.delete(e), e.secret && !this.disposed && !p.has(n)) {
|
|
218
|
+
let n = setTimeout(() => this.expire(e, t).catch((e) => this.report(e)), this.reconnectWindow);
|
|
219
|
+
n.unref?.(), this.held.set(e, n), await this.onDrop(e);
|
|
220
|
+
} else await this.expire(e, t);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async expire(e, t) {
|
|
224
|
+
clearTimeout(this.held.get(e)), this.held.delete(e), this.secrets.delete(e.secret);
|
|
225
|
+
try {
|
|
226
|
+
await this.onLeave(e, t);
|
|
227
|
+
} finally {
|
|
228
|
+
this.empty && this.autoDispose && this.release();
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
release() {
|
|
232
|
+
if (!this.disposed) {
|
|
233
|
+
this.disposed = !0, this.patchTimer !== void 0 && clearInterval(this.patchTimer), this.simulationTimer !== void 0 && clearInterval(this.simulationTimer);
|
|
234
|
+
for (let e of [...this.held.keys()]) this.expire(e, !1).catch((e) => this.report(e));
|
|
235
|
+
this.onEmpty?.();
|
|
236
|
+
try {
|
|
237
|
+
let e = this.onDispose();
|
|
238
|
+
e instanceof Promise && e.catch((e) => this.report(e));
|
|
239
|
+
} catch (e) {
|
|
240
|
+
this.report(e);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}, S = (e, t) => e.length === t.length && e.every((e, n) => e === t[n]), C = (e, t, n, r) => {
|
|
245
|
+
if (typeof t != "number" || !(t >= n && t < Infinity) || r && t % 1) throw RangeError(`RoomServer ${e} is ${String(t)}; pass ${r ? "an integer" : "a number"} >= ${n}`);
|
|
246
|
+
return t;
|
|
247
|
+
}, w = (e, t) => {
|
|
248
|
+
if (typeof t != "number" || !(t > 0 && t < Infinity)) throw RangeError(`${e} is ${String(t)}; pass a finite number > 0`);
|
|
249
|
+
return t;
|
|
250
|
+
}, T = class {
|
|
251
|
+
constructor(e = {}) {
|
|
252
|
+
this.classes = /* @__PURE__ */ new Map(), this.live = /* @__PURE__ */ new Map(), this.creating = /* @__PURE__ */ new Map(), this.joining = /* @__PURE__ */ new Set(), this.open = 0, this.sessions = 0, this.closed = !1, this.onerror = null;
|
|
253
|
+
let t = e ?? {}, n = t.limits ?? {};
|
|
254
|
+
this.joinTimeout = C("joinTimeout", t.joinTimeout ?? 1e4, 1, !1), this.maxConnections = C("maxConnections", t.maxConnections ?? 1024, 1, !0), this.maxRooms = C("maxRooms", t.maxRooms ?? 256, 1, !0), this.rate = w("RoomServer limits.messagesPerSecond", n.messagesPerSecond ?? 120), this.burst = C("limits.burst", n.burst ?? 60, 1, !1);
|
|
255
|
+
}
|
|
256
|
+
get rooms() {
|
|
257
|
+
return [...this.live.values()];
|
|
258
|
+
}
|
|
259
|
+
get connections() {
|
|
260
|
+
return this.open;
|
|
261
|
+
}
|
|
262
|
+
report(e, t) {
|
|
263
|
+
this.onerror ? this.onerror(e, t) : console.error(e);
|
|
264
|
+
}
|
|
265
|
+
define(e, t, n = {}) {
|
|
266
|
+
if (this.classes.has(e)) throw TypeError(`Room '${e}' is already defined; pick another name`);
|
|
267
|
+
return this.classes.set(e, {
|
|
268
|
+
Class: t,
|
|
269
|
+
defaults: n
|
|
270
|
+
}), this;
|
|
271
|
+
}
|
|
272
|
+
async create(e, t = {}) {
|
|
273
|
+
let r = this.classes.get(e);
|
|
274
|
+
if (!r) throw n("failed", `No room named '${e}' is defined; call define('${e}', RoomClass)`);
|
|
275
|
+
if (this.closed) throw n("disposed", "RoomServer is closed; create a new RoomServer");
|
|
276
|
+
if (this.full) throw n("failed", `RoomServer holds maxRooms (${this.maxRooms}) rooms; raise maxRooms or wait for one to close`);
|
|
277
|
+
let i = new r.Class();
|
|
278
|
+
if (i.maxMessagesPerSecond = w(`Room '${e}' maxMessagesPerSecond`, i.maxMessagesPerSecond ?? this.rate), i.messageBurst = i.messageBurst ?? this.burst, !(i.messageBurst >= 1 && i.messageBurst < Infinity)) throw RangeError(`Room '${e}' messageBurst is ${i.messageBurst}; pass a finite number >= 1`);
|
|
279
|
+
i.roomName = e;
|
|
280
|
+
do
|
|
281
|
+
i.roomId = D();
|
|
282
|
+
while (this.live.has(i.roomId) || [...this.creating.keys()].some((e) => e.roomId === i.roomId));
|
|
283
|
+
i.report = (e) => this.report(e, i);
|
|
284
|
+
let a = (async () => i.onCreate({
|
|
285
|
+
...r.defaults,
|
|
286
|
+
...t
|
|
287
|
+
}))();
|
|
288
|
+
this.creating.set(i, a);
|
|
289
|
+
try {
|
|
290
|
+
await a;
|
|
291
|
+
} catch (e) {
|
|
292
|
+
throw i.release(), e;
|
|
293
|
+
} finally {
|
|
294
|
+
this.creating.delete(i);
|
|
295
|
+
}
|
|
296
|
+
if (this.closed) throw i.release(), n("disposed", "RoomServer closed while the room was being created; create a new RoomServer");
|
|
297
|
+
return i.onEmpty = () => this.live.delete(i.roomId), this.live.set(i.roomId, i), i;
|
|
298
|
+
}
|
|
299
|
+
get full() {
|
|
300
|
+
return this.live.size + this.creating.size >= this.maxRooms;
|
|
301
|
+
}
|
|
302
|
+
accept(e) {
|
|
303
|
+
if (this.closed) throw n("disposed", "RoomServer is closed; create a new RoomServer");
|
|
304
|
+
if (this.open >= this.maxConnections) return e.close(r.full, `server full: ${this.maxConnections} connections (maxConnections)`);
|
|
305
|
+
this.open++, this.joining.add(e);
|
|
306
|
+
let t = setTimeout(() => e.close(r.timeout, `no join within ${this.joinTimeout} ms (joinTimeout)`), this.joinTimeout), i = !0;
|
|
307
|
+
e.onclose = () => {
|
|
308
|
+
clearTimeout(t), this.joining.delete(e), i && this.open--, i = !1;
|
|
309
|
+
}, e.onmessage = (n) => {
|
|
310
|
+
e.onmessage = null, clearTimeout(t), this.joining.delete(e), this.join(e, n).catch((t) => {
|
|
311
|
+
this.report(t, void 0), e.close(r.error, "room error");
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
refuse(t, n) {
|
|
316
|
+
let i = new e(64);
|
|
317
|
+
i.u8(c), g(i, n), t.send(i.done(), "reliable"), t.close(r.normal, "refused");
|
|
318
|
+
}
|
|
319
|
+
async join(e, n) {
|
|
320
|
+
let a, o, s, c;
|
|
321
|
+
try {
|
|
322
|
+
let e = new t(n, "Room join", "send what RoomsClient sends");
|
|
323
|
+
if (e.u8() !== i) throw e.fail("is not a join");
|
|
324
|
+
if (a = f[e.u8()], !a) throw e.fail("names an unknown join method");
|
|
325
|
+
o = _(e, 256), s = _(e, 64);
|
|
326
|
+
let r = JSON.parse(_(e, 4096));
|
|
327
|
+
if (e.end(), !r || typeof r != "object" || Array.isArray(r)) throw TypeError("options must be a JSON object");
|
|
328
|
+
c = r;
|
|
329
|
+
} catch (t) {
|
|
330
|
+
if (!(t instanceof RangeError || t instanceof SyntaxError || t instanceof TypeError)) throw t;
|
|
331
|
+
return e.close(r.protocol, t.message);
|
|
332
|
+
}
|
|
333
|
+
if (a === "reconnect") {
|
|
334
|
+
let t = String(c.token), n = t.indexOf("."), r = this.live.get(t.slice(0, n));
|
|
335
|
+
(r ? await r.resume(e, t.slice(n + 1)) : "refused") === "refused" && this.refuse(e, "the seat is gone; join the room again");
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (!this.classes.has(o) && a !== "joinById") return this.refuse(e, `no room named '${o}'`);
|
|
339
|
+
let l = () => a === "joinById" ? this.live.get(s) : [...this.live.values()].find((e) => e.roomName === o && !e.locked), u = a === "create" ? void 0 : l();
|
|
340
|
+
if (!u && a !== "create") {
|
|
341
|
+
let e = [...this.creating].filter(([e]) => a === "joinById" ? e.roomId === s : e.roomName === o).map(([, e]) => e.then(() => {}, () => {}));
|
|
342
|
+
e.length && (await Promise.all(e), u = l());
|
|
343
|
+
}
|
|
344
|
+
if (!u && (a === "create" || a === "joinOrCreate")) {
|
|
345
|
+
if (this.full) return this.refuse(e, `the server holds its most rooms (${this.maxRooms}); try later`);
|
|
346
|
+
u = await this.create(o, c);
|
|
347
|
+
}
|
|
348
|
+
if (!u) return this.refuse(e, a === "joinById" ? `no room '${s}'` : `no open '${o}' room`);
|
|
349
|
+
await u.admit(e, `${D()}${(++this.sessions).toString(36)}`, c) === "refused" && (this.refuse(e, "the room refused the join"), u.empty && u.autoDispose && u.release());
|
|
350
|
+
}
|
|
351
|
+
close() {
|
|
352
|
+
this.closed = !0;
|
|
353
|
+
for (let e of [...this.joining]) e.close(r.removed, "server closing");
|
|
354
|
+
for (let e of [...this.live.values()]) e.disconnect();
|
|
355
|
+
}
|
|
356
|
+
[Symbol.dispose]() {
|
|
357
|
+
this.close();
|
|
358
|
+
}
|
|
359
|
+
}, E = () => [...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16))].map((e) => e.toString(16).padStart(2, "0")).join(""), D = () => [...crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(6))].map((e) => e.toString(36).padStart(2, "0")).join("").slice(0, 9), O = class {
|
|
360
|
+
constructor(e, t, n, r, i, a) {
|
|
361
|
+
this.connection = e, this.schema = t, this.stateType = n, this.stateHandlers = /* @__PURE__ */ new Set(), this.messageHandlers = /* @__PURE__ */ new Map(), this.leaveHandlers = /* @__PURE__ */ new Set(), this.roomId = r, this.sessionId = i, this.reconnectionToken = a, e.onmessage = (e) => this.receive(e), e.onclose = (e) => {
|
|
362
|
+
for (let t of this.leaveHandlers) t(e);
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
get state() {
|
|
366
|
+
return this.current;
|
|
367
|
+
}
|
|
368
|
+
receive(e) {
|
|
369
|
+
try {
|
|
370
|
+
let n = new t(e, "Room message", "update the client to the server version"), r = n.u8();
|
|
371
|
+
if (r === l || r === u) {
|
|
372
|
+
let e = v(n);
|
|
373
|
+
if (r === u && this.current === void 0) throw n.fail("patches a state it never sent");
|
|
374
|
+
this.current = r === l ? this.schema.decode(this.stateType, e) : this.schema.decodeDelta(this.stateType, e, this.current);
|
|
375
|
+
for (let e of this.stateHandlers) e(this.current);
|
|
376
|
+
} else if (r === d) {
|
|
377
|
+
let e = _(n, 256), t = JSON.parse(_(n, 1 << 20));
|
|
378
|
+
n.end();
|
|
379
|
+
for (let n of this.messageHandlers.get(e) ?? this.messageHandlers.get("*") ?? []) n(t);
|
|
380
|
+
} else throw n.fail(`has unknown kind ${r}`);
|
|
381
|
+
} catch (e) {
|
|
382
|
+
if (!(e instanceof RangeError || e instanceof SyntaxError || e instanceof TypeError)) throw e;
|
|
383
|
+
this.connection.close(r.protocol, e.message.slice(0, 120));
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
onStateChange(e) {
|
|
387
|
+
return this.stateHandlers.add(e), () => this.stateHandlers.delete(e);
|
|
388
|
+
}
|
|
389
|
+
onMessage(e, t) {
|
|
390
|
+
let n = this.messageHandlers.get(e) ?? /* @__PURE__ */ new Set();
|
|
391
|
+
return n.add(t), this.messageHandlers.set(e, n), () => n.delete(t);
|
|
392
|
+
}
|
|
393
|
+
onLeave(e) {
|
|
394
|
+
return this.leaveHandlers.add(e), () => this.leaveHandlers.delete(e);
|
|
395
|
+
}
|
|
396
|
+
async *states({ signal: e } = {}) {
|
|
397
|
+
let t = [], n = null, r = !this.connection.open, i = () => {
|
|
398
|
+
let e = n;
|
|
399
|
+
n = null, e?.();
|
|
400
|
+
}, a = this.onStateChange((e) => {
|
|
401
|
+
t.push(e), i();
|
|
402
|
+
}), o = this.onLeave(() => {
|
|
403
|
+
r = !0, i();
|
|
404
|
+
}), s = () => {
|
|
405
|
+
r = !0, i();
|
|
406
|
+
};
|
|
407
|
+
e?.addEventListener("abort", s, { once: !0 });
|
|
408
|
+
try {
|
|
409
|
+
for (;;) {
|
|
410
|
+
for (; t.length;) yield t.shift();
|
|
411
|
+
if (r) return;
|
|
412
|
+
await new Promise((e) => n = e);
|
|
413
|
+
}
|
|
414
|
+
} finally {
|
|
415
|
+
a(), o(), e?.removeEventListener("abort", s);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
send(t, n) {
|
|
419
|
+
let r = new e();
|
|
420
|
+
r.u8(a), g(r, t), g(r, y(n)), this.connection.send(r.done(), "reliable");
|
|
421
|
+
}
|
|
422
|
+
leave() {
|
|
423
|
+
if (!this.connection.open) return;
|
|
424
|
+
let t = new e(4);
|
|
425
|
+
t.u8(o), this.connection.send(t.done(), "reliable"), this.connection.close(r.normal, "left");
|
|
426
|
+
}
|
|
427
|
+
[Symbol.dispose]() {
|
|
428
|
+
this.leave();
|
|
429
|
+
}
|
|
430
|
+
}, k = class {
|
|
431
|
+
constructor(e) {
|
|
432
|
+
this.connect = e;
|
|
433
|
+
}
|
|
434
|
+
joinOrCreate(e, t, n) {
|
|
435
|
+
return this.open("joinOrCreate", e, "", t, n);
|
|
436
|
+
}
|
|
437
|
+
create(e, t, n) {
|
|
438
|
+
return this.open("create", e, "", t, n);
|
|
439
|
+
}
|
|
440
|
+
join(e, t, n) {
|
|
441
|
+
return this.open("join", e, "", t, n);
|
|
442
|
+
}
|
|
443
|
+
joinById(e, t, n) {
|
|
444
|
+
return this.open("joinById", "", e, t, n);
|
|
445
|
+
}
|
|
446
|
+
reconnect(e, t) {
|
|
447
|
+
return this.open("reconnect", "", "", { token: e }, t);
|
|
448
|
+
}
|
|
449
|
+
async open(a, o, l, u, { schema: d, stateType: p, signal: m }) {
|
|
450
|
+
m?.throwIfAborted();
|
|
451
|
+
let h = await this.connect(), v = d;
|
|
452
|
+
if (m?.aborted) throw h.close(r.normal, "aborted"), m.reason;
|
|
453
|
+
return new Promise((d, b) => {
|
|
454
|
+
let x = () => {
|
|
455
|
+
h.onclose = null, h.onmessage = null, h.close(r.normal, "aborted"), b(m?.reason);
|
|
456
|
+
};
|
|
457
|
+
m?.addEventListener("abort", x, { once: !0 });
|
|
458
|
+
let S = (e) => {
|
|
459
|
+
m?.removeEventListener("abort", x), b(n("failed", e));
|
|
460
|
+
};
|
|
461
|
+
h.onclose = (e, t) => S(`Room join closed (${e} ${t}); check the room name and server`), h.onmessage = (e) => {
|
|
462
|
+
try {
|
|
463
|
+
let n = new t(e, "Room reply", "update the client to the server version"), i = n.u8();
|
|
464
|
+
if (i === c) return S(`Room join refused: ${_(n, 4096)}; check the name or try another`);
|
|
465
|
+
if (i !== s) throw n.fail(`has kind ${i}, not a join reply`);
|
|
466
|
+
let a = _(n, 64), o = _(n, 64), l = n.u32(), u = _(n, 128);
|
|
467
|
+
if (n.end(), l !== v.hash) return h.close(r.mismatch, "schema differs"), S("Room state schema differs from the server; use the same schema on both ends");
|
|
468
|
+
m?.removeEventListener("abort", x), d(new O(h, v, p, a, o, u));
|
|
469
|
+
} catch (e) {
|
|
470
|
+
h.close(r.protocol, e.message.slice(0, 120)), S(`Room join failed: ${e.message}`);
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
let C = new e(64);
|
|
474
|
+
C.u8(i), C.u8(f.indexOf(a)), g(C, o), g(C, l), g(C, y(u)), h.send(C.done(), "reliable");
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
//#endregion
|
|
479
|
+
export { O as JoinedRoom, x as Room, b as RoomClient, T as RoomServer, k as RoomsClient };
|