forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatproto.ts
More file actions
31 lines (25 loc) · 1.02 KB
/
atproto.ts
File metadata and controls
31 lines (25 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { TID_CLOCK_ID } from './constants'
import * as TID from '@atcute/tid'
const ONE_DAY_MILLISECONDS = 86400000
const MS_TO_MICROSECONDS = 1000
// A very simple hasher to get an offset for blog posts on the same day
const simpleHash = (str: string): number => {
let h = 0
for (let i = 0; i < str.length; i++) {
h = ((h << 5) - h + str.charCodeAt(i)) >>> 0
}
return h
}
// Parse date from frontmatter, add slug-path entropy for same-date collision resolution
export const generateBlogTID = (dateString: string, slug: string): string => {
let timestamp = Date.parse(dateString)
if (timestamp % ONE_DAY_MILLISECONDS === 0) {
const offset = simpleHash(slug) % 1000000
timestamp += offset
}
// Clock id(3) needs to be the same everytime to get the same TID from a timestamp
return TID.create(timestamp * MS_TO_MICROSECONDS, TID_CLOCK_ID)
}
// Using our release date as the tid for the publication
export const npmxPublicationRkey = () =>
TID.create(Date.parse('2026-03-03') * MS_TO_MICROSECONDS, TID_CLOCK_ID)