tickflare 0.0.0-stage → 0.2.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/CHANGELOG.md +240 -0
- package/LICENSE +21 -0
- package/README.md +112 -2
- package/dist/binary.d.ts +91 -0
- package/dist/binary.js +466 -0
- package/dist/binary.js.map +1 -0
- package/dist/client/accounts.d.ts +193 -0
- package/dist/client/accounts.js +306 -0
- package/dist/client/accounts.js.map +1 -0
- package/dist/client/buffered.d.ts +88 -0
- package/dist/client/buffered.js +229 -0
- package/dist/client/buffered.js.map +1 -0
- package/dist/client/clock.d.ts +36 -0
- package/dist/client/clock.js +83 -0
- package/dist/client/clock.js.map +1 -0
- package/dist/client/draft.d.ts +17 -0
- package/dist/client/draft.js +133 -0
- package/dist/client/draft.js.map +1 -0
- package/dist/client/index.d.ts +182 -0
- package/dist/client/index.js +496 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/interpolate.d.ts +31 -0
- package/dist/client/interpolate.js +59 -0
- package/dist/client/interpolate.js.map +1 -0
- package/dist/client/lockstep.d.ts +86 -0
- package/dist/client/lockstep.js +339 -0
- package/dist/client/lockstep.js.map +1 -0
- package/dist/client/matchmaking.d.ts +21 -0
- package/dist/client/matchmaking.js +55 -0
- package/dist/client/matchmaking.js.map +1 -0
- package/dist/client/pacer.d.ts +22 -0
- package/dist/client/pacer.js +65 -0
- package/dist/client/pacer.js.map +1 -0
- package/dist/client/predict.d.ts +75 -0
- package/dist/client/predict.js +203 -0
- package/dist/client/predict.js.map +1 -0
- package/dist/client/token.d.ts +10 -0
- package/dist/client/token.js +15 -0
- package/dist/client/token.js.map +1 -0
- package/dist/lockstep.d.ts +154 -0
- package/dist/lockstep.js +140 -0
- package/dist/lockstep.js.map +1 -0
- package/dist/patch.d.ts +54 -0
- package/dist/patch.js +186 -0
- package/dist/patch.js.map +1 -0
- package/dist/protocol.d.ts +268 -0
- package/dist/protocol.js +54 -0
- package/dist/protocol.js.map +1 -0
- package/dist/server/accounts.d.ts +320 -0
- package/dist/server/accounts.js +1223 -0
- package/dist/server/accounts.js.map +1 -0
- package/dist/server/delta.d.ts +50 -0
- package/dist/server/delta.js +426 -0
- package/dist/server/delta.js.map +1 -0
- package/dist/server/frames.d.ts +15 -0
- package/dist/server/frames.js +23 -0
- package/dist/server/frames.js.map +1 -0
- package/dist/server/gateway.d.ts +80 -0
- package/dist/server/gateway.js +144 -0
- package/dist/server/gateway.js.map +1 -0
- package/dist/server/index.d.ts +21 -0
- package/dist/server/index.js +22 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/limiter.d.ts +12 -0
- package/dist/server/limiter.js +43 -0
- package/dist/server/limiter.js.map +1 -0
- package/dist/server/lockstep.d.ts +127 -0
- package/dist/server/lockstep.js +549 -0
- package/dist/server/lockstep.js.map +1 -0
- package/dist/server/matches.d.ts +31 -0
- package/dist/server/matches.js +69 -0
- package/dist/server/matches.js.map +1 -0
- package/dist/server/matchmaker.d.ts +121 -0
- package/dist/server/matchmaker.js +479 -0
- package/dist/server/matchmaker.js.map +1 -0
- package/dist/server/room.d.ts +402 -0
- package/dist/server/room.js +1896 -0
- package/dist/server/room.js.map +1 -0
- package/dist/server/signals.d.ts +18 -0
- package/dist/server/signals.js +37 -0
- package/dist/server/signals.js.map +1 -0
- package/dist/server/storage.d.ts +117 -0
- package/dist/server/storage.js +330 -0
- package/dist/server/storage.js.map +1 -0
- package/dist/server/tokens.d.ts +74 -0
- package/dist/server/tokens.js +117 -0
- package/dist/server/tokens.js.map +1 -0
- package/dist/server/tracked.d.ts +47 -0
- package/dist/server/tracked.js +270 -0
- package/dist/server/tracked.js.map +1 -0
- package/dist/world.d.ts +20 -0
- package/dist/world.js +21 -0
- package/dist/world.js.map +1 -0
- package/package.json +74 -4
- package/src/binary.ts +496 -0
- package/src/client/accounts.ts +423 -0
- package/src/client/buffered.ts +264 -0
- package/src/client/clock.ts +86 -0
- package/src/client/draft.ts +138 -0
- package/src/client/index.ts +631 -0
- package/src/client/interpolate.ts +60 -0
- package/src/client/lockstep.ts +398 -0
- package/src/client/matchmaking.ts +61 -0
- package/src/client/pacer.ts +67 -0
- package/src/client/predict.ts +239 -0
- package/src/client/token.ts +22 -0
- package/src/lockstep.ts +285 -0
- package/src/patch.ts +188 -0
- package/src/protocol.ts +300 -0
- package/src/server/accounts.ts +1474 -0
- package/src/server/delta.ts +467 -0
- package/src/server/frames.ts +23 -0
- package/src/server/gateway.ts +213 -0
- package/src/server/index.ts +86 -0
- package/src/server/limiter.ts +41 -0
- package/src/server/lockstep.ts +617 -0
- package/src/server/matches.ts +99 -0
- package/src/server/matchmaker.ts +537 -0
- package/src/server/room.ts +1999 -0
- package/src/server/signals.ts +46 -0
- package/src/server/storage.ts +392 -0
- package/src/server/tokens.ts +184 -0
- package/src/server/tracked.ts +260 -0
- package/src/world.ts +29 -0
- package/tools/rooms-report.mjs +168 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Close reasons are limited to 123 bytes by the WebSocket protocol. */
|
|
2
|
+
export const MAX_CLOSE_REASON = 120;
|
|
3
|
+
/** Checks the shape of a parsed client frame. `data` is passed on untouched: the room's `parseCommand` checks it. */
|
|
4
|
+
export function readFrame(value) {
|
|
5
|
+
if (typeof value !== "object" || value === null)
|
|
6
|
+
return undefined;
|
|
7
|
+
const frame = value;
|
|
8
|
+
if (!Number.isSafeInteger(frame.id))
|
|
9
|
+
return undefined;
|
|
10
|
+
const id = frame.id;
|
|
11
|
+
if (frame.t === "cmd")
|
|
12
|
+
return { t: "cmd", id, data: frame.data };
|
|
13
|
+
if (frame.t === "leave")
|
|
14
|
+
return { t: "leave", id };
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
/** Completes the upgrade, then closes straight away so the client learns why it was turned away. */
|
|
18
|
+
export function refuse(client, server, code, reason) {
|
|
19
|
+
server.accept();
|
|
20
|
+
server.close(code, reason.slice(0, MAX_CLOSE_REASON));
|
|
21
|
+
return new Response(null, { status: 101, webSocket: client });
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=frames.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frames.js","sourceRoot":"","sources":["../../src/server/frames.ts"],"names":[],"mappings":"AAGA,wEAAwE;AACxE,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAEpC,qHAAqH;AACrH,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IAClE,MAAM,KAAK,GAAG,KAAsD,CAAC;IACrE,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC;IACtD,MAAM,EAAE,GAAG,KAAK,CAAC,EAAY,CAAC;IAC9B,IAAI,KAAK,CAAC,CAAC,KAAK,KAAK;QAAE,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;IACjE,IAAI,KAAK,CAAC,CAAC,KAAK,OAAO;QAAE,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;IACnD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,oGAAoG;AACpG,MAAM,UAAU,MAAM,CAAC,MAAiB,EAAE,MAAiB,EAAE,IAAY,EAAE,MAAc;IACvF,MAAM,CAAC,MAAM,EAAE,CAAC;IAChB,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC;IACtD,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Party } from "./tokens.js";
|
|
2
|
+
/** Header the gateway uses to tell a room who is connecting. Never trusted from the client. */
|
|
3
|
+
export declare const PLAYER_HEADER = "x-tickflare-player";
|
|
4
|
+
/** Header the gateway uses to tell a room the id it is known by. */
|
|
5
|
+
export declare const ROOM_HEADER = "x-tickflare-room";
|
|
6
|
+
/** Header the gateway uses to tell a room the player's name, URI-encoded. Never trusted from the client. */
|
|
7
|
+
export declare const NAME_HEADER = "x-tickflare-name";
|
|
8
|
+
/** Header the gateway uses to tell a matchmaker roughly where a player is. */
|
|
9
|
+
export declare const HINT_HEADER = "x-tickflare-hint";
|
|
10
|
+
/** Header the gateway uses to tell a matchmaker the player's party, as `size:id`. Never trusted from the client. */
|
|
11
|
+
export declare const PARTY_HEADER = "x-tickflare-party";
|
|
12
|
+
/**
|
|
13
|
+
* Who `authenticate` found: a player id, or a player id with a display name.
|
|
14
|
+
* A name reaches the room's hooks as `player.name`.
|
|
15
|
+
*/
|
|
16
|
+
export type Authenticated = string | {
|
|
17
|
+
/** The player id. */
|
|
18
|
+
id: string;
|
|
19
|
+
/** A display name, e.g. the account name. */
|
|
20
|
+
name?: string;
|
|
21
|
+
/** The party the player queues with, for a matchmaker. See `JoinTokenClaims.party`. */
|
|
22
|
+
party?: Party;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The part of a Durable Object namespace Tickflare needs to reach rooms and
|
|
26
|
+
* matchmakers. Any namespace binding fits, e.g. `env.ROOMS`.
|
|
27
|
+
*/
|
|
28
|
+
export interface RoomNamespace {
|
|
29
|
+
/** The id of the object with this name. */
|
|
30
|
+
idFromName(name: string): DurableObjectId;
|
|
31
|
+
/** A stub for the object with this id. */
|
|
32
|
+
get(id: DurableObjectId, options?: DurableObjectNamespaceGetDurableObjectOptions): {
|
|
33
|
+
/** Sends the object a request. */
|
|
34
|
+
fetch(request: Request): Promise<Response>;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** How `routeToRoom` finds rooms and decides who may connect. */
|
|
38
|
+
export interface RouteOptions {
|
|
39
|
+
/** Durable Object namespace of your `Room` class, e.g. `env.ROOMS`. */
|
|
40
|
+
rooms: RoomNamespace;
|
|
41
|
+
/** URL prefix rooms live under. Defaults to `/rooms`. */
|
|
42
|
+
prefix?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Work out who is connecting. Return the player's id (or `{ id, name }`),
|
|
45
|
+
* or a `Response` to refuse the connection. This is the only place
|
|
46
|
+
* identity is established: rooms trust whatever this returns. `roomId` is
|
|
47
|
+
* the room being opened. `tokenAuth` gives you one that checks signed join
|
|
48
|
+
* tokens, and `Accounts.authenticate` one that also knows signed-in players.
|
|
49
|
+
*/
|
|
50
|
+
authenticate(request: Request, roomId: string): Authenticated | Response | Promise<Authenticated | Response>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Forwards room traffic to the room's Durable Object: WebSocket upgrades on
|
|
54
|
+
* `{prefix}/{roomId}`, and plain HTTP requests on `{prefix}/{roomId}/...`,
|
|
55
|
+
* which reach the room's `onHttp` hook. Both are authenticated first.
|
|
56
|
+
* Returns `null` when the URL is not a room URL, so the caller can fall
|
|
57
|
+
* through to its other routes.
|
|
58
|
+
*/
|
|
59
|
+
export declare function routeToRoom(request: Request, options: RouteOptions): Promise<Response | null>;
|
|
60
|
+
/** How `routeToMatchmaker` finds queues and decides who may join them. */
|
|
61
|
+
export interface MatchmakerRouteOptions {
|
|
62
|
+
/** Durable Object namespace of your `Matchmaker` class, e.g. `env.MATCHMAKERS`. Each queue is one object in it. */
|
|
63
|
+
matchmakers: RoomNamespace;
|
|
64
|
+
/** URL prefix queues live under. Defaults to `/matchmaking`. */
|
|
65
|
+
prefix?: string;
|
|
66
|
+
/** Same contract as for rooms: the player's id (or `{ id, name }`), or a `Response` to refuse. `queue` is the queue name. */
|
|
67
|
+
authenticate(request: Request, queue: string): Authenticated | Response | Promise<Authenticated | Response>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Forwards `{prefix}/{queue}` WebSocket upgrades to a matchmaker. Each queue
|
|
71
|
+
* name is its own Durable Object, so `/matchmaking/ranked` and
|
|
72
|
+
* `/matchmaking/casual` fill separately. Returns `null` for other URLs.
|
|
73
|
+
*/
|
|
74
|
+
export declare function routeToMatchmaker(request: Request, options: MatchmakerRouteOptions): Promise<Response | null>;
|
|
75
|
+
/**
|
|
76
|
+
* The Durable Object location hint closest to where a request came from,
|
|
77
|
+
* worked out from Cloudflare's `request.cf`. Undefined when that is missing,
|
|
78
|
+
* as it is in local development.
|
|
79
|
+
*/
|
|
80
|
+
export declare function locationHintFor(request: Request): DurableObjectLocationHint | undefined;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/** Header the gateway uses to tell a room who is connecting. Never trusted from the client. */
|
|
2
|
+
export const PLAYER_HEADER = "x-tickflare-player";
|
|
3
|
+
/** Header the gateway uses to tell a room the id it is known by. */
|
|
4
|
+
export const ROOM_HEADER = "x-tickflare-room";
|
|
5
|
+
/** Header the gateway uses to tell a room the player's name, URI-encoded. Never trusted from the client. */
|
|
6
|
+
export const NAME_HEADER = "x-tickflare-name";
|
|
7
|
+
/** Header the gateway uses to tell a matchmaker roughly where a player is. */
|
|
8
|
+
export const HINT_HEADER = "x-tickflare-hint";
|
|
9
|
+
/** Header the gateway uses to tell a matchmaker the player's party, as `size:id`. Never trusted from the client. */
|
|
10
|
+
export const PARTY_HEADER = "x-tickflare-party";
|
|
11
|
+
const ROOM_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
12
|
+
/** Visible ASCII without spaces: safe as a header value and as a WebSocket tag. */
|
|
13
|
+
const PLAYER_ID = /^[\x21-\x7E]{1,128}$/;
|
|
14
|
+
/** Up to 64 characters without control characters. */
|
|
15
|
+
const PLAYER_NAME = /^[^\p{Cc}]{1,64}$/u;
|
|
16
|
+
/** Visible ASCII without spaces, as player ids are. */
|
|
17
|
+
const PARTY_ID = /^[\x21-\x7E]{1,64}$/;
|
|
18
|
+
/** The most members a party can have. */
|
|
19
|
+
const MOST_IN_A_PARTY = 64;
|
|
20
|
+
/**
|
|
21
|
+
* Forwards room traffic to the room's Durable Object: WebSocket upgrades on
|
|
22
|
+
* `{prefix}/{roomId}`, and plain HTTP requests on `{prefix}/{roomId}/...`,
|
|
23
|
+
* which reach the room's `onHttp` hook. Both are authenticated first.
|
|
24
|
+
* Returns `null` when the URL is not a room URL, so the caller can fall
|
|
25
|
+
* through to its other routes.
|
|
26
|
+
*/
|
|
27
|
+
export async function routeToRoom(request, options) {
|
|
28
|
+
const prefix = (options.prefix ?? "/rooms").replace(/\/+$/, "");
|
|
29
|
+
const { pathname } = new URL(request.url);
|
|
30
|
+
if (!pathname.startsWith(`${prefix}/`))
|
|
31
|
+
return null;
|
|
32
|
+
const [roomId = "", ...rest] = pathname.slice(prefix.length + 1).split("/");
|
|
33
|
+
const upgrade = request.headers.get("Upgrade")?.toLowerCase() === "websocket";
|
|
34
|
+
if (!ROOM_ID.test(roomId) || (upgrade && rest.length > 0)) {
|
|
35
|
+
return new Response("Invalid room id", { status: 400 });
|
|
36
|
+
}
|
|
37
|
+
if (!upgrade && rest.length === 0) {
|
|
38
|
+
return new Response("Expected a WebSocket upgrade", { status: 426 });
|
|
39
|
+
}
|
|
40
|
+
const identity = checkIdentity(await options.authenticate(request, roomId));
|
|
41
|
+
if (identity instanceof Response)
|
|
42
|
+
return identity;
|
|
43
|
+
const headers = identityHeaders(request, identity);
|
|
44
|
+
headers.set(ROOM_HEADER, roomId);
|
|
45
|
+
const stub = options.rooms.get(options.rooms.idFromName(roomId));
|
|
46
|
+
return stub.fetch(new Request(request, { headers }));
|
|
47
|
+
}
|
|
48
|
+
/** What `authenticate` returned, as an id and maybe a name, or the answer for something that cannot be a player. */
|
|
49
|
+
function checkIdentity(identity) {
|
|
50
|
+
if (identity instanceof Response)
|
|
51
|
+
return identity;
|
|
52
|
+
const id = typeof identity === "object" && identity !== null ? identity.id : identity;
|
|
53
|
+
const name = typeof identity === "object" && identity !== null ? identity.name : undefined;
|
|
54
|
+
const party = typeof identity === "object" && identity !== null ? identity.party : undefined;
|
|
55
|
+
// An `authenticate` that falls off its end returns undefined. That is nobody, not a player.
|
|
56
|
+
if (typeof id !== "string") {
|
|
57
|
+
return new Response("Not authenticated", { status: 401 });
|
|
58
|
+
}
|
|
59
|
+
// "__proto__" would do damage as a key in a game's state.
|
|
60
|
+
if (!PLAYER_ID.test(id) || id === "__proto__") {
|
|
61
|
+
return new Response("Invalid player id", { status: 400 });
|
|
62
|
+
}
|
|
63
|
+
if (name !== undefined && (typeof name !== "string" || !PLAYER_NAME.test(name))) {
|
|
64
|
+
return new Response("Invalid player name", { status: 400 });
|
|
65
|
+
}
|
|
66
|
+
const checked = name === undefined ? { id } : { id, name };
|
|
67
|
+
if (party !== undefined) {
|
|
68
|
+
const { id: partyId, size } = (party ?? {});
|
|
69
|
+
if (typeof partyId !== "string" || !PARTY_ID.test(partyId) || !Number.isInteger(size) || size < 1 || size > MOST_IN_A_PARTY) {
|
|
70
|
+
return new Response("Invalid party", { status: 400 });
|
|
71
|
+
}
|
|
72
|
+
checked.party = { id: partyId, size: size };
|
|
73
|
+
}
|
|
74
|
+
return checked;
|
|
75
|
+
}
|
|
76
|
+
/** The request's headers with the gateway's own: anything the client sent under the same names is replaced or dropped. */
|
|
77
|
+
function identityHeaders(request, identity) {
|
|
78
|
+
const headers = new Headers(request.headers);
|
|
79
|
+
headers.set(PLAYER_HEADER, identity.id);
|
|
80
|
+
if (identity.name === undefined)
|
|
81
|
+
headers.delete(NAME_HEADER);
|
|
82
|
+
else
|
|
83
|
+
headers.set(NAME_HEADER, encodeURIComponent(identity.name));
|
|
84
|
+
if (identity.party === undefined)
|
|
85
|
+
headers.delete(PARTY_HEADER);
|
|
86
|
+
else
|
|
87
|
+
headers.set(PARTY_HEADER, `${identity.party.size}:${identity.party.id}`);
|
|
88
|
+
return headers;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Forwards `{prefix}/{queue}` WebSocket upgrades to a matchmaker. Each queue
|
|
92
|
+
* name is its own Durable Object, so `/matchmaking/ranked` and
|
|
93
|
+
* `/matchmaking/casual` fill separately. Returns `null` for other URLs.
|
|
94
|
+
*/
|
|
95
|
+
export async function routeToMatchmaker(request, options) {
|
|
96
|
+
const prefix = (options.prefix ?? "/matchmaking").replace(/\/+$/, "");
|
|
97
|
+
const { pathname } = new URL(request.url);
|
|
98
|
+
if (!pathname.startsWith(`${prefix}/`))
|
|
99
|
+
return null;
|
|
100
|
+
const queue = pathname.slice(prefix.length + 1);
|
|
101
|
+
if (!ROOM_ID.test(queue)) {
|
|
102
|
+
return new Response("Invalid queue name", { status: 400 });
|
|
103
|
+
}
|
|
104
|
+
if (request.headers.get("Upgrade")?.toLowerCase() !== "websocket") {
|
|
105
|
+
return new Response("Expected a WebSocket upgrade", { status: 426 });
|
|
106
|
+
}
|
|
107
|
+
const identity = checkIdentity(await options.authenticate(request, queue));
|
|
108
|
+
if (identity instanceof Response)
|
|
109
|
+
return identity;
|
|
110
|
+
const headers = identityHeaders(request, identity);
|
|
111
|
+
const hint = locationHintFor(request);
|
|
112
|
+
if (hint)
|
|
113
|
+
headers.set(HINT_HEADER, hint);
|
|
114
|
+
else
|
|
115
|
+
headers.delete(HINT_HEADER);
|
|
116
|
+
const stub = options.matchmakers.get(options.matchmakers.idFromName(queue));
|
|
117
|
+
return stub.fetch(new Request(request, { headers }));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The Durable Object location hint closest to where a request came from,
|
|
121
|
+
* worked out from Cloudflare's `request.cf`. Undefined when that is missing,
|
|
122
|
+
* as it is in local development.
|
|
123
|
+
*/
|
|
124
|
+
export function locationHintFor(request) {
|
|
125
|
+
const cf = request.cf;
|
|
126
|
+
const longitude = Number(cf?.longitude);
|
|
127
|
+
switch (cf?.continent) {
|
|
128
|
+
case "NA":
|
|
129
|
+
return longitude < -100 ? "wnam" : "enam";
|
|
130
|
+
case "SA":
|
|
131
|
+
return "sam";
|
|
132
|
+
case "EU":
|
|
133
|
+
return longitude > 20 ? "eeur" : "weur";
|
|
134
|
+
case "AS":
|
|
135
|
+
return longitude < 60 ? "me" : "apac";
|
|
136
|
+
case "OC":
|
|
137
|
+
return "oc";
|
|
138
|
+
case "AF":
|
|
139
|
+
return "afr";
|
|
140
|
+
default:
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=gateway.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway.js","sourceRoot":"","sources":["../../src/server/gateway.ts"],"names":[],"mappings":"AAEA,+FAA+F;AAC/F,MAAM,CAAC,MAAM,aAAa,GAAG,oBAAoB,CAAC;AAClD,oEAAoE;AACpE,MAAM,CAAC,MAAM,WAAW,GAAG,kBAAkB,CAAC;AAC9C,4GAA4G;AAC5G,MAAM,CAAC,MAAM,WAAW,GAAG,kBAAkB,CAAC;AAC9C,8EAA8E;AAC9E,MAAM,CAAC,MAAM,WAAW,GAAG,kBAAkB,CAAC;AAC9C,oHAAoH;AACpH,MAAM,CAAC,MAAM,YAAY,GAAG,mBAAmB,CAAC;AAEhD,MAAM,OAAO,GAAG,uBAAuB,CAAC;AACxC,mFAAmF;AACnF,MAAM,SAAS,GAAG,sBAAsB,CAAC;AACzC,sDAAsD;AACtD,MAAM,WAAW,GAAG,oBAAoB,CAAC;AACzC,uDAAuD;AACvD,MAAM,QAAQ,GAAG,qBAAqB,CAAC;AACvC,yCAAyC;AACzC,MAAM,eAAe,GAAG,EAAE,CAAC;AAkD3B;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,OAAgB,EAChB,OAAqB;IAErB,MAAM,MAAM,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,QAAQ,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAChE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;I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AAE,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;;QACpC,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAEjC,MAAM,IAAI,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;IAC5E,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;AACvD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,OAAgB;IAC9C,MAAM,EAAE,GAAI,OAA+D,CAAC,EAAE,CAAC;IAC/E,MAAM,SAAS,GAAG,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;IACxC,QAAQ,EAAE,EAAE,SAAS,EAAE,CAAC;QACtB,KAAK,IAAI;YACP,OAAO,SAAS,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;QAC5C,KAAK,IAAI;YACP,OAAO,KAAK,CAAC;QACf,KAAK,IAAI;YACP,OAAO,SAAS,GAAG,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;QAC1C,KAAK,IAAI;YACP,OAAO,SAAS,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;QACxC,KAAK,IAAI;YACP,OAAO,IAAI,CAAC;QACd,KAAK,IAAI;YACP,OAAO,KAAK,CAAC;QACf;YACE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server side: rooms, the gateway, join tokens, accounts, matchmaking,
|
|
3
|
+
* match results and the helpers for worlds split across rooms. Runs in a
|
|
4
|
+
* Cloudflare Worker.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module tickflare
|
|
8
|
+
*/
|
|
9
|
+
export { Room, Rejection, reject, type ActOutcome } from "./room.js";
|
|
10
|
+
export { plain } from "./tracked.js";
|
|
11
|
+
export { LockstepRoom, type LockstepCommand, type LockstepState } from "./lockstep.js";
|
|
12
|
+
export { fingerprint, runReplay, seededRandom, stepThrough, type CatchUp, type LockstepView, type TurnsMessage, type Replay, type SeededRandom, type Simulation, type Snapshot, type TurnEntry, } from "../lockstep.js";
|
|
13
|
+
export { Matchmaker } from "./matchmaker.js";
|
|
14
|
+
export { createJoinToken, tokenAuth, verifyJoinToken, type JoinTokenClaims, type Party, type TokenAuthOptions, type VerifiedJoinToken, } from "./tokens.js";
|
|
15
|
+
export { cellOf, shardFor, type Cell } from "../world.js";
|
|
16
|
+
export { MATCHES_SCHEMA, recentMatches, recordMatches, type MatchRecord } from "./matches.js";
|
|
17
|
+
export { ACCOUNTS_SCHEMA, AccountError, Accounts, hashPassword, verifyPassword, type AccountsOptions, type DataPolicy, type Identity, type ImportedAccount, type LeaderboardEntry, type LeaderboardPage, type OwnAccount, type PasswordReset, type PublicProfile, } from "./accounts.js";
|
|
18
|
+
export { locationHintFor, routeToMatchmaker, routeToRoom, NAME_HEADER, PLAYER_HEADER, type Authenticated, type MatchmakerRouteOptions, type RoomNamespace, type RouteOptions, } from "./gateway.js";
|
|
19
|
+
export { applyPatch, diff, type ArrayPatch, type Patch } from "../patch.js";
|
|
20
|
+
export { decodePatchFrame } from "../binary.js";
|
|
21
|
+
export { CloseCode, PROTOCOL_VERSION, type AckFrame, type ClockFrame, type ClientMessage, type CommandFrame, type ErrorFrame, type LeaveFrame, type MatchFound, type MatchResult, type MessageFrame, type PatchFrame, type Player, type PresenceFrame, type RejectFrame, type ServerMessage, type StateFrame, type WelcomeFrame, } from "../protocol.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server side: rooms, the gateway, join tokens, accounts, matchmaking,
|
|
3
|
+
* match results and the helpers for worlds split across rooms. Runs in a
|
|
4
|
+
* Cloudflare Worker.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module tickflare
|
|
8
|
+
*/
|
|
9
|
+
export { Room, Rejection, reject } from "./room.js";
|
|
10
|
+
export { plain } from "./tracked.js";
|
|
11
|
+
export { LockstepRoom } from "./lockstep.js";
|
|
12
|
+
export { fingerprint, runReplay, seededRandom, stepThrough, } from "../lockstep.js";
|
|
13
|
+
export { Matchmaker } from "./matchmaker.js";
|
|
14
|
+
export { createJoinToken, tokenAuth, verifyJoinToken, } from "./tokens.js";
|
|
15
|
+
export { cellOf, shardFor } from "../world.js";
|
|
16
|
+
export { MATCHES_SCHEMA, recentMatches, recordMatches } from "./matches.js";
|
|
17
|
+
export { ACCOUNTS_SCHEMA, AccountError, Accounts, hashPassword, verifyPassword, } from "./accounts.js";
|
|
18
|
+
export { locationHintFor, routeToMatchmaker, routeToRoom, NAME_HEADER, PLAYER_HEADER, } from "./gateway.js";
|
|
19
|
+
export { applyPatch, diff } from "../patch.js";
|
|
20
|
+
export { decodePatchFrame } from "../binary.js";
|
|
21
|
+
export { CloseCode, PROTOCOL_VERSION, } from "../protocol.js";
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAmB,MAAM,WAAW,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,YAAY,EAA4C,MAAM,eAAe,CAAC;AACvF,OAAO,EACL,WAAW,EACX,SAAS,EACT,YAAY,EACZ,WAAW,GASZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EACL,eAAe,EACf,SAAS,EACT,eAAe,GAKhB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAa,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,aAAa,EAAoB,MAAM,cAAc,CAAC;AAC9F,OAAO,EACL,eAAe,EACf,YAAY,EACZ,QAAQ,EACR,YAAY,EACZ,cAAc,GAUf,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,eAAe,EACf,iBAAiB,EACjB,WAAW,EACX,WAAW,EACX,aAAa,GAKd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,UAAU,EAAE,IAAI,EAA+B,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EACL,SAAS,EACT,gBAAgB,GAiBjB,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A token bucket per key: it refills at `rate` a second and holds at most
|
|
3
|
+
* one second's worth (never less than one), so `rate` is both the sustained
|
|
4
|
+
* limit and the largest burst. A bucket belongs to the key, not to a
|
|
5
|
+
* connection: dropping and reconnecting does not refill it. Kept in memory
|
|
6
|
+
* only: a room that restarts starts everyone afresh.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RateLimiter {
|
|
9
|
+
#private;
|
|
10
|
+
/** Take one from `key`'s bucket. False when it is empty. An infinite rate always passes. */
|
|
11
|
+
spend(key: string, rate: number): boolean;
|
|
12
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** How often idle buckets are cleared out. */
|
|
2
|
+
const SWEEP_MS = 10_000;
|
|
3
|
+
/**
|
|
4
|
+
* A token bucket per key: it refills at `rate` a second and holds at most
|
|
5
|
+
* one second's worth (never less than one), so `rate` is both the sustained
|
|
6
|
+
* limit and the largest burst. A bucket belongs to the key, not to a
|
|
7
|
+
* connection: dropping and reconnecting does not refill it. Kept in memory
|
|
8
|
+
* only: a room that restarts starts everyone afresh.
|
|
9
|
+
*/
|
|
10
|
+
export class RateLimiter {
|
|
11
|
+
#buckets = new Map();
|
|
12
|
+
#sweptAt = 0;
|
|
13
|
+
/** Take one from `key`'s bucket. False when it is empty. An infinite rate always passes. */
|
|
14
|
+
spend(key, rate) {
|
|
15
|
+
if (rate === Number.POSITIVE_INFINITY)
|
|
16
|
+
return true;
|
|
17
|
+
if (!(rate > 0))
|
|
18
|
+
return false;
|
|
19
|
+
// A rate below one a second still lets single frames through, just further apart.
|
|
20
|
+
const size = Math.max(1, rate);
|
|
21
|
+
const now = Date.now();
|
|
22
|
+
if (now - this.#sweptAt >= SWEEP_MS)
|
|
23
|
+
this.#sweep(now, rate, size);
|
|
24
|
+
const bucket = this.#buckets.get(key) ?? { left: size, at: now };
|
|
25
|
+
bucket.left = Math.min(size, bucket.left + ((now - bucket.at) / 1000) * rate);
|
|
26
|
+
bucket.at = now;
|
|
27
|
+
this.#buckets.set(key, bucket);
|
|
28
|
+
if (bucket.left < 1)
|
|
29
|
+
return false;
|
|
30
|
+
bucket.left -= 1;
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
/** Forgets the keys that have been quiet for long enough to be full again. */
|
|
34
|
+
#sweep(now, rate, size) {
|
|
35
|
+
this.#sweptAt = now;
|
|
36
|
+
const refillMs = (size / rate) * 1000;
|
|
37
|
+
for (const [key, bucket] of this.#buckets) {
|
|
38
|
+
if (now - bucket.at >= refillMs)
|
|
39
|
+
this.#buckets.delete(key);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=limiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limiter.js","sourceRoot":"","sources":["../../src/server/limiter.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,MAAM,QAAQ,GAAG,MAAM,CAAC;AAExB;;;;;;GAMG;AACH,MAAM,OAAO,WAAW;IACtB,QAAQ,G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M,QAAQ,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;QACtC,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC1C,IAAI,GAAG,GAAG,MAAM,CAAC,EAAE,IAAI,QAAQ;gBAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC7D,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { LockstepView, Replay, TurnEntry } from "../lockstep.js";
|
|
2
|
+
import type { Player } from "../protocol.js";
|
|
3
|
+
import { Room } from "./room.js";
|
|
4
|
+
/** What a lockstep room keeps: what clients see, and the inputs waiting for the next turn. */
|
|
5
|
+
export interface LockstepState<Input> extends LockstepView<Input> {
|
|
6
|
+
/** Inputs and events waiting for the next turn. Not sent to clients. */
|
|
7
|
+
pending: TurnEntry<Input>[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* What a `LockstepClient` sends: an input (`i`), its simulation's hash after
|
|
11
|
+
* turn `n` (`h`), or, to a room whose `turnClock` is `"players"`, a request
|
|
12
|
+
* to close the turns that are due (`t`).
|
|
13
|
+
*/
|
|
14
|
+
export type LockstepCommand<Input> = {
|
|
15
|
+
/** A request to close the turns that are due. */
|
|
16
|
+
k: "t";
|
|
17
|
+
} | {
|
|
18
|
+
/** An input. */
|
|
19
|
+
k: "i";
|
|
20
|
+
/** The input, before `parseInput`. */
|
|
21
|
+
d: Input;
|
|
22
|
+
} | {
|
|
23
|
+
/** A hash of the client's simulation. */
|
|
24
|
+
k: "h";
|
|
25
|
+
/** The turn the simulation was at. */
|
|
26
|
+
n: number;
|
|
27
|
+
/** The hash. */
|
|
28
|
+
h: string;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* A lockstep room. It never runs the game: it validates inputs, orders them
|
|
32
|
+
* into turns at `tickRate` turns a second, records every turn in which
|
|
33
|
+
* something happened, and sends all players the same stream. Players coming
|
|
34
|
+
* and going are part of that stream, so every simulation sees them at the
|
|
35
|
+
* same turn.
|
|
36
|
+
*
|
|
37
|
+
* The recording is served at `{prefix}/{roomId}/replay`. Clients read it when
|
|
38
|
+
* they (re)connect, to catch up, and it doubles as the match replay.
|
|
39
|
+
*
|
|
40
|
+
* By default the room closes the turns itself, on a timer, and so is awake
|
|
41
|
+
* and billed for as long as the match lasts. Set `turnClock = "players"` and
|
|
42
|
+
* it sleeps between turns instead: see `turnClock`.
|
|
43
|
+
*
|
|
44
|
+
* If you override `onJoin`, `onDisconnect` or `onReconnect`, call `super` so
|
|
45
|
+
* the event still reaches the turn stream. A seat that is given up, whether
|
|
46
|
+
* the player left, timed out or was removed with `removePlayer`, is
|
|
47
|
+
* announced to the stream for you.
|
|
48
|
+
*/
|
|
49
|
+
export declare abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<LockstepState<Input>, LockstepCommand<Input>, Env> {
|
|
50
|
+
#private;
|
|
51
|
+
/** Turns per second. */
|
|
52
|
+
protected tickRate: number;
|
|
53
|
+
/** Inputs one player may send within a single turn. More are refused. */
|
|
54
|
+
protected maxInputsPerTurn: number;
|
|
55
|
+
/**
|
|
56
|
+
* Who keeps time.
|
|
57
|
+
*
|
|
58
|
+
* `"room"`: the room closes a turn every `1 / tickRate` seconds on a timer
|
|
59
|
+
* of its own. It is awake for the whole match.
|
|
60
|
+
*
|
|
61
|
+
* `"players"`: the room has no timer. `LockstepClient` asks it for each
|
|
62
|
+
* turn as it falls due, one player's client at a time, and the room closes
|
|
63
|
+
* as many turns as its own clock says have passed, never more: a player
|
|
64
|
+
* can neither speed the game up nor hold it back while anyone else is
|
|
65
|
+
* connected. Between requests the room is asleep, which is most of the
|
|
66
|
+
* time. Orders take effect as soon as they do with `"room"`. Two things
|
|
67
|
+
* differ: the game stands still while nobody is connected to ask, and after
|
|
68
|
+
* a restart it skips ahead by up to `saveIntervalMs` of empty turns instead
|
|
69
|
+
* of going back by as much.
|
|
70
|
+
*/
|
|
71
|
+
protected turnClock: "room" | "players";
|
|
72
|
+
/**
|
|
73
|
+
* How often the room takes a snapshot of the simulation from its players,
|
|
74
|
+
* so that someone who joins, reloads or reconnects late starts from it and
|
|
75
|
+
* not from turn 1. 0 keeps none.
|
|
76
|
+
*
|
|
77
|
+
* The room has no simulation of its own. A snapshot is what one player's
|
|
78
|
+
* client saved, with `save`, at a turn every client agrees on, and it is
|
|
79
|
+
* kept once the hash it came with is the one another connected player
|
|
80
|
+
* reported for that turn; a player who is alone is taken at their word.
|
|
81
|
+
* The client that loads it hashes the result again before trusting it.
|
|
82
|
+
* Clients whose simulation has no `save`, `load` and `hash` send none and
|
|
83
|
+
* load none, and catch up from the recording as before.
|
|
84
|
+
*
|
|
85
|
+
* Only the latest is kept. The recording is not shortened: it is still the
|
|
86
|
+
* whole match.
|
|
87
|
+
*/
|
|
88
|
+
protected snapshotIntervalMs: number;
|
|
89
|
+
/** The longest snapshot the room keeps, in characters of JSON. A longer one is refused. */
|
|
90
|
+
protected maxSnapshotLength: number;
|
|
91
|
+
constructor(ctx: DurableObjectState, env: Env);
|
|
92
|
+
/**
|
|
93
|
+
* Turn an untrusted client payload into an input. Every client will feed
|
|
94
|
+
* what this returns to its simulation, so reject anything malformed here.
|
|
95
|
+
*/
|
|
96
|
+
protected abstract parseInput(raw: unknown): Input;
|
|
97
|
+
/** Two players reported different hashes for `turn`: their simulations have diverged. */
|
|
98
|
+
protected onDesync(_turn: number): void;
|
|
99
|
+
protected initialState(): LockstepState<Input>;
|
|
100
|
+
protected parseCommand(raw: unknown): LockstepCommand<Input>;
|
|
101
|
+
/** The room keeps time only when it is asked to: with players asking, it has no simulation to wake for. */
|
|
102
|
+
protected shouldTick(): boolean;
|
|
103
|
+
protected onCommand(player: Player, command: LockstepCommand<Input>): void;
|
|
104
|
+
protected onJoin(player: Player): void;
|
|
105
|
+
/** Every way of losing a seat ends here, so every simulation hears about it. */
|
|
106
|
+
protected removePlayer(playerId: string, reason?: string): void;
|
|
107
|
+
protected onDisconnect(player: Player): void;
|
|
108
|
+
protected onReconnect(player: Player): void;
|
|
109
|
+
/**
|
|
110
|
+
* Closes the turn: whatever arrived since the last one becomes this turn's
|
|
111
|
+
* entries. A turn with something in it is one row in the recording, written
|
|
112
|
+
* before any player is sent it; `#resume` is what lets the room itself be
|
|
113
|
+
* saved on its usual schedule instead of with every turn.
|
|
114
|
+
*/
|
|
115
|
+
protected onTick(): void;
|
|
116
|
+
protected view(): LockstepView<Input>;
|
|
117
|
+
/**
|
|
118
|
+
* `GET …/replay` is the recording, or the end of it for a client that is
|
|
119
|
+
* catching up. `POST …/snapshot` is a player's client sending a snapshot.
|
|
120
|
+
* If you override this, pass on what is not yours with `super.onHttp`.
|
|
121
|
+
*/
|
|
122
|
+
protected onHttp(request: Request, player: Player): Response | Promise<Response>;
|
|
123
|
+
/** Announces the end of the match and, if `matchBucket` is set, stores the recording as its replay. */
|
|
124
|
+
protected finishMatch(result?: Record<string, unknown>): void;
|
|
125
|
+
/** The recording so far. */
|
|
126
|
+
protected replay(): Replay<Input>;
|
|
127
|
+
}
|