peerodactyl 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +200 -2
- package/dist/angular.d.ts +42 -0
- package/dist/angular.js +48 -0
- package/dist/angular.js.map +1 -0
- package/dist/chunk-ULFDGPNT.js +963 -0
- package/dist/chunk-ULFDGPNT.js.map +1 -0
- package/dist/client-DRstnAkE.d.ts +374 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +317 -0
- package/dist/index.js.map +1 -0
- package/package.json +71 -4
|
@@ -0,0 +1,963 @@
|
|
|
1
|
+
// src/code.ts
|
|
2
|
+
var ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
|
|
3
|
+
function generateCode(length = 4) {
|
|
4
|
+
const bytes = new Uint8Array(length);
|
|
5
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
6
|
+
let out = "";
|
|
7
|
+
for (const b of bytes) out += ALPHABET[b % ALPHABET.length];
|
|
8
|
+
return out;
|
|
9
|
+
}
|
|
10
|
+
function normalizeCode(code) {
|
|
11
|
+
return code.trim().toUpperCase().replace(/[\s-]+/g, "");
|
|
12
|
+
}
|
|
13
|
+
function generateSessionId() {
|
|
14
|
+
const bytes = new Uint8Array(16);
|
|
15
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
16
|
+
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/errors.ts
|
|
20
|
+
var PeerodactylError = class extends Error {
|
|
21
|
+
code;
|
|
22
|
+
constructor(code, message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "PeerodactylError";
|
|
25
|
+
this.code = code;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/logger.ts
|
|
30
|
+
var defaultScreams = {
|
|
31
|
+
hostStarted: "SKREEEEEE!",
|
|
32
|
+
guestJoined: "gyaaaaaaaaa!",
|
|
33
|
+
guestLeft: "kraaaa\u2026",
|
|
34
|
+
hostLeft: "kraaaa\u2026 \u{1F4A4}",
|
|
35
|
+
guestAway: "skree\u2026? \u{1F319}",
|
|
36
|
+
guestBack: "gyaaaa! \u{1F504}",
|
|
37
|
+
reconnecting: "skreeee\u2026? \u{1F4E1}",
|
|
38
|
+
reconnected: "SKREEEE! \u2705",
|
|
39
|
+
error: "SKRAAAWWK!!! \u{1F4A5}"
|
|
40
|
+
};
|
|
41
|
+
var COLORS = {
|
|
42
|
+
hostStarted: "#2ecc71",
|
|
43
|
+
guestJoined: "#3498db",
|
|
44
|
+
guestLeft: "#95a5a6",
|
|
45
|
+
hostLeft: "#95a5a6",
|
|
46
|
+
guestAway: "#f1c40f",
|
|
47
|
+
guestBack: "#1abc9c",
|
|
48
|
+
reconnecting: "#f1c40f",
|
|
49
|
+
reconnected: "#1abc9c",
|
|
50
|
+
error: "#e74c3c"
|
|
51
|
+
};
|
|
52
|
+
function createLogger(debug, screams) {
|
|
53
|
+
const words = { ...defaultScreams, ...screams };
|
|
54
|
+
return {
|
|
55
|
+
log(kind, text) {
|
|
56
|
+
if (!debug) return;
|
|
57
|
+
const out = kind === "error" ? console.error : console.log;
|
|
58
|
+
out(
|
|
59
|
+
`%c\u{1F985} peerodactyl%c ${text} ${words[kind]}`,
|
|
60
|
+
`color:${COLORS[kind]};font-weight:bold`,
|
|
61
|
+
"color:inherit"
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/protocol.ts
|
|
68
|
+
var PROTOCOL_VERSION = 2;
|
|
69
|
+
var TAG = "peerodactyl";
|
|
70
|
+
function msg(body) {
|
|
71
|
+
return { p: TAG, v: PROTOCOL_VERSION, ...body };
|
|
72
|
+
}
|
|
73
|
+
function isMsg(m) {
|
|
74
|
+
return typeof m === "object" && m !== null && m.p === TAG && typeof m.t === "string";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/emitter.ts
|
|
78
|
+
var Emitter = class {
|
|
79
|
+
listeners = /* @__PURE__ */ new Map();
|
|
80
|
+
on(event, cb) {
|
|
81
|
+
let set = this.listeners.get(event);
|
|
82
|
+
if (!set) {
|
|
83
|
+
set = /* @__PURE__ */ new Set();
|
|
84
|
+
this.listeners.set(event, set);
|
|
85
|
+
}
|
|
86
|
+
set.add(cb);
|
|
87
|
+
return () => {
|
|
88
|
+
set.delete(cb);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
fire(event, ...args) {
|
|
92
|
+
const set = this.listeners.get(event);
|
|
93
|
+
if (!set) return;
|
|
94
|
+
for (const cb of [...set]) {
|
|
95
|
+
try {
|
|
96
|
+
;
|
|
97
|
+
cb(...args);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
console.error("[peerodactyl] listener threw", err);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
clearListeners() {
|
|
104
|
+
this.listeners.clear();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// src/timers.ts
|
|
109
|
+
function unref(timer) {
|
|
110
|
+
;
|
|
111
|
+
timer.unref?.();
|
|
112
|
+
return timer;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/guest.ts
|
|
116
|
+
var GuestRoom = class _GuestRoom extends Emitter {
|
|
117
|
+
role = "guest";
|
|
118
|
+
code;
|
|
119
|
+
/** Stable session id: the id the host sees for you, even after reconnecting. */
|
|
120
|
+
selfId;
|
|
121
|
+
/** True if the host already knew this session id when we joined (e.g. after a page reload). */
|
|
122
|
+
resumed;
|
|
123
|
+
factory;
|
|
124
|
+
log;
|
|
125
|
+
reconnectEnabled;
|
|
126
|
+
reconnectTimeoutMs;
|
|
127
|
+
transport;
|
|
128
|
+
_hostId;
|
|
129
|
+
_snapshot;
|
|
130
|
+
_state = "connected";
|
|
131
|
+
_latency = null;
|
|
132
|
+
hbMs;
|
|
133
|
+
timeoutMs;
|
|
134
|
+
lastHostSeen = Date.now();
|
|
135
|
+
pingN = 0;
|
|
136
|
+
leaveTimer;
|
|
137
|
+
unsubs = [];
|
|
138
|
+
hbTimer;
|
|
139
|
+
retryTimer;
|
|
140
|
+
helloTimer;
|
|
141
|
+
deadlineTimer;
|
|
142
|
+
attemptToken = 0;
|
|
143
|
+
onOnline = () => this.nudge();
|
|
144
|
+
onVisibility = () => {
|
|
145
|
+
if (typeof document !== "undefined" && document.visibilityState === "visible") this.nudge();
|
|
146
|
+
};
|
|
147
|
+
constructor(init) {
|
|
148
|
+
super();
|
|
149
|
+
this.factory = init.factory;
|
|
150
|
+
this.transport = init.transport;
|
|
151
|
+
this.code = init.code;
|
|
152
|
+
this.selfId = init.sid;
|
|
153
|
+
this._hostId = init.hostId;
|
|
154
|
+
this._snapshot = init.welcome.snapshot;
|
|
155
|
+
this.resumed = init.welcome.resumed;
|
|
156
|
+
this.reconnectEnabled = init.reconnect;
|
|
157
|
+
this.reconnectTimeoutMs = init.reconnectTimeoutMs;
|
|
158
|
+
this.log = init.log;
|
|
159
|
+
this.hbMs = init.welcome.hb;
|
|
160
|
+
this.timeoutMs = init.welcome.timeout;
|
|
161
|
+
this.attach(this.transport);
|
|
162
|
+
this.startHeartbeat();
|
|
163
|
+
if (typeof addEventListener === "function") addEventListener("online", this.onOnline);
|
|
164
|
+
if (typeof document !== "undefined") {
|
|
165
|
+
document.addEventListener("visibilitychange", this.onVisibility);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
get hostId() {
|
|
169
|
+
return this._hostId;
|
|
170
|
+
}
|
|
171
|
+
/** The most recent state the host sent. */
|
|
172
|
+
get snapshot() {
|
|
173
|
+
return this._snapshot;
|
|
174
|
+
}
|
|
175
|
+
get state() {
|
|
176
|
+
return this._state;
|
|
177
|
+
}
|
|
178
|
+
/** Round-trip time to the host in ms, measured by the heartbeat (null until the first pong). */
|
|
179
|
+
get latency() {
|
|
180
|
+
return this._latency;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Look for the host of `code` and join it. Rejects with a PeerodactylError
|
|
184
|
+
* (`host-not-found`, `room-full`, `version-mismatch`).
|
|
185
|
+
*/
|
|
186
|
+
static async join(opts) {
|
|
187
|
+
const code = normalizeCode(opts.code);
|
|
188
|
+
const sid = opts.sessionId ?? generateSessionId();
|
|
189
|
+
const log = createLogger(opts.debug ?? false, opts.screams);
|
|
190
|
+
const transport = await opts.transport(code);
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
let settled = false;
|
|
193
|
+
const cleanup = [];
|
|
194
|
+
const fail = (err) => {
|
|
195
|
+
if (settled) return;
|
|
196
|
+
settled = true;
|
|
197
|
+
for (const off of cleanup) off();
|
|
198
|
+
transport.leave();
|
|
199
|
+
log.log("error", err.message);
|
|
200
|
+
reject(err);
|
|
201
|
+
};
|
|
202
|
+
const hello = () => {
|
|
203
|
+
if (!settled) transport.send(msg({ t: "hello", sid }));
|
|
204
|
+
};
|
|
205
|
+
cleanup.push(
|
|
206
|
+
// Peers appear one at a time: say hello to each newcomer, the host answers.
|
|
207
|
+
transport.onPeerJoin(hello),
|
|
208
|
+
transport.onMessage((m, from) => {
|
|
209
|
+
if (settled || !isMsg(m)) return;
|
|
210
|
+
if (m.t === "welcome") {
|
|
211
|
+
settled = true;
|
|
212
|
+
for (const off of cleanup) off();
|
|
213
|
+
const room = new _GuestRoom({
|
|
214
|
+
factory: opts.transport,
|
|
215
|
+
transport,
|
|
216
|
+
code,
|
|
217
|
+
sid,
|
|
218
|
+
hostId: from,
|
|
219
|
+
welcome: m,
|
|
220
|
+
reconnect: opts.reconnect ?? true,
|
|
221
|
+
reconnectTimeoutMs: opts.reconnectTimeoutMs ?? 3e4,
|
|
222
|
+
log
|
|
223
|
+
});
|
|
224
|
+
log.log("guestJoined", `joined room ${code}`);
|
|
225
|
+
resolve(room);
|
|
226
|
+
} else if (m.t === "reject") {
|
|
227
|
+
fail(
|
|
228
|
+
m.reason === "full" ? new PeerodactylError("room-full", `Room ${code} is full`) : new PeerodactylError("version-mismatch", "Host speaks a different protocol version")
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
})
|
|
232
|
+
);
|
|
233
|
+
const retry = setInterval(hello, 1e3);
|
|
234
|
+
const timer = setTimeout(
|
|
235
|
+
() => fail(new PeerodactylError("host-not-found", `No host found for room ${code}`)),
|
|
236
|
+
opts.timeoutMs ?? 1e4
|
|
237
|
+
);
|
|
238
|
+
cleanup.push(
|
|
239
|
+
() => clearInterval(retry),
|
|
240
|
+
() => clearTimeout(timer)
|
|
241
|
+
);
|
|
242
|
+
hello();
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
/** Ask the host to do something. The host decides what actually happens. */
|
|
246
|
+
sendIntent(intent) {
|
|
247
|
+
if (this._state !== "connected") return;
|
|
248
|
+
this.transport.send(msg({ t: "intent", d: intent }), this._hostId);
|
|
249
|
+
}
|
|
250
|
+
/** Leave the room for good (the host frees your seat immediately). */
|
|
251
|
+
leave() {
|
|
252
|
+
if (this._state === "closed") return;
|
|
253
|
+
this.transport.send(msg({ t: "bye" }), this._hostId);
|
|
254
|
+
this.shutdown();
|
|
255
|
+
}
|
|
256
|
+
/* ------------------------------------------------------------------ */
|
|
257
|
+
attach(t) {
|
|
258
|
+
this.unsubs.push(
|
|
259
|
+
t.onMessage((m, from) => this.handle(m, from)),
|
|
260
|
+
t.onPeerLeave((id) => {
|
|
261
|
+
if (id !== this._hostId || this._state !== "connected") return;
|
|
262
|
+
clearTimeout(this.leaveTimer);
|
|
263
|
+
this.leaveTimer = unref(
|
|
264
|
+
setTimeout(() => {
|
|
265
|
+
if (this._state === "connected") this.startReconnect();
|
|
266
|
+
}, 150)
|
|
267
|
+
);
|
|
268
|
+
})
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
detach() {
|
|
272
|
+
for (const off of this.unsubs) off();
|
|
273
|
+
this.unsubs = [];
|
|
274
|
+
}
|
|
275
|
+
handle(m, from) {
|
|
276
|
+
if (this._state === "closed" || !isMsg(m)) return;
|
|
277
|
+
if (m.t === "welcome") {
|
|
278
|
+
this.onWelcome(m, from);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (m.t === "bye" && from === this._hostId) {
|
|
282
|
+
this.hostGone(m.reason === "kicked" ? "kicked" : "closed");
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (this._state !== "connected" || from !== this._hostId) return;
|
|
286
|
+
this.lastHostSeen = Date.now();
|
|
287
|
+
switch (m.t) {
|
|
288
|
+
case "event":
|
|
289
|
+
this.fire("event", m.d);
|
|
290
|
+
return;
|
|
291
|
+
case "snapshot":
|
|
292
|
+
this._snapshot = m.d;
|
|
293
|
+
this.fire("snapshot", this._snapshot);
|
|
294
|
+
return;
|
|
295
|
+
case "pong":
|
|
296
|
+
this._latency = Math.max(0, Date.now() - m.ts);
|
|
297
|
+
return;
|
|
298
|
+
case "bye":
|
|
299
|
+
this.hostGone(m.reason === "kicked" ? "kicked" : "closed");
|
|
300
|
+
return;
|
|
301
|
+
default:
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
onWelcome(m, from) {
|
|
306
|
+
if (this._state === "reconnecting") {
|
|
307
|
+
this._hostId = from;
|
|
308
|
+
this.applyWelcome(m);
|
|
309
|
+
this.stopReconnectTimers();
|
|
310
|
+
this._state = "connected";
|
|
311
|
+
this.log.log("reconnected", `back in room ${this.code}`);
|
|
312
|
+
this.fire("snapshot", this._snapshot);
|
|
313
|
+
this.fire("reconnected", m.resumed);
|
|
314
|
+
} else if (this._state === "connected" && from === this._hostId && m.resumed) {
|
|
315
|
+
this.applyWelcome(m);
|
|
316
|
+
this.fire("snapshot", this._snapshot);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
applyWelcome(m) {
|
|
320
|
+
this.lastHostSeen = Date.now();
|
|
321
|
+
this._snapshot = m.snapshot;
|
|
322
|
+
if (m.hb !== this.hbMs || m.timeout !== this.timeoutMs) {
|
|
323
|
+
this.hbMs = m.hb;
|
|
324
|
+
this.timeoutMs = m.timeout;
|
|
325
|
+
this.startHeartbeat();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
/* ----------------------------- heartbeat ---------------------------- */
|
|
329
|
+
startHeartbeat() {
|
|
330
|
+
clearInterval(this.hbTimer);
|
|
331
|
+
this.hbTimer = unref(setInterval(() => this.tick(), this.hbMs));
|
|
332
|
+
}
|
|
333
|
+
/** Watchdog + ping. Also called when the tab wakes up or the network returns. */
|
|
334
|
+
tick() {
|
|
335
|
+
if (this._state !== "connected") return;
|
|
336
|
+
if (Date.now() - this.lastHostSeen > this.timeoutMs) {
|
|
337
|
+
this.startReconnect();
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
this.transport.send(msg({ t: "ping", n: ++this.pingN, ts: Date.now() }), this._hostId);
|
|
341
|
+
}
|
|
342
|
+
/** Something suggests the connection state changed (tab visible, network back): check now. */
|
|
343
|
+
nudge() {
|
|
344
|
+
if (this._state === "connected") this.tick();
|
|
345
|
+
else if (this._state === "reconnecting") {
|
|
346
|
+
this.stopAttemptTimers();
|
|
347
|
+
void this.attempt(0);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/* --------------------------- reconnection --------------------------- */
|
|
351
|
+
startReconnect() {
|
|
352
|
+
if (this._state !== "connected") return;
|
|
353
|
+
if (!this.reconnectEnabled) {
|
|
354
|
+
this.hostGone("lost");
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
this._state = "reconnecting";
|
|
358
|
+
this.log.log("reconnecting", `lost the host, trying to get back into ${this.code}`);
|
|
359
|
+
this.deadlineTimer = unref(setTimeout(() => this.hostGone("lost"), this.reconnectTimeoutMs));
|
|
360
|
+
this.fire("reconnecting");
|
|
361
|
+
void this.attempt(0);
|
|
362
|
+
}
|
|
363
|
+
async attempt(n) {
|
|
364
|
+
if (this._state !== "reconnecting") return;
|
|
365
|
+
const token = ++this.attemptToken;
|
|
366
|
+
this.stopAttemptTimers();
|
|
367
|
+
this.detach();
|
|
368
|
+
this.transport.leave();
|
|
369
|
+
let t;
|
|
370
|
+
try {
|
|
371
|
+
t = await this.factory(this.code);
|
|
372
|
+
} catch (err) {
|
|
373
|
+
this.log.log("error", `reconnect attempt failed: ${String(err)}`);
|
|
374
|
+
if (token === this.attemptToken && this._state === "reconnecting") this.scheduleRetry(n);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (this._state !== "reconnecting" || token !== this.attemptToken) {
|
|
378
|
+
t.leave();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
this.transport = t;
|
|
382
|
+
this.attach(t);
|
|
383
|
+
const hello = () => {
|
|
384
|
+
if (this._state === "reconnecting" && this.transport === t) {
|
|
385
|
+
t.send(msg({ t: "hello", sid: this.selfId }));
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
this.unsubs.push(t.onPeerJoin(hello));
|
|
389
|
+
hello();
|
|
390
|
+
this.helloTimer = unref(setInterval(hello, 1e3));
|
|
391
|
+
this.scheduleRetry(n);
|
|
392
|
+
}
|
|
393
|
+
scheduleRetry(n) {
|
|
394
|
+
const wait = Math.min(4e3 + n * 2e3, 1e4);
|
|
395
|
+
this.retryTimer = unref(setTimeout(() => void this.attempt(n + 1), wait));
|
|
396
|
+
}
|
|
397
|
+
stopAttemptTimers() {
|
|
398
|
+
clearTimeout(this.retryTimer);
|
|
399
|
+
clearInterval(this.helloTimer);
|
|
400
|
+
this.retryTimer = void 0;
|
|
401
|
+
this.helloTimer = void 0;
|
|
402
|
+
}
|
|
403
|
+
stopReconnectTimers() {
|
|
404
|
+
this.stopAttemptTimers();
|
|
405
|
+
clearTimeout(this.deadlineTimer);
|
|
406
|
+
this.deadlineTimer = void 0;
|
|
407
|
+
}
|
|
408
|
+
/* ------------------------------ teardown ---------------------------- */
|
|
409
|
+
hostGone(reason) {
|
|
410
|
+
if (this._state === "closed") return;
|
|
411
|
+
this.log.log("hostLeft", `host ${this._hostId.slice(0, 6)} left (${reason})`);
|
|
412
|
+
this.fire("hostLeft", reason);
|
|
413
|
+
this.shutdown();
|
|
414
|
+
}
|
|
415
|
+
shutdown() {
|
|
416
|
+
this._state = "closed";
|
|
417
|
+
clearTimeout(this.leaveTimer);
|
|
418
|
+
clearInterval(this.hbTimer);
|
|
419
|
+
this.stopReconnectTimers();
|
|
420
|
+
this.attemptToken++;
|
|
421
|
+
if (typeof removeEventListener === "function") removeEventListener("online", this.onOnline);
|
|
422
|
+
if (typeof document !== "undefined") {
|
|
423
|
+
document.removeEventListener("visibilitychange", this.onVisibility);
|
|
424
|
+
}
|
|
425
|
+
this.detach();
|
|
426
|
+
this.clearListeners();
|
|
427
|
+
this.transport.leave();
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
// src/host.ts
|
|
432
|
+
var HostRoom = class _HostRoom extends Emitter {
|
|
433
|
+
role = "host";
|
|
434
|
+
code;
|
|
435
|
+
selfId;
|
|
436
|
+
transport;
|
|
437
|
+
members = /* @__PURE__ */ new Map();
|
|
438
|
+
byPeer = /* @__PURE__ */ new Map();
|
|
439
|
+
maxGuests;
|
|
440
|
+
heartbeatMs;
|
|
441
|
+
timeoutMs;
|
|
442
|
+
graceMs;
|
|
443
|
+
log;
|
|
444
|
+
snapshotProvider;
|
|
445
|
+
closed = false;
|
|
446
|
+
unsubs = [];
|
|
447
|
+
sweeper;
|
|
448
|
+
constructor(opts, code, transport) {
|
|
449
|
+
super();
|
|
450
|
+
this.code = code;
|
|
451
|
+
this.transport = transport;
|
|
452
|
+
this.maxGuests = opts.maxGuests ?? Infinity;
|
|
453
|
+
this.heartbeatMs = opts.heartbeatMs ?? 5e3;
|
|
454
|
+
this.timeoutMs = opts.timeoutMs ?? this.heartbeatMs * 3;
|
|
455
|
+
this.graceMs = opts.graceMs ?? 6e4;
|
|
456
|
+
this.log = createLogger(opts.debug ?? false, opts.screams);
|
|
457
|
+
this.selfId = transport.selfId;
|
|
458
|
+
this.unsubs.push(
|
|
459
|
+
this.transport.onMessage((m, from) => this.handle(m, from)),
|
|
460
|
+
this.transport.onPeerLeave((peerId) => this.onPeerLeave(peerId))
|
|
461
|
+
);
|
|
462
|
+
this.sweeper = unref(
|
|
463
|
+
setInterval(() => this.sweep(), Math.max(100, Math.floor(this.heartbeatMs / 2)))
|
|
464
|
+
);
|
|
465
|
+
this.log.log("hostStarted", `host connects, room ${this.code}`);
|
|
466
|
+
}
|
|
467
|
+
static async create(opts) {
|
|
468
|
+
const code = opts.code ? normalizeCode(opts.code) : generateCode();
|
|
469
|
+
const transport = await opts.transport(code);
|
|
470
|
+
return new _HostRoom(opts, code, transport);
|
|
471
|
+
}
|
|
472
|
+
/** Ids of the guests that are connected right now. */
|
|
473
|
+
get guests() {
|
|
474
|
+
return [...this.members.values()].filter((m) => !m.away).map((m) => m.id);
|
|
475
|
+
}
|
|
476
|
+
/** Ids of the guests that lost their connection but still hold a seat. */
|
|
477
|
+
get awayGuests() {
|
|
478
|
+
return [...this.members.values()].filter((m) => m.away).map((m) => m.id);
|
|
479
|
+
}
|
|
480
|
+
isAway(id) {
|
|
481
|
+
return this.members.get(id)?.away ?? false;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Tells peerodactyl how to describe the current game state. It is sent to
|
|
485
|
+
* every guest as soon as they join or come back (and on `broadcastSnapshot()`).
|
|
486
|
+
*/
|
|
487
|
+
setSnapshot(provider) {
|
|
488
|
+
this.snapshotProvider = provider;
|
|
489
|
+
}
|
|
490
|
+
/** Send an event to every connected guest. Away guests miss it: they get a fresh snapshot when they return. */
|
|
491
|
+
broadcast(event) {
|
|
492
|
+
if (this.closed) return;
|
|
493
|
+
for (const m of this.members.values()) {
|
|
494
|
+
if (!m.away) this.transport.send(msg({ t: "event", d: event }), m.peerId);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/** Send an event to a single guest (ignored while that guest is away). */
|
|
498
|
+
sendTo(id, event) {
|
|
499
|
+
const m = this.members.get(id);
|
|
500
|
+
if (this.closed || !m || m.away) return;
|
|
501
|
+
this.transport.send(msg({ t: "event", d: event }), m.peerId);
|
|
502
|
+
}
|
|
503
|
+
/** Push the full current state to every connected guest. */
|
|
504
|
+
broadcastSnapshot() {
|
|
505
|
+
if (this.closed || !this.snapshotProvider) return;
|
|
506
|
+
const snapshot = this.snapshotProvider();
|
|
507
|
+
for (const m of this.members.values()) {
|
|
508
|
+
if (!m.away) this.transport.send(msg({ t: "snapshot", d: snapshot }), m.peerId);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
/** Remove a guest from the room for good. */
|
|
512
|
+
kick(id) {
|
|
513
|
+
const m = this.members.get(id);
|
|
514
|
+
if (!m) return;
|
|
515
|
+
this.transport.send(msg({ t: "bye", reason: "kicked" }), m.peerId);
|
|
516
|
+
this.remove(id, "kicked");
|
|
517
|
+
}
|
|
518
|
+
close() {
|
|
519
|
+
if (this.closed) return;
|
|
520
|
+
this.transport.send(msg({ t: "bye" }));
|
|
521
|
+
this.closed = true;
|
|
522
|
+
clearInterval(this.sweeper);
|
|
523
|
+
for (const m of this.members.values()) clearTimeout(m.graceTimer);
|
|
524
|
+
for (const off of this.unsubs) off();
|
|
525
|
+
this.members.clear();
|
|
526
|
+
this.byPeer.clear();
|
|
527
|
+
this.clearListeners();
|
|
528
|
+
this.transport.leave();
|
|
529
|
+
}
|
|
530
|
+
/* ------------------------------------------------------------------ */
|
|
531
|
+
handle(m, from) {
|
|
532
|
+
if (this.closed || !isMsg(m)) return;
|
|
533
|
+
if (m.t === "hello") {
|
|
534
|
+
this.onHello(m.v, m.sid, from);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const id = this.byPeer.get(from);
|
|
538
|
+
const member = id !== void 0 ? this.members.get(id) : void 0;
|
|
539
|
+
if (!member || member.peerId !== from) return;
|
|
540
|
+
if (m.t === "bye") {
|
|
541
|
+
this.remove(member.id, "bye");
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
member.lastSeen = Date.now();
|
|
545
|
+
if (member.away) this.rebind(member, from);
|
|
546
|
+
switch (m.t) {
|
|
547
|
+
case "ping":
|
|
548
|
+
this.transport.send(msg({ t: "pong", n: m.n, ts: m.ts }), from);
|
|
549
|
+
return;
|
|
550
|
+
case "intent":
|
|
551
|
+
this.fire("intent", m.d, member.id);
|
|
552
|
+
return;
|
|
553
|
+
default:
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
onHello(version, sid, from) {
|
|
558
|
+
if (version !== PROTOCOL_VERSION) {
|
|
559
|
+
this.transport.send(msg({ t: "reject", reason: "version" }), from);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
if (typeof sid !== "string" || sid.length === 0) return;
|
|
563
|
+
const existing = this.members.get(sid);
|
|
564
|
+
if (existing) {
|
|
565
|
+
existing.lastSeen = Date.now();
|
|
566
|
+
this.rebind(existing, from);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
if (this.members.size >= this.maxGuests) {
|
|
570
|
+
this.transport.send(msg({ t: "reject", reason: "full" }), from);
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const member = {
|
|
574
|
+
id: sid,
|
|
575
|
+
peerId: from,
|
|
576
|
+
lastSeen: Date.now(),
|
|
577
|
+
away: false,
|
|
578
|
+
graceTimer: void 0
|
|
579
|
+
};
|
|
580
|
+
this.members.set(sid, member);
|
|
581
|
+
this.byPeer.set(from, sid);
|
|
582
|
+
this.welcome(member, false);
|
|
583
|
+
this.log.log("guestJoined", `guest ${shortId(sid)} joins`);
|
|
584
|
+
this.fire("guestJoin", sid);
|
|
585
|
+
}
|
|
586
|
+
/** Point an existing seat at `peerId` and (re)send the welcome with a fresh snapshot. */
|
|
587
|
+
rebind(member, peerId) {
|
|
588
|
+
const peerChanged = member.peerId !== peerId;
|
|
589
|
+
const wasAway = member.away;
|
|
590
|
+
if (peerChanged) {
|
|
591
|
+
this.byPeer.delete(member.peerId);
|
|
592
|
+
member.peerId = peerId;
|
|
593
|
+
this.byPeer.set(peerId, member.id);
|
|
594
|
+
}
|
|
595
|
+
if (wasAway) {
|
|
596
|
+
member.away = false;
|
|
597
|
+
clearTimeout(member.graceTimer);
|
|
598
|
+
member.graceTimer = void 0;
|
|
599
|
+
this.log.log("guestBack", `guest ${shortId(member.id)} is back`);
|
|
600
|
+
}
|
|
601
|
+
this.welcome(member, peerChanged || wasAway);
|
|
602
|
+
if (wasAway) this.fire("guestBack", member.id);
|
|
603
|
+
}
|
|
604
|
+
welcome(member, resumed) {
|
|
605
|
+
const snapshot = this.snapshotProvider ? this.snapshotProvider() : null;
|
|
606
|
+
this.transport.send(
|
|
607
|
+
msg({
|
|
608
|
+
t: "welcome",
|
|
609
|
+
hostId: this.selfId,
|
|
610
|
+
snapshot,
|
|
611
|
+
resumed,
|
|
612
|
+
hb: this.heartbeatMs,
|
|
613
|
+
timeout: this.timeoutMs
|
|
614
|
+
}),
|
|
615
|
+
member.peerId
|
|
616
|
+
);
|
|
617
|
+
}
|
|
618
|
+
onPeerLeave(peerId) {
|
|
619
|
+
const id = this.byPeer.get(peerId);
|
|
620
|
+
const member = id !== void 0 ? this.members.get(id) : void 0;
|
|
621
|
+
if (member && member.peerId === peerId && !member.away) this.markAway(member);
|
|
622
|
+
}
|
|
623
|
+
sweep() {
|
|
624
|
+
if (this.closed) return;
|
|
625
|
+
const now = Date.now();
|
|
626
|
+
for (const m of [...this.members.values()]) {
|
|
627
|
+
if (!m.away && now - m.lastSeen > this.timeoutMs) this.markAway(m);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
markAway(member) {
|
|
631
|
+
member.away = true;
|
|
632
|
+
member.graceTimer = unref(setTimeout(() => this.remove(member.id, "timeout"), this.graceMs));
|
|
633
|
+
this.log.log("guestAway", `guest ${shortId(member.id)} lost connection`);
|
|
634
|
+
this.fire("guestAway", member.id);
|
|
635
|
+
}
|
|
636
|
+
remove(id, reason) {
|
|
637
|
+
const member = this.members.get(id);
|
|
638
|
+
if (!member) return;
|
|
639
|
+
clearTimeout(member.graceTimer);
|
|
640
|
+
this.members.delete(id);
|
|
641
|
+
if (this.byPeer.get(member.peerId) === id) this.byPeer.delete(member.peerId);
|
|
642
|
+
this.log.log("guestLeft", `guest ${shortId(id)} leaves (${reason})`);
|
|
643
|
+
this.fire("guestLeave", id, reason);
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
var shortId = (id) => id.slice(0, 6);
|
|
647
|
+
|
|
648
|
+
// src/client.ts
|
|
649
|
+
var IDLE = () => ({
|
|
650
|
+
status: "idle",
|
|
651
|
+
role: null,
|
|
652
|
+
code: null,
|
|
653
|
+
selfId: null,
|
|
654
|
+
hostId: null,
|
|
655
|
+
players: [],
|
|
656
|
+
latency: null,
|
|
657
|
+
snapshot: null,
|
|
658
|
+
resumed: false,
|
|
659
|
+
endedReason: null
|
|
660
|
+
});
|
|
661
|
+
var PeerodactylClient = class extends Emitter {
|
|
662
|
+
constructor(opts) {
|
|
663
|
+
super();
|
|
664
|
+
this.opts = opts;
|
|
665
|
+
}
|
|
666
|
+
opts;
|
|
667
|
+
_state = IDLE();
|
|
668
|
+
subscribers = /* @__PURE__ */ new Set();
|
|
669
|
+
room = null;
|
|
670
|
+
roomUnsubs = [];
|
|
671
|
+
snapshotProvider;
|
|
672
|
+
latencyTimer;
|
|
673
|
+
/** Bumped on every leave/start, so late results of an abandoned attempt are ignored. */
|
|
674
|
+
epoch = 0;
|
|
675
|
+
/* ------------------------------ state ------------------------------ */
|
|
676
|
+
get state() {
|
|
677
|
+
return this._state;
|
|
678
|
+
}
|
|
679
|
+
/** Listen to every state change. Returns the unsubscribe function. */
|
|
680
|
+
subscribe(listener) {
|
|
681
|
+
this.subscribers.add(listener);
|
|
682
|
+
return () => {
|
|
683
|
+
this.subscribers.delete(listener);
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
get isHost() {
|
|
687
|
+
return this._state.role === "host";
|
|
688
|
+
}
|
|
689
|
+
set(patch) {
|
|
690
|
+
this._state = { ...this._state, ...patch };
|
|
691
|
+
for (const cb of [...this.subscribers]) {
|
|
692
|
+
try {
|
|
693
|
+
cb(this._state);
|
|
694
|
+
} catch (err) {
|
|
695
|
+
console.error("[peerodactyl] subscriber threw", err);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
/* ------------------------------ rooms ------------------------------ */
|
|
700
|
+
/** Open a room as the host. Resolves with the room code. */
|
|
701
|
+
async hostRoom(options = {}) {
|
|
702
|
+
this.leave();
|
|
703
|
+
const epoch = ++this.epoch;
|
|
704
|
+
this.set({ ...IDLE(), status: "connecting" });
|
|
705
|
+
try {
|
|
706
|
+
const room = await HostRoom.create({
|
|
707
|
+
transport: this.opts.transport,
|
|
708
|
+
...options.code !== void 0 ? { code: options.code } : {},
|
|
709
|
+
...pick(this.opts, ["maxGuests", "heartbeatMs", "timeoutMs", "graceMs", "debug", "screams"])
|
|
710
|
+
});
|
|
711
|
+
if (epoch !== this.epoch) {
|
|
712
|
+
room.close();
|
|
713
|
+
throw new PeerodactylError("transport", "Room opening was cancelled");
|
|
714
|
+
}
|
|
715
|
+
this.room = room;
|
|
716
|
+
if (this.snapshotProvider) room.setSnapshot(this.snapshotProvider);
|
|
717
|
+
this.wireHost(room);
|
|
718
|
+
this.set({
|
|
719
|
+
status: "hosting",
|
|
720
|
+
role: "host",
|
|
721
|
+
code: room.code,
|
|
722
|
+
selfId: room.selfId,
|
|
723
|
+
hostId: room.selfId,
|
|
724
|
+
players: this.hostPlayers(room)
|
|
725
|
+
});
|
|
726
|
+
return room.code;
|
|
727
|
+
} catch (err) {
|
|
728
|
+
if (epoch === this.epoch) this.set(IDLE());
|
|
729
|
+
throw err;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Join a room as a guest. Pass `sessionId` to get a previous seat back (see `rejoin()`).
|
|
734
|
+
* Rejects with a `PeerodactylError` (`host-not-found`, `room-full`, `version-mismatch`).
|
|
735
|
+
*/
|
|
736
|
+
async joinRoom(code, options = {}) {
|
|
737
|
+
this.leave();
|
|
738
|
+
const epoch = ++this.epoch;
|
|
739
|
+
this.set({ ...IDLE(), status: "connecting" });
|
|
740
|
+
try {
|
|
741
|
+
const room = await GuestRoom.join({
|
|
742
|
+
transport: this.opts.transport,
|
|
743
|
+
code,
|
|
744
|
+
...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {},
|
|
745
|
+
...this.opts.joinTimeoutMs !== void 0 ? { timeoutMs: this.opts.joinTimeoutMs } : {},
|
|
746
|
+
...pick(this.opts, ["reconnect", "reconnectTimeoutMs", "debug", "screams"])
|
|
747
|
+
});
|
|
748
|
+
if (epoch !== this.epoch) {
|
|
749
|
+
room.leave();
|
|
750
|
+
throw new PeerodactylError("transport", "Joining was cancelled");
|
|
751
|
+
}
|
|
752
|
+
this.room = room;
|
|
753
|
+
this.wireGuest(room);
|
|
754
|
+
this.saveSession(room.code, room.selfId);
|
|
755
|
+
this.set({
|
|
756
|
+
status: "connected",
|
|
757
|
+
role: "guest",
|
|
758
|
+
code: room.code,
|
|
759
|
+
selfId: room.selfId,
|
|
760
|
+
hostId: room.hostId,
|
|
761
|
+
snapshot: room.snapshot,
|
|
762
|
+
resumed: room.resumed,
|
|
763
|
+
latency: room.latency
|
|
764
|
+
});
|
|
765
|
+
} catch (err) {
|
|
766
|
+
if (epoch === this.epoch) this.set(IDLE());
|
|
767
|
+
throw err;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
/** The room this tab was in before a reload, if it can still be rejoined. */
|
|
771
|
+
get savedSession() {
|
|
772
|
+
const key = this.storageKey();
|
|
773
|
+
if (!key) return null;
|
|
774
|
+
try {
|
|
775
|
+
const raw = sessionStorage.getItem(key);
|
|
776
|
+
const v = raw ? JSON.parse(raw) : null;
|
|
777
|
+
return v?.code && v.sid ? { code: v.code, sessionId: v.sid } : null;
|
|
778
|
+
} catch {
|
|
779
|
+
return null;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
/** Try to get the previous seat back (after a reload). Resolves false if there is nothing to rejoin. */
|
|
783
|
+
async rejoin() {
|
|
784
|
+
const saved = this.savedSession;
|
|
785
|
+
if (!saved) return false;
|
|
786
|
+
try {
|
|
787
|
+
await this.joinRoom(saved.code, { sessionId: saved.sessionId });
|
|
788
|
+
return true;
|
|
789
|
+
} catch (err) {
|
|
790
|
+
this.clearSession();
|
|
791
|
+
if (err instanceof PeerodactylError && err.code === "host-not-found") return false;
|
|
792
|
+
throw err;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
/** Leave on purpose: a guest frees its seat at once, a host closes the room. Safe to call anytime. */
|
|
796
|
+
leave() {
|
|
797
|
+
this.epoch++;
|
|
798
|
+
const room = this.room;
|
|
799
|
+
this.room = null;
|
|
800
|
+
this.detach();
|
|
801
|
+
if (!room) {
|
|
802
|
+
if (this._state.status !== "idle") this.set(IDLE());
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
this.clearSession();
|
|
806
|
+
if (room.role === "host") room.close();
|
|
807
|
+
else room.leave();
|
|
808
|
+
this.set(IDLE());
|
|
809
|
+
}
|
|
810
|
+
/* --------------------------- talking ------------------------------- */
|
|
811
|
+
/**
|
|
812
|
+
* Ask the host to do something. A guest sends it over the network; the host delivers it
|
|
813
|
+
* to its own `intent` handler (as `from === selfId`), so the host plays by the same rules.
|
|
814
|
+
*/
|
|
815
|
+
sendIntent(intent) {
|
|
816
|
+
const room = this.room;
|
|
817
|
+
if (!room) return;
|
|
818
|
+
if (room.role === "host") this.fire("intent", intent, room.selfId);
|
|
819
|
+
else room.sendIntent(intent);
|
|
820
|
+
}
|
|
821
|
+
/** Host: send an event to every guest (and to your own `event` handler). */
|
|
822
|
+
broadcast(event) {
|
|
823
|
+
const room = this.hostOrNull();
|
|
824
|
+
if (!room) return;
|
|
825
|
+
room.broadcast(event);
|
|
826
|
+
this.fire("event", event);
|
|
827
|
+
}
|
|
828
|
+
/** Host: send an event to a single player. Sending to yourself fires your own `event` handler. */
|
|
829
|
+
sendTo(id, event) {
|
|
830
|
+
const room = this.hostOrNull();
|
|
831
|
+
if (!room) return;
|
|
832
|
+
if (id === room.selfId) this.fire("event", event);
|
|
833
|
+
else room.sendTo(id, event);
|
|
834
|
+
}
|
|
835
|
+
/** Host: how to build the full state. It is sent on join and after every reconnection. */
|
|
836
|
+
setSnapshot(provider) {
|
|
837
|
+
this.snapshotProvider = provider;
|
|
838
|
+
this.hostOrNull()?.setSnapshot(provider);
|
|
839
|
+
}
|
|
840
|
+
/** Host: push the full state to every guest now. */
|
|
841
|
+
broadcastSnapshot() {
|
|
842
|
+
this.hostOrNull()?.broadcastSnapshot();
|
|
843
|
+
}
|
|
844
|
+
/** Host: remove a guest. */
|
|
845
|
+
kick(id) {
|
|
846
|
+
this.hostOrNull()?.kick(id);
|
|
847
|
+
}
|
|
848
|
+
/* ------------------------------ wiring ----------------------------- */
|
|
849
|
+
wireHost(room) {
|
|
850
|
+
const refresh = () => {
|
|
851
|
+
if (this.room === room) this.set({ players: this.hostPlayers(room) });
|
|
852
|
+
};
|
|
853
|
+
this.roomUnsubs.push(
|
|
854
|
+
room.on("intent", (intent, from) => this.fire("intent", intent, from)),
|
|
855
|
+
room.on("guestJoin", (id) => {
|
|
856
|
+
refresh();
|
|
857
|
+
this.fire("guestJoin", id);
|
|
858
|
+
}),
|
|
859
|
+
room.on("guestAway", (id) => {
|
|
860
|
+
refresh();
|
|
861
|
+
this.fire("guestAway", id);
|
|
862
|
+
}),
|
|
863
|
+
room.on("guestBack", (id) => {
|
|
864
|
+
refresh();
|
|
865
|
+
this.fire("guestBack", id);
|
|
866
|
+
}),
|
|
867
|
+
room.on("guestLeave", (id, reason) => {
|
|
868
|
+
refresh();
|
|
869
|
+
this.fire("guestLeave", id, reason);
|
|
870
|
+
})
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
wireGuest(room) {
|
|
874
|
+
this.roomUnsubs.push(
|
|
875
|
+
room.on("event", (e) => this.fire("event", e)),
|
|
876
|
+
room.on("snapshot", (s) => {
|
|
877
|
+
this.set({ snapshot: s });
|
|
878
|
+
this.fire("snapshot", s);
|
|
879
|
+
}),
|
|
880
|
+
room.on("reconnecting", () => {
|
|
881
|
+
this.set({ status: "reconnecting" });
|
|
882
|
+
this.fire("reconnecting");
|
|
883
|
+
}),
|
|
884
|
+
room.on("reconnected", (resumed) => {
|
|
885
|
+
this.set({ status: "connected", resumed, hostId: room.hostId });
|
|
886
|
+
this.fire("reconnected", resumed);
|
|
887
|
+
}),
|
|
888
|
+
room.on("hostLeft", (reason) => {
|
|
889
|
+
this.room = null;
|
|
890
|
+
this.detach();
|
|
891
|
+
this.clearSession();
|
|
892
|
+
this.set({ ...IDLE(), endedReason: reason });
|
|
893
|
+
this.fire("ended", reason);
|
|
894
|
+
})
|
|
895
|
+
);
|
|
896
|
+
this.latencyTimer = setInterval(() => {
|
|
897
|
+
if (this.room === room && room.latency !== this._state.latency) {
|
|
898
|
+
this.set({ latency: room.latency });
|
|
899
|
+
}
|
|
900
|
+
}, 2e3);
|
|
901
|
+
this.latencyTimer.unref?.();
|
|
902
|
+
}
|
|
903
|
+
detach() {
|
|
904
|
+
for (const off of this.roomUnsubs.splice(0)) off();
|
|
905
|
+
clearInterval(this.latencyTimer);
|
|
906
|
+
this.latencyTimer = void 0;
|
|
907
|
+
}
|
|
908
|
+
hostOrNull() {
|
|
909
|
+
return this.room?.role === "host" ? this.room : null;
|
|
910
|
+
}
|
|
911
|
+
hostPlayers(room) {
|
|
912
|
+
const guest = (id, away) => ({
|
|
913
|
+
id,
|
|
914
|
+
isHost: false,
|
|
915
|
+
isSelf: false,
|
|
916
|
+
away
|
|
917
|
+
});
|
|
918
|
+
return [
|
|
919
|
+
{ id: room.selfId, isHost: true, isSelf: true, away: false },
|
|
920
|
+
...room.guests.map((id) => guest(id, false)),
|
|
921
|
+
...room.awayGuests.map((id) => guest(id, true))
|
|
922
|
+
];
|
|
923
|
+
}
|
|
924
|
+
/* ---------------------------- persistence --------------------------- */
|
|
925
|
+
storageKey() {
|
|
926
|
+
const k = this.opts.storageKey;
|
|
927
|
+
return k === false ? null : k ?? "peerodactyl-session";
|
|
928
|
+
}
|
|
929
|
+
saveSession(code, sid) {
|
|
930
|
+
const key = this.storageKey();
|
|
931
|
+
if (!key) return;
|
|
932
|
+
try {
|
|
933
|
+
sessionStorage.setItem(key, JSON.stringify({ code, sid }));
|
|
934
|
+
} catch {
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
clearSession() {
|
|
938
|
+
const key = this.storageKey();
|
|
939
|
+
if (!key) return;
|
|
940
|
+
try {
|
|
941
|
+
sessionStorage.removeItem(key);
|
|
942
|
+
} catch {
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
function pick(obj, keys) {
|
|
947
|
+
const out = {};
|
|
948
|
+
for (const k of keys) if (obj[k] !== void 0) out[k] = obj[k];
|
|
949
|
+
return out;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
export {
|
|
953
|
+
generateCode,
|
|
954
|
+
normalizeCode,
|
|
955
|
+
generateSessionId,
|
|
956
|
+
PeerodactylError,
|
|
957
|
+
defaultScreams,
|
|
958
|
+
PROTOCOL_VERSION,
|
|
959
|
+
GuestRoom,
|
|
960
|
+
HostRoom,
|
|
961
|
+
PeerodactylClient
|
|
962
|
+
};
|
|
963
|
+
//# sourceMappingURL=chunk-ULFDGPNT.js.map
|