From 1be75f7e8ddaa9ade8d1882f6470dfc3148dbb82 Mon Sep 17 00:00:00 2001 From: streamich Date: Wed, 7 Aug 2024 14:46:47 +0200 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20support=20for?= =?UTF-8?q?=20Buffer=20in=20CBOR=20codec?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cbor/CborEncoder.ts | 3 +++ src/cbor/__tests__/CborEncoder.spec.ts | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/cbor/CborEncoder.ts b/src/cbor/CborEncoder.ts index 706f0ee..1f11427 100644 --- a/src/cbor/CborEncoder.ts +++ b/src/cbor/CborEncoder.ts @@ -40,6 +40,9 @@ export class CborEncoder { const decoded = decode(encoded) as Buffer; expect(toUint8Array(decoded)).toStrictEqual(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9])); }); + + test('can encode Buffer', () => { + const buf = Buffer.from('asdf'); + const encoded = encoder.encode(buf); + const decoded = toUint8Array(decode(encoded)); + expect(decoded).toStrictEqual(toUint8Array(buf)); + }); }); describe('strings', () => { From 5b278520ee2b37fe152f312d54d5d4990f96264e Mon Sep 17 00:00:00 2001 From: streamich Date: Wed, 7 Aug 2024 15:01:47 +0200 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20support=20Buffer=20i?= =?UTF-8?q?n=20JSON=20codec?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json/JsonEncoder.ts | 2 ++ src/json/__tests__/buffer.spec.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/json/__tests__/buffer.spec.ts diff --git a/src/json/JsonEncoder.ts b/src/json/JsonEncoder.ts index db15716..a9acc88 100644 --- a/src/json/JsonEncoder.ts +++ b/src/json/JsonEncoder.ts @@ -40,6 +40,8 @@ export class JsonEncoder implements BinaryJsonEncoder, StreamingBinaryJsonEncode case Uint8Array: return this.writeBin(value as Uint8Array); default: + if (value instanceof Uint8Array) return this.writeBin(value); + if (Array.isArray(value)) return this.writeArr(value); return this.writeUnknown(value); } } diff --git a/src/json/__tests__/buffer.spec.ts b/src/json/__tests__/buffer.spec.ts new file mode 100644 index 0000000..4264588 --- /dev/null +++ b/src/json/__tests__/buffer.spec.ts @@ -0,0 +1,12 @@ +import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer'; +import {JsonEncoder} from '../JsonEncoder'; +import {JsonDecoder} from '../JsonDecoder'; + +test('supports Buffer', () => { + const encoder = new JsonEncoder(new Writer()); + const buf = Buffer.from([1, 2, 3]); + const encoded = encoder.encode(buf); + const decoder = new JsonDecoder(); + const decoded = decoder.decode(encoded); + expect(decoded).toStrictEqual(new Uint8Array([1, 2, 3])); +}); From 4946ecbd31d83273d76eda5137a54ebc1282ab7c Mon Sep 17 00:00:00 2001 From: streamich Date: Wed, 7 Aug 2024 15:04:05 +0200 Subject: [PATCH 3/3] =?UTF-8?q?test:=20=F0=9F=92=8D=20verify=20Buffer=20is?= =?UTF-8?q?=20supported=20in=20MessagePack=20codec?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/msgpack/__tests__/MsgPackEncoder.spec.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/msgpack/__tests__/MsgPackEncoder.spec.ts b/src/msgpack/__tests__/MsgPackEncoder.spec.ts index f6beda0..8c6644c 100644 --- a/src/msgpack/__tests__/MsgPackEncoder.spec.ts +++ b/src/msgpack/__tests__/MsgPackEncoder.spec.ts @@ -18,6 +18,15 @@ describe('binary', () => { }); }); +describe('Buffer', () => { + test('supports Buffer instances', () => { + const data = {foo: Buffer.from([3, 2, 1])}; + const encoded = encode(data); + const decoded = decode(encoded); + expect(decoded).toStrictEqual({foo: new Uint8Array([3, 2, 1])}); + }); +}); + describe('extensions', () => { test('can encode a 5 byte extension', () => { const ext = new JsonPackExtension(33, new Uint8Array([1, 2, 3, 4, 5]));