Files
usenet-indexer/lib/headers/file_key.ts
T
Daan MeijerandClaude Opus 5.5 d773c91ad0
Test and deploy / test (push) Successful in 29s
Test and deploy / deploy (push) Skipped
Rebuild: phase 1 modules from the current app, ordered by pipeline stage
lib/nntp (XOVER client, Xref, GROUP), lib/headers (subjects, file keys,
segments from overview lines; the yEnc path stays behind for phase 2),
lib/release (identity, random names, now in lib), lib/files (aggregator,
volume names), lib/rules (posting tools, owner by priority, release
verdicts), lib/chunks (release chunk codec). Their unit tests came along;
new ones for codec, rules and volumes.

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

48 lines
2.4 KiB
TypeScript

import { createHash } 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 createHash('sha1').update(`${poster}\n${base}\n${totalSegments}`).digest('hex')
}
/** 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 createHash('sha1').update(`weak\n${identity}\n${size}\n${totalParts}`).digest('hex')
}
/**
* 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 createHash('sha1')
.update(`counter\n${segment.poster}\n${Number(counter[1])}/${Number(counter[2])}\n${segment.totalParts}\n${day}`)
.digest('hex')
}