add support for v2 hashing algorithm.

update README.md and demo file to be more in sync.
update to v0.10.0.
This commit is contained in:
2026-03-29 22:34:43 -04:00
parent d5363c3960
commit c776b5df6a
33 changed files with 2255 additions and 1524 deletions
+1 -1
View File
@@ -1 +1 @@
export declare const __dirname: string;
export declare const HASH_V2_EPOCH = "2026-07-01T00:00:00Z";
+2 -1
View File
@@ -1,2 +1,3 @@
import { Twt } from './types.ts';
export default function hashTwt(twt: Twt): string;
export type HashableTwt = Pick<Twt, "content" | "created" | "url"> & Partial<Omit<Twt, "content" | "created" | "url">>;
export default function hashTwt(twt: HashableTwt, version?: number): string;
+7 -2
View File
@@ -2900,6 +2900,7 @@ const base32Encode = (payload) => {
return encoder.write(payload).finalize();
};
const getValueOrFirstEntry = (value) => Array.isArray(value) && value.length ? value[0] : value;
const HASH_V2_EPOCH = `2026-07-01T00:00:00Z`;
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})([tT ])(\d{2}):(\d{2}):(\d{2})\.?(\d{3})?(?:(?:([+-]\d{2}):?(\d{2}))|Z)?$/;
const formatRFC3339 = (date) => {
const pad = (num = 0) => `${+num < 10 ? 0 : ""}${+num}`;
@@ -2928,10 +2929,14 @@ const formatRFC3339 = (date) => {
offset
].join("");
};
function hashTwt(twt) {
function hashTwt(twt, version = 0) {
const created = formatRFC3339(twt.created);
const payload = [twt.url, created, twt.content].join("\n");
return base32Encode(blakejsExports.blake2b(payload, void 0, 32)).toLowerCase().slice(-7);
const encoded = base32Encode(blakejsExports.blake2b(payload, void 0, 32)).toLowerCase();
const createdDate = new Date(twt.created), epochDate = new Date(HASH_V2_EPOCH);
if (version === 1 || !version && createdDate < epochDate)
return encoded.slice(-7);
return encoded.slice(0, 12);
}
var dayjs_min$1 = { exports: {} };
var dayjs_min = dayjs_min$1.exports;
File diff suppressed because one or more lines are too long
+400 -400
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long