Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/thatdeveloperguy-website / server/inquiries/duplicate-request-policy.ts |
| Date | Not specified in source |
| Repository | Janady13/thatdeveloperguy-website |
| Revision | 2bea2ebdff0e34494bff597d4fa9c8422a0c3a94 |
| Branch | main |
| Path or archive member | server/inquiries/duplicate-request-policy.ts |
| Original SHA-256 | 1f923fb30228c44086fd1f07ddc6186a80e3f344cffbb59ec33a820ca8be0f25 |
| Git blob SHA | 889551d00717d490f68a45aded1ab8799fdac27f |
| Reading-copy SHA-256 | 1f923fb30228c44086fd1f07ddc6186a80e3f344cffbb59ec33a820ca8be0f25 |
Recovered source transcript
Download complete source text ↓29 lines · 1.4 KB
import { createHash } from 'node:crypto';
import type { Inquiry } from './validate.ts';
import type { Outcome } from './delivery-outcome.ts';
export interface DuplicateRecord { outcome: Outcome; requestId: string; at: number }
/**
* A resend of the same message within the window (double-click, refresh of the POST, retry after an uncertain timeout)
* gets the first attempt's outcome back and is not sent again. Ambiguous outcomes are not retried automatically either:
* a second identical message could arrive twice, so the visitor is told to call if it matters.
*/
export class DuplicatePolicy {
private readonly seen = new Map<string, DuplicateRecord>();
constructor(private readonly windowMs: number, private readonly max = 2000) {}
digest(inquiry: Inquiry): string {
return createHash('sha256').update([inquiry.email.toLowerCase(), inquiry.name.toLowerCase(), inquiry.message.replace(/\s+/g, ' ').trim().toLowerCase()].join('\n')).digest('hex');
}
lookup(digest: string, now = Date.now()): DuplicateRecord | null {
const record = this.seen.get(digest);
if (!record) return null;
if (now - record.at > this.windowMs) { this.seen.delete(digest); return null; }
return record;
}
remember(digest: string, record: DuplicateRecord): void {
if (this.seen.size >= this.max) { const oldest = this.seen.keys().next().value; if (oldest) this.seen.delete(oldest); }
this.seen.set(digest, record);
}
}