2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2019-04-13 16:08:26 +00:00
|
|
|
// AID
|
|
|
|
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
|
|
|
|
|
2022-02-27 02:07:39 +00:00
|
|
|
import * as crypto from 'node:crypto';
|
2019-04-13 16:08:26 +00:00
|
|
|
|
2023-04-08 19:41:06 +00:00
|
|
|
export const aidRegExp = /^[0-9a-z]{10}$/;
|
|
|
|
|
2019-04-13 16:08:26 +00:00
|
|
|
const TIME2000 = 946684800000;
|
2020-01-26 20:37:53 +00:00
|
|
|
let counter = crypto.randomBytes(2).readUInt16LE(0);
|
2019-04-13 16:08:26 +00:00
|
|
|
|
2022-12-09 23:55:07 +00:00
|
|
|
function getTime(time: number): string {
|
2019-04-13 16:08:26 +00:00
|
|
|
time = time - TIME2000;
|
|
|
|
if (time < 0) time = 0;
|
|
|
|
|
|
|
|
return time.toString(36).padStart(8, '0');
|
|
|
|
}
|
|
|
|
|
2022-12-09 23:55:07 +00:00
|
|
|
function getNoise(): string {
|
2019-04-13 17:33:50 +00:00
|
|
|
return counter.toString(36).padStart(2, '0').slice(-2);
|
2019-04-13 16:08:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 01:45:22 +00:00
|
|
|
export function genAid(t: number): string {
|
2023-05-29 02:54:49 +00:00
|
|
|
if (isNaN(t)) throw new Error('Failed to create AID: Invalid Date');
|
2019-04-13 16:08:26 +00:00
|
|
|
counter++;
|
2020-04-26 02:54:51 +00:00
|
|
|
return getTime(t) + getNoise();
|
2019-04-13 16:08:26 +00:00
|
|
|
}
|
2023-04-03 02:49:58 +00:00
|
|
|
|
|
|
|
export function parseAid(id: string): { date: Date; } {
|
|
|
|
const time = parseInt(id.slice(0, 8), 36) + TIME2000;
|
|
|
|
return { date: new Date(time) };
|
|
|
|
}
|
2023-12-03 05:38:42 +00:00
|
|
|
|
|
|
|
export function isSafeAidT(t: number): boolean {
|
|
|
|
return t > TIME2000;
|
|
|
|
}
|