@ksaa-nlp/gamehub-sdk 0.9.0 → 3.6.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/README.md +158 -373
- package/SPEC.md +869 -0
- package/dist/gamehub-sdk.js +168 -1191
- package/dist/gamehub-sdk.js.map +3 -3
- package/dist/src/api-client.d.ts +15 -5
- package/dist/src/api-client.d.ts.map +1 -1
- package/dist/src/api-client.js +21 -5
- package/dist/src/api-client.js.map +1 -1
- package/dist/src/bridge.d.ts +31 -14
- package/dist/src/bridge.d.ts.map +1 -1
- package/dist/src/bridge.js +148 -159
- package/dist/src/bridge.js.map +1 -1
- package/dist/src/constants.d.ts +8 -6
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +8 -6
- package/dist/src/constants.js.map +1 -1
- package/dist/src/init.d.ts +18 -12
- package/dist/src/init.d.ts.map +1 -1
- package/dist/src/init.js +23 -156
- package/dist/src/init.js.map +1 -1
- package/dist/src/main.d.ts +45 -85
- package/dist/src/main.d.ts.map +1 -1
- package/dist/src/main.js +33 -52
- package/dist/src/main.js.map +1 -1
- package/dist/src/match-context.d.ts.map +1 -1
- package/dist/src/match-context.js +0 -14
- package/dist/src/match-context.js.map +1 -1
- package/dist/src/server-dev-harness.d.ts +85 -0
- package/dist/src/server-dev-harness.d.ts.map +1 -0
- package/dist/src/server-dev-harness.js +233 -0
- package/dist/src/server-dev-harness.js.map +1 -0
- package/dist/src/server-entry.d.ts +127 -0
- package/dist/src/server-entry.d.ts.map +1 -0
- package/dist/src/server-entry.js +26 -0
- package/dist/src/server-entry.js.map +1 -0
- package/dist/src/session.d.ts +16 -9
- package/dist/src/session.d.ts.map +1 -1
- package/dist/src/session.js +38 -27
- package/dist/src/session.js.map +1 -1
- package/dist/src/state.d.ts +17 -55
- package/dist/src/state.d.ts.map +1 -1
- package/dist/src/state.js +13 -32
- package/dist/src/state.js.map +1 -1
- package/dist/src/utils.d.ts +3 -14
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +3 -31
- package/dist/src/utils.js.map +1 -1
- package/dist/types/bootstrap.d.ts +56 -36
- package/dist/types/bootstrap.d.ts.map +1 -1
- package/dist/types/question.d.ts +36 -0
- package/dist/types/question.d.ts.map +1 -0
- package/dist/types/question.js +21 -0
- package/dist/types/question.js.map +1 -0
- package/dist/types/session.d.ts +46 -20
- package/dist/types/session.d.ts.map +1 -1
- package/package.json +14 -4
- package/CHANGELOG.md +0 -545
- package/MIGRATION.md +0 -322
- package/dist/src/multiplayer-match.d.ts +0 -42
- package/dist/src/multiplayer-match.d.ts.map +0 -1
- package/dist/src/multiplayer-match.js +0 -551
- package/dist/src/multiplayer-match.js.map +0 -1
- package/dist/src/multiplayer.d.ts +0 -96
- package/dist/src/multiplayer.d.ts.map +0 -1
- package/dist/src/multiplayer.js +0 -536
- package/dist/src/multiplayer.js.map +0 -1
- package/dist/types/multiplayer-match.d.ts +0 -204
- package/dist/types/multiplayer-match.d.ts.map +0 -1
- package/dist/types/multiplayer-match.js +0 -15
- package/dist/types/multiplayer-match.js.map +0 -1
- package/dist/types/multiplayer.d.ts +0 -194
- package/dist/types/multiplayer.d.ts.map +0 -1
- package/dist/types/multiplayer.js +0 -2
- package/dist/types/multiplayer.js.map +0 -1
|
@@ -24,20 +24,6 @@ export async function fetchMatchContext() {
|
|
|
24
24
|
const data = unwrapResult(result);
|
|
25
25
|
if (result?.success && data) {
|
|
26
26
|
state.matchContext = data;
|
|
27
|
-
state.participant1Id =
|
|
28
|
-
data.participant1?.participantId || state.participant1Id;
|
|
29
|
-
state.participant2Id =
|
|
30
|
-
data.participant2?.participantId || state.participant2Id;
|
|
31
|
-
state.participant1MemberIds = Array.isArray(data.participant1?.members)
|
|
32
|
-
? data.participant1.members
|
|
33
|
-
.map((member) => member?.leaguePlayerId)
|
|
34
|
-
.filter(Boolean)
|
|
35
|
-
: state.participant1MemberIds;
|
|
36
|
-
state.participant2MemberIds = Array.isArray(data.participant2?.members)
|
|
37
|
-
? data.participant2.members
|
|
38
|
-
.map((member) => member?.leaguePlayerId)
|
|
39
|
-
.filter(Boolean)
|
|
40
|
-
: state.participant2MemberIds;
|
|
41
27
|
return state.matchContext;
|
|
42
28
|
}
|
|
43
29
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"match-context.js","sourceRoot":"","sources":["../../src/match-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG1C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB;IACrC,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO;QAAE,OAAO,IAAI,CAAC;IACnD,IAAI,KAAK,CAAC,YAAY;QAAE,OAAO,KAAK,CAAC,YAAY,CAAC;IAClD,IAAI,KAAK,CAAC,mBAAmB;QAAE,OAAO,MAAM,KAAK,CAAC,mBAAmB,CAAC;IAEtE,KAAK,CAAC,mBAAmB,GAAG,UAAU,CACpC,WAAW;QACT,kBAAkB,CAAC,KAAK,CAAC,QAAQ,CAAC;QAClC,WAAW;QACX,kBAAkB,CAAC,KAAK,CAAC,OAAO,CAAC,EACnC,EAAE,MAAM,EAAE,KAAK,EAAE,CAClB;SACE,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;QACf,MAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;QAClC,IAAI,MAAM,EAAE,OAAO,IAAI,IAAI,EAAE,CAAC;YAC5B,KAAK,CAAC,YAAY,GAAG,IAAoB,CAAC;YAC1C,
|
|
1
|
+
{"version":3,"file":"match-context.js","sourceRoot":"","sources":["../../src/match-context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG1C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB;IACrC,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO;QAAE,OAAO,IAAI,CAAC;IACnD,IAAI,KAAK,CAAC,YAAY;QAAE,OAAO,KAAK,CAAC,YAAY,CAAC;IAClD,IAAI,KAAK,CAAC,mBAAmB;QAAE,OAAO,MAAM,KAAK,CAAC,mBAAmB,CAAC;IAEtE,KAAK,CAAC,mBAAmB,GAAG,UAAU,CACpC,WAAW;QACT,kBAAkB,CAAC,KAAK,CAAC,QAAQ,CAAC;QAClC,WAAW;QACX,kBAAkB,CAAC,KAAK,CAAC,OAAO,CAAC,EACnC,EAAE,MAAM,EAAE,KAAK,EAAE,CAClB;SACE,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;QACf,MAAM,IAAI,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC;QAClC,IAAI,MAAM,EAAE,OAAO,IAAI,IAAI,EAAE,CAAC;YAC5B,KAAK,CAAC,YAAY,GAAG,IAAoB,CAAC;YAC1C,OAAO,KAAK,CAAC,YAAY,CAAC;QAC5B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;SACD,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC;SACjB,OAAO,CAAC,GAAG,EAAE;QACZ,KAAK,CAAC,mBAAmB,GAAG,IAAI,CAAC;IACnC,CAAC,CAAC,CAAC;IAEL,OAAO,MAAM,KAAK,CAAC,mBAAmB,CAAC;AACzC,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local dev harness for UGC server bundles. Runs the guest module in the
|
|
3
|
+
* current Node process (no isolate, no native deps) against a simulated
|
|
4
|
+
* `ctx`. Use from Vitest/Jest/Mocha to unit-test a `server/index.js`
|
|
5
|
+
* without uploading to GameHub.
|
|
6
|
+
*
|
|
7
|
+
* Differences from production:
|
|
8
|
+
* - No memory cap, no per-call wall-clock timeout. The harness is a
|
|
9
|
+
* trust-the-author test tool, not a sandbox.
|
|
10
|
+
* - No JSON snapshot/diff round-trip. The harness reads the live
|
|
11
|
+
* `ctx.state` object directly. This means a bug where the guest
|
|
12
|
+
* mutates non-serializable values would be caught later by the
|
|
13
|
+
* production sandbox but not here. Keep state JSON-serializable.
|
|
14
|
+
* - Deterministic clock + PRNG are opt-in via `seed` and `clock`.
|
|
15
|
+
*
|
|
16
|
+
* The captured `outbox` is the full log of bridge calls in order, useful
|
|
17
|
+
* for assertions like "the guest broadcast `score:awarded` after the
|
|
18
|
+
* second `answer` message".
|
|
19
|
+
*/
|
|
20
|
+
import type { BroadcastOptions, CreateOptions, EndMatchPayload, GuestModule, PeerInfo, SeededPlayer, Question } from './server-entry.js';
|
|
21
|
+
export type OutboxEntry = {
|
|
22
|
+
kind: 'broadcast';
|
|
23
|
+
type: string;
|
|
24
|
+
payload: unknown;
|
|
25
|
+
opts: BroadcastOptions | null;
|
|
26
|
+
} | {
|
|
27
|
+
kind: 'send';
|
|
28
|
+
peerId: string;
|
|
29
|
+
type: string;
|
|
30
|
+
payload: unknown;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'kick';
|
|
33
|
+
peerId: string;
|
|
34
|
+
reason: string | null;
|
|
35
|
+
} | {
|
|
36
|
+
kind: 'endMatch';
|
|
37
|
+
payload: EndMatchPayload;
|
|
38
|
+
} | {
|
|
39
|
+
kind: 'log';
|
|
40
|
+
args: unknown[];
|
|
41
|
+
};
|
|
42
|
+
export interface LocalRunOptions {
|
|
43
|
+
/** Seed for `ctx.random()`. Default: `1`. */
|
|
44
|
+
seed?: number;
|
|
45
|
+
/** Clock mode: `manual` (advance via `room.advance(ms)`), or `wall`. */
|
|
46
|
+
clock?: 'manual' | 'wall';
|
|
47
|
+
/** Options passed to `guest.onCreate`. Default: stub values. */
|
|
48
|
+
initialOptions?: Partial<CreateOptions>;
|
|
49
|
+
/** Stand-in question pack passed to `onCreate` as `opts.questions`. */
|
|
50
|
+
questions?: Question[];
|
|
51
|
+
/** Stand-in seeded roster passed to `onCreate` as `opts.seededPlayers`.
|
|
52
|
+
* Mirrors the lobby-spawn flow. The harness does NOT auto-call onStart
|
|
53
|
+
* when peers join — call `room.start()` explicitly. */
|
|
54
|
+
seededPlayers?: SeededPlayer[];
|
|
55
|
+
}
|
|
56
|
+
export interface LocalPeer extends PeerInfo {
|
|
57
|
+
}
|
|
58
|
+
export interface LocalRoom {
|
|
59
|
+
/** Live `ctx.state` object — the same one the guest mutates. */
|
|
60
|
+
readonly state: Record<string, unknown>;
|
|
61
|
+
/** Ordered log of every bridge call (broadcast/send/kick/endMatch/log). */
|
|
62
|
+
readonly outbox: OutboxEntry[];
|
|
63
|
+
/** Snapshot of the currently joined peers. */
|
|
64
|
+
readonly peers: ReadonlyArray<LocalPeer>;
|
|
65
|
+
/** Set true once the guest called `ctx.endMatch`. */
|
|
66
|
+
readonly ended: boolean;
|
|
67
|
+
/** Resolved payload from `ctx.endMatch`, if any. */
|
|
68
|
+
readonly endResult: EndMatchPayload | null;
|
|
69
|
+
/** Simulate a peer joining. */
|
|
70
|
+
join(peerId: string, info?: Partial<Omit<PeerInfo, 'peerId'>>): void;
|
|
71
|
+
/** Simulate a peer leaving. */
|
|
72
|
+
leave(peerId: string, code?: number | null): void;
|
|
73
|
+
/** Send a message from a peer to the guest. */
|
|
74
|
+
send(peerId: string, type: string, payload?: unknown): void;
|
|
75
|
+
/** Trigger the lobby→active transition (calls `guest.onStart`). */
|
|
76
|
+
start(): void;
|
|
77
|
+
/** Advance the manual clock by `ms`. No-op in `wall` mode. */
|
|
78
|
+
advance(ms: number): void;
|
|
79
|
+
/** Tear down (calls `guest.onDispose`). */
|
|
80
|
+
dispose(): void;
|
|
81
|
+
/** Clear the outbox in-place (useful between assertions). */
|
|
82
|
+
clearOutbox(): void;
|
|
83
|
+
}
|
|
84
|
+
export declare function runLocal(guest: GuestModule, opts?: LocalRunOptions): LocalRoom;
|
|
85
|
+
//# sourceMappingURL=server-dev-harness.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-dev-harness.d.ts","sourceRoot":"","sources":["../../src/server-dev-harness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EACV,gBAAgB,EAEhB,aAAa,EACb,eAAe,EACf,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,QAAQ,EAET,MAAM,mBAAmB,CAAC;AAE3B,MAAM,MAAM,WAAW,GACnB;IACE,IAAI,EAAE,WAAW,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,gBAAgB,GAAG,IAAI,CAAC;CAC/B,GACD;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAChE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GACvD;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,OAAO,EAAE,eAAe,CAAA;CAAE,GAC9C;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,OAAO,EAAE,CAAA;CAAE,CAAC;AAErC,MAAM,WAAW,eAAe;IAC9B,6CAA6C;IAC7C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wEAAwE;IACxE,KAAK,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC1B,gEAAgE;IAChE,cAAc,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IACxC,uEAAuE;IACvE,SAAS,CAAC,EAAE,QAAQ,EAA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**
|
|
2
|
+
* Local dev harness for UGC server bundles. Runs the guest module in the
|
|
3
|
+
* current Node process (no isolate, no native deps) against a simulated
|
|
4
|
+
* `ctx`. Use from Vitest/Jest/Mocha to unit-test a `server/index.js`
|
|
5
|
+
* without uploading to GameHub.
|
|
6
|
+
*
|
|
7
|
+
* Differences from production:
|
|
8
|
+
* - No memory cap, no per-call wall-clock timeout. The harness is a
|
|
9
|
+
* trust-the-author test tool, not a sandbox.
|
|
10
|
+
* - No JSON snapshot/diff round-trip. The harness reads the live
|
|
11
|
+
* `ctx.state` object directly. This means a bug where the guest
|
|
12
|
+
* mutates non-serializable values would be caught later by the
|
|
13
|
+
* production sandbox but not here. Keep state JSON-serializable.
|
|
14
|
+
* - Deterministic clock + PRNG are opt-in via `seed` and `clock`.
|
|
15
|
+
*
|
|
16
|
+
* The captured `outbox` is the full log of bridge calls in order, useful
|
|
17
|
+
* for assertions like "the guest broadcast `score:awarded` after the
|
|
18
|
+
* second `answer` message".
|
|
19
|
+
*/
|
|
20
|
+
const DEFAULT_OPTS = {
|
|
21
|
+
roomCode: '000000',
|
|
22
|
+
maxPlayers: 4,
|
|
23
|
+
hostUserId: 'local-host',
|
|
24
|
+
gameSlug: 'local-test',
|
|
25
|
+
questionProfileId: null,
|
|
26
|
+
questions: null,
|
|
27
|
+
seededPlayers: null,
|
|
28
|
+
};
|
|
29
|
+
export function runLocal(guest, opts = {}) {
|
|
30
|
+
const peers = new Map();
|
|
31
|
+
const outbox = [];
|
|
32
|
+
let ended = false;
|
|
33
|
+
let endResult = null;
|
|
34
|
+
let disposed = false;
|
|
35
|
+
// Manual clock state.
|
|
36
|
+
let nowOffset = 0;
|
|
37
|
+
const startedAt = Date.now();
|
|
38
|
+
const clockMode = opts.clock ?? 'manual';
|
|
39
|
+
function now() {
|
|
40
|
+
return clockMode === 'manual' ? startedAt + nowOffset : Date.now();
|
|
41
|
+
}
|
|
42
|
+
// Seeded PRNG (Mulberry32) for parity with the production sandbox.
|
|
43
|
+
let seed = (opts.seed ?? 1) >>> 0;
|
|
44
|
+
function random() {
|
|
45
|
+
seed = (seed + 0x6d2b79f5) >>> 0;
|
|
46
|
+
let t = seed;
|
|
47
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
48
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
49
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
50
|
+
}
|
|
51
|
+
const state = {};
|
|
52
|
+
const timers = [];
|
|
53
|
+
let nextTimerToken = 1;
|
|
54
|
+
function fireDueTimers() {
|
|
55
|
+
while (timers.length) {
|
|
56
|
+
const t = timers[0];
|
|
57
|
+
if (t.fireAt > now())
|
|
58
|
+
return;
|
|
59
|
+
timers.shift();
|
|
60
|
+
try {
|
|
61
|
+
t.cb();
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
outbox.push({
|
|
65
|
+
kind: 'log',
|
|
66
|
+
args: ['timer threw', err.message],
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const ctx = {
|
|
72
|
+
state,
|
|
73
|
+
broadcast(type, payload, options) {
|
|
74
|
+
outbox.push({
|
|
75
|
+
kind: 'broadcast',
|
|
76
|
+
type,
|
|
77
|
+
payload,
|
|
78
|
+
opts: options ?? null,
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
send(peerId, type, payload) {
|
|
82
|
+
outbox.push({ kind: 'send', peerId, type, payload });
|
|
83
|
+
},
|
|
84
|
+
kick(peerId, reason) {
|
|
85
|
+
outbox.push({ kind: 'kick', peerId, reason: reason ?? null });
|
|
86
|
+
peers.delete(peerId);
|
|
87
|
+
},
|
|
88
|
+
endMatch(payload = {}) {
|
|
89
|
+
if (ended)
|
|
90
|
+
return;
|
|
91
|
+
ended = true;
|
|
92
|
+
endResult = payload;
|
|
93
|
+
outbox.push({ kind: 'endMatch', payload });
|
|
94
|
+
},
|
|
95
|
+
log(...args) {
|
|
96
|
+
outbox.push({ kind: 'log', args });
|
|
97
|
+
},
|
|
98
|
+
now,
|
|
99
|
+
random,
|
|
100
|
+
setTimeout(cb, ms) {
|
|
101
|
+
if (typeof cb !== 'function')
|
|
102
|
+
return null;
|
|
103
|
+
const token = nextTimerToken++;
|
|
104
|
+
const fireAt = now() + Math.max(0, ms | 0);
|
|
105
|
+
const entry = { token, fireAt, cb };
|
|
106
|
+
timers.push(entry);
|
|
107
|
+
timers.sort((a, b) => a.fireAt - b.fireAt);
|
|
108
|
+
if (clockMode === 'wall') {
|
|
109
|
+
// Use the global setTimeout/clearTimeout; the harness runs on Node
|
|
110
|
+
// (or any host that polyfills the globals) so we don't reach into
|
|
111
|
+
// `globalThis` via a type-narrowed path.
|
|
112
|
+
const g = globalThis;
|
|
113
|
+
entry.wallHandle = g.setTimeout(() => {
|
|
114
|
+
const idx = timers.indexOf(entry);
|
|
115
|
+
if (idx >= 0)
|
|
116
|
+
timers.splice(idx, 1);
|
|
117
|
+
try {
|
|
118
|
+
cb();
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
outbox.push({
|
|
122
|
+
kind: 'log',
|
|
123
|
+
args: ['timer threw', err.message],
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}, ms);
|
|
127
|
+
}
|
|
128
|
+
return { __t: token };
|
|
129
|
+
},
|
|
130
|
+
clearTimeout(handle) {
|
|
131
|
+
if (!handle)
|
|
132
|
+
return;
|
|
133
|
+
const idx = timers.findIndex((t) => t.token === handle.__t);
|
|
134
|
+
if (idx < 0)
|
|
135
|
+
return;
|
|
136
|
+
const entry = timers[idx];
|
|
137
|
+
if (entry.wallHandle) {
|
|
138
|
+
const g = globalThis;
|
|
139
|
+
g.clearTimeout(entry.wallHandle);
|
|
140
|
+
}
|
|
141
|
+
timers.splice(idx, 1);
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
const initialOptions = {
|
|
145
|
+
...DEFAULT_OPTS,
|
|
146
|
+
...opts.initialOptions,
|
|
147
|
+
questions: opts.questions ?? opts.initialOptions?.questions ?? null,
|
|
148
|
+
questionProfileId: opts.initialOptions?.questionProfileId ??
|
|
149
|
+
(opts.questions ? 'default' : null),
|
|
150
|
+
seededPlayers: opts.seededPlayers ?? opts.initialOptions?.seededPlayers ?? null,
|
|
151
|
+
};
|
|
152
|
+
guest.onCreate?.(ctx, initialOptions);
|
|
153
|
+
function defaultPeerInfo(peerId, info = {}) {
|
|
154
|
+
return {
|
|
155
|
+
peerId,
|
|
156
|
+
userId: info.userId ?? peerId,
|
|
157
|
+
displayName: info.displayName ?? peerId,
|
|
158
|
+
avatar: info.avatar,
|
|
159
|
+
viewer: !!info.viewer,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
get state() {
|
|
164
|
+
return state;
|
|
165
|
+
},
|
|
166
|
+
get outbox() {
|
|
167
|
+
return outbox;
|
|
168
|
+
},
|
|
169
|
+
get peers() {
|
|
170
|
+
return Array.from(peers.values());
|
|
171
|
+
},
|
|
172
|
+
get ended() {
|
|
173
|
+
return ended;
|
|
174
|
+
},
|
|
175
|
+
get endResult() {
|
|
176
|
+
return endResult;
|
|
177
|
+
},
|
|
178
|
+
join(peerId, info) {
|
|
179
|
+
if (disposed)
|
|
180
|
+
throw new Error('room disposed');
|
|
181
|
+
const peer = defaultPeerInfo(peerId, info);
|
|
182
|
+
peers.set(peerId, peer);
|
|
183
|
+
guest.onJoin?.(ctx, peer);
|
|
184
|
+
},
|
|
185
|
+
leave(peerId, code) {
|
|
186
|
+
if (disposed)
|
|
187
|
+
throw new Error('room disposed');
|
|
188
|
+
const peer = peers.get(peerId);
|
|
189
|
+
if (!peer)
|
|
190
|
+
return;
|
|
191
|
+
peers.delete(peerId);
|
|
192
|
+
guest.onLeave?.(ctx, peer, code ?? null);
|
|
193
|
+
},
|
|
194
|
+
send(peerId, type, payload) {
|
|
195
|
+
if (disposed)
|
|
196
|
+
throw new Error('room disposed');
|
|
197
|
+
const peer = peers.get(peerId);
|
|
198
|
+
if (!peer) {
|
|
199
|
+
throw new Error(`unknown peer: ${peerId}`);
|
|
200
|
+
}
|
|
201
|
+
guest.onMessage?.(ctx, peer, type, payload);
|
|
202
|
+
},
|
|
203
|
+
start() {
|
|
204
|
+
if (disposed)
|
|
205
|
+
throw new Error('room disposed');
|
|
206
|
+
guest.onStart?.(ctx);
|
|
207
|
+
},
|
|
208
|
+
advance(ms) {
|
|
209
|
+
if (clockMode === 'manual') {
|
|
210
|
+
nowOffset += ms;
|
|
211
|
+
fireDueTimers();
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
dispose() {
|
|
215
|
+
if (disposed)
|
|
216
|
+
return;
|
|
217
|
+
disposed = true;
|
|
218
|
+
try {
|
|
219
|
+
guest.onDispose?.(ctx);
|
|
220
|
+
}
|
|
221
|
+
catch (err) {
|
|
222
|
+
outbox.push({
|
|
223
|
+
kind: 'log',
|
|
224
|
+
args: ['onDispose threw', err.message],
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
clearOutbox() {
|
|
229
|
+
outbox.length = 0;
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=server-dev-harness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-dev-harness.js","sourceRoot":"","sources":["../../src/server-dev-harness.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAuEH,MAAM,YAAY,GAAkB;IAClC,QAAQ,EAAE,QAAQ;IAClB,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,YAAY;IACxB,QAAQ,EAAE,YAAY;IACtB,iBAAiB,EAAE,IAAI;IACvB,SAAS,EAAE,IAAI;IACf,aAAa,EAAE,IAAI;CACpB,CAAC;AAEF,MAAM,UAAU,QAAQ,CACtB,KAAkB,EAClB,OAAwB,EAAE;IAE1B,MAAM,KAAK,GAAG,IAAI,GA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,CAAC,eAAe,CAAC,CAAC;YAC/C,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YAC/B,IAAI,CAAC,IAAI,EAAE,CAAC;gBACV,MAAM,IAAI,KAAK,CAAC,iBAAiB,MAAM,EAAE,CAAC,CAAC;YAC7C,CAAC;YACD,KAAK,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC;QACD,KAAK;YACH,IAAI,QAAQ;gBAAE,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;YAC/C,KAAK,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC;QACvB,CAAC;QACD,OAAO,CAAC,EAAE;YACR,IAAI,SAAS,KAAK,QAAQ,EAAE,CAAC;gBAC3B,SAAS,IAAI,EAAE,CAAC;gBAChB,aAAa,EAAE,CAAC;YAClB,CAAC;QACH,CAAC;QACD,OAAO;YACL,IAAI,QAAQ;gBAAE,OAAO;YACrB,QAAQ,GAAG,IAAI,CAAC;YAChB,IAAI,CAAC;gBACH,KAAK,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,CAAC;YACzB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,MAAM,CAAC,IAAI,CAAC;oBACV,IAAI,EAAE,KAAK;oBACX,IAAI,EAAE,CAAC,iBAAiB,EAAG,GAAa,CAAC,OAAO,CAAC;iBAClD,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QACD,WAAW;YACT,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;QACpB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types and minimal helpers for authoring UGC server bundles
|
|
3
|
+
* (`server/index.js`) that run inside the GameHub colyseus UGC sandbox.
|
|
4
|
+
*
|
|
5
|
+
* This entry is **types-only at runtime**: the actual `ctx` object is
|
|
6
|
+
* injected by the host (see `packages/colyseus/src/rooms/ugc/sandbox.ts`).
|
|
7
|
+
* A guest bundle does not need to import anything from this entry to run,
|
|
8
|
+
* but importing the types makes authoring in TypeScript pleasant:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { GuestModule, Ctx, PeerInfo } from '@ksaa-nlp/gamehub-sdk/server';
|
|
12
|
+
*
|
|
13
|
+
* const guest: GuestModule = {
|
|
14
|
+
* onCreate(ctx, opts) { ctx.state.round = 0; },
|
|
15
|
+
* onJoin(ctx, peer) { ctx.broadcast('peer:joined', { peerId: peer.peerId }); },
|
|
16
|
+
* onMessage(ctx, peer, type, payload) { ... },
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* module.exports = guest;
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* For local unit testing without the colyseus host, see
|
|
23
|
+
* `@ksaa-nlp/gamehub-sdk/server/dev` (`runLocal`).
|
|
24
|
+
*/
|
|
25
|
+
export interface PeerInfo {
|
|
26
|
+
/** Stable per-room session id. Use this as the canonical identifier. */
|
|
27
|
+
peerId: string;
|
|
28
|
+
userId: string;
|
|
29
|
+
displayName: string;
|
|
30
|
+
avatar?: string;
|
|
31
|
+
viewer: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface BroadcastOptions {
|
|
34
|
+
/** Skip the peer with this id when broadcasting. */
|
|
35
|
+
exceptPeerId?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface EndMatchPayload {
|
|
38
|
+
winnerPeerId?: string;
|
|
39
|
+
isDraw?: boolean;
|
|
40
|
+
reason?: string;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The runtime API the host injects as `ctx`. Top-level keys of `ctx.state`
|
|
44
|
+
* are diffed and replicated to clients as `UgcState.game` (a MapSchema).
|
|
45
|
+
* Each top-level value is JSON-encoded once per dispatch — mutate the
|
|
46
|
+
* tree freely; the host snapshots after each lifecycle call.
|
|
47
|
+
*
|
|
48
|
+
* Effect methods (`broadcast`, `send`, `kick`, `endMatch`, `log`) are
|
|
49
|
+
* fire-and-forget from the guest's perspective.
|
|
50
|
+
*/
|
|
51
|
+
export interface Ctx {
|
|
52
|
+
/** Game-defined state. Plain JSON-serializable values only. */
|
|
53
|
+
state: Record<string, unknown>;
|
|
54
|
+
/** Send a message to every connected peer (optionally excluding one). */
|
|
55
|
+
broadcast(type: string, payload?: unknown, opts?: BroadcastOptions): void;
|
|
56
|
+
/** Send a message to one peer by `peerId`. */
|
|
57
|
+
send(peerId: string, type: string, payload?: unknown): void;
|
|
58
|
+
/** Disconnect a peer with an optional reason string. */
|
|
59
|
+
kick(peerId: string, reason?: string): void;
|
|
60
|
+
/** Signal the host to end the match. Triggers `fx:match-end` + persistence. */
|
|
61
|
+
endMatch(payload?: EndMatchPayload): void;
|
|
62
|
+
/** Structured log line. Pipes to colyseus' log under `[ugc/<slug>/<roomId>]`. */
|
|
63
|
+
log(...args: unknown[]): void;
|
|
64
|
+
/** Server-side epoch milliseconds (host clock). */
|
|
65
|
+
now(): number;
|
|
66
|
+
/** Deterministic PRNG seeded per-bundle-hash. */
|
|
67
|
+
random(): number;
|
|
68
|
+
/**
|
|
69
|
+
* Schedule `cb` to run after `ms` milliseconds. Returns an opaque handle,
|
|
70
|
+
* or `null` if the per-room active-timer cap (`maxActiveTimers`) is hit.
|
|
71
|
+
* The host owns the actual delay via its Colyseus clock, so timers
|
|
72
|
+
* survive ticks but are cleared on room dispose.
|
|
73
|
+
*/
|
|
74
|
+
setTimeout(cb: () => void, ms: number): TimerHandle | null;
|
|
75
|
+
/** Cancel a timer scheduled by `setTimeout`. No-op if already fired. */
|
|
76
|
+
clearTimeout(handle: TimerHandle | null): void;
|
|
77
|
+
}
|
|
78
|
+
/** Opaque handle returned by `ctx.setTimeout`. Pass back to `ctx.clearTimeout`. */
|
|
79
|
+
export interface TimerHandle {
|
|
80
|
+
readonly __t: number;
|
|
81
|
+
}
|
|
82
|
+
export type { Question, QuestionDifficulty } from '../types/question.js';
|
|
83
|
+
import type { Question } from '../types/question.js';
|
|
84
|
+
export interface CreateOptions {
|
|
85
|
+
roomCode: string;
|
|
86
|
+
maxPlayers: number;
|
|
87
|
+
hostUserId: string;
|
|
88
|
+
gameSlug: string;
|
|
89
|
+
/** Question-profile ID supplied to the matchmaker, or null if none. */
|
|
90
|
+
questionProfileId?: string | null;
|
|
91
|
+
/** Pre-loaded questions when the room was created with a `questionProfileId`.
|
|
92
|
+
* `null` when no profile was requested. The host loads the set once at
|
|
93
|
+
* room create; the guest is responsible for round-robin / ordering. */
|
|
94
|
+
questions?: Question[] | null;
|
|
95
|
+
/** Seeded roster when the room was spawned by the platform `LobbyRoom`
|
|
96
|
+
* (the "useExternalLobby" flow). `null` for normal direct-create rooms.
|
|
97
|
+
* When present, the host has already pre-populated `state.players` with
|
|
98
|
+
* one entry per seeded user; the guest's `onStart` fires automatically
|
|
99
|
+
* once they're all online instead of waiting for a `lobby:start` intent. */
|
|
100
|
+
seededPlayers?: SeededPlayer[] | null;
|
|
101
|
+
}
|
|
102
|
+
export interface SeededPlayer {
|
|
103
|
+
userId: string;
|
|
104
|
+
displayName: string;
|
|
105
|
+
avatar?: string;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Default-exported shape of a guest bundle. Every method is optional —
|
|
109
|
+
* the host calls a method only if the guest defines it. `onMessage` is
|
|
110
|
+
* the most commonly defined; `onCreate`/`onStart` for setup, `onJoin`/
|
|
111
|
+
* `onLeave` for roster reactions, `onDispose` for cleanup.
|
|
112
|
+
*
|
|
113
|
+
* Methods are called synchronously inside the V8 isolate with a per-call
|
|
114
|
+
* wall-clock cap (default 50 ms). Long-running work must be split across
|
|
115
|
+
* tick callbacks or message handlers; there is no `await`-safe path
|
|
116
|
+
* across the sandbox boundary in v1.
|
|
117
|
+
*/
|
|
118
|
+
export interface GuestModule {
|
|
119
|
+
onCreate?(ctx: Ctx, opts: CreateOptions): void;
|
|
120
|
+
onStart?(ctx: Ctx): void;
|
|
121
|
+
onJoin?(ctx: Ctx, peer: PeerInfo): void;
|
|
122
|
+
onReconnect?(ctx: Ctx, peer: PeerInfo): void;
|
|
123
|
+
onLeave?(ctx: Ctx, peer: PeerInfo, code: number | null): void;
|
|
124
|
+
onMessage?(ctx: Ctx, peer: PeerInfo, type: string, payload: unknown): void;
|
|
125
|
+
onDispose?(ctx: Ctx): void;
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=server-entry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-entry.d.ts","sourceRoot":"","sources":["../../src/server-entry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,MAAM,WAAW,QAAQ;IACvB,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,oDAAoD;IACpD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,GAAG;IAClB,+DAA+D;IAC/D,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,yEAAyE;IACzE,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAC1E,8CAA8C;IAC9C,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,C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AAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;;GAUG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI,CAAC;IAC/C,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;IACzB,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAC;IACxC,WAAW,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAC;IAC7C,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IAC9D,SAAS,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAC3E,SAAS,CAAC,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC;CAC5B"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types and minimal helpers for authoring UGC server bundles
|
|
3
|
+
* (`server/index.js`) that run inside the GameHub colyseus UGC sandbox.
|
|
4
|
+
*
|
|
5
|
+
* This entry is **types-only at runtime**: the actual `ctx` object is
|
|
6
|
+
* injected by the host (see `packages/colyseus/src/rooms/ugc/sandbox.ts`).
|
|
7
|
+
* A guest bundle does not need to import anything from this entry to run,
|
|
8
|
+
* but importing the types makes authoring in TypeScript pleasant:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { GuestModule, Ctx, PeerInfo } from '@ksaa-nlp/gamehub-sdk/server';
|
|
12
|
+
*
|
|
13
|
+
* const guest: GuestModule = {
|
|
14
|
+
* onCreate(ctx, opts) { ctx.state.round = 0; },
|
|
15
|
+
* onJoin(ctx, peer) { ctx.broadcast('peer:joined', { peerId: peer.peerId }); },
|
|
16
|
+
* onMessage(ctx, peer, type, payload) { ... },
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* module.exports = guest;
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* For local unit testing without the colyseus host, see
|
|
23
|
+
* `@ksaa-nlp/gamehub-sdk/server/dev` (`runLocal`).
|
|
24
|
+
*/
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=server-entry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-entry.js","sourceRoot":"","sources":["../../src/server-entry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG"}
|
package/dist/src/session.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import type { MatchResultPayload } from '../types/match.js';
|
|
2
1
|
import type { Question, QuestionOptions, ScoreMetadata, StartSessionResult, SubmitScoreResult } from '../types/session.js';
|
|
2
|
+
import type { MatchResultPayload } from '../types/match.js';
|
|
3
3
|
/**
|
|
4
|
-
* Starts a new game session and caches the returned session token. Safe
|
|
5
|
-
* call multiple times — the cached token short-circuits subsequent
|
|
4
|
+
* Starts a new game session and caches the returned session token. Safe
|
|
5
|
+
* to call multiple times — the cached token short-circuits subsequent
|
|
6
|
+
* calls.
|
|
6
7
|
*
|
|
7
8
|
* Endpoint is selected from the active context:
|
|
8
9
|
* - league + game + match → scoped match session
|
|
@@ -11,14 +12,19 @@ import type { Question, QuestionOptions, ScoreMetadata, StartSessionResult, Subm
|
|
|
11
12
|
*/
|
|
12
13
|
export declare function startSession(): Promise<StartSessionResult>;
|
|
13
14
|
/**
|
|
14
|
-
* Submit a score to the current session. If no session exists yet we try
|
|
15
|
-
* open one (re-using any in-flight start attempt).
|
|
15
|
+
* Submit a score to the current session. If no session exists yet we try
|
|
16
|
+
* to open one (re-using any in-flight start attempt).
|
|
17
|
+
*
|
|
18
|
+
* Multiplayer authority lives on the Colyseus server now — score
|
|
19
|
+
* submission is no longer gated by a client-side host role. Games that
|
|
20
|
+
* want only one peer to call this should make that decision based on
|
|
21
|
+
* their room state (e.g. `room.state.hostPeerId === selfId`).
|
|
16
22
|
*/
|
|
17
23
|
export declare function submitScore(score: number, meta?: ScoreMetadata): Promise<SubmitScoreResult>;
|
|
18
24
|
/**
|
|
19
25
|
* Convenience wrapper that tags a score with the current league match
|
|
20
|
-
* context. Fails early if there's no matchId — a plain `submitScore` is
|
|
21
|
-
* right call in that case.
|
|
26
|
+
* context. Fails early if there's no matchId — a plain `submitScore` is
|
|
27
|
+
* the right call in that case.
|
|
22
28
|
*/
|
|
23
29
|
export declare function submitMatchScore(score: number, meta?: ScoreMetadata): Promise<SubmitScoreResult>;
|
|
24
30
|
/**
|
|
@@ -28,8 +34,9 @@ export declare function submitMatchScore(score: number, meta?: ScoreMetadata): P
|
|
|
28
34
|
*/
|
|
29
35
|
export declare function submitMatchResult(result?: MatchResultPayload): Promise<SubmitScoreResult>;
|
|
30
36
|
/**
|
|
31
|
-
* Fetch questions for this game. Returns an empty array on any failure
|
|
32
|
-
* invalid input — callers can treat "no questions" as the degraded
|
|
37
|
+
* Fetch questions for this game. Returns an empty array on any failure
|
|
38
|
+
* or invalid input — callers can treat "no questions" as the degraded
|
|
39
|
+
* state.
|
|
33
40
|
*/
|
|
34
41
|
export declare function getQuestions(options?: QuestionOptions): Promise<Question[]>;
|
|
35
42
|
//# sourceMappingURL=session.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,QAAQ,EACR,eAAe,EACf,aAAa,EACb,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;;;;;;;;GASG;AACH,wBAAsB,YAAY,IAAI,OAAO,CAAC,kBAAkB,CAAC,CA2ChE;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,aAAkB,GACvB,OAAO,CAAC,iBAAiB,CAAC,CAmE5B;AAED;;;;GAIG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,aAAkB,GACvB,OAAO,CAAC,iBAAiB,CAAC,CAc5B;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,GAAE,kBAAuB,GAC9B,OAAO,CAAC,iBAAiB,CAAC,CAM5B;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAChC,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,QAAQ,EAAE,CAAC,CAuDrB"}
|