From 43f2717b44088ab424e24e365f7068734d5c806a Mon Sep 17 00:00:00 2001 From: Snipzil Date: Wed, 21 Oct 2026 22:22:59 -0400 Subject: [PATCH] perf(signup): derive a web sign-up's password off the main thread POST /api/register used accounts.register, whose scryptSync froze every player for as long as each password took, so a burst of sign-ups was felt in the game. accounts.registerAsync checks the same things, derives the hash on libuv's pool (hashPasswordAsync), checks the name again after it (another request may have taken it meanwhile) and inserts the account as before. The game's own Create Account keeps the sync path. Co-Authored-By: Claude Opus 5.5 --- lib/accounts.js | 30 +++++++++++++++++++++++++++++- lib/admin.js | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/accounts.js b/lib/accounts.js index 48f7480..5429bae 100644 --- a/lib/accounts.js +++ b/lib/accounts.js @@ -737,6 +737,34 @@ const register = (username, password, avatar = {}) => { return {account}; }; +// Public sign-ups can arrive in a burst. scryptSync is fine for the local, +// legacy Create Account socket but it would freeze all players while each web +// registration is derived. Keep the same validation and database insert on +// the main thread, but derive its password on libuv's worker pool. Check the +// username again after awaiting: another request may have claimed it first. +const registerAsync = async (username, password, avatar = {}) => { + username = typeof username === "string" ? username.trim() : username; + let error = usernameProblem(username) || passwordProblem(password, username) || (defpacks.avatarDef(avatar.type) ? null : "choose_animal"); + if (error) { + return {error}; + } + const salt = crypto.randomBytes(16).toString("hex"); + const hash = await hashPasswordAsync(password, salt); + error = usernameProblem(username) || passwordProblem(password, username) || (defpacks.avatarDef(avatar.type) ? null : "choose_animal"); + if (error) { + return {error}; + } + const type = Number(avatar.type); + const account = insert(username, salt, hash, now(), { + avatars: [newAvatar({perUserAvId: 1, avInvId: 1}, avatar.name || username, type, -1)], + activeAvatar: 1, + badges: byId.size < EARLY_MEMBERS ? [EARLY_BADGE] : [], + recoveryEmail: typeof avatar.email === "string" && EMAIL_PATTERN.test(avatar.email.trim()) ? avatar.email.trim().toLowerCase() : "", + }); + console.log(`[accounts] registered "${logText(username)}" (id ${logText(account.id)})`); + return {account}; +}; + const authenticate = (username, password) => { // "name" and "name " are the same account. const account = typeof username === "string" ? touch(byKey.get(username.trim().toLowerCase())) : null; @@ -954,4 +982,4 @@ const all = () => [...byId.values()]; const database = () => db; -module.exports = {FEATURES, featureOn, recordLogin, revokeTokens, load, save, flush, hold, release, startBackups, database, setLoginChoices, setPassword, rename, remove, setting, setSetting, hashPassword, register, usernameProblem, authenticate, authenticateAsync, issueToken, byToken, byName, nameOf, byId: accountById, newAvatar, freeAvatarSlot, all}; +module.exports = {FEATURES, featureOn, recordLogin, revokeTokens, load, save, flush, hold, release, startBackups, database, setLoginChoices, setPassword, rename, remove, setting, setSetting, hashPassword, register, registerAsync, usernameProblem, authenticate, authenticateAsync, issueToken, byToken, byName, nameOf, byId: accountById, newAvatar, freeAvatarSlot, all}; diff --git a/lib/admin.js b/lib/admin.js index 2b1487c..63ab2a6 100644 --- a/lib/admin.js +++ b/lib/admin.js @@ -282,7 +282,7 @@ const handle = async (route, params, body, from) => { if (!STARTER_ANIMALS.includes(avatarType)) { return {status: 400, body: {error: "choose_animal"}}; } - const result = accounts.register(username, password, {type: avatarType, email}); + const result = await accounts.registerAsync(username, password, {type: avatarType, email}); if (result.account) { result.account.agreed = {version: config.termsVersion, at: Math.floor(Date.now() / 1000)}; accounts.save(result.account);