All files / ua NodeId.js

87.5% Statements 63/72
88.89% Branches 40/45
100% Functions 12/12
86.57% Lines 58/67

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  55x 110x   55x 55x 55x 55x 55x 55x 55x 55x 55x         169x 169x 169x     66x 66x 66x   46x 46x     6x 6x 6x     1x 1x 1x               12x 12x 12x     1x 1x 1x         66x     66x 66x 66x   46x 46x   6x 6x 6x   1x 1x 1x   12x 12x 12x   1x 1x 1x                     2x     2x     132x     55x 120x         55x         55x         55x         55x          
"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 Guid_1 = __importDefault(require("./Guid"));
exports.NodeIdTypeTwoByte = 0;
exports.NodeIdTypeFourByte = 1;
exports.NodeIdTypeNumeric = 2;
exports.NodeIdTypeString = 3;
exports.NodeIdTypeGuid = 4;
exports.NodeIdTypeByteString = 5;
// https://reference.opcfoundation.org/v104/Core/docs/Part6/5.2.2/#5.2.2.9
class NodeId {
    constructor(options) {
        var _a, _b, _c, _d, _e, _f;
        this.Type = (_b = (_a = options) === null || _a === void 0 ? void 0 : _a.Type, (_b !== null && _b !== void 0 ? _b : exports.NodeIdTypeTwoByte));
        this.Identifier = (_d = (_c = options) === null || _c === void 0 ? void 0 : _c.Identifier, (_d !== null && _d !== void 0 ? _d : 0));
        this.Namespace = (_f = (_e = options) === null || _e === void 0 ? void 0 : _e.Namespace, (_f !== null && _f !== void 0 ? _f : 0));
    }
    encode() {
        const bucket = new Bucket_1.default();
        bucket.writeUint8(this.Type);
        switch (this.type()) {
            case exports.NodeIdTypeTwoByte: {
                bucket.writeUint8(this.Identifier);
                break;
            }
            case exports.NodeIdTypeFourByte: {
                bucket.writeUint8(this.Namespace);
                bucket.writeUint16(this.Identifier);
                break;
            }
            case exports.NodeIdTypeNumeric: {
                bucket.writeUint16(this.Namespace);
                bucket.writeUint32(this.Identifier);
                break;
            }
            case exports.NodeIdTypeGuid: {
                bucket.writeUint16(this.Namespace);
                bucket.writeStruct(this.Identifier);
                break;
            }
            case exports.NodeIdTypeByteString: {
                bucket.writeUint16(this.Namespace);
                bucket.writeByteString(this.Identifier);
                break;
            }
            case exports.NodeIdTypeString: {
                bucket.writeUint16(this.Namespace);
                bucket.writeString(this.Identifier);
                break;
            }
            default:
                throw new Error(`invalid node id type ${this.Type}`);
        }
        return bucket.bytes;
    }
    decode(b, position) {
        const bucket = new Bucket_1.default(b, position);
        this.Type = bucket.readUint8();
        switch (this.type()) {
            case exports.NodeIdTypeTwoByte:
                this.Identifier = bucket.readUint8();
                return bucket.position;
            case exports.NodeIdTypeFourByte:
                this.Namespace = bucket.readUint8();
                this.Identifier = bucket.readUint16();
                return bucket.position;
            case exports.NodeIdTypeNumeric:
                this.Namespace = bucket.readUint16();
                this.Identifier = bucket.readUint32();
                return bucket.position;
            case exports.NodeIdTypeByteString:
                this.Namespace = bucket.readUint16();
                this.Identifier = bucket.readByteString();
                return bucket.position;
            case exports.NodeIdTypeString:
                this.Namespace = bucket.readUint16();
                this.Identifier = bucket.readString();
                return bucket.position;
            case exports.NodeIdTypeGuid:
                this.Namespace = bucket.readUint16();
                this.Identifier = new Guid_1.default();
                bucket.readStruct(this.Identifier);
                return bucket.position;
            default:
                throw new Error(`invalid node id type ${this.type()}`);
        }
    }
    setServerIndexFlag() {
        this.Type |= 0x40;
    }
    setNamespaceUriFlag() {
        this.Type |= 0x80;
    }
    type() {
        return this.Type & 0xf;
    }
}
exports.default = NodeId;
exports.NewTwoByteNodeId = (value) => new NodeId({
    Type: exports.NodeIdTypeTwoByte,
    Identifier: value,
    Namespace: 0
});
exports.NewFourByteNodeId = (namespace, value) => new NodeId({
    Type: exports.NodeIdTypeFourByte,
    Identifier: value,
    Namespace: namespace
});
exports.NewNumericNodeID = (namespace, id) => new NodeId({
    Type: exports.NodeIdTypeNumeric,
    Identifier: id,
    Namespace: namespace
});
exports.NewStringNodeId = (namespace, value) => new NodeId({
    Type: exports.NodeIdTypeString,
    Identifier: value,
    Namespace: namespace
});
exports.NewByteStringNodeId = (namespace, id) => new NodeId({
    Type: exports.NodeIdTypeByteString,
    Namespace: namespace,
    Identifier: id
});