Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

node-opcua-schemas

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-opcua-schemas - npm Package Compare versions

Comparing version 2.4.2 to 2.5.0-alpha.0

@@ -1,12 +0,11 @@

import { StructuredTypeSchema } from "node-opcua-factory";
import { DataTypeFactory, StructuredTypeSchema } from "node-opcua-factory";
import { ExpandedNodeId } from "node-opcua-nodeid";
import { TypeDictionary } from "./parse_binary_xsd";
export declare function getOrCreateConstructor(fieldType: string, typeDictionary: TypeDictionary, encodingDefaultBinary?: ExpandedNodeId, encodingDefaultXml?: ExpandedNodeId): AnyConstructorFunc;
export declare function getOrCreateConstructor(dataTypeName: string, dataTypeFactory: DataTypeFactory, encodingDefaultBinary?: ExpandedNodeId, encodingDefaultXml?: ExpandedNodeId): AnyConstructorFunc;
interface AnyConstructable {
schema: StructuredTypeSchema;
possibleFields: string[];
new (options?: any, schema?: StructuredTypeSchema, typeDictionary?: TypeDictionary): any;
new (options?: any, schema?: StructuredTypeSchema, factory?: DataTypeFactory): any;
}
export declare type AnyConstructorFunc = AnyConstructable;
export declare function createDynamicObjectConstructor(schema: StructuredTypeSchema, typeDictionary: TypeDictionary): AnyConstructorFunc;
export declare function createDynamicObjectConstructor(schema: StructuredTypeSchema, dataTypeFactory: DataTypeFactory): AnyConstructorFunc;
export {};

@@ -13,12 +13,12 @@ "use strict";

