Skip to content

Commit 91c725f

Browse files
authored
Merge pull request #7 from jsonjoy-com/bin-support
`Buffer` support
2 parents 8606307 + 4946ecb commit 91c725f

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

src/cbor/CborEncoder.ts

+3
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

+7
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', () => {

src/json/JsonEncoder.ts

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export class JsonEncoder implements BinaryJsonEncoder, StreamingBinaryJsonEncode
4040
case Uint8Array:
4141
return this.writeBin(value as Uint8Array);
4242
default:
43+
if (value instanceof Uint8Array) return this.writeBin(value);
44+
if (Array.isArray(value)) return this.writeArr(value);
4345
return this.writeUnknown(value);
4446
}
4547
}

src/json/__tests__/buffer.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Writer} from '@jsonjoy.com/util/lib/buffers/Writer';
2+
import {JsonEncoder} from '../JsonEncoder';
3+
import {JsonDecoder} from '../JsonDecoder';
4+
5+
test('supports Buffer', () => {
6+
const encoder = new JsonEncoder(new Writer());
7+
const buf = Buffer.from([1, 2, 3]);
8+
const encoded = encoder.encode(buf);
9+
const decoder = new JsonDecoder();
10+
const decoded = decoder.decode(encoded);
11+
expect(decoded).toStrictEqual(new Uint8Array([1, 2, 3]));
12+
});

src/msgpack/__tests__/MsgPackEncoder.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ describe('binary', () => {
1818
});
1919
});
2020

21+
describe('Buffer', () => {
22+
test('supports Buffer instances', () => {
23+
const data = {foo: Buffer.from([3, 2, 1])};
24+
const encoded = encode(data);
25+
const decoded = decode(encoded);
26+
expect(decoded).toStrictEqual({foo: new Uint8Array([3, 2, 1])});
27+
});
28+
});
29+
2130
describe('extensions', () => {
2231
test('can encode a 5 byte extension', () => {
2332
const ext = new JsonPackExtension(33, new Uint8Array([1, 2, 3, 4, 5]));

0 commit comments

Comments
 (0)