node-opcua-factory 2.51.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 (54) hide show
  1. package/LICENSE +20 -0
  2. package/dist/constructor_type.d.ts +15 -0
  3. package/dist/constructor_type.js +3 -0
  4. package/dist/constructor_type.js.map +1 -0
  5. package/dist/datatype_factory.d.ts +40 -0
  6. package/dist/datatype_factory.js +330 -0
  7. package/dist/datatype_factory.js.map +1 -0
  8. package/dist/factories_baseobject.d.ts +56 -0
  9. package/dist/factories_baseobject.js +498 -0
  10. package/dist/factories_baseobject.js.map +1 -0
  11. package/dist/factories_basic_type.d.ts +40 -0
  12. package/dist/factories_basic_type.js +136 -0
  13. package/dist/factories_basic_type.js.map +1 -0
  14. package/dist/factories_builtin_types.d.ts +32 -0
  15. package/dist/factories_builtin_types.js +262 -0
  16. package/dist/factories_builtin_types.js.map +1 -0
  17. package/dist/factories_builtin_types_special.d.ts +5 -0
  18. package/dist/factories_builtin_types_special.js +46 -0
  19. package/dist/factories_builtin_types_special.js.map +1 -0
  20. package/dist/factories_enumerations.d.ts +31 -0
  21. package/dist/factories_enumerations.js +77 -0
  22. package/dist/factories_enumerations.js.map +1 -0
  23. package/dist/factories_factories.d.ts +17 -0
  24. package/dist/factories_factories.js +54 -0
  25. package/dist/factories_factories.js.map +1 -0
  26. package/dist/factories_id_generator.d.ts +3 -0
  27. package/dist/factories_id_generator.js +22 -0
  28. package/dist/factories_id_generator.js.map +1 -0
  29. package/dist/factories_schema_helpers.d.ts +26 -0
  30. package/dist/factories_schema_helpers.js +121 -0
  31. package/dist/factories_schema_helpers.js.map +1 -0
  32. package/dist/factories_structuredTypeSchema.d.ts +46 -0
  33. package/dist/factories_structuredTypeSchema.js +269 -0
  34. package/dist/factories_structuredTypeSchema.js.map +1 -0
  35. package/dist/index.d.ts +15 -0
  36. package/dist/index.js +28 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/types.d.ts +111 -0
  39. package/dist/types.js +56 -0
  40. package/dist/types.js.map +1 -0
  41. package/package.json +41 -0
  42. package/source/constructor_type.ts +18 -0
  43. package/source/datatype_factory.ts +368 -0
  44. package/source/factories_baseobject.ts +567 -0
  45. package/source/factories_basic_type.ts +168 -0
  46. package/source/factories_builtin_types.ts +381 -0
  47. package/source/factories_builtin_types_special.ts +55 -0
  48. package/source/factories_enumerations.ts +97 -0
  49. package/source/factories_factories.ts +58 -0
  50. package/source/factories_id_generator.ts +18 -0
  51. package/source/factories_schema_helpers.ts +125 -0
  52. package/source/factories_structuredTypeSchema.ts +330 -0
  53. package/source/index.ts +15 -0
  54. package/source/types.ts +180 -0
