mirror of
https://github.com/jwetzell/psn-js.git
synced 2026-09-08 07:49:17 +00:00
remove binary-parsser lib dependency
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
const SystemNameChunk = require('./info-system-name-chunk');
|
||||
const TrackerListChunk = require('./info-tracker-list-chunk');
|
||||
|
||||
module.exports = {
|
||||
SystemNameChunk,
|
||||
TrackerListChunk,
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const packetHeaderChunk = require('../packet-header-chunk');
|
||||
const infoSystemNameChunk = require('./info-system-name-chunk');
|
||||
const infoTrackerListChunk = require('./info-tracker-list-chunk');
|
||||
|
||||
function decodeInfoPacketChunk(infoPacketChunk) {
|
||||
if (infoPacketChunk.has_subchunks && infoPacketChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < infoPacketChunk.data_len) {
|
||||
const chunkId = infoPacketChunk.chunk_data.readUInt16LE(offset);
|
||||
switch (chunkId) {
|
||||
case 0x0000:
|
||||
infoPacketChunk.packet_header = packetHeaderChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoPacketChunk.packet_header.data_len;
|
||||
break;
|
||||
case 0x0001:
|
||||
infoPacketChunk.system_name = infoSystemNameChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoPacketChunk.system_name.data_len;
|
||||
break;
|
||||
case 0x0002:
|
||||
infoPacketChunk.tracker_list = infoTrackerListChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoPacketChunk.tracker_list.data_len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeInfoPacketChunk);
|
||||
@@ -1,3 +1,9 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
const chunk = require('../chunk');
|
||||
|
||||
module.exports = new Parser().string('system_name', { greedy: true });
|
||||
function decodeSystemNameChunk(systemNameChunk) {
|
||||
if (systemNameChunk.chunk_data !== undefined && systemNameChunk.data_len !== undefined) {
|
||||
systemNameChunk.system_name = systemNameChunk.chunk_data.subarray(0, systemNameChunk.data_len).toString();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeSystemNameChunk);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const infoTrackerNameChunk = require('./info-tracker-name-chunk');
|
||||
|
||||
function decodeInfoTrackerChunk(infoTrackerChunk) {
|
||||
if (infoTrackerChunk.has_subchunks && infoTrackerChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < infoTrackerChunk.data_len) {
|
||||
const chunkId = infoTrackerChunk.chunk_data.readUInt16LE(offset);
|
||||
switch (chunkId) {
|
||||
case 0x0000:
|
||||
infoTrackerChunk.tracker_name = infoTrackerNameChunk(infoTrackerChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoTrackerChunk.tracker_name.data_len;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeInfoTrackerChunk);
|
||||
@@ -1,39 +1,17 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const infoTrackerChunk = require('./info-tracker-chunk');
|
||||
|
||||
const infoTrackerNameParser = 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('tracker_name', { length: 'data_len' });
|
||||
|
||||
const infoTrackerParser = 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';
|
||||
},
|
||||
})
|
||||
.nest('tracker_name', {
|
||||
type: infoTrackerNameParser,
|
||||
});
|
||||
|
||||
module.exports = new Parser().array('trackers', { type: infoTrackerParser, readUntil: 'eof' });
|
||||
function decodeInfoTrackerListChunk(infoTrackerListChunk) {
|
||||
infoTrackerListChunk.trackers = {};
|
||||
if (infoTrackerListChunk.has_subchunks && infoTrackerListChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < infoTrackerListChunk.data_len) {
|
||||
const trackerChunk = infoTrackerChunk(infoTrackerListChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += trackerChunk.data_len;
|
||||
infoTrackerListChunk.trackers[trackerChunk.id] = trackerChunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports = (buffer) => chunk(buffer, decodeInfoTrackerListChunk);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
const chunk = require('../chunk');
|
||||
|
||||
function decodeTrackerNameChunk(trackerNameChunk) {
|
||||
if (trackerNameChunk.chunk_data !== undefined && trackerNameChunk.data_len !== undefined) {
|
||||
trackerNameChunk.tracker_name = trackerNameChunk.chunk_data.subarray(0, trackerNameChunk.data_len).toString();
|
||||
}
|
||||
}
|
||||
module.exports = (buffer) => chunk(buffer, decodeTrackerNameChunk);
|
||||
Reference in New Issue
Block a user