refactor(utils): hashTokenをapiToken.tsから独立したtokenHash.tsに分離#73
Open
hashrock wants to merge 1 commit into
Open
Conversation
観測: utils/apiToken.ts は純粋なSHA-256ハッシュ関数 hashToken と、 DB(drizzle-orm/d1, db/schema)に依存する getUserByToken を同一ファイルに 同居させていた。utils/ は crypto.ts が示す通り依存を持たない基盤層である べきだが、apiToken.ts を経由すると hashToken だけを使いたい場面でも drizzle-orm/d1 と db/schema が芋づる式に読み込まれ、DBなしでは テストできない状態になっていた(直前のresolveDevGuestPreference分離と 同じ境界分離パターン)。 変更: hashToken を app/utils/tokenHash.ts に抽出し、apiToken.ts と server.ts はそこから import する形に変更。ロジックは無変更。 確認: pnpm test(新規 tokenHash.test.ts 3件含め450件成功、既存の editorKeymap.test.ts 2件の失敗はこの変更前から存在し無関係と確認済み)、 tsc --noEmit エラーなし。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016FXQM7dDrPE7h5uZmroBuV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
観測した問題
app/utils/apiToken.tsは、外部依存のない純粋関数hashToken(SHA-256ハッシュ)と、DB(drizzle-orm/d1,db/schema)に依存するgetUserByTokenを同一ファイルに同居させていました。app/utils/はcrypto.tsが示す通り、本来は上位レイヤーに依存しない基盤層です。しかしapiToken.tsを経由すると、hashTokenだけを使いたい場面でもdrizzle-orm/d1とdb/schemaが芋づる式に読み込まれ、DBやHonoのContextなしでは独立してテストできない状態になっていました。これは直前の #71(
resolveDevGuestPreferenceを dev-bypass ロジックから分離し、DBやリクエストなしでテスト可能にした変更)と同じ「境界分離」パターンの見落としです。変更内容
hashTokenを新しいapp/utils/tokenHash.tsに抽出(ロジックは無変更)apiToken.tsはgetUserByToken(DB依存のトークン検証)に専念し、tokenHash.tsからhashTokenを importserver.tsの import元を更新tokenHash.test.tsを新規追加し、DBなしでhashTokenを独立してテスト可能なことを実証守りたい設計意図
app/utils/はDB・フレームワークに依存しない基盤ユーティリティ層である、という境界resolveDevGuestPreference分離と同じ方針)確認方法
npx tsc --noEmit -p tsconfig.json— エラーなしpnpm test— 450件成功(新規tokenHash.test.ts3件含む)。editorKeymap.test.tsの既存2件の失敗は本変更前から存在し、無関係であることをgit stashで切り分けて確認済みレビュー観点
hashTokenのロジック・戻り値は一切変更していないこと(diffで確認可能)apiToken.ts・server.tsの import 更新に抜け漏れがないこと🤖 Generated with Claude Code
Generated by Claude Code