@neko-makura/railid 4.2.1 → 4.3.1
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 +77 -14
- package/errors.js +152 -0
- package/index.d.ts +69 -3
- package/index.js +98 -12
- package/no-tpm.d.ts +27 -3
- package/no-tpm.js +2 -0
- package/package.json +2 -1
- package/railid-tpm.linux-x64-gnu.node +0 -0
- package/railid.darwin-arm64.node +0 -0
- package/railid.darwin-x64.node +0 -0
- package/railid.linux-arm-gnu.node +0 -0
- package/railid.linux-arm64-gnu.node +0 -0
- package/railid.linux-arm64-musl.node +0 -0
- package/railid.linux-riscv64-gnu.node +0 -0
- package/railid.linux-x64-gnu.node +0 -0
- package/railid.linux-x64-musl.node +0 -0
- package/railid.win32-arm64-msvc.node +0 -0
- package/railid.win32-x64-msvc.node +0 -0
- package/v2.js +8 -16
package/README.md
CHANGED
|
@@ -97,6 +97,10 @@ still never falls back implicitly.
|
|
|
97
97
|
| `generateV4BufferOsOnly()` | `Buffer` | 126-bit v4; OS-only as 21 ASCII bytes |
|
|
98
98
|
| `generateV4ManyEncoded(count)` | `Buffer` | 126-bit v4; TPM profile; `count * 21` bytes |
|
|
99
99
|
| `generateV4ManyEncodedOsOnly(count)` | `Buffer` | 126-bit v4; OS-only; `count * 21` bytes |
|
|
100
|
+
| `generateV4ManyEncodedAsync(count)` | `Promise<Buffer>` | 126-bit v4; TPM profile; off the JS thread |
|
|
101
|
+
| `generateV4ManyEncodedOsOnlyAsync(count)` | `Promise<Buffer>` | 126-bit v4; OS-only; off the JS thread |
|
|
102
|
+
| `generateV4ManyEncodedInto(buffer)` | `void` | 126-bit v4; TPM profile; writes `buffer` (multiple of 21) |
|
|
103
|
+
| `generateV4ManyEncodedOsOnlyInto(buffer)` | `void` | 126-bit v4; OS-only; writes `buffer` |
|
|
100
104
|
| `generateMany(count, osOnly?)` | `string[]` | Sync; `osOnly` defaults to `false` |
|
|
101
105
|
| `generateManyEncoded(count, osOnly?)` | `Buffer` | Sync; `count * 32` bytes |
|
|
102
106
|
| `generateManyAsync(count, osOnly?)` | `Promise<string[]>` | Bulk work off the JS thread |
|
|
@@ -115,7 +119,32 @@ Limits:
|
|
|
115
119
|
- OS-only encoded: 1,000,000 IDs
|
|
116
120
|
- TPM: 1,024 IDs
|
|
117
121
|
|
|
118
|
-
Counts above the limit are rejected before allocation.
|
|
122
|
+
Counts above the limit are rejected before allocation. Every batch `count`
|
|
123
|
+
must be a safe integer in `0..4294967295`; fractional, negative, `NaN`,
|
|
124
|
+
`Infinity`, and out-of-range values throw instead of wrapping. v2 batches use
|
|
125
|
+
the same caps (1,024 TPM / 1,000,000 OS-only), as do the v4 async batches. The
|
|
126
|
+
v4 `*Into` functions derive their count from the buffer length, require an
|
|
127
|
+
exact multiple of 21, and apply the same caps before writing.
|
|
128
|
+
|
|
129
|
+
## Errors
|
|
130
|
+
|
|
131
|
+
Native N-API failures are mapped once at the JavaScript boundary to a stable
|
|
132
|
+
`error.code`, so callers do not need to parse messages:
|
|
133
|
+
|
|
134
|
+
| `error.code` | Meaning |
|
|
135
|
+
|---|---|
|
|
136
|
+
| `TpmUnavailable` | The fail-closed TPM hybrid profile has no usable TPM |
|
|
137
|
+
| `TpmRandomFailure` | The TPM was reached but returned no random bytes |
|
|
138
|
+
| `OsRandomFailure` | The operating system CSPRNG failed |
|
|
139
|
+
| `TimestampOutOfRange` | A timestamp does not fit the requested field |
|
|
140
|
+
| `InvalidInput` | Invalid count, format, length, or encoding |
|
|
141
|
+
| `CapacityOverflow` | A batch cannot be represented in memory |
|
|
142
|
+
| `WorkerFailure` | A libuv worker thread panicked during an async batch |
|
|
143
|
+
|
|
144
|
+
`generate()` / `generateV4()` and the TPM batch APIs throw `TpmUnavailable`
|
|
145
|
+
rather than falling back to OS randomness. The `no-tpm` entry point intentionally
|
|
146
|
+
uses the OS-only profile, so it does not produce that error.
|
|
147
|
+
|
|
119
148
|
|
|
120
149
|
`generateBuffer()` avoids the JS string but still allocates a Node Buffer per
|
|
121
150
|
call; for throughput use `generateManyEncoded()` or
|
|
@@ -144,19 +173,44 @@ const batch = railid.generateV4ManyEncodedOsOnly(1000);
|
|
|
144
173
|
for (let i = 0; i < 1000; i++) {
|
|
145
174
|
const value = batch.toString('ascii', i * 21, (i + 1) * 21);
|
|
146
175
|
}
|
|
176
|
+
|
|
177
|
+
// Off the JS thread: resolves to the same contiguous Buffer.
|
|
178
|
+
const asyncBatch = await railid.generateV4ManyEncodedOsOnlyAsync(100_000);
|
|
179
|
+
|
|
180
|
+
// Caller-owned buffer: no 21 MB allocation per call. The length must be an
|
|
181
|
+
// exact multiple of 21 and determines the count.
|
|
182
|
+
const target = Buffer.allocUnsafe(100_000 * 21);
|
|
183
|
+
railid.generateV4ManyEncodedOsOnlyInto(target);
|
|
147
184
|
```
|
|
148
185
|
|
|
149
186
|
- `generateV4()` and `generateV4ManyEncoded()` use the TPM hybrid profile,
|
|
150
187
|
which is fail-closed: if no TPM or TSS transport is available they throw
|
|
151
188
|
`TpmUnavailable` and never silently use the OS CSPRNG.
|
|
152
|
-
- `generateV4OsOnly()`, `generateV4BufferOsOnly()`,
|
|
153
|
-
`generateV4ManyEncodedOsOnly()
|
|
154
|
-
`no-tpm` entry point maps
|
|
155
|
-
`
|
|
189
|
+
- `generateV4OsOnly()`, `generateV4BufferOsOnly()`,
|
|
190
|
+
`generateV4ManyEncodedOsOnly()`, and the corresponding `*Async`/`*Into`
|
|
191
|
+
functions are the explicit OS-only opt-ins. The `no-tpm` entry point maps
|
|
192
|
+
the unsuffixed `generateV4`, `generateV4Buffer`, `generateV4ManyEncoded`,
|
|
193
|
+
`generateV4ManyEncodedAsync`, and `generateV4ManyEncodedInto` to them.
|
|
156
194
|
- A v4 batch samples the UTC day once, so every ID in one call carries the
|
|
157
|
-
same day. `count == 0` returns an empty Buffer.
|
|
195
|
+
same day. `count == 0` returns an empty Buffer (or writes nothing).
|
|
158
196
|
- v4 batch limits match v1: 1,024 IDs for the TPM profile and 1,000,000 IDs
|
|
159
|
-
for OS-only. Counts above the limit are rejected before allocation
|
|
197
|
+
for OS-only. Counts above the limit are rejected before allocation and
|
|
198
|
+
before a TPM is touched.
|
|
199
|
+
- `generateV4ManyEncodedAsync()` and `generateV4ManyEncodedOsOnlyAsync()` run
|
|
200
|
+
the same batch on a libuv worker thread and resolve to one Buffer. Worker
|
|
201
|
+
panics are converted to an error instead of unwinding across FFI.
|
|
202
|
+
- `generateV4ManyEncodedInto(buffer)` and
|
|
203
|
+
`generateV4ManyEncodedOsOnlyInto(buffer)` write into a Buffer the caller
|
|
204
|
+
owns. The length must be an exact multiple of 21; it is validated before
|
|
205
|
+
writing, so a misaligned or over-limit buffer is never partially filled.
|
|
206
|
+
These functions are the recommended high-throughput path: a 1,000,000-ID
|
|
207
|
+
batch avoids a 21 MB external allocation and the associated GC churn.
|
|
208
|
+
- Batch Buffers returned by the allocating APIs are registered with V8's
|
|
209
|
+
external-memory accounting, so repeated batches trigger GC instead of
|
|
210
|
+
growing RSS silently. `*Into` avoids that accounting entirely because the
|
|
211
|
+
caller owns the storage.
|
|
212
|
+
- Errors from v4 APIs carry the stable `code` values documented under
|
|
213
|
+
[Errors](#errors), including `TpmUnavailable` and `InvalidInput`.
|
|
160
214
|
- The v1 192-bit API above is unchanged: `generate()` and `generateOsOnly()`
|
|
161
215
|
still return 32-character v1 IDs. v4 does **not** reuse those names.
|
|
162
216
|
|
|
@@ -208,14 +262,23 @@ const buffer = railid.generateV2ManyEncoded({ bits: 192 }, 1000); // 32,000 byte
|
|
|
208
262
|
emit the format warnings once per call.
|
|
209
263
|
- The v2 epoch is `2020-01-01T00:00:00Z`. Timestamps are returned as decimal
|
|
210
264
|
strings to preserve 64-bit ticks and 128-bit microsecond values.
|
|
265
|
+
- `generateV2Many` and `generateV2ManyEncoded` reject counts above 1,024 (TPM)
|
|
266
|
+
or 1,000,000 (OS-only) before allocating. Errors use the stable codes in
|
|
267
|
+
[Errors](#errors); invalid formats and lengths report `InvalidInput`.
|
|
211
268
|
- v2 is not wire-compatible with v1. See `docs/SPEC-v2.md` in the repository.
|
|
212
269
|
- The `no-tpm` entry point exposes `generateV2`, `generateV2Buffer`,
|
|
213
270
|
`generateV2Many`, and `generateV2ManyEncoded` with the OS-only profile.
|
|
214
271
|
Batch encoded buffers use `count * formatV2(format).encodedLen` bytes.
|
|
215
272
|
|
|
216
|
-
## Caller-provided buffer
|
|
273
|
+
## Caller-provided buffer APIs
|
|
274
|
+
|
|
275
|
+
The shipped batch form is `generateV4ManyEncodedInto(buffer)` (and its
|
|
276
|
+
`OsOnly` counterpart): the caller owns the storage, the buffer length must be a
|
|
277
|
+
multiple of 21, and nothing is allocated per call. Prefer it for sustained
|
|
278
|
+
throughput; see [RailID v4](#railid-v4-126-bit).
|
|
217
279
|
|
|
218
|
-
A zero-allocation single-ID API
|
|
280
|
+
A zero-allocation **single-ID** API with an explicit offset is still not
|
|
281
|
+
shipped. It would look like:
|
|
219
282
|
|
|
220
283
|
```ts
|
|
221
284
|
generateInto(target: Buffer, offset?: number): number;
|
|
@@ -227,11 +290,11 @@ generateOsOnlyInto(target: Buffer, offset?: number): number;
|
|
|
227
290
|
- It performs no allocation and no JS string creation.
|
|
228
291
|
- It is synchronous only.
|
|
229
292
|
|
|
230
|
-
|
|
231
|
-
execution contexts, and napi-rs documents mutable `Buffer` access
|
|
232
|
-
hazard. Shipping it safely needs an exclusive-ownership
|
|
233
|
-
of `SharedArrayBuffer`-backed buffers, or a custom
|
|
234
|
-
use the batch APIs.
|
|
293
|
+
That single-ID form is **not shipped** because N-API buffers can be shared
|
|
294
|
+
with other JS execution contexts, and napi-rs documents mutable `Buffer` access
|
|
295
|
+
as an aliasing hazard. Shipping it safely needs an exclusive-ownership
|
|
296
|
+
guarantee, a rejection of `SharedArrayBuffer`-backed buffers, or a custom
|
|
297
|
+
writer object. Until then, use the batch APIs.
|
|
235
298
|
|
|
236
299
|
## Linux and TPM
|
|
237
300
|
|
package/errors.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Shared validation and error-code mapping for the public JavaScript surface.
|
|
4
|
+
//
|
|
5
|
+
// N-API only exposes a small set of status strings (`GenericFailure`,
|
|
6
|
+
// `InvalidArg`, ...), so the bindings would otherwise report every failure as
|
|
7
|
+
// `GenericFailure`. The native functions throw our own crates' exact error
|
|
8
|
+
// messages; this module maps them once, at the boundary, to stable codes that
|
|
9
|
+
// callers can switch on.
|
|
10
|
+
|
|
11
|
+
const MAX_COUNT = 0xffffffff;
|
|
12
|
+
const TPM_BATCH_LIMIT = 1_024;
|
|
13
|
+
const OS_ONLY_BATCH_LIMIT = 1_000_000;
|
|
14
|
+
const OS_ONLY_STRING_BATCH_LIMIT = 100_000;
|
|
15
|
+
|
|
16
|
+
const ERROR_CODES = Object.freeze({
|
|
17
|
+
TpmUnavailable: 'TpmUnavailable',
|
|
18
|
+
TpmRandomFailure: 'TpmRandomFailure',
|
|
19
|
+
OsRandomFailure: 'OsRandomFailure',
|
|
20
|
+
TimestampOutOfRange: 'TimestampOutOfRange',
|
|
21
|
+
InvalidInput: 'InvalidInput',
|
|
22
|
+
CapacityOverflow: 'CapacityOverflow',
|
|
23
|
+
WorkerFailure: 'WorkerFailure',
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Ordered: the first matching rule wins. Capacity and timestamp rules must run
|
|
27
|
+
// before the generic invalid-input rule.
|
|
28
|
+
const RULES = [
|
|
29
|
+
[/TPM 2\.0 is unavailable/i, ERROR_CODES.TpmUnavailable],
|
|
30
|
+
[/TPM 2\.0 random generation failed/i, ERROR_CODES.TpmRandomFailure],
|
|
31
|
+
[/operating system CSPRNG failed/i, ERROR_CODES.OsRandomFailure],
|
|
32
|
+
[/worker (?:thread )?panicked/i, ERROR_CODES.WorkerFailure],
|
|
33
|
+
[/too large|capacity overflow|allocation failed/i, ERROR_CODES.CapacityOverflow],
|
|
34
|
+
[
|
|
35
|
+
/timestamp (?:is|does|arithmetic)|outside the 24-bit day range/i,
|
|
36
|
+
ERROR_CODES.TimestampOutOfRange,
|
|
37
|
+
],
|
|
38
|
+
[
|
|
39
|
+
/invalid|must be|required|out of range|non-canonical|non-zero|padding|exceeds binding limit|buffer length|length does not match|unknown tick|unknown preset/i,
|
|
40
|
+
ERROR_CODES.InvalidInput,
|
|
41
|
+
],
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
function inputError(message) {
|
|
45
|
+
const error = new TypeError(message);
|
|
46
|
+
error.code = ERROR_CODES.InvalidInput;
|
|
47
|
+
return error;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function classify(message) {
|
|
51
|
+
for (const [pattern, code] of RULES) {
|
|
52
|
+
if (pattern.test(message)) {
|
|
53
|
+
return code;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function decorate(error) {
|
|
60
|
+
if (error === null || typeof error !== 'object') {
|
|
61
|
+
return error;
|
|
62
|
+
}
|
|
63
|
+
// JS validation errors already carry a stable code. Native errors arrive as
|
|
64
|
+
// `GenericFailure` (or `InvalidArg` if a native check used that status); map
|
|
65
|
+
// those to the documented public code.
|
|
66
|
+
const current = error.code;
|
|
67
|
+
if (
|
|
68
|
+
typeof current === 'string' &&
|
|
69
|
+
current !== 'GenericFailure' &&
|
|
70
|
+
current !== 'InvalidArg'
|
|
71
|
+
) {
|
|
72
|
+
return error;
|
|
73
|
+
}
|
|
74
|
+
const message = typeof error.message === 'string' ? error.message : String(error);
|
|
75
|
+
const code = classify(message);
|
|
76
|
+
if (code === null) {
|
|
77
|
+
return error;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
error.code = code;
|
|
81
|
+
} catch {
|
|
82
|
+
try {
|
|
83
|
+
Object.defineProperty(error, 'code', {
|
|
84
|
+
value: code,
|
|
85
|
+
enumerable: true,
|
|
86
|
+
configurable: true,
|
|
87
|
+
writable: true,
|
|
88
|
+
});
|
|
89
|
+
} catch {
|
|
90
|
+
// A frozen/native error object keeps its original code; the message is
|
|
91
|
+
// still available. This should not happen for N-API errors.
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return error;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Wrap a native/public function so synchronous throws and rejected promises
|
|
98
|
+
// carry the documented `code` property.
|
|
99
|
+
function wrap(fn) {
|
|
100
|
+
return function wrapped(...args) {
|
|
101
|
+
let result;
|
|
102
|
+
try {
|
|
103
|
+
result = fn.apply(this, args);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw decorate(error);
|
|
106
|
+
}
|
|
107
|
+
if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
|
|
108
|
+
return result.catch((error) => {
|
|
109
|
+
throw decorate(error);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function checkCount(count, limit, name = 'count') {
|
|
117
|
+
if (
|
|
118
|
+
typeof count !== 'number' ||
|
|
119
|
+
!Number.isSafeInteger(count) ||
|
|
120
|
+
count < 0 ||
|
|
121
|
+
count > MAX_COUNT
|
|
122
|
+
) {
|
|
123
|
+
throw inputError(`${name} must be an integer between 0 and 4294967295`);
|
|
124
|
+
}
|
|
125
|
+
if (count > limit) {
|
|
126
|
+
throw inputError(`${name} ${count} exceeds binding limit ${limit}`);
|
|
127
|
+
}
|
|
128
|
+
return count;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function batchLimit(osOnly) {
|
|
132
|
+
return osOnly ? OS_ONLY_BATCH_LIMIT : TPM_BATCH_LIMIT;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function stringBatchLimit(osOnly) {
|
|
136
|
+
return osOnly ? OS_ONLY_STRING_BATCH_LIMIT : TPM_BATCH_LIMIT;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = {
|
|
140
|
+
ERROR_CODES,
|
|
141
|
+
MAX_COUNT,
|
|
142
|
+
TPM_BATCH_LIMIT,
|
|
143
|
+
OS_ONLY_BATCH_LIMIT,
|
|
144
|
+
OS_ONLY_STRING_BATCH_LIMIT,
|
|
145
|
+
inputError,
|
|
146
|
+
classify,
|
|
147
|
+
decorate,
|
|
148
|
+
wrap,
|
|
149
|
+
checkCount,
|
|
150
|
+
batchLimit,
|
|
151
|
+
stringBatchLimit,
|
|
152
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
export type Backend = "tpm" | "os-only";
|
|
2
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
|
+
|
|
3
25
|
export interface DecodedRailId {
|
|
4
26
|
/** Unix timestamp in milliseconds. */
|
|
5
27
|
timestampMs: number;
|
|
@@ -39,16 +61,60 @@ export declare function generateV4BufferOsOnly(): Buffer;
|
|
|
39
61
|
/**
|
|
40
62
|
* Generate `count` v4 IDs as one contiguous `count * 21` Buffer with the TPM
|
|
41
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.
|
|
42
70
|
*/
|
|
43
71
|
export declare function generateV4ManyEncoded(count: number): Buffer;
|
|
44
72
|
|
|
45
73
|
/**
|
|
46
74
|
* Generate `count` v4 IDs as one contiguous `count * 21` Buffer with the
|
|
47
|
-
* explicitly degraded OS-only profile. Capped at 1,000,000 IDs.
|
|
75
|
+
* explicitly degraded OS-only profile. Capped at 1,000,000 IDs. Invalid
|
|
76
|
+
* counts throw `InvalidInput`.
|
|
48
77
|
*/
|
|
49
78
|
export declare function generateV4ManyEncodedOsOnly(count: number): Buffer;
|
|
50
79
|
|
|
51
|
-
/**
|
|
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. */
|
|
52
118
|
export declare function generateMany(count: number, osOnly?: boolean): string[];
|
|
53
119
|
|
|
54
120
|
/** Generate `count` IDs synchronously as one contiguous `count * 32` Buffer. */
|
|
@@ -121,7 +187,7 @@ export declare function generateV2(format: V2Format): string;
|
|
|
121
187
|
/** Generate one v2 value as 3-171 ASCII bytes. */
|
|
122
188
|
export declare function generateV2Buffer(format: V2Format): Buffer;
|
|
123
189
|
|
|
124
|
-
/** Generate `count` v2 values as a string array. */
|
|
190
|
+
/** Generate `count` v2 values as a string array. Capped at 1,024 (TPM) or 1,000,000 (OS-only). */
|
|
125
191
|
export declare function generateV2Many(format: V2Format, count: number): string[];
|
|
126
192
|
|
|
127
193
|
/** Generate `count` v2 values as one contiguous `count * encodedLen` Buffer. */
|
package/index.js
CHANGED
|
@@ -65,6 +65,14 @@ if (!binding) {
|
|
|
65
65
|
throw error;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
const {
|
|
69
|
+
checkCount,
|
|
70
|
+
batchLimit,
|
|
71
|
+
stringBatchLimit,
|
|
72
|
+
inputError,
|
|
73
|
+
wrap,
|
|
74
|
+
} = require('./errors.js');
|
|
75
|
+
|
|
68
76
|
const v2 = require('./v2.js')(binding);
|
|
69
77
|
const publicBinding = { ...binding };
|
|
70
78
|
|
|
@@ -81,34 +89,112 @@ for (const key of Object.keys(publicBinding)) {
|
|
|
81
89
|
key === 'v2GenerateMany' ||
|
|
82
90
|
key === 'v2GenerateManyEncoded' ||
|
|
83
91
|
key === 'v2GenerateManyEncodedInto' ||
|
|
84
|
-
key === 'generateManyEncodedInto'
|
|
92
|
+
key === 'generateManyEncodedInto' ||
|
|
93
|
+
key === 'generateV4ManyEncodedInto' ||
|
|
94
|
+
key === 'generateV4ManyEncodedOsOnlyInto'
|
|
85
95
|
) {
|
|
86
96
|
delete publicBinding[key];
|
|
87
97
|
}
|
|
88
98
|
}
|
|
89
99
|
|
|
90
100
|
const V1_ENCODED_LEN = 32;
|
|
91
|
-
const
|
|
92
|
-
|
|
101
|
+
const V4_ENCODED_LEN = 21;
|
|
102
|
+
|
|
103
|
+
function generateMany(count, osOnly) {
|
|
104
|
+
const degraded = Boolean(osOnly);
|
|
105
|
+
checkCount(count, stringBatchLimit(degraded));
|
|
106
|
+
return binding.generateMany(count, degraded);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function generateManyAsync(count, osOnly) {
|
|
110
|
+
const degraded = Boolean(osOnly);
|
|
111
|
+
checkCount(count, stringBatchLimit(degraded));
|
|
112
|
+
return binding.generateManyAsync(count, degraded);
|
|
113
|
+
}
|
|
93
114
|
|
|
94
115
|
function generateManyEncoded(count, osOnly) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
const limit = osOnly ? V1_OS_LIMIT : V1_TPM_LIMIT;
|
|
99
|
-
if (count > limit) {
|
|
100
|
-
throw new Error(`count ${count} exceeds binding limit ${limit}`);
|
|
101
|
-
}
|
|
116
|
+
const degraded = Boolean(osOnly);
|
|
117
|
+
checkCount(count, batchLimit(degraded));
|
|
102
118
|
const buffer = Buffer.allocUnsafe(count * V1_ENCODED_LEN);
|
|
103
|
-
binding.generateManyEncodedInto(buffer,
|
|
119
|
+
binding.generateManyEncodedInto(buffer, degraded);
|
|
104
120
|
return buffer;
|
|
105
121
|
}
|
|
106
122
|
|
|
107
|
-
|
|
123
|
+
function generateManyEncodedAsync(count, osOnly) {
|
|
124
|
+
const degraded = Boolean(osOnly);
|
|
125
|
+
checkCount(count, batchLimit(degraded));
|
|
126
|
+
return binding.generateManyEncodedAsync(count, degraded);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function generateV4ManyEncoded(count) {
|
|
130
|
+
checkCount(count, batchLimit(false));
|
|
131
|
+
return binding.generateV4ManyEncoded(count);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function generateV4ManyEncodedOsOnly(count) {
|
|
135
|
+
checkCount(count, batchLimit(true));
|
|
136
|
+
return binding.generateV4ManyEncodedOsOnly(count);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function generateV4ManyEncodedAsync(count) {
|
|
140
|
+
checkCount(count, batchLimit(false));
|
|
141
|
+
return binding.generateV4ManyEncodedAsync(count);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function generateV4ManyEncodedOsOnlyAsync(count) {
|
|
145
|
+
checkCount(count, batchLimit(true));
|
|
146
|
+
return binding.generateV4ManyEncodedOsOnlyAsync(count);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// A v4 target buffer is exactly `count * 21` bytes, so its length determines
|
|
150
|
+
// the count. Validate before the native call so an over-limit buffer never
|
|
151
|
+
// reaches a TPM operation and a misaligned buffer never gets partially
|
|
152
|
+
// written.
|
|
153
|
+
function checkV4IntoBuffer(buffer, limit) {
|
|
154
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
155
|
+
throw inputError('buffer must be a Buffer');
|
|
156
|
+
}
|
|
157
|
+
if (buffer.length % V4_ENCODED_LEN !== 0) {
|
|
158
|
+
throw inputError('buffer length must be a multiple of 21');
|
|
159
|
+
}
|
|
160
|
+
const count = buffer.length / V4_ENCODED_LEN;
|
|
161
|
+
checkCount(count, limit, 'v4 into count');
|
|
162
|
+
return count;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function generateV4ManyEncodedInto(buffer) {
|
|
166
|
+
checkV4IntoBuffer(buffer, batchLimit(false));
|
|
167
|
+
return binding.generateV4ManyEncodedInto(buffer);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function generateV4ManyEncodedOsOnlyInto(buffer) {
|
|
171
|
+
checkV4IntoBuffer(buffer, batchLimit(true));
|
|
172
|
+
return binding.generateV4ManyEncodedOsOnlyInto(buffer);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const api = {
|
|
108
176
|
...publicBinding,
|
|
177
|
+
generateMany,
|
|
178
|
+
generateManyAsync,
|
|
109
179
|
generateManyEncoded,
|
|
180
|
+
generateManyEncodedAsync,
|
|
181
|
+
generateV4ManyEncoded,
|
|
182
|
+
generateV4ManyEncodedOsOnly,
|
|
183
|
+
generateV4ManyEncodedAsync,
|
|
184
|
+
generateV4ManyEncodedOsOnlyAsync,
|
|
185
|
+
generateV4ManyEncodedInto,
|
|
186
|
+
generateV4ManyEncodedOsOnlyInto,
|
|
110
187
|
// `backend` is the backend compiled into the loaded binary. A TPM build can
|
|
111
188
|
// still return TpmUnavailable at runtime when no TPM device is present.
|
|
112
189
|
backend: () => backend,
|
|
113
190
|
...v2,
|
|
114
191
|
};
|
|
192
|
+
|
|
193
|
+
// Map native errors to stable `code` values once, at the public boundary.
|
|
194
|
+
for (const [key, value] of Object.entries(api)) {
|
|
195
|
+
if (typeof value === 'function') {
|
|
196
|
+
api[key] = wrap(value);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
module.exports = api;
|
package/no-tpm.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { DecodedRailId } from "./index";
|
|
1
|
+
import type { DecodedRailId, RailIdError, RailIdErrorCode } from "./index";
|
|
2
2
|
|
|
3
|
-
export type { DecodedRailId };
|
|
3
|
+
export type { DecodedRailId, RailIdError, RailIdErrorCode };
|
|
4
4
|
export type Backend = "os-only";
|
|
5
5
|
|
|
6
6
|
/** Generate one RailID with the explicitly degraded OS-only profile. */
|
|
@@ -36,6 +36,30 @@ export declare function generateV4ManyEncoded(count: number): Buffer;
|
|
|
36
36
|
/** Same as `generateV4ManyEncoded()`; kept for symmetry. */
|
|
37
37
|
export declare function generateV4ManyEncodedOsOnly(count: number): Buffer;
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Generate `count` v4 IDs asynchronously with the OS-only profile. Resolves to
|
|
41
|
+
* a `count * 21` Buffer after running on the libuv thread pool. Capped at
|
|
42
|
+
* 1,000,000 IDs; invalid counts throw `InvalidInput` synchronously.
|
|
43
|
+
*/
|
|
44
|
+
export declare function generateV4ManyEncodedAsync(count: number): Promise<Buffer>;
|
|
45
|
+
|
|
46
|
+
/** Same as `generateV4ManyEncodedAsync()`; kept for symmetry. */
|
|
47
|
+
export declare function generateV4ManyEncodedOsOnlyAsync(
|
|
48
|
+
count: number,
|
|
49
|
+
): Promise<Buffer>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Generate v4 IDs with the OS-only profile into a caller-provided Buffer. The
|
|
53
|
+
* buffer length must be an exact multiple of 21 and determines the count;
|
|
54
|
+
* capped at 1,000,000 IDs. Nothing is returned. Invalid lengths throw
|
|
55
|
+
* `InvalidInput` before any bytes are written. This avoids the `count * 21`
|
|
56
|
+
* external allocation and is the recommended high-throughput path.
|
|
57
|
+
*/
|
|
58
|
+
export declare function generateV4ManyEncodedInto(buffer: Buffer): void;
|
|
59
|
+
|
|
60
|
+
/** Same as `generateV4ManyEncodedInto()`; kept for symmetry. */
|
|
61
|
+
export declare function generateV4ManyEncodedOsOnlyInto(buffer: Buffer): void;
|
|
62
|
+
|
|
39
63
|
/** Generate `count` IDs synchronously as strings. */
|
|
40
64
|
export declare function generateMany(count: number): string[];
|
|
41
65
|
|
|
@@ -64,7 +88,7 @@ export type { V2Decoded, V2Format, V2FormatInfo };
|
|
|
64
88
|
/** Generate one OS-only v2 value. */
|
|
65
89
|
export declare function generateV2(format: V2Format): string;
|
|
66
90
|
|
|
67
|
-
/** Generate one OS-only v2 value as
|
|
91
|
+
/** Generate one OS-only v2 value as 3-171 ASCII bytes. */
|
|
68
92
|
export declare function generateV2Buffer(format: V2Format): Buffer;
|
|
69
93
|
|
|
70
94
|
/** Generate `count` OS-only v2 values as a string array. */
|
package/no-tpm.js
CHANGED
|
@@ -17,6 +17,8 @@ module.exports = {
|
|
|
17
17
|
generateV4: core.generateV4OsOnly,
|
|
18
18
|
generateV4Buffer: core.generateV4BufferOsOnly,
|
|
19
19
|
generateV4ManyEncoded: (count) => core.generateV4ManyEncodedOsOnly(count),
|
|
20
|
+
generateV4ManyEncodedAsync: (count) => core.generateV4ManyEncodedOsOnlyAsync(count),
|
|
21
|
+
generateV4ManyEncodedInto: (buffer) => core.generateV4ManyEncodedOsOnlyInto(buffer),
|
|
20
22
|
// The `osOnly` argument is accepted for signature compatibility and always
|
|
21
23
|
// resolves to the OS-only profile.
|
|
22
24
|
generateMany: (count) => core.generateMany(count, true),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neko-makura/railid",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.1",
|
|
4
4
|
"description": "Native N-API bindings for RailID: 192-bit v1 and variable-length v2 timestamp-first identifiers with TPM 2.0 + OS CSPRNG randomness",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"files": [
|
|
29
29
|
"index.js",
|
|
30
30
|
"index.d.ts",
|
|
31
|
+
"errors.js",
|
|
31
32
|
"no-tpm.js",
|
|
32
33
|
"no-tpm.d.ts",
|
|
33
34
|
"v2.js",
|
|
Binary file
|
package/railid.darwin-arm64.node
CHANGED
|
Binary file
|
package/railid.darwin-x64.node
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/v2.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { checkCount, batchLimit } = require('./errors.js');
|
|
4
|
+
|
|
3
5
|
const TICKS = ['1d', '1h', '1s', '100ms', '10ms', '1ms', '0.1ms'];
|
|
4
6
|
|
|
5
7
|
function tickIndex(name) {
|
|
@@ -182,33 +184,23 @@ module.exports = function createV2(binding) {
|
|
|
182
184
|
return binding.v2GenerateBufferPacked(entry.key | (profile === 'os-only' ? 1 << 24 : 0));
|
|
183
185
|
}
|
|
184
186
|
|
|
185
|
-
function batchCount(count) {
|
|
186
|
-
if (!Number.isInteger(count) || count < 0 || count > 0xFFFFFFFF) {
|
|
187
|
-
throw new TypeError('count must be an integer between 0 and 4294967295');
|
|
188
|
-
}
|
|
189
|
-
return count;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
187
|
function generateV2Many(format, count, profile) {
|
|
193
|
-
|
|
188
|
+
const degraded = profile === 'os-only';
|
|
189
|
+
checkCount(count, batchLimit(degraded));
|
|
194
190
|
const args = toNativeArguments(format);
|
|
195
191
|
const entry = cachedInfo(args);
|
|
196
192
|
emitWarnings(entry.commands);
|
|
197
|
-
return binding.v2GenerateMany(...args, count,
|
|
193
|
+
return binding.v2GenerateMany(...args, count, degraded);
|
|
198
194
|
}
|
|
199
195
|
|
|
200
196
|
function generateV2ManyEncoded(format, count, profile) {
|
|
201
|
-
|
|
197
|
+
const degraded = profile === 'os-only';
|
|
198
|
+
checkCount(count, batchLimit(degraded));
|
|
202
199
|
const args = toNativeArguments(format);
|
|
203
200
|
const entry = cachedInfo(args);
|
|
204
201
|
emitWarnings(entry.commands);
|
|
205
202
|
const buffer = Buffer.allocUnsafe(count * entry.info.encodedLen);
|
|
206
|
-
binding.v2GenerateManyEncodedInto(
|
|
207
|
-
...args,
|
|
208
|
-
count,
|
|
209
|
-
profile === 'os-only',
|
|
210
|
-
buffer
|
|
211
|
-
);
|
|
203
|
+
binding.v2GenerateManyEncodedInto(...args, count, degraded, buffer);
|
|
212
204
|
return buffer;
|
|
213
205
|
}
|
|
214
206
|
|