All files / ua Variant.js

63.08% Statements 82/130
74.26% Branches 75/101
100% Functions 9/9
63.08% Lines 82/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266  56x 448x   56x 56x 56x 56x 56x 56x 56x 56x 56x 56x   56x   56x       34x 34x 34x 34x 34x     17x 17x 17x 17x 17x           17x 17x 17x     17x 17x     17x     17x   1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     3x 3x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x                                                                                             34x     34x     17x   1x     1x     1x     1x     1x     1x     1x     1x     1x     3x     1x     1x     1x     1x 1x 1x     1x                                                                                                               56x  
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Bucket_1 = __importDefault(require("./Bucket"));
const DiagnosticInfo_1 = __importDefault(require("./DiagnosticInfo"));
const QualifiedName_1 = __importDefault(require("./QualifiedName"));
const LocalizedText_1 = __importDefault(require("./LocalizedText"));
const ExtensionObject_1 = __importDefault(require("./ExtensionObject"));
const NodeId_1 = __importDefault(require("./NodeId"));
const ExpandedNodeId_1 = __importDefault(require("./ExpandedNodeId"));
const Guid_1 = __importDefault(require("./Guid"));
const enums_1 = require("./enums");
// VariantArrayDimensions flags whether the array has more than one dimension
exports.VariantArrayDimensions = 0x40;
// VariantArrayValues flags whether the value is an array.
exports.VariantArrayValues = 0x80;
class Variant {
    constructor(options) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
        this.EncodingMask = (_b = (_a = options) === null || _a === void 0 ? void 0 : _a.EncodingMask, (_b !== null && _b !== void 0 ? _b : 0));
        this.ArrayLength = (_d = (_c = options) === null || _c === void 0 ? void 0 : _c.ArrayLength, (_d !== null && _d !== void 0 ? _d : 0));
        this.Value = (_f = (_e = options) === null || _e === void 0 ? void 0 : _e.Value, (_f !== null && _f !== void 0 ? _f : null));
        this.ArrayDimensionsLength = (_h = (_g = options) === null || _g === void 0 ? void 0 : _g.ArrayDimensionsLength, (_h !== null && _h !== void 0 ? _h : null));
        this.ArrayDimensions = (_k = (_j = options) === null || _j === void 0 ? void 0 : _j.ArrayDimensions, (_k !== null && _k !== void 0 ? _k : null));
    }
    decode(b, position) {
        const bucket = new Bucket_1.default(b, position);
        this.EncodingMask = bucket.readUint8();
        Eif (!this.has(exports.VariantArrayValues)) {
            this.Value = this.decodeValue(bucket);
            return bucket.position;
        }
        this.ArrayLength = bucket.readInt32();
        return bucket.position;
    }
    encode() {
        const bucket = new Bucket_1.default();
        bucket.writeUint8(this.EncodingMask);
        Iif (this.has(exports.VariantArrayValues)) {
            bucket.writeInt32(this.ArrayLength);
        }
        this.encodeRecursive(bucket, this.Value);
        return bucket.bytes;
    }
    encodeRecursive(b, val) {
        this.encodeValue(b, val);
    }
    encodeValue(b, v) {
        switch (this.type()) {
            case enums_1.TypeIdBoolean: {
                b.writeBoolean(v);
                break;
            }
            case enums_1.TypeIdSByte: {
                b.writeInt8(v);
                break;
            }
            case enums_1.TypeIdByte: {
                b.writeUint8(v);
                break;
            }
            case enums_1.TypeIdInt16: {
                b.writeInt16(v);
                break;
            }
            case enums_1.TypeIdUint16: {
                b.writeUint16(v);
                break;
            }
            case enums_1.TypeIdInt32: {
                b.writeInt32(v);
                break;
            }
            case enums_1.TypeIdUint32: {
                b.writeUint32(v);
                break;
            }
            case enums_1.TypeIdInt64: {
                b.writeInt64(v);
                break;
            }
            case enums_1.TypeIdUint64: {
                b.writeUint64(v);
                break;
            }
            case enums_1.TypeIdFloat: {
                b.writeFloat32(v);
                break;
            }
            case enums_1.TypeIdDouble: {
                b.writeFloat64(v);
                break;
            }
            case enums_1.TypeIdString: {
                b.writeString(v);
                break;
            }
            case enums_1.TypeIdDateTime: {
                b.writeDate(v);
                break;
            }
            case enums_1.TypeIdGUID: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdByteString: {
                b.writeByteString(v);
                break;
            }
            //   case 'XMLElement': {
            //     b.writeString(string(v))
            //     break
            //   }
            case enums_1.TypeIdNodeID: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdExpandedNodeID: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdStatusCode: {
                b.writeUint32(v);
                break;
            }
            case enums_1.TypeIdQualifiedName: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdLocalizedText: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdExtensionObject: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdDataValue: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdVariant: {
                b.writeStruct(v);
                break;
            }
            case enums_1.TypeIdDiagnosticInfo: {
                b.writeStruct(v);
                break;
            }
            default:
                throw new Error(`unsupported type: ${this.type()}`);
        }
    }
    has(mask) {
        return (this.EncodingMask & mask) === mask;
    }
    type() {
        return this.EncodingMask & 0x3f;
    }
    decodeValue(b) {
        switch (this.type()) {
            case enums_1.TypeIdBoolean: {
                return b.readBoolean();
            }
            case enums_1.TypeIdSByte: {
                return b.readInt8();
            }
            case enums_1.TypeIdByte: {
                return b.readUint8();
            }
            case enums_1.TypeIdInt16: {
                return b.readInt16();
            }
            case enums_1.TypeIdUint16: {
                return b.readUint16();
            }
            case enums_1.TypeIdInt32: {
                return b.readInt32();
            }
            case enums_1.TypeIdUint32: {
                return b.readUint32();
            }
            case enums_1.TypeIdInt64: {
                return b.readInt64();
            }
            case enums_1.TypeIdUint64: {
                return b.readUint64();
            }
            case enums_1.TypeIdFloat: {
                return b.readFloat32();
            }
            case enums_1.TypeIdDouble: {
                return b.readFloat64();
            }
            case enums_1.TypeIdString: {
                return b.readString();
            }
            case enums_1.TypeIdDateTime: {
                return b.readDate();
            }
            case enums_1.TypeIdGUID: {
                const v = new Guid_1.default();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdByteString: {
                return b.readByteString();
            }
            // case TypeIDXMLElement: {
            // return XMLElement(buf.ReadString())
            // break
            // }
            case enums_1.TypeIdNodeID: {
                const v = new NodeId_1.default();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdExpandedNodeID: {
                const v = new ExpandedNodeId_1.default();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdStatusCode: {
                return b.readUint32();
            }
            case enums_1.TypeIdQualifiedName: {
                const v = new QualifiedName_1.default();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdLocalizedText: {
                const v = new LocalizedText_1.default();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdExtensionObject: {
                const v = new ExtensionObject_1.default();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdDataValue: {
                const v = new Date();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdVariant: {
                // todo(fs): limit recursion depth to 100
                const v = new Variant();
                b.readStruct(v);
                return v;
            }
            case enums_1.TypeIdDiagnosticInfo: {
                // todo(fs): limit recursion depth to 100
                const v = new DiagnosticInfo_1.default();
                b.readStruct(v);
                return v;
            }
            default:
                return null;
        }
    }
}
exports.default = Variant;