completely rework the parsing structure because I now realize how frames work

This commit is contained in:
2023-09-10 23:40:06 -05:00
parent 7a1366303f
commit 17224d9319
9 changed files with 261 additions and 132 deletions
+57
View File
@@ -0,0 +1,57 @@
const { Parser } = require('binary-parser');
const { ChunkParser, HeaderParser } = require('../parsers/common');
const { InfoSystemNameParser, InfoTrackerListParser } = require('../parsers/info');
class InfoPacket {
constructor(packet) {
this.packet = packet;
this.subChunks = [];
if (this.packet.has_subchunks) {
this.subChunkParser = new Parser().array('chunks', {
type: ChunkParser,
lengthInBytes: this.packet.data_len,
});
this.subChunks = this.subChunkParser.parse(this.packet.chunk_data)?.chunks;
this.subChunks = this.subChunks.map((subChunk) => {
let populatedSubChunk = {};
populatedSubChunk.data_len_valid = subChunk.chunk_data.length === subChunk.data_len;
switch (subChunk.id) {
case 0:
populatedSubChunk = {
...subChunk,
...HeaderParser.parse(subChunk.chunk_data),
};
break;
case 1:
populatedSubChunk = {
...subChunk,
...InfoSystemNameParser.parse(subChunk.chunk_data),
};
break;
case 2:
populatedSubChunk = {
...subChunk,
...InfoTrackerListParser.parse(subChunk.chunk_data),
};
break;
default:
populatedSubChunk = {
...subChunk,
};
break;
}
return populatedSubChunk;
});
}
}
getHeaderPacket() {
return this.subChunks.find((subChunk) => subChunk.id === 0x0000);
}
getSystemPacket() {
return this.subChunks.find((subChunk) => subChunk.id === 0x0001);
}
}
module.exports = InfoPacket;