Closed
Description
I have a use case where I'm receiving network data which has its own time source (milliseconds from unix epoch), but I want to preserve this timestamp (epoch) in the generated KSUID.
Delving into the source doesn't allow for a timestamp to be specified. next()
might not be the right place for an optional epoch, so the following suggests a dedicated API.
It's possible to override the KSUID64 base class as follows:
class MyKSUID64 extends KSUID64 {
fromEpoch(epoch: number = Date.now()) {
const buf = this.timeOnlyBinary(epoch);
return this.rnd
? randomBytesFrom(this.rnd, buf, this.epochSize)
: randomBytes(buf, this.epochSize);
}
}
This takes the base implementation of nextBinary()
and allows the epoch to be specified and passed through. This overcomes an issue with using an instance of the base class from defKSUID64() as rnd
is protected, so inheritance is required.
It would make sense to add this API higher in the inheritance chain, such as AKSUID
.
Usage:
const id = new MyKSUID64()
id.fromEpoch(Date.now()) // or other millisecond count as long as its greater than or equal to the base epoch in options
Metadata
Metadata
Assignees
Labels
No labels