Files
usenet-indexer/lib/headers/file_key.ts
T
Daan MeijerandClaude Opus 5.5 9dc0a27bb4 Rebuild: a lease never waits for prefetching; one-shot hashes
A lease was only granted for a range with spooled chunks, and the
prefetcher kept ranges ahead per active lease: once the spool ran dry a
worker got none and slept 30 s, fewer ranges were leased, fewer
prefetched. Three workers held one lease between them (~50k headers/s).
A lease now takes a spooled range first, else the next planned one
(leased ranges are fetched first; the stream waits).

crypto.hash instead of createHash().update().digest() for file and
release keys: same digests, ~2.5x faster (~4.6% of a worker's time).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 03:08:19 +02:00

46 lines
2.3 KiB
TypeScript

import { hash } from 'node:crypto'
/**
* Identity of one uploaded file: every segment of it has the same poster, the
* same subject apart from the segment counter, and the same segment count.
* The group is deliberately left out so crossposts resolve to one file.
* See CONCEPT.md (D12).
*/
export function fileKey(poster: string, base: string, totalSegments: number): string {
return hash('sha1', `${poster}\n${base}\n${totalSegments}`)
}
/** True when a yEnc name looks like a real filename (it has an extension), not a random token. */
export function looksLikeFilename(name: string): boolean {
return /\.[A-Za-z0-9]{1,5}$/.test(name) && !/^[0-9a-f]{32,}$/i.test(name)
}
/**
* Identity of a file known only from its yEnc header (obfuscated posts).
* The poster is left out: obfuscating posters randomise it per article.
* - A real filename is stable across the articles of a file, and needed to
* tell apart the equal-sized volumes of one archive.
* - A random, extensionless name can change per article too; the exact
* byte size and part count are what stays the same.
* Collisions are caught by the conflict check in IngestService.
*/
export function weakFileKey(name: string, size: number, totalParts: number): string {
const identity = looksLikeFilename(name) ? `name\n${name}` : 'shape'
return hash('sha1', `weak\n${identity}\n${size}\n${totalParts}`)
}
/**
* Identity of a file by its counters, for uploads that give every article
* its own random subject (CONCEPT.md D36): poster, file counter `[i/M]`,
* part count and posting day stay the same across the articles of one file.
* The day keeps different uploads with the same shape apart; collisions that
* remain are caught by the conflict check (these files are weak identities).
* Null when the subject has no file counter.
*/
export function counterFileKey(segment: { poster: string; subject: string; totalParts: number; postedAt?: Date }): string | null {
const counter = /\[(\d+)\/(\d+)\]/.exec(segment.subject)
if (!counter || segment.totalParts < 2) return null
const day = segment.postedAt && !Number.isNaN(segment.postedAt.getTime()) ? segment.postedAt.toISOString().slice(0, 10) : ''
return hash('sha1', `counter\n${segment.poster}\n${Number(counter[1])}/${Number(counter[2])}\n${segment.totalParts}\n${day}`)
}