node-opcua-factory 2.72.1 → 2.74.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 (75) hide show
  1. package/LICENSE +3 -1
  2. package/dist/base_ua_object.d.ts +53 -0
  3. package/dist/base_ua_object.js +536 -0
  4. package/dist/base_ua_object.js.map +1 -0
  5. package/dist/basic_type.d.ts +40 -0
  6. package/dist/basic_type.js +119 -0
  7. package/dist/basic_type.js.map +1 -0
  8. package/dist/builtin_types.d.ts +46 -0
  9. package/dist/builtin_types.js +284 -0
  10. package/dist/builtin_types.js.map +1 -0
  11. package/dist/builtin_types_special.d.ts +5 -0
  12. package/dist/builtin_types_special.js +46 -0
  13. package/dist/builtin_types_special.js.map +1 -0
  14. package/dist/datatype_factory.d.ts +8 -11
  15. package/dist/datatype_factory.js +25 -42
  16. package/dist/datatype_factory.js.map +1 -1
  17. package/dist/encode_decode.d.ts +9 -0
  18. package/dist/encode_decode.js +40 -0
  19. package/dist/encode_decode.js.map +1 -0
  20. package/dist/enumerations.d.ts +32 -0
  21. package/dist/enumerations.js +78 -0
  22. package/dist/enumerations.js.map +1 -0
  23. package/dist/get_built_in_type.d.ts +0 -0
  24. package/dist/get_built_in_type.js +2 -0
  25. package/dist/get_built_in_type.js.map +1 -0
  26. package/dist/get_standard_data_type_factory.d.ts +12 -0
  27. package/dist/get_standard_data_type_factory.js +35 -0
  28. package/dist/get_standard_data_type_factory.js.map +1 -0
  29. package/dist/get_structured_type_schema.d.ts +4 -0
  30. package/dist/get_structured_type_schema.js +13 -0
  31. package/dist/get_structured_type_schema.js.map +1 -0
  32. package/dist/id_generator.d.ts +3 -0
  33. package/dist/id_generator.js +22 -0
  34. package/dist/id_generator.js.map +1 -0
  35. package/dist/index.d.ts +13 -10
  36. package/dist/index.js +13 -10
  37. package/dist/index.js.map +1 -1
  38. package/dist/nodeid_type.d.ts +13 -0
  39. package/dist/nodeid_type.js +19 -0
  40. package/dist/nodeid_type.js.map +1 -0
  41. package/dist/parameters.d.ts +3 -0
  42. package/dist/parameters.js +7 -0
  43. package/dist/parameters.js.map +1 -0
  44. package/dist/register_class_definition.d.ts +3 -0
  45. package/dist/register_class_definition.js +9 -0
  46. package/dist/register_class_definition.js.map +1 -0
  47. package/dist/schema_helpers.d.ts +24 -0
  48. package/dist/schema_helpers.js +100 -0
  49. package/dist/schema_helpers.js.map +1 -0
  50. package/dist/structured_type_schema.d.ts +44 -0
  51. package/dist/structured_type_schema.js +279 -0
  52. package/dist/structured_type_schema.js.map +1 -0
  53. package/dist/types.d.ts +86 -45
  54. package/dist/types.js +1 -43
  55. package/dist/types.js.map +1 -1
  56. package/package.json +13 -12
  57. package/source/{factories_baseobject.ts → base_ua_object.ts} +48 -44
  58. package/source/{factories_basic_type.ts → basic_type.ts} +7 -28
  59. package/source/{factories_builtin_types.ts → builtin_types.ts} +111 -89
  60. package/source/{factories_builtin_types_special.ts → builtin_types_special.ts} +2 -2
  61. package/source/datatype_factory.ts +44 -47
  62. package/source/encode_decode.ts +37 -0
  63. package/source/{factories_enumerations.ts → enumerations.ts} +6 -4
  64. package/source/get_built_in_type.ts +0 -0
  65. package/source/{factories_factories.ts → get_standard_data_type_factory.ts} +7 -25
  66. package/source/get_structured_type_schema.ts +10 -0
  67. package/source/{factories_id_generator.ts → id_generator.ts} +0 -0
  68. package/source/index.ts +14 -11
  69. package/source/nodeid_type.ts +17 -0
  70. package/source/parameters.ts +3 -0
  71. package/source/register_class_definition.ts +11 -0
  72. package/source/{factories_schema_helpers.ts → schema_helpers.ts} +15 -37
  73. package/source/{factories_structuredTypeSchema.ts → structured_type_schema.ts} +37 -36
  74. package/source/types.ts +104 -79
  75. package/source/constructor_type.ts +0 -18
