@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/errors.js CHANGED
@@ -1,152 +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
- };
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
+ };