@@ -0,0 +1,111 @@
1
+ import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
2
+ import { Enum } from "node-opcua-enum";
3
+ import { NodeId } from "node-opcua-nodeid";
4
+ import { ConstructorFunc } from "./constructor_type";
5
+ export interface CommonInterface {
6
+ name: string;
7
+ encode?: (value: any, stream: OutputBinaryStream) => void;
8
+ decode?: (stream: BinaryStream) => any;
9
+ coerce?: (value: any) => any;
10
+ toJSON?: (value: any) => any;
11
+ random?: () => any;
12
+ validate?: (value: any) => void;
13
+ defaultValue?: any;
14
+ computer_default_value(defaultValue: any): any;
15
+ }
16
+ export declare enum FieldCategory {
17
+ enumeration = "enumeration",
18
+ complex = "complex",
19
+ basic = "basic"
20
+ }
21
+ export interface StructuredTypeField {
22
+ name: string;
23
+ fieldType: string;
24
+ isArray?: boolean;
25
+ documentation?: string;
26
+ category: FieldCategory;
27
+ schema: CommonInterface;
28
+ fieldTypeConstructor?: ConstructorFunc;
29
+ subType?: string;
30
+ defaultValue?: any;
31
+ validate?: (value: any) => boolean;
32
+ decode?: (stream: BinaryStream) => any;
33
+ switchBit?: number;
34
+ switchValue?: number;
35
+ }
36
+ export interface FieldEnumeration extends StructuredTypeField {
37
+ }
38
+ export interface FieldComplex extends StructuredTypeField {
39
+ }
40
+ export interface FieldBasic extends StructuredTypeField {
41
+ }
42
+ export declare type FieldType = FieldEnumeration | FieldComplex | FieldBasic;
43
+ export declare type DefaultValueFunc = () => any;
44
+ export interface FieldInterfaceOptions {
45
+ name: string;
46
+ fieldType: string;
47
+ isArray?: boolean;
48
+ documentation?: string;
49
+ category?: FieldCategory;
50
+ defaultValue?: any | DefaultValueFunc;
51
+ schema?: any;
52
+ switchBit?: number;
53
+ switchValue?: number;
54
+ }
55
+ export interface StructuredTypeOptions {
56
+ name: string;
57
+ id?: number | NodeId;
58
+ fields: FieldInterfaceOptions[];
59
+ documentation?: string;
60
+ baseType: string;
61
+ _resolved?: boolean;
62
+ bitFields?: any[];
63
+ base?: StructuredTypeOptions;
64
+ }
65
+ export interface TypeSchemaConstructorOptions {
66
+ name: string;
67
+ category?: FieldCategory;
68
+ defaultValue?: any;
69
+ encode?: (value: any, stream: OutputBinaryStream) => void;
70
+ decode?: (stream: BinaryStream) => any;
71
+ coerce?: (value: any) => any;
72
+ }
73
+ export interface BasicTypeDefinitionOptions extends TypeSchemaConstructorOptions {
74
+ subType: string;
75
+ toJSON?: (value: any) => any;
76
+ random?: () => any;
77
+ validate?: (value: any) => void;
78
+ }
79
+ export interface BasicTypeDefinition extends CommonInterface {
80
+ subType: string;
81
+ }
82
+ export interface BuiltInTypeDefinition extends BasicTypeDefinition {
83
+ }
84
+ export interface EnumerationDefinition extends CommonInterface {
85
+ enumValues: any;
86
+ typedEnum: Enum;
87
+ documentation?: string;
88
+ }
89
+ export declare type TypeDefinition = BuiltInTypeDefinition | EnumerationDefinition | BasicTypeDefinition | TypeSchemaBase;
90
+ /**
91
+ * @class TypeSchemaBase
92
+ * @param options {Object}
93
+ * @constructor
94
+ * create a new type Schema
95
+ */
96
+ export declare class TypeSchemaBase implements CommonInterface {
97
+ name: string;
98
+ defaultValue: any;
99
+ encode?: (value: any, stream: OutputBinaryStream) => void;
100
+ decode?: (stream: BinaryStream) => any;
101
+ coerce?: (value: any) => any;
102
+ toJSON?: () => string;
103
+ category: FieldCategory;
104
+ constructor(options: TypeSchemaConstructorOptions);
105
+ /**
106
+ * @method computer_default_value
107
+ * @param defaultValue {*} the default value
108
+ * @return {*}
109
+ */
110
+ computer_default_value(defaultValue: any): any;
111
+ }
package/dist/types.js ADDED
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypeSchemaBase = exports.FieldCategory = void 0;
4
+ /**
5
+ * @module node-opcua-factory
6
+ */
7
+ const node_opcua_assert_1 = require("node-opcua-assert");
8
+ var FieldCategory;
9
+ (function (FieldCategory) {
10
+ FieldCategory["enumeration"] = "enumeration";
11
+ FieldCategory["complex"] = "complex";
12
+ FieldCategory["basic"] = "basic";
13
+ })(FieldCategory = exports.FieldCategory || (exports.FieldCategory = {}));
14
+ // tslint:disable-next-line:no-empty
15
+ function defaultEncode(value, stream) { }
16
+ // tslint:disable-next-line:no-empty
17
+ function defaultDecode(stream) { }
18
+ /**
19
+ * @class TypeSchemaBase
20
+ * @param options {Object}
21
+ * @constructor
22
+ * create a new type Schema
23
+ */
24
+ class TypeSchemaBase {
25
+ constructor(options) {
26
+ (0, node_opcua_assert_1.assert)(options.category !== null);
27
+ this.encode = options.encode || undefined;
28
+ this.decode = options.decode || undefined;
29
+ this.coerce = options.coerce;
30
+ this.category = options.category || FieldCategory.basic;
31
+ this.name = options.name;
32
+ for (const prop in options) {
33
+ if (Object.prototype.hasOwnProperty.call(options, prop)) {
34
+ this[prop] = options[prop];
35
+ }
36
+ }
37
+ }
38
+ /**
39
+ * @method computer_default_value
40
+ * @param defaultValue {*} the default value
41
+ * @return {*}
42
+ */
43
+ computer_default_value(defaultValue) {
44
+ if (defaultValue === undefined) {
45
+ defaultValue = this.defaultValue;
46
+ }
47
+ if (typeof defaultValue === "function") {
48
+ // be careful not to cache this value , it must be call each time to make sure
49
+ // we do not end up with the same value/instance twice.
50
+ defaultValue = defaultValue();
51
+ }
52
+ return defaultValue;
53
+ }
54
+ }
55
+ exports.TypeSchemaBase = TypeSchemaBase;
56
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../source/types.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,yDAA2C;AA4B3C,IAAY,aAIX;AAJD,WAAY,aAAa;IACrB,4CAA2B,CAAA;IAC3B,oCAAmB,CAAA;IACnB,gCAAe,CAAA;AACnB,CAAC,EAJW,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAIxB;AA4FD,oCAAoC;AACpC,SAAS,aAAa,CAAC,KAAU,EAAE,MAAoB,IAAS,CAAC;AAEjE,oCAAoC;AACpC,SAAS,aAAa,CAAC,MAAoB,IAAS,CAAC;AAErD;;;;;GAKG;AACH,MAAa,cAAc;IASvB,YAAY,OAAqC;QAC7C,IAAA,0BAAM,EAAC,OAAO,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC;QAClC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,SAAS,CAAC;QAC1C,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,SAAS,CAAC;QAC1C,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,aAAa,CAAC,KAAK,CAAC;QACxD,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QAEzB,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE;YACxB,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,OAAO,EAAC,IAAI,CAAC,EAAE;gBACnD,IAAY,CAAC,IAAI,CAAC,GAAI,OAAe,CAAC,IAAI,CAAC,CAAC;aAChD;SACJ;IACL,CAAC;IAED;;;;OAIG;IACI,sBAAsB,CAAC,YAAiB;QAC3C,IAAI,YAAY,KAAK,SAAS,EAAE;YAC5B,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC;SACpC;QACD,IAAI,OAAO,YAAY,KAAK,UAAU,EAAE;YACpC,8EAA8E;YAC9E,uDAAuD;YACvD,YAAY,GAAG,YAAY,EAAE,CAAC;SACjC;QACD,OAAO,YAAY,CAAC;IACxB,CAAC;CACJ;AAxCD,wCAwCC"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "node-opcua-factory",
3
+ "version": "2.51.0",
4
+ "description": "pure nodejs OPCUA SDK - module -factory",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc -b",
9
+ "lint": "tslint source/**/*.ts",
10
+ "clean": "node -e \"require('rimraf').sync('dist');\"",
11
+ "test": "echo no test"
12
+ },
13
+ "dependencies": {
14
+ "chalk": "4.1.2",
15
+ "node-opcua-assert": "2.51.0",
16
+ "node-opcua-basic-types": "2.51.0",
17
+ "node-opcua-binary-stream": "2.51.0",
18
+ "node-opcua-debug": "2.51.0",
19
+ "node-opcua-enum": "2.51.0",
20
+ "node-opcua-guid": "2.51.0",
21
+ "node-opcua-nodeid": "2.51.0",
22
+ "node-opcua-status-code": "2.51.0",
23
+ "node-opcua-utils": "2.51.0"
24
+ },
25
+ "author": "Etienne Rossignon",
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git://github.com/node-opcua/node-opcua.git"
30
+ },
31
+ "keywords": [
32
+ "OPCUA",
33
+ "opcua",
34
+ "m2m",
35
+ "iot",
36
+ "opc ua",
37
+ "internet of things"
38
+ ],
39
+ "homepage": "http://node-opcua.github.io/",
40
+ "gitHead": "75feb111daf7ec65fa0111e4fa5beb8987fd4945"
41
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @module node-opcua-factory
3
+ */
4
+ import { ExpandedNodeId } from "node-opcua-nodeid";
5
+
6
+ import { BaseUAObject } from "./factories_baseobject";
7
+ import { StructuredTypeSchema } from "./factories_structuredTypeSchema";
8
+
9
+ type BaseUAObjectConstructable = new (options?: any) => BaseUAObject;
10
+ export type ConstructorFunc = BaseUAObjectConstructable;
11
+ // new (...args: any[]) => BaseUAObjectConstructable;
12
+
13
+ export interface ConstructorFuncWithSchema extends ConstructorFunc {
14
+ schema: StructuredTypeSchema;
15
+ possibleFields: string[];
16
+ encodingDefaultBinary: ExpandedNodeId;
17
+ encodingDefaultXml: ExpandedNodeId;
18
+ }
@@ -0,0 +1,368 @@
1
+ /**
2
+ * @module node-opcua-factory
3
+ */
4
+ // tslint:disable:no-console
5
+ import * as chalk from "chalk";
6
+ import * as util from "util";
7
+
8
+ import { assert } from "node-opcua-assert";
9
+ import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
10
+ import { ExpandedNodeId, NodeId } from "node-opcua-nodeid";
11
+
12
+ import { ConstructorFunc, ConstructorFuncWithSchema } from "./constructor_type";
13
+ import { BaseUAObject } from "./factories_baseobject";
14
+ import { getBuildInType, hasBuiltInType } from "./factories_builtin_types";
15
+ import { EnumerationDefinitionSchema, getEnumeration, hasEnumeration } from "./factories_enumerations";
16
+ import { StructuredTypeSchema } from "./factories_structuredTypeSchema";
17
+ import { BasicTypeDefinition } from "./types";
18
+
19
+ const debugLog = make_debugLog(__filename);
20
+ const doDebug = checkDebugFlag(__filename);
21
+
22
+ export class DataTypeFactory {
23
+ public defaultByteOrder: string;
24
+ public targetNamespace: string;
25
+ public imports: string[] = [];
26
+
27
+ private _structureTypeConstructorByNameMap: Map<string, ConstructorFuncWithSchema> = new Map();
28
+ private _structureTypeConstructorByDataTypeMap: Map<string, ConstructorFuncWithSchema> = new Map();
29
+ private _structureTypeConstructorByEncodingNodeIdMap: Map<string, any> = new Map();
30
+ private _enumerations: Map<string, EnumerationDefinitionSchema> = new Map();
31
+ private _simpleTypes: Map<string, { nodeId: NodeId; definition: BasicTypeDefinition }> = new Map();
32
+
33
+ private baseDataFactories: DataTypeFactory[];
34
+
35
+ public constructor(baseDataFactories: DataTypeFactory[]) {
36
+ this.defaultByteOrder = "LittleEndian";
37
+ this.targetNamespace = "";
38
+ this.baseDataFactories = baseDataFactories;
39
+ }
40
+
41
+ public repairBaseDataFactories(baseDataFactories: DataTypeFactory[]): void {
42
+ this.baseDataFactories = baseDataFactories;
43
+ }
44
+ // -----------------------------
45
+ public registerSimpleType(name: string, dataTypeNodeId: NodeId, def: BasicTypeDefinition) {
46
+ // istanbul ignore next
47
+ if (this._simpleTypes.has(name)) {
48
+ throw new Error("registerSimpleType " + name + " already register");
49
+ }
50
+ this._simpleTypes.set(name, { nodeId: dataTypeNodeId, definition: def });
51
+ }
52
+
53
+ public hasSimpleType(name: string): boolean {
54
+ if (this._simpleTypes.has(name)) {
55
+ return true;
56
+ }
57
+ for (const factory of this.baseDataFactories) {
58
+ if (factory.hasSimpleType(name)) {
59
+ return true;
60
+ }
61
+ }
62
+ const hasSimpleT = hasBuiltInType(name);
63
+ if (hasSimpleT) {
64
+ return hasSimpleT;
65
+ }
66
+ return hasBuiltInType(name);
67
+ }
68
+ public getSimpleType(name: string): BasicTypeDefinition {
69
+ if (this._simpleTypes.has(name)) {
70
+ return this._simpleTypes.get(name)!.definition;
71
+ }
72
+ for (const factory of this.baseDataFactories) {
73
+ if (factory.hasSimpleType(name)) {
74
+ return factory.getSimpleType(name);
75
+ }
76
+ }
77
+ return getBuildInType(name);
78
+ }
79
+ // -----------------------------
80
+ // EnumerationDefinitionSchema
81
+ public registerEnumeration(enumeration: EnumerationDefinitionSchema): void {
82
+ debugLog("Registering Enumeration ", enumeration.name);
83
+ assert(!this._enumerations.has(enumeration.name));
84
+ this._enumerations.set(enumeration.name, enumeration);
85
+ }
86
+
87
+ public hasEnumeration(enumName: string): boolean {
88
+ if (this._enumerations.has(enumName)) {
89
+ return true;
90
+ }
91
+ for (const factory of this.baseDataFactories) {
92
+ const e = factory.hasEnumeration(enumName);
93
+ if (e) {
94
+ return true;
95
+ }
96
+ }
97
+ if (hasEnumeration(enumName)) {
98
+ return true;
99
+ }
100
+ return false;
101
+ }
102
+ public getEnumeration(enumName: string): EnumerationDefinitionSchema | null {
103
+ if (this._enumerations.has(enumName)) {
104
+ return this._enumerations.get(enumName) || null;
105
+ }
106
+ for (const factory of this.baseDataFactories) {
107
+ const hasEnum = factory.hasEnumeration(enumName);
108
+ if (hasEnum) {
109
+ const e = factory.getEnumeration(enumName);
110
+ return e;
111
+ }
112
+ }
113
+ const ee = getEnumeration(enumName);
114
+ return ee;
115
+ }
116
+ // ----------------------------
117
+
118
+ public findConstructorForDataType(dataTypeNodeId: NodeId): ConstructorFuncWithSchema {
119
+ const constructor = this.getConstructorForDataType(dataTypeNodeId);
120
+ if (constructor) {
121
+ return constructor;
122
+ }
123
+ this.getConstructorForDataType(dataTypeNodeId);
124
+ throw new Error("Cannot find StructureType constructor for dataType " + dataTypeNodeId.toString());
125
+ }
126
+ public getConstructorForDataType(dataTypeNodeId: NodeId): ConstructorFuncWithSchema | null {
127
+ const constructor = this._structureTypeConstructorByDataTypeMap.get(dataTypeNodeId.toString());
128
+ if (constructor) {
129
+ return constructor;
130
+ }
131
+ for (const factory of this.baseDataFactories) {
132
+ const constructor2 = factory.getConstructorForDataType(dataTypeNodeId);
133
+ if (constructor2) {
134
+ return constructor2;
135
+ }
136
+ }
137
+ return null;
138
+ }
139
+ // ----------------------------------------------------------------------------------------------------
140
+ // Access by typeName
141
+ // ----------------------------------------------------------------------------------------------------
142
+ public structuredTypesNames(): IterableIterator<string> {
143
+ return this._structureTypeConstructorByNameMap.keys();
144
+ }
145
+
146
+ public getStructureTypeConstructor(typeName: string): ConstructorFuncWithSchema {
147
+ const constructor = this._structureTypeConstructorByNameMap.get(typeName);
148
+ if (constructor) {
149
+ return constructor;
150
+ }
151
+ for (const factory of this.baseDataFactories) {
152
+ if (!factory.hasStructuredType(typeName)) {
153
+ continue;
154
+ }
155
+ const constructor2 = factory.getStructureTypeConstructor(typeName);
156
+ if (constructor2) {
157
+ return constructor2;
158
+ }
159
+ }
160
+ // istanbul ignore next
161
+ if (doDebug) {
162
+ console.log([...this.structuredTypesNames()].join(" "));
163
+ }
164
+ // istanbul ignore next
165
+ throw new Error(
166
+ "Cannot find StructureType constructor for " + typeName + " - it may be abstract, or it could be a basic type"
167
+ );
168
+ }
169
+
170
+ public hasStructuredType(typeName: string): boolean {
171
+ if (this._structureTypeConstructorByNameMap.has(typeName)) {
172
+ return true;
173
+ }
174
+ for (const factory of this.baseDataFactories) {
175
+ if (factory.hasStructuredType(typeName)) {
176
+ return true;
177
+ }
178
+ }
179
+ return false;
180
+ }
181
+
182
+ public getStructuredTypeSchema(typeName: string): StructuredTypeSchema {
183
+ const constructor = this.getStructureTypeConstructor(typeName);
184
+ return constructor.schema;
185
+ }
186
+
187
+ // istanbul ignore next
188
+ public dump(): void {
189
+ console.log(" dumping registered factories");
190
+ console.log(
191
+ " Factory ",
192
+ [...this.structuredTypesNames()].sort().forEach((e) => e)
193
+ );
194
+ console.log(" done");
195
+ }
196
+
197
+ public registerClassDefinition(dataTypeNodeId: NodeId, className: string, classConstructor: ConstructorFuncWithSchema): void {
198
+ this._registerFactory(dataTypeNodeId, className, classConstructor);
199
+ if (classConstructor.encodingDefaultBinary && classConstructor.encodingDefaultBinary.value !== 0) {
200
+ this.associateWithBinaryEncoding(className, classConstructor.encodingDefaultBinary);
201
+ } else {
202
+ // for instance in DI FetchResultDataType should be abstract but is not
203
+ debugLog("warning ", dataTypeNodeId.toString(), "name=", className, " do not have binary encoding");
204
+ }
205
+ }
206
+
207
+ // ----------------------------------------------------------------------------------------------------
208
+ // Access by binaryEncodingNodeId
209
+ // ----------------------------------------------------------------------------------------------------
210
+ public getConstructor(binaryEncodingNodeId: NodeId): ConstructorFunc | null {
211
+ const expandedNodeIdKey = makeExpandedNodeIdKey(binaryEncodingNodeId);
212
+ const constructor = this._structureTypeConstructorByEncodingNodeIdMap.get(expandedNodeIdKey);
213
+ if (constructor) {
214
+ return constructor;
215
+ }
216
+ for (const factory of this.baseDataFactories) {
217
+ const constructor2 = factory.getConstructor(binaryEncodingNodeId);
218
+ if (constructor2) {
219
+ return constructor2;
220
+ }
221
+ }
222
+ debugLog(chalk.red("#getConstructor : cannot find constructor for expandedId "), binaryEncodingNodeId.toString());
223
+ return null;
224
+ }
225
+
226
+ public hasConstructor(binaryEncodingNodeId: NodeId): boolean {
227
+ if (!binaryEncodingNodeId) {
228
+ return false;
229
+ }
230
+ /* istanbul ignore next */
231
+ if (!verifyExpandedNodeId(binaryEncodingNodeId)) {
232
+ console.log("Invalid expandedNodeId");
233
+ return false;
234
+ }
235
+ const expandedNodeIdKey = makeExpandedNodeIdKey(binaryEncodingNodeId);
236
+ const constructor = this._structureTypeConstructorByEncodingNodeIdMap.get(expandedNodeIdKey);
237
+ if (constructor) {
238
+ return true;
239
+ }
240
+ for (const factory of this.baseDataFactories) {
241
+ const constructor2 = factory.getConstructor(binaryEncodingNodeId);
242
+ if (constructor2) {
243
+ return true;
244
+ }
245
+ }
246
+ return false;
247
+ }
248
+
249
+ public constructObject(binaryEncodingNodeId: NodeId): BaseUAObject {
250
+ if (!verifyExpandedNodeId(binaryEncodingNodeId)) {
251
+ throw new Error(" constructObject : invalid expandedNodeId provided " + binaryEncodingNodeId.toString());
252
+ }
253
+ const constructor = this.getConstructor(binaryEncodingNodeId);
254
+
255
+ if (!constructor) {
256
+ debugLog("Cannot find constructor for " + binaryEncodingNodeId.toString());
257
+ return new BaseUAObject();
258
+ // throw new Error("Cannot find constructor for " + expandedNodeId.toString());
259
+ }
260
+ return callConstructor(constructor);
261
+ }
262
+
263
+ public associateWithBinaryEncoding(className: string, expandedNodeId: ExpandedNodeId) {
264
+ const classConstructor = this.getStructureTypeConstructor(className);
265
+ if (doDebug) {
266
+ debugLog(" associateWithBinaryEncoding ", className, expandedNodeId.toString());
267
+ }
268
+
269
+ /* istanbul ignore next */
270
+ if (!verifyExpandedNodeId(expandedNodeId)) {
271
+ throw new Error("Invalid expandedNodeId " + expandedNodeId.toString() + " className = " + className);
272
+ }
273
+ const expandedNodeIdKey = makeExpandedNodeIdKey(expandedNodeId);
274
+
275
+ /* istanbul ignore next */
276
+ if (this._structureTypeConstructorByEncodingNodeIdMap.has(expandedNodeIdKey)) {
277
+ throw new Error(
278
+ " Class " +
279
+ className +
280
+ " with ID " +
281
+ expandedNodeId +
282
+ " already in constructorMap for " +
283
+ this._structureTypeConstructorByEncodingNodeIdMap.get(expandedNodeIdKey).name
284
+ );
285
+ }
286
+
287
+ this._structureTypeConstructorByEncodingNodeIdMap.set(expandedNodeIdKey, classConstructor);
288
+ }
289
+
290
+ public toString(): string {
291
+ const l: string[] = [];
292
+ function write(...args: [any, ...any[]]) {
293
+ l.push(util.format.apply(util.format, args));
294
+ }
295
+ dumpDataFactory(this, write);
296
+ return l.join("\n");
297
+ }
298
+
299
+ private _registerFactory(dataTypeNodeId: NodeId, typeName: string, constructor: ConstructorFuncWithSchema): void {
300
+ /* istanbul ignore next */
301
+ if (this.hasStructuredType(typeName)) {
302
+ console.log(this.getStructureTypeConstructor(typeName));
303
+ console.log("target namespace =", this.targetNamespace);
304
+ throw new Error(" registerFactory : " + typeName + " already registered");
305
+ }
306
+ debugLog("registering typeName ", typeName, dataTypeNodeId.toString());
307
+ this._structureTypeConstructorByNameMap.set(typeName, constructor);
308
+ if (dataTypeNodeId.value !== 0) {
309
+ this._structureTypeConstructorByDataTypeMap.set(dataTypeNodeId.toString(), constructor);
310
+ }
311
+ Object.defineProperty(constructor.schema, "$$factory", {
312
+ enumerable: false,
313
+ value: this,
314
+ writable: false
315
+ });
316
+ }
317
+ }
318
+
319
+ function dumpSchema(schema: StructuredTypeSchema, write: any) {
320
+ write("name ", schema.name);
321
+ write("dataType ", schema.dataTypeNodeId.toString());
322
+ write("binaryEncoding ", schema.encodingDefaultBinary!.toString());
323
+ for (const f of schema.fields) {
324
+ write(" ", f.name.padEnd(30, " "), f.isArray ? true : false, f.fieldType);
325
+ }
326
+ }
327
+ function dumpDataFactory(dataFactory: DataTypeFactory, write: any) {
328
+ for (const structureTypeName of dataFactory.structuredTypesNames()) {
329
+ const schema = dataFactory.getStructuredTypeSchema(structureTypeName);
330
+
331
+ write("structureTypeName =", structureTypeName);
332
+
333
+ if (!dataFactory.getConstructorForDataType(schema.dataTypeNodeId)) {
334
+ write(" ( No constructor for " + schema.name + " " + schema.dataTypeNodeId.toString());
335
+ }
336
+ if (!schema.encodingDefaultBinary) {
337
+ write(" (Schema has no encoding defaultBinary )");
338
+ } else {
339
+ if (dataFactory.hasConstructor(schema.encodingDefaultBinary)) {
340
+ console.log("schema", schema.name);
341
+ console.log("schema", schema.dataTypeNodeId.toString());
342
+ console.log("schema", schema.encodingDefaultBinary ? schema.encodingDefaultBinary.toString() : " ");
343
+ console.log("schema", schema.encodingDefaultXml ? schema.encodingDefaultXml.toString() : " ");
344
+ // return;
345
+ // throw new Error("Not in Binary Encoding Map!!!!! " + schema.encodingDefaultBinary);
346
+ }
347
+ }
348
+ dumpSchema(schema, write);
349
+ }
350
+ }
351
+
352
+ function verifyExpandedNodeId(expandedNodeId: NodeId): boolean {
353
+ /* istanbul ignore next */
354
+ if (expandedNodeId.value instanceof Buffer) {
355
+ throw new Error("getConstructor not implemented for opaque nodeid");
356
+ }
357
+ return true;
358
+ }
359
+
360
+ function makeExpandedNodeIdKey(expandedNodeId: NodeId): string {
361
+ return expandedNodeId.toString();
362
+ }
363
+
364
+ export function callConstructor(constructor: ConstructorFunc): BaseUAObject {
365
+ assert(typeof constructor === "function");
366
+ const constructorFunc: any = constructor.bind.apply(constructor, arguments as any);
367
+ return new constructorFunc();
368
+ }