22 lines
597 B
TypeScript
22 lines
597 B
TypeScript
export default defineNitroPlugin(nitroApp => {
|
||
|
||
nitroApp.hooks.hook('request', async (event) => {
|
||
event.context.getId = generateEnhancedId
|
||
})
|
||
})
|
||
const EPOCH = 1609459200000;
|
||
let sequence = 0;
|
||
|
||
function generateEnhancedId(): string {
|
||
const now = Date.now();
|
||
const timestamp = now - EPOCH;
|
||
|
||
// 10 位序列号(支持 1024 个 ID/ms)
|
||
sequence = (sequence + 1) % 0x400;
|
||
|
||
// 组合成 64 位 ID
|
||
const high = (timestamp << 14) | sequence;
|
||
const low = Math.floor(Math.random() * 0x100000000);
|
||
|
||
return ((BigInt(high) << 32n) | BigInt(low)).toString();
|
||
} |