mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-30 07:13:09 +02:00
Update id generation methods
This commit is contained in:
parent
b247be80cc
commit
e64912545a
6 changed files with 30 additions and 72 deletions
|
@ -127,19 +127,11 @@ drive:
|
|||
# change it according to your preferences.
|
||||
|
||||
# Available methods:
|
||||
# aid1 ... Use AID for ID generation (with random 1 char)
|
||||
# aid2 ... Use AID for ID generation (with random 2 chars)
|
||||
# aid3 ... Use AID for ID generation (with random 3 chars)
|
||||
# aid4 ... Use AID for ID generation (with random 4 chars)
|
||||
# aid ... Use AID for ID generation
|
||||
# ulid ... Use ulid for ID generation
|
||||
# objectid ... This is left for backward compatibility.
|
||||
|
||||
# AID(n) is the original ID generation method.
|
||||
# The trailing n represents the number of random characters that
|
||||
# will be suffixed.
|
||||
# The larger n is the safer. If n is small, the possibility of
|
||||
# collision at the same time increases, but there are also
|
||||
# advantages such as shortening of the URL.
|
||||
# AID is the original ID generation method.
|
||||
|
||||
# ULID: Universally Unique Lexicographically Sortable Identifier.
|
||||
# for more details: https://github.com/ulid/spec
|
||||
|
@ -148,7 +140,7 @@ drive:
|
|||
# ObjectID is the method used in previous versions of Misskey.
|
||||
# * Choose this if you are migrating from a previous Misskey.
|
||||
|
||||
id: 'aid2'
|
||||
id: 'aid'
|
||||
|
||||
# ┌─────────────────────┐
|
||||
#───┘ Other configuration └─────────────────────────────────────
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
// AID
|
||||
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さnの[ランダムな文字列]
|
||||
|
||||
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
const TIME2000 = 946684800000;
|
||||
|
||||
function getTime(time: number) {
|
||||
time = time - TIME2000;
|
||||
if (time < 0) time = 0;
|
||||
|
||||
return time.toString(36);
|
||||
}
|
||||
|
||||
function getRandom(length: number) {
|
||||
let str = '';
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
str += CHARS[Math.floor(Math.random() * CHARS.length)];
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
export function genAid(date: Date, rand: number): string {
|
||||
return getTime(date.getTime()).padStart(8, CHARS[0]) + getRandom(rand);
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
// AID(Cheep)
|
||||
// 長さ6の[2000年1月1日からの経過秒をbase36でエンコードしたもの] + 長さ3の[ランダムな文字列]
|
||||
|
||||
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
const TIME2000 = 946684800000;
|
||||
|
||||
function getTime(time: number) {
|
||||
time = time - TIME2000;
|
||||
if (time < 0) time = 0;
|
||||
time = Math.floor(time / 1000);
|
||||
return time.toString(36);
|
||||
}
|
||||
|
||||
function getRandom() {
|
||||
let str = '';
|
||||
|
||||
for (let i = 0; i < 3; i++) {
|
||||
str += CHARS[Math.floor(Math.random() * CHARS.length)];
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
export function genAidc(date: Date): string {
|
||||
return getTime(date.getTime()).padStart(6, CHARS[0]) + getRandom();
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
import { ulid } from 'ulid';
|
||||
import { genAid } from './aid';
|
||||
import { genAidc } from './aidc';
|
||||
import { genObjectId } from './object-id';
|
||||
import { genAid } from './id/aid';
|
||||
import { genObjectId } from './id/object-id';
|
||||
import config from '../config';
|
||||
|
||||
const metohd = config.id.toLowerCase();
|
||||
|
@ -10,11 +9,7 @@ export function genId(date?: Date): string {
|
|||
if (!date || (date > new Date())) date = new Date();
|
||||
|
||||
switch (metohd) {
|
||||
case 'aidc': return genAidc(date);
|
||||
case 'aid1': return genAid(date, 1);
|
||||
case 'aid2': return genAid(date, 2);
|
||||
case 'aid3': return genAid(date, 3);
|
||||
case 'aid4': return genAid(date, 4);
|
||||
case 'aid': return genAid(date);
|
||||
case 'ulid': return ulid(date.getTime());
|
||||
case 'objectid': return genObjectId(date);
|
||||
default: throw 'unknown id generation method';
|
||||
|
|
23
src/misc/id/aid.ts
Normal file
23
src/misc/id/aid.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
// AID
|
||||
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
|
||||
|
||||
import * as cluster from 'cluster';
|
||||
|
||||
const TIME2000 = 946684800000;
|
||||
let counter = process.pid + (cluster.isMaster ? 0 : cluster.worker.id);
|
||||
|
||||
function getTime(time: number) {
|
||||
time = time - TIME2000;
|
||||
if (time < 0) time = 0;
|
||||
|
||||
return time.toString(36).padStart(8, '0');
|
||||
}
|
||||
|
||||
function getRandom() {
|
||||
return counter.toString(36).padStart(length, '0').substr(2);
|
||||
}
|
||||
|
||||
export function genAid(date: Date): string {
|
||||
counter++;
|
||||
return getTime(date.getTime()) + getRandom();
|
||||
}
|
|
@ -8,7 +8,7 @@ function getTime(time: number) {
|
|||
|
||||
time = Math.floor(time / 1000);
|
||||
|
||||
return time.toString(16);
|
||||
return time.toString(16).padStart(8, CHARS[0]);
|
||||
}
|
||||
|
||||
function getRandom() {
|
Loading…
Reference in a new issue