@node-dlc/messaging
Version:
DLC Messaging Protocol
58 lines • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DlcIdsV0 = exports.DlcIds = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
/**
* DlcIds message contains list of buffers
*/
class DlcIds {
constructor() {
/**
* The type for dlc_ids message
*/
this.type = DlcIds.type;
this.ids = [];
}
static deserialize(buf) {
const reader = new bufio_1.BufferReader(buf);
const type = Number(reader.readUInt16BE());
switch (type) {
case MessageType_1.MessageType.DlcIdsV0:
return DlcIds.deserializeV0(buf);
default:
throw new Error(`DLC IDs message type must be DlcIdsV0`);
}
}
/**
* Deserializes an dlc_ids message
* @param buf
*/
static deserializeV0(buf) {
const instance = new DlcIds();
const reader = new bufio_1.BufferReader(buf);
reader.readUInt16BE(); // read type
const idsLen = reader.readBigSize(); // ids length
for (let i = 0; i < idsLen; i++) {
instance.ids.push(reader.readBytes(32));
}
return instance;
}
/**
* Serializes the dlc_ids message into a Buffer
*/
serialize() {
const writer = new bufio_1.BufferWriter();
writer.writeUInt16BE(this.type);
writer.writeBigSize(this.ids.length);
for (const id of this.ids) {
writer.writeBytes(id);
}
return writer.toBuffer();
}
}
exports.DlcIds = DlcIds;
DlcIds.type = MessageType_1.MessageType.DlcIds;
// Legacy support - keeping old class name as alias
exports.DlcIdsV0 = DlcIds;
//# sourceMappingURL=DlcIds.js.map