From f476d149a61387525571f162c7d22a393b6fd80c Mon Sep 17 00:00:00 2001 From: Johann150 Date: Mon, 11 Jul 2022 16:04:02 +0200 Subject: [PATCH] remove alternative ID generation Although these are configurable, there seems to be no benefit of being able to configure these. The expected use of configuration methods other than "aid" is expected to be low. --- packages/backend/package.json | 1 - packages/backend/src/misc/gen-id.ts | 32 ++++++++++++----------- packages/backend/src/misc/id/aid.ts | 25 ------------------ packages/backend/src/misc/id/meid.ts | 26 ------------------ packages/backend/src/misc/id/meidg.ts | 28 -------------------- packages/backend/src/misc/id/object-id.ts | 26 ------------------ 6 files changed, 17 insertions(+), 121 deletions(-) delete mode 100644 packages/backend/src/misc/id/aid.ts delete mode 100644 packages/backend/src/misc/id/meid.ts delete mode 100644 packages/backend/src/misc/id/meidg.ts delete mode 100644 packages/backend/src/misc/id/object-id.ts diff --git a/packages/backend/package.json b/packages/backend/package.json index c6c76b30a..2ff70e0ed 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -111,7 +111,6 @@ "tsconfig-paths": "4.0.0", "twemoji-parser": "14.0.0", "typeorm": "0.3.6", - "ulid": "2.3.0", "unzipper": "0.10.11", "uuid": "8.3.2", "web-push": "3.5.0", diff --git a/packages/backend/src/misc/gen-id.ts b/packages/backend/src/misc/gen-id.ts index fcf476857..1a7a511b6 100644 --- a/packages/backend/src/misc/gen-id.ts +++ b/packages/backend/src/misc/gen-id.ts @@ -1,21 +1,23 @@ -import { ulid } from 'ulid'; -import { genAid } from './id/aid.js'; -import { genMeid } from './id/meid.js'; -import { genMeidg } from './id/meidg.js'; -import { genObjectId } from './id/object-id.js'; -import config from '@/config/index.js'; +import * as crypto from 'node:crypto'; -const metohd = config.id.toLowerCase(); +// AID generation +// 8 chars: milliseconds elapsed since 2000-01-01 00:00:00.000Z encoded as base36 +// + 2 random chars + +const TIME2000 = 946684800000; +let counter = crypto.randomBytes(2).readUInt16LE(0); export function genId(date?: Date): string { if (!date || (date > new Date())) date = new Date(); - switch (metohd) { - case 'aid': return genAid(date); - case 'meid': return genMeid(date); - case 'meidg': return genMeidg(date); - case 'ulid': return ulid(date.getTime()); - case 'objectid': return genObjectId(date); - default: throw new Error('unrecognized id generation method'); - } + let t = date.getTime(); + t -= TIME2000; + if (t < 0) t = 0; + if (isNaN(t)) throw 'Failed to create AID: Invalid Date'; + const time = t.toString(36).padStart(8, '0'); + + counter++; + const noise = counter.toString(36).padStart(2, '0').slice(-2); + + return time + noise; } diff --git a/packages/backend/src/misc/id/aid.ts b/packages/backend/src/misc/id/aid.ts deleted file mode 100644 index 87e688826..000000000 --- a/packages/backend/src/misc/id/aid.ts +++ /dev/null @@ -1,25 +0,0 @@ -// AID -// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列] - -import * as crypto from 'node:crypto'; - -const TIME2000 = 946684800000; -let counter = crypto.randomBytes(2).readUInt16LE(0); - -function getTime(time: number) { - time = time - TIME2000; - if (time < 0) time = 0; - - return time.toString(36).padStart(8, '0'); -} - -function getNoise() { - return counter.toString(36).padStart(2, '0').slice(-2); -} - -export function genAid(date: Date): string { - const t = date.getTime(); - if (isNaN(t)) throw 'Failed to create AID: Invalid Date'; - counter++; - return getTime(t) + getNoise(); -} diff --git a/packages/backend/src/misc/id/meid.ts b/packages/backend/src/misc/id/meid.ts deleted file mode 100644 index 30bbdf169..000000000 --- a/packages/backend/src/misc/id/meid.ts +++ /dev/null @@ -1,26 +0,0 @@ -const CHARS = '0123456789abcdef'; - -function getTime(time: number) { - if (time < 0) time = 0; - if (time === 0) { - return CHARS[0]; - } - - time += 0x800000000000; - - return time.toString(16).padStart(12, CHARS[0]); -} - -function getRandom() { - let str = ''; - - for (let i = 0; i < 12; i++) { - str += CHARS[Math.floor(Math.random() * CHARS.length)]; - } - - return str; -} - -export function genMeid(date: Date): string { - return getTime(date.getTime()) + getRandom(); -} diff --git a/packages/backend/src/misc/id/meidg.ts b/packages/backend/src/misc/id/meidg.ts deleted file mode 100644 index d4aaaea1b..000000000 --- a/packages/backend/src/misc/id/meidg.ts +++ /dev/null @@ -1,28 +0,0 @@ -const CHARS = '0123456789abcdef'; - -// 4bit Fixed hex value 'g' -// 44bit UNIX Time ms in Hex -// 48bit Random value in Hex - -function getTime(time: number) { - if (time < 0) time = 0; - if (time === 0) { - return CHARS[0]; - } - - return time.toString(16).padStart(11, CHARS[0]); -} - -function getRandom() { - let str = ''; - - for (let i = 0; i < 12; i++) { - str += CHARS[Math.floor(Math.random() * CHARS.length)]; - } - - return str; -} - -export function genMeidg(date: Date): string { - return 'g' + getTime(date.getTime()) + getRandom(); -} diff --git a/packages/backend/src/misc/id/object-id.ts b/packages/backend/src/misc/id/object-id.ts deleted file mode 100644 index 392ea4330..000000000 --- a/packages/backend/src/misc/id/object-id.ts +++ /dev/null @@ -1,26 +0,0 @@ -const CHARS = '0123456789abcdef'; - -function getTime(time: number) { - if (time < 0) time = 0; - if (time === 0) { - return CHARS[0]; - } - - time = Math.floor(time / 1000); - - return time.toString(16).padStart(8, CHARS[0]); -} - -function getRandom() { - let str = ''; - - for (let i = 0; i < 16; i++) { - str += CHARS[Math.floor(Math.random() * CHARS.length)]; - } - - return str; -} - -export function genObjectId(date: Date): string { - return getTime(date.getTime()) + getRandom(); -}