Skip to content

Commit 1be75f7

Browse files
committed
feat: 🎸 add support for Buffer in CBOR codec
1 parent 8606307 commit 1be75f7

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/cbor/CborEncoder.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export class CborEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriter
4040
const buf = (value as JsonPackValue).val;
4141
return this.writer.buf(buf, buf.length);
4242
default:
43+
if (value instanceof Uint8Array) return this.writeBin(value);
44+
if (Array.isArray(value)) return this.writeArr(value);
45+
if (value instanceof Map) return this.writeMap(value);
4346
return this.writeUnknown(value);
4447
}
4548
}

src/cbor/__tests__/CborEncoder.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ describe('binary', () => {
135135
const decoded = decode(encoded) as Buffer;
136136
expect(toUint8Array(decoded)).toStrictEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9]));
137137
});
138+
139+
test('can encode Buffer', () => {
140+
const buf = Buffer.from('asdf');
141+
const encoded = encoder.encode(buf);
142+
const decoded = toUint8Array(decode(encoded));
143+
expect(decoded).toStrictEqual(toUint8Array(buf));
144+
});
138145
});
139146

140147
describe('strings', () => {

0 commit comments

Comments
 (0)