@neko-makura/railid 2.0.2 → 3.0.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 CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  Native N-API bindings for [RailID](https://crates.io/crates/railid).
4
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
+
5
8
  ```bash
6
9
  npm install @neko-makura/railid
7
10
  ```
@@ -96,7 +99,8 @@ still never falls back implicitly.
96
99
  | `version()` | `string` | RailID crate version |
97
100
  | `backend()` | `"tpm" \| "os-only"` | Backend compiled into the loaded binary |
98
101
 
99
- v2 adds `generateV2`, `generateV2Buffer`, `decodeV2`, and `formatV2`; see
102
+ v2 adds `generateV2`, `generateV2Buffer`, `generateV2Many`,
103
+ `generateV2ManyEncoded`, `decodeV2`, and `formatV2`; see
100
104
  [RailID v2](#railid-v2) below.
101
105
 
102
106
  Limits:
@@ -134,6 +138,10 @@ const custom = railid.formatV2({
134
138
  force: true,
135
139
  });
136
140
  // custom.warnings: NON_DEFAULT_TIME, DECOUPLED_TIME, SHORT_RANGE
141
+
142
+ // Batches use one format. The buffer has count * encodedLen bytes.
143
+ const ids = railid.generateV2Many({ bits: 192 }, 1000); // string[]
144
+ const buffer = railid.generateV2ManyEncoded({ bits: 192 }, 1000); // 32,000 bytes
137
145
  ```
138
146
 
139
147
  | Preset | Bits | Timestamp | Random | Characters |
@@ -151,12 +159,14 @@ const custom = railid.formatV2({
151
159
  - `force: true` allows non-preset lengths, timestamps on short lengths, no
152
160
  timestamp on long lengths, and decoupled tick/width pairs.
153
161
  `generateV2`/`generateV2Buffer` emit the warnings on every call through
154
- `process.emitWarning`; `formatV2` returns them in `warnings`.
162
+ `process.emitWarning`; `formatV2` returns them in `warnings`. Batch calls
163
+ emit the format warnings once per call.
155
164
  - The v2 epoch is `2020-01-01T00:00:00Z`. Timestamps are returned as decimal
156
165
  strings to preserve 64-bit ticks and 128-bit microsecond values.
157
166
  - v2 is not wire-compatible with v1. See `docs/SPEC-v2.md` in the repository.
158
- - The `no-tpm` entry point exposes `generateV2` and `generateV2Buffer` with the
159
- OS-only profile.
167
+ - The `no-tpm` entry point exposes `generateV2`, `generateV2Buffer`,
168
+ `generateV2Many`, and `generateV2ManyEncoded` with the OS-only profile.
169
+ Batch encoded buffers use `count * formatV2(format).encodedLen` bytes.
160
170
 
161
171
  ## Caller-provided buffer API (design, not shipped)
162
172
 
package/index.d.ts CHANGED
@@ -94,6 +94,12 @@ export declare function generateV2(format: V2Format): string;
94
94
  /** Generate one v2 value as 3-171 ASCII bytes. */
95
95
  export declare function generateV2Buffer(format: V2Format): Buffer;
96
96
 
97
+ /** Generate `count` v2 values as a string array. */
98
+ export declare function generateV2Many(format: V2Format, count: number): string[];
99
+
100
+ /** Generate `count` v2 values as one contiguous `count * encodedLen` Buffer. */
101
+ export declare function generateV2ManyEncoded(format: V2Format, count: number): Buffer;
102
+
97
103
  /** Decode a v2 value with the expected format. */
98
104
  export declare function decodeV2(input: string, format: V2Format): V2Decoded;
99
105
 
package/index.js CHANGED
@@ -77,14 +77,36 @@ for (const key of Object.keys(publicBinding)) {
77
77
  key === 'v2GeneratePreset' ||
78
78
  key === 'v2GeneratePresetBuffer' ||
79
79
  key === 'v2GeneratePacked' ||
80
- key === 'v2GenerateBufferPacked'
80
+ key === 'v2GenerateBufferPacked' ||
81
+ key === 'v2GenerateMany' ||
82
+ key === 'v2GenerateManyEncoded' ||
83
+ key === 'v2GenerateManyEncodedInto' ||
84
+ key === 'generateManyEncodedInto'
81
85
  ) {
82
86
  delete publicBinding[key];
83
87
  }
84
88
  }
85
89
 
90
+ const V1_ENCODED_LEN = 32;
91
+ const V1_OS_LIMIT = 1_000_000;
92
+ const V1_TPM_LIMIT = 1_024;
93
+
94
+ function generateManyEncoded(count, osOnly) {
95
+ if (!Number.isInteger(count) || count < 0 || count > 0xffffffff) {
96
+ throw new TypeError('count must be an integer between 0 and 4294967295');
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
+ }
102
+ const buffer = Buffer.allocUnsafe(count * V1_ENCODED_LEN);
103
+ binding.generateManyEncodedInto(buffer, Boolean(osOnly));
104
+ return buffer;
105
+ }
106
+
86
107
  module.exports = {
87
108
  ...publicBinding,
109
+ generateManyEncoded,
88
110
  // `backend` is the backend compiled into the loaded binary. A TPM build can
89
111
  // still return TpmUnavailable at runtime when no TPM device is present.
90
112
  backend: () => backend,
package/no-tpm.d.ts CHANGED
@@ -46,6 +46,12 @@ export declare function generateV2(format: V2Format): string;
46
46
  /** Generate one OS-only v2 value as 32-171 ASCII bytes. */
47
47
  export declare function generateV2Buffer(format: V2Format): Buffer;
48
48
 
49
+ /** Generate `count` OS-only v2 values as a string array. */
50
+ export declare function generateV2Many(format: V2Format, count: number): string[];
51
+
52
+ /** Generate `count` OS-only v2 values as one contiguous `count * encodedLen` Buffer. */
53
+ export declare function generateV2ManyEncoded(format: V2Format, count: number): Buffer;
54
+
49
55
  /** Decode a v2 value with the expected format. */
50
56
  export declare function decodeV2(input: string, format: V2Format): V2Decoded;
51
57
 
package/no-tpm.js CHANGED
@@ -21,6 +21,9 @@ module.exports = {
21
21
  // v2 generation uses the OS-only profile; decoding is profile-independent.
22
22
  generateV2: (format) => core.generateV2(format, 'os-only'),
23
23
  generateV2Buffer: (format) => core.generateV2Buffer(format, 'os-only'),
24
+ generateV2Many: (format, count) => core.generateV2Many(format, count, 'os-only'),
25
+ generateV2ManyEncoded: (format, count) =>
26
+ core.generateV2ManyEncoded(format, count, 'os-only'),
24
27
  // The loaded binary may still contain the TPM backend, but this module never
25
28
  // calls it.
26
29
  backend: () => 'os-only',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neko-makura/railid",
3
- "version": "2.0.2",
3
+ "version": "3.0.0",
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
package/v2.js CHANGED
@@ -182,6 +182,36 @@ module.exports = function createV2(binding) {
182
182
  return binding.v2GenerateBufferPacked(entry.key | (profile === 'os-only' ? 1 << 24 : 0));
183
183
  }
184
184
 
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
+ function generateV2Many(format, count, profile) {
193
+ batchCount(count);
194
+ const args = toNativeArguments(format);
195
+ const entry = cachedInfo(args);
196
+ emitWarnings(entry.commands);
197
+ return binding.v2GenerateMany(...args, count, profile === 'os-only');
198
+ }
199
+
200
+ function generateV2ManyEncoded(format, count, profile) {
201
+ batchCount(count);
202
+ const args = toNativeArguments(format);
203
+ const entry = cachedInfo(args);
204
+ emitWarnings(entry.commands);
205
+ const buffer = Buffer.allocUnsafe(count * entry.info.encodedLen);
206
+ binding.v2GenerateManyEncodedInto(
207
+ ...args,
208
+ count,
209
+ profile === 'os-only',
210
+ buffer
211
+ );
212
+ return buffer;
213
+ }
214
+
185
215
  function decodeV2(input, format) {
186
216
  if (typeof input !== 'string') {
187
217
  throw new TypeError('input must be a string');
@@ -189,5 +219,12 @@ module.exports = function createV2(binding) {
189
219
  return normalizeDecoded(binding.v2Decode(input, ...toNativeArguments(format)));
190
220
  }
191
221
 
192
- return { generateV2, generateV2Buffer, decodeV2, formatV2 };
222
+ return {
223
+ generateV2,
224
+ generateV2Buffer,
225
+ generateV2Many,
226
+ generateV2ManyEncoded,
227
+ decodeV2,
228
+ formatV2,
229
+ };
193
230
  };