Skip to content

Commit 94c6e2c

Browse files
authored
Merge pull request #554 from streamich/cbor-packed-value-fix
Support pre-packed values in CBOR codec
2 parents be415a2 + 48e341d commit 94c6e2c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/json-pack/cbor/CborEncoder.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {isFloat32} from '../../util/buffers/isFloat32';
22
import {JsonPackExtension} from '../JsonPackExtension';
33
import {CborEncoderFast} from './CborEncoderFast';
44
import type {IWriter, IWriterGrowable} from '../../util/buffers';
5+
import {JsonPackValue} from '../JsonPackValue';
56

67
export class CborEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> extends CborEncoderFast<W> {
78
/**
@@ -35,6 +36,9 @@ export class CborEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriter
3536
return this.writeMap(value as Map<unknown, unknown>);
3637
case JsonPackExtension:
3738
return this.writeTag((<JsonPackExtension>value).tag, (<JsonPackExtension>value).val);
39+
case JsonPackValue:
40+
const buf = (value as JsonPackValue).val;
41+
return this.writer.buf(buf, buf.length);
3842
default:
3943
return this.writeUnknown(value);
4044
}

src/json-pack/cbor/__tests__/CborEncoder.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Writer} from '../../../util/buffers/Writer';
2+
import {JsonPackValue} from '../../JsonPackValue';
23
import {CborEncoder} from '../CborEncoder';
34
import {decode} from 'cbor';
45

@@ -398,3 +399,15 @@ describe('tokens (simple values)', () => {
398399
testJsTokens(null);
399400
testJsTokens(undefined);
400401
});
402+
403+
describe('JsonPackValue', () => {
404+
test('can encode pre-packed value', () => {
405+
const internal = encoder.encode({foo: 'bar'});
406+
const val = new JsonPackValue(internal);
407+
const data = {boo: [1, val, 2]};
408+
const encoded = encoder.encode(data);
409+
expect(decode(encoded)).toEqual({
410+
boo: [1, {foo: 'bar'}, 2],
411+
});
412+
});
413+
});

0 commit comments

Comments
 (0)