| 1 | /** |
| 2 | * Branded integer identifiers for cluster runners. A `MachineId` marks the |
| 3 | * machine component used by cluster services, especially snowflake id |
| 4 | * generation, while keeping the value distinct from an ordinary `number` in |
| 5 | * TypeScript APIs. |
| 6 | * |
| 7 | * @stability unstable |
| 8 | * @since 4.0.0 |
| 9 | */ |
| 10 | import * as Schema from "../Schema.ts" |
| 11 | |
| 12 | /** |
| 13 | * Schema for branded integer machine identifiers used by the cluster. |
| 14 | * |
| 15 | * @stability unstable |
| 16 | * @category constructors |
| 17 | * @since 4.0.0 |
| 18 | */ |
| 19 | export const MachineId = Schema.Int.pipe( |
| 20 | Schema.brand("~effect/cluster/MachineId"), |
| 21 | Schema.annotate({ |
| 22 | toFormatter: () => (machineId: string) => `MachineId(${machineId})` |
| 23 | }) |
| 24 | ) |
| 25 | |
| 26 | /** |
| 27 | * Branded integer type representing a cluster machine ID. |
| 28 | * |
| 29 | * @stability unstable |
| 30 | * @category models |
| 31 | * @since 4.0.0 |
| 32 | */ |
| 33 | export type MachineId = typeof MachineId.Type |
| 34 | |
| 35 | /** |
| 36 | * Brands a number as a `MachineId`. |
| 37 | * |
| 38 | * **When to use** |
| 39 | * |
| 40 | * Use to turn a trusted numeric machine id into the branded type when |
| 41 | * implementing runner storage adapters or configuring snowflake generation. |
| 42 | * |
| 43 | * **Details** |
| 44 | * |
| 45 | * The branded value is the original number at runtime. |
| 46 | * |
| 47 | * **Gotchas** |
| 48 | * |
| 49 | * `make` does not validate integer input or enforce the snowflake machine-id |
| 50 | * range. Snowflake ids encode the machine component modulo 1024. |
| 51 | * |
| 52 | * @see {@link MachineId} for the schema that validates branded integer machine identifiers |
| 53 | * |
| 54 | * @stability unstable |
| 55 | * @category constructors |
| 56 | * @since 4.0.0 |
| 57 | */ |
| 58 | export const make = (id: number): MachineId => id as MachineId |