Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions worker/src/db/queries/shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ export async function createShareRecord(

// 3. 生成令牌与哈希(唯一冲突重试最多 3 次)
const MAX_RETRIES = 3
let lastError: unknown = null

for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
for (let attempt = 0; ; attempt++) {
const token = generateShareToken()
const tokenHash = await sha256Hex(token)
const id = crypto.randomUUID()
Expand All @@ -125,21 +124,18 @@ export async function createShareRecord(
expiresAt: shareExpiresAt
}
} catch (err) {
lastError = err
const message =
err instanceof Error
? err.message
: typeof err === "object" && err !== null && "message" in err
? String((err as any).message)
: String(err)
const isConflict = /unique/i.test(message) || /constraint/i.test(message)
if (!isConflict || attempt === MAX_RETRIES) {
if (!isConflict || attempt >= MAX_RETRIES) {
throw err
}
}
}

throw lastError
}

export async function listSharesByFile(
Expand Down