@neko-makura/railid 4.4.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (8) hide show
  1. package/README.md +402 -382
  2. package/errors.js +152 -152
  3. package/index.d.ts +267 -224
  4. package/index.js +238 -238
  5. package/no-tpm.d.ts +143 -125
  6. package/no-tpm.js +48 -41
  7. package/package.json +57 -57
  8. package/v2.js +245 -245
package/index.d.ts CHANGED
@@ -1,224 +1,267 @@
1
- export type Backend = "tpm" | "os-only";
2
-
3
- /**
4
- * Stable `error.code` values set by the bindings. Native N-API failures are
5
- * mapped to these codes at the JavaScript boundary; `TpmUnavailable` is the
6
- * fail-closed signal from the TPM hybrid profile.
7
- */
8
- export type RailIdErrorCode =
9
- | "TpmUnavailable"
10
- | "TpmRandomFailure"
11
- | "OsRandomFailure"
12
- | "TimestampOutOfRange"
13
- | "InvalidInput"
14
- | "CapacityOverflow"
15
- | "WorkerFailure";
16
-
17
- /**
18
- * Errors thrown by the bindings carry one of the {@link RailIdErrorCode}
19
- * values in `code`.
20
- */
21
- export interface RailIdError extends Error {
22
- code: RailIdErrorCode;
23
- }
24
-
25
- export interface DecodedRailId {
26
- /** Unix timestamp in milliseconds. */
27
- timestampMs: number;
28
- /** 150-bit random field as 38 lowercase hex characters. */
29
- randomHex: string;
30
- /** Canonical 32-character Base64URL encoding. */
31
- encoded: string;
32
- }
33
-
34
- /** Generate one RailID with the standard TPM hybrid profile. */
35
- export declare function generate(): string;
36
-
37
- /** Generate one RailID with the explicitly degraded OS-only profile. */
38
- export declare function generateOsOnly(): string;
39
-
40
- /** Generate one TPM-profile RailID as 32 ASCII bytes, without a JS string. */
41
- export declare function generateBuffer(): Buffer;
42
-
43
- /** Generate one OS-only RailID as 32 ASCII bytes, without a JS string. */
44
- export declare function generateOsOnlyBuffer(): Buffer;
45
-
46
- /**
47
- * Generate one 126-bit v4 RailID as a 21-character string with the TPM hybrid
48
- * profile. Throws `TpmUnavailable` rather than falling back to OS-only.
49
- */
50
- export declare function generateV4(): string;
51
-
52
- /** Generate one 126-bit v4 RailID as a 21-character string with the OS-only profile. */
53
- export declare function generateV4OsOnly(): string;
54
-
55
- /** Generate one 126-bit v4 RailID as 21 ASCII bytes with the TPM hybrid profile. */
56
- export declare function generateV4Buffer(): Buffer;
57
-
58
- /** Generate one 126-bit v4 RailID as 21 ASCII bytes with the OS-only profile. */
59
- export declare function generateV4BufferOsOnly(): Buffer;
60
-
61
- /**
62
- * Generate `count` v4 IDs as one contiguous `count * 21` Buffer with the TPM
63
- * hybrid profile. Capped at 1,024 IDs and fails closed without a TPM.
64
- * Counts are validated before allocation; invalid counts throw `InvalidInput`.
65
- *
66
- * The returned Buffer's external allocation is reported to V8 so repeated
67
- * batches raise GC pressure instead of growing RSS silently. For sustained
68
- * throughput, prefer `generateV4ManyEncodedInto` to avoid the `count * 21`
69
- * allocation entirely.
70
- */
71
- export declare function generateV4ManyEncoded(count: number): Buffer;
72
-
73
- /**
74
- * Generate `count` v4 IDs as one contiguous `count * 21` Buffer with the
75
- * explicitly degraded OS-only profile. Capped at 1,000,000 IDs. Invalid
76
- * counts throw `InvalidInput`.
77
- */
78
- export declare function generateV4ManyEncodedOsOnly(count: number): Buffer;
79
-
80
- /**
81
- * Generate `count` v4 IDs asynchronously with the TPM hybrid profile. The
82
- * batch runs on a libuv worker thread and resolves to a `count * 21` Buffer.
83
- * Capped at 1,024 IDs. Fails closed with `TpmUnavailable` when no TPM is
84
- * usable; invalid counts throw `InvalidInput` synchronously.
85
- */
86
- export declare function generateV4ManyEncodedAsync(count: number): Promise<Buffer>;
87
-
88
- /**
89
- * Generate `count` v4 IDs asynchronously with the OS-only profile. The batch
90
- * runs on a libuv worker thread and resolves to a `count * 21` Buffer. Capped
91
- * at 1,000,000 IDs. Invalid counts throw `InvalidInput` synchronously.
92
- */
93
- export declare function generateV4ManyEncodedOsOnlyAsync(
94
- count: number,
95
- ): Promise<Buffer>;
96
-
97
- /**
98
- * Generate v4 IDs with the TPM hybrid profile into a caller-provided Buffer.
99
- * The buffer length must be an exact multiple of 21 and determines the count;
100
- * it is capped at 1,024 IDs. Nothing is returned. Invalid lengths throw
101
- * `InvalidInput` before any bytes are written. Fails closed with
102
- * `TpmUnavailable` without a TPM.
103
- *
104
- * This is the recommended high-throughput path: it avoids allocating and
105
- * handing a `count * 21` external Buffer to V8 on every call.
106
- */
107
- export declare function generateV4ManyEncodedInto(buffer: Buffer): void;
108
-
109
- /**
110
- * Generate v4 IDs with the OS-only profile into a caller-provided Buffer.
111
- * The buffer length must be an exact multiple of 21 and determines the count;
112
- * it is capped at 1,000,000 IDs. Nothing is returned. Invalid lengths throw
113
- * `InvalidInput` before any bytes are written.
114
- */
115
- export declare function generateV4ManyEncodedOsOnlyInto(buffer: Buffer): void;
116
-
117
- /** Generate `count` IDs synchronously as strings. OS-only strings cap at 100,000. */
118
- export declare function generateMany(count: number, osOnly?: boolean): string[];
119
-
120
- /** Generate `count` IDs synchronously as one contiguous `count * 32` Buffer. */
121
- export declare function generateManyEncoded(count: number, osOnly?: boolean): Buffer;
122
-
123
- /** Generate `count` IDs asynchronously as strings. */
124
- export declare function generateManyAsync(count: number, osOnly?: boolean): Promise<string[]>;
125
-
126
- /** Generate `count` IDs asynchronously as one contiguous `count * 32` Buffer. */
127
- export declare function generateManyEncodedAsync(
128
- count: number,
129
- osOnly?: boolean,
130
- ): Promise<Buffer>;
131
-
132
- /** Decode and validate a RailID. */
133
- export declare function decode(input: string): DecodedRailId;
134
-
135
- /** RailID crate version. */
136
- export declare function version(): string;
137
-
138
- /** Native backend compiled into the loaded addon. */
139
- export declare function backend(): Backend;
140
-
141
- export type V2Tick = "1d" | "1h" | "1s" | "100ms" | "10ms" | "1ms" | "0.1ms";
142
-
143
- /** A timestamp tick and field width. */
144
- export interface V2TimeSpec {
145
- tick: V2Tick;
146
- bits: number;
147
- }
148
-
149
- /** A v2 format description. `time` omitted means the default for `bits`. */
150
- export interface V2Format {
151
- bits: number;
152
- time?: V2Tick | V2TimeSpec | null;
153
- force?: boolean;
154
- }
155
-
156
- /** A non-fatal v2 format warning. */
157
- export interface V2Warning {
158
- code: string;
159
- reason: string;
160
- }
161
-
162
- /** A normalized v2 format. */
163
- export interface V2FormatInfo {
164
- totalBits: number;
165
- timestampBits: number;
166
- randomBits: number;
167
- tick: V2Tick | null;
168
- encodedLen: number;
169
- warnings: V2Warning[];
170
- }
171
-
172
- /** Decoded v2 fields. Timestamps are decimal strings for full precision. */
173
- export interface V2Decoded {
174
- encoded: string;
175
- bits: number;
176
- timestampBits: number;
177
- randomBits: number;
178
- ticks: string | null;
179
- timestampMs: string | null;
180
- timestampUs: string | null;
181
- randomHex: string;
182
- }
183
-
184
- /** Generate one v2 value. */
185
- export declare function generateV2(format: V2Format): string;
186
-
187
- /** Generate one v2 value as 3-171 ASCII bytes. */
188
- export declare function generateV2Buffer(format: V2Format): Buffer;
189
-
190
- /** Generate `count` v2 values as a string array. Capped at 1,024 (TPM) or 1,000,000 (OS-only). */
191
- export declare function generateV2Many(format: V2Format, count: number): string[];
192
-
193
- /** Generate `count` v2 values as one contiguous `count * encodedLen` Buffer. */
194
- export declare function generateV2ManyEncoded(format: V2Format, count: number): Buffer;
195
-
196
- /**
197
- * Generate `count` v2 values asynchronously as a string array with the TPM
198
- * hybrid profile. The batch runs on a libuv worker thread. Capped at 1,024
199
- * IDs; fails closed with `TpmUnavailable` when no TPM is usable. Invalid
200
- * formats and counts throw `InvalidInput` synchronously, before any work is
201
- * scheduled.
202
- */
203
- export declare function generateV2ManyAsync(
204
- format: V2Format,
205
- count: number,
206
- ): Promise<string[]>;
207
-
208
- /**
209
- * Generate `count` v2 values asynchronously as one contiguous
210
- * `count * encodedLen` Buffer with the TPM hybrid profile. The batch runs on a
211
- * libuv worker thread. Capped at 1,024 IDs; fails closed with
212
- * `TpmUnavailable` when no TPM is usable. Invalid formats and counts throw
213
- * `InvalidInput` synchronously, before any work is scheduled.
214
- */
215
- export declare function generateV2ManyEncodedAsync(
216
- format: V2Format,
217
- count: number,
218
- ): Promise<Buffer>;
219
-
220
- /** Decode a v2 value with the expected format. */
221
- export declare function decodeV2(input: string, format: V2Format): V2Decoded;
222
-
223
- /** Normalize and validate a v2 format. */
224
- export declare function formatV2(format: V2Format): V2FormatInfo;
1
+ export type Backend = "tpm" | "os-only";
2
+
3
+ /**
4
+ * Stable `error.code` values set by the bindings. Native N-API failures are
5
+ * mapped to these codes at the JavaScript boundary; `TpmUnavailable` is the
6
+ * fail-closed signal from the TPM hybrid profile.
7
+ */
8
+ export type RailIdErrorCode =
9
+ | "TpmUnavailable"
10
+ | "TpmRandomFailure"
11
+ | "OsRandomFailure"
12
+ | "TimestampOutOfRange"
13
+ | "InvalidInput"
14
+ | "CapacityOverflow"
15
+ | "WorkerFailure";
16
+
17
+ /**
18
+ * Errors thrown by the bindings carry one of the {@link RailIdErrorCode}
19
+ * values in `code`.
20
+ */
21
+ export interface RailIdError extends Error {
22
+ code: RailIdErrorCode;
23
+ }
24
+
25
+ export interface DecodedRailId {
26
+ /** Unix timestamp in milliseconds. */
27
+ timestampMs: number;
28
+ /** 150-bit random field as 38 lowercase hex characters. */
29
+ randomHex: string;
30
+ /** Canonical 32-character Base64URL encoding. */
31
+ encoded: string;
32
+ }
33
+
34
+ /** Generate one RailID with the standard TPM hybrid profile. */
35
+ export declare function generate(): string;
36
+
37
+ /** Generate one RailID with the explicitly degraded OS-only profile. */
38
+ export declare function generateOsOnly(): string;
39
+
40
+ /** Generate one TPM-profile RailID as 32 ASCII bytes, without a JS string. */
41
+ export declare function generateBuffer(): Buffer;
42
+
43
+ /** Generate one OS-only RailID as 32 ASCII bytes, without a JS string. */
44
+ export declare function generateOsOnlyBuffer(): Buffer;
45
+
46
+
47
+ /** Generate one timestamp-less 192-bit v5 RailID (32 characters) with TPM hybrid. */
48
+ export declare function generateV5192(): string;
49
+
50
+ /** Generate one timestamp-less 192-bit v5 RailID with the OS-only profile. */
51
+ export declare function generateV5192OsOnly(): string;
52
+
53
+ /** Generate one timestamp-less 126-bit v5 RailID (21 characters) with TPM hybrid. */
54
+ export declare function generateV5126(): string;
55
+
56
+ /** Generate one timestamp-less 126-bit v5 RailID with the OS-only profile. */
57
+ export declare function generateV5126OsOnly(): string;
58
+
59
+ /** Generate one v5-192 value as 32 ASCII bytes with the TPM hybrid profile. */
60
+ export declare function generateV5192Buffer(): Buffer;
61
+
62
+ /** Generate one v5-192 value as 32 ASCII bytes with the OS-only profile. */
63
+ export declare function generateV5192BufferOsOnly(): Buffer;
64
+
65
+ /** Generate one v5-126 value as 21 ASCII bytes with the TPM hybrid profile. */
66
+ export declare function generateV5126Buffer(): Buffer;
67
+
68
+ /** Generate one v5-126 value as 21 ASCII bytes with the OS-only profile. */
69
+ export declare function generateV5126BufferOsOnly(): Buffer;
70
+
71
+ /** Generate `count` v5-192 IDs as one contiguous `count * 32` Buffer. */
72
+ export declare function generateV5192ManyEncoded(count: number): Buffer;
73
+
74
+ /** Generate `count` v5-192 IDs with the OS-only profile. */
75
+ export declare function generateV5192ManyEncodedOsOnly(count: number): Buffer;
76
+
77
+ /** Generate `count` v5-126 IDs as one contiguous `count * 21` Buffer. */
78
+ export declare function generateV5126ManyEncoded(count: number): Buffer;
79
+
80
+ /** Generate `count` v5-126 IDs with the OS-only profile. */
81
+ export declare function generateV5126ManyEncodedOsOnly(count: number): Buffer;
82
+
83
+ /** Validate a v5-192 string and return its canonical spelling. */
84
+ export declare function decodeV5192(input: string): string;
85
+
86
+ /** Validate a v5-126 string and return its canonical spelling. */
87
+ export declare function decodeV5126(input: string): string;
88
+
89
+ /**
90
+ * Generate one 126-bit v4 RailID as a 21-character string with the TPM hybrid
91
+ * profile. Throws `TpmUnavailable` rather than falling back to OS-only.
92
+ */
93
+ export declare function generateV4(): string;
94
+
95
+ /** Generate one 126-bit v4 RailID as a 21-character string with the OS-only profile. */
96
+ export declare function generateV4OsOnly(): string;
97
+
98
+ /** Generate one 126-bit v4 RailID as 21 ASCII bytes with the TPM hybrid profile. */
99
+ export declare function generateV4Buffer(): Buffer;
100
+
101
+ /** Generate one 126-bit v4 RailID as 21 ASCII bytes with the OS-only profile. */
102
+ export declare function generateV4BufferOsOnly(): Buffer;
103
+
104
+ /**
105
+ * Generate `count` v4 IDs as one contiguous `count * 21` Buffer with the TPM
106
+ * hybrid profile. Capped at 1,024 IDs and fails closed without a TPM.
107
+ * Counts are validated before allocation; invalid counts throw `InvalidInput`.
108
+ *
109
+ * The returned Buffer's external allocation is reported to V8 so repeated
110
+ * batches raise GC pressure instead of growing RSS silently. For sustained
111
+ * throughput, prefer `generateV4ManyEncodedInto` to avoid the `count * 21`
112
+ * allocation entirely.
113
+ */
114
+ export declare function generateV4ManyEncoded(count: number): Buffer;
115
+
116
+ /**
117
+ * Generate `count` v4 IDs as one contiguous `count * 21` Buffer with the
118
+ * explicitly degraded OS-only profile. Capped at 1,000,000 IDs. Invalid
119
+ * counts throw `InvalidInput`.
120
+ */
121
+ export declare function generateV4ManyEncodedOsOnly(count: number): Buffer;
122
+
123
+ /**
124
+ * Generate `count` v4 IDs asynchronously with the TPM hybrid profile. The
125
+ * batch runs on a libuv worker thread and resolves to a `count * 21` Buffer.
126
+ * Capped at 1,024 IDs. Fails closed with `TpmUnavailable` when no TPM is
127
+ * usable; invalid counts throw `InvalidInput` synchronously.
128
+ */
129
+ export declare function generateV4ManyEncodedAsync(count: number): Promise<Buffer>;
130
+
131
+ /**
132
+ * Generate `count` v4 IDs asynchronously with the OS-only profile. The batch
133
+ * runs on a libuv worker thread and resolves to a `count * 21` Buffer. Capped
134
+ * at 1,000,000 IDs. Invalid counts throw `InvalidInput` synchronously.
135
+ */
136
+ export declare function generateV4ManyEncodedOsOnlyAsync(
137
+ count: number,
138
+ ): Promise<Buffer>;
139
+
140
+ /**
141
+ * Generate v4 IDs with the TPM hybrid profile into a caller-provided Buffer.
142
+ * The buffer length must be an exact multiple of 21 and determines the count;
143
+ * it is capped at 1,024 IDs. Nothing is returned. Invalid lengths throw
144
+ * `InvalidInput` before any bytes are written. Fails closed with
145
+ * `TpmUnavailable` without a TPM.
146
+ *
147
+ * This is the recommended high-throughput path: it avoids allocating and
148
+ * handing a `count * 21` external Buffer to V8 on every call.
149
+ */
150
+ export declare function generateV4ManyEncodedInto(buffer: Buffer): void;
151
+
152
+ /**
153
+ * Generate v4 IDs with the OS-only profile into a caller-provided Buffer.
154
+ * The buffer length must be an exact multiple of 21 and determines the count;
155
+ * it is capped at 1,000,000 IDs. Nothing is returned. Invalid lengths throw
156
+ * `InvalidInput` before any bytes are written.
157
+ */
158
+ export declare function generateV4ManyEncodedOsOnlyInto(buffer: Buffer): void;
159
+
160
+ /** Generate `count` IDs synchronously as strings. OS-only strings cap at 100,000. */
161
+ export declare function generateMany(count: number, osOnly?: boolean): string[];
162
+
163
+ /** Generate `count` IDs synchronously as one contiguous `count * 32` Buffer. */
164
+ export declare function generateManyEncoded(count: number, osOnly?: boolean): Buffer;
165
+
166
+ /** Generate `count` IDs asynchronously as strings. */
167
+ export declare function generateManyAsync(count: number, osOnly?: boolean): Promise<string[]>;
168
+
169
+ /** Generate `count` IDs asynchronously as one contiguous `count * 32` Buffer. */
170
+ export declare function generateManyEncodedAsync(
171
+ count: number,
172
+ osOnly?: boolean,
173
+ ): Promise<Buffer>;
174
+
175
+ /** Decode and validate a RailID. */
176
+ export declare function decode(input: string): DecodedRailId;
177
+
178
+ /** RailID crate version. */
179
+ export declare function version(): string;
180
+
181
+ /** Native backend compiled into the loaded addon. */
182
+ export declare function backend(): Backend;
183
+
184
+ export type V2Tick = "1d" | "1h" | "1s" | "100ms" | "10ms" | "1ms" | "0.1ms";
185
+
186
+ /** A timestamp tick and field width. */
187
+ export interface V2TimeSpec {
188
+ tick: V2Tick;
189
+ bits: number;
190
+ }
191
+
192
+ /** A v2 format description. `time` omitted means the default for `bits`. */
193
+ export interface V2Format {
194
+ bits: number;
195
+ time?: V2Tick | V2TimeSpec | null;
196
+ force?: boolean;
197
+ }
198
+
199
+ /** A non-fatal v2 format warning. */
200
+ export interface V2Warning {
201
+ code: string;
202
+ reason: string;
203
+ }
204
+
205
+ /** A normalized v2 format. */
206
+ export interface V2FormatInfo {
207
+ totalBits: number;
208
+ timestampBits: number;
209
+ randomBits: number;
210
+ tick: V2Tick | null;
211
+ encodedLen: number;
212
+ warnings: V2Warning[];
213
+ }
214
+
215
+ /** Decoded v2 fields. Timestamps are decimal strings for full precision. */
216
+ export interface V2Decoded {
217
+ encoded: string;
218
+ bits: number;
219
+ timestampBits: number;
220
+ randomBits: number;
221
+ ticks: string | null;
222
+ timestampMs: string | null;
223
+ timestampUs: string | null;
224
+ randomHex: string;
225
+ }
226
+
227
+ /** Generate one v2 value. */
228
+ export declare function generateV2(format: V2Format): string;
229
+
230
+ /** Generate one v2 value as 3-171 ASCII bytes. */
231
+ export declare function generateV2Buffer(format: V2Format): Buffer;
232
+
233
+ /** Generate `count` v2 values as a string array. Capped at 1,024 (TPM) or 1,000,000 (OS-only). */
234
+ export declare function generateV2Many(format: V2Format, count: number): string[];
235
+
236
+ /** Generate `count` v2 values as one contiguous `count * encodedLen` Buffer. */
237
+ export declare function generateV2ManyEncoded(format: V2Format, count: number): Buffer;
238
+
239
+ /**
240
+ * Generate `count` v2 values asynchronously as a string array with the TPM
241
+ * hybrid profile. The batch runs on a libuv worker thread. Capped at 1,024
242
+ * IDs; fails closed with `TpmUnavailable` when no TPM is usable. Invalid
243
+ * formats and counts throw `InvalidInput` synchronously, before any work is
244
+ * scheduled.
245
+ */
246
+ export declare function generateV2ManyAsync(
247
+ format: V2Format,
248
+ count: number,
249
+ ): Promise<string[]>;
250
+
251
+ /**
252
+ * Generate `count` v2 values asynchronously as one contiguous
253
+ * `count * encodedLen` Buffer with the TPM hybrid profile. The batch runs on a
254
+ * libuv worker thread. Capped at 1,024 IDs; fails closed with
255
+ * `TpmUnavailable` when no TPM is usable. Invalid formats and counts throw
256
+ * `InvalidInput` synchronously, before any work is scheduled.
257
+ */
258
+ export declare function generateV2ManyEncodedAsync(
259
+ format: V2Format,
260
+ count: number,
261
+ ): Promise<Buffer>;
262
+
263
+ /** Decode a v2 value with the expected format. */
264
+ export declare function decodeV2(input: string, format: V2Format): V2Decoded;
265
+
266
+ /** Normalize and validate a v2 format. */
267
+ export declare function formatV2(format: V2Format): V2FormatInfo;