aevitas / flakeid

Discord-like implementation of highly optimized decentralized, K-ordered unique IDs in .NET.
Apache License 2.0
38 stars 9 forks source link

Add support for displaying snowflakes as long form base64 strings #3

Closed aevitas closed 2 years ago

aevitas commented 2 years ago

FlakeIds are problematic with clients that are powered by v8. This includes NodeJS in backend services, and most (if not all) JavaScript clients and browsers. These runtimes support 56 bit floating point numbers as opposed to the 64 bit integers FlakeId is based on.

To alleviate this, serving applications can call ToString() on a FlakeId, and serve the ID as a string to their clients. This is inherently dangerous, as the client will observe this ID to be a number, and may therefore parse it as such. This parse operation will be successful and the client will be in the possession of a number ID.

However, because this number is lacking (very important) 8 bits, it is no longer guaranteed to be unique and will sooner or later collide. This is an extremely pernicious bug that could have disastrous consequences, such as retrieving the wrong customer information from an external service, or worse.

Therefore, this PR introduces an alternate ToString() method in the form of a ToStringIdentifier() extension method, which takes the textual bytes of the FlakeId, and returns them in base64 format. This base64 identifier can then freely be passed around, similar to YouTube video IDs. They're a lot longer, though.