@neko-makura/railid 4.3.3 → 4.4.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 +382 -372
- package/errors.js +152 -152
- package/index.d.ts +224 -200
- package/index.js +238 -200
- package/no-tpm.d.ts +125 -104
- package/no-tpm.js +41 -37
- package/package.json +57 -45
- package/v2.js +245 -222
- 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/index.js
CHANGED
|
@@ -1,200 +1,238 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('node:path');
|
|
4
|
-
|
|
5
|
-
function linuxLibc() {
|
|
6
|
-
if (process.platform !== 'linux') {
|
|
7
|
-
return '';
|
|
8
|
-
}
|
|
9
|
-
try {
|
|
10
|
-
const report = process.report && process.report.getReport();
|
|
11
|
-
const version = report && report.header && report.header.glibcVersionRuntime;
|
|
12
|
-
return version ? 'gnu' : 'musl';
|
|
13
|
-
} catch {
|
|
14
|
-
return 'gnu';
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function suffix() {
|
|
19
|
-
const platform = process.platform;
|
|
20
|
-
const arch = process.arch;
|
|
21
|
-
if (platform === 'win32') {
|
|
22
|
-
return `win32-${arch}-msvc`;
|
|
23
|
-
}
|
|
24
|
-
if (platform === 'linux') {
|
|
25
|
-
return `linux-${arch}-${linuxLibc()}`;
|
|
26
|
-
}
|
|
27
|
-
if (platform === 'darwin') {
|
|
28
|
-
return `darwin-${arch}`;
|
|
29
|
-
}
|
|
30
|
-
return `${platform}-${arch}`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function candidates() {
|
|
34
|
-
const tag = suffix();
|
|
35
|
-
if (process.platform === 'win32') {
|
|
36
|
-
return [`railid.${tag}.node`];
|
|
37
|
-
}
|
|
38
|
-
if (process.platform === 'linux') {
|
|
39
|
-
return [`railid-tpm.${tag}.node`, `railid.${tag}.node`];
|
|
40
|
-
}
|
|
41
|
-
return [`railid.${tag}.node`];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
let binding = null;
|
|
45
|
-
let backend = null;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
checkCount
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
function
|
|
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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
generateV4ManyEncodedOsOnlyAsync
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
|
|
5
|
+
function linuxLibc() {
|
|
6
|
+
if (process.platform !== 'linux') {
|
|
7
|
+
return '';
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const report = process.report && process.report.getReport();
|
|
11
|
+
const version = report && report.header && report.header.glibcVersionRuntime;
|
|
12
|
+
return version ? 'gnu' : 'musl';
|
|
13
|
+
} catch {
|
|
14
|
+
return 'gnu';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function suffix() {
|
|
19
|
+
const platform = process.platform;
|
|
20
|
+
const arch = process.arch;
|
|
21
|
+
if (platform === 'win32') {
|
|
22
|
+
return `win32-${arch}-msvc`;
|
|
23
|
+
}
|
|
24
|
+
if (platform === 'linux') {
|
|
25
|
+
return `linux-${arch}-${linuxLibc()}`;
|
|
26
|
+
}
|
|
27
|
+
if (platform === 'darwin') {
|
|
28
|
+
return `darwin-${arch}`;
|
|
29
|
+
}
|
|
30
|
+
return `${platform}-${arch}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function candidates() {
|
|
34
|
+
const tag = suffix();
|
|
35
|
+
if (process.platform === 'win32') {
|
|
36
|
+
return [`railid.${tag}.node`];
|
|
37
|
+
}
|
|
38
|
+
if (process.platform === 'linux') {
|
|
39
|
+
return [`railid-tpm.${tag}.node`, `railid.${tag}.node`];
|
|
40
|
+
}
|
|
41
|
+
return [`railid.${tag}.node`];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let binding = null;
|
|
45
|
+
let backend = null;
|
|
46
|
+
let nativePackage = null;
|
|
47
|
+
const loadErrors = [];
|
|
48
|
+
|
|
49
|
+
// A local build wins so development and the CI test flows keep working without
|
|
50
|
+
// installing anything. Published installs resolve the addon through the
|
|
51
|
+
// per-platform package instead (ADR-014).
|
|
52
|
+
for (const file of candidates()) {
|
|
53
|
+
try {
|
|
54
|
+
binding = require(path.join(__dirname, file));
|
|
55
|
+
backend = typeof binding.backend === 'function' ? binding.backend() : 'unknown';
|
|
56
|
+
break;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
loadErrors.push(`${file}: ${error.message}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!binding) {
|
|
63
|
+
const tag = suffix();
|
|
64
|
+
nativePackage = `@neko-makura/railid-${tag}`;
|
|
65
|
+
try {
|
|
66
|
+
binding = require(nativePackage);
|
|
67
|
+
backend = typeof binding.backend === 'function' ? binding.backend() : 'unknown';
|
|
68
|
+
} catch (error) {
|
|
69
|
+
loadErrors.push(`${nativePackage}: ${error.message}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!binding) {
|
|
74
|
+
const tag = suffix();
|
|
75
|
+
const error = new Error(
|
|
76
|
+
`No usable RailID native addon for ${process.platform}/${process.arch}. ` +
|
|
77
|
+
`Tried: ${[...candidates(), `@neko-makura/railid-${tag}`].join(', ')}. ` +
|
|
78
|
+
'Install `@neko-makura/railid` again to refresh its optional platform ' +
|
|
79
|
+
'package, or run `npm run build` with Rust and the platform TPM libraries.'
|
|
80
|
+
);
|
|
81
|
+
error.cause = loadErrors.join('\n');
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// A platform package that lags behind the JavaScript package would silently
|
|
86
|
+
// miss entry points, so report the difference instead. Major differences are
|
|
87
|
+
// fatal; a patch or minor difference is a warning.
|
|
88
|
+
if (typeof binding.version === 'function') {
|
|
89
|
+
const expected = require('./package.json').version;
|
|
90
|
+
const actual = binding.version();
|
|
91
|
+
const differs = actual !== expected;
|
|
92
|
+
const majorDiffers = actual.split('.')[0] !== expected.split('.')[0];
|
|
93
|
+
if (differs) {
|
|
94
|
+
const message =
|
|
95
|
+
`RailID addon version ${actual} does not match @neko-makura/railid ${expected}` +
|
|
96
|
+
`${nativePackage ? ` (loaded from ${nativePackage})` : ' (loaded from a local build)'}.`;
|
|
97
|
+
if (majorDiffers) {
|
|
98
|
+
throw new Error(`${message} Reinstall the package to refresh its platform packages.`);
|
|
99
|
+
}
|
|
100
|
+
process.emitWarning(`${message} Some APIs may be unavailable.`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const {
|
|
105
|
+
checkCount,
|
|
106
|
+
batchLimit,
|
|
107
|
+
stringBatchLimit,
|
|
108
|
+
inputError,
|
|
109
|
+
wrap,
|
|
110
|
+
} = require('./errors.js');
|
|
111
|
+
|
|
112
|
+
const v2 = require('./v2.js')(binding);
|
|
113
|
+
const publicBinding = { ...binding };
|
|
114
|
+
|
|
115
|
+
// The OS-only preset entry points are selected by the v2 wrapper, and the
|
|
116
|
+
// packed preset helpers are binding implementation details. Neither is part of
|
|
117
|
+
// the public surface.
|
|
118
|
+
for (const key of Object.keys(publicBinding)) {
|
|
119
|
+
if (
|
|
120
|
+
key.startsWith('v2InternalPreset') ||
|
|
121
|
+
key === 'v2GeneratePreset' ||
|
|
122
|
+
key === 'v2GeneratePresetBuffer' ||
|
|
123
|
+
key === 'v2GeneratePacked' ||
|
|
124
|
+
key === 'v2GenerateBufferPacked' ||
|
|
125
|
+
key === 'v2GenerateMany' ||
|
|
126
|
+
key === 'v2GenerateManyEncoded' ||
|
|
127
|
+
key === 'v2GenerateManyEncodedInto' ||
|
|
128
|
+
key === 'v2GenerateManyAsync' ||
|
|
129
|
+
key === 'v2GenerateManyEncodedAsync' ||
|
|
130
|
+
key === 'generateManyEncodedInto' ||
|
|
131
|
+
key === 'generateV4ManyEncodedInto' ||
|
|
132
|
+
key === 'generateV4ManyEncodedOsOnlyInto'
|
|
133
|
+
) {
|
|
134
|
+
delete publicBinding[key];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const V1_ENCODED_LEN = 32;
|
|
139
|
+
const V4_ENCODED_LEN = 21;
|
|
140
|
+
|
|
141
|
+
function generateMany(count, osOnly) {
|
|
142
|
+
const degraded = Boolean(osOnly);
|
|
143
|
+
checkCount(count, stringBatchLimit(degraded));
|
|
144
|
+
return binding.generateMany(count, degraded);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function generateManyAsync(count, osOnly) {
|
|
148
|
+
const degraded = Boolean(osOnly);
|
|
149
|
+
checkCount(count, stringBatchLimit(degraded));
|
|
150
|
+
return binding.generateManyAsync(count, degraded);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function generateManyEncoded(count, osOnly) {
|
|
154
|
+
const degraded = Boolean(osOnly);
|
|
155
|
+
checkCount(count, batchLimit(degraded));
|
|
156
|
+
const buffer = Buffer.allocUnsafe(count * V1_ENCODED_LEN);
|
|
157
|
+
binding.generateManyEncodedInto(buffer, degraded);
|
|
158
|
+
return buffer;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function generateManyEncodedAsync(count, osOnly) {
|
|
162
|
+
const degraded = Boolean(osOnly);
|
|
163
|
+
checkCount(count, batchLimit(degraded));
|
|
164
|
+
return binding.generateManyEncodedAsync(count, degraded);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function generateV4ManyEncoded(count) {
|
|
168
|
+
checkCount(count, batchLimit(false));
|
|
169
|
+
return binding.generateV4ManyEncoded(count);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function generateV4ManyEncodedOsOnly(count) {
|
|
173
|
+
checkCount(count, batchLimit(true));
|
|
174
|
+
return binding.generateV4ManyEncodedOsOnly(count);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function generateV4ManyEncodedAsync(count) {
|
|
178
|
+
checkCount(count, batchLimit(false));
|
|
179
|
+
return binding.generateV4ManyEncodedAsync(count);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function generateV4ManyEncodedOsOnlyAsync(count) {
|
|
183
|
+
checkCount(count, batchLimit(true));
|
|
184
|
+
return binding.generateV4ManyEncodedOsOnlyAsync(count);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// A v4 target buffer is exactly `count * 21` bytes, so its length determines
|
|
188
|
+
// the count. Validate before the native call so an over-limit buffer never
|
|
189
|
+
// reaches a TPM operation and a misaligned buffer never gets partially
|
|
190
|
+
// written.
|
|
191
|
+
function checkV4IntoBuffer(buffer, limit) {
|
|
192
|
+
if (!Buffer.isBuffer(buffer)) {
|
|
193
|
+
throw inputError('buffer must be a Buffer');
|
|
194
|
+
}
|
|
195
|
+
if (buffer.length % V4_ENCODED_LEN !== 0) {
|
|
196
|
+
throw inputError('buffer length must be a multiple of 21');
|
|
197
|
+
}
|
|
198
|
+
const count = buffer.length / V4_ENCODED_LEN;
|
|
199
|
+
checkCount(count, limit, 'v4 into count');
|
|
200
|
+
return count;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function generateV4ManyEncodedInto(buffer) {
|
|
204
|
+
checkV4IntoBuffer(buffer, batchLimit(false));
|
|
205
|
+
return binding.generateV4ManyEncodedInto(buffer);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function generateV4ManyEncodedOsOnlyInto(buffer) {
|
|
209
|
+
checkV4IntoBuffer(buffer, batchLimit(true));
|
|
210
|
+
return binding.generateV4ManyEncodedOsOnlyInto(buffer);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const api = {
|
|
214
|
+
...publicBinding,
|
|
215
|
+
generateMany,
|
|
216
|
+
generateManyAsync,
|
|
217
|
+
generateManyEncoded,
|
|
218
|
+
generateManyEncodedAsync,
|
|
219
|
+
generateV4ManyEncoded,
|
|
220
|
+
generateV4ManyEncodedOsOnly,
|
|
221
|
+
generateV4ManyEncodedAsync,
|
|
222
|
+
generateV4ManyEncodedOsOnlyAsync,
|
|
223
|
+
generateV4ManyEncodedInto,
|
|
224
|
+
generateV4ManyEncodedOsOnlyInto,
|
|
225
|
+
// `backend` is the backend compiled into the loaded binary. A TPM build can
|
|
226
|
+
// still return TpmUnavailable at runtime when no TPM device is present.
|
|
227
|
+
backend: () => backend,
|
|
228
|
+
...v2,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// Map native errors to stable `code` values once, at the public boundary.
|
|
232
|
+
for (const [key, value] of Object.entries(api)) {
|
|
233
|
+
if (typeof value === 'function') {
|
|
234
|
+
api[key] = wrap(value);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
module.exports = api;
|