@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/v2.js CHANGED
@@ -1,245 +1,245 @@
1
- 'use strict';
2
-
3
- const { checkCount, batchLimit } = require('./errors.js');
4
-
5
- const TICKS = ['1d', '1h', '1s', '100ms', '10ms', '1ms', '0.1ms'];
6
-
7
- function tickIndex(name) {
8
- const index = TICKS.indexOf(name);
9
- if (index < 0) {
10
- throw new TypeError(
11
- `unknown tick: ${String(name)} (expected ${TICKS.join(', ')})`
12
- );
13
- }
14
- return index;
15
- }
16
-
17
- function toNativeArguments(format) {
18
- if (format === null || typeof format !== 'object') {
19
- throw new TypeError('format must be an object');
20
- }
21
- const bits = format.bits;
22
- if (!Number.isInteger(bits) || bits < 16 || bits > 1024) {
23
- throw new TypeError('format.bits must be an integer between 16 and 1024');
24
- }
25
- const force = Boolean(format.force);
26
- const time = format.time;
27
-
28
- if (time === undefined) {
29
- return [bits, 0, undefined, undefined, force];
30
- }
31
- if (time === null) {
32
- return [bits, 1, undefined, undefined, force];
33
- }
34
- if (typeof time === 'string') {
35
- return [bits, 2, tickIndex(time), undefined, force];
36
- }
37
- if (typeof time === 'object') {
38
- if (time.tick === undefined) {
39
- throw new TypeError('format.time.tick is required');
40
- }
41
- if (time.bits === undefined) {
42
- throw new TypeError(
43
- 'format.time.bits is required for an object time; use a tick string for the conventional width'
44
- );
45
- }
46
- if (!Number.isInteger(time.bits)) {
47
- throw new TypeError('format.time.bits must be an integer');
48
- }
49
- return [bits, 2, tickIndex(time.tick), time.bits, force];
50
- }
51
- throw new TypeError('format.time must be a tick name, an object, null, or undefined');
52
- }
53
-
54
- function formatKey(args) {
55
- return (
56
- args[0] |
57
- (args[1] << 11) |
58
- ((args[2] ?? 7) << 13) |
59
- ((args[3] ?? 127) << 16) |
60
- (args[4] ? 1 << 23 : 0)
61
- );
62
- }
63
-
64
- function prepareWarnings(warnings) {
65
- return warnings.map((warning) => ({
66
- reason: warning.reason,
67
- options: Object.freeze({ code: `RAILID_V2_${warning.code}` }),
68
- }));
69
- }
70
-
71
- function emitWarnings(commands) {
72
- for (const command of commands) {
73
- process.emitWarning(command.reason, command.options);
74
- }
75
- }
76
-
77
- module.exports = function createV2(binding) {
78
- const infoCache = new Map();
79
- const MAX_CACHE_ENTRIES = 256;
80
-
81
- function presetIndex(format) {
82
- if (
83
- format === null ||
84
- typeof format !== 'object' ||
85
- format.force ||
86
- format.time !== undefined
87
- ) {
88
- return -1;
89
- }
90
- switch (format.bits) {
91
- case 32:
92
- return 0;
93
- case 64:
94
- return 1;
95
- case 126:
96
- return 2;
97
- case 192:
98
- return 3;
99
- case 256:
100
- return 4;
101
- case 512:
102
- return 5;
103
- default:
104
- return -1;
105
- }
106
- }
107
-
108
- function normalizeInfo(info) {
109
- return {
110
- ...info,
111
- tick: info.tick ?? null,
112
- warnings: info.warnings ?? [],
113
- };
114
- }
115
-
116
- function normalizeDecoded(decoded) {
117
- return {
118
- ...decoded,
119
- ticks: decoded.ticks ?? null,
120
- timestampMs: decoded.timestampMs ?? null,
121
- timestampUs: decoded.timestampUs ?? null,
122
- };
123
- }
124
-
125
- // Format construction is deterministic for a given argument tuple. Cache the
126
- // normalized descriptor so `generateV2` does not rebuild it (and allocate a
127
- // N-API object) for every ID. The cache key is a packed integer, not a
128
- // joined string, so the hot path does not allocate.
129
- function cachedInfo(args) {
130
- const key = formatKey(args);
131
- let entry = infoCache.get(key);
132
- if (entry === undefined) {
133
- if (infoCache.size >= MAX_CACHE_ENTRIES) {
134
- infoCache.clear();
135
- }
136
- const info = normalizeInfo(binding.v2Format(...args));
137
- entry = { key, info, commands: prepareWarnings(info.warnings) };
138
- infoCache.set(key, entry);
139
- }
140
- return entry;
141
- }
142
-
143
- function formatV2(format) {
144
- const { info } = cachedInfo(toNativeArguments(format));
145
- return { ...info, warnings: info.warnings.map((warning) => ({ ...warning })) };
146
- }
147
-
148
- // The OS-only profile is the throughput-critical path. Each preset has a
149
- // dedicated zero-argument N-API entry point so V8 does not pay the argument
150
- // marshalling cost on every ID.
151
- const OS_PRESETS = [
152
- binding.v2InternalPresetB32Os,
153
- binding.v2InternalPresetB64Os,
154
- binding.v2InternalPresetB126Os,
155
- binding.v2InternalPresetB192Os,
156
- binding.v2InternalPresetB256Os,
157
- binding.v2InternalPresetB512Os,
158
- ];
159
-
160
- function presetArgument(preset, profile) {
161
- return preset | (profile === 'os-only' ? 0x100 : 0);
162
- }
163
-
164
- function generateV2(format, profile) {
165
- const preset = presetIndex(format);
166
- if (preset >= 0) {
167
- if (profile === 'os-only') {
168
- return OS_PRESETS[preset]();
169
- }
170
- return binding.v2GeneratePreset(presetArgument(preset, profile));
171
- }
172
- const entry = cachedInfo(toNativeArguments(format));
173
- emitWarnings(entry.commands);
174
- return binding.v2GeneratePacked(entry.key | (profile === 'os-only' ? 1 << 24 : 0));
175
- }
176
-
177
- function generateV2Buffer(format, profile) {
178
- const preset = presetIndex(format);
179
- if (preset >= 0) {
180
- return binding.v2GeneratePresetBuffer(presetArgument(preset, profile));
181
- }
182
- const entry = cachedInfo(toNativeArguments(format));
183
- emitWarnings(entry.commands);
184
- return binding.v2GenerateBufferPacked(entry.key | (profile === 'os-only' ? 1 << 24 : 0));
185
- }
186
-
187
- function generateV2Many(format, count, profile) {
188
- const degraded = profile === 'os-only';
189
- checkCount(count, batchLimit(degraded));
190
- const args = toNativeArguments(format);
191
- const entry = cachedInfo(args);
192
- emitWarnings(entry.commands);
193
- return binding.v2GenerateMany(...args, count, degraded);
194
- }
195
-
196
- function generateV2ManyEncoded(format, count, profile) {
197
- const degraded = profile === 'os-only';
198
- checkCount(count, batchLimit(degraded));
199
- const args = toNativeArguments(format);
200
- const entry = cachedInfo(args);
201
- emitWarnings(entry.commands);
202
- const buffer = Buffer.allocUnsafe(count * entry.info.encodedLen);
203
- binding.v2GenerateManyEncodedInto(...args, count, degraded, buffer);
204
- return buffer;
205
- }
206
-
207
- // Async batches validate the format and count on the JS thread; only the
208
- // generation runs on a libuv worker. The resolved values have the same shape
209
- // as the synchronous functions.
210
- function generateV2ManyAsync(format, count, profile) {
211
- const degraded = profile === 'os-only';
212
- checkCount(count, batchLimit(degraded));
213
- const args = toNativeArguments(format);
214
- const entry = cachedInfo(args);
215
- emitWarnings(entry.commands);
216
- return binding.v2GenerateManyAsync(...args, count, degraded);
217
- }
218
-
219
- function generateV2ManyEncodedAsync(format, count, profile) {
220
- const degraded = profile === 'os-only';
221
- checkCount(count, batchLimit(degraded));
222
- const args = toNativeArguments(format);
223
- const entry = cachedInfo(args);
224
- emitWarnings(entry.commands);
225
- return binding.v2GenerateManyEncodedAsync(...args, count, degraded);
226
- }
227
-
228
- function decodeV2(input, format) {
229
- if (typeof input !== 'string') {
230
- throw new TypeError('input must be a string');
231
- }
232
- return normalizeDecoded(binding.v2Decode(input, ...toNativeArguments(format)));
233
- }
234
-
235
- return {
236
- generateV2,
237
- generateV2Buffer,
238
- generateV2Many,
239
- generateV2ManyEncoded,
240
- generateV2ManyAsync,
241
- generateV2ManyEncodedAsync,
242
- decodeV2,
243
- formatV2,
244
- };
245
- };
1
+ 'use strict';
2
+
3
+ const { checkCount, batchLimit } = require('./errors.js');
4
+
5
+ const TICKS = ['1d', '1h', '1s', '100ms', '10ms', '1ms', '0.1ms'];
6
+
7
+ function tickIndex(name) {
8
+ const index = TICKS.indexOf(name);
9
+ if (index < 0) {
10
+ throw new TypeError(
11
+ `unknown tick: ${String(name)} (expected ${TICKS.join(', ')})`
12
+ );
13
+ }
14
+ return index;
15
+ }
16
+
17
+ function toNativeArguments(format) {
18
+ if (format === null || typeof format !== 'object') {
19
+ throw new TypeError('format must be an object');
20
+ }
21
+ const bits = format.bits;
22
+ if (!Number.isInteger(bits) || bits < 16 || bits > 1024) {
23
+ throw new TypeError('format.bits must be an integer between 16 and 1024');
24
+ }
25
+ const force = Boolean(format.force);
26
+ const time = format.time;
27
+
28
+ if (time === undefined) {
29
+ return [bits, 0, undefined, undefined, force];
30
+ }
31
+ if (time === null) {
32
+ return [bits, 1, undefined, undefined, force];
33
+ }
34
+ if (typeof time === 'string') {
35
+ return [bits, 2, tickIndex(time), undefined, force];
36
+ }
37
+ if (typeof time === 'object') {
38
+ if (time.tick === undefined) {
39
+ throw new TypeError('format.time.tick is required');
40
+ }
41
+ if (time.bits === undefined) {
42
+ throw new TypeError(
43
+ 'format.time.bits is required for an object time; use a tick string for the conventional width'
44
+ );
45
+ }
46
+ if (!Number.isInteger(time.bits)) {
47
+ throw new TypeError('format.time.bits must be an integer');
48
+ }
49
+ return [bits, 2, tickIndex(time.tick), time.bits, force];
50
+ }
51
+ throw new TypeError('format.time must be a tick name, an object, null, or undefined');
52
+ }
53
+
54
+ function formatKey(args) {
55
+ return (
56
+ args[0] |
57
+ (args[1] << 11) |
58
+ ((args[2] ?? 7) << 13) |
59
+ ((args[3] ?? 127) << 16) |
60
+ (args[4] ? 1 << 23 : 0)
61
+ );
62
+ }
63
+
64
+ function prepareWarnings(warnings) {
65
+ return warnings.map((warning) => ({
66
+ reason: warning.reason,
67
+ options: Object.freeze({ code: `RAILID_V2_${warning.code}` }),
68
+ }));
69
+ }
70
+
71
+ function emitWarnings(commands) {
72
+ for (const command of commands) {
73
+ process.emitWarning(command.reason, command.options);
74
+ }
75
+ }
76
+
77
+ module.exports = function createV2(binding) {
78
+ const infoCache = new Map();
79
+ const MAX_CACHE_ENTRIES = 256;
80
+
81
+ function presetIndex(format) {
82
+ if (
83
+ format === null ||
84
+ typeof format !== 'object' ||
85
+ format.force ||
86
+ format.time !== undefined
87
+ ) {
88
+ return -1;
89
+ }
90
+ switch (format.bits) {
91
+ case 32:
92
+ return 0;
93
+ case 64:
94
+ return 1;
95
+ case 126:
96
+ return 2;
97
+ case 192:
98
+ return 3;
99
+ case 256:
100
+ return 4;
101
+ case 512:
102
+ return 5;
103
+ default:
104
+ return -1;
105
+ }
106
+ }
107
+
108
+ function normalizeInfo(info) {
109
+ return {
110
+ ...info,
111
+ tick: info.tick ?? null,
112
+ warnings: info.warnings ?? [],
113
+ };
114
+ }
115
+
116
+ function normalizeDecoded(decoded) {
117
+ return {
118
+ ...decoded,
119
+ ticks: decoded.ticks ?? null,
120
+ timestampMs: decoded.timestampMs ?? null,
121
+ timestampUs: decoded.timestampUs ?? null,
122
+ };
123
+ }
124
+
125
+ // Format construction is deterministic for a given argument tuple. Cache the
126
+ // normalized descriptor so `generateV2` does not rebuild it (and allocate a
127
+ // N-API object) for every ID. The cache key is a packed integer, not a
128
+ // joined string, so the hot path does not allocate.
129
+ function cachedInfo(args) {
130
+ const key = formatKey(args);
131
+ let entry = infoCache.get(key);
132
+ if (entry === undefined) {
133
+ if (infoCache.size >= MAX_CACHE_ENTRIES) {
134
+ infoCache.clear();
135
+ }
136
+ const info = normalizeInfo(binding.v2Format(...args));
137
+ entry = { key, info, commands: prepareWarnings(info.warnings) };
138
+ infoCache.set(key, entry);
139
+ }
140
+ return entry;
141
+ }
142
+
143
+ function formatV2(format) {
144
+ const { info } = cachedInfo(toNativeArguments(format));
145
+ return { ...info, warnings: info.warnings.map((warning) => ({ ...warning })) };
146
+ }
147
+
148
+ // The OS-only profile is the throughput-critical path. Each preset has a
149
+ // dedicated zero-argument N-API entry point so V8 does not pay the argument
150
+ // marshalling cost on every ID.
151
+ const OS_PRESETS = [
152
+ binding.v2InternalPresetB32Os,
153
+ binding.v2InternalPresetB64Os,
154
+ binding.v2InternalPresetB126Os,
155
+ binding.v2InternalPresetB192Os,
156
+ binding.v2InternalPresetB256Os,
157
+ binding.v2InternalPresetB512Os,
158
+ ];
159
+
160
+ function presetArgument(preset, profile) {
161
+ return preset | (profile === 'os-only' ? 0x100 : 0);
162
+ }
163
+
164
+ function generateV2(format, profile) {
165
+ const preset = presetIndex(format);
166
+ if (preset >= 0) {
167
+ if (profile === 'os-only') {
168
+ return OS_PRESETS[preset]();
169
+ }
170
+ return binding.v2GeneratePreset(presetArgument(preset, profile));
171
+ }
172
+ const entry = cachedInfo(toNativeArguments(format));
173
+ emitWarnings(entry.commands);
174
+ return binding.v2GeneratePacked(entry.key | (profile === 'os-only' ? 1 << 24 : 0));
175
+ }
176
+
177
+ function generateV2Buffer(format, profile) {
178
+ const preset = presetIndex(format);
179
+ if (preset >= 0) {
180
+ return binding.v2GeneratePresetBuffer(presetArgument(preset, profile));
181
+ }
182
+ const entry = cachedInfo(toNativeArguments(format));
183
+ emitWarnings(entry.commands);
184
+ return binding.v2GenerateBufferPacked(entry.key | (profile === 'os-only' ? 1 << 24 : 0));
185
+ }
186
+
187
+ function generateV2Many(format, count, profile) {
188
+ const degraded = profile === 'os-only';
189
+ checkCount(count, batchLimit(degraded));
190
+ const args = toNativeArguments(format);
191
+ const entry = cachedInfo(args);
192
+ emitWarnings(entry.commands);
193
+ return binding.v2GenerateMany(...args, count, degraded);
194
+ }
195
+
196
+ function generateV2ManyEncoded(format, count, profile) {
197
+ const degraded = profile === 'os-only';
198
+ checkCount(count, batchLimit(degraded));
199
+ const args = toNativeArguments(format);
200
+ const entry = cachedInfo(args);
201
+ emitWarnings(entry.commands);
202
+ const buffer = Buffer.allocUnsafe(count * entry.info.encodedLen);
203
+ binding.v2GenerateManyEncodedInto(...args, count, degraded, buffer);
204
+ return buffer;
205
+ }
206
+
207
+ // Async batches validate the format and count on the JS thread; only the
208
+ // generation runs on a libuv worker. The resolved values have the same shape
209
+ // as the synchronous functions.
210
+ function generateV2ManyAsync(format, count, profile) {
211
+ const degraded = profile === 'os-only';
212
+ checkCount(count, batchLimit(degraded));
213
+ const args = toNativeArguments(format);
214
+ const entry = cachedInfo(args);
215
+ emitWarnings(entry.commands);
216
+ return binding.v2GenerateManyAsync(...args, count, degraded);
217
+ }
218
+
219
+ function generateV2ManyEncodedAsync(format, count, profile) {
220
+ const degraded = profile === 'os-only';
221
+ checkCount(count, batchLimit(degraded));
222
+ const args = toNativeArguments(format);
223
+ const entry = cachedInfo(args);
224
+ emitWarnings(entry.commands);
225
+ return binding.v2GenerateManyEncodedAsync(...args, count, degraded);
226
+ }
227
+
228
+ function decodeV2(input, format) {
229
+ if (typeof input !== 'string') {
230
+ throw new TypeError('input must be a string');
231
+ }
232
+ return normalizeDecoded(binding.v2Decode(input, ...toNativeArguments(format)));
233
+ }
234
+
235
+ return {
236
+ generateV2,
237
+ generateV2Buffer,
238
+ generateV2Many,
239
+ generateV2ManyEncoded,
240
+ generateV2ManyAsync,
241
+ generateV2ManyEncodedAsync,
242
+ decodeV2,
243
+ formatV2,
244
+ };
245
+ };