From 17df03c65c06b04875c87763e39a0b6762f88bf6 Mon Sep 17 00:00:00 2001 From: yml Date: Thu, 21 May 2026 15:42:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E7=AB=AF=E8=BF=81=E7=A7=BB=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E4=B8=BB=E7=BC=96=E6=8E=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../services/session/session-service-store.ts | 5 +- .../session/{session.js => session.ts} | 62 ++++++++++++++----- apps/backend/tsconfig.json | 1 - docs/backend-typescript-migration-plan.md | 9 +++ 4 files changed, 57 insertions(+), 20 deletions(-) rename apps/backend/src/services/session/{session.js => session.ts} (85%) diff --git a/apps/backend/src/services/session/session-service-store.ts b/apps/backend/src/services/session/session-service-store.ts index 2f8fd6fc..9306d401 100644 --- a/apps/backend/src/services/session/session-service-store.ts +++ b/apps/backend/src/services/session/session-service-store.ts @@ -27,9 +27,10 @@ export type TencentBrowserSession = Record & { autoCloseAt: number autoCloseTimer: ReturnType | null sessionDir: string - browserContext?: BrowserContextLike | null - page?: unknown + browserContext: any + page: any browserContextClosed?: boolean + presentationSyncAttempts: number } type RegisterSessionInput = { diff --git a/apps/backend/src/services/session/session.js b/apps/backend/src/services/session/session.ts similarity index 85% rename from apps/backend/src/services/session/session.js rename to apps/backend/src/services/session/session.ts index 71980268..928495b0 100644 --- a/apps/backend/src/services/session/session.js +++ b/apps/backend/src/services/session/session.ts @@ -1,5 +1,3 @@ -// @ts-nocheck - import { getLoginTypeLabel, normalizeLoginType, @@ -72,11 +70,26 @@ import { registerTencentBrowserSession, resetTencentSessionAutoClose, } from './session-service-store.js' +import type { TencentBrowserSession } from './session-service-store.js' + +type JsonObject = Record + +type IncludeQrImageOptions = { + includeQrImage?: boolean +} + +type CloseAllSessionsOptions = { + markClosed?: boolean +} + +type CloseManagedSessionOptions = { + markClosed: boolean +} export { resolveTencentSessionExpiresAt } export { REDEEM_SUCCESS_AUTO_CLOSE_MS, warmupTencentBrowser } -export async function createTencentBrowserSession(payload = {}) { +export async function createTencentBrowserSession(payload: JsonObject = {}): Promise { await cleanupExpiredTencentBrowserSessions({ closeSession: closeManagedSession }) const loginType = normalizeLoginType(payload.loginType || payload.qrType) @@ -120,15 +133,18 @@ export async function createTencentBrowserSession(payload = {}) { } } -export async function getTencentBrowserSession(sessionId, options = {}) { +export async function getTencentBrowserSession( + sessionId: unknown, + options: IncludeQrImageOptions = {}, +): Promise { return refreshTencentBrowserSession(sessionId, options) } -export async function getTencentBrowserSessionSummary(sessionId) { +export async function getTencentBrowserSessionSummary(sessionId: unknown): Promise { return refreshTencentBrowserSession(sessionId, { includeQrImage: false }) } -export async function reloadTencentBrowserSession(sessionId) { +export async function reloadTencentBrowserSession(sessionId: unknown): Promise { await cleanupExpiredTencentBrowserSessions({ closeSession: closeManagedSession }) const session = await getRequiredSession(sessionId) @@ -170,7 +186,10 @@ export async function reloadTencentBrowserSession(sessionId) { } } -export async function refreshTencentBrowserSession(sessionId, { includeQrImage = true } = {}) { +export async function refreshTencentBrowserSession( + sessionId: unknown, + { includeQrImage = true }: IncludeQrImageOptions = {}, +): Promise { await cleanupExpiredTencentBrowserSessions({ closeSession: closeManagedSession }) const session = await getRequiredSession(sessionId) @@ -255,7 +274,7 @@ export async function refreshTencentBrowserSession(sessionId, { includeQrImage = } } -export async function redeemTencentBrowserSession(sessionId, payload = {}) { +export async function redeemTencentBrowserSession(sessionId: unknown, payload: JsonObject = {}): Promise { const session = await getRequiredSession(sessionId) const code = String(payload.code || payload.sCdKey || '').trim() const maxAttempts = Math.max(1, Math.min(Number(payload.maxAttempts || 6), 12)) @@ -282,7 +301,8 @@ export async function redeemTencentBrowserSession(sessionId, payload = {}) { session, code, maxAttempts, - ensureLoggedInPresentation, + ensureLoggedInPresentation: (redeemSession, options) => + ensureLoggedInPresentation(redeemSession as any, options as any), ensureActivityInfoReady, fillRedeemCodeInBrowser, capturePageCaptchaForOcr: (page, options) => @@ -307,7 +327,7 @@ export async function redeemTencentBrowserSession(sessionId, payload = {}) { } } -export async function closeTencentBrowserSession(sessionId) { +export async function closeTencentBrowserSession(sessionId: unknown): Promise { const session = await getRequiredSession(sessionId) await closeManagedSession(session, { markClosed: true }) return { @@ -316,7 +336,9 @@ export async function closeTencentBrowserSession(sessionId) { } } -export async function closeAllTencentBrowserSessions({ markClosed = true } = {}) { +export async function closeAllTencentBrowserSessions( + { markClosed = true }: CloseAllSessionsOptions = {}, +): Promise { const activeSessions = listActiveTencentBrowserSessions() await Promise.all(activeSessions.map((session) => closeManagedSession(session, { markClosed }))) @@ -326,7 +348,7 @@ export async function closeAllTencentBrowserSessions({ markClosed = true } = {}) } } -export async function getTencentBrowserSessionScreenshotPath(sessionId) { +export async function getTencentBrowserSessionScreenshotPath(sessionId: unknown): Promise { const session = await getRequiredSession(sessionId) const screenshotPath = session.lastRedeem?.screenshotPath @@ -340,7 +362,7 @@ export async function getTencentBrowserSessionScreenshotPath(sessionId) { return screenshotPath } -export async function getTencentBrowserSessionReviewScreenshotPath(sessionId) { +export async function getTencentBrowserSessionReviewScreenshotPath(sessionId: unknown): Promise { const session = await getRequiredSession(sessionId) const screenshotPath = session.lastReview?.screenshotPath @@ -354,11 +376,14 @@ export async function getTencentBrowserSessionReviewScreenshotPath(sessionId) { return screenshotPath } -async function persistSessionState(session) { +async function persistSessionState(session: TencentBrowserSession): Promise { await persistStoredSessionState(session, { buildSessionPayload }) } -function buildSessionPayload(session, { includeQrImage = true } = {}) { +function buildSessionPayload( + session: TencentBrowserSession, + { includeQrImage = true }: IncludeQrImageOptions = {}, +): JsonObject { return buildSessionPayloadBase(session, { activityUrl: ACTIVITY_URL, includeQrImage, @@ -367,11 +392,14 @@ function buildSessionPayload(session, { includeQrImage = true } = {}) { }) } -async function fillRedeemCodeInBrowser(page, code) { +async function fillRedeemCodeInBrowser(page: any, code: string): Promise { await prepareRedeemCodeFill(page, code, { ensureActivityInfoReady }) } -async function closeManagedSession(session, { markClosed }) { +async function closeManagedSession( + session: TencentBrowserSession, + { markClosed }: CloseManagedSessionOptions, +): Promise { await closeStoredSession(session, { markClosed, persistSessionState, diff --git a/apps/backend/tsconfig.json b/apps/backend/tsconfig.json index e31204e4..6b7bc9ce 100644 --- a/apps/backend/tsconfig.json +++ b/apps/backend/tsconfig.json @@ -19,7 +19,6 @@ "node_modules", "data", "src/**/*.test.js", - "src/services/session/**/*.js", "src/services/platforms/agiso/**/*.js" ] } diff --git a/docs/backend-typescript-migration-plan.md b/docs/backend-typescript-migration-plan.md index 08930aea..0c1676c9 100644 --- a/docs/backend-typescript-migration-plan.md +++ b/docs/backend-typescript-migration-plan.md @@ -496,6 +496,15 @@ - `npm run typecheck` - `npm run build` - `npm test` 共 139 个用例通过 +107. 腾讯浏览器会话主编排迁移到 `.ts`: + - `src/services/session/session.ts` +108. 会话创建 / 刷新 / 重载 / 兑换 / 关闭入口已进入 TS 编译链路,`session-service-store.ts` 补齐主编排所需 browser context、page 与 presentation sync 字段类型 +109. `tsconfig.json` 已移除 `src/services/session/**/*.js` 排除项;除测试文件外,`src/services/session` 服务目录已全量纳入 TS 编译 +110. Docker 内验证通过: + - `src/services/session/session.test.js`、`src/services/session/session-redeem.test.js`、`src/services/session/session-proof.test.js` 共 24 个用例通过 + - `npm run typecheck` + - `npm run build` + - `npm test` 共 139 个用例通过 ## 下一步建议