@neko-makura/railid 4.2.2 → 4.3.2

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 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 |
@@ -118,7 +122,9 @@ Limits:
118
122
  Counts above the limit are rejected before allocation. Every batch `count`
119
123
  must be a safe integer in `0..4294967295`; fractional, negative, `NaN`,
120
124
  `Infinity`, and out-of-range values throw instead of wrapping. v2 batches use
121
- the same caps (1,024 TPM / 1,000,000 OS-only).
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.
122
128
 
123
129
  ## Errors
124
130
 
@@ -133,6 +139,7 @@ Native N-API failures are mapped once at the JavaScript boundary to a stable
133
139
  | `TimestampOutOfRange` | A timestamp does not fit the requested field |
134
140
  | `InvalidInput` | Invalid count, format, length, or encoding |
135
141
  | `CapacityOverflow` | A batch cannot be represented in memory |
142
+ | `WorkerFailure` | A libuv worker thread panicked during an async batch |
136
143
 
137
144
  `generate()` / `generateV4()` and the TPM batch APIs throw `TpmUnavailable`
138
145
  rather than falling back to OS randomness. The `no-tpm` entry point intentionally
@@ -166,21 +173,42 @@ const batch = railid.generateV4ManyEncodedOsOnly(1000);
166
173
  for (let i = 0; i < 1000; i++) {
167
174
  const value = batch.toString('ascii', i * 21, (i + 1) * 21);
168
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);
169
184
  ```
170
185
 
171
186
  - `generateV4()` and `generateV4ManyEncoded()` use the TPM hybrid profile,
172
187
  which is fail-closed: if no TPM or TSS transport is available they throw
173
188
  `TpmUnavailable` and never silently use the OS CSPRNG.
174
- - `generateV4OsOnly()`, `generateV4BufferOsOnly()`, and
175
- `generateV4ManyEncodedOsOnly()` are the explicit OS-only opt-ins. The
176
- `no-tpm` entry point maps `generateV4`, `generateV4Buffer`, and
177
- `generateV4ManyEncoded` to them.
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.
178
194
  - A v4 batch samples the UTC day once, so every ID in one call carries the
179
- same day. `count == 0` returns an empty Buffer.
195
+ same day. `count == 0` returns an empty Buffer (or writes nothing).
180
196
  - v4 batch limits match v1: 1,024 IDs for the TPM profile and 1,000,000 IDs
181
- for OS-only. Counts above the limit are rejected before allocation.
182
- - v4 has no asynchronous batch API yet; use the synchronous encoded batches,
183
- or `@neko-makura/railid/no-tpm` for OS-only workloads.
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.
184
212
  - Errors from v4 APIs carry the stable `code` values documented under
185
213
  [Errors](#errors), including `TpmUnavailable` and `InvalidInput`.
186
214
  - The v1 192-bit API above is unchanged: `generate()` and `generateOsOnly()`
@@ -242,9 +270,15 @@ const buffer = railid.generateV2ManyEncoded({ bits: 192 }, 1000); // 32,000 byte
242
270
  `generateV2Many`, and `generateV2ManyEncoded` with the OS-only profile.
243
271
  Batch encoded buffers use `count * formatV2(format).encodedLen` bytes.
244
272
 
245
- ## Caller-provided buffer API (design, not shipped)
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).
246
279
 
247
- A zero-allocation single-ID API would look like:
280
+ A zero-allocation **single-ID** API with an explicit offset is still not
281
+ shipped. It would look like:
248
282
 
249
283
  ```ts
250
284
  generateInto(target: Buffer, offset?: number): number;
@@ -256,11 +290,11 @@ generateOsOnlyInto(target: Buffer, offset?: number): number;
256
290
  - It performs no allocation and no JS string creation.
257
291
  - It is synchronous only.
258
292
 
