mirror of
https://github.com/jwetzell/psn-js.git
synced 2026-08-19 14:14:22 +00:00
convert to typescript
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
module.exports = (buffer, dataDecoder) => {
|
||||
export interface Chunk {
|
||||
id?: number;
|
||||
has_subchunks?: boolean;
|
||||
data_len?: number;
|
||||
chunk_data?: Buffer;
|
||||
}
|
||||
|
||||
export default (buffer: Buffer, dataDecoder: Function): Chunk => {
|
||||
let offset = 0;
|
||||
const chunk = {};
|
||||
const chunk: Chunk = {};
|
||||
chunk.id = buffer.readUInt16LE(offset);
|
||||
offset += 2;
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const packetHeaderChunk = require('../packet-header-chunk');
|
||||
const dataTrackerListChunk = require('./data-tracker-list-chunk');
|
||||
|
||||
function decodeDataPacketChunk(dataPacketChunk) {
|
||||
if (dataPacketChunk.has_subchunks && dataPacketChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < dataPacketChunk.data_len) {
|
||||
const chunkId = dataPacketChunk.chunk_data.readUInt16LE(offset);
|
||||
switch (chunkId) {
|
||||
case 0x0000:
|
||||
dataPacketChunk.packet_header = packetHeaderChunk(dataPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += dataPacketChunk.packet_header.data_len;
|
||||
break;
|
||||
case 0x0001:
|
||||
dataPacketChunk.tracker_list = dataTrackerListChunk(dataPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += dataPacketChunk.tracker_list.data_len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeDataPacketChunk);
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Constants } from '../../constants';
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
import packetHeaderChunk, { PacketHeaderChunk } from '../packet-header-chunk';
|
||||
import dataTrackerListChunk, { DataTrackerListChunk } from './data-tracker-list-chunk';
|
||||
|
||||
export interface DataPacketChunk extends Chunk {
|
||||
packet_header: PacketHeaderChunk;
|
||||
tracker_list: DataTrackerListChunk;
|
||||
}
|
||||
|
||||
function decodeDataPacketChunk(dataPacketChunk: DataPacketChunk) {
|
||||
if (dataPacketChunk.has_subchunks && dataPacketChunk.chunk_data && dataPacketChunk.data_len) {
|
||||
let offset = 0;
|
||||
while (offset < dataPacketChunk.data_len) {
|
||||
const chunkId = dataPacketChunk.chunk_data.readUInt16LE(offset);
|
||||
switch (chunkId) {
|
||||
case 0x0000:
|
||||
dataPacketChunk.packet_header = packetHeaderChunk(dataPacketChunk.chunk_data.subarray(offset));
|
||||
offset += Constants.CHUNK_HEADER_SIZE;
|
||||
if (dataPacketChunk.packet_header?.data_len) {
|
||||
offset += dataPacketChunk.packet_header.data_len;
|
||||
}
|
||||
break;
|
||||
case 0x0001:
|
||||
dataPacketChunk.tracker_list = dataTrackerListChunk(dataPacketChunk.chunk_data.subarray(offset));
|
||||
offset += Constants.CHUNK_HEADER_SIZE;
|
||||
if (dataPacketChunk.tracker_list.data_len) {
|
||||
offset += dataPacketChunk.tracker_list.data_len;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default (buffer: Buffer): DataPacketChunk => chunk(buffer, decodeDataPacketChunk) as DataPacketChunk;
|
||||
@@ -1,41 +0,0 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const dataTrackerFieldChunk = require('./data-tracker-field-chunk');
|
||||
|
||||
function decodeTrackerChunk(trackerChunk) {
|
||||
if (trackerChunk.chunk_data && trackerChunk.data_len > 0) {
|
||||
let offset = 0;
|
||||
while (offset < trackerChunk.data_len) {
|
||||
const trackerFieldChunk = dataTrackerFieldChunk(trackerChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += trackerFieldChunk.data_len;
|
||||
switch (trackerFieldChunk.id) {
|
||||
case 0x0000:
|
||||
trackerChunk.pos = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0001:
|
||||
trackerChunk.speed = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0002:
|
||||
trackerChunk.ori = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0003:
|
||||
trackerChunk.status = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0004:
|
||||
trackerChunk.accel = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0005:
|
||||
trackerChunk.trgtpos = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0006:
|
||||
trackerChunk.timestamp = trackerFieldChunk;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeTrackerChunk);
|
||||
@@ -0,0 +1,53 @@
|
||||
import { Constants } from '../../constants';
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
import dataTrackerFieldChunk, { DataTrackerFieldChunk } from './data-tracker-field-chunk';
|
||||
|
||||
export interface DataTrackerChunk extends Chunk {
|
||||
pos: DataTrackerFieldChunk;
|
||||
speed: DataTrackerFieldChunk;
|
||||
ori: DataTrackerFieldChunk;
|
||||
status: DataTrackerFieldChunk;
|
||||
accel: DataTrackerFieldChunk;
|
||||
trgtpos: DataTrackerFieldChunk;
|
||||
timestamp: DataTrackerFieldChunk;
|
||||
}
|
||||
|
||||
function decodeTrackerChunk(trackerChunk: DataTrackerChunk) {
|
||||
if (trackerChunk.chunk_data && trackerChunk.data_len) {
|
||||
let offset = 0;
|
||||
while (offset < trackerChunk.data_len) {
|
||||
const trackerFieldChunk = dataTrackerFieldChunk(trackerChunk.chunk_data.subarray(offset));
|
||||
offset += Constants.CHUNK_HEADER_SIZE;
|
||||
if (trackerFieldChunk.data_len) {
|
||||
offset += trackerFieldChunk.data_len;
|
||||
switch (trackerFieldChunk.id) {
|
||||
case 0x0000:
|
||||
trackerChunk.pos = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0001:
|
||||
trackerChunk.speed = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0002:
|
||||
trackerChunk.ori = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0003:
|
||||
trackerChunk.status = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0004:
|
||||
trackerChunk.accel = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0005:
|
||||
trackerChunk.trgtpos = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0006:
|
||||
trackerChunk.timestamp = trackerFieldChunk;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default (buffer: Buffer): DataTrackerChunk => chunk(buffer, decodeTrackerChunk) as DataTrackerChunk;
|
||||
@@ -1,43 +0,0 @@
|
||||
/* eslint-disable no-case-declarations */
|
||||
const chunk = require('../chunk');
|
||||
|
||||
function readXYZ(trackerFieldChunk, prefix) {
|
||||
if (prefix === undefined) {
|
||||
prefix = '';
|
||||
}
|
||||
|
||||
trackerFieldChunk[`${prefix}x`] = trackerFieldChunk.chunk_data.readFloatLE(0);
|
||||
trackerFieldChunk[`${prefix}y`] = trackerFieldChunk.chunk_data.readFloatLE(4);
|
||||
trackerFieldChunk[`${prefix}z`] = trackerFieldChunk.chunk_data.readFloatLE(8);
|
||||
}
|
||||
function decodeTrackerFieldChunk(trackerFieldChunk) {
|
||||
if (trackerFieldChunk.id !== undefined) {
|
||||
switch (trackerFieldChunk.id) {
|
||||
case 0x0000:
|
||||
readXYZ(trackerFieldChunk, 'pos_');
|
||||
break;
|
||||
case 0x0001:
|
||||
readXYZ(trackerFieldChunk, 'speed_');
|
||||
break;
|
||||
case 0x0002:
|
||||
readXYZ(trackerFieldChunk, 'ori_');
|
||||
break;
|
||||
case 0x0003:
|
||||
trackerFieldChunk.validity = trackerFieldChunk.chunk_data.readFloatLE();
|
||||
break;
|
||||
case 0x0004:
|
||||
readXYZ(trackerFieldChunk, 'accel_');
|
||||
break;
|
||||
case 0x0005:
|
||||
readXYZ(trackerFieldChunk, 'trgtpos_');
|
||||
break;
|
||||
case 0x0006:
|
||||
trackerFieldChunk.tracker_timestamp = trackerFieldChunk.chunk_data.readBigUInt64LE();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeTrackerFieldChunk);
|
||||
@@ -0,0 +1,50 @@
|
||||
/* eslint-disable no-case-declarations */
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
|
||||
export interface DataTrackerFieldChunk extends Chunk {
|
||||
[key: string]: number | bigint | number[] | Buffer | undefined | boolean;
|
||||
}
|
||||
|
||||
function readXYZ(trackerFieldChunk: DataTrackerFieldChunk, prefix: string = '') {
|
||||
if (trackerFieldChunk.chunk_data) {
|
||||
trackerFieldChunk[`${prefix}x`] = trackerFieldChunk.chunk_data.readFloatLE(0);
|
||||
trackerFieldChunk[`${prefix}y`] = trackerFieldChunk.chunk_data.readFloatLE(4);
|
||||
trackerFieldChunk[`${prefix}z`] = trackerFieldChunk.chunk_data.readFloatLE(8);
|
||||
}
|
||||
}
|
||||
function decodeTrackerFieldChunk(trackerFieldChunk: DataTrackerFieldChunk) {
|
||||
if (trackerFieldChunk.id !== undefined) {
|
||||
switch (trackerFieldChunk.id) {
|
||||
case 0x0000:
|
||||
readXYZ(trackerFieldChunk, 'pos_');
|
||||
break;
|
||||
case 0x0001:
|
||||
readXYZ(trackerFieldChunk, 'speed_');
|
||||
break;
|
||||
case 0x0002:
|
||||
readXYZ(trackerFieldChunk, 'ori_');
|
||||
break;
|
||||
case 0x0003:
|
||||
if (trackerFieldChunk.chunk_data) {
|
||||
trackerFieldChunk.validity = trackerFieldChunk.chunk_data.readFloatLE();
|
||||
}
|
||||
break;
|
||||
case 0x0004:
|
||||
readXYZ(trackerFieldChunk, 'accel_');
|
||||
break;
|
||||
case 0x0005:
|
||||
readXYZ(trackerFieldChunk, 'trgtpos_');
|
||||
break;
|
||||
case 0x0006:
|
||||
if (trackerFieldChunk.chunk_data) {
|
||||
trackerFieldChunk.tracker_timestamp = trackerFieldChunk.chunk_data.readBigUInt64LE();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default (buffer: Buffer): DataTrackerFieldChunk =>
|
||||
chunk(buffer, decodeTrackerFieldChunk) as DataTrackerFieldChunk;
|
||||
@@ -1,17 +0,0 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const dataTrackerChunk = require('./data-tracker-chunk');
|
||||
|
||||
function decodeDataTrackerListChunk(dataTrackerListChunk) {
|
||||
dataTrackerListChunk.trackers = {};
|
||||
|
||||
// TODO(jwetzell): add error handling
|
||||
let offset = 0;
|
||||
while (offset < dataTrackerListChunk.chunk_data.length) {
|
||||
const trackerChunk = dataTrackerChunk(dataTrackerListChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += trackerChunk.data_len;
|
||||
dataTrackerListChunk.trackers[trackerChunk.id] = trackerChunk;
|
||||
}
|
||||
}
|
||||
module.exports = (buffer) => chunk(buffer, decodeDataTrackerListChunk);
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Constants } from '../../constants';
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
import dataTrackerChunk, { DataTrackerChunk } from './data-tracker-chunk';
|
||||
|
||||
export interface DataTrackerListChunk extends Chunk {
|
||||
trackers: {
|
||||
[key: number]: DataTrackerChunk;
|
||||
};
|
||||
}
|
||||
|
||||
function decodeDataTrackerListChunk(dataTrackerListChunk: DataTrackerListChunk) {
|
||||
dataTrackerListChunk.trackers = {};
|
||||
|
||||
// TODO(jwetzell): add error handling
|
||||
let offset = 0;
|
||||
if (dataTrackerListChunk.chunk_data) {
|
||||
while (offset < dataTrackerListChunk.chunk_data.length) {
|
||||
const trackerChunk = dataTrackerChunk(dataTrackerListChunk.chunk_data.subarray(offset));
|
||||
offset += Constants.CHUNK_HEADER_SIZE;
|
||||
if (trackerChunk.data_len) {
|
||||
offset += trackerChunk.data_len;
|
||||
}
|
||||
if (trackerChunk.id) {
|
||||
dataTrackerListChunk.trackers[trackerChunk.id] = trackerChunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
export default (buffer: Buffer): DataTrackerListChunk =>
|
||||
chunk(buffer, decodeDataTrackerListChunk) as DataTrackerListChunk;
|
||||
@@ -1,35 +0,0 @@
|
||||
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);
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Constants } from '../../constants';
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
import packetHeaderChunk, { PacketHeaderChunk } from '../packet-header-chunk';
|
||||
import infoSystemNameChunk, { InfoSystemNameChunk } from './info-system-name-chunk';
|
||||
import infoTrackerListChunk, { InfoTrackerListChunk } from './info-tracker-list-chunk';
|
||||
|
||||
export interface InfoPacketChunk extends Chunk {
|
||||
packet_header: PacketHeaderChunk;
|
||||
system_name: InfoSystemNameChunk;
|
||||
tracker_list: InfoTrackerListChunk;
|
||||
}
|
||||
|
||||
function decodeInfoPacketChunk(infoPacketChunk: InfoPacketChunk) {
|
||||
if (infoPacketChunk.has_subchunks && infoPacketChunk.chunk_data && infoPacketChunk.data_len) {
|
||||
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 += Constants.CHUNK_HEADER_SIZE;
|
||||
if (infoPacketChunk.packet_header?.data_len) {
|
||||
offset += infoPacketChunk.packet_header.data_len;
|
||||
}
|
||||
break;
|
||||
case 0x0001:
|
||||
infoPacketChunk.system_name = infoSystemNameChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += Constants.CHUNK_HEADER_SIZE;
|
||||
if (infoPacketChunk.system_name?.data_len) {
|
||||
offset += infoPacketChunk.system_name.data_len;
|
||||
}
|
||||
break;
|
||||
case 0x0002:
|
||||
infoPacketChunk.tracker_list = infoTrackerListChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += Constants.CHUNK_HEADER_SIZE;
|
||||
if (infoPacketChunk.tracker_list?.data_len) {
|
||||
offset += infoPacketChunk.tracker_list.data_len;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default (buffer: Buffer): InfoPacketChunk => chunk(buffer, decodeInfoPacketChunk) as InfoPacketChunk;
|
||||
@@ -1,9 +0,0 @@
|
||||
const chunk = require('../chunk');
|
||||
|
||||
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,13 @@
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
|
||||
export interface InfoSystemNameChunk extends Chunk {
|
||||
system_name: string;
|
||||
}
|
||||
|
||||
function decodeSystemNameChunk(systemNameChunk: InfoSystemNameChunk) {
|
||||
if (systemNameChunk.chunk_data !== undefined && systemNameChunk.data_len !== undefined) {
|
||||
systemNameChunk.system_name = systemNameChunk.chunk_data.subarray(0, systemNameChunk.data_len).toString();
|
||||
}
|
||||
}
|
||||
|
||||
export default (buffer: Buffer): InfoSystemNameChunk => chunk(buffer, decodeSystemNameChunk) as InfoSystemNameChunk;
|
||||
@@ -1,24 +0,0 @@
|
||||
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);
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Constants } from '../../constants';
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
import infoTrackerNameChunk, { InfoTrackerNameChunk } from './info-tracker-name-chunk';
|
||||
|
||||
export interface InfoTrackerChunk extends Chunk {
|
||||
tracker_name?: InfoTrackerNameChunk;
|
||||
}
|
||||
|
||||
function decodeInfoTrackerChunk(infoTrackerChunk: InfoTrackerChunk) {
|
||||
if (infoTrackerChunk.has_subchunks && infoTrackerChunk.chunk_data && infoTrackerChunk.data_len) {
|
||||
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 += Constants.CHUNK_HEADER_SIZE;
|
||||
if (infoTrackerChunk.tracker_name?.data_len) {
|
||||
offset += infoTrackerChunk?.tracker_name?.data_len;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default (buffer: Buffer): InfoTrackerChunk => chunk(buffer, decodeInfoTrackerChunk);
|
||||
@@ -1,17 +0,0 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const infoTrackerChunk = require('./info-tracker-chunk');
|
||||
|
||||
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,29 @@
|
||||
import { Constants } from '../../constants';
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
import infoTrackerChunk, { InfoTrackerChunk } from './info-tracker-chunk';
|
||||
|
||||
export interface InfoTrackerListChunk extends Chunk {
|
||||
trackers: {
|
||||
[key: number]: InfoTrackerChunk;
|
||||
};
|
||||
}
|
||||
function decodeInfoTrackerListChunk(infoTrackerListChunk: InfoTrackerListChunk) {
|
||||
infoTrackerListChunk.trackers = {};
|
||||
if (infoTrackerListChunk.has_subchunks && infoTrackerListChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
if (infoTrackerListChunk.data_len) {
|
||||
while (offset < infoTrackerListChunk.data_len) {
|
||||
const trackerChunk = infoTrackerChunk(infoTrackerListChunk.chunk_data.subarray(offset));
|
||||
offset += Constants.CHUNK_HEADER_SIZE;
|
||||
if (trackerChunk.data_len) {
|
||||
offset += trackerChunk.data_len;
|
||||
}
|
||||
if (trackerChunk.id) {
|
||||
infoTrackerListChunk.trackers[trackerChunk.id] = trackerChunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
export default (buffer: Buffer): InfoTrackerListChunk =>
|
||||
chunk(buffer, decodeInfoTrackerListChunk) as InfoTrackerListChunk;
|
||||
@@ -1,8 +0,0 @@
|
||||
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);
|
||||
@@ -0,0 +1,11 @@
|
||||
import chunk, { Chunk } from '../chunk';
|
||||
export interface InfoTrackerNameChunk extends Chunk {
|
||||
tracker_name: string;
|
||||
}
|
||||
|
||||
function decodeTrackerNameChunk(trackerNameChunk: InfoTrackerNameChunk) {
|
||||
if (trackerNameChunk.chunk_data !== undefined && trackerNameChunk.data_len !== undefined) {
|
||||
trackerNameChunk.tracker_name = trackerNameChunk.chunk_data.subarray(0, trackerNameChunk.data_len).toString();
|
||||
}
|
||||
}
|
||||
export default (buffer: Buffer): InfoTrackerNameChunk => chunk(buffer, decodeTrackerNameChunk) as InfoTrackerNameChunk;
|
||||
@@ -1,12 +0,0 @@
|
||||
const chunk = require('./chunk');
|
||||
|
||||
function decodePacketHeaderChunk(packetHeaderChunk) {
|
||||
// TODO(jwetzell): remove the string conversion
|
||||
packetHeaderChunk.packet_timestamp = packetHeaderChunk.chunk_data.readBigUInt64LE(0);
|
||||
packetHeaderChunk.version_high = packetHeaderChunk.chunk_data.readUInt8(8);
|
||||
packetHeaderChunk.version_low = packetHeaderChunk.chunk_data.readUInt8(9);
|
||||
packetHeaderChunk.frame_id = packetHeaderChunk.chunk_data.readUInt8(10);
|
||||
packetHeaderChunk.frame_packet_count = packetHeaderChunk.chunk_data.readUInt8(11);
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodePacketHeaderChunk);
|
||||
@@ -0,0 +1,21 @@
|
||||
import chunk, { Chunk } from './chunk';
|
||||
export interface PacketHeaderChunk extends Chunk {
|
||||
packet_timestamp?: bigint;
|
||||
version_high?: number;
|
||||
version_low?: number;
|
||||
frame_id?: number;
|
||||
frame_packet_count?: number;
|
||||
}
|
||||
|
||||
function decodePacketHeaderChunk(packetHeaderChunk: PacketHeaderChunk) {
|
||||
// TODO(jwetzell): remove the string conversion
|
||||
if (packetHeaderChunk.chunk_data) {
|
||||
packetHeaderChunk.packet_timestamp = packetHeaderChunk.chunk_data.readBigUInt64LE(0);
|
||||
packetHeaderChunk.version_high = packetHeaderChunk.chunk_data.readUInt8(8);
|
||||
packetHeaderChunk.version_low = packetHeaderChunk.chunk_data.readUInt8(9);
|
||||
packetHeaderChunk.frame_id = packetHeaderChunk.chunk_data.readUInt8(10);
|
||||
packetHeaderChunk.frame_packet_count = packetHeaderChunk.chunk_data.readUInt8(11);
|
||||
}
|
||||
}
|
||||
|
||||
export default (buffer: Buffer): PacketHeaderChunk => chunk(buffer, decodePacketHeaderChunk);
|
||||
@@ -1,14 +0,0 @@
|
||||
const dataPacketChunk = require('./data/data-packet-chunk');
|
||||
const infoPacketChunk = require('./info/info-packet-chunk');
|
||||
|
||||
module.exports = (buffer) => {
|
||||
const chunkId = buffer.readUInt16LE();
|
||||
switch (chunkId) {
|
||||
case 0x6756:
|
||||
return infoPacketChunk(buffer);
|
||||
case 0x6755:
|
||||
return dataPacketChunk(buffer);
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
import infoPacketChunk, { InfoPacketChunk } from './info/info-packet-chunk';
|
||||
|
||||
import dataPacketChunk, { DataPacketChunk } from './data/data-packet-chunk';
|
||||
|
||||
export default (buffer: Buffer): InfoPacketChunk | DataPacketChunk | undefined => {
|
||||
const chunkId = buffer.readUInt16LE();
|
||||
switch (chunkId) {
|
||||
case 0x6756:
|
||||
return infoPacketChunk(buffer);
|
||||
case 0x6755:
|
||||
return dataPacketChunk(buffer);
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user