1단계 뼈대: joke-app에서 로그인(디스코드·게스트)·세션·권한·관리자·DB를 가져와 음악퀴즈용으로 정리, 새 디자인·아이콘, WS 연결·시계 맞추기, 테스트·e2e

- apps/server: Hono API(auth/me/config/admin), Bun WebSocket(welcome·ping/pong, 압축 끔), SQLite 마이그레이션
- 사이트 설정: 채팅 도배 방지 기본 꺼짐(관리자가 켬, 초당·순간 횟수)
- apps/web: 첫 화면·로그인·관리자(대시보드·사이트·사용자·관리자·기록), 라이트/다크, 휴대폰, noindex
- packages/shared: 닉네임 규칙, 프로토콜(zod), 시계 차이 계산
- 테스트 20개 + 브라우저 e2e 6단계 통과

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-18 19:44:47 +09:00
parent 7ae34c49c1
commit 391bbe69e5
61 changed files with 3569 additions and 3 deletions

View File

@@ -0,0 +1,3 @@
export * from './nickname';
export * from './protocol';
export * from './types';

View File

@@ -0,0 +1,3 @@
/** Browser entry without zod: nickname rules and plain types. */
export * from './nickname';
export * from './types';

View File

@@ -0,0 +1,37 @@
/** Nickname rules (docs/02). Shared by server validation and the web form. Ported from joke-app. */
export const NICK_MIN = 2;
export const NICK_MAX = 12;
const ALLOWED = /^[0-9A-Za-z가-힣ㄱ-ㅎㅏ-ㅣ _.\-]+$/;
// Minimal built-in list; the admin site setting `bannedWords` extends it. Matching ignores spaces and punctuation.
const BANNED = ['시발', '씨발', '병신', '좆', '개새끼', '느금', '니애미', 'fuck', 'shit', 'bitch'];
export function normalizeNickname(raw: string): string {
return raw.normalize('NFC').trim().replace(/\s+/g, ' ');
}
export function containsProfanity(text: string, extra: readonly string[] = []): boolean {
const squashed = text.normalize('NFC').toLowerCase().replace(/[\s_.\-*0-9]/g, '');
return [...BANNED, ...extra.map((w) => w.toLowerCase())].some((w) => w && squashed.includes(w));
}
/** Returns a Korean error message, or null when valid. Input must already be normalized. */
export function nicknameError(nick: string, extraBanned: readonly string[] = []): string | null {
const len = [...nick].length;
if (len < NICK_MIN) return `닉네임은 ${NICK_MIN}글자 이상이어야 해요.`;
if (len > NICK_MAX) return `닉네임은 ${NICK_MAX}글자까지 쓸 수 있어요.`;
if (!ALLOWED.test(nick)) return '한글, 영어, 숫자와 _ - . 만 쓸 수 있어요.';
if (containsProfanity(nick, extraBanned)) return '다른 닉네임을 써 주세요.';
return null;
}
const ADJ = ['신나는', '흥얼대는', '귀가 밝은', '리듬 타는', '노래하는', '박자 맞는', '음치 아닌', '흥 많은'];
const NOUN = ['레코드', '카세트', '마이크', '스피커', '헤드폰', '피아노', '기타', '드럼'];
export function suggestNickname(rand: () => number = Math.random): string {
for (;;) {
const n = `${ADJ[Math.floor(rand() * ADJ.length)]!} ${NOUN[Math.floor(rand() * NOUN.length)]!}`;
if ([...n].length <= NICK_MAX) return n;
}
}

View File

@@ -0,0 +1,36 @@
/**
* WebSocket protocol (docs/01 "실시간 메시지"). Stage 1 only has the connection handshake and clock sync;
* room/game messages are added in later stages.
*/
import { z } from 'zod';
export const PROTOCOL = 1;
export const MAX_CLIENT_MESSAGE_BYTES = 16 * 1024;
/** Chat line limit (docs/06). */
export const CHAT_MAX = 200;
export const ClientMessage = z.discriminatedUnion('t', [
/** Clock sync (docs/12 Q1): the client sends its own clock, the server echoes it with the server clock. */
z.object({ t: z.literal('ping'), c: z.number() }),
]);
export type ClientMessage = z.infer<typeof ClientMessage>;
export type ServerMessage =
| { t: 'welcome'; protocol: number; userId: string; serverNow: number }
| { t: 'pong'; c: number; s: number }
| { t: 'error'; message: string };
/**
* Server clock offset from ping samples (docs/12 Q1): use the sample with the smallest round trip.
* offset = server - client, so serverNow ≈ Date.now() + offset.
*/
export function clockOffset(samples: { sent: number; received: number; server: number }[]): { offset: number; rtt: number } | null {
let best: { offset: number; rtt: number } | null = null;
for (const s of samples) {
const rtt = s.received - s.sent;
if (rtt < 0) continue;
const offset = s.server - (s.sent + rtt / 2);
if (!best || rtt < best.rtt) best = { offset, rtt };
}
return best;
}

View File

@@ -0,0 +1,22 @@
import { expect, test } from 'bun:test';
import { clockOffset, nicknameError, normalizeNickname, suggestNickname } from './index';
test('nickname rules', () => {
expect(nicknameError(normalizeNickname(' 사금향 '))).toBeNull();
expect(nicknameError('a')).not.toBeNull();
expect(nicknameError('열세글자가넘는아주긴닉네임이에요')).not.toBeNull();
expect(nicknameError('씨 발')).not.toBeNull();
for (let i = 0; i < 50; i++) expect(nicknameError(suggestNickname())).toBeNull();
});
test('clock offset uses the sample with the smallest round trip', () => {
// client clock is 1000ms behind the server; the second sample has a 10ms round trip.
const r = clockOffset([
{ sent: 0, received: 200, server: 1150 },
{ sent: 500, received: 510, server: 1505 },
{ sent: 900, received: 1100, server: 1950 },
])!;
expect(r.rtt).toBe(10);
expect(r.offset).toBe(1000);
expect(clockOffset([])).toBeNull();
});

View File

@@ -0,0 +1,8 @@
export interface PublicUser {
id: string;
nickname: string;
avatar: string | null;
kind: 'guest' | 'member';
}
export type Role = 'user' | 'admin' | 'superadmin';