const doDebug = node_opcua_debug_1.checkDebugFlag(__filename);
function getOrCreateConstructor(fieldType, typeDictionary, encodingDefaultBinary, encodingDefaultXml) {
if (typeDictionary.hasStructuredType(fieldType)) {
return typeDictionary.getStructureTypeConstructor(fieldType);
function getOrCreateConstructor(dataTypeName, dataTypeFactory, encodingDefaultBinary, encodingDefaultXml) {
if (dataTypeFactory.hasStructuredType(dataTypeName)) {
return dataTypeFactory.getStructureTypeConstructor(dataTypeName);
}
const schema = typeDictionary.structuredTypes[fieldType];
const schema = dataTypeFactory.getStructuredTypeSchema(dataTypeName);
// istanbul ignore next
if (!schema) {
throw new Error("Unknown type in dictionary " + fieldType);
throw new Error("Unknown type in dictionary " + dataTypeName);
}
const constructor = createDynamicObjectConstructor(schema, typeDictionary);
const constructor = createDynamicObjectConstructor(schema, dataTypeFactory);
if (!constructor) {

@@ -28,4 +28,4 @@ return constructor;

// istanbul ignore next
if (!typeDictionary.hasStructuredType(fieldType)) {
typeDictionary.registerFactory(fieldType, constructor);
if (!dataTypeFactory.hasStructuredType(dataTypeName)) {
dataTypeFactory.registerFactory(schema.id, dataTypeName, constructor);
return constructor;

@@ -39,3 +39,3 @@ // hrow new Error("constructor should now be registered - " + fieldType);

constructor.encodingDefaultXml = encodingDefaultXml;
typeDictionary.associateWithBinaryEncoding(fieldType, encodingDefaultBinary);
dataTypeFactory.associateWithBinaryEncoding(dataTypeName, encodingDefaultBinary);
}

@@ -78,3 +78,3 @@ return constructor;

}
function decodeArrayOrElement(typeDictionary, field, obj, stream, decodeFunc) {
function decodeArrayOrElement(factory, field, obj, stream, decodeFunc) {
if (field.isArray) {

@@ -93,3 +93,3 @@ const array = [];

// construct an instance
const constructor = typeDictionary.getStructureTypeConstructor(field.fieldType);
const constructor = factory.getStructureTypeConstructor(field.fieldType);
const element = new constructor({});

@@ -109,3 +109,3 @@ element.decode(stream);

if (!obj[field.name]) {
const constructor = typeDictionary.getStructureTypeConstructor(field.fieldType);
const constructor = factory.getStructureTypeConstructor(field.fieldType);
obj[field.name] = new constructor({});

@@ -117,11 +117,12 @@ }

}
function initializeField(field, thisAny, options, schema, typeDictionary) {
function initializeField(field, thisAny, options, schema, factory) {
const name = field.name;
switch (field.category) {
case node_opcua_factory_1.FieldCategory.complex: {
const constructor = getOrCreateConstructor(field.fieldType, typeDictionary) || node_opcua_factory_1.BaseUAObject;
const constructor = factory.getStructureTypeConstructor(field.fieldType);
// getOrCreateConstructor(field.fieldType, factory) || BaseUAObject;
if (field.isArray) {
const arr = options[name] || [];
if (!arr.map) {
console.log("Errror", options);
console.log("Error", options);
}

@@ -152,8 +153,8 @@ (thisAny)[name] = arr.map((x) => constructor ? new constructor(x) : null);

* @param schema
* @param typeDictionary
* @param factory
*/
function initializeFields(thisAny, options, schema, typeDictionary) {
function initializeFields(thisAny, options, schema, factory) {
// initialize base class first
if (schema._baseSchema && schema._baseSchema.fields.length) {
initializeFields(thisAny, options, schema._baseSchema, typeDictionary);
initializeFields(thisAny, options, schema._baseSchema, factory);
}

@@ -168,3 +169,3 @@ // finding fields that are in options but not in schema!

}
initializeField(field, thisAny, options, schema, typeDictionary);
initializeField(field, thisAny, options, schema, factory);
}

@@ -213,6 +214,6 @@ }

}
function decodeFields(thisAny, schema, stream, typeDictionary) {
function decodeFields(thisAny, schema, stream, factory) {
// encodeFields base class first
if (schema._baseSchema && schema._baseSchema.fields.length) {
decodeFields(thisAny, schema._baseSchema, stream, typeDictionary);
decodeFields(thisAny, schema._baseSchema, stream, factory);
}

@@ -236,3 +237,3 @@ // ============ Deal with switchBits

// need to create empty structure for deserialisation
initializeField(field, thisAny, {}, schema, typeDictionary);
initializeField(field, thisAny, {}, schema, factory);
}

@@ -243,7 +244,7 @@ }

case node_opcua_factory_1.FieldCategory.complex:
decodeArrayOrElement(typeDictionary, field, thisAny, stream);
decodeArrayOrElement(factory, field, thisAny, stream);
break;
case node_opcua_factory_1.FieldCategory.enumeration:
case node_opcua_factory_1.FieldCategory.basic:
decodeArrayOrElement(typeDictionary, field, thisAny, stream, field.schema.decode);
decodeArrayOrElement(factory, field, thisAny, stream, field.schema.decode);
break;

@@ -257,11 +258,11 @@ default:

class DynamicExtensionObject extends node_opcua_extension_object_1.ExtensionObject {
constructor(options, schema, typeDictionary) {
constructor(options, schema, factory) {
node_opcua_assert_1.assert(schema, "expecting a schema here ");
node_opcua_assert_1.assert(typeDictionary, "expecting a typeDic");
node_opcua_assert_1.assert(factory, "expecting a typeDic");
super(options);
options = options || {};
this.__schema = schema;
this._typeDictionary = typeDictionary;
this._factory = factory;
node_opcua_factory_1.check_options_correctness_against_schema(this, this.schema, options);
initializeFields(this, options, this.schema, typeDictionary);
initializeFields(this, options, this.schema, factory);
}

@@ -274,3 +275,3 @@ encode(stream) {

super.decode(stream);
decodeFields(this, this.schema, stream, this._typeDictionary);
decodeFields(this, this.schema, stream, this._factory);
}

@@ -285,6 +286,6 @@ get schema() {

class UnionBaseClass extends node_opcua_factory_1.BaseUAObject {
constructor(options, schema, typeDictionary) {
constructor(options, schema, factory) {
super();
node_opcua_assert_1.assert(schema, "expecting a schema here ");
node_opcua_assert_1.assert(typeDictionary, "expecting a typeDic");
node_opcua_assert_1.assert(factory, "expecting a typeDic");
options = options || {};

@@ -330,3 +331,4 @@ this.__schema = schema;

case node_opcua_factory_1.FieldCategory.complex: {
const constuctor = getOrCreateConstructor(field.fieldType, typeDictionary) || node_opcua_factory_1.BaseUAObject;
const constuctor = factory.getStructureTypeConstructor(field.fieldType);
// getOrCreateConstructor(field.fieldType, factory) || BaseUAObject;
if (field.isArray) {

@@ -397,3 +399,3 @@ this[name] = (options[name] || []).map((x) => constuctor ? new constuctor(x) : null);

decode(stream) {
const typeDictionary = this.schema.$typeDictionary;
const factory = this.schema.$$factory;
const switchValue = stream.readUInt32();

@@ -408,7 +410,7 @@ const switchFieldName = this.schema.fields[0].name;

case node_opcua_factory_1.FieldCategory.complex:
decodeArrayOrElement(typeDictionary, field, this, stream);
decodeArrayOrElement(factory, field, this, stream);
break;
case node_opcua_factory_1.FieldCategory.enumeration:
case node_opcua_factory_1.FieldCategory.basic:
decodeArrayOrElement(typeDictionary, field, this, stream, field.schema.decode);
decodeArrayOrElement(factory, field, this, stream, field.schema.decode);
break;

@@ -460,3 +462,3 @@ default:

}
function _createDynamicUnionConstructor(schema, typeDictionary) {
function _createDynamicUnionConstructor(schema, factory) {
const possibleFields = schema.fields.map((x) => x.name);

@@ -466,3 +468,3 @@ // tslint:disable-next-line:max-classes-per-file

constructor(options) {
super(options, schema, typeDictionary);
super(options, schema, factory);
node_opcua_assert_1.assert(this.schema === schema);

@@ -477,3 +479,3 @@ }

}
function createDynamicObjectConstructor(schema, typeDictionary) {
function createDynamicObjectConstructor(schema, dataTypeFactory) {
const schemaPriv = schema;

@@ -483,5 +485,7 @@ if (schemaPriv.$Constructor) {

}
const dataTypeNodeId = schemaPriv.id;
if (schema.baseType === "Union") {
const UNIONConstructor = _createDynamicUnionConstructor(schema, typeDictionary);
const UNIONConstructor = _createDynamicUnionConstructor(schema, dataTypeFactory);
schemaPriv.$Constructor = UNIONConstructor;
dataTypeFactory.registerFactory(dataTypeNodeId, schema.name, UNIONConstructor);
return UNIONConstructor;

@@ -491,16 +495,24 @@ }

let BaseClass = DynamicExtensionObject;
if (schema.baseType !== "ExtensionObject") {
BaseClass = getOrCreateConstructor(schema.baseType, typeDictionary);
if (!BaseClass) {
throw new Error("Cannot find base class : " + schema.baseType);
if (schema.baseType !== "ExtensionObject"
&& schema.baseType !== "DataTypeDescription"
&& schema.baseType !== "DataTypeDefinition"
&& schema.baseType !== "EnumValueType") {
try {
BaseClass = getOrCreateConstructor(schema.baseType, dataTypeFactory);
if (!BaseClass) {
throw new Error("Cannot find base class : " + schema.baseType);
}
if (BaseClass.possibleFields) {
possibleFields = BaseClass.possibleFields.concat(possibleFields);
}
schema._baseSchema = BaseClass.schema;
}
if (BaseClass.possibleFields) {
possibleFields = BaseClass.possibleFields.concat(possibleFields);
catch (err) {
console.log(err.message);
}
schema._baseSchema = BaseClass.schema;
}
// tslint:disable-next-line:max-classes-per-file
class EXTENSION extends BaseClass {
constructor(options, schema2, typeDictionary2) {
super(options, schema2 ? schema2 : schema, typeDictionary2 ? typeDictionary2 : typeDictionary);
constructor(options, schema2, factory2) {
super(options, schema2 ? schema2 : schema, factory2 ? factory2 : dataTypeFactory);
}

@@ -518,3 +530,3 @@ toString() {

schemaPriv.$Constructor = EXTENSION;
typeDictionary.registerFactory(schema.name, EXTENSION);
dataTypeFactory.registerFactory(dataTypeNodeId, schema.name, EXTENSION);
return EXTENSION;

@@ -521,0 +533,0 @@ }

export * from "./parse_binary_xsd";
export * from "./dynamic_extension_object";
export * from "./toTypeScript";
export * from "./tools";

@@ -9,2 +9,3 @@ "use strict";

__export(require("./toTypeScript"));
__export(require("./tools"));
//# sourceMappingURL=index.js.map
/**
* @module node-opcua-schemas
*/
import { EnumerationDefinitionSchema, StructuredTypeSchema } from "node-opcua-factory";
import { DataTypeFactory } from "node-opcua-factory";
import { NodeId } from "node-opcua-nodeid";
export interface EnumeratedType {

@@ -21,30 +21,25 @@ name: string;

imports: string[];
structuredTypes: {
[key: string]: StructuredTypeSchema;
};
enumeratedTypes: {
[key: string]: EnumerationDefinitionSchema;
};
structuredTypesRaw: {
[key: string]: StructureTypeRaw;
};
enumeratedTypesRaw: {
[key: string]: EnumeratedType;
};
structuredTypesRaw: StructureTypeRaw[];
enumeratedTypesRaw: EnumeratedType[];
}
export declare class TypeDictionary extends DataTypeFactory implements ITypeDictionary {
structuredTypes: {
[key: string]: StructuredTypeSchema;
};
enumeratedTypes: {
[key: string]: EnumerationDefinitionSchema;
};
structuredTypesRaw: {
[key: string]: StructureTypeRaw;
};
enumeratedTypesRaw: {
[key: string]: EnumeratedType;
};
constructor(baseDataFactories: DataTypeFactory[]);
export declare class TypeDictionary implements ITypeDictionary {
targetNamespace: string;
imports: string[];
structuredTypesRaw: StructureTypeRaw[];
enumeratedTypesRaw: EnumeratedType[];
private structuredTypesRawMap;
constructor();
addRaw(structuredType: StructureTypeRaw): void;
getStructuredTypesRawByName(name: string): StructureTypeRaw;
}
export declare function parseBinaryXSD(xmlString: string, dataTypeFactories: DataTypeFactory[], callback: (err: Error | null, typeDictionary: TypeDictionary) => void): void;
export interface DataTypeAndEncodingId {
dataTypeNodeId: NodeId;
binaryEncodingNodeId: NodeId;
xmlEncodingNodeId: NodeId;
jsonEncodingNodeId: NodeId;
}
export interface MapDataTypeAndEncodingIdProvider {
getDataTypeAndEncodingId(key: string): DataTypeAndEncodingId;
}
export declare function parseBinaryXSD(xmlString: string, idProvider: MapDataTypeAndEncodingIdProvider, dataTypeFactory: DataTypeFactory, callback: (err?: Error | null) => void): void;
export declare function parseBinaryXSDAsync(xmlString: string, idProvider: MapDataTypeAndEncodingIdProvider, dataTypeFactory: DataTypeFactory): Promise<void>;

@@ -8,4 +8,14 @@ "use strict";

// tslint:disable:no-empty
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk = require("chalk");
const node_opcua_assert_1 = require("node-opcua-assert");
const node_opcua_debug_1 = require("node-opcua-debug");

@@ -16,2 +26,3 @@ const node_opcua_factory_1 = require("node-opcua-factory");

const doDebug = node_opcua_debug_1.checkDebugFlag(__filename);
const debugLog = node_opcua_debug_1.make_debugLog(__filename);
function w(s, l) {

@@ -69,10 +80,17 @@ return (s + " ").substr(0, l);

}
class TypeDictionary extends node_opcua_factory_1.DataTypeFactory {
constructor(baseDataFactories) {
super(baseDataFactories);
this.structuredTypes = {};
this.structuredTypesRaw = {};
this.enumeratedTypes = {};
this.enumeratedTypesRaw = {};
class TypeDictionary {
constructor() {
this.targetNamespace = "";
this.imports = [];
this.structuredTypesRaw = [];
this.enumeratedTypesRaw = [];
this.structuredTypesRawMap = {};
}
addRaw(structuredType) {
this.structuredTypesRaw.push(structuredType);
this.structuredTypesRawMap[structuredType.name] = structuredType;
}
getStructuredTypesRawByName(name) {
return this.structuredTypesRawMap[name];
}
}

@@ -100,3 +118,3 @@ exports.TypeDictionary = TypeDictionary;

if (doDebug) {
console.log("Import NameSpace = ", this.attrs.Namespace, " Location", this.attrs.Location);
debugLog("Import NameSpace = ", this.attrs.Namespace, " Location", this.attrs.Location);
}

@@ -109,3 +127,3 @@ }

if (doDebug) {
console.log(chalk.cyan("EnumeratedType Name="), w(this.attrs.Name, 40), "LengthInBits=", this.attrs.LengthInBits);
debugLog(chalk.cyan("EnumeratedType Name="), w(this.attrs.Name, 40), "LengthInBits=", this.attrs.LengthInBits);
}

@@ -128,3 +146,3 @@ this.enumeratedType = {

if (doDebug) {
console.log(" EnumeratedValue Name=", w(this.attrs.Name, 40), " Value=", this.attrs.Value);
debugLog(" EnumeratedValue Name=", w(this.attrs.Name, 40), " Value=", this.attrs.Value);
}

@@ -143,3 +161,3 @@ const key = this.attrs.Name;

if (doDebug) {
console.log(" this.typescriptDefinition = ", this.typescriptDefinition);
debugLog(" this.typescriptDefinition = ", this.typescriptDefinition);
}

@@ -151,6 +169,6 @@ }

if (doDebug) {
console.log(chalk.cyan("StructureType Name="), chalk.green(this.attrs.Name), " BaseType=", this.attrs.BaseType);
debugLog(chalk.cyan("StructureType Name="), chalk.green(this.attrs.Name), " BaseType=", this.attrs.BaseType);
}
const baseType = this.attrs.BaseType;
const base = this.parent.typeDictionary.structuredTypesRaw[baseType];
const base = this.parent.typeDictionary.structuredTypesRawMap[baseType];
const structuredType = {

@@ -174,3 +192,3 @@ name: this.attrs.Name,

if (doDebug) {
console.log(chalk.yellow(" field Name="), w(this.attrs.Name, 40), chalk.yellow(" field TypeName="), w(this.attrs.TypeName, 40), chalk.yellow(" field LengthField="), w(this.attrs.LengthField, 40));
debugLog(chalk.yellow(" field Name="), w(this.attrs.Name, 40), chalk.yellow(" typeName="), w(this.attrs.TypeName, 40), this.attrs.LengthField ? chalk.yellow(" lengthField= ") + w(this.attrs.LengthField, 40) : "", this.attrs.SwitchField ? chalk.yellow(" SwitchField= ") + w(this.attrs.SwitchField, 40) : "", this.attrs.SwitchValue !== undefined ? chalk.yellow(" SwitchValue= ") + w(this.attrs.SwitchValue, 40) : "");
}

@@ -205,3 +223,3 @@ resolveType(this.parent.typeDictionary, this.attrs.TypeName);

if (doDebug) {
console.log("field", field.name, " is part of a union => ", switchField, " value #", field.switchValue);
debugLog("field", field.name, " is part of a union => ", switchField, " value #", field.switchValue);
}

@@ -213,3 +231,3 @@ }

if (doDebug) {
console.log("field", field.name, " is optional => ", switchField, "bit #", field.switchBit);
debugLog("field", field.name, " is optional => ", switchField, "bit #", field.switchBit);
}

@@ -222,3 +240,4 @@ }

finish: function () {
this.parent.typeDictionary.structuredTypesRaw[this.attrs.Name] = this.structuredType;
node_opcua_assert_1.default(this.attrs.Name === this.structuredType.name);
this.parent.typeDictionary.addRaw(this.structuredType);
}

@@ -230,5 +249,5 @@ }

};
function parseBinaryXSD(xmlString, dataTypeFactories, callback) {
const typeDictionary = new TypeDictionary(dataTypeFactories);
function parseBinaryXSD(xmlString, idProvider, dataTypeFactory, callback) {
const parser = new node_opcua_xml2json_1.Xml2Json(state0);
const typeDictionary = new TypeDictionary();
parser.typeDictionary = typeDictionary;

@@ -242,19 +261,74 @@ parser.parseString(xmlString, (err) => {

const enumeratedType = typeDictionary.enumeratedTypesRaw[key];
tools_1.prepareEnumeratedType(enumeratedType, typeDictionary);
const e = new node_opcua_factory_1.EnumerationDefinitionSchema({
enumValues: enumeratedType.enumeratedValues,
name: key
});
dataTypeFactory.registerEnumeration(e);
}
// resolve complex types
for (const key in typeDictionary.structuredTypesRaw) {
if (!typeDictionary.structuredTypesRaw.hasOwnProperty(key)) {
continue;
if (doDebug) {
debugLog("------------------------------- Resolving complex Type");
typeDictionary.structuredTypesRaw.map((x) => debugLog(x.name));
}
// create area in navigation order
function createExplorationOrder() {
const array = [];
const map = {};
function visitStructure(structuredType) {
if (!structuredType) {
return;
}
if (map[structuredType.name]) {
return;
}
if (structuredType.baseType) {
const base = typeDictionary.getStructuredTypesRawByName(structuredType.baseType);
if (base) {
visitStructure(base);
}
}
for (const f of structuredType.fields) {
const fieldType = f.fieldType.split(":")[1];
const s = typeDictionary.getStructuredTypesRawByName(fieldType);
if (s !== structuredType && s) {
visitStructure(s);
}
else {
map[fieldType] = "1";
}
}
if (!map[structuredType.name]) {
map[structuredType.name] = 1;
array.push(structuredType);
}
}
const structuredType = typeDictionary.structuredTypesRaw[key];
if (structuredType.name !== key) {
throw new Error("Invalid name");
for (const structuredType of typeDictionary.structuredTypesRaw) {
visitStructure(structuredType);
}
tools_1.prepareStructureType(structuredType, typeDictionary);
return array;
}
callback(err, typeDictionary);
// resolve complex types
const schemaInVisitingOrder = createExplorationOrder();
for (const structuredType of schemaInVisitingOrder) {
debugLog("processing ", chalk.cyan(structuredType.name));
tools_1.getOrCreateStructuredTypeSchema(structuredType.name, typeDictionary, dataTypeFactory, idProvider);
}
callback(err);
});
}
exports.parseBinaryXSD = parseBinaryXSD;
function parseBinaryXSDAsync(xmlString, idProvider, dataTypeFactory) {
return __awaiter(this, void 0, void 0, function* () {
yield new Promise((resolve, reject) => {
parseBinaryXSD(xmlString, idProvider, dataTypeFactory, (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
});
}
exports.parseBinaryXSDAsync = parseBinaryXSDAsync;
//# sourceMappingURL=parse_binary_xsd.js.map

@@ -1,5 +0,3 @@

import { StructuredTypeSchema } from "node-opcua-factory";
import { EnumeratedType, StructureTypeRaw, TypeDictionary } from "./parse_binary_xsd";
export declare function getOrCreateStructuredTypeSchema(name: string, typeDictionary: TypeDictionary): StructuredTypeSchema;
export declare function prepareStructureType(structuredType: StructureTypeRaw, typeDictionary: TypeDictionary): StructuredTypeSchema;
export declare function prepareEnumeratedType(enumeratedType: EnumeratedType, typeDictionary: TypeDictionary): void;
import { DataTypeFactory, StructuredTypeSchema } from "node-opcua-factory";
import { MapDataTypeAndEncodingIdProvider, TypeDictionary } from "./parse_binary_xsd";
export declare function getOrCreateStructuredTypeSchema(name: string, typeDictionary: TypeDictionary, dataTypeFactory: DataTypeFactory, idProvider: MapDataTypeAndEncodingIdProvider): StructuredTypeSchema;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const node_opcua_factory_1 = require("node-opcua-factory");
function removeNamespacePart(str) {
const dynamic_extension_object_1 = require("./dynamic_extension_object");
const node_opcua_nodeid_1 = require("node-opcua-nodeid");
function _removeNamespacePart(str) {
if (!str) {

@@ -11,6 +13,6 @@ return str;

}
function getNamespacePart(str) {
function _getNamespacePart(str) {
return str.split(":")[0];
}
function adjustFieldTypeName(fieldTypeName) {
function _adjustFieldTypeName(fieldTypeName) {
// special cases

@@ -25,104 +27,102 @@ if (fieldTypeName === "String" || fieldTypeName === "CharArray") {

}
function getOrCreateStructuredTypeSchema(name, typeDictionary) {
let structuredTypeSchema = typeDictionary.structuredTypes[name];
if (structuredTypeSchema) {
return structuredTypeSchema;
}
// construct it !
const structuredType = typeDictionary.structuredTypesRaw[name];
if (!structuredType) {
throw new Error("Cannot find structuredType" + name);
}
structuredType.baseType = removeNamespacePart(structuredType.baseType);
structuredType.baseType = structuredType.baseType ? structuredType.baseType : "BaseUAObject";
for (const field of structuredType.fields) {
const fieldType = field.fieldType;
if (!field.schema) {
const prefix = getNamespacePart(fieldType);
const fieldTypeName = adjustFieldTypeName(removeNamespacePart(fieldType));
switch (prefix) {
case "tns":
field.fieldType = fieldTypeName;
const enumeratedType = typeDictionary.enumeratedTypes[fieldTypeName];
if (enumeratedType) {
field.category = node_opcua_factory_1.FieldCategory.enumeration;
field.schema = enumeratedType;
}
else {
// must be a structure then ....
field.category = node_opcua_factory_1.FieldCategory.complex;
field.schema = getOrCreateStructuredTypeSchema(fieldTypeName, typeDictionary);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("cannot find schema for ", fieldTypeName);
function getOrCreateStructuredTypeSchema(name, typeDictionary, dataTypeFactory, idProvider) {
function _getOrCreateStructuredTypeSchema(_name) {
if (dataTypeFactory.hasStructuredType(_name)) {
return dataTypeFactory.getStructuredTypeSchema(_name);
}
// construct it !
const structuredType = typeDictionary.getStructuredTypesRawByName(_name);
if (!structuredType) {
throw new Error("Cannot find structuredType " + _name);
}
structuredType.baseType = _removeNamespacePart(structuredType.baseType);
structuredType.baseType = structuredType.baseType ? structuredType.baseType : "ExtensionObject";
for (const field of structuredType.fields) {
const fieldType = field.fieldType;
if (!field.schema) {
const prefix = _getNamespacePart(fieldType);
const fieldTypeName = _adjustFieldTypeName(_removeNamespacePart(fieldType));
switch (prefix) {
case "tns":
field.fieldType = fieldTypeName;
if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
const enumeratedType = dataTypeFactory.getEnumeration(fieldTypeName);
field.category = node_opcua_factory_1.FieldCategory.enumeration;
field.schema = enumeratedType;
}
}
break;
case "ua":
field.fieldType = fieldTypeName;
if (node_opcua_factory_1.hasBuiltInType(fieldTypeName)) {
field.category = node_opcua_factory_1.FieldCategory.basic;
field.schema = node_opcua_factory_1.getBuildInType(fieldTypeName);
}
else if (node_opcua_factory_1.hasStructuredType(fieldTypeName)) {
field.category = node_opcua_factory_1.FieldCategory.complex;
field.schema = node_opcua_factory_1.getStructuredTypeSchema(fieldTypeName);
}
else {
field.category = node_opcua_factory_1.FieldCategory.basic;
// try in this
field.schema = getOrCreateStructuredTypeSchema(fieldTypeName, typeDictionary);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("What should I do ??", fieldTypeName, " ", node_opcua_factory_1.hasStructuredType(fieldTypeName));
else {
// must be a structure then ....
field.category = node_opcua_factory_1.FieldCategory.complex;
field.schema = _getOrCreateStructuredTypeSchema(fieldTypeName);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("cannot find schema for ", fieldTypeName);
}
}
break;
case "ua":
field.fieldType = fieldTypeName;
if (node_opcua_factory_1.hasBuiltInType(fieldTypeName)) {
field.category = node_opcua_factory_1.FieldCategory.basic;
field.schema = node_opcua_factory_1.getBuildInType(fieldTypeName);
}
else if (node_opcua_factory_1.hasStructuredType(fieldTypeName)) {
field.category = node_opcua_factory_1.FieldCategory.complex;
field.schema = node_opcua_factory_1.getStructuredTypeSchema(fieldTypeName);
}
else {
if (node_opcua_factory_1.hasBuiltInType(fieldTypeName)) {
field.category = node_opcua_factory_1.FieldCategory.basic;
field.category = node_opcua_factory_1.FieldCategory.basic;
// try in this
field.schema = _getOrCreateStructuredTypeSchema(fieldTypeName);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("What should I do ??", fieldTypeName, " ", node_opcua_factory_1.hasStructuredType(fieldTypeName));
}
else {
field.category = node_opcua_factory_1.FieldCategory.complex;
if (node_opcua_factory_1.hasBuiltInType(fieldTypeName)) {
field.category = node_opcua_factory_1.FieldCategory.basic;
}
else {
field.category = node_opcua_factory_1.FieldCategory.complex;
}
}
}
}
break;
case "opc":
if ((fieldTypeName === "UAString" || fieldTypeName === "String") && field.name === "IndexRange") {
field.fieldType = "NumericRange";
// xx console.log(" NumericRange detected here !");
}
else {
field.fieldType = fieldTypeName;
}
if (!node_opcua_factory_1.hasBuiltInType(fieldTypeName)) {
throw new Error("Unknown basic type " + fieldTypeName);
}
field.category = node_opcua_factory_1.FieldCategory.basic;
break;
break;
case "opc":
if ((fieldTypeName === "UAString" || fieldTypeName === "String") && field.name === "IndexRange") {
field.fieldType = "NumericRange";
// xx console.log(" NumericRange detected here !");
}
else {
field.fieldType = fieldTypeName;
}
if (!node_opcua_factory_1.hasBuiltInType(fieldTypeName)) {
throw new Error("Unknown basic type " + fieldTypeName);
}
field.category = node_opcua_factory_1.FieldCategory.basic;
break;
}
}
}
const schema = node_opcua_factory_1.buildStructuredType(structuredType);
const ids = idProvider.getDataTypeAndEncodingId(schema.name);
if (!ids) {
throw new Error("Cannot find getDataTypeAndEncodingId for " + schema.name);
}
schema.id = ids.dataTypeNodeId;
schema.dataTypeNodeId = ids.dataTypeNodeId;
if (schema.id.namespace === 0 && schema.id.value === 0) {
return schema;
}
schema.encodingDefaultXml = node_opcua_nodeid_1.ExpandedNodeId.fromNodeId(ids.xmlEncodingNodeId);
schema.encodingDefaultJson = node_opcua_nodeid_1.ExpandedNodeId.fromNodeId(ids.jsonEncodingNodeId);
schema.encodingDefaultBinary = node_opcua_nodeid_1.ExpandedNodeId.fromNodeId(ids.binaryEncodingNodeId);
const Constructor = dynamic_extension_object_1.createDynamicObjectConstructor(schema, dataTypeFactory);
Constructor.encodingDefaultBinary = schema.encodingDefaultBinary;
Constructor.encodingDefaultXml = schema.encodingDefaultXml;
return schema;
}
structuredTypeSchema = node_opcua_factory_1.buildStructuredType(structuredType);
typeDictionary.structuredTypes[name] = structuredTypeSchema;
return structuredTypeSchema;
return _getOrCreateStructuredTypeSchema(name);
}
exports.getOrCreateStructuredTypeSchema = getOrCreateStructuredTypeSchema;
function prepareStructureType(structuredType, typeDictionary) {
const key = structuredType.name;
if (typeDictionary.structuredTypes[key]) {
return typeDictionary.structuredTypes[key]; // already done
}
typeDictionary.structuredTypes[key] = getOrCreateStructuredTypeSchema(key, typeDictionary);
return typeDictionary.structuredTypes[key];
}
exports.prepareStructureType = prepareStructureType;
function prepareEnumeratedType(enumeratedType, typeDictionary) {
const key = enumeratedType.name;
const e = new node_opcua_factory_1.EnumerationDefinitionSchema({
enumValues: enumeratedType.enumeratedValues,
name: key
});
typeDictionary.enumeratedTypes[key] = e;
}
exports.prepareEnumeratedType = prepareEnumeratedType;
//# sourceMappingURL=tools.js.map

@@ -1,2 +0,2 @@

import { TypeDictionary } from "./parse_binary_xsd";
export declare function toTypeScript(typeDictionnary: TypeDictionary): string;
import { DataTypeFactory } from "node-opcua-factory";
export declare function toTypeScript(dataTypeFactory: DataTypeFactory): string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function toTypeScript(typeDictionnary) {
function toTypeScript(dataTypeFactory) {
const enumeratedTypes = dataTypeFactory._enumerations;
const structuredTypes = dataTypeFactory._structureTypeConstructorByNameMap;
const declaration = {};
function adjustType(t) {
if (!typeDictionnary.enumeratedTypes[t] && !typeDictionnary.structuredTypes[t]) {
if (!enumeratedTypes[t] && !structuredTypes[t]) {
declaration[t] = t;

@@ -13,3 +15,3 @@ }

// enumeration
for (const e of Object.values(typeDictionnary.enumeratedTypes)) {
for (const e of Object.values(enumeratedTypes)) {
l.push(`export enum ${e.name} {`);

@@ -28,5 +30,6 @@ // console.log((e.typedEnum as any).enumItems);

function dumpType(o) {
var _a;
// base type first
const b = o.baseType;
const bt = typeDictionnary.structuredTypes[b];
const bt = (_a = structuredTypes[b]) === null || _a === void 0 ? void 0 : _a.schema;
if (b && !alreadyDone[o.baseType] && bt) {

@@ -87,7 +90,7 @@ dumpType(bt);

// objects
for (const o of Object.values(typeDictionnary.structuredTypes)) {
if (alreadyDone[o.name]) {
for (const o of Object.values(structuredTypes)) {
if (alreadyDone[o.schema.name]) {
continue;
}
dumpType(o);
dumpType(o.schema);
}

@@ -94,0 +97,0 @@ const opcuatypes = Object.keys(declaration).sort().join(",\n ");

{
"name": "node-opcua-schemas",
"version": "2.4.2",
"description": "pure nodejs OPCUA SDK - module -schemas",
"main": "dist/source/index.js",
"types": "dist/source/index.d.ts",
"scripts": {
"build": "tsc",
"test": "echo no test"
},
"dependencies": {
"node-opcua-assert": "^2.3.0",
"node-opcua-binary-stream": "^2.4.2",
"node-opcua-data-model": "^2.4.2",
"node-opcua-debug": "^2.4.2",
"node-opcua-enum": "^2.4.2",
"node-opcua-extension-object": "^2.4.2",
"node-opcua-factory": "^2.4.2",
"node-opcua-nodeid": "^2.4.2",
"node-opcua-variant": "^2.4.2",
"node-opcua-xml2json": "^2.4.2"
},
"devDependencies": {
"node-opcua-packet-analyzer": "^2.4.2"
},
"author": "Etienne Rossignon",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/node-opcua/node-opcua.git"
},
"keywords": [
"OPCUA",
"opcua",
"m2m",
"iot",
"opc ua",
"internet of things"
],
"homepage": "http://node-opcua.github.io/",
"gitHead": "6e4967fd20a09317ddd00098bcfe93e92c1df510"
"name": "node-opcua-schemas",
"version": "2.5.0-alpha.0",
"description": "pure nodejs OPCUA SDK - module -schemas",
"main": "dist/source/index.js",
"types": "dist/source/index.d.ts",
"scripts": {
"build": "tsc -b",
"test": "echo no test"
},
"dependencies": {
"node-opcua-assert": "^2.5.0-alpha.0",
"node-opcua-binary-stream": "^2.5.0-alpha.0",
"node-opcua-data-model": "^2.5.0-alpha.0",
"node-opcua-debug": "^2.5.0-alpha.0",
"node-opcua-enum": "^2.5.0-alpha.0",
"node-opcua-extension-object": "^2.5.0-alpha.0",
"node-opcua-factory": "^2.5.0-alpha.0",
"node-opcua-nodeid": "^2.5.0-alpha.0",
"node-opcua-utils": "^2.5.0-alpha.0",
"node-opcua-variant": "^2.5.0-alpha.0",
"node-opcua-xml2json": "^2.5.0-alpha.0",
"thenify": "^3.3.0"
},
"devDependencies": {
"node-opcua-packet-analyzer": "^2.5.0-alpha.0"
},
"author": "Etienne Rossignon",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/node-opcua/node-opcua.git"
},
"keywords": [
"OPCUA",
"opcua",
"m2m",
"iot",
"opc ua",
"internet of things"
],
"homepage": "http://node-opcua.github.io/",
"gitHead": "341ac6292b30304bb028ab10971dc8552ff1a35a"
}

@@ -12,2 +12,3 @@ /**

ConstructorFuncWithSchema,
DataTypeFactory,
FieldCategory,

@@ -20,4 +21,3 @@ FieldType,

import { ExpandedNodeId, NodeIdType } from "node-opcua-nodeid";
import { TypeDictionary } from "./parse_binary_xsd";
import { ExpandedNodeId, NodeId, NodeIdType } from "node-opcua-nodeid";

@@ -28,4 +28,4 @@ const debugLog = make_debugLog(__filename);

export function getOrCreateConstructor(
fieldType: string,
typeDictionary: TypeDictionary,
dataTypeName: string,
dataTypeFactory: DataTypeFactory,
encodingDefaultBinary?: ExpandedNodeId,

@@ -35,13 +35,13 @@ encodingDefaultXml?: ExpandedNodeId

if (typeDictionary.hasStructuredType(fieldType)) {
return typeDictionary.getStructureTypeConstructor(fieldType);
if (dataTypeFactory.hasStructuredType(dataTypeName)) {
return dataTypeFactory.getStructureTypeConstructor(dataTypeName);
}
const schema = typeDictionary.structuredTypes[fieldType];
const schema = dataTypeFactory.getStructuredTypeSchema(dataTypeName);
// istanbul ignore next
if (!schema) {
throw new Error("Unknown type in dictionary " + fieldType);
throw new Error("Unknown type in dictionary " + dataTypeName);
}
const constructor = createDynamicObjectConstructor(schema, typeDictionary);
const constructor = createDynamicObjectConstructor(schema, dataTypeFactory);

@@ -52,5 +52,4 @@ if (!constructor) {

// istanbul ignore next
if (!typeDictionary.hasStructuredType(fieldType)) {
typeDictionary.registerFactory(fieldType, constructor as ConstructorFuncWithSchema);
if (!dataTypeFactory.hasStructuredType(dataTypeName)) {
dataTypeFactory.registerFactory(schema.id, dataTypeName, constructor as ConstructorFuncWithSchema);
return constructor;

@@ -65,3 +64,3 @@ // hrow new Error("constructor should now be registered - " + fieldType);

(constructor as any).encodingDefaultXml = encodingDefaultXml;
typeDictionary.associateWithBinaryEncoding(fieldType, encodingDefaultBinary);
dataTypeFactory.associateWithBinaryEncoding(dataTypeName, encodingDefaultBinary);
}

@@ -72,3 +71,2 @@ return constructor;

function encodeArrayOrElement(
field: FieldType,

@@ -109,3 +107,3 @@ obj: any,

function decodeArrayOrElement(
typeDictionary: TypeDictionary,
factory: DataTypeFactory,
field: FieldType,

@@ -128,3 +126,3 @@ obj: any,

// construct an instance
const constructor = typeDictionary.getStructureTypeConstructor(field.fieldType);
const constructor = factory.getStructureTypeConstructor(field.fieldType);
const element = new constructor({});

@@ -142,3 +140,3 @@ element.decode(stream);

if (!obj[field.name]) {
const constructor = typeDictionary.getStructureTypeConstructor(field.fieldType);
const constructor = factory.getStructureTypeConstructor(field.fieldType);
obj[field.name] = new constructor({});

@@ -156,3 +154,3 @@ }

schema: StructuredTypeSchema,
typeDictionary: TypeDictionary
factory: DataTypeFactory
) {

@@ -164,7 +162,8 @@

case FieldCategory.complex: {
const constructor = getOrCreateConstructor(field.fieldType, typeDictionary) || BaseUAObject;
const constructor = factory.getStructureTypeConstructor(field.fieldType);
// getOrCreateConstructor(field.fieldType, factory) || BaseUAObject;
if (field.isArray) {
const arr =options[name] || [];
const arr = options[name] || [];
if (!arr.map) {
console.log("Errror", options);
console.log("Error", options);
}

@@ -189,3 +188,2 @@ (thisAny)[name] = arr.map((x: any) =>

}
}

@@ -197,9 +195,9 @@ /**

* @param schema
* @param typeDictionary
* @param factory
*/
function initializeFields(thisAny: any, options: any, schema: StructuredTypeSchema, typeDictionary: TypeDictionary) {
function initializeFields(thisAny: any, options: any, schema: StructuredTypeSchema, factory: DataTypeFactory) {
// initialize base class first
if (schema._baseSchema && schema._baseSchema.fields.length) {
initializeFields(thisAny, options, schema._baseSchema!, typeDictionary);
initializeFields(thisAny, options, schema._baseSchema!, factory);
}

@@ -216,3 +214,3 @@ // finding fields that are in options but not in schema!

}
initializeField(field, thisAny, options, schema, typeDictionary);
initializeField(field, thisAny, options, schema, factory);
}

@@ -275,3 +273,3 @@

stream: BinaryStream,
typeDictionary: TypeDictionary
factory: DataTypeFactory
) {

@@ -281,3 +279,3 @@

if (schema._baseSchema && schema._baseSchema.fields.length) {
decodeFields(thisAny, schema._baseSchema!, stream, typeDictionary);
decodeFields(thisAny, schema._baseSchema!, stream, factory);
}

@@ -295,3 +293,3 @@

// ignore fields that have a switch bit when bit is not set
if (hasOptionalFields && field.switchBit !== undefined) {
if (hasOptionalFields && field.switchBit !== undefined) {
// tslint:disable-next-line:no-bitwise

@@ -304,3 +302,3 @@ if ((bitField & (1 << field.switchBit)) === 0) {

// need to create empty structure for deserialisation
initializeField(field, thisAny, {}, schema, typeDictionary);
initializeField(field, thisAny, {}, schema, factory);
}

@@ -312,7 +310,7 @@ }

case FieldCategory.complex:
decodeArrayOrElement(typeDictionary, field, thisAny, stream);
decodeArrayOrElement(factory, field, thisAny, stream);
break;
case FieldCategory.enumeration:
case FieldCategory.basic:
decodeArrayOrElement(typeDictionary, field, thisAny, stream, field.schema.decode);
decodeArrayOrElement(factory, field, thisAny, stream, field.schema.decode);
break;

@@ -330,8 +328,8 @@ default:

public static possibleFields: string[] = [];
private readonly _typeDictionary: TypeDictionary;
private readonly _factory: DataTypeFactory;
private readonly __schema?: StructuredTypeSchema;
constructor(options: any, schema: StructuredTypeSchema, typeDictionary: TypeDictionary) {
constructor(options: any, schema: StructuredTypeSchema, factory: DataTypeFactory) {
assert(schema, "expecting a schema here ");
assert(typeDictionary, "expecting a typeDic");
assert(factory, "expecting a typeDic");

@@ -342,7 +340,7 @@ super(options);

this._typeDictionary = typeDictionary;
this._factory = factory;
check_options_correctness_against_schema(this, this.schema, options);
initializeFields(this as any, options, this.schema, typeDictionary);
initializeFields(this as any, options, this.schema, factory);
}

@@ -357,3 +355,3 @@

super.decode(stream);
decodeFields(this as any, this.schema, stream, this._typeDictionary);
decodeFields(this as any, this.schema, stream, this._factory);
}

@@ -371,3 +369,3 @@

possibleFields: string[];
new(options?: any, schema?: StructuredTypeSchema, typeDictionary?: TypeDictionary): any;
new(options?: any, schema?: StructuredTypeSchema, factory?: DataTypeFactory): any;
}

@@ -382,7 +380,7 @@

constructor(options: any, schema: StructuredTypeSchema, typeDictionary: TypeDictionary) {
constructor(options: any, schema: StructuredTypeSchema, factory: DataTypeFactory) {
super();
assert(schema, "expecting a schema here ");
assert(typeDictionary, "expecting a typeDic");
assert(factory, "expecting a typeDic");
options = options || {};

@@ -434,3 +432,4 @@ this.__schema = schema;

case FieldCategory.complex: {
const constuctor = getOrCreateConstructor(field.fieldType, typeDictionary) || BaseUAObject;
const constuctor = factory.getStructureTypeConstructor(field.fieldType);
// getOrCreateConstructor(field.fieldType, factory) || BaseUAObject;
if (field.isArray) {

@@ -507,3 +506,3 @@ (this as any)[name] = (options[name] || []).map((x: any) =>

const typeDictionary: TypeDictionary = (this.schema as any).$typeDictionary;
const factory: DataTypeFactory = (this.schema as any).$$factory;

@@ -523,7 +522,7 @@ const switchValue = stream.readUInt32();

case FieldCategory.complex:
decodeArrayOrElement(typeDictionary, field, this as any, stream);
decodeArrayOrElement(factory, field, this as any, stream);
break;
case FieldCategory.enumeration:
case FieldCategory.basic:
decodeArrayOrElement(typeDictionary, field, this as any, stream, field.schema.decode);
decodeArrayOrElement(factory, field, this as any, stream, field.schema.decode);
break;

@@ -586,3 +585,3 @@ default:

schema: StructuredTypeSchema,
typeDictionary: TypeDictionary
factory: DataTypeFactory
): AnyConstructorFunc {

@@ -598,3 +597,3 @@

constructor(options?: any) {
super(options, schema, typeDictionary);
super(options, schema, factory);
assert(this.schema === schema);

@@ -613,13 +612,16 @@ }

schema: StructuredTypeSchema,
typeDictionary: TypeDictionary
dataTypeFactory: DataTypeFactory
): AnyConstructorFunc {
const schemaPriv = schema as any;
if (schemaPriv.$Constructor) {
return schemaPriv.$Constructor;
}
const dataTypeNodeId = schemaPriv.id;
if (schema.baseType === "Union") {
const UNIONConstructor = _createDynamicUnionConstructor(schema, typeDictionary);
const UNIONConstructor = _createDynamicUnionConstructor(schema, dataTypeFactory);
schemaPriv.$Constructor = UNIONConstructor;
dataTypeFactory.registerFactory(dataTypeNodeId, schema.name, UNIONConstructor as any);
return UNIONConstructor;

@@ -631,12 +633,22 @@ }

let BaseClass: AnyConstructorFunc = DynamicExtensionObject as AnyConstructorFunc;
if (schema.baseType !== "ExtensionObject") {
BaseClass = getOrCreateConstructor(schema.baseType, typeDictionary);
if (!BaseClass) {
throw new Error("Cannot find base class : " + schema.baseType);
if (schema.baseType !== "ExtensionObject"
&& schema.baseType !== "DataTypeDescription"
&& schema.baseType !== "DataTypeDefinition"
&& schema.baseType !== "EnumValueType"
) {
try {
BaseClass = getOrCreateConstructor(schema.baseType, dataTypeFactory);
if (!BaseClass) {
throw new Error("Cannot find base class : " + schema.baseType);
}
if ((BaseClass as any).possibleFields) {
possibleFields = (BaseClass as any).possibleFields.concat(possibleFields);
}
schema._baseSchema = BaseClass.schema;
} catch (err) {
console.log(err.message);
}
if ((BaseClass as any).possibleFields) {
possibleFields = (BaseClass as any).possibleFields.concat(possibleFields);
}
schema._baseSchema = BaseClass.schema;
}

@@ -651,4 +663,4 @@

constructor(options?: any, schema2?: StructuredTypeSchema, typeDictionary2?: TypeDictionary) {
super(options, schema2 ? schema2 : schema, typeDictionary2 ? typeDictionary2 : typeDictionary);
constructor(options?: any, schema2?: StructuredTypeSchema, factory2?: DataTypeFactory) {
super(options, schema2 ? schema2 : schema, factory2 ? factory2 : dataTypeFactory);
}

@@ -667,5 +679,5 @@

typeDictionary.registerFactory(schema.name, EXTENSION as any);
dataTypeFactory.registerFactory(dataTypeNodeId, schema.name, EXTENSION as any);
return EXTENSION;
}
export * from "./parse_binary_xsd";
export * from "./dynamic_extension_object";
export * from "./toTypeScript";
export * from "./toTypeScript";
export * from "./tools";

@@ -9,3 +9,9 @@ /**

import * as chalk from "chalk";
import assert from "node-opcua-assert";
import {
checkDebugFlag,
make_debugLog
} from "node-opcua-debug";
import {
EnumerationDefinitionSchema,

@@ -17,9 +23,12 @@ FieldInterfaceOptions,

} from "node-opcua-factory";
import { checkDebugFlag } from "node-opcua-debug";
import { DataTypeFactory } from "node-opcua-factory";
import { NodeId } from "node-opcua-nodeid";
import { Xml2Json } from "node-opcua-xml2json";
import { prepareEnumeratedType, prepareStructureType } from "./tools";
import {
getOrCreateStructuredTypeSchema,
} from "./tools";
const doDebug = checkDebugFlag(__filename);
const debugLog = make_debugLog(__filename);

@@ -101,29 +110,21 @@ function w(s: string, l: number): string {

imports: string[];
structuredTypes: { [key: string]: StructuredTypeSchema; };
enumeratedTypes: { [key: string]: EnumerationDefinitionSchema; };
structuredTypesRaw: { [key: string]: StructureTypeRaw };
enumeratedTypesRaw: { [key: string]: EnumeratedType; };
structuredTypesRaw: StructureTypeRaw[];
enumeratedTypesRaw: EnumeratedType[];
}
export class TypeDictionary extends DataTypeFactory implements ITypeDictionary {
public structuredTypes: {
[key: string]: StructuredTypeSchema;
};
public enumeratedTypes: {
[key: string]: EnumerationDefinitionSchema;
};
public structuredTypesRaw: {
[key: string]: StructureTypeRaw;
};
public enumeratedTypesRaw: {
[key: string]: EnumeratedType;
};
constructor(baseDataFactories: DataTypeFactory[]) {
super(baseDataFactories);
this.structuredTypes = {};
this.structuredTypesRaw = {};
this.enumeratedTypes = {};
this.enumeratedTypesRaw = {};
export class TypeDictionary implements ITypeDictionary {
public targetNamespace: string = "";
public imports: string[] = [];
public structuredTypesRaw: StructureTypeRaw[] = [];
public enumeratedTypesRaw: EnumeratedType[] = [];
private structuredTypesRawMap: any = {};
constructor() {
}
public addRaw(structuredType: StructureTypeRaw) {
this.structuredTypesRaw.push(structuredType);
this.structuredTypesRawMap[structuredType.name] = structuredType;
}
public getStructuredTypesRawByName(name: string): StructureTypeRaw {
return this.structuredTypesRawMap[name]! as StructureTypeRaw;
}
}

@@ -140,3 +141,3 @@

this.typeDictionary = this.engine.typeDictionary as TypeDictionary;
this.typeDictionary = this.engine.typeDictionary as DataTypeFactory;
this.typeDictionary.defaultByteOrder = attributes.DefaultByteOrder;

@@ -154,3 +155,3 @@ this.typeDictionary.targetNamespace = attributes.TargetNamespace;

if (doDebug) {
console.log("Import NameSpace = ", this.attrs.Namespace,
debugLog("Import NameSpace = ", this.attrs.Namespace,
" Location", this.attrs.Location);

@@ -166,3 +167,3 @@ }

if (doDebug) {
console.log(chalk.cyan("EnumeratedType Name="),
debugLog(chalk.cyan("EnumeratedType Name="),
w(this.attrs.Name, 40), "LengthInBits=", this.attrs.LengthInBits);

@@ -188,3 +189,3 @@ }

if (doDebug) {
console.log(" EnumeratedValue Name=",
debugLog(" EnumeratedValue Name=",
w(this.attrs.Name, 40), " Value=", this.attrs.Value);

@@ -204,3 +205,3 @@ }

if (doDebug) {
console.log(" this.typescriptDefinition = ", this.typescriptDefinition);
debugLog(" this.typescriptDefinition = ", this.typescriptDefinition);
}

@@ -213,3 +214,3 @@ }

if (doDebug) {
console.log(chalk.cyan("StructureType Name="),
debugLog(chalk.cyan("StructureType Name="),
chalk.green(this.attrs.Name), " BaseType=", this.attrs.BaseType);

@@ -220,3 +221,3 @@ }

const base = this.parent.typeDictionary.structuredTypesRaw[baseType];
const base = this.parent.typeDictionary.structuredTypesRawMap[baseType];

@@ -242,6 +243,10 @@ const structuredType: StructuredTypeOptions = {

if (doDebug) {
console.log(
debugLog(
chalk.yellow(" field Name="), w(this.attrs.Name, 40),
chalk.yellow(" field TypeName="), w(this.attrs.TypeName, 40),
chalk.yellow(" field LengthField="), w(this.attrs.LengthField, 40));
chalk.yellow(" typeName="), w(this.attrs.TypeName, 40),
this.attrs.LengthField ? chalk.yellow(" lengthField= ") + w(this.attrs.LengthField, 40) : "",
this.attrs.SwitchField ? chalk.yellow(" SwitchField= ") + w(this.attrs.SwitchField, 40) : "",
this.attrs.SwitchValue !== undefined ? chalk.yellow(" SwitchValue= ") + w(this.attrs.SwitchValue, 40) : "",
//chalk.yellow(" lengthField="), w(this.attrs.LengthField, 40)
);
}

@@ -280,3 +285,3 @@ resolveType(this.parent.typeDictionary, this.attrs.TypeName);

if (doDebug) {
console.log("field", field.name, " is part of a union => ", switchField, " value #", field.switchValue);
debugLog("field", field.name, " is part of a union => ", switchField, " value #", field.switchValue);
}

@@ -287,3 +292,3 @@ } else {

if (doDebug) {
console.log("field", field.name, " is optional => ", switchField, "bit #", field.switchBit);
debugLog("field", field.name, " is optional => ", switchField, "bit #", field.switchBit);
}

@@ -296,3 +301,4 @@ }

finish: function (this: any) {
this.parent.typeDictionary.structuredTypesRaw[this.attrs.Name] = this.structuredType;
assert(this.attrs.Name === this.structuredType.name);
this.parent.typeDictionary.addRaw(this.structuredType);
}

@@ -305,13 +311,26 @@ }

export interface DataTypeAndEncodingId {
dataTypeNodeId: NodeId;
binaryEncodingNodeId: NodeId;
xmlEncodingNodeId: NodeId;
jsonEncodingNodeId: NodeId;
}
export interface MapDataTypeAndEncodingIdProvider {
// getDataTypeNodeId(key: string): NodeId;
getDataTypeAndEncodingId(key: string): DataTypeAndEncodingId;
}
export function parseBinaryXSD(
xmlString: string,
dataTypeFactories: DataTypeFactory[],
callback: (err: Error | null, typeDictionary: TypeDictionary
) => void) {
idProvider: MapDataTypeAndEncodingIdProvider,
dataTypeFactory: DataTypeFactory,
callback: (err?: Error | null) => void
) {
const typeDictionary = new TypeDictionary(dataTypeFactories);
const parser = new Xml2Json(state0);
const typeDictionary = new TypeDictionary();
(parser as any).typeDictionary = typeDictionary;
parser.parseString(xmlString, (err?: Error | null) => {
// resolve and prepare enumerations

@@ -323,18 +342,77 @@ for (const key in typeDictionary.enumeratedTypesRaw) {

const enumeratedType = typeDictionary.enumeratedTypesRaw[key];
prepareEnumeratedType(enumeratedType, typeDictionary);
const e = new EnumerationDefinitionSchema({
enumValues: enumeratedType.enumeratedValues,
name: key
});
dataTypeFactory.registerEnumeration(e);
}
// resolve complex types
for (const key in typeDictionary.structuredTypesRaw) {
if (!typeDictionary.structuredTypesRaw.hasOwnProperty(key)) {
continue;
if (doDebug) {
debugLog("------------------------------- Resolving complex Type");
typeDictionary.structuredTypesRaw.map((x: any) => debugLog(x.name));
}
// create area in navigation order
function createExplorationOrder(): StructureTypeRaw[] {
const array: StructureTypeRaw[] = [];
const map: any = {};
function visitStructure(structuredType: StructureTypeRaw) {
if (!structuredType) {
return;
}
if (map[structuredType.name]) {
return;
}
if (structuredType.baseType) {
const base = typeDictionary.getStructuredTypesRawByName(structuredType.baseType);
if (base) {
visitStructure(base);
}
}
for (const f of structuredType.fields) {
const fieldType = f.fieldType.split(":")[1];
const s = typeDictionary.getStructuredTypesRawByName(fieldType);
if (s !== structuredType && s) {
visitStructure(s);
} else {
map[fieldType] = "1";
}
}
if (!map[structuredType.name]) {
map[structuredType.name] = 1;
array.push(structuredType);
}
}
const structuredType = typeDictionary.structuredTypesRaw[key];
if (structuredType.name !== key) {
throw new Error("Invalid name");
for (const structuredType of typeDictionary.structuredTypesRaw) {
visitStructure(structuredType);
}
prepareStructureType(structuredType, typeDictionary);
return array;
}
callback(err!, typeDictionary);
// resolve complex types
const schemaInVisitingOrder = createExplorationOrder();
for (const structuredType of schemaInVisitingOrder) {
debugLog("processing ", chalk.cyan(structuredType.name));
getOrCreateStructuredTypeSchema(structuredType.name, typeDictionary, dataTypeFactory, idProvider);
}
callback(err);
});
}
export async function parseBinaryXSDAsync(
xmlString: string,
idProvider: MapDataTypeAndEncodingIdProvider,
dataTypeFactory: DataTypeFactory
): Promise<void> {
await new Promise((resolve, reject) => {
parseBinaryXSD(xmlString, idProvider, dataTypeFactory, (err?: Error | null) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
import {
buildStructuredType,
EnumerationDefinitionSchema,
ConstructorFuncWithSchema,
DataTypeFactory,
FieldCategory,

@@ -10,7 +11,14 @@ getBuildInType,

StructuredTypeOptions,
StructuredTypeSchema
StructuredTypeSchema,
} from "node-opcua-factory";
import { EnumeratedType, StructureTypeRaw, TypeDictionary } from "./parse_binary_xsd";
import {
createDynamicObjectConstructor
} from "./dynamic_extension_object";
import {
MapDataTypeAndEncodingIdProvider,
TypeDictionary,
} from "./parse_binary_xsd";
import { ExpandedNodeId } from "node-opcua-nodeid";
function removeNamespacePart(str?: string): string | undefined {
function _removeNamespacePart(str?: string): string | undefined {
if (!str) {

@@ -23,7 +31,7 @@ return str;

function getNamespacePart(str: string): string {
function _getNamespacePart(str: string): string {
return str.split(":")[0];
}
function adjustFieldTypeName(fieldTypeName: string): string {
function _adjustFieldTypeName(fieldTypeName: string): string {
// special cases

@@ -40,114 +48,113 @@ if (fieldTypeName === "String" || fieldTypeName === "CharArray") {

export function getOrCreateStructuredTypeSchema(name: string, typeDictionary: TypeDictionary): StructuredTypeSchema {
let structuredTypeSchema = typeDictionary.structuredTypes[name];
if (structuredTypeSchema) {
return structuredTypeSchema;
}
export function getOrCreateStructuredTypeSchema(
name: string,
typeDictionary: TypeDictionary,
dataTypeFactory: DataTypeFactory,
idProvider: MapDataTypeAndEncodingIdProvider
): StructuredTypeSchema {
// construct it !
const structuredType = typeDictionary.structuredTypesRaw[name];
if (!structuredType) {
throw new Error("Cannot find structuredType" + name);
}
function _getOrCreateStructuredTypeSchema(
_name: string,
): StructuredTypeSchema {
structuredType.baseType = removeNamespacePart(structuredType.baseType);
structuredType.baseType = structuredType.baseType ? structuredType.baseType : "BaseUAObject";
if (dataTypeFactory.hasStructuredType(_name)) {
return dataTypeFactory.getStructuredTypeSchema(_name);
}
for (const field of structuredType.fields) {
const fieldType = field.fieldType;
if (!field.schema) {
// construct it !
const structuredType = typeDictionary.getStructuredTypesRawByName(_name);
if (!structuredType) {
throw new Error("Cannot find structuredType " + _name);
}
const prefix = getNamespacePart(fieldType);
const fieldTypeName = adjustFieldTypeName(removeNamespacePart(fieldType)!);
structuredType.baseType = _removeNamespacePart(structuredType.baseType);
structuredType.baseType = structuredType.baseType ? structuredType.baseType : "ExtensionObject";
switch (prefix) {
case "tns":
for (const field of structuredType.fields) {
const fieldType = field.fieldType;
if (!field.schema) {
field.fieldType = fieldTypeName;
const enumeratedType = typeDictionary.enumeratedTypes[fieldTypeName];
if (enumeratedType) {
field.category = FieldCategory.enumeration;
field.schema = enumeratedType;
const prefix = _getNamespacePart(fieldType);
const fieldTypeName = _adjustFieldTypeName(_removeNamespacePart(fieldType)!);
} else {
// must be a structure then ....
field.category = FieldCategory.complex;
field.schema = getOrCreateStructuredTypeSchema(fieldTypeName, typeDictionary);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("cannot find schema for ", fieldTypeName);
switch (prefix) {
case "tns":
field.fieldType = fieldTypeName;
if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
const enumeratedType = dataTypeFactory.getEnumeration(fieldTypeName);
field.category = FieldCategory.enumeration;
field.schema = enumeratedType;
} else {
// must be a structure then ....
field.category = FieldCategory.complex;
field.schema = _getOrCreateStructuredTypeSchema(fieldTypeName);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("cannot find schema for ", fieldTypeName);
}
}
}
break;
case "ua":
field.fieldType = fieldTypeName;
if (hasBuiltInType(fieldTypeName)) {
field.category = FieldCategory.basic;
field.schema = getBuildInType(fieldTypeName);
} else if (hasStructuredType(fieldTypeName)) {
field.category = FieldCategory.complex;
field.schema = getStructuredTypeSchema(fieldTypeName);
break;
case "ua":
field.fieldType = fieldTypeName;
if (hasBuiltInType(fieldTypeName)) {
field.category = FieldCategory.basic;
field.schema = getBuildInType(fieldTypeName);
} else if (hasStructuredType(fieldTypeName)) {
field.category = FieldCategory.complex;
field.schema = getStructuredTypeSchema(fieldTypeName);
} else {
field.category = FieldCategory.basic;
// try in this
field.schema = getOrCreateStructuredTypeSchema(fieldTypeName, typeDictionary);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("What should I do ??", fieldTypeName, " ", hasStructuredType(fieldTypeName));
} else {
if (hasBuiltInType(fieldTypeName)) {
field.category = FieldCategory.basic;
field.category = FieldCategory.basic;
// try in this
field.schema = _getOrCreateStructuredTypeSchema(fieldTypeName);
if (!field.schema) {
// tslint:disable-next-line:no-console
console.log("What should I do ??", fieldTypeName, " ", hasStructuredType(fieldTypeName));
} else {
field.category = FieldCategory.complex;
if (hasBuiltInType(fieldTypeName)) {
field.category = FieldCategory.basic;
} else {
field.category = FieldCategory.complex;
}
}
}
}
break;
case "opc":
if ((fieldTypeName === "UAString" || fieldTypeName === "String") && field.name === "IndexRange") {
field.fieldType = "NumericRange";
// xx console.log(" NumericRange detected here !");
} else {
field.fieldType = fieldTypeName;
}
if (!hasBuiltInType(fieldTypeName)) {
throw new Error("Unknown basic type " + fieldTypeName);
}
field.category = FieldCategory.basic;
break;
break;
case "opc":
if ((fieldTypeName === "UAString" || fieldTypeName === "String") && field.name === "IndexRange") {
field.fieldType = "NumericRange";
// xx console.log(" NumericRange detected here !");
} else {
field.fieldType = fieldTypeName;
}
if (!hasBuiltInType(fieldTypeName)) {
throw new Error("Unknown basic type " + fieldTypeName);
}
field.category = FieldCategory.basic;
break;
}
}
}
}
structuredTypeSchema = buildStructuredType(structuredType as StructuredTypeOptions);
typeDictionary.structuredTypes[name] = structuredTypeSchema;
return structuredTypeSchema;
const schema = buildStructuredType(structuredType as StructuredTypeOptions);
const ids = idProvider.getDataTypeAndEncodingId(schema.name);
if (!ids) {
throw new Error("Cannot find getDataTypeAndEncodingId for " + schema.name);
}
schema.id = ids.dataTypeNodeId;
schema.dataTypeNodeId = ids.dataTypeNodeId;
if (schema.id.namespace === 0 && schema.id.value === 0) {
return schema;
}
schema.encodingDefaultXml = ExpandedNodeId.fromNodeId(ids.xmlEncodingNodeId);
schema.encodingDefaultJson = ExpandedNodeId.fromNodeId(ids.jsonEncodingNodeId);
schema.encodingDefaultBinary = ExpandedNodeId.fromNodeId(ids.binaryEncodingNodeId);
}
const Constructor = createDynamicObjectConstructor(schema, dataTypeFactory) as ConstructorFuncWithSchema;
Constructor.encodingDefaultBinary = schema.encodingDefaultBinary;
Constructor.encodingDefaultXml = schema.encodingDefaultXml;
export function prepareStructureType(
structuredType: StructureTypeRaw,
typeDictionary: TypeDictionary
): StructuredTypeSchema {
const key = structuredType.name;
if (typeDictionary.structuredTypes[key]) {
return typeDictionary.structuredTypes[key]; // already done
return schema;
}
typeDictionary.structuredTypes[key] = getOrCreateStructuredTypeSchema(key, typeDictionary);
return typeDictionary.structuredTypes[key];
return _getOrCreateStructuredTypeSchema(name);
}
export function prepareEnumeratedType(
enumeratedType: EnumeratedType,
typeDictionary: TypeDictionary
): void {
const key = enumeratedType.name;
const e = new EnumerationDefinitionSchema({
enumValues: enumeratedType.enumeratedValues,
name: key
});
typeDictionary.enumeratedTypes[key] = e;
}

@@ -1,10 +0,16 @@

import { StructuredTypeSchema } from "node-opcua-factory";
import { TypeDictionary } from "./parse_binary_xsd";
import {
ConstructorFuncWithSchema,
DataTypeFactory,
StructuredTypeSchema,
} from "node-opcua-factory";
export function toTypeScript(typeDictionnary: TypeDictionary): string {
export function toTypeScript(dataTypeFactory: DataTypeFactory): string {
const declaration: {[key: string]: string} = {};
const enumeratedTypes = (dataTypeFactory as any)._enumerations;
const structuredTypes = (dataTypeFactory as any)._structureTypeConstructorByNameMap;
const declaration: { [key: string]: string } = {};
function adjustType(t: string): string {
if (!typeDictionnary.enumeratedTypes[t] && !typeDictionnary.structuredTypes[t]) {
if (!enumeratedTypes[t] && !structuredTypes[t]) {
declaration[t] = t;

@@ -16,3 +22,3 @@ }

// enumeration
for (const e of Object.values(typeDictionnary.enumeratedTypes)) {
for (const e of Object.values(enumeratedTypes) as any[]) {
l.push(`export enum ${e.name} {`);

@@ -30,7 +36,9 @@ // console.log((e.typedEnum as any).enumItems);

}
const alreadyDone: {[key: string]: StructuredTypeSchema} = {};
const alreadyDone: { [key: string]: StructuredTypeSchema } = {};
function dumpType(o: StructuredTypeSchema) {
// base type first
const b = o.baseType;
const bt = typeDictionnary.structuredTypes[b];
const bt = structuredTypes[b]?.schema;
if (b && !alreadyDone[o.baseType] && bt) {

@@ -73,3 +81,3 @@ dumpType(bt);

if (o.fields.length === 0 ) {
if (o.fields.length === 0) {
l.push("// tslint:disable-next-line: no-empty-interface");

@@ -95,7 +103,7 @@ }

// objects
for (const o of Object.values(typeDictionnary.structuredTypes)) {
if (alreadyDone[o.name]) {
for (const o of Object.values(structuredTypes) as ConstructorFuncWithSchema[]) {
if (alreadyDone[o.schema.name]) {
continue;
}
dumpType(o);
dumpType(o.schema);
}

@@ -102,0 +110,0 @@ const opcuatypes = Object.keys(declaration).sort().join(",\n ");

{
"extends" : "../tsconfig.json",
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist"
"outDir": "dist",
"composite": true
},
"types": [ "mocha" ],
"files": [ "source/index.ts"]
"types": [
"mocha"
],
"include": [
"source/**/*.ts"
],
"references": [
{
"path": "../node-opcua-assert"
},
{
"path": "../node-opcua-binary-stream"
},
{
"path": "../node-opcua-data-model"
},
{
"path": "../node-opcua-debug"
},
{
"path": "../node-opcua-enum"
},
{
"path": "../node-opcua-extension-object"
},
{
"path": "../node-opcua-factory"
},
{
"path": "../node-opcua-nodeid"
},
{
"path": "../node-opcua-variant"
},
{
"path": "../node-opcua-xml2json"
},
{
"path": "../node-opcua-utils"
},
{
"path": "../node-opcua-packet-analyzer"
}
],
"exclude": [
"node_modules",
"dist"
]
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet