Skip to content

Buffer support #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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