mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-05-13 18:45:35 +02:00
fix(backend): ULIDを正しく処理できない問題を修正 (#17310)
fix(backend): fix parseUlidFull to correctly handle Crockford Base32 chars W/X/Y/Z
This commit is contained in:
@@ -5,12 +5,19 @@
|
||||
|
||||
// Crockford's Base32
|
||||
// https://github.com/ulid/spec#encoding
|
||||
import { parseBigInt32 } from '@/misc/bigint.js';
|
||||
|
||||
const CHARS = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
|
||||
|
||||
export const ulidRegExp = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/;
|
||||
|
||||
function parseBigIntCrockford(str: string): bigint {
|
||||
let result = 0n;
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
result = result * 32n + BigInt(CHARS.indexOf(str[i]));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseBase32(timestamp: string) {
|
||||
let time = 0;
|
||||
for (let i = 0; i < timestamp.length; i++) {
|
||||
@@ -26,6 +33,6 @@ export function parseUlid(id: string): { date: Date; } {
|
||||
export function parseUlidFull(id: string): { date: number; additional: bigint; } {
|
||||
return {
|
||||
date: parseBase32(id.slice(0, 10)),
|
||||
additional: parseBigInt32(id.slice(10, 26)),
|
||||
additional: parseBigIntCrockford(id.slice(10, 26)),
|
||||
};
|
||||
}
|
||||
|
||||
41
packages/backend/test/unit/misc/ulid.ts
Normal file
41
packages/backend/test/unit/misc/ulid.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: syuilo and misskey-project
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { describe, expect, test } from '@jest/globals';
|
||||
import { parseUlidFull } from '@/misc/id/ulid.js';
|
||||
|
||||
// Timestamp part "01KPS7S300" encodes 1776816000000ms (2026-04-22T00:00:00.000Z)
|
||||
// Verified: 1*32^8 + 19*32^7 + 22*32^6 + 25*32^5 + 7*32^4 + 25*32^3 + 3*32^2 = 1776816000000
|
||||
|
||||
describe('misc:ulid', () => {
|
||||
test('parseUlidFull - timestamp is parsed correctly', () => {
|
||||
// id[10..25] = all zeros (valid Crockford Base32)
|
||||
// 2026-04-22T00:00:00.000Z
|
||||
const { date } = parseUlidFull('01KPS7S3000000000000000000');
|
||||
expect(date).toBe(1776816000000);
|
||||
});
|
||||
|
||||
test('parseUlidFull - W/X/Y/Z at id[10] (chunk 1 head) do not throw', () => {
|
||||
// id[10] = W
|
||||
expect(() => parseUlidFull('01KPS7S300W000000000000000')).not.toThrow();
|
||||
// id[10] = X
|
||||
expect(() => parseUlidFull('01KPS7S300X000000000000000')).not.toThrow();
|
||||
// id[10] = Y
|
||||
expect(() => parseUlidFull('01KPS7S300Y000000000000000')).not.toThrow();
|
||||
// id[10] = Z
|
||||
expect(() => parseUlidFull('01KPS7S300Z000000000000000')).not.toThrow();
|
||||
});
|
||||
|
||||
test('parseUlidFull - W/X/Y/Z at id[16] (chunk 2 head) do not throw', () => {
|
||||
// id[16] = W
|
||||
expect(() => parseUlidFull('01KPS7S300ABCDEFW000000000')).not.toThrow();
|
||||
// id[16] = X
|
||||
expect(() => parseUlidFull('01KPS7S300ABCDEFX000000000')).not.toThrow();
|
||||
// id[16] = Y
|
||||
expect(() => parseUlidFull('01KPS7S300ABCDEFY000000000')).not.toThrow();
|
||||
// id[16] = Z
|
||||
expect(() => parseUlidFull('01KPS7S300ABCDEFZ000000000')).not.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user