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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export function genAid(date: Date): string {
|
2020-04-26 02:54:51 +00:00
|
|
|
const t = date.getTime();
|
|
|
|
if (isNaN(t)) throw '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) };
|
|
|
|
}
|