node-opcua-variant 2.164.0 → 2.167.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/DataType_enum.d.ts +1 -4
- package/dist/DataType_enum.js +4 -1
- package/dist/DataType_enum.js.map +1 -1
- package/dist/VariantArrayType_enum.d.ts +1 -1
- package/dist/adjust_variant.d.ts +1 -1
- package/dist/adjust_variant.js +5 -8
- package/dist/adjust_variant.js.map +1 -1
- package/dist/cast_variant.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +17 -17
- package/dist/index.js.map +1 -1
- package/dist/variant.d.ts +2 -2
- package/dist/variant.js +59 -61
- package/dist/variant.js.map +1 -1
- package/dist/verify_rank_and_dimension.js +2 -5
- package/dist/verify_rank_and_dimension.js.map +1 -1
- package/package.json +16 -15
- package/source/DataType_enum.ts +4 -2
- package/source/VariantArrayType_enum.ts +1 -1
- package/source/adjust_variant.ts +2 -2
- package/source/cast_variant.ts +0 -2
- package/source/index.ts +5 -4
- package/source/variant.ts +116 -99
- package/source/verify_rank_and_dimension.ts +2 -5
package/source/variant.ts
CHANGED
|
@@ -2,45 +2,45 @@
|
|
|
2
2
|
* @module node-opcua-variant
|
|
3
3
|
*/
|
|
4
4
|
import { assert } from "node-opcua-assert";
|
|
5
|
-
import { NodeId } from "node-opcua-nodeid";
|
|
6
5
|
import {
|
|
7
6
|
coerceInt64,
|
|
8
7
|
coerceUInt64,
|
|
9
|
-
decodeUInt32,
|
|
10
8
|
decodeUInt8,
|
|
11
|
-
|
|
9
|
+
decodeUInt32,
|
|
12
10
|
encodeUInt8,
|
|
11
|
+
encodeUInt32,
|
|
13
12
|
isValidBoolean,
|
|
14
13
|
isValidByteString,
|
|
14
|
+
isValidInt8,
|
|
15
15
|
isValidInt16,
|
|
16
16
|
isValidInt32,
|
|
17
17
|
isValidInt64,
|
|
18
|
-
isValidInt8,
|
|
19
18
|
isValidNodeId,
|
|
19
|
+
isValidUInt8,
|
|
20
20
|
isValidUInt16,
|
|
21
21
|
isValidUInt32,
|
|
22
|
-
isValidUInt64
|
|
23
|
-
isValidUInt8
|
|
22
|
+
isValidUInt64
|
|
24
23
|
} from "node-opcua-basic-types";
|
|
24
|
+
import type { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
|
|
25
25
|
import { LocalizedText, QualifiedName } from "node-opcua-data-model";
|
|
26
|
+
import { make_warningLog } from "node-opcua-debug";
|
|
26
27
|
import {
|
|
27
28
|
BaseUAObject,
|
|
28
29
|
buildStructuredType,
|
|
30
|
+
type DecodeDebugOptions,
|
|
31
|
+
FieldCategory,
|
|
29
32
|
findBuiltInType,
|
|
30
33
|
initialize_field,
|
|
31
34
|
registerSpecialVariantEncoder,
|
|
32
|
-
registerType
|
|
33
|
-
DecodeDebugOptions,
|
|
34
|
-
FieldCategory
|
|
35
|
+
registerType
|
|
35
36
|
} from "node-opcua-factory";
|
|
36
|
-
|
|
37
|
+
import { NodeId } from "node-opcua-nodeid";
|
|
37
38
|
import { isNullOrUndefined } from "node-opcua-utils";
|
|
38
|
-
import {
|
|
39
|
+
import { DataType } from "./DataType_enum";
|
|
40
|
+
import { VariantArrayType } from "./VariantArrayType_enum";
|
|
39
41
|
|
|
40
|
-
import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
|
|
41
|
-
import { _enumerationDataType, DataType } from "./DataType_enum";
|
|
42
|
-
import { _enumerationVariantArrayType, VariantArrayType } from "./VariantArrayType_enum";
|
|
43
42
|
export { VariantArrayType };
|
|
43
|
+
|
|
44
44
|
// tslint:disable:no-bitwise
|
|
45
45
|
|
|
46
46
|
const warningLog = make_warningLog(__filename);
|
|
@@ -84,6 +84,7 @@ function _coerceVariant(variantLike: VariantOptions | Variant): Variant {
|
|
|
84
84
|
export interface VariantOptions {
|
|
85
85
|
dataType?: DataType | string;
|
|
86
86
|
arrayType?: VariantArrayType | string;
|
|
87
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentionally using any here
|
|
87
88
|
value?: any;
|
|
88
89
|
dimensions?: number[] | null;
|
|
89
90
|
}
|
|
@@ -91,6 +92,7 @@ export interface VariantOptions {
|
|
|
91
92
|
export interface VariantOptions2 {
|
|
92
93
|
dataType: DataType;
|
|
93
94
|
arrayType: VariantArrayType;
|
|
95
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentionally using any here
|
|
94
96
|
value: any;
|
|
95
97
|
dimensions: number[] | null;
|
|
96
98
|
}
|
|
@@ -104,6 +106,7 @@ export class Variant extends BaseUAObject {
|
|
|
104
106
|
public static computer_default_value = (): Variant => new Variant({ dataType: DataType.Null });
|
|
105
107
|
public dataType: DataType;
|
|
106
108
|
public arrayType: VariantArrayType;
|
|
109
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentionally using any here
|
|
107
110
|
public value: any;
|
|
108
111
|
public dimensions: number[] | null;
|
|
109
112
|
|
|
@@ -180,38 +183,42 @@ Variant.prototype.schema = schemaVariant;
|
|
|
180
183
|
|
|
181
184
|
export type VariantLike = VariantOptions;
|
|
182
185
|
|
|
183
|
-
function variantToString(self: Variant,
|
|
184
|
-
function
|
|
186
|
+
function variantToString(self: Variant, _options?: Record<string, unknown>) {
|
|
187
|
+
function asString(value: unknown): string {
|
|
185
188
|
switch (self.dataType) {
|
|
186
189
|
case DataType.ByteString:
|
|
187
|
-
return value ?
|
|
190
|
+
return value ? `0x${(value as Buffer).toString("hex")}` : "<null>";
|
|
188
191
|
case DataType.NodeId:
|
|
189
|
-
return value instanceof NodeId ?
|
|
192
|
+
return value instanceof NodeId ? value.displayText() : value ? String(value) : "";
|
|
190
193
|
case DataType.Boolean:
|
|
191
|
-
return value
|
|
194
|
+
return String(value);
|
|
192
195
|
case DataType.DateTime:
|
|
193
|
-
return value
|
|
196
|
+
return value
|
|
197
|
+
? typeof (value as Date).toISOString === "function"
|
|
198
|
+
? (value as Date).toISOString()
|
|
199
|
+
: String(value)
|
|
200
|
+
: "<null>";
|
|
194
201
|
default:
|
|
195
|
-
return value ? value
|
|
202
|
+
return value ? String(value) : "0";
|
|
196
203
|
}
|
|
197
204
|
}
|
|
198
205
|
|
|
199
|
-
function f(value:
|
|
206
|
+
function f(value: unknown) {
|
|
200
207
|
if (value === undefined || (value === null && typeof value === "object")) {
|
|
201
208
|
return "<null>";
|
|
202
209
|
}
|
|
203
|
-
return
|
|
210
|
+
return asString(value);
|
|
204
211
|
}
|
|
205
212
|
|
|
206
213
|
let data = VariantArrayType[self.arrayType];
|
|
207
214
|
|
|
208
215
|
if (self.dimensions && self.dimensions.length >= 0) {
|
|
209
|
-
data +=
|
|
216
|
+
data += `[ ${self.dimensions.join(",")} ]`;
|
|
210
217
|
}
|
|
211
218
|
|
|
212
|
-
data +=
|
|
219
|
+
data += `<${DataType[self.dataType]}>`;
|
|
213
220
|
if (self.arrayType === VariantArrayType.Scalar) {
|
|
214
|
-
data +=
|
|
221
|
+
data += `, value: ${f(self.value)}`;
|
|
215
222
|
} else if (self.arrayType === VariantArrayType.Array || self.arrayType === VariantArrayType.Matrix) {
|
|
216
223
|
if (!self.value) {
|
|
217
224
|
data += ", null";
|
|
@@ -224,10 +231,10 @@ function variantToString(self: Variant, options?: any) {
|
|
|
224
231
|
if (self.value.length > 10) {
|
|
225
232
|
a.push("...");
|
|
226
233
|
}
|
|
227
|
-
data +=
|
|
234
|
+
data += `, l= ${self.value.length}, value=[${a.map(f).join(",")}]`;
|
|
228
235
|
}
|
|
229
236
|
}
|
|
230
|
-
return
|
|
237
|
+
return `Variant(${data})`;
|
|
231
238
|
}
|
|
232
239
|
|
|
233
240
|
/***
|
|
@@ -275,12 +282,17 @@ export function encodeVariant(variant: Variant | undefined | null, stream: Outpu
|
|
|
275
282
|
}
|
|
276
283
|
|
|
277
284
|
/* c8 ignore start */
|
|
278
|
-
function _decodeVariantArrayDebug(
|
|
285
|
+
function _decodeVariantArrayDebug(
|
|
286
|
+
stream: BinaryStream,
|
|
287
|
+
decode: (stream: BinaryStream) => unknown,
|
|
288
|
+
tracer: { trace: (...args: unknown[]) => void; dump: (...args: unknown[]) => void },
|
|
289
|
+
dataType: DataType
|
|
290
|
+
) {
|
|
279
291
|
let cursorBefore = stream.length;
|
|
280
292
|
const length = decodeUInt32(stream);
|
|
281
293
|
|
|
282
|
-
let i;
|
|
283
|
-
let element;
|
|
294
|
+
let i: number = 0;
|
|
295
|
+
let element: unknown;
|
|
284
296
|
tracer.trace("start_array", "Variant", -1, cursorBefore, stream.length);
|
|
285
297
|
if (length === 0xffffffff) {
|
|
286
298
|
// empty array
|
|
@@ -329,7 +341,7 @@ function decodeDebugVariant(self: Variant, stream: BinaryStream, options: Decode
|
|
|
329
341
|
|
|
330
342
|
/* c8 ignore next */
|
|
331
343
|
if (!decode) {
|
|
332
|
-
throw new Error(
|
|
344
|
+
throw new Error(`Variant.decode : cannot find decoder for type ${DataType[self.dataType]}`);
|
|
333
345
|
}
|
|
334
346
|
|
|
335
347
|
const cursorBefore = stream.length;
|
|
@@ -353,7 +365,7 @@ function decodeDebugVariant(self: Variant, stream: BinaryStream, options: Decode
|
|
|
353
365
|
// stop and raise a BadDecodingError.
|
|
354
366
|
if (hasDimension) {
|
|
355
367
|
self.dimensions = decodeDimension(stream);
|
|
356
|
-
const
|
|
368
|
+
const _verification = calculate_product(self.dimensions);
|
|
357
369
|
}
|
|
358
370
|
}
|
|
359
371
|
/* c8 ignore stop */
|
|
@@ -407,13 +419,13 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
|
|
|
407
419
|
|
|
408
420
|
if (opts.dataType === DataType.ExtensionObject) {
|
|
409
421
|
if (opts.arrayType === VariantArrayType.Scalar) {
|
|
410
|
-
if (opts
|
|
422
|
+
if (opts?.value?.constructor) {
|
|
411
423
|
opts.value = new opts.value.constructor(opts.value);
|
|
412
424
|
}
|
|
413
425
|
} else {
|
|
414
426
|
if (opts.value) {
|
|
415
|
-
opts.value = opts.value.map((e:
|
|
416
|
-
if (e
|
|
427
|
+
opts.value = opts.value.map((e: { constructor: new (e: unknown) => unknown } | null) => {
|
|
428
|
+
if (e?.constructor) {
|
|
417
429
|
return new e.constructor(e);
|
|
418
430
|
}
|
|
419
431
|
return null;
|
|
@@ -432,7 +444,7 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
|
|
|
432
444
|
const d = findBuiltInType(options.dataType);
|
|
433
445
|
/* c8 ignore start */
|
|
434
446
|
if (!d) {
|
|
435
|
-
throw new Error(
|
|
447
|
+
throw new Error(`Cannot find Built-In data type or any DataType resolving to ${options.dataType}`);
|
|
436
448
|
}
|
|
437
449
|
/* c8 ignore stop */
|
|
438
450
|
options.dataType = DataType[d.name as keyof typeof DataType];
|
|
@@ -440,10 +452,12 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
|
|
|
440
452
|
|
|
441
453
|
// array type could be a string
|
|
442
454
|
if (typeof options.arrayType === "string") {
|
|
443
|
-
const at: VariantArrayType | undefined = (VariantArrayType as
|
|
455
|
+
const at: VariantArrayType | undefined = (VariantArrayType as unknown as Record<string, VariantArrayType | undefined>)[
|
|
456
|
+
options.arrayType
|
|
457
|
+
];
|
|
444
458
|
/* c8 ignore start */
|
|
445
459
|
if (at === undefined) {
|
|
446
|
-
throw new Error(
|
|
460
|
+
throw new Error(`ArrayType: invalid ${options.arrayType}`);
|
|
447
461
|
}
|
|
448
462
|
/* c8 ignore stop */
|
|
449
463
|
options.arrayType = at;
|
|
@@ -459,9 +473,8 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
|
|
|
459
473
|
"Variant#constructor : when using UInt64 ou Int64" +
|
|
460
474
|
" arrayType must be specified , as automatic detection cannot be made"
|
|
461
475
|
);
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
else {
|
|
476
|
+
} else {
|
|
477
|
+
/* c8 ignore stop */
|
|
465
478
|
options.arrayType = VariantArrayType.Array;
|
|
466
479
|
isArrayTypeUnspecified = false;
|
|
467
480
|
}
|
|
@@ -473,9 +486,8 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
|
|
|
473
486
|
const value1 = coerceVariantArray(options.dataType, options.value);
|
|
474
487
|
assert(value1 === null || value1 !== options.value);
|
|
475
488
|
options.value = value1;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
else {
|
|
489
|
+
} else {
|
|
490
|
+
/* c8 ignore start */
|
|
479
491
|
assert(options.arrayType === VariantArrayType.Matrix);
|
|
480
492
|
options.value = options.value || [];
|
|
481
493
|
|
|
@@ -484,14 +496,9 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
|
|
|
484
496
|
if (!options.dimensions) {
|
|
485
497
|
throw new Error("Matrix Variant : missing dimensions");
|
|
486
498
|
}
|
|
487
|
-
if (options.value.length
|
|
499
|
+
if (options.value.length !== 0 && options.value.length !== calculate_product(options.dimensions)) {
|
|
488
500
|
throw new Error(
|
|
489
|
-
|
|
490
|
-
options.value.length +
|
|
491
|
-
"!=" +
|
|
492
|
-
calculate_product(options.dimensions) +
|
|
493
|
-
" => " +
|
|
494
|
-
JSON.stringify(options.dimensions)
|
|
501
|
+
`Matrix Variant : invalid value size = options.value.length ${options.value.length} != ${calculate_product(options.dimensions)} => ${JSON.stringify(options.dimensions)}`
|
|
495
502
|
);
|
|
496
503
|
}
|
|
497
504
|
}
|
|
@@ -499,22 +506,14 @@ function constructHook(options: VariantOptions | Variant): VariantOptions2 {
|
|
|
499
506
|
} else {
|
|
500
507
|
assert(options.arrayType === VariantArrayType.Scalar || options.arrayType === undefined);
|
|
501
508
|
options.arrayType = VariantArrayType.Scalar;
|
|
502
|
-
const
|
|
509
|
+
const _tmp = options.value;
|
|
503
510
|
// scalar
|
|
504
511
|
options.value = coerceVariantType(options.dataType, options.value);
|
|
505
512
|
|
|
506
513
|
/* c8 ignore start */
|
|
507
514
|
if (!isValidVariant(options.arrayType, options.dataType, options.value, null)) {
|
|
508
515
|
throw new Error(
|
|
509
|
-
|
|
510
|
-
VariantArrayType[options.arrayType] +
|
|
511
|
-
" dataType: " +
|
|
512
|
-
DataType[options.dataType] +
|
|
513
|
-
" value:" +
|
|
514
|
-
options.value +
|
|
515
|
-
" (javascript type = " +
|
|
516
|
-
typeof options.value +
|
|
517
|
-
" )"
|
|
516
|
+
`Invalid variant arrayType: ${VariantArrayType[options.arrayType]} dataType: ${DataType[options.dataType]} value:${options.value} (javascript type = ${typeof options.value} )`
|
|
518
517
|
);
|
|
519
518
|
}
|
|
520
519
|
/* c8 ignore stop */
|
|
@@ -537,13 +536,13 @@ function get_encoder(dataType: DataType) {
|
|
|
537
536
|
const dataTypeAsString = typeof dataType === "string" ? dataType : DataType[dataType];
|
|
538
537
|
/* c8 ignore start */
|
|
539
538
|
if (!dataTypeAsString) {
|
|
540
|
-
throw new Error(
|
|
539
|
+
throw new Error(`invalid dataType ${dataType}`);
|
|
541
540
|
}
|
|
542
541
|
/* c8 ignore stop */
|
|
543
542
|
const encode = findBuiltInType(dataTypeAsString).encode;
|
|
544
543
|
/* c8 ignore start */
|
|
545
544
|
if (!encode) {
|
|
546
|
-
throw new Error(
|
|
545
|
+
throw new Error(`Cannot find encode function for dataType ${dataTypeAsString}`);
|
|
547
546
|
}
|
|
548
547
|
/* c8 ignore stop */
|
|
549
548
|
return encode;
|
|
@@ -554,7 +553,7 @@ function get_decoder(dataType: DataType) {
|
|
|
554
553
|
const decode = findBuiltInType(dataTypeAsString).decode;
|
|
555
554
|
/* c8 ignore start */
|
|
556
555
|
if (!decode) {
|
|
557
|
-
throw new Error(
|
|
556
|
+
throw new Error(`Variant.decode : cannot find decoder for type ${dataTypeAsString}`);
|
|
558
557
|
}
|
|
559
558
|
/* c8 ignore stop */
|
|
560
559
|
return decode;
|
|
@@ -577,10 +576,15 @@ export type BufferedArray2 =
|
|
|
577
576
|
|
|
578
577
|
interface BufferedArrayConstructor {
|
|
579
578
|
BYTES_PER_ELEMENT: number;
|
|
580
|
-
new(
|
|
579
|
+
new(length: number): BufferedArray2;
|
|
580
|
+
new(buffer: ArrayBufferLike, byteOffset?: number, length?: number): BufferedArray2;
|
|
581
581
|
}
|
|
582
582
|
|
|
583
|
-
function convertTo(
|
|
583
|
+
function convertTo(
|
|
584
|
+
dataType: DataType,
|
|
585
|
+
arrayTypeConstructor: BufferedArrayConstructor | null,
|
|
586
|
+
value: BufferedArray2 | unknown[] | null
|
|
587
|
+
) {
|
|
584
588
|
// c8 ignore next
|
|
585
589
|
if (value === undefined || value === null) {
|
|
586
590
|
return null;
|
|
@@ -597,18 +601,18 @@ function convertTo(dataType: DataType, arrayTypeConstructor: BufferedArrayConstr
|
|
|
597
601
|
const coerceFunc = coerceVariantType.bind(null, dataType);
|
|
598
602
|
const n = value.length;
|
|
599
603
|
|
|
600
|
-
const newArr:
|
|
604
|
+
const newArr: BufferedArray2 | unknown[] = arrayTypeConstructor ? new arrayTypeConstructor(n) : new Array(n);
|
|
601
605
|
for (let i = 0; i < n; i++) {
|
|
602
606
|
newArr[i] = coerceFunc(value[i]);
|
|
603
607
|
}
|
|
604
608
|
// c8 ignore next
|
|
605
609
|
if (arrayTypeConstructor && displayWarning && n > 10) {
|
|
606
|
-
warningLog(
|
|
610
|
+
warningLog(`Warning ! an array containing ${DataType[dataType]} elements has been provided as a generic array. `);
|
|
607
611
|
warningLog(
|
|
608
|
-
" This is inefficient as every array value will
|
|
612
|
+
" This is inefficient as every array value will have to be coerced and verified against the expected type"
|
|
609
613
|
);
|
|
610
614
|
warningLog(
|
|
611
|
-
" It is highly recommended that you use a
|
|
615
|
+
" It is highly recommended that you use a typed array ",
|
|
612
616
|
arrayTypeConstructor.constructor.name,
|
|
613
617
|
" instead"
|
|
614
618
|
);
|
|
@@ -617,9 +621,9 @@ function convertTo(dataType: DataType, arrayTypeConstructor: BufferedArrayConstr
|
|
|
617
621
|
}
|
|
618
622
|
|
|
619
623
|
interface DataTypeHelper {
|
|
620
|
-
coerce: (value:
|
|
621
|
-
encode: (stream: OutputBinaryStream, value:
|
|
622
|
-
decode: (stream: BinaryStream) =>
|
|
624
|
+
coerce: (value: BufferedArray2 | unknown[] | null) => BufferedArray2 | unknown[] | null;
|
|
625
|
+
encode: (stream: OutputBinaryStream, value: unknown) => void;
|
|
626
|
+
decode: (stream: BinaryStream) => unknown;
|
|
623
627
|
}
|
|
624
628
|
|
|
625
629
|
const typedArrayHelpers: { [key: string]: DataTypeHelper } = {};
|
|
@@ -628,7 +632,7 @@ function _getHelper(dataType: DataType) {
|
|
|
628
632
|
return typedArrayHelpers[DataType[dataType]];
|
|
629
633
|
}
|
|
630
634
|
|
|
631
|
-
function coerceVariantArray(dataType: DataType, value:
|
|
635
|
+
function coerceVariantArray(dataType: DataType, value: BufferedArray2 | unknown[] | null) {
|
|
632
636
|
const helper = _getHelper(dataType);
|
|
633
637
|
if (helper) {
|
|
634
638
|
return helper.coerce(value);
|
|
@@ -637,14 +641,15 @@ function coerceVariantArray(dataType: DataType, value: any) {
|
|
|
637
641
|
}
|
|
638
642
|
}
|
|
639
643
|
|
|
640
|
-
function encodeTypedArray(arrayTypeConstructor: BufferedArrayConstructor, stream: OutputBinaryStream, value:
|
|
641
|
-
|
|
642
|
-
assert(
|
|
643
|
-
|
|
644
|
-
|
|
644
|
+
function encodeTypedArray(arrayTypeConstructor: BufferedArrayConstructor, stream: OutputBinaryStream, value: unknown) {
|
|
645
|
+
const typedValue = value as BufferedArray2;
|
|
646
|
+
assert(typedValue instanceof arrayTypeConstructor);
|
|
647
|
+
assert(typedValue.buffer instanceof ArrayBuffer);
|
|
648
|
+
encodeUInt32(typedValue.length, stream);
|
|
649
|
+
stream.writeArrayBuffer(typedValue.buffer as ArrayBuffer, typedValue.byteOffset, typedValue.byteLength);
|
|
645
650
|
}
|
|
646
651
|
|
|
647
|
-
function encodeGeneralArray(dataType: DataType, stream: OutputBinaryStream, value:
|
|
652
|
+
function encodeGeneralArray(dataType: DataType, stream: OutputBinaryStream, value: unknown[] | null) {
|
|
648
653
|
if (!value) {
|
|
649
654
|
assert(value === null);
|
|
650
655
|
encodeUInt32(0xffffffff, stream);
|
|
@@ -657,11 +662,11 @@ function encodeGeneralArray(dataType: DataType, stream: OutputBinaryStream, valu
|
|
|
657
662
|
}
|
|
658
663
|
}
|
|
659
664
|
|
|
660
|
-
function encodeVariantArray(dataType: DataType, stream: OutputBinaryStream, value:
|
|
661
|
-
if (value && value.buffer) {
|
|
665
|
+
function encodeVariantArray(dataType: DataType, stream: OutputBinaryStream, value: BufferedArray2 | unknown[] | null) {
|
|
666
|
+
if (value && (value as BufferedArray2).buffer) {
|
|
662
667
|
return _getHelper(dataType).encode(stream, value);
|
|
663
668
|
}
|
|
664
|
-
return encodeGeneralArray(dataType, stream, value);
|
|
669
|
+
return encodeGeneralArray(dataType, stream, value as unknown[] | null);
|
|
665
670
|
}
|
|
666
671
|
|
|
667
672
|
function decodeTypedArray(arrayTypeConstructor: BufferedArrayConstructor, stream: BinaryStream) {
|
|
@@ -713,7 +718,7 @@ function decodeVariantArray(dataType: DataType, stream: BinaryStream) {
|
|
|
713
718
|
}
|
|
714
719
|
}
|
|
715
720
|
|
|
716
|
-
function _declareTypeArrayHelper(dataType: DataType, typedArrayConstructor:
|
|
721
|
+
function _declareTypeArrayHelper(dataType: DataType, typedArrayConstructor: BufferedArrayConstructor) {
|
|
717
722
|
typedArrayHelpers[DataType[dataType]] = {
|
|
718
723
|
coerce: convertTo.bind(null, dataType, typedArrayConstructor),
|
|
719
724
|
decode: decodeTypedArray.bind(null, typedArrayConstructor),
|
|
@@ -738,15 +743,16 @@ function encodeDimension(dimensions: number[], stream: OutputBinaryStream) {
|
|
|
738
743
|
return encodeGeneralArray(DataType.UInt32, stream, dimensions);
|
|
739
744
|
}
|
|
740
745
|
|
|
741
|
-
function isEnumerationItem(value:
|
|
746
|
+
function isEnumerationItem(value: unknown): boolean {
|
|
742
747
|
return (
|
|
743
748
|
value instanceof Object &&
|
|
744
|
-
Object.
|
|
745
|
-
Object.
|
|
749
|
+
Object.hasOwn(value, "value") &&
|
|
750
|
+
Object.hasOwn(value, "key") &&
|
|
746
751
|
value.constructor.name === "EnumValueType"
|
|
747
752
|
);
|
|
748
753
|
}
|
|
749
754
|
|
|
755
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentional
|
|
750
756
|
export function coerceVariantType(dataType: DataType, value: undefined | any): any {
|
|
751
757
|
/* eslint max-statements: ["error",1000], complexity: ["error",1000]*/
|
|
752
758
|
if (value === undefined) {
|
|
@@ -758,9 +764,7 @@ export function coerceVariantType(dataType: DataType, value: undefined | any): a
|
|
|
758
764
|
// [...]Enumeration are always encoded as Int32 on the wire [...]
|
|
759
765
|
|
|
760
766
|
if (dataType !== DataType.Int32 && dataType !== DataType.ExtensionObject) {
|
|
761
|
-
throw new Error(
|
|
762
|
-
"expecting DataType.Int32 for enumeration values ;" + " got DataType." + dataType.toString() + " instead"
|
|
763
|
-
);
|
|
767
|
+
throw new Error(`expecting DataType.Int32 for enumeration values ; got DataType.${dataType.toString()} instead`);
|
|
764
768
|
}
|
|
765
769
|
}
|
|
766
770
|
/* c8 ignore stop */
|
|
@@ -788,8 +792,8 @@ export function coerceVariantType(dataType: DataType, value: undefined | any): a
|
|
|
788
792
|
assert(value !== undefined);
|
|
789
793
|
value = parseInt(value, 10);
|
|
790
794
|
/* c8 ignore start */
|
|
791
|
-
if (!isFinite(value)) {
|
|
792
|
-
throw new Error(
|
|
795
|
+
if (!Number.isFinite(value)) {
|
|
796
|
+
throw new Error(`expecting a number ${value}`);
|
|
793
797
|
}
|
|
794
798
|
/* c8 ignore stop */
|
|
795
799
|
break;
|
|
@@ -820,19 +824,18 @@ export function coerceVariantType(dataType: DataType, value: undefined | any): a
|
|
|
820
824
|
default:
|
|
821
825
|
assert(dataType !== undefined && dataType !== null, "Invalid DataType");
|
|
822
826
|
break;
|
|
823
|
-
case DataType.NodeId:
|
|
824
|
-
break;
|
|
825
827
|
}
|
|
826
828
|
return value;
|
|
827
829
|
}
|
|
828
830
|
|
|
831
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentionally using an y here
|
|
829
832
|
function isValidScalarVariant(dataType: DataType, value: any): boolean {
|
|
830
833
|
assert(
|
|
831
834
|
value === null ||
|
|
832
835
|
DataType.Int64 === dataType ||
|
|
833
836
|
DataType.ByteString === dataType ||
|
|
834
837
|
DataType.UInt64 === dataType ||
|
|
835
|
-
!(value
|
|
838
|
+
!Array.isArray(value)
|
|
836
839
|
);
|
|
837
840
|
assert(value === null || !(value instanceof Int32Array));
|
|
838
841
|
assert(value === null || !(value instanceof Uint32Array));
|
|
@@ -866,6 +869,7 @@ function isValidScalarVariant(dataType: DataType, value: any): boolean {
|
|
|
866
869
|
}
|
|
867
870
|
}
|
|
868
871
|
|
|
872
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentional - polymorphic Variant value
|
|
869
873
|
function isValidArrayVariant(dataType: DataType, value: any): boolean {
|
|
870
874
|
if (value === null) {
|
|
871
875
|
return true;
|
|
@@ -898,6 +902,7 @@ function isValidArrayVariant(dataType: DataType, value: any): boolean {
|
|
|
898
902
|
}
|
|
899
903
|
|
|
900
904
|
/* c8 ignore start */
|
|
905
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentional - polymorphic Variant value
|
|
901
906
|
function isValidMatrixVariant(dataType: DataType, value: any, dimensions: number[] | null) {
|
|
902
907
|
if (!dimensions) {
|
|
903
908
|
return false;
|
|
@@ -922,7 +927,7 @@ export function isValidVariant(
|
|
|
922
927
|
return isValidArrayVariant(dataType, value);
|
|
923
928
|
default:
|
|
924
929
|
assert(arrayType === VariantArrayType.Matrix);
|
|
925
|
-
return isValidMatrixVariant(dataType, value, dimensions
|
|
930
|
+
return isValidMatrixVariant(dataType, value, dimensions ?? null);
|
|
926
931
|
}
|
|
927
932
|
}
|
|
928
933
|
|
|
@@ -931,7 +936,16 @@ export function buildVariantArray(
|
|
|
931
936
|
nbElements: number,
|
|
932
937
|
defaultValue: unknown
|
|
933
938
|
): Float32Array | Float64Array | Uint32Array | Int32Array | Uint16Array | Int16Array | Uint8Array | Int8Array | Array<unknown> {
|
|
934
|
-
let value
|
|
939
|
+
let value:
|
|
940
|
+
| Float32Array
|
|
941
|
+
| Float64Array
|
|
942
|
+
| Uint32Array
|
|
943
|
+
| Int32Array
|
|
944
|
+
| Uint16Array
|
|
945
|
+
| Int16Array
|
|
946
|
+
| Uint8Array
|
|
947
|
+
| Int8Array
|
|
948
|
+
| Array<unknown>;
|
|
935
949
|
switch (dataType) {
|
|
936
950
|
case DataType.Float:
|
|
937
951
|
value = new Float32Array(nbElements);
|
|
@@ -972,9 +986,11 @@ export function buildVariantArray(
|
|
|
972
986
|
const oldNodeVersion =
|
|
973
987
|
typeof process === "object" && process.versions && process.versions.node && process.versions.node.substring(0, 1) === "0";
|
|
974
988
|
|
|
989
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentional - deep equality comparison of polymorphic values
|
|
975
990
|
function __type(a: any): string {
|
|
976
991
|
return Object.prototype.toString.call(a);
|
|
977
992
|
}
|
|
993
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentional - deep equality comparison of polymorphic values
|
|
978
994
|
function __check_same_object(o1: any, o2: any): boolean {
|
|
979
995
|
if (o1 === o2) return true;
|
|
980
996
|
if ((!o1 && o2) || (!o2 && o1)) return false;
|
|
@@ -1020,11 +1036,12 @@ function __check_same_object(o1: any, o2: any): boolean {
|
|
|
1020
1036
|
case "[object BigInt]":
|
|
1021
1037
|
return o1 === o2;
|
|
1022
1038
|
case "[object Date]":
|
|
1023
|
-
return o1.getTime()
|
|
1039
|
+
return o1.getTime() === o2.getTime();
|
|
1024
1040
|
default:
|
|
1025
1041
|
return o1 === o2;
|
|
1026
1042
|
}
|
|
1027
1043
|
}
|
|
1044
|
+
// biome-ignore lint/suspicious/noExplicitAny: intentional - deep equality comparison of polymorphic values
|
|
1028
1045
|
function __check_same_array(arr1: any, arr2: any) {
|
|
1029
1046
|
if (!arr1 || !arr2) {
|
|
1030
1047
|
return !arr1 && !arr2;
|
|
@@ -33,17 +33,14 @@ export function verifyRankAndDimensions(options: { valueRank?: number; arrayDime
|
|
|
33
33
|
}
|
|
34
34
|
// c8 ignore start
|
|
35
35
|
if (!options.arrayDimensions && options.valueRank > 0) {
|
|
36
|
-
throw new Error(
|
|
36
|
+
throw new Error(`[CONFORMANCE] arrayDimension must be specified if valueRank >0 ${options.valueRank}`);
|
|
37
37
|
}
|
|
38
38
|
// c8 ignore stop
|
|
39
39
|
|
|
40
40
|
// c8 ignore start
|
|
41
41
|
if (options.valueRank > 0 && options.arrayDimensions!.length !== options.valueRank) {
|
|
42
42
|
throw new Error(
|
|
43
|
-
|
|
44
|
-
options.valueRank +
|
|
45
|
-
" whereas arrayDimensions.length =" +
|
|
46
|
-
options.arrayDimensions!.length
|
|
43
|
+
`[CONFORMANCE] when valueRank> 0, arrayDimensions must have valueRank elements, this.valueRank =${options.valueRank} whereas arrayDimensions.length =${options.arrayDimensions?.length}`
|
|
47
44
|
);
|
|
48
45
|
}
|
|
49
46
|
// c8 ignore stop
|