Skip to content

Commit

Permalink
visualizer: support base64
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Nov 16, 2023
1 parent afc60a8 commit f5362c8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/app/user-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export namespace ext {
export const node: INode = <any> null;

/**
* Visualize a packet
* @param packet can be hex string, binary buffer or an encodable e.g. Interest
* Visualize a NDN TLV block or packet
* @param packet can be hex or base64 string, binary buffer or an encodable e.g. Interest
*/
export function visualize(packet: string | Uint8Array | ArrayBuffer | tlv.Encodable | undefined): void {};

Expand Down
15 changes: 13 additions & 2 deletions src/app/visualizer/visualizer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,19 @@ export class VisualizerComponent implements OnInit {
let buffer: Uint8Array;

if (typeof tlv == 'string') {
const matches = tlv.replace(/\s/g, '').match(/.{1,2}/ig);
buffer = new Uint8Array((matches || []).map(byte => parseInt(byte, 16)));
const tlvStr = tlv.replace(/\s/g, '');

// Guess if hex or base64
if (tlvStr.match(/^[0-9a-fA-F]+$/)) {
// Everything matches hex, so assume hex
buffer = new Uint8Array((tlv.match(/.{1,2}/ig) ?? []).map(c => parseInt(c, 16)));
} else if (tlvStr.match(/^[0-9a-zA-Z+/]+={0,2}$/)) {
// Assume base64
buffer = Uint8Array.from(atob(tlvStr), c => c.charCodeAt(0));
} else {
console.error('Invalid TLV string (not hex or base64)');
return [];
}
} else if (tlv instanceof Uint8Array) {
buffer = tlv;
} else if (tlv instanceof ArrayBuffer) {
Expand Down
4 changes: 2 additions & 2 deletions src/assets/user-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,8 +1231,8 @@ export declare namespace ext {
};
const node: INode;
/**
* Visualize a packet
* @param packet can be hex string, binary buffer or an encodable e.g. Interest
* Visualize a NDN TLV block or packet
* @param packet can be hex or base64 string, binary buffer or an encodable e.g. Interest
*/
export function visualize(packet: string | Uint8Array | ArrayBuffer | tlv.Encodable | undefined): void;
/**
Expand Down

0 comments on commit f5362c8

Please sign in to comment.