firefish/packages/backend/src/misc/gen-id.ts
naskya 30619301d3
chore: replace new Date().getTime() with Date.now()
959cc8ff37

Co-authored-by: zyoshoka <107108195+zyoshoka@users.noreply.github.com>
2024-04-08 17:00:41 +09:00

27 lines
845 B
TypeScript

import config from "@/config/index.js";
import {
nativeCreateId,
nativeInitIdGenerator,
nativeGetTimestamp,
} from "backend-rs";
const length = Math.min(Math.max(config.cuid?.length ?? 16, 16), 24);
const fingerprint = config.cuid?.fingerprint ?? "";
nativeInitIdGenerator(length, fingerprint);
/**
* The generated ID results in the form of `[8 chars timestamp] + [cuid2]`.
* The minimum and maximum lengths are 16 and 24, respectively.
* With the length of 16, namely 8 for cuid2, roughly 1427399 IDs are needed
* in the same millisecond to reach 50% chance of collision.
*
* Ref: https://github.com/paralleldrive/cuid2#parameterized-length
*/
export function genId(date?: Date): string {
return nativeCreateId(date?.getTime() ?? Date.now());
}
export function getTimestamp(id: string): number {
return nativeGetTimestamp(id);
}