File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 1+ import type { CliCodec } from '../types' ;
2+
3+ export class CliCodecHex implements CliCodec < 'hex' > {
4+ public readonly id = 'hex' ;
5+ public readonly description = 'Same as "raw", but encodes binary data as HEX octets' ;
6+
7+ encode ( value : unknown ) : Uint8Array {
8+ const buf = value instanceof Uint8Array ? value : new TextEncoder ( ) . encode ( String ( value ) ) ;
9+ const hex = Array . from ( buf )
10+ . map ( ( byte ) => byte . toString ( 16 ) . padStart ( 2 , '0' ) )
11+ . join ( ' ' ) ;
12+ return new TextEncoder ( ) . encode ( hex + '\n' ) ;
13+ }
14+
15+ decode ( bytes : Uint8Array ) : unknown {
16+ throw new Error ( 'Not available' ) ;
17+ }
18+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import {CliCodecRaw} from './codecs/raw';
99import { CliCodecText } from './codecs/text' ;
1010import { CliCodecTree } from './codecs/tree' ;
1111import { CliCodecUbjson } from './codecs/ubjson' ;
12+ import { CliCodecHex } from './codecs/hex' ;
1213
1314export const defaultCodecs = new CliCodecs ( ) ;
1415
@@ -22,3 +23,4 @@ defaultCodecs.register(new CliCodecUbjson(writer));
2223defaultCodecs . register ( new CliCodecText ( ) ) ;
2324defaultCodecs . register ( new CliCodecTree ( ) ) ;
2425defaultCodecs . register ( new CliCodecRaw ( ) ) ;
26+ defaultCodecs . register ( new CliCodecHex ( ) ) ;
You can’t perform that action at this time.
0 commit comments