https://github.com/bradbot1/gleam_bespoke
A UID library for gleam
https://github.com/bradbot1/gleam_bespoke
erlang gleam gleam-lang gleam-language hexdocs javascript library
Last synced: 3 months ago
JSON representation
A UID library for gleam
- Host: GitHub
- URL: https://github.com/bradbot1/gleam_bespoke
- Owner: BradBot1
- License: apache-2.0
- Created: 2025-07-10T00:56:17.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2025-07-16T22:49:55.000Z (12 months ago)
- Last Synced: 2025-07-18T05:26:30.869Z (12 months ago)
- Topics: erlang, gleam, gleam-lang, gleam-language, hexdocs, javascript, library
- Language: Gleam
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bespoke
[](https://hex.pm/packages/bespoke)
[](https://hexdocs.pm/bespoke/)
```sh
gleam add bespoke@1
```
## Types
### Bespoke
- A `Bespoke` instance *cannot* be serialized and transmitted over the network.
- It's guarentees do *not* apply across machines, but *do* apply across erlang processes within the same cluster.
- In erlang this is implemented via a `reference`.
- In javascript this is implemented via a `Symbol`.
### SerializableBespoke
- A `SerializableBespoke` instance *can* be serialized and transmitted over the network.
- It's guarentees apply everywhere, but could theoretically be broken.
- Larger memory overhead than a `Bespoke` instance.
- It is represented as a BitArray of 2048 bits split into the time of creation and random number generation.
- Can be shared accross erlang *and* javascript processes.
- On javascript this is implemented via a `Uint32Array`. (This is mutable so avoid editing it!)
- On erlang this is implemented via a `bin`.
## Usage
### Local / Internal guaranteed uniqueness
```gleam
import bespoke
pub fn main() -> Nil {
let id = bespoke.new()
let id2 = bespoke.new()
// id and id2 are guaranteed to be unique
}
```
### Shared / External guaranteed* uniqueness
> **Note**: *Guaranteed uniqueness is not guaranteed as psudo-randomness is utilised
```gleam
import bespoke/serializable as bespoke
pub fn main() -> Nil {
let key = bespoke.new()
let stringified = bespoke.serialize(key)
let assert Ok(parsed) = bespoke.deserialize(stringified)
assert parsed == key // Will be parsed to be equal to the original key
}
```
## Supported runtimes
|Runtime|Supported|
|-------|---------|
|Erlang|✅|
|Node|✅|
|Deno|✅|
|Bun|✅|
## Contributing
PRs are welcome with or without prior discussion.
## License
This project is licensed under the Apache License, Version 2.0.
See [LICENSE](LICENSE) for more information.
## Additional information
If you're unsure about anything, contact me directly via BradBot_1#2042
Further documentation can be found at .