Skip to content

Commit

Permalink
Merge pull request #7 from jsonjoy-com/bin-support
Browse files Browse the repository at this point in the history
`Buffer` support
  • Loading branch information
streamich authored Aug 7, 2024
2 parents 8606307 + 4946ecb commit 91c725f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cbor/CborEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export class CborEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriter
const buf = (value as JsonPackValue).val;
return this.writer.buf(buf, buf.length);
default:
if (value instanceof Uint8Array) return this.writeBin(value);
if (Array.isArray(value)) return this.writeArr(value);
if (value instanceof Map) return this.writeMap(value);
return this.writeUnknown(value);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/cbor/__tests__/CborEncoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ describe('binary', () => {
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', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/json/JsonEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/json/__tests__/buffer.spec.ts
Original file line number Diff line number Diff line change
@@ -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]));
});
9 changes: 9 additions & 0 deletions src/msgpack/__tests__/MsgPackEncoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down

0 comments on commit 91c725f

Please sign in to comment.