node-opcua-extension-object
Advanced tools
Comparing version 2.0.0-alpha.13 to 2.0.0-alpha.18
@@ -0,7 +1,15 @@ | ||
/// <reference types="node" /> | ||
import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream"; | ||
import { BaseUAObject } from "node-opcua-factory"; | ||
import { NodeId } from "node-opcua-nodeid"; | ||
export declare class ExtensionObject extends BaseUAObject { | ||
constructor(otions: any); | ||
} | ||
export declare function encodeExtensionObject(object: any, stream: OutputBinaryStream): void; | ||
export declare function encodeExtensionObject(object: ExtensionObject | null, stream: OutputBinaryStream): void; | ||
export declare class OpaqueStructure extends ExtensionObject { | ||
nodeId: NodeId; | ||
buffer: Buffer; | ||
constructor(nodeId: NodeId, buffer: Buffer); | ||
toString(): string; | ||
} | ||
export declare function decodeExtensionObject(stream: BinaryStream): ExtensionObject | null; |
@@ -54,3 +54,12 @@ "use strict"; | ||
else { | ||
/* istanbul ignore next */ | ||
if (!(object instanceof ExtensionObject)) { | ||
throw new Error("Expecting a extension object"); | ||
} | ||
// ensure we have a valid encoding Default Binary ID !!! | ||
/* istanbul ignore next */ | ||
if (!object.schema) { | ||
debugLog(" object = ", object); | ||
throw new Error("object has no schema " + object.constructor.name); | ||
} | ||
const encodingDefaultBinary = object.schema.encodingDefaultBinary; | ||
@@ -64,3 +73,3 @@ /* istanbul ignore next */ | ||
if (encodingDefaultBinary.isEmpty()) { | ||
debugLog(chalk_1.default.yellow("xxxxxxxxx encoding ExtObj "), object.encodingDefaultBinary.toString()); | ||
debugLog(chalk_1.default.yellow("xxxxxxxxx encoding ExtObj "), object.constructor.encodingDefaultBinary.toString()); | ||
throw new Error("Cannot find encodingDefaultBinary for this object"); | ||
@@ -70,3 +79,3 @@ } | ||
if (node_opcua_factory_1.is_internal_id(encodingDefaultBinary.value)) { | ||
debugLog(chalk_1.default.yellow("xxxxxxxxx encoding ExtObj "), object.encodingDefaultBinary.toString(), object.schema.name); | ||
debugLog(chalk_1.default.yellow("xxxxxxxxx encoding ExtObj "), object.constructor.encodingDefaultBinary.toString(), object.schema.name); | ||
throw new Error("Cannot find valid OPCUA encodingDefaultBinary for this object"); | ||
@@ -81,2 +90,18 @@ } | ||
exports.encodeExtensionObject = encodeExtensionObject; | ||
// tslint:disable:max-classes-per-file | ||
class OpaqueStructure extends ExtensionObject { | ||
constructor(nodeId, buffer) { | ||
super({}); | ||
this.nodeId = nodeId; | ||
this.buffer = buffer; | ||
} | ||
toString() { | ||
const str = "/* OpaqueStructure */ { \n" + | ||
"nodeId " + this.nodeId.toString() + "\n" + | ||
"buffer = \n" + node_opcua_debug_1.hexDump(this.buffer) + "\n" + | ||
"}"; | ||
return str; | ||
} | ||
} | ||
exports.OpaqueStructure = OpaqueStructure; | ||
function decodeExtensionObject(stream) { | ||
@@ -93,16 +118,31 @@ const nodeId = node_opcua_basic_types_1.decodeNodeId(stream); | ||
} | ||
const object = constructEmptyExtensionObject(nodeId); | ||
/* istanbul ignore next */ | ||
if (object === null) { | ||
// this object is unknown to us .. | ||
stream.length += length; | ||
return {}; | ||
} | ||
// let verify that decode will use the expected number of bytes | ||
const streamLengthBefore = stream.length; | ||
try { | ||
object.decode(stream); | ||
let object; | ||
if (nodeId.namespace !== 0) { | ||
// this is a extension object define in a other namespace | ||
// we can only threat it as an opaque object for the time being | ||
// the caller that may now more about the namespace Array and type | ||
// definition will be able to turn the opaque object into a meaningful | ||
// structure | ||
// lets rewind before the length | ||
stream.length -= 4; | ||
object = new OpaqueStructure(nodeId, stream.readByteStream()); | ||
} | ||
catch (err) { | ||
debugLog("Cannot decode object ", err.message); | ||
else { | ||
object = constructEmptyExtensionObject(nodeId); | ||
/* istanbul ignore next */ | ||
if (object === null) { | ||
// this object is unknown to us .. | ||
stream.length += length; | ||
object = {}; | ||
} | ||
else { | ||
try { | ||
object.decode(stream); | ||
} | ||
catch (err) { | ||
debugLog("Cannot decode object ", err.message); | ||
} | ||
} | ||
} | ||
@@ -115,3 +155,3 @@ if (streamLengthBefore + length !== stream.length) { | ||
// tslint:disable-next-line:no-console | ||
console.warn("WARNING => Extension object decoding error on ", object.constructor.name, " expected size was", length, "actual size was ", stream.length - streamLengthBefore); | ||
console.warn("WARNING => Extension object decoding error on ", object.constructor.name, " expected size was", length, "but only this amount of bytes have been read :", stream.length - streamLengthBefore); | ||
stream.length = streamLengthBefore + length; | ||
@@ -118,0 +158,0 @@ } |
{ | ||
"name": "node-opcua-extension-object", | ||
"version": "2.0.0-alpha.13", | ||
"version": "2.0.0-alpha.18", | ||
"description": "pure nodejs OPCUA SDK - module -extension-object", | ||
@@ -14,7 +14,7 @@ "main": "./dist/index.js", | ||
"node-opcua-assert": "^2.0.0-alpha.10", | ||
"node-opcua-basic-types": "^2.0.0-alpha.13", | ||
"node-opcua-binary-stream": "^2.0.0-alpha.13", | ||
"node-opcua-debug": "^2.0.0-alpha.10", | ||
"node-opcua-factory": "^2.0.0-alpha.13", | ||
"node-opcua-nodeid": "^2.0.0-alpha.13" | ||
"node-opcua-basic-types": "^2.0.0-alpha.18", | ||
"node-opcua-binary-stream": "^2.0.0-alpha.18", | ||
"node-opcua-debug": "^2.0.0-alpha.18", | ||
"node-opcua-factory": "^2.0.0-alpha.18", | ||
"node-opcua-nodeid": "^2.0.0-alpha.18" | ||
}, | ||
@@ -36,3 +36,3 @@ "author": "Etienne Rossignon", | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "6a0c56afb819a44d5abd453d39ce684df6fb3505" | ||
"gitHead": "a92d7f1be990cfaadd5f52560e2454e252b0b081" | ||
} |
@@ -6,3 +6,3 @@ /** | ||
import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream"; | ||
import { checkDebugFlag, make_debugLog } from "node-opcua-debug"; | ||
import { checkDebugFlag, hexDump, make_debugLog } from "node-opcua-debug"; | ||
import { BaseUAObject, constructObject, is_internal_id, registerBuiltInType } from "node-opcua-factory"; | ||
@@ -55,3 +55,3 @@ import { ExpandedNodeId, makeNodeId, NodeId } from "node-opcua-nodeid"; | ||
export function encodeExtensionObject(object: any, stream: OutputBinaryStream): void { | ||
export function encodeExtensionObject(object: ExtensionObject | null, stream: OutputBinaryStream): void { | ||
@@ -63,4 +63,12 @@ if (!object) { | ||
} else { | ||
/* istanbul ignore next */ | ||
if (!((object as any) instanceof ExtensionObject)) { | ||
throw new Error("Expecting a extension object"); | ||
} | ||
// ensure we have a valid encoding Default Binary ID !!! | ||
/* istanbul ignore next */ | ||
if (!object.schema) { | ||
debugLog(" object = ", object); | ||
throw new Error("object has no schema " + object.constructor.name); | ||
} | ||
const encodingDefaultBinary = object.schema.encodingDefaultBinary; | ||
@@ -74,3 +82,3 @@ /* istanbul ignore next */ | ||
if (encodingDefaultBinary.isEmpty()) { | ||
debugLog(chalk.yellow("xxxxxxxxx encoding ExtObj "), object.encodingDefaultBinary.toString()); | ||
debugLog(chalk.yellow("xxxxxxxxx encoding ExtObj "), (object.constructor as any).encodingDefaultBinary.toString()); | ||
throw new Error("Cannot find encodingDefaultBinary for this object"); | ||
@@ -81,3 +89,3 @@ } | ||
debugLog(chalk.yellow("xxxxxxxxx encoding ExtObj "), | ||
object.encodingDefaultBinary.toString(), object.schema.name); | ||
(object.constructor as any).encodingDefaultBinary.toString(), object.schema.name); | ||
throw new Error("Cannot find valid OPCUA encodingDefaultBinary for this object"); | ||
@@ -93,2 +101,22 @@ } | ||
// tslint:disable:max-classes-per-file | ||
export class OpaqueStructure extends ExtensionObject { | ||
public nodeId: NodeId; | ||
public buffer: Buffer; | ||
constructor(nodeId: NodeId, buffer: Buffer) { | ||
super({}); | ||
this.nodeId = nodeId; | ||
this.buffer = buffer; | ||
} | ||
public toString(): string { | ||
const str = | ||
"/* OpaqueStructure */ { \n" + | ||
"nodeId " + this.nodeId.toString() + "\n" + | ||
"buffer = \n" + hexDump(this.buffer) + "\n" + | ||
"}"; | ||
return str; | ||
} | ||
} | ||
export function decodeExtensionObject(stream: BinaryStream): ExtensionObject | null { | ||
@@ -110,17 +138,30 @@ | ||
const object = constructEmptyExtensionObject(nodeId); | ||
/* istanbul ignore next */ | ||
if (object === null) { | ||
// this object is unknown to us .. | ||
stream.length += length; | ||
return {} as ExtensionObject; | ||
} | ||
// let verify that decode will use the expected number of bytes | ||
const streamLengthBefore = stream.length; | ||
try { | ||
object.decode(stream); | ||
} catch (err) { | ||
debugLog("Cannot decode object ", err.message); | ||
let object: any; | ||
if (nodeId.namespace !== 0) { | ||
// this is a extension object define in a other namespace | ||
// we can only threat it as an opaque object for the time being | ||
// the caller that may now more about the namespace Array and type | ||
// definition will be able to turn the opaque object into a meaningful | ||
// structure | ||
// lets rewind before the length | ||
stream.length -= 4; | ||
object = new OpaqueStructure(nodeId, stream.readByteStream()!); | ||
} else { | ||
object = constructEmptyExtensionObject(nodeId); | ||
/* istanbul ignore next */ | ||
if (object === null) { | ||
// this object is unknown to us .. | ||
stream.length += length; | ||
object = {} as ExtensionObject; | ||
} else { | ||
try { | ||
object.decode(stream); | ||
} catch (err) { | ||
debugLog("Cannot decode object ", err.message); | ||
} | ||
} | ||
} | ||
@@ -137,3 +178,3 @@ | ||
object.constructor.name, " expected size was", length, | ||
"actual size was ", stream.length - streamLengthBefore); | ||
"but only this amount of bytes have been read :", stream.length - streamLengthBefore); | ||
stream.length = streamLengthBefore + length; | ||
@@ -140,0 +181,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22309
362