@node-dlc/messaging
Version:
DLC Messaging Protocol
171 lines • 6.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NegotiationFieldsV2 = exports.NegotiationFieldsV1 = exports.NegotiationFieldsV0 = exports.NegotiationFields = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
const getTlv_1 = require("../serialize/getTlv");
const RoundingIntervalsV0_1 = require("./RoundingIntervalsV0");
class NegotiationFields {
static deserialize(buf) {
const reader = new bufio_1.BufferReader(buf);
const type = Number(reader.readBigSize());
switch (type) {
case MessageType_1.MessageType.NegotiationFieldsV0:
return NegotiationFieldsV0.deserialize(buf);
case MessageType_1.MessageType.NegotiationFieldsV1:
return NegotiationFieldsV1.deserialize(buf);
case MessageType_1.MessageType.NegotiationFieldsV2:
return NegotiationFieldsV2.deserialize(buf);
default:
throw new Error(`Negotiation fields TLV type must be NegotiationFieldsV0, NegotiationFieldsV1 or NegotiationFieldsV2`);
}
}
}
exports.NegotiationFields = NegotiationFields;
/**
* NegotiationFields V0 contains preferences of the accepter of a DLC
* which are taken into account during DLC construction.
*/
class NegotiationFieldsV0 extends NegotiationFields {
constructor() {
super(...arguments);
/**
* The type for negotiation_fields_v0 message. negotiation_fields_v0 = 55334
*/
this.type = NegotiationFieldsV0.type;
}
/**
* Deserializes an negotiation_fields_v0 message
* @param buf
*/
static deserialize(buf) {
const instance = new NegotiationFieldsV0();
const reader = new bufio_1.BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
return instance;
}
/**
* Converts negotiation_fields_v0 to JSON
*/
toJSON() {
return {
type: this.type,
};
}
/**
* Serializes the negotiation_fields_v0 message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeBigSize(this.type);
writer.writeBigSize(0);
return writer.toBuffer();
}
}
exports.NegotiationFieldsV0 = NegotiationFieldsV0;
NegotiationFieldsV0.type = MessageType_1.MessageType.NegotiationFieldsV0;
/**
* NegotiationFields V1 contains preferences of the acceptor of a DLC
* which are taken into account during DLC construction.
*/
class NegotiationFieldsV1 extends NegotiationFields {
constructor() {
super(...arguments);
/**
* The type for negotiation_fields_v1 message. negotiation_fields_v1 = 55336
*/
this.type = NegotiationFieldsV1.type;
}
/**
* Deserializes an negotiation_fields_v1 message
* @param buf
*/
static deserialize(buf) {
const instance = new NegotiationFieldsV1();
const reader = new bufio_1.BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
instance.roundingIntervals = RoundingIntervalsV0_1.RoundingIntervalsV0.deserialize((0, getTlv_1.getTlv)(reader));
return instance;
}
/**
* Converts negotiation_fields_v1 to JSON
*/
toJSON() {
return {
type: this.type,
roundingIntervals: this.roundingIntervals.toJSON(),
};
}
/**
* Serializes the negotiation_fields_v1 message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new bufio_1.BufferWriter();
dataWriter.writeBytes(this.roundingIntervals.serialize());
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
exports.NegotiationFieldsV1 = NegotiationFieldsV1;
NegotiationFieldsV1.type = MessageType_1.MessageType.NegotiationFieldsV1;
/**
* NegotiationFields V2 contains preferences of the acceptor of a DLC
* which are taken into account during DLC construction.
*/
class NegotiationFieldsV2 extends NegotiationFields {
constructor() {
super(...arguments);
/**
* The type for negotiation_fields_v2 message. negotiation_fields_v2 = 55346
*/
this.type = NegotiationFieldsV2.type;
this.negotiationFieldsList = [];
}
/**
* Deserializes an negotiation_fields_v1 message
* @param buf
*/
static deserialize(buf) {
const instance = new NegotiationFieldsV2();
const reader = new bufio_1.BufferReader(buf);
reader.readBigSize(); // read type
instance.length = reader.readBigSize();
reader.readBigSize(); // num_disjoint_events
while (!reader.eof) {
instance.negotiationFieldsList.push(NegotiationFields.deserialize((0, getTlv_1.getTlv)(reader)));
}
return instance;
}
/**
* Converts negotiation_fields_v2 to JSON
*/
toJSON() {
return {
type: this.type,
negotiationFieldsList: this.negotiationFieldsList.map((field) => field.toJSON()),
};
}
/**
* Serializes the negotiation_fields_v2 message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeBigSize(this.type);
const dataWriter = new bufio_1.BufferWriter();
dataWriter.writeBigSize(this.negotiationFieldsList.length);
for (const negotiationFields of this.negotiationFieldsList) {
dataWriter.writeBytes(negotiationFields.serialize());
}
writer.writeBigSize(dataWriter.size);
writer.writeBytes(dataWriter.toBuffer());
return writer.toBuffer();
}
}
exports.NegotiationFieldsV2 = NegotiationFieldsV2;
NegotiationFieldsV2.type = MessageType_1.MessageType.NegotiationFieldsV2;
//# sourceMappingURL=NegotiationFields.js.map