node-opcua-status-code 2.180.0 → 2.181.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/dist/_generated_status_codes.d.ts +2 -2
- package/dist/_generated_status_codes.js +276 -276
- package/dist/_generated_status_codes.js.map +1 -1
- package/dist/callbacks.d.ts +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/opcua_status_code.d.ts +9 -5
- package/dist/opcua_status_code.js +58 -28
- package/dist/opcua_status_code.js.map +1 -1
- package/dist/status_codes_registry.d.ts +24 -0
- package/dist/status_codes_registry.js +46 -0
- package/dist/status_codes_registry.js.map +1 -0
- package/package.json +5 -4
- package/source/_generated_status_codes.ts +1 -1
- package/source/callbacks.ts +1 -1
- package/source/index.ts +4 -2
- package/source/opcua_status_code.ts +63 -36
- package/source/status_codes_registry.ts +52 -0
|
@@ -119,8 +119,26 @@ export abstract class StatusCode {
|
|
|
119
119
|
/**
|
|
120
120
|
* returns a status code that can be modified
|
|
121
121
|
*/
|
|
122
|
-
|
|
123
|
-
|
|
122
|
+
// `optionalBits` is optional in fact, and always was: the body below skips `set` when
|
|
123
|
+
// it is absent, and callers have been omitting it. It returns a ModifiableStatusCode,
|
|
124
|
+
// never a plain StatusCode, which is what makes `.set`/`.unset` valid on the result.
|
|
125
|
+
// Both were only mis-declared; the test suite was reaching them through require()'s
|
|
126
|
+
// `any` and so never checked.
|
|
127
|
+
public static makeStatusCode(statusCode: StatusCode | string, optionalBits?: string | number): ModifiableStatusCode {
|
|
128
|
+
// The instanceof case is handled here rather than deferred to the injected
|
|
129
|
+
// coerce, because the generated table calls this during its own evaluation
|
|
130
|
+
// (`static GoodWithOverflowBit = StatusCode.makeStatusCode(StatusCodes.Good, ...)`),
|
|
131
|
+
// which is before the registry has installed anything. That call passes a
|
|
132
|
+
// StatusCode, so it needs no table; only the string and number forms do.
|
|
133
|
+
let _base: StatusCode;
|
|
134
|
+
if (statusCode instanceof StatusCode) {
|
|
135
|
+
_base = statusCode;
|
|
136
|
+
} else {
|
|
137
|
+
if (!coerce) {
|
|
138
|
+
throw new Error("node-opcua-status-code: the status code table has not been installed yet");
|
|
139
|
+
}
|
|
140
|
+
_base = coerce(statusCode);
|
|
141
|
+
}
|
|
124
142
|
const tmp = new ModifiableStatusCode({ _base });
|
|
125
143
|
if (optionalBits || typeof optionalBits === "number") {
|
|
126
144
|
tmp.set(optionalBits);
|
|
@@ -254,15 +272,45 @@ export function encodeStatusCode(statusCode: StatusCode | ConstantStatusCode, st
|
|
|
254
272
|
stream.writeUInt32(statusCode.value);
|
|
255
273
|
}
|
|
256
274
|
|
|
257
|
-
// StatusCodes (in the generated _generated_status_codes.ts) is a class with one static
|
|
258
|
-
// ConstantStatusCode field per named status, plus makeStatusCode monkey-patched onto it below.
|
|
259
|
-
// These types capture that dynamic shape for the casts that index/extend it by string key.
|
|
260
|
-
type IndexedStatusCodes = Record<string, ConstantStatusCode>;
|
|
261
|
-
type StatusCodesWithMakeStatusCode = typeof StatusCodes & { makeStatusCode: typeof StatusCode.makeStatusCode };
|
|
262
|
-
|
|
263
275
|
/** @internal construct status codes fast search indexes */
|
|
264
276
|
const statusCodesReversedMap: Record<string, StatusCode> = {};
|
|
265
277
|
|
|
278
|
+
/**
|
|
279
|
+
* The status code returned for a code that is not in the table. It is StatusCodes.Bad,
|
|
280
|
+
* but this module must not import the generated table to get it.
|
|
281
|
+
*
|
|
282
|
+
* _generated_status_codes.ts builds its ConstantStatusCode instances while its own module
|
|
283
|
+
* body runs, using the classes declared here. Under ESM an import is hoisted and the
|
|
284
|
+
* imported module is evaluated first, so importing the generated table from this file
|
|
285
|
+
* would evaluate it before these class declarations were initialised, and every one of
|
|
286
|
+
* those 280 constructions would throw a TDZ ReferenceError. It only works under CommonJS
|
|
287
|
+
* because `require()` runs where it is written, which used to be at the foot of this file.
|
|
288
|
+
*
|
|
289
|
+
* So the dependency is inverted: status_codes_registry.ts imports both and injects.
|
|
290
|
+
*/
|
|
291
|
+
let unknownStatusCodeFallback: StatusCode | undefined;
|
|
292
|
+
|
|
293
|
+
/** injected for the same reason as the fallback: coerceStatusCode needs the table */
|
|
294
|
+
let coerce: ((statusCode: StatusCode | number | string | { value: number }) => StatusCode) | undefined;
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* @internal Called once by status_codes_registry, which is the only module that may
|
|
298
|
+
* import the generated table. Populates the reverse lookup and supplies the two things
|
|
299
|
+
* this module needs from that table.
|
|
300
|
+
*/
|
|
301
|
+
export function _installStatusCodes(
|
|
302
|
+
codes: Record<string, StatusCode>,
|
|
303
|
+
fallback: StatusCode,
|
|
304
|
+
coerceStatusCode: (statusCode: StatusCode | number | string | { value: number }) => StatusCode
|
|
305
|
+
): void {
|
|
306
|
+
for (const name of Object.keys(codes)) {
|
|
307
|
+
const code = codes[name];
|
|
308
|
+
statusCodesReversedMap[code.value.toString()] = code;
|
|
309
|
+
}
|
|
310
|
+
unknownStatusCodeFallback = fallback;
|
|
311
|
+
coerce = coerceStatusCode;
|
|
312
|
+
}
|
|
313
|
+
|
|
266
314
|
/**
|
|
267
315
|
* returns the StatusCode corresponding to the provided value, if any
|
|
268
316
|
* @note: if code is not known , then StatusCodes.Bad will be returned
|
|
@@ -275,7 +323,10 @@ export function getStatusCodeFromCode(code: number): StatusCode {
|
|
|
275
323
|
|
|
276
324
|
/* c8 ignore next */
|
|
277
325
|
if (!sc) {
|
|
278
|
-
|
|
326
|
+
if (!unknownStatusCodeFallback) {
|
|
327
|
+
throw new Error("node-opcua-status-code: the status code table has not been installed yet");
|
|
328
|
+
}
|
|
329
|
+
sc = unknownStatusCodeFallback;
|
|
279
330
|
warnLog(`expecting a known StatusCode but got 0x${codeWithoutInfoBits.toString(16)}`, ` code was 0x${code.toString(16)}`);
|
|
280
331
|
}
|
|
281
332
|
if (infoBits) {
|
|
@@ -377,30 +428,6 @@ export class ModifiableStatusCode extends StatusCode {
|
|
|
377
428
|
Object.defineProperty(ModifiableStatusCode.prototype, "_base", { enumerable: false, writable: true });
|
|
378
429
|
Object.defineProperty(ModifiableStatusCode.prototype, "_extraBits", { enumerable: false, writable: true });
|
|
379
430
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
export function coerceStatusCode(statusCode: StatusCode | number | string | { value: number }): StatusCode {
|
|
385
|
-
if (statusCode instanceof StatusCode) {
|
|
386
|
-
return statusCode;
|
|
387
|
-
}
|
|
388
|
-
if (typeof statusCode === "object" && Object.hasOwn(statusCode, "value")) {
|
|
389
|
-
return getStatusCodeFromCode(statusCode.value);
|
|
390
|
-
}
|
|
391
|
-
if (typeof statusCode === "number") {
|
|
392
|
-
return getStatusCodeFromCode(statusCode);
|
|
393
|
-
}
|
|
394
|
-
const _StatusCodes = StatusCodes as unknown as IndexedStatusCodes;
|
|
395
|
-
if (!_StatusCodes[statusCode as string]) {
|
|
396
|
-
throw new Error(`Cannot find StatusCode ${statusCode}`);
|
|
397
|
-
}
|
|
398
|
-
return _StatusCodes[statusCode as string];
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
for (const name of Object.keys(StatusCodes)) {
|
|
402
|
-
const code = (StatusCodes as unknown as IndexedStatusCodes)[name];
|
|
403
|
-
statusCodesReversedMap[code.value.toString()] = code;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
(StatusCodes as StatusCodesWithMakeStatusCode).makeStatusCode = StatusCode.makeStatusCode;
|
|
431
|
+
// StatusCodes, coerceStatusCode and the table installation now live in
|
|
432
|
+
// status_codes_registry.ts, which is the only module allowed to import the generated
|
|
433
|
+
// table. See the note on unknownStatusCodeFallback above for why.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module node-opcua-status-codes
|
|
3
|
+
*
|
|
4
|
+
* The one module that may import the generated status code table.
|
|
5
|
+
*
|
|
6
|
+
* _generated_status_codes.ts constructs 280 ConstantStatusCode instances while its own
|
|
7
|
+
* module body runs, using the classes declared in opcua_status_code.ts. That makes the
|
|
8
|
+
* dependency one-directional: generated -> opcua_status_code, and nothing may point back.
|
|
9
|
+
*
|
|
10
|
+
* It used to point back. opcua_status_code.ts imported the generated table at the foot of
|
|
11
|
+
* the file, after the class declarations, and iterated it at module scope to build the
|
|
12
|
+
* reverse lookup. That works under CommonJS purely because `require()` runs where it is
|
|
13
|
+
* written. ESM hoists every import to the top and evaluates dependencies first, so the
|
|
14
|
+
* generated module would have run before the classes it needs were initialised, and each
|
|
15
|
+
* of those 280 constructions would have thrown a TDZ ReferenceError on the first
|
|
16
|
+
* `import "node-opcua"`.
|
|
17
|
+
*
|
|
18
|
+
* Inverting it here keeps the graph linear: registry -> generated -> opcua_status_code.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { StatusCodes } from "./_generated_status_codes.js";
|
|
22
|
+
import type { ConstantStatusCode } from "./opcua_status_code.js";
|
|
23
|
+
import { _installStatusCodes, getStatusCodeFromCode, StatusCode } from "./opcua_status_code.js";
|
|
24
|
+
|
|
25
|
+
export { StatusCodes } from "./_generated_status_codes.js";
|
|
26
|
+
|
|
27
|
+
// StatusCodes is a class with one static ConstantStatusCode field per named status, plus
|
|
28
|
+
// makeStatusCode attached below. These types capture that dynamic shape for the casts
|
|
29
|
+
// that index or extend it by string key.
|
|
30
|
+
type IndexedStatusCodes = Record<string, ConstantStatusCode>;
|
|
31
|
+
type StatusCodesWithMakeStatusCode = typeof StatusCodes & { makeStatusCode: typeof StatusCode.makeStatusCode };
|
|
32
|
+
|
|
33
|
+
export function coerceStatusCode(statusCode: StatusCode | number | string | { value: number }): StatusCode {
|
|
34
|
+
if (statusCode instanceof StatusCode) {
|
|
35
|
+
return statusCode;
|
|
36
|
+
}
|
|
37
|
+
if (typeof statusCode === "object" && Object.hasOwn(statusCode, "value")) {
|
|
38
|
+
return getStatusCodeFromCode(statusCode.value);
|
|
39
|
+
}
|
|
40
|
+
if (typeof statusCode === "number") {
|
|
41
|
+
return getStatusCodeFromCode(statusCode);
|
|
42
|
+
}
|
|
43
|
+
const _StatusCodes = StatusCodes as unknown as IndexedStatusCodes;
|
|
44
|
+
if (!_StatusCodes[statusCode as string]) {
|
|
45
|
+
throw new Error(`Cannot find StatusCode ${statusCode}`);
|
|
46
|
+
}
|
|
47
|
+
return _StatusCodes[statusCode as string];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_installStatusCodes(StatusCodes as unknown as IndexedStatusCodes, StatusCodes.Bad, coerceStatusCode);
|
|
51
|
+
|
|
52
|
+
(StatusCodes as StatusCodesWithMakeStatusCode).makeStatusCode = StatusCode.makeStatusCode;
|