fix(douyin): handle missing passport csrf cookie#143
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the Douyin route by replacing the temporary cookie fetching logic with a randomly generated UUID for the passport_csrf_token cookie, and adds validation for the API response structure and status code. The review feedback suggests using the globally available Web API crypto.randomUUID() instead of importing from node:crypto to ensure compatibility with edge runtimes, and recommends explicitly checking for a null or undefined responseData first to simplify the error handling and avoid repetitive optional chaining.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| const responseData = result.data; | ||
| const list = responseData?.data?.word_list; | ||
| if (responseData?.status_code !== 0) { | ||
| const reason = responseData?.status_msg || `status_code=${responseData?.status_code}`; | ||
| throw new Error(`获取抖音热榜失败: ${reason}`); | ||
| } | ||
| if (!Array.isArray(list) || list.length === 0) { | ||
| throw new Error("获取抖音热榜失败: word_list 缺失或为空"); | ||
| } |
There was a problem hiding this comment.
Checking responseData for null/undefined first is safer and provides a cleaner error message. If responseData is null or undefined, evaluating responseData?.status_code !== 0 will result in true (since undefined !== 0), which then throws an error with status_code=undefined. Explicitly checking !responseData first avoids this and allows us to remove the repetitive optional chaining.
const responseData = result.data;
if (!responseData) {
throw new Error("获取抖音热榜失败: 响应数据为空");
}
if (responseData.status_code !== 0) {
const reason = responseData.status_msg || "status_code=" + responseData.status_code;
throw new Error("获取抖音热榜失败: " + reason);
}
const list = responseData.data?.word_list;
if (!Array.isArray(list) || list.length === 0) {
throw new Error("获取抖音热榜失败: word_list 缺失或为空");
}| noCache, | ||
| headers: { | ||
| Cookie: `passport_csrf_token=${cookie}`, | ||
| Cookie: `passport_csrf_token=${randomUUID()}`, |
There was a problem hiding this comment.
Since this project uses Hono, it is highly likely to be deployed on edge runtimes like Cloudflare Workers, where node:crypto is not available by default (unless Node.js compatibility is explicitly enabled). Using the standard Web API crypto.randomUUID() (which is globally available in modern Node.js, Cloudflare Workers, Deno, and Bun) improves cross-runtime compatibility. You can remove the import { randomUUID } from "node:crypto"; on line 4 and use crypto.randomUUID() directly here.
| Cookie: `passport_csrf_token=${randomUUID()}`, | |
| Cookie: "passport_csrf_token=" + crypto.randomUUID(), |
Summary
login_guiding_strategycookie bootstrap requestpassport_csrf_tokenfor the Douyin hot-list requeststatus_codeand reject missing or emptyword_listresponsesWhy
Douyin no longer returns
passport_csrf_tokenfrom the login-guiding endpoint. The existing regex therefore returnsnulland logs:TypeError: Cannot read properties of null (reading '1')The hot-list endpoint currently requires a non-empty
passport_csrf_tokencookie but does not require the value to come from that obsolete endpoint.Verification
pnpm exec eslint src/routes/douyin.tspnpm exec prettier --check src/routes/douyin.tspnpm run build/douyin?cache=false: HTTP 200, 50 items,fromCache=false/douyin: HTTP 200, 50 items,fromCache=true