@@ -13,13 +13,11 @@ import {
13
13
  coerceInt16,
14
14
  coerceInt32,
15
15
  coerceInt64,
16
- coerceInt8,
17
16
  coerceNodeId,
18
17
  coerceSByte,
19
18
  coerceUInt16,
20
19
  coerceUInt32,
21
20
  coerceUInt64,
22
- coerceUInt8,
23
21
  decodeBoolean,
24
22
  decodeByte,
25
23
  decodeByteString,
@@ -31,14 +29,12 @@ import {
31
29
  decodeInt16,
32
30
  decodeInt32,
33
31
  decodeInt64,
34
- decodeInt8,
35
32
  decodeNodeId,
36
33
  decodeSByte,
37
34
  decodeString,
38
35
  decodeUInt16,
39
36
  decodeUInt32,
40
37
  decodeUInt64,
41
- decodeUInt8,
42
38
  encodeBoolean,
43
39
  encodeByte,
44
40
  encodeByteString,
@@ -50,70 +46,106 @@ import {
50
46
  encodeInt16,
51
47
  encodeInt32,
52
48
  encodeInt64,
53
- encodeInt8,
54
49
  encodeNodeId,
55
50
  encodeSByte,
56
51
  encodeString,
57
52
  encodeUInt16,
58
53
  encodeUInt32,
59
54
  encodeUInt64,
60
- encodeUInt8
55
+ minDate
61
56
  } from "node-opcua-basic-types";
62
57
  import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
58
+ import { DataTypeIds } from "node-opcua-constants";
59
+
63
60
  import { emptyGuid } from "node-opcua-guid";
64
61
  import { makeExpandedNodeId, makeNodeId } from "node-opcua-nodeid";
65
62
  import { coerceStatusCode, decodeStatusCode, encodeStatusCode, StatusCodes } from "node-opcua-status-code";
66
- import { BasicTypeDefinition, BasicTypeDefinitionOptions, FieldCategory, TypeSchemaBase } from "./types";
63
+ import { defaultEncode, defaultDecode, decodeNull, encodeNull, decodeAny, encodeAny, toJSONGuid } from "./encode_decode";
64
+ import { BasicTypeDefinition, BasicTypeDefinitionOptions, BasicTypeDefinitionOptionsBase, CommonInterface, FieldCategory, TypeSchemaConstructorOptions } from "./types";
65
+
67
66
 
68
- // eslint-disable-next-line @typescript-eslint/no-empty-function
69
- function defaultEncode(value: any, stream: OutputBinaryStream): void {}
67
+ /**
68
+ * @class TypeSchemaBase
69
+ * @param options {Object}
70
+ * @constructor
71
+ * create a new type Schema
72
+ */
73
+ export class TypeSchemaBase implements CommonInterface {
74
+ public name: string;
75
+ public defaultValue: any;
76
+ public encode?: (value: any, stream: OutputBinaryStream) => void;
77
+ public decode?: (stream: BinaryStream) => any;
78
+ public coerce?: (value: any) => any;
79
+ public toJSON?: () => string;
80
+ public category: FieldCategory;
81
+ public subType: string;
82
+ public isAbstract: boolean;
83
+
84
+ constructor(options: TypeSchemaConstructorOptions) {
85
+ assert(options.category !== null);
86
+ this.encode = options.encode || undefined;
87
+ this.decode = options.decode || undefined;
88
+ this.coerce = options.coerce;
89
+ this.category = options.category || FieldCategory.basic;
90
+ this.name = options.name;
91
+ for (const prop in options) {
92
+ if (Object.prototype.hasOwnProperty.call(options, prop)) {
93
+ (this as any)[prop] = (options as any)[prop];
94
+ }
95
+ }
96
+ this.subType = options.subType || "";
97
+ this.isAbstract = options.isAbstract || false;
98
+ }
70
99
 
71
- function defaultDecode(stream: BinaryStream): any {
72
- return null;
100
+ /**
101
+ * @method computer_default_value
102
+ * @param defaultValue {*} the default value
103
+ * @return {*}
104
+ */
105
+ public computer_default_value(defaultValue: unknown): any {
106
+ if (defaultValue === undefined) {
107
+ defaultValue = this.defaultValue;
108
+ }
109
+ if (typeof defaultValue === "function") {
110
+ // be careful not to cache this value , it must be call each time to make sure
111
+ // we do not end up with the same value/instance twice.
112
+ defaultValue = defaultValue();
113
+ }
114
+ return defaultValue;
115
+ }
116
+
117
+ public getBaseType(): CommonInterface | null {
118
+ if (!this.subType) return null;
119
+ return getBuiltInType(this.subType) as CommonInterface;
120
+ }
121
+
122
+ public isSubTypeOf(type: CommonInterface): boolean {
123
+ if (this.name === type.name) {
124
+ return true;
125
+ }
126
+ const baseType = this.getBaseType();
127
+ if (!baseType) {
128
+ return false;
129
+ }
130
+ return baseType.isSubTypeOf(type);
131
+ }
73
132
  }
74
133
 
75
134
  export class BasicTypeSchema extends TypeSchemaBase implements BasicTypeDefinition {
76
135
  public subType: string;
77
-
136
+ public isAbstract: boolean;
78
137
  public encode: (value: any, stream: OutputBinaryStream) => void;
79
138
  public decode: (stream: BinaryStream) => any;
80
139
 
81
140
  constructor(options: BasicTypeDefinitionOptions) {
82
141
  super(options);
83
142
  this.subType = options.subType;
143
+ this.isAbstract = options.isAbstract || false;
84
144
  this.encode = options.encode || defaultEncode;
85
145
  this.decode = options.decode || defaultDecode;
86
146
  }
87
147
  }
88
148
 
89
- export const minDate = new Date(Date.UTC(1601, 0, 1, 0, 0, 0));
90
-
91
- function defaultGuidValue(): any {
92
- return Buffer.alloc(0);
93
- }
94
-
95
- function toJSONGuid(value: any): any {
96
- if (typeof value === "string") {
97
- return value;
98
- }
99
- assert(value instanceof Buffer);
100
- return value.toString("base64");
101
- }
102
-
103
- function encodeAny(value: any, stream: OutputBinaryStream) {
104
- assert(false, "type 'Any' cannot be encoded");
105
- }
106
-
107
- function decodeAny(stream: BinaryStream) {
108
- assert(false, "type 'Any' cannot be decoded");
109
- }
110
-
111
- // eslint-disable-next-line @typescript-eslint/no-empty-function
112
- function encodeNull(value: any, stream: OutputBinaryStream): void {}
113
-
114
- function decodeNull(stream: BinaryStream): any {
115
- return null;
116
- }
117
149
 
118
150
  // there are 4 types of DataTypes in opcua:
119
151
  // Built-In DataType
@@ -132,8 +164,9 @@ interface T {
132
164
  defaultValue?: any;
133
165
  toJSON?: any;
134
166
  }
167
+
135
168
  // Built-In Type
136
- const _defaultType: any[] = [
169
+ const _defaultType: BasicTypeDefinitionOptionsBase[] = [
137
170
  // Built-in DataTypes ( see OPCUA Part III v1.02 - $5.8.2 )
138
171
  {
139
172
  name: "Null",
@@ -144,8 +177,8 @@ const _defaultType: any[] = [
144
177
  defaultValue: null
145
178
  },
146
179
  {
180
+ // special case
147
181
  name: "Any",
148
-
149
182
  decode: decodeAny,
150
183
  encode: encodeAny
151
184
  },
@@ -158,17 +191,19 @@ const _defaultType: any[] = [
158
191
  coerce: coerceBoolean,
159
192
  defaultValue: false
160
193
  },
161
- { name: "Int8", encode: encodeInt8, decode: decodeInt8, defaultValue: 0, coerce: coerceInt8 },
162
- { name: "UInt8", encode: encodeUInt8, decode: decodeUInt8, defaultValue: 0, coerce: coerceUInt8 },
163
- { name: "SByte", encode: encodeSByte, decode: decodeSByte, defaultValue: 0, coerce: coerceSByte },
164
- { name: "Byte", encode: encodeByte, decode: decodeByte, defaultValue: 0, coerce: coerceByte },
165
- { name: "Int16", encode: encodeInt16, decode: decodeInt16, defaultValue: 0, coerce: coerceInt16 },
166
- { name: "UInt16", encode: encodeUInt16, decode: decodeUInt16, defaultValue: 0, coerce: coerceUInt16 },
167
- { name: "Int32", encode: encodeInt32, decode: decodeInt32, defaultValue: 0, coerce: coerceInt32 },
168
- { name: "UInt32", encode: encodeUInt32, decode: decodeUInt32, defaultValue: 0, coerce: coerceUInt32 },
194
+
195
+ { name: "Number", isAbstract: true },
196
+ { name: "Integer", subType: "Number", isAbstract: true },
197
+ { name: "UInteger", subType: "Number", isAbstract: true },
198
+ { name: "SByte", subType: "Integer", encode: encodeSByte, decode: decodeSByte, defaultValue: 0, coerce: coerceSByte },
199
+ { name: "Byte", subType: "UInteger", encode: encodeByte, decode: decodeByte, defaultValue: 0, coerce: coerceByte },
200
+ { name: "Int16", subType: "Integer", encode: encodeInt16, decode: decodeInt16, defaultValue: 0, coerce: coerceInt16 },
201
+ { name: "UInt16", subType: "UInteger", encode: encodeUInt16, decode: decodeUInt16, defaultValue: 0, coerce: coerceUInt16 },
202
+ { name: "Int32", subType: "Integer", encode: encodeInt32, decode: decodeInt32, defaultValue: 0, coerce: coerceInt32 },
203
+ { name: "UInt32", subType: "UInteger", encode: encodeUInt32, decode: decodeUInt32, defaultValue: 0, coerce: coerceUInt32 },
169
204
  {
170
205
  name: "Int64",
171
-
206
+ subType: "Integer",
172
207
  decode: decodeInt64,
173
208
  encode: encodeInt64,
174
209
 
@@ -177,7 +212,7 @@ const _defaultType: any[] = [
177
212
  },
178
213
  {
179
214
  name: "UInt64",
180
-
215
+ subType: "UInteger",
181
216
  decode: decodeUInt64,
182
217
  encode: encodeUInt64,
183
218
 
@@ -186,6 +221,7 @@ const _defaultType: any[] = [
186
221
  },
187
222
  {
188
223
  name: "Float",
224
+ subType: "Number",
189
225
 
190
226
  decode: decodeFloat,
191
227
  encode: encodeFloat,
@@ -195,6 +231,7 @@ const _defaultType: any[] = [
195
231
  },
196
232
  {
197
233
  name: "Double",
234
+ subType: "Number",
198
235
 
199
236
  decode: decodeDouble,
200
237
  encode: encodeDouble,
@@ -218,7 +255,7 @@ const _defaultType: any[] = [
218
255
  encode: encodeDateTime,
219
256
 
220
257
  coerce: coerceDateTime,
221
- defaultValue: exports.minDate
258
+ defaultValue: ()=> minDate
222
259
  },
223
260
  {
224
261
  name: "Guid",
@@ -315,18 +352,20 @@ _defaultType.forEach(registerType);
315
352
  * @method registerType
316
353
  * @param schema {TypeSchemaBase}
317
354
  */
318
- export function registerType(schema: BasicTypeDefinitionOptions): void {
319
- assert(typeof schema.name === "string");
320
- if (typeof schema.encode !== "function") {
321
- throw new Error("schema " + schema.name + " has no encode function");
322
- }
323
- if (typeof schema.decode !== "function") {
324
- throw new Error("schema " + schema.name + " has no decode function");
355
+ export function registerType(schema: BasicTypeDefinitionOptionsBase): void {
356
+ if (!schema.isAbstract) {
357
+ assert(schema.encode);
358
+ assert(schema.decode);
325
359
  }
326
-
327
360
  schema.category = FieldCategory.basic;
328
-
329
- const definition = new BasicTypeSchema(schema);
361
+ schema.subType = schema.subType || "";
362
+ if (schema.name !== "Null" && schema.name !== "Any" && schema.name !== "Variant" && schema.name !== "ExtensionObject") {
363
+ const dataType = DataTypeIds[schema.name as keyof typeof DataTypeIds];
364
+ if (!dataType) {
365
+ throw new Error("registerType : dataType " + schema.name + " is not defined");
366
+ }
367
+ }
368
+ const definition = new BasicTypeSchema(schema as BasicTypeDefinitionOptions);
330
369
  _defaultTypeMap.set(schema.name, definition);
331
370
  }
332
371
 
@@ -336,46 +375,29 @@ export function unregisterType(typeName: string): void {
336
375
  _defaultTypeMap.delete(typeName);
337
376
  }
338
377
 
339
- /**
340
- * @method findSimpleType
341
- * @param name
342
- * @return {TypeSchemaBase|null}
343
- */
344
- export function findSimpleType(name: string): BasicTypeDefinition {
378
+ export function getBuiltInType(name: string): TypeSchemaBase {
345
379
  const typeSchema = _defaultTypeMap.get(name);
346
380
  if (!typeSchema) {
347
381
  throw new Error("Cannot find schema for simple type " + name);
348
382
  }
349
- assert(typeSchema instanceof TypeSchemaBase);
350
- return typeSchema as BasicTypeDefinition;
383
+ return typeSchema;
351
384
  }
352
385
 
353
386
  export function hasBuiltInType(name: string): boolean {
354
387
  return _defaultTypeMap.has(name);
355
388
  }
356
389
 
357
- export function getBuildInType(name: string): BasicTypeDefinition {
358
- return _defaultTypeMap.get(name) as BasicTypeDefinition;
359
- }
360
-
361
- /**
362
- * @method findBuiltInType
363
- * find the Builtin Type that this
364
- * @param dataTypeName
365
- * @return {*}
366
- */
390
+ /** */
367
391
  export function findBuiltInType(dataTypeName: string): BasicTypeDefinition {
368
392
  assert(typeof dataTypeName === "string", "findBuiltInType : expecting a string " + dataTypeName);
369
- const t = _defaultTypeMap.get(dataTypeName) as BasicTypeDefinition;
370
- if (!t) {
371
- throw new Error("datatype " + dataTypeName + " must be registered");
372
- }
393
+ const t = getBuiltInType(dataTypeName);
373
394
  if (t.subType && t.subType !== t.name /* avoid infinite recursion */) {
374
- return findBuiltInType(t.subType);
395
+ const st = getBuiltInType(t.subType);
396
+ if (!st.isAbstract) {
397
+ return findBuiltInType(t.subType);
398
+ }
375
399
  }
376
400
  return t;
377
401
  }
378
402
 
379
- export function getTypeMap(): Map<string, BasicTypeSchema> {
380
- return _defaultTypeMap;
381
- }
403
+
@@ -5,8 +5,8 @@
5
5
  import { assert } from "node-opcua-assert";
6
6
  import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
7
7
 
8
- import { registerType } from "./factories_builtin_types";
9
- import { ConstructorFunc } from "./constructor_type";
8
+ import { registerType } from "./builtin_types";
9
+ import { ConstructorFunc } from "./types";
10
10
 
11
11
  function _self_encode(constructor: any) {
12
12
  assert(typeof constructor === "function");
@@ -7,17 +7,38 @@ import * as chalk from "chalk";
7
7
  import { assert } from "node-opcua-assert";
8
8
  import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
9
9
  import { ExpandedNodeId, NodeId } from "node-opcua-nodeid";
10
+ import { DataTypeIds } from "node-opcua-constants";
10
11
 
11
- import { ConstructorFunc, ConstructorFuncWithSchema } from "./constructor_type";
12
- import { BaseUAObject } from "./factories_baseobject";
13
- import { getBuildInType, hasBuiltInType } from "./factories_builtin_types";
14
- import { EnumerationDefinitionSchema, getEnumeration, hasEnumeration } from "./factories_enumerations";
15
- import { StructuredTypeSchema } from "./factories_structuredTypeSchema";
16
- import { BasicTypeDefinition } from "./types";
12
+ import { getBuiltInType as getBuiltInType, hasBuiltInType } from "./builtin_types";
13
+ import { EnumerationDefinitionSchema, getBuiltInEnumeration, hasBuiltInEnumeration } from "./enumerations";
14
+ import {
15
+ CommonInterface,
16
+ StructuredTypeField,
17
+ IStructuredTypeSchema,
18
+ ConstructorFuncWithSchema,
19
+ ConstructorFunc,
20
+ IBaseUAObject
21
+ } from "./types";
17
22
 
18
23
  const debugLog = make_debugLog(__filename);
19
24
  const doDebug = checkDebugFlag(__filename);
20
25
 
26
+ export function _findFieldSchema(typeDictionary: DataTypeFactory, field: StructuredTypeField, value: any): IStructuredTypeSchema {
27
+ const fieldType = field.fieldType;
28
+
29
+ if (field.allowSubType && field.category === "complex") {
30
+ const fieldTypeConstructor = value ? value.constructor : field.fieldTypeConstructor;
31
+
32
+ const _newFieldSchema = fieldTypeConstructor.schema;
33
+
34
+ return _newFieldSchema as IStructuredTypeSchema;
35
+ }
36
+
37
+ const fieldTypeConstructor = field.fieldTypeConstructor || typeDictionary.getStructureTypeConstructor(fieldType);
38
+
39
+ return (field.schema as IStructuredTypeSchema) || (fieldTypeConstructor as any).schema;
40
+ }
41
+
21
42
  export class DataTypeFactory {
22
43
  public defaultByteOrder: string;
23
44
  public targetNamespace: string;
@@ -27,7 +48,6 @@ export class DataTypeFactory {
27
48
  private _structureTypeConstructorByDataTypeMap: Map<string, ConstructorFuncWithSchema> = new Map();
28
49
  private _structureTypeConstructorByEncodingNodeIdMap: Map<string, any> = new Map();
29
50
  private _enumerations: Map<string, EnumerationDefinitionSchema> = new Map();
30
- private _simpleTypes: Map<string, { nodeId: NodeId; definition: BasicTypeDefinition }> = new Map();
31
51
 
32
52
  private baseDataFactories: DataTypeFactory[];
33
53
 
@@ -40,46 +60,23 @@ export class DataTypeFactory {
40
60
  public repairBaseDataFactories(baseDataFactories: DataTypeFactory[]): void {
41
61
  this.baseDataFactories = baseDataFactories;
42
62
  }
43
- // -----------------------------
44
- public registerSimpleType(name: string, dataTypeNodeId: NodeId, def: BasicTypeDefinition): void {
45
- // istanbul ignore next
46
- if (this._simpleTypes.has(name)) {
47
- throw new Error("registerSimpleType " + name + " already register");
48
- }
49
- this._simpleTypes.set(name, { nodeId: dataTypeNodeId, definition: def });
50
- }
51
63
 
52
- public hasSimpleType(name: string): boolean {
53
- if (this._simpleTypes.has(name)) {
54
- return true;
55
- }
56
- for (const factory of this.baseDataFactories) {
57
- if (factory.hasSimpleType(name)) {
58
- return true;
59
- }
60
- }
61
- const hasSimpleT = hasBuiltInType(name);
62
- if (hasSimpleT) {
63
- return hasSimpleT;
64
- }
64
+ public hasBuiltInType(name: string): boolean {
65
65
  return hasBuiltInType(name);
66
66
  }
67
- public getSimpleType(name: string): BasicTypeDefinition {
68
- if (this._simpleTypes.has(name)) {
69
- return this._simpleTypes.get(name)!.definition;
70
- }
71
- for (const factory of this.baseDataFactories) {
72
- if (factory.hasSimpleType(name)) {
73
- return factory.getSimpleType(name);
74
- }
75
- }
76
- return getBuildInType(name);
67
+
68
+ public getBuiltInType(name: string): CommonInterface {
69
+ return getBuiltInType(name);
77
70
  }
71
+
72
+ public getBuiltInTypeByDataType(nodeId: NodeId): CommonInterface {
73
+ return getBuiltInType(DataTypeIds[nodeId.value as number]);
74
+ }
75
+
78
76
  // -----------------------------
79
77
  // EnumerationDefinitionSchema
80
78
  public registerEnumeration(enumeration: EnumerationDefinitionSchema): void {
81
- debugLog("Registering Enumeration ", enumeration.name);
82
- assert(!this._enumerations.has(enumeration.name));
79
+ assert(!this._enumerations.has(enumeration.name), "enumeration already registered");
83
80
  this._enumerations.set(enumeration.name, enumeration);
84
81
  }
85
82
 
@@ -93,11 +90,12 @@ export class DataTypeFactory {
93
90
  return true;
94
91
  }
95
92
  }
96
- if (hasEnumeration(enumName)) {
93
+ if (hasBuiltInEnumeration(enumName)) {
97
94
  return true;
98
95
  }
99
96
  return false;
100
97
  }
98
+
101
99
  public getEnumeration(enumName: string): EnumerationDefinitionSchema | null {
102
100
  if (this._enumerations.has(enumName)) {
103
101
  return this._enumerations.get(enumName) || null;
@@ -109,7 +107,7 @@ export class DataTypeFactory {
109
107
  return e;
110
108
  }
111
109
  }
112
- const ee = getEnumeration(enumName);
110
+ const ee = getBuiltInEnumeration(enumName);
113
111
  return ee;
114
112
  }
115
113
  // ----------------------------
@@ -178,7 +176,7 @@ export class DataTypeFactory {
178
176
  return false;
179
177
  }
180
178
 
181
- public getStructuredTypeSchema(typeName: string): StructuredTypeSchema {
179
+ public getStructuredTypeSchema(typeName: string): IStructuredTypeSchema {
182
180
  const constructor = this.getStructureTypeConstructor(typeName);
183
181
  return constructor.schema;
184
182
  }
@@ -245,7 +243,7 @@ export class DataTypeFactory {
245
243
  return false;
246
244
  }
247
245
 
248
- public constructObject(binaryEncodingNodeId: NodeId): BaseUAObject {
246
+ public constructObject(binaryEncodingNodeId: NodeId): IBaseUAObject {
249
247
  if (!verifyExpandedNodeId(binaryEncodingNodeId)) {
250
248
  throw new Error(" constructObject : invalid expandedNodeId provided " + binaryEncodingNodeId.toString());
251
249
  }
@@ -253,8 +251,7 @@ export class DataTypeFactory {
253
251
 
254
252
  if (!constructor) {
255
253
  debugLog("Cannot find constructor for " + binaryEncodingNodeId.toString());
256
- return new BaseUAObject();
257
- // throw new Error("Cannot find constructor for " + expandedNodeId.toString());
254
+ throw new Error("Cannot find constructor for " + binaryEncodingNodeId.toString());
258
255
  }
259
256
  return new constructor();
260
257
  }
@@ -315,7 +312,7 @@ export class DataTypeFactory {
315
312
  }
316
313
  }
317
314
 
318
- function dumpSchema(schema: StructuredTypeSchema, write: any) {
315
+ function dumpSchema(schema: IStructuredTypeSchema, write: any) {
319
316
  write("name ", schema.name);
320
317
  write("dataType ", schema.dataTypeNodeId.toString());
321
318
  write("binaryEncoding ", schema.encodingDefaultBinary!.toString());
@@ -0,0 +1,37 @@
1
+ import assert from "node-opcua-assert";
2
+ import { OutputBinaryStream, BinaryStream } from "node-opcua-binary-stream";
3
+
4
+ export function defaultEncode(value: any, stream: OutputBinaryStream): void {
5
+ /** */
6
+ }
7
+
8
+ export function defaultDecode(stream: BinaryStream): any {
9
+ return null;
10
+ }
11
+
12
+ export function defaultGuidValue(): any {
13
+ return Buffer.alloc(0);
14
+ }
15
+
16
+ export function toJSONGuid(value: any): any {
17
+ if (typeof value === "string") {
18
+ return value;
19
+ }
20
+ assert(value instanceof Buffer);
21
+ return value.toString("base64");
22
+ }
23
+
24
+ export function encodeAny(value: any, stream: OutputBinaryStream) {
25
+ assert(false, "type 'Any' cannot be encoded");
26
+ }
27
+
28
+ export function decodeAny(stream: BinaryStream) {
29
+ assert(false, "type 'Any' cannot be decoded");
30
+ }
31
+
32
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
33
+ export function encodeNull(value: any, stream: OutputBinaryStream): void {}
34
+
35
+ export function decodeNull(stream: BinaryStream): any {
36
+ return null;
37
+ }
@@ -5,7 +5,8 @@ import { assert } from "node-opcua-assert";
5
5
 
6
6
  import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
7
7
  import { Enum, EnumItem, _TypescriptEnum , adaptTypescriptEnum} from "node-opcua-enum";
8
- import { EnumerationDefinition, TypeSchemaBase, TypeSchemaConstructorOptions } from "./types";
8
+ import { TypeSchemaBase } from "./builtin_types";
9
+ import { EnumerationDefinition, TypeSchemaConstructorOptions } from "./types";
9
10
 
10
11
  function _encode_enumeration(typedEnum: Enum, value: number, stream: OutputBinaryStream): void {
11
12
  assert(typeof value === "number", "Expecting a number here");
@@ -88,13 +89,14 @@ export function registerEnumeration(options: EnumerationDefinitionOptions): Enum
88
89
  return enumerationDefinition.typedEnum;
89
90
  }
90
91
 
91
- export function hasEnumeration(enumerationName: string): boolean {
92
+ export function hasBuiltInEnumeration(enumerationName: string): boolean {
92
93
  return _enumerations.has(enumerationName);
93
94
  }
94
95
 
95
- export function getEnumeration(enumerationName: string): EnumerationDefinitionSchema {
96
- if (!exports.hasEnumeration(enumerationName)) {
96
+ export function getBuiltInEnumeration(enumerationName: string): EnumerationDefinitionSchema {
97
+ if (!hasBuiltInEnumeration(enumerationName)) {
97
98
  throw new Error("Cannot find enumeration with type " + enumerationName);
98
99
  }
99
100
  return _enumerations.get(enumerationName) as EnumerationDefinitionSchema;
100
101
  }
102
+
File without changes
@@ -1,24 +1,14 @@
1
1
  /**
2
2
  * @module node-opcua-factory
3
3
  */
4
- // tslint:disable:no-console
5
-
6
- import * as chalk from "chalk";
7
-
8
- import { assert } from "node-opcua-assert";
9
- import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
10
4
  import { ExpandedNodeId, NodeId } from "node-opcua-nodeid";
11
5
 
12
6
  import { DataTypeFactory } from "./datatype_factory";
13
- import { ConstructorFuncWithSchema, ConstructorFunc } from "./constructor_type";
14
-
15
- import { BaseUAObject } from "./factories_baseobject";
16
- import { StructuredTypeSchema } from "./factories_structuredTypeSchema";
17
7
 
18
- const debugLog = make_debugLog(__filename);
19
- const doDebug = checkDebugFlag(__filename);
8
+ import { ConstructorFunc, ConstructorFuncWithSchema } from "./types";
20
9
 
21
10
  let globalFactory: DataTypeFactory;
11
+
22
12
  export function getStandardDataTypeFactory(): DataTypeFactory {
23
13
  if (!globalFactory) {
24
14
  globalFactory = new DataTypeFactory([]);
@@ -26,32 +16,24 @@ export function getStandardDataTypeFactory(): DataTypeFactory {
26
16
  }
27
17
  return globalFactory;
28
18
  }
19
+
29
20
  export function getStructureTypeConstructor(typeName: string): ConstructorFuncWithSchema {
30
21
  return getStandardDataTypeFactory().getStructureTypeConstructor(typeName);
31
22
  }
23
+
32
24
  export function hasStructuredType(typeName: string): boolean {
33
25
  return getStandardDataTypeFactory().hasStructuredType(typeName);
34
26
  }
35
- export function getStructuredTypeSchema(typeName: string): StructuredTypeSchema {
36
- return getStandardDataTypeFactory().getStructuredTypeSchema(typeName);
37
- }
38
27
 
39
28
  export function getConstructor(binaryEncodingNodeId: ExpandedNodeId): ConstructorFunc | null {
40
29
  return getStandardDataTypeFactory().getConstructor(binaryEncodingNodeId);
41
30
  }
31
+
42
32
  export function hasConstructor(binaryEncodingNodeId: ExpandedNodeId): boolean {
43
33
  return getStandardDataTypeFactory().hasConstructor(binaryEncodingNodeId);
44
34
  }
45
- export function constructObject(binaryEncodingNodeId: ExpandedNodeId): BaseUAObject {
46
- return getStandardDataTypeFactory().constructObject(binaryEncodingNodeId);
47
- }
48
- export function registerClassDefinition(
49
- dataTypeNodeId: NodeId,
50
- className: string,
51
- classConstructor: ConstructorFuncWithSchema
52
- ): void {
53
- return getStandardDataTypeFactory().registerClassDefinition(dataTypeNodeId, className, classConstructor);
54
- }
35
+
36
+
55
37
  /* istanbul ignore next */
56
38
  export function dump(): void {
57
39
  getStandardDataTypeFactory().dump();
@@ -0,0 +1,10 @@
1
+ import { ExpandedNodeId } from "node-opcua-nodeid";
2
+ import { getStandardDataTypeFactory } from "./get_standard_data_type_factory";
3
+ import { IBaseUAObject, IStructuredTypeSchema } from "./types";
4
+
5
+ export function getStructuredTypeSchema(typeName: string): IStructuredTypeSchema {
6
+ return getStandardDataTypeFactory().getStructuredTypeSchema(typeName);
7
+ }
8
+ export function constructObject(binaryEncodingNodeId: ExpandedNodeId): IBaseUAObject {
9
+ return getStandardDataTypeFactory().constructObject(binaryEncodingNodeId);
10
+ }