mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-12-17 02:49:01 +01:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
// AIDX
|
|
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ4の[個体ID] + 長さ4の[カウンタ]
|
|
// (c) mei23
|
|
// https://misskey.m544.net/notes/71899acdcc9859ec5708ac24
|
|
|
|
import { customAlphabet } from 'nanoid';
|
|
|
|
export const aidxRegExp = /^[0-9a-z]{16}$/;
|
|
|
|
const TIME2000 = 946684800000;
|
|
const TIME_LENGTH = 8;
|
|
const NODE_LENGTH = 4;
|
|
const NOISE_LENGTH = 4;
|
|
|
|
const nodeId = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', NODE_LENGTH)();
|
|
let counter = 0;
|
|
|
|
function getTime(time: number): string {
|
|
time = time - TIME2000;
|
|
if (time < 0) time = 0;
|
|
|
|
return time.toString(36).padStart(TIME_LENGTH, '0').slice(-TIME_LENGTH);
|
|
}
|
|
|
|
function getNoise(): string {
|
|
return counter.toString(36).padStart(NOISE_LENGTH, '0').slice(-NOISE_LENGTH);
|
|
}
|
|
|
|
export function genAidx(t: number): string {
|
|
if (isNaN(t)) throw new Error('Failed to create AIDX: Invalid Date');
|
|
counter++;
|
|
return getTime(t) + nodeId + getNoise();
|
|
}
|
|
|
|
export function parseAidx(id: string): { date: Date; } {
|
|
const time = parseInt(id.slice(0, TIME_LENGTH), 36) + TIME2000;
|
|
return { date: new Date(time) };
|
|
}
|
|
|
|
export function isSafeAidxT(t: number): boolean {
|
|
return t > TIME2000;
|
|
}
|