Skip to content

Commit

Permalink
chore: Check SERVER_ID in generateUID (#9270)
Browse files Browse the repository at this point in the history
* chore: Check SERVER_ID in generateUID

If the server id is out of range, we potentially generate duplicate ids,
so it's better to throw immediately.

* Leave breadcrumps
  • Loading branch information
Dschoordsch committed Dec 7, 2023
1 parent b9ddeff commit 7b8ead0
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/server/generateUID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const MID_OFFSET = BigInt(SEQ_BIT_LEN)
const BIG_ZERO = BigInt(0)
const MAX_SEQ = 2 ** SEQ_BIT_LEN - 1

// if MID overflows, we will generate duplicate ids, throw instead
if (MID < 0 || MID > 2 ** MACHINE_ID_BIT_LEN - 1) {
throw new Error('SERVER_ID must be between 0 and 1023')
}

let seq = 0
let lastTime = Date.now()
const generateUID = () => {
Expand Down

0 comments on commit 7b8ead0

Please sign in to comment.