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.
Files changed (125) hide show
  1. package/CHANGELOG.md +240 -0
  2. package/LICENSE +21 -0
  3. package/README.md +112 -2
  4. package/dist/binary.d.ts +91 -0
  5. package/dist/binary.js +466 -0
  6. package/dist/binary.js.map +1 -0
  7. package/dist/client/accounts.d.ts +193 -0
  8. package/dist/client/accounts.js +306 -0
  9. package/dist/client/accounts.js.map +1 -0
  10. package/dist/client/buffered.d.ts +88 -0
  11. package/dist/client/buffered.js +229 -0
  12. package/dist/client/buffered.js.map +1 -0
  13. package/dist/client/clock.d.ts +36 -0
  14. package/dist/client/clock.js +83 -0
  15. package/dist/client/clock.js.map +1 -0
  16. package/dist/client/draft.d.ts +17 -0
  17. package/dist/client/draft.js +133 -0
  18. package/dist/client/draft.js.map +1 -0
  19. package/dist/client/index.d.ts +182 -0
  20. package/dist/client/index.js +496 -0
  21. package/dist/client/index.js.map +1 -0
  22. package/dist/client/interpolate.d.ts +31 -0
  23. package/dist/client/interpolate.js +59 -0
  24. package/dist/client/interpolate.js.map +1 -0
  25. package/dist/client/lockstep.d.ts +86 -0
  26. package/dist/client/lockstep.js +339 -0
  27. package/dist/client/lockstep.js.map +1 -0
  28. package/dist/client/matchmaking.d.ts +21 -0
  29. package/dist/client/matchmaking.js +55 -0
  30. package/dist/client/matchmaking.js.map +1 -0
  31. package/dist/client/pacer.d.ts +22 -0
  32. package/dist/client/pacer.js +65 -0
  33. package/dist/client/pacer.js.map +1 -0
  34. package/dist/client/predict.d.ts +75 -0
  35. package/dist/client/predict.js +203 -0
  36. package/dist/client/predict.js.map +1 -0
  37. package/dist/client/token.d.ts +10 -0
  38. package/dist/client/token.js +15 -0
  39. package/dist/client/token.js.map +1 -0
  40. package/dist/lockstep.d.ts +154 -0
  41. package/dist/lockstep.js +140 -0
  42. package/dist/lockstep.js.map +1 -0
  43. package/dist/patch.d.ts +54 -0
  44. package/dist/patch.js +186 -0
  45. package/dist/patch.js.map +1 -0
  46. package/dist/protocol.d.ts +268 -0
  47. package/dist/protocol.js +54 -0
  48. package/dist/protocol.js.map +1 -0
  49. package/dist/server/accounts.d.ts +320 -0
  50. package/dist/server/accounts.js +1223 -0
  51. package/dist/server/accounts.js.map +1 -0
  52. package/dist/server/delta.d.ts +50 -0
  53. package/dist/server/delta.js +426 -0
  54. package/dist/server/delta.js.map +1 -0
  55. package/dist/server/frames.d.ts +15 -0
  56. package/dist/server/frames.js +23 -0
  57. package/dist/server/frames.js.map +1 -0
  58. package/dist/server/gateway.d.ts +80 -0
  59. package/dist/server/gateway.js +144 -0
  60. package/dist/server/gateway.js.map +1 -0
  61. package/dist/server/index.d.ts +21 -0
  62. package/dist/server/index.js +22 -0
  63. package/dist/server/index.js.map +1 -0
  64. package/dist/server/limiter.d.ts +12 -0
  65. package/dist/server/limiter.js +43 -0
  66. package/dist/server/limiter.js.map +1 -0
  67. package/dist/server/lockstep.d.ts +127 -0
  68. package/dist/server/lockstep.js +549 -0
  69. package/dist/server/lockstep.js.map +1 -0
  70. package/dist/server/matches.d.ts +31 -0
  71. package/dist/server/matches.js +69 -0
  72. package/dist/server/matches.js.map +1 -0
  73. package/dist/server/matchmaker.d.ts +121 -0
  74. package/dist/server/matchmaker.js +479 -0
  75. package/dist/server/matchmaker.js.map +1 -0
  76. package/dist/server/room.d.ts +402 -0
  77. package/dist/server/room.js +1896 -0
  78. package/dist/server/room.js.map +1 -0
  79. package/dist/server/signals.d.ts +18 -0
  80. package/dist/server/signals.js +37 -0
  81. package/dist/server/signals.js.map +1 -0
  82. package/dist/server/storage.d.ts +117 -0
  83. package/dist/server/storage.js +330 -0
  84. package/dist/server/storage.js.map +1 -0
  85. package/dist/server/tokens.d.ts +74 -0
  86. package/dist/server/tokens.js +117 -0
  87. package/dist/server/tokens.js.map +1 -0
  88. package/dist/server/tracked.d.ts +47 -0
  89. package/dist/server/tracked.js +270 -0
  90. package/dist/server/tracked.js.map +1 -0
  91. package/dist/world.d.ts +20 -0
  92. package/dist/world.js +21 -0
  93. package/dist/world.js.map +1 -0
  94. package/package.json +74 -4
  95. package/src/binary.ts +496 -0
  96. package/src/client/accounts.ts +423 -0
  97. package/src/client/buffered.ts +264 -0
  98. package/src/client/clock.ts +86 -0
  99. package/src/client/draft.ts +138 -0
  100. package/src/client/index.ts +631 -0
  101. package/src/client/interpolate.ts +60 -0
  102. package/src/client/lockstep.ts +398 -0
  103. package/src/client/matchmaking.ts +61 -0
  104. package/src/client/pacer.ts +67 -0
  105. package/src/client/predict.ts +239 -0
  106. package/src/client/token.ts +22 -0
  107. package/src/lockstep.ts +285 -0
  108. package/src/patch.ts +188 -0
  109. package/src/protocol.ts +300 -0
  110. package/src/server/accounts.ts +1474 -0
  111. package/src/server/delta.ts +467 -0
  112. package/src/server/frames.ts +23 -0
  113. package/src/server/gateway.ts +213 -0
  114. package/src/server/index.ts +86 -0
  115. package/src/server/limiter.ts +41 -0
  116. package/src/server/lockstep.ts +617 -0
  117. package/src/server/matches.ts +99 -0
  118. package/src/server/matchmaker.ts +537 -0
  119. package/src/server/room.ts +1999 -0
  120. package/src/server/signals.ts +46 -0
  121. package/src/server/storage.ts +392 -0
  122. package/src/server/tokens.ts +184 -0
  123. package/src/server/tracked.ts +260 -0
  124. package/src/world.ts +29 -0
  125. package/tools/rooms-report.mjs +168 -0
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Wire protocol shared by the room kernel and the client.
3
+ *
4
+ * Every frame is a JSON text frame, except the keepalive: the client sends the
5
+ * literal string "ping" and the runtime answers "pong" without waking the room.
6
+ */
7
+ /**
8
+ * The latest version of the protocol. A client asks for a version with `v`
9
+ * in the query when it connects, and the room's `welcome` frame says which
10
+ * it will speak: the one asked for, or this one if that is later. A client
11
+ * that does not ask is spoken to in version 1.
12
+ *
13
+ * Version 2 adds array patches: an array that changes can be sent element
14
+ * by element, as `[changes, length]` in a patch.
15
+ *
16
+ * Version 3 adds presence as changes: a `presence` frame can say who is new
17
+ * or different (`set`) and who is gone (`gone`) in place of the whole list.
18
+ *
19
+ * Version 4 adds the room's clock: while the simulation runs, `welcome`
20
+ * says where the clock is (`ms` and `epoch`), and `clock` frames say it
21
+ * again every few seconds, whenever it starts, stops or moves on.
22
+ */
23
+ export const PROTOCOL_VERSION = 4;
24
+ /** The keepalive a client sends, as a plain text frame. @internal */
25
+ export const PING = "ping";
26
+ /** The runtime's answer to `PING`, sent without waking the room. @internal */
27
+ export const PONG = "pong";
28
+ /** WebSocket close codes the room uses. The client does not reconnect after those from 4000 to 4099. */
29
+ export const CloseCode = {
30
+ /** The same player opened a newer connection. */
31
+ Replaced: 4000,
32
+ /** Every seat is taken. */
33
+ RoomFull: 4001,
34
+ /** `onJoin` refused the player; the close reason carries why. */
35
+ JoinRejected: 4002,
36
+ /** The player no longer has a seat: they left, or the room removed them. The close reason says which. */
37
+ Removed: 4003,
38
+ /**
39
+ * The room handed the player over to another room; the close reason is that
40
+ * room's id. The client reconnects there by itself.
41
+ */
42
+ Moved: 4004,
43
+ /**
44
+ * A ticking room heard nothing on this connection for `silentAfterMs`, not
45
+ * even a keepalive, and took it for dead. A client that is there after
46
+ * all connects again: this code is outside those it gives up on.
47
+ */
48
+ Silent: 4100,
49
+ };
50
+ /** Whether a client should stop reconnecting after this close code. @internal */
51
+ export function isFatalCloseCode(code) {
52
+ return code >= 4000 && code < 4100;
53
+ }
54
+ //# sourceMappingURL=protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol.js","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAElC,qEAAqE;AACrE,MAAM,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC;AAC3B,8EAA8E;AAC9E,MAAM,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC;AAuN3B,wGAAwG;AACxG,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,iDAAiD;IACjD,QAAQ,EAAE,IAAI;IACd,2BAA2B;IAC3B,QAAQ,EAAE,IAAI;IACd,iEAAiE;IACjE,YAAY,EAAE,IAAI;IAClB,yGAAyG;IACzG,OAAO,EAAE,IAAI;IACb;;;OAGG;IACH,KAAK,EAAE,IAAI;IACX;;;;OAIG;IACH,MAAM,EAAE,IAAI;CACJ,CAAC;AA8BX,iFAAiF;AACjF,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,OAAO,IAAI,IAAI,IAAI,IAAI,IAAI,GAAG,IAAI,CAAC;AACrC,CAAC"}
@@ -0,0 +1,320 @@
1
+ import { type Party } from "./tokens.js";
2
+ /** Who may read or write one key of a player's data. */
3
+ export interface DataPolicy {
4
+ /** "owner": only the player (and the server). "public": anyone, on the player's profile. Default "owner". */
5
+ read?: "owner" | "public";
6
+ /** "owner": the player may set it from the client. "server": only server code may. Default "server". */
7
+ write?: "owner" | "server";
8
+ }
9
+ /** How `Accounts` behaves. Only `secret` is required. */
10
+ export interface AccountsOptions {
11
+ /** Signs guest and join tokens. The same secret `tokenAuth` uses. At least 16 characters. */
12
+ secret: string;
13
+ /** Where the HTTP API is served. Default "/api/accounts". */
14
+ prefix?: string;
15
+ /** The session cookie's name. Default "tf_session". */
16
+ cookie?: string;
17
+ /**
18
+ * The session cookie's SameSite. "Lax" (the default) keeps players signed in
19
+ * when they follow a link to your game; "Strict" does not send the cookie on
20
+ * that first page load. A cookie set for an allowed origin of its own is
21
+ * "None" either way, or it could not be used there.
22
+ */
23
+ sameSite?: "Lax" | "Strict";
24
+ /** How long a session lasts since it was last used, in milliseconds. Default 30 days. */
25
+ sessionTtlMs?: number;
26
+ /** How long a guest token lasts, in seconds. A guest renews it, keeping their id, before then. Default 30 days. */
27
+ guestTtlSeconds?: number;
28
+ /** How long a join token from `/token` lasts, in seconds. Default ten minutes. */
29
+ joinTokenTtlSeconds?: number;
30
+ /**
31
+ * Browser origins, besides the API's own, that may make signed-in requests
32
+ * with the session cookie: where your game's pages are served from if that
33
+ * is another origin. Anything else is refused, which is what stops another
34
+ * site from acting as a signed-in player.
35
+ */
36
+ allowedOrigins?: string[];
37
+ /** Policies per data key. Keys not listed are private and server-written. */
38
+ data?: Record<string, DataPolicy>;
39
+ /** The largest value one data key may hold, in bytes of JSON. Default 64 KiB. */
40
+ maxDataBytes?: number;
41
+ /** How many data keys one player may have. Default 100. */
42
+ maxDataKeys?: number;
43
+ /**
44
+ * Your own rules for account names, in place of the default (3 to 24
45
+ * letters, digits, spaces, dots, dashes and underscores). Return the name
46
+ * as it should be stored, or null to refuse it. Uniqueness is checked after,
47
+ * ignoring case, spaces, dots, dashes and underscores.
48
+ */
49
+ normalizeName?(name: string): string | null;
50
+ /**
51
+ * Checks a password against a hash this module did not make, so accounts
52
+ * imported from another service keep working. On success the password is
53
+ * hashed again the usual way. Return false for hashes you do not recognise.
54
+ */
55
+ verifyImportedPassword?(hash: string, password: string): boolean | Promise<boolean>;
56
+ /**
57
+ * Sends a player the link to set a new password: Tickflare makes the
58
+ * token, and sending the email is yours to do, with whichever mail service
59
+ * you use. Setting this turns `POST /forgot` on. Put `reset.token` in a
60
+ * link to a page of yours that asks for a new password and sends both to
61
+ * `POST /reset`.
62
+ */
63
+ sendPasswordReset?(reset: PasswordReset, request: Request): void | Promise<void>;
64
+ /** How long a password reset token lasts, in seconds. Default one hour. */
65
+ passwordResetTtlSeconds?: number;
66
+ }
67
+ /** What `sendPasswordReset` is given: whom to write to, and the token that lets them set a new password. */
68
+ export interface PasswordReset {
69
+ /** The player id. */
70
+ id: string;
71
+ /** The account name, to remind the player of it. */
72
+ name: string;
73
+ /** The account's email address. */
74
+ email: string;
75
+ /** Lets its holder set a new password, once. Only its hash is stored. */
76
+ token: string;
77
+ /** When the token stops working, in epoch milliseconds. */
78
+ expiresAt: number;
79
+ }
80
+ /** Who is behind a request. */
81
+ export interface Identity {
82
+ /** The player id: the account's, or the guest's. */
83
+ id: string;
84
+ /** The account name, or undefined for a guest without an account. */
85
+ name?: string;
86
+ /** True for a guest without an account. */
87
+ guest: boolean;
88
+ /** The party the player queues with, if their join token carried one. See `JoinTokenClaims.party`. */
89
+ party?: Party;
90
+ }
91
+ /** What anyone may see of a player. */
92
+ export interface PublicProfile {
93
+ /** The player id. */
94
+ id: string;
95
+ /** The account name. */
96
+ name: string;
97
+ /** When the account was made, in epoch milliseconds. */
98
+ createdAt: number;
99
+ /** The data keys whose policy is `read: "public"`. */
100
+ data: Record<string, unknown>;
101
+ /** Every stat the player has. */
102
+ stats: Record<string, number>;
103
+ }
104
+ /** What a player sees of their own account. */
105
+ export interface OwnAccount {
106
+ /** The player id. */
107
+ id: string;
108
+ /** The account name. */
109
+ name: string;
110
+ /** The email address, if one was given. */
111
+ email: string | null;
112
+ /** When the account was made, in epoch milliseconds. */
113
+ createdAt: number;
114
+ /** Whether the account has a recovery code. The code itself is shown once, when it is made. */
115
+ recoveryCode: boolean;
116
+ /** Every data key the player has, private ones included. */
117
+ data: Record<string, unknown>;
118
+ /** Every stat the player has. */
119
+ stats: Record<string, number>;
120
+ }
121
+ /** One line of a leaderboard. */
122
+ export interface LeaderboardEntry {
123
+ /** 1 for the highest value. Players with the same value share a rank. */
124
+ rank: number;
125
+ /** The player id. */
126
+ id: string;
127
+ /** The account name. */
128
+ name: string;
129
+ /** The player's value of the stat. */
130
+ value: number;
131
+ }
132
+ /** A page of a leaderboard, highest value first. */
133
+ export interface LeaderboardPage {
134
+ /** The stat it ranks. */
135
+ stat: string;
136
+ /** This page's lines. */
137
+ entries: LeaderboardEntry[];
138
+ /** Players with a value for this stat, in total. */
139
+ total: number;
140
+ }
141
+ /** An account taken over from another service. See `Accounts.importAccount`. */
142
+ export interface ImportedAccount {
143
+ /** The player id to keep, so references to it elsewhere stay valid. */
144
+ id: string;
145
+ /** The account name. It must follow the name rules and be free. */
146
+ name: string;
147
+ /** The email address, if there is one. */
148
+ email?: string | null;
149
+ /** A hash `verifyImportedPassword` understands. */
150
+ passwordHash: string;
151
+ /** When the account was made, in epoch milliseconds. Defaults to now. */
152
+ createdAt?: number;
153
+ }
154
+ /** The tables, in case you prefer to apply them as a migration. `Accounts` creates them on first use. */
155
+ export declare const ACCOUNTS_SCHEMA = "\nCREATE TABLE IF NOT EXISTS tickflare_accounts (\n id TEXT PRIMARY KEY,\n name TEXT NOT NULL,\n name_key TEXT NOT NULL UNIQUE,\n email TEXT,\n password_hash TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n password_changed_at INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS tickflare_sessions (\n token_hash TEXT PRIMARY KEY,\n account_id TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n expires_at INTEGER NOT NULL\n);\nCREATE INDEX IF NOT EXISTS tickflare_sessions_account ON tickflare_sessions (account_id);\nCREATE TABLE IF NOT EXISTS tickflare_player_data (\n player TEXT NOT NULL,\n key TEXT NOT NULL,\n value TEXT NOT NULL,\n updated_at INTEGER NOT NULL,\n PRIMARY KEY (player, key)\n);\nCREATE TABLE IF NOT EXISTS tickflare_player_stats (\n player TEXT NOT NULL,\n stat TEXT NOT NULL,\n value REAL NOT NULL,\n updated_at INTEGER NOT NULL,\n PRIMARY KEY (player, stat)\n);\nCREATE INDEX IF NOT EXISTS tickflare_player_stats_board ON tickflare_player_stats (stat, value DESC);\nCREATE TABLE IF NOT EXISTS tickflare_stat_awards (\n key TEXT PRIMARY KEY,\n awarded_at INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS tickflare_sign_in_failures (\n key TEXT PRIMARY KEY,\n failures INTEGER NOT NULL,\n window_start INTEGER NOT NULL\n);\nCREATE TABLE IF NOT EXISTS tickflare_recovery (\n token_hash TEXT PRIMARY KEY,\n account_id TEXT NOT NULL,\n kind TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n expires_at INTEGER\n);\nCREATE INDEX IF NOT EXISTS tickflare_recovery_account ON tickflare_recovery (account_id);\n";
156
+ /** Raised for requests that cannot be done. `status` is the HTTP status the API answers with. */
157
+ export declare class AccountError extends Error {
158
+ /** The HTTP status the API answers with, e.g. 409. */
159
+ readonly status: number;
160
+ /** Machine-readable, e.g. `name_taken` or `invalid_credentials`. */
161
+ readonly code: string;
162
+ constructor(status: number, code: string, message: string);
163
+ }
164
+ /**
165
+ * A small player backend on D1: accounts with a name and a password,
166
+ * sessions, guests who can sign up without losing what they earned,
167
+ * per-player data, stats and leaderboards, public profiles, and a player's
168
+ * right to download or delete everything about them.
169
+ *
170
+ * Make one per request; it holds no state of its own beyond its options.
171
+ *
172
+ * const accounts = new Accounts(env.DB, { secret: env.JOIN_SECRET });
173
+ * return (await accounts.route(request)) ?? new Response("Not found", { status: 404 });
174
+ */
175
+ export declare class Accounts {
176
+ #private;
177
+ constructor(db: D1Database, options: AccountsOptions);
178
+ /** Creates the tables if they are not there yet. Called for you before the first query. */
179
+ migrate(): Promise<void>;
180
+ /**
181
+ * Who is making this request: the account its session cookie or bearer
182
+ * session token belongs to, else the player a join token (`?token=` or
183
+ * bearer) speaks for, else nobody. `scope` is checked against scoped join
184
+ * tokens, as `tokenAuth` does.
185
+ */
186
+ identify(request: Request, scope?: string): Promise<Identity | null>;
187
+ /**
188
+ * For `routeToRoom` and `routeToMatchmaker`: lets in signed-in players and
189
+ * holders of a join token, guests included, and answers 401 otherwise. The
190
+ * room learns the player's name too.
191
+ */
192
+ readonly authenticate: (request: Request, scope: string) => Promise<Identity | Response>;
193
+ /** Like `authenticate`, for places that need an account: guests get 401 too. */
194
+ readonly requireAccount: (request: Request, scope: string) => Promise<Identity | Response>;
195
+ /**
196
+ * Answers requests to the accounts API, under `prefix`; null for any other
197
+ * URL so your Worker can try its other routes.
198
+ *
199
+ * POST /guest a guest player and a join token for them
200
+ * POST /register { name, password, email?, guestToken? }
201
+ * POST /login { name, password }
202
+ * POST /logout
203
+ * GET /me your account, data and stats
204
+ * DELETE /me { password }: delete your account and everything in it
205
+ * POST /password { current, next }: change it and sign out everywhere else
206
+ * POST /recovery-code { password }: a code that sets a new password once, shown this one time
207
+ * POST /recover { name, code, password }: set a new password with that code, and sign in
208
+ * POST /forgot { name } or { email }: send a reset link, if `sendPasswordReset` is set
209
+ * POST /reset { token, password }: set a new password with the link's token, and sign in
210
+ * POST /name { name }: take another name
211
+ * POST /email { password, email }: set or (with null) remove the email address
212
+ * GET /export everything stored about you, as a JSON download
213
+ * POST /token { scope? }: a short-lived join token for a room or queue
214
+ * GET /data/:key PUT /data/:key { value } DELETE /data/:key
215
+ * GET /players/:id a public profile
216
+ * GET /leaderboards/:stat?limit=&offset=
217
+ *
218
+ * Pass the Worker's `ctx` as well if you reset passwords by email: the
219
+ * email is then sent after the answer, which takes as long whether or not
220
+ * there was an account to write to.
221
+ */
222
+ route(request: Request, ctx?: Pick<ExecutionContext, "waitUntil">): Promise<Response | null>;
223
+ /** Creates an account. `id` lets a guest keep theirs; it is made up otherwise. */
224
+ register(input: {
225
+ name: string;
226
+ password: string;
227
+ email?: string | null;
228
+ id?: string;
229
+ }): Promise<Identity>;
230
+ /** Brings in an account from another service. Its password works through `verifyImportedPassword`. */
231
+ importAccount(account: ImportedAccount): Promise<void>;
232
+ /** Checks a name and password. Repeated failures for one name or one address are refused for a while. */
233
+ signIn(name: string, password: string, from?: string): Promise<Identity>;
234
+ /** Gives an account another name, if nobody has it. Sessions and join tokens issued later carry the new one. */
235
+ rename(accountId: string, name: string): Promise<Identity>;
236
+ /** Sets the email address, or removes it with null. */
237
+ setEmail(accountId: string, email: string | null): Promise<void>;
238
+ /**
239
+ * Sets a new password, ends every session of the account, and makes any
240
+ * password reset token it was sent useless. Its recovery code stays.
241
+ */
242
+ setPassword(accountId: string, password: string): Promise<void>;
243
+ /**
244
+ * Makes a recovery code for an account and returns it: twenty letters and
245
+ * digits the player keeps somewhere safe, which set a new password once,
246
+ * with no email involved. It replaces the code the account had, if any.
247
+ * Only its hash is stored, so this is the one time it can be shown.
248
+ */
249
+ createRecoveryCode(accountId: string): Promise<string>;
250
+ /**
251
+ * Sets a new password for the account called `name`, given its recovery
252
+ * code, and ends its sessions. The code is used up: make another once
253
+ * signed in. Wrong guesses are counted as wrong passwords are.
254
+ */
255
+ recoverWithCode(name: string, code: string, password: string, from?: string): Promise<Identity>;
256
+ /**
257
+ * Makes a password reset token for the account with this name, or for
258
+ * each account with this email address, and returns what to send to whom.
259
+ * Empty if there is no such account or it has no email address. A token
260
+ * replaces the one the account was sent before. `POST /forgot` calls this
261
+ * and hands each result to `sendPasswordReset`.
262
+ */
263
+ startPasswordReset(who: {
264
+ name?: string;
265
+ email?: string;
266
+ }): Promise<PasswordReset[]>;
267
+ /** Sets a new password with a token from `startPasswordReset`, once, and ends the account's sessions. */
268
+ resetPassword(token: string, password: string): Promise<Identity>;
269
+ /** Starts a session for an account and returns its token. Usually done for you by `/login`. */
270
+ createSession(accountId: string): Promise<string>;
271
+ /** Deletes an account, its sessions, data and stats. Match records keep the id, but it no longer resolves to a name. */
272
+ deleteAccount(accountId: string): Promise<void>;
273
+ /** Everything stored about a player, for them to download. */
274
+ exportAccount(accountId: string): Promise<Record<string, unknown>>;
275
+ /** The account as its owner sees it. */
276
+ ownAccount(accountId: string): Promise<OwnAccount>;
277
+ /** A player's public profile, or null if there is no account with that id. */
278
+ profile(playerId: string): Promise<PublicProfile | null>;
279
+ /** Account names by player id. Guests and deleted accounts are left out. */
280
+ names(playerIds: readonly string[]): Promise<Map<string, string>>;
281
+ /** The value a player has under `key`, or undefined. Server code reads every key, whatever its policy. */
282
+ getData(playerId: string, key: string): Promise<unknown>;
283
+ /** Stores a JSON value for a player. Server code may set any key; clients only those with `write: "owner"`. */
284
+ setData(playerId: string, key: string, value: unknown): Promise<void>;
285
+ /** Removes a player's value under `key`. Does nothing if there is none. */
286
+ deleteData(playerId: string, key: string): Promise<void>;
287
+ /** Adds to a player's stats: `{ wins: 1, kills: 7 }`. Server only: clients cannot write stats. */
288
+ addStats(playerId: string, increments: Record<string, number>): Promise<void>;
289
+ /** Sets stats to these values. */
290
+ setStats(playerId: string, values: Record<string, number>): Promise<void>;
291
+ /** Keeps the higher of the stored and the given value: best scores, longest streaks. */
292
+ maxStats(playerId: string, values: Record<string, number>): Promise<void>;
293
+ /**
294
+ * Writes several players' stats in one go, once per `key`: a match id, say.
295
+ * Calling it again with the same key changes nothing and returns false, so
296
+ * code that retries after a failure cannot count a result twice. Either all
297
+ * of it is written or none of it.
298
+ */
299
+ awardStats(key: string, awards: readonly {
300
+ player: string;
301
+ add?: Record<string, number>;
302
+ set?: Record<string, number>;
303
+ max?: Record<string, number>;
304
+ }[]): Promise<boolean>;
305
+ /** Every stat a player has. */
306
+ stats(playerId: string): Promise<Record<string, number>>;
307
+ /** Several players' stats at once, by player id. `stats` limits it to those stats. Players without any are left out. */
308
+ statsOf(playerIds: readonly string[], stats?: readonly string[]): Promise<Map<string, Record<string, number>>>;
309
+ /** The players with the highest value of a stat. Players without an account are not listed. */
310
+ leaderboard(stat: string, page?: {
311
+ limit?: number;
312
+ offset?: number;
313
+ }): Promise<LeaderboardPage>;
314
+ /** A player's place on a leaderboard, or null if they have no value for that stat. */
315
+ rank(stat: string, playerId: string): Promise<Pick<LeaderboardEntry, "rank" | "value"> | null>;
316
+ }
317
+ /** A PBKDF2-SHA256 hash, as `pbkdf2$sha256$<iterations>$<salt>$<hash>` in base64url. */
318
+ export declare function hashPassword(password: string, iterations?: number): Promise<string>;
319
+ /** Whether `password` matches a hash made by `hashPassword`, in constant time. */
320
+ export declare function verifyPassword(stored: string, password: string): Promise<boolean>;