Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

etag.ts

23 lines888 Bytes

1import { createHash } from 'node:crypto'
2​
3export interface ReplayIdentity {
4 readonly generationId: string
5 readonly servingEpoch: string
6 readonly start: string
7 readonly end: string
8 readonly contentType: string
9 readonly closed: boolean
11​
12/** The epoch distinguishes rewritten bytes after restoring a prior position. */
13export function replayEtag(identity: ReplayIdentity): string {
14 const representation = [1, identity.generationId, identity.servingEpoch, identity.start, identity.end, identity.contentType, identity.closed]
15 return `"${createHash('sha256').update(JSON.stringify(representation)).digest('hex')}"`
17​
18export function matchesEtag(condition: string | null | undefined, etag: string): boolean {
19 return condition?.split(',').some((candidate) => {
20 const value = candidate.trim()
21 return value === '*' || value.replace(/^W\//, '') === etag
22 }) ?? false