[go: nahoru, domu]

Skip to content

Commit

Permalink
Add a pretty printer for snac debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewML committed Dec 31, 2020
1 parent 42304f3 commit d29ef6d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/BossServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ssiLimitsSnac,
} from './serverSnacs';
import assert from 'assert';
import { parseSnac, matchSnac } from '../snacUtils';
import { parseSnac, matchSnac, prettyPrintSnac } from '../snacUtils';
import { UserClass, UserStatus } from './constants';

export class BossServer extends OscarServer {
Expand Down Expand Up @@ -75,7 +75,8 @@ export class BossServer extends OscarServer {
});
}

console.log('boss unhandled data flap: ', flap);
console.log('BOSS: unhandled snac:');
console.log(prettyPrintSnac(snac));
});

oscarSocket.onFlap(FlapType.ERROR, (flap) => {
Expand Down
37 changes: 37 additions & 0 deletions src/buf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { endianness } from 'os';

const GREEN_TEXT = '\x1b[32m';
const RESET_TEXT = '\x1b[0m';

const isLE = endianness() === 'LE';

/**
Expand All @@ -11,3 +14,37 @@ export function uint16(values: number | number[]) {
if (isLE) buf.swap16();
return buf;
}

/**
* @summary Pretty print a Buffer as hex-encoded bytes
*/
export function prettyPrint(buffer: Buffer, maxBytesPerLine = 10) {
const hexStr = buffer.toString('hex');
// split hex string into byte-sized chunks
const bytes = hexStr.match(/.{1,2}/g);

if (!bytes) return '';

let output = '';
let itemCount = 0;

while (bytes.length) {
const byte = bytes.shift();
output += `0x${byte}`;

if (itemCount + 1 === maxBytesPerLine) {
// reset line item counter
itemCount = 0;
// drop to a new line
output += '\n';
} else {
if (bytes.length) {
// not the last item in the row, so add whitespace padding
output += ' ';
}
itemCount++;
}
}

return `${GREEN_TEXT}${output}${RESET_TEXT}`;
}
19 changes: 19 additions & 0 deletions src/snacUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Snac } from './types';
import { SNACS } from './constants';
import { prettyPrint } from './buf';

const BOLD_TEXT = '\x1b[1m';
const RESET_TEXT = '\x1b[0m';

/**
* @summary Determines whether a Snac is a specific
Expand Down Expand Up @@ -54,3 +58,18 @@ export function parseSnac(rawSnac: Buffer): Snac {
data: rawSnac.subarray(10),
};
}

/**
* @summary Pretty Print a parsed SNAC
*/
export function prettyPrintSnac(snac: Snac) {
const printedData = prettyPrint(snac.data);
const family = `0x${snac.family.toString(16)}`;
const subtype = `0x${snac.subtype.toString(16)}`;

return `${BOLD_TEXT}Family:${RESET_TEXT} ${family}
${BOLD_TEXT}Subtype:${RESET_TEXT} ${subtype}
${BOLD_TEXT}Flags:${RESET_TEXT} ${snac.flags}
${BOLD_TEXT}Request ID:${RESET_TEXT} ${snac.requestID}
${BOLD_TEXT}Payload:${RESET_TEXT}\n${printedData}\n`;
}

0 comments on commit d29ef6d

Please sign in to comment.