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
+4 -44
View File
@@ -1,24 +1,5 @@
const { PSN_PACKET_HEADER_PARSER } = require('../common');
const Parser = require('binary-parser').Parser;
const PSN_INFO_SYSTEM_NAME_PARSER = new Parser()
.uint16le('id')
.uint16le('data_len', {
formatter: (item) => {
const binary = item.toString(2).padStart(16, '0');
return Number.parseInt(binary.substring(1), 2);
},
})
.seek(-2)
.uint16le('has_subchunks', {
formatter: (item) => {
const binary = item.toString(2).padStart(16, '0');
return binary.charAt(0) === '1';
},
})
.string('system_name', { length: 'data_len' });
const PSN_INFO_TRACKER_NAME_PARSER = new Parser()
.uint16le('id')
.uint16le('data_len', {
@@ -55,32 +36,11 @@ const PSN_INFO_TRACKER_PARSER = new Parser()
type: PSN_INFO_TRACKER_NAME_PARSER,
});
const PSN_INFO_TRACKER_LIST_PARSER = new Parser()
.uint16le('id')
.uint16le('data_len', {
formatter: (item) => {
const binary = item.toString(2).padStart(16, '0');
return Number.parseInt(binary.substring(1), 2);
},
})
.seek(-2)
.uint16le('has_subchunks', {
formatter: (item) => {
const binary = item.toString(2).padStart(16, '0');
return binary.charAt(0) === '1';
},
})
.array('trackers', { type: PSN_INFO_TRACKER_PARSER, lengthInBytes: 'data_len' });
const InfoSystemNameParser = new Parser().string('system_name', { greedy: true });
const PSN_INFO_CHUNK_DATA_PARSER = new Parser()
.nest('packet_header', { type: PSN_PACKET_HEADER_PARSER })
.nest('system_name', { type: PSN_INFO_SYSTEM_NAME_PARSER })
.nest('tracker_list', { type: PSN_INFO_TRACKER_LIST_PARSER });
const InfoTrackerListParser = new Parser().array('trackers', { type: PSN_INFO_TRACKER_PARSER, readUntil: 'eof' });
module.exports = {
PSN_INFO_SYSTEM_NAME_PARSER,
PSN_INFO_TRACKER_NAME_PARSER,
PSN_INFO_TRACKER_PARSER,
PSN_INFO_TRACKER_LIST_PARSER,
PSN_INFO_CHUNK_DATA_PARSER,
InfoSystemNameParser,
InfoTrackerListParser,
};