Skip to content

Commit 7a27d1e

Browse files
committed
feat(json-pack): 🎸 add Bencode ecndogin support for const tokens
1 parent 239442e commit 7a27d1e

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/json-pack/bencode/BencodeEncoder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ export class BencodeEncoder implements BinaryJsonEncoder {
6060
}
6161

6262
public writeNull(): void {
63-
throw new Error('NULL_NOT_SUPPORTED');
63+
this.writer.u8(110); // 'n'
6464
}
6565

6666
public writeUndef(): void {
67-
throw new Error('UNDEF_NOT_SUPPORTED');
67+
this.writer.u8(117); // 'u'
6868
}
6969

7070
public writeBoolean(bool: boolean): void {
71-
throw new Error('BOOL_NOT_SUPPORTED');
71+
this.writer.u8(bool ? 0x74 : 0x66); // 't' or 'f'
7272
}
7373

7474
public writeNumber(num: number): void {

src/json-pack/bencode/__tests__/BencodeEncoder.spec.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,27 @@ const assertEncoder = (value: unknown, expected: Uint8Array) => {
1111
expect(encoded).toEqual(expected);
1212
};
1313

14-
// describe('null', () => {
15-
// test('null', () => {
16-
// assertEncoder(null);
17-
// });
18-
// });
19-
20-
// describe('undefined', () => {
21-
// test('undefined', () => {
22-
// const encoded = encoder.encode(undefined);
23-
// const txt = Buffer.from(encoded).toString('utf-8');
24-
// expect(txt).toBe('"data:application/cbor,base64;9w=="');
25-
// });
26-
27-
// test('undefined in object', () => {
28-
// const encoded = encoder.encode({foo: undefined});
29-
// const txt = Buffer.from(encoded).toString('utf-8');
30-
// expect(txt).toBe('{"foo":"data:application/cbor,base64;9w=="}');
31-
// });
32-
// });
33-
34-
// describe('boolean', () => {
35-
// test('true', () => {
36-
// assertEncoder(true);
37-
// });
38-
39-
// test('false', () => {
40-
// assertEncoder(false);
41-
// });
42-
// });
14+
describe('null', () => {
15+
test('null', () => {
16+
assertEncoder(null, utf8`n`);
17+
});
18+
});
19+
20+
describe('undefined', () => {
21+
test('undefined', () => {
22+
assertEncoder(undefined, utf8`u`);
23+
});
24+
});
25+
26+
describe('boolean', () => {
27+
test('true', () => {
28+
assertEncoder(true, utf8`t`);
29+
});
30+
31+
test('false', () => {
32+
assertEncoder(false, utf8`f`);
33+
});
34+
});
4335

4436
describe('number', () => {
4537
test('integers', () => {

0 commit comments

Comments
 (0)