@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.
- package/README.md +402 -382
- package/errors.js +152 -152
- package/index.d.ts +267 -224
- package/index.js +238 -238
- package/no-tpm.d.ts +143 -125
- package/no-tpm.js +48 -41
- package/package.json +57 -57
- package/v2.js +245 -245
package/README.md
CHANGED
|
@@ -1,382 +1,402 @@
|
|
|
1
|
-
# @neko-makura/railid (Node.js)
|
|
2
|
-
|
|
3
|
-
Native N-API bindings for [RailID](https://crates.io/crates/railid).
|
|
4
|
-
|
|
5
|
-
> Maintenance status: this binding remains supported, but Rust is the primary
|
|
6
|
-
> optimization target. For throughput, prefer the batch APIs over per-ID calls.
|
|
7
|
-
|
|
8
|
-
```bash
|
|
9
|
-
npm install @neko-makura/railid
|
|
10
|
-
```
|
|
11
|
-
|
|
12
|
-
The
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
|
19
|
-
|
|
20
|
-
| Linux |
|
|
21
|
-
| Linux |
|
|
22
|
-
| Linux |
|
|
23
|
-
| Linux |
|
|
24
|
-
|
|
|
25
|
-
|
|
|
26
|
-
|
|
|
27
|
-
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
|
108
|
-
|
|
109
|
-
| `
|
|
110
|
-
| `
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
`
|
|
114
|
-
`
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
- OS-only
|
|
119
|
-
|
|
120
|
-
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
`
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
`
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
`
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
-
|
|
279
|
-
|
|
280
|
-
`
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
`
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
```bash
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
1
|
+
# @neko-makura/railid (Node.js)
|
|
2
|
+
|
|
3
|
+
Native N-API bindings for [RailID](https://crates.io/crates/railid).
|
|
4
|
+
|
|
5
|
+
> Maintenance status: this binding remains supported, but Rust is the primary
|
|
6
|
+
> optimization target. For throughput, prefer the batch APIs over per-ID calls.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @neko-makura/railid
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The npm install pulls in one of the ten optional `@neko-makura/railid-<target>`
|
|
13
|
+
packages, each containing that platform's prebuilt N-API addon. The main
|
|
14
|
+
package itself is JavaScript only. Node.js 18 or newer is required.
|
|
15
|
+
|
|
16
|
+
## Platforms
|
|
17
|
+
|
|
18
|
+
| Platform | Architecture | libc | Backend |
|
|
19
|
+
|---|---|---|---|
|
|
20
|
+
| Linux | x64 | glibc | TPM (`libtss2`) + OS-only fallback |
|
|
21
|
+
| Linux | x64 | musl | OS-only |
|
|
22
|
+
| Linux | arm64 | glibc | OS-only |
|
|
23
|
+
| Linux | arm64 | musl | OS-only |
|
|
24
|
+
| Linux | armv7 | glibc (hard-float) | OS-only |
|
|
25
|
+
| Linux | riscv64 | glibc | OS-only |
|
|
26
|
+
| Windows | x64 | MSVC | TPM (TBS) |
|
|
27
|
+
| Windows | arm64 | MSVC | TPM (TBS) |
|
|
28
|
+
| macOS | x64 | — | OS-only |
|
|
29
|
+
| macOS | arm64 | — | OS-only |
|
|
30
|
+
|
|
31
|
+
`index.js` selects the platform package from `process.platform`, `process.arch`
|
|
32
|
+
and, on Linux, whether the Node process runs on glibc or musl. A local `.node`
|
|
33
|
+
file in the checkout takes precedence, so `npm run build` still works. When the
|
|
34
|
+
addon's version differs from the JavaScript package, loading warns (minor)
|
|
35
|
+
or fails (major). The darwin packages may lag a release while a macOS runner
|
|
36
|
+
is unavailable; check `railid.version()` before relying on new APIs.
|
|
37
|
+
|
|
38
|
+
The riscv64 addon targets the `riscv64gc` glibc ABI used by distribution Node
|
|
39
|
+
builds (for example Ubuntu 24.04). Node.js does not publish official riscv64
|
|
40
|
+
binaries; use a distribution package.
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
## v5 timestamp-less formats
|
|
45
|
+
|
|
46
|
+
`generateV5192()` / `generateV5126()` produce 32- / 21-character identifiers
|
|
47
|
+
with no timestamp and no time ordering. Their values are identical to the
|
|
48
|
+
equivalent v2 formats (custom 192-bit without a timestamp and the `B126`
|
|
49
|
+
preset). Buffer variants (`generateV5192Buffer`, `generateV5126Buffer`) and
|
|
50
|
+
contiguous batch variants (`generateV5192ManyEncoded`, `generateV5126ManyEncoded`)
|
|
51
|
+
avoid JavaScript string allocations. `decodeV5192()` / `decodeV5126()` validate
|
|
52
|
+
and return the canonical spelling. The strings are not self-describing: decode
|
|
53
|
+
with the intended function and store an external format tag when formats are
|
|
54
|
+
mixed. The `no-tpm` entry point maps each v5 generator to its OS-only variant.
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
const railid = require('@neko-makura/railid');
|
|
60
|
+
|
|
61
|
+
console.log(railid.version());
|
|
62
|
+
console.log(railid.backend()); // "tpm" or "os-only"
|
|
63
|
+
|
|
64
|
+
// Standard TPM hybrid profile. Throws if the backend is OS-only or no TPM
|
|
65
|
+
// device is available.
|
|
66
|
+
const id = railid.generate();
|
|
67
|
+
console.log(id);
|
|
68
|
+
|
|
69
|
+
// Explicitly degraded OS-only profile.
|
|
70
|
+
const osId = railid.generateOsOnly();
|
|
71
|
+
|
|
72
|
+
// Synchronous batch.
|
|
73
|
+
const ids = railid.generateMany(1000, true);
|
|
74
|
+
const batch = railid.generateManyEncoded(1000, true);
|
|
75
|
+
// batch is a Buffer of 1000 * 32 ASCII bytes.
|
|
76
|
+
|
|
77
|
+
// Asynchronous batch: runs on the libuv thread pool.
|
|
78
|
+
const asyncIds = await railid.generateManyAsync(1000, true);
|
|
79
|
+
const asyncBatch = await railid.generateManyEncodedAsync(1000, true);
|
|
80
|
+
|
|
81
|
+
const decoded = railid.decode(id);
|
|
82
|
+
console.log(decoded.timestampMs, decoded.randomHex, decoded.encoded);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## TPM-less deployments
|
|
86
|
+
|
|
87
|
+
The default entry point keeps the fail-closed TPM hybrid profile: `generate()`
|
|
88
|
+
throws `TpmUnavailable` when the backend or the device is missing. Hosts that
|
|
89
|
+
never have a TPM (for example a Raspberry Pi 4) should import the explicit
|
|
90
|
+
TPM-less entry point:
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
const railid = require('@neko-makura/railid/no-tpm');
|
|
94
|
+
|
|
95
|
+
railid.backend(); // "os-only"
|
|
96
|
+
const id = railid.generate();
|
|
97
|
+
const ids = railid.generateMany(1000);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
It exposes the same functions and output format, but every generator uses the
|
|
101
|
+
explicitly degraded OS-only profile and `generateMany(count)` takes no profile
|
|
102
|
+
argument. The choice is visible at the import site, and the default entry point
|
|
103
|
+
still never falls back implicitly.
|
|
104
|
+
|
|
105
|
+
## API
|
|
106
|
+
|
|
107
|
+
| Function | Result | Notes |
|
|
108
|
+
|---|---|---|
|
|
109
|
+
| `generate()` | `string` | TPM hybrid profile; throws `TpmUnavailable` if unavailable |
|
|
110
|
+
| `generateOsOnly()` | `string` | Explicit degraded profile |
|
|
111
|
+
| `generateBuffer()` | `Buffer` | TPM profile as 32 ASCII bytes, no JS string |
|
|
112
|
+
| `generateOsOnlyBuffer()` | `Buffer` | OS-only as 32 ASCII bytes, no JS string |
|
|
113
|
+
| `generateV4()` | `string` | 126-bit v4; TPM hybrid profile, fails closed |
|
|
114
|
+
| `generateV4OsOnly()` | `string` | 126-bit v4; explicit degraded OS-only profile |
|
|
115
|
+
| `generateV4Buffer()` | `Buffer` | 126-bit v4; TPM profile as 21 ASCII bytes |
|
|
116
|
+
| `generateV4BufferOsOnly()` | `Buffer` | 126-bit v4; OS-only as 21 ASCII bytes |
|
|
117
|
+
| `generateV4ManyEncoded(count)` | `Buffer` | 126-bit v4; TPM profile; `count * 21` bytes |
|
|
118
|
+
| `generateV4ManyEncodedOsOnly(count)` | `Buffer` | 126-bit v4; OS-only; `count * 21` bytes |
|
|
119
|
+
| `generateV4ManyEncodedAsync(count)` | `Promise<Buffer>` | 126-bit v4; TPM profile; off the JS thread |
|
|
120
|
+
| `generateV4ManyEncodedOsOnlyAsync(count)` | `Promise<Buffer>` | 126-bit v4; OS-only; off the JS thread |
|
|
121
|
+
| `generateV4ManyEncodedInto(buffer)` | `void` | 126-bit v4; TPM profile; writes `buffer` (multiple of 21) |
|
|
122
|
+
| `generateV4ManyEncodedOsOnlyInto(buffer)` | `void` | 126-bit v4; OS-only; writes `buffer` |
|
|
123
|
+
| `generateMany(count, osOnly?)` | `string[]` | Sync; `osOnly` defaults to `false` |
|
|
124
|
+
| `generateManyEncoded(count, osOnly?)` | `Buffer` | Sync; `count * 32` bytes |
|
|
125
|
+
| `generateManyAsync(count, osOnly?)` | `Promise<string[]>` | Bulk work off the JS thread |
|
|
126
|
+
| `generateManyEncodedAsync(count, osOnly?)` | `Promise<Buffer>` | Bulk work off the JS thread |
|
|
127
|
+
| `decode(input)` | `{ timestampMs, randomHex, encoded }` | Validates canonical Base64URL |
|
|
128
|
+
| `version()` | `string` | RailID crate version |
|
|
129
|
+
| `backend()` | `"tpm" \| "os-only"` | Backend compiled into the loaded binary |
|
|
130
|
+
|
|
131
|
+
v2 adds `generateV2`, `generateV2Buffer`, `generateV2Many`,
|
|
132
|
+
`generateV2ManyEncoded`, `generateV2ManyAsync`, `generateV2ManyEncodedAsync`,
|
|
133
|
+
`decodeV2`, and `formatV2`; see [RailID v2](#railid-v2) below.
|
|
134
|
+
|
|
135
|
+
Limits:
|
|
136
|
+
|
|
137
|
+
- OS-only strings: 100,000 IDs
|
|
138
|
+
- OS-only encoded: 1,000,000 IDs
|
|
139
|
+
- TPM: 1,024 IDs
|
|
140
|
+
|
|
141
|
+
Counts above the limit are rejected before allocation. Every batch `count`
|
|
142
|
+
must be a safe integer in `0..4294967295`; fractional, negative, `NaN`,
|
|
143
|
+
`Infinity`, and out-of-range values throw instead of wrapping. v2 batches use
|
|
144
|
+
the same caps (1,024 TPM / 1,000,000 OS-only), as do the v4 async batches. The
|
|
145
|
+
v4 `*Into` functions derive their count from the buffer length, require an
|
|
146
|
+
exact multiple of 21, and apply the same caps before writing.
|
|
147
|
+
|
|
148
|
+
## Errors
|
|
149
|
+
|
|
150
|
+
Native N-API failures are mapped once at the JavaScript boundary to a stable
|
|
151
|
+
`error.code`, so callers do not need to parse messages:
|
|
152
|
+
|
|
153
|
+
| `error.code` | Meaning |
|
|
154
|
+
|---|---|
|
|
155
|
+
| `TpmUnavailable` | The fail-closed TPM hybrid profile has no usable TPM |
|
|
156
|
+
| `TpmRandomFailure` | The TPM was reached but returned no random bytes |
|
|
157
|
+
| `OsRandomFailure` | The operating system CSPRNG failed |
|
|
158
|
+
| `TimestampOutOfRange` | A timestamp does not fit the requested field |
|
|
159
|
+
| `InvalidInput` | Invalid count, format, length, or encoding |
|
|
160
|
+
| `CapacityOverflow` | A batch cannot be represented in memory |
|
|
161
|
+
| `WorkerFailure` | A libuv worker thread panicked during an async batch |
|
|
162
|
+
|
|
163
|
+
`generate()` / `generateV4()` and the TPM batch APIs throw `TpmUnavailable`
|
|
164
|
+
rather than falling back to OS randomness. The `no-tpm` entry point intentionally
|
|
165
|
+
uses the OS-only profile, so it does not produce that error.
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
`generateBuffer()` avoids the JS string but still allocates a Node Buffer per
|
|
169
|
+
call; for throughput use `generateManyEncoded()` or
|
|
170
|
+
`generateManyEncodedAsync()`, which allocate one contiguous buffer.
|
|
171
|
+
|
|
172
|
+
## RailID v4 (126-bit)
|
|
173
|
+
|
|
174
|
+
v4 is a distinct 126-bit format that encodes to exactly 21 characters of
|
|
175
|
+
unpadded Base64URL. It has single-value, Buffer, and contiguous batch APIs:
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
const railid = require('@neko-makura/railid');
|
|
179
|
+
|
|
180
|
+
// TPM hybrid profile; throws TpmUnavailable instead of falling back.
|
|
181
|
+
const id = railid.generateV4();
|
|
182
|
+
console.log(id); // 21 characters
|
|
183
|
+
|
|
184
|
+
// Explicit degraded OS-only profile.
|
|
185
|
+
const osId = railid.generateV4OsOnly();
|
|
186
|
+
|
|
187
|
+
// The Buffer contains the 21 encoded ASCII bytes, not the raw 16-byte value.
|
|
188
|
+
const encoded = railid.generateV4BufferOsOnly();
|
|
189
|
+
|
|
190
|
+
// One contiguous Buffer with no separators: count * 21 bytes.
|
|
191
|
+
const batch = railid.generateV4ManyEncodedOsOnly(1000);
|
|
192
|
+
for (let i = 0; i < 1000; i++) {
|
|
193
|
+
const value = batch.toString('ascii', i * 21, (i + 1) * 21);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Off the JS thread: resolves to the same contiguous Buffer.
|
|
197
|
+
const asyncBatch = await railid.generateV4ManyEncodedOsOnlyAsync(100_000);
|
|
198
|
+
|
|
199
|
+
// Caller-owned buffer: no 21 MB allocation per call. The length must be an
|
|
200
|
+
// exact multiple of 21 and determines the count.
|
|
201
|
+
const target = Buffer.allocUnsafe(100_000 * 21);
|
|
202
|
+
railid.generateV4ManyEncodedOsOnlyInto(target);
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
- `generateV4()` and `generateV4ManyEncoded()` use the TPM hybrid profile,
|
|
206
|
+
which is fail-closed: if no TPM or TSS transport is available they throw
|
|
207
|
+
`TpmUnavailable` and never silently use the OS CSPRNG.
|
|
208
|
+
- `generateV4OsOnly()`, `generateV4BufferOsOnly()`,
|
|
209
|
+
`generateV4ManyEncodedOsOnly()`, and the corresponding `*Async`/`*Into`
|
|
210
|
+
functions are the explicit OS-only opt-ins. The `no-tpm` entry point maps
|
|
211
|
+
the unsuffixed `generateV4`, `generateV4Buffer`, `generateV4ManyEncoded`,
|
|
212
|
+
`generateV4ManyEncodedAsync`, and `generateV4ManyEncodedInto` to them.
|
|
213
|
+
- A v4 batch samples the UTC day once, so every ID in one call carries the
|
|
214
|
+
same day. `count == 0` returns an empty Buffer (or writes nothing).
|
|
215
|
+
- v4 batch limits match v1: 1,024 IDs for the TPM profile and 1,000,000 IDs
|
|
216
|
+
for OS-only. Counts above the limit are rejected before allocation and
|
|
217
|
+
before a TPM is touched.
|
|
218
|
+
- `generateV4ManyEncodedAsync()` and `generateV4ManyEncodedOsOnlyAsync()` run
|
|
219
|
+
the same batch on a libuv worker thread and resolve to one Buffer. Worker
|
|
220
|
+
panics are converted to an error instead of unwinding across FFI.
|
|
221
|
+
- `generateV4ManyEncodedInto(buffer)` and
|
|
222
|
+
`generateV4ManyEncodedOsOnlyInto(buffer)` write into a Buffer the caller
|
|
223
|
+
owns. The length must be an exact multiple of 21; it is validated before
|
|
224
|
+
writing, so a misaligned or over-limit buffer is never partially filled.
|
|
225
|
+
These functions are the recommended high-throughput path: a 1,000,000-ID
|
|
226
|
+
batch avoids a 21 MB external allocation and the associated GC churn.
|
|
227
|
+
- Batch Buffers returned by the allocating APIs are registered with V8's
|
|
228
|
+
external-memory accounting, so repeated batches trigger GC instead of
|
|
229
|
+
growing RSS silently. `*Into` avoids that accounting entirely because the
|
|
230
|
+
caller owns the storage.
|
|
231
|
+
- Errors from v4 APIs carry the stable `code` values documented under
|
|
232
|
+
[Errors](#errors), including `TpmUnavailable` and `InvalidInput`.
|
|
233
|
+
- The v1 192-bit API above is unchanged: `generate()` and `generateOsOnly()`
|
|
234
|
+
still return 32-character v1 IDs. v4 does **not** reuse those names.
|
|
235
|
+
|
|
236
|
+
## RailID v2
|
|
237
|
+
|
|
238
|
+
v2 is a separate format generation for variable-length values with an optional
|
|
239
|
+
timestamp. The v1 functions above are unchanged.
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
const railid = require('@neko-makura/railid');
|
|
243
|
+
|
|
244
|
+
// Preset: 192 bits, 44-bit 100 ms timestamp.
|
|
245
|
+
const id = railid.generateV2({ bits: 192 });
|
|
246
|
+
const info = railid.decodeV2(id, { bits: 192 });
|
|
247
|
+
// { encoded, bits, timestampBits, randomBits, ticks, timestampMs, timestampUs, randomHex }
|
|
248
|
+
|
|
249
|
+
// No timestamp (a custom format needs force).
|
|
250
|
+
const raw = railid.generateV2({ bits: 128, time: null, force: true });
|
|
251
|
+
|
|
252
|
+
// Decoupled tick and width.
|
|
253
|
+
const custom = railid.formatV2({
|
|
254
|
+
bits: 192,
|
|
255
|
+
time: { tick: '1ms', bits: 42 },
|
|
256
|
+
force: true,
|
|
257
|
+
});
|
|
258
|
+
// custom.warnings: NON_DEFAULT_TIME, DECOUPLED_TIME, SHORT_RANGE
|
|
259
|
+
|
|
260
|
+
// Batches use one format. The buffer has count * encodedLen bytes.
|
|
261
|
+
const ids = railid.generateV2Many({ bits: 192 }, 1000); // string[]
|
|
262
|
+
const buffer = railid.generateV2ManyEncoded({ bits: 192 }, 1000); // 32,000 bytes
|
|
263
|
+
|
|
264
|
+
// Async batches resolve with the same shapes after running off the JS thread.
|
|
265
|
+
const asyncIds = await railid.generateV2ManyAsync({ bits: 192 }, 1000);
|
|
266
|
+
const asyncBuffer = await railid.generateV2ManyEncodedAsync({ bits: 192 }, 1000);
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
| Preset | Bits | Timestamp | Random | Characters |
|
|
270
|
+
|---|---:|---|---:|---:|
|
|
271
|
+
| `B32` | 32 | none | 32 | 6 |
|
|
272
|
+
| `B64` | 64 | none | 64 | 11 |
|
|
273
|
+
| `B126` | 126 | none | 126 | 21 |
|
|
274
|
+
| `B192` (default) | 192 | 100 ms / 44 bit | 148 | 32 |
|
|
275
|
+
| `B256` | 256 | 10 ms / 48 bit | 208 | 43 |
|
|
276
|
+
| `B512` | 512 | 1 ms / 52 bit | 460 | 86 |
|
|
277
|
+
|
|
278
|
+
- `time` omitted selects the default for `bits`; `time: null` disables the
|
|
279
|
+
timestamp; a profile name such as `'1s'` selects the conventional pair. An
|
|
280
|
+
object `time` requires both `tick` and `bits`.
|
|
281
|
+
- `force: true` allows non-preset lengths, timestamps on short lengths, no
|
|
282
|
+
timestamp on long lengths, and decoupled tick/width pairs.
|
|
283
|
+
`generateV2`/`generateV2Buffer` emit the warnings on every call through
|
|
284
|
+
`process.emitWarning`; `formatV2` returns them in `warnings`. Batch calls
|
|
285
|
+
emit the format warnings once per call.
|
|
286
|
+
- The v2 epoch is `2020-01-01T00:00:00Z`. Timestamps are returned as decimal
|
|
287
|
+
strings to preserve 64-bit ticks and 128-bit microsecond values.
|
|
288
|
+
- `generateV2Many` and `generateV2ManyEncoded` reject counts above 1,024 (TPM)
|
|
289
|
+
or 1,000,000 (OS-only) before allocating. The async variants
|
|
290
|
+
(`generateV2ManyAsync`, `generateV2ManyEncodedAsync`) apply the same caps
|
|
291
|
+
synchronously, before scheduling work on the libuv thread pool; only the
|
|
292
|
+
generation itself is asynchronous. Errors use the stable codes in
|
|
293
|
+
[Errors](#errors); invalid formats and lengths report `InvalidInput`.
|
|
294
|
+
- The async v2 batches keep the TPM profile fail-closed: without a usable TPM
|
|
295
|
+
they reject with `TpmUnavailable` instead of falling back to OS randomness.
|
|
296
|
+
- v2 is not wire-compatible with v1. See `docs/SPEC-v2.md` in the repository.
|
|
297
|
+
- The `no-tpm` entry point exposes `generateV2`, `generateV2Buffer`,
|
|
298
|
+
`generateV2Many`, `generateV2ManyEncoded`, `generateV2ManyAsync`, and
|
|
299
|
+
`generateV2ManyEncodedAsync` with the OS-only profile.
|
|
300
|
+
Batch encoded buffers use `count * formatV2(format).encodedLen` bytes.
|
|
301
|
+
|
|
302
|
+
## Caller-provided buffer APIs
|
|
303
|
+
|
|
304
|
+
The shipped batch form is `generateV4ManyEncodedInto(buffer)` (and its
|
|
305
|
+
`OsOnly` counterpart): the caller owns the storage, the buffer length must be a
|
|
306
|
+
multiple of 21, and nothing is allocated per call. Prefer it for sustained
|
|
307
|
+
throughput; see [RailID v4](#railid-v4-126-bit).
|
|
308
|
+
|
|
309
|
+
A zero-allocation **single-ID** API with an explicit offset is still not
|
|
310
|
+
shipped. It would look like:
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
generateInto(target: Buffer, offset?: number): number;
|
|
314
|
+
generateOsOnlyInto(target: Buffer, offset?: number): number;
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
- `offset` defaults to 0 and must satisfy `offset + 32 <= target.length`.
|
|
318
|
+
- The function writes 32 ASCII bytes and returns the end offset.
|
|
319
|
+
- It performs no allocation and no JS string creation.
|
|
320
|
+
- It is synchronous only.
|
|
321
|
+
|
|
322
|
+
That single-ID form is **not shipped** because N-API buffers can be shared
|
|
323
|
+
with other JS execution contexts, and napi-rs documents mutable `Buffer` access
|
|
324
|
+
as an aliasing hazard. Shipping it safely needs an exclusive-ownership
|
|
325
|
+
guarantee, a rejection of `SharedArrayBuffer`-backed buffers, or a custom
|
|
326
|
+
writer object. Until then, use the batch APIs.
|
|
327
|
+
|
|
328
|
+
## Linux and TPM
|
|
329
|
+
|
|
330
|
+
The Linux x64 glibc TPM binary links against `libtss2` (`tss-esapi`). The
|
|
331
|
+
package ships two Linux x64 glibc binaries:
|
|
332
|
+
|
|
333
|
+
- `railid-tpm.linux-x64-gnu.node` — TPM backend; needs `libtss2`
|
|
334
|
+
- `railid.linux-x64-gnu.node` — OS-only; no TSS dependency
|
|
335
|
+
|
|
336
|
+
`index.js` tries the TPM binary first and falls back to the OS-only binary when
|
|
337
|
+
the TPM binary cannot be loaded (for example, when `libtss2` is missing). In
|
|
338
|
+
that case `generate()` throws `TpmUnavailable`; `generateOsOnly()` still works.
|
|
339
|
+
This preserves fail-closed behavior.
|
|
340
|
+
|
|
341
|
+
Debian/Ubuntu TPM users need:
|
|
342
|
+
|
|
343
|
+
```bash
|
|
344
|
+
sudo apt install libtss2
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
Alpine, arm64, armv7 and riscv64 ship the OS-only backend only. Those
|
|
348
|
+
platforms resolve a per-platform `@neko-makura/railid-<target>` package, so
|
|
349
|
+
`generate()` throws `TpmUnavailable` and `generateOsOnly()` is the supported
|
|
350
|
+
profile.
|
|
351
|
+
|
|
352
|
+
Windows uses the system TBS service (`Tbs.dll`) and needs no extra DLL. macOS
|
|
353
|
+
ships the OS-only backend.
|
|
354
|
+
|
|
355
|
+
## Build from source
|
|
356
|
+
|
|
357
|
+
Requirements:
|
|
358
|
+
|
|
359
|
+
- Rust 1.88+
|
|
360
|
+
- Linux TPM build: `pkg-config`, `libtss2-dev`
|
|
361
|
+
- Node.js 18+
|
|
362
|
+
|
|
363
|
+
```bash
|
|
364
|
+
npm run build # host default
|
|
365
|
+
npm run build -- --os-only
|
|
366
|
+
npm run build -- --target linux-arm64-musl # cross target, OS-only
|
|
367
|
+
npm run verify # check every .node binary against its target tag
|
|
368
|
+
npm test
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
`node scripts/build.js --list` prints the supported targets. On Linux the
|
|
372
|
+
default build produces both TPM and OS-only binaries for x64; other targets
|
|
373
|
+
produce the OS-only binary. On Windows it produces the TPM binary. On macOS it
|
|
374
|
+
produces both OS-only slices.
|
|
375
|
+
|
|
376
|
+
Cross-compiling without a matching host is easiest in a container. The helper
|
|
377
|
+
installs the toolchain for the requested target:
|
|
378
|
+
|
|
379
|
+
```bash
|
|
380
|
+
docker run --rm -v "$PWD:/src" -w /src/node rust:1.88-alpine \
|
|
381
|
+
sh scripts/build-in-docker.sh linux-arm64-musl
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
## Benchmarks
|
|
385
|
+
|
|
386
|
+
The Node addon benchmark against UUIDv4 and NanoID is in
|
|
387
|
+
[`bench/`](bench/README.md). It includes the latest Windows and Raspberry Pi 4
|
|
388
|
+
results.
|
|
389
|
+
|
|
390
|
+
## Security notes
|
|
391
|
+
|
|
392
|
+
RailID is an identifier generator. This package does not provide single-use
|
|
393
|
+
tokens, expiry, or revocation; enforce those in your application. A RailID
|
|
394
|
+
discloses its creation time at 100 ms precision. TPM presence does not prove
|
|
395
|
+
the firmware or boot chain is trustworthy.
|
|
396
|
+
|
|
397
|
+
TPM access is serialized inside the process. Use the async batch APIs for
|
|
398
|
+
server workloads so the event loop is not blocked by large batches.
|
|
399
|
+
|
|
400
|
+
## License
|
|
401
|
+
|
|
402
|
+
MIT. Copyright (c) 2026 Nekomakura. See [LICENSE](LICENSE).
|