259
- This is **not shipped** because N-API buffers can be shared with other JS
260
- execution contexts, and napi-rs documents mutable `Buffer` access as an aliasing
261
- hazard. Shipping it safely needs an exclusive-ownership guarantee, a rejection
262
- of `SharedArrayBuffer`-backed buffers, or a custom writer object. Until then,
263
- 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.
264
298
 
265
299
  ## Linux and TPM
266
300
 
package/index.d.ts CHANGED
@@ -11,7 +11,8 @@ export type RailIdErrorCode =
11
11
  | "OsRandomFailure"
12
12
  | "TimestampOutOfRange"
13
13
  | "InvalidInput"
14
- | "CapacityOverflow";
14
+ | "CapacityOverflow"
15
+ | "WorkerFailure";
15
16
 
16
17
  /**
17
18
  * Errors thrown by the bindings carry one of the {@link RailIdErrorCode}
@@ -61,7 +62,11 @@ export declare function generateV4BufferOsOnly(): Buffer;
61
62
  * Generate `count` v4 IDs as one contiguous `count * 21` Buffer with the TPM
62
63
  * hybrid profile. Capped at 1,024 IDs and fails closed without a TPM.
63
64
  * Counts are validated before allocation; invalid counts throw `InvalidInput`.
64
- * v4 has no asynchronous batch API yet.
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.
65
70
  */
66
71
  export declare function generateV4ManyEncoded(count: number): Buffer;
67
72
 
@@ -72,6 +77,43 @@ export declare function generateV4ManyEncoded(count: number): Buffer;
72
77
  */
73
78
  export declare function generateV4ManyEncodedOsOnly(count: number): Buffer;
74
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
+
75
117
  /** Generate `count` IDs synchronously as strings. OS-only strings cap at 100,000. */
76
118
  export declare function generateMany(count: number, osOnly?: boolean): string[];
77
119
 
package/index.js CHANGED
@@ -69,6 +69,7 @@ const {
69
69
  checkCount,
70
70
  batchLimit,
71
71
  stringBatchLimit,
72
+ inputError,
72
73
  wrap,
73
74
  } = require('./errors.js');
74
75
 
@@ -88,13 +89,16 @@ for (const key of Object.keys(publicBinding)) {
88
89
  key === 'v2GenerateMany' ||
89
90
  key === 'v2GenerateManyEncoded' ||
90
91
  key === 'v2GenerateManyEncodedInto' ||
91
- key === 'generateManyEncodedInto'
92
+ key === 'generateManyEncodedInto' ||
93
+ key === 'generateV4ManyEncodedInto' ||
94
+ key === 'generateV4ManyEncodedOsOnlyInto'
92
95
  ) {
93
96
  delete publicBinding[key];
94
97
  }
95
98
  }
96
99
 
97
100
  const V1_ENCODED_LEN = 32;
101
+ const V4_ENCODED_LEN = 21;
98
102
 
99
103
  function generateMany(count, osOnly) {
100
104
  const degraded = Boolean(osOnly);
@@ -132,6 +136,42 @@ function generateV4ManyEncodedOsOnly(count) {
132
136
  return binding.generateV4ManyEncodedOsOnly(count);
133
137
  }
134
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
+
135
175
  const api = {
136
176
  ...publicBinding,
137
177
  generateMany,
@@ -140,6 +180,10 @@ const api = {
140
180
  generateManyEncodedAsync,
141
181
  generateV4ManyEncoded,
142
182
  generateV4ManyEncodedOsOnly,
183
+ generateV4ManyEncodedAsync,
184
+ generateV4ManyEncodedOsOnlyAsync,
185
+ generateV4ManyEncodedInto,
186
+ generateV4ManyEncodedOsOnlyInto,
143
187
  // `backend` is the backend compiled into the loaded binary. A TPM build can
144
188
  // still return TpmUnavailable at runtime when no TPM device is present.
145
189
  backend: () => backend,
package/no-tpm.d.ts CHANGED
@@ -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
 
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.2.2",
3
+ "version": "4.3.2",
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",
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file