Skip to content

Commit 1a06ae2

Browse files
committedSep 6, 2020
fix the order of args in Decoder/Encoder
1 parent 9825173 commit 1a06ae2

8 files changed

+18
-18
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ deepStrictEqual(decode(encoded), object);
5050
- [`decodeArrayStream(stream: AsyncIterable<ArrayLike<number>> | ReadableStream<ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`](#decodearraystreamstream-asynciterablearraylikenumber--readablestreamarraylikenumber-options-decodeasyncoptions-asynciterableunknown)
5151
- [`decodeStream(stream: AsyncIterable<ArrayLike<number>> | ReadableStream<ArrayLike<number>>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`](#decodestreamstream-asynciterablearraylikenumber--readablestreamarraylikenumber-options-decodeasyncoptions-asynciterableunknown)
5252
- [Extension Types](#extension-types)
53-
- [Codec context](#codec-context)
53+
- [ExtensionCodec context](#extensioncodec-context)
5454
- [Handling BigInt with ExtensionCodec](#handling-bigint-with-extensioncodec)
5555
- [The temporal module as timestamp extensions](#the-temporal-module-as-timestamp-extensions)
5656
- [Decoding a Blob](#decoding-a-blob)
@@ -266,7 +266,7 @@ const decoded = decode(encoded, { extensionCodec });
266266

267267
Not that extension types for custom objects must be `[0, 127]`, while `[-1, -128]` is reserved for MessagePack itself.
268268

269-
#### Codec context
269+
#### ExtensionCodec context
270270

271271
When using an extension codec, it may be necessary to keep encoding/decoding state, to keep track of which objects got encoded/re-created. To do this, pass a `context` to the `EncodeOptions` and `DecodeOptions` (and if using typescript, type the `ExtensionCodec` too). Don't forget to pass the `{extensionCodec, context}` along recursive encoding/decoding:
272272

‎benchmark/benchmark-from-msgpack-lite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ if (msgpack_msgpack) {
6767
const encoder = new msgpack_msgpack.Encoder();
6868
const decoder = new msgpack_msgpack.Decoder();
6969
buf = bench('buf = /* @msgpack/msgpack */ encoder.encode(obj);', (data) => encoder.encode(data), data);
70-
obj = bench('obj = /* @msgpack/msgpack */ decoder.decodeSync(buf);', (buf) => decoder.decodeSync(buf), buf);
70+
obj = bench('obj = /* @msgpack/msgpack */ decoder.decode(buf);', (buf) => decoder.decode(buf), buf);
7171
runTest(obj);
7272

7373
if (process.env.CACHE_HIT_RATE) {

‎src/Decoder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export class Decoder<ContextType> {
7070
private readonly stack: Array<StackState> = [];
7171

7272
public constructor(
73-
private readonly context: ContextType,
7473
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
74+
private readonly context: ContextType = undefined as any,
7575
private readonly maxStrLength = DEFAULT_MAX_LENGTH,
7676
private readonly maxBinLength = DEFAULT_MAX_LENGTH,
7777
private readonly maxArrayLength = DEFAULT_MAX_LENGTH,

‎src/Encoder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export class Encoder<ContextType> {
1313
private bytes = new Uint8Array(this.view.buffer);
1414

1515
public constructor(
16-
private readonly context: ContextType,
1716
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any,
17+
private readonly context: ContextType = undefined as any,
1818
private readonly maxDepth = DEFAULT_MAX_DEPTH,
1919
private readonly initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE,
2020
private readonly sortKeys = false,

‎src/decode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function decode<ContextType>(
4747
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any,
4848
): unknown {
4949
const decoder = new Decoder<ContextType>(
50-
(options as typeof options & { context: any }).context,
5150
options.extensionCodec,
51+
(options as typeof options & { context: any }).context,
5252
options.maxStrLength,
5353
options.maxBinLength,
5454
options.maxArrayLength,

‎src/decodeAsync.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export async function decodeAsync<ContextType>(
1010
const stream = ensureAsyncIterabe(streamLike);
1111

1212
const decoder = new Decoder<ContextType>(
13-
(options as typeof options & { context: any }).context,
1413
options.extensionCodec,
14+
(options as typeof options & { context: any }).context,
1515
options.maxStrLength,
1616
options.maxBinLength,
1717
options.maxArrayLength,
@@ -28,8 +28,8 @@ export function decodeArrayStream<ContextType>(
2828
const stream = ensureAsyncIterabe(streamLike);
2929

3030
const decoder = new Decoder<ContextType>(
31-
(options as typeof options & { context: any }).context,
3231
options.extensionCodec,
32+
(options as typeof options & { context: any }).context,
3333
options.maxStrLength,
3434
options.maxBinLength,
3535
options.maxArrayLength,
@@ -47,8 +47,8 @@ export function decodeStream<ContextType>(
4747
const stream = ensureAsyncIterabe(streamLike);
4848

4949
const decoder = new Decoder<ContextType>(
50-
(options as typeof options & { context: any }).context,
5150
options.extensionCodec,
51+
(options as typeof options & { context: any }).context,
5252
options.maxStrLength,
5353
options.maxBinLength,
5454
options.maxArrayLength,

‎src/encode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export function encode<ContextType>(
4040
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any,
4141
): Uint8Array {
4242
const encoder = new Encoder<ContextType>(
43-
(options as typeof options & { context: any }).context,
4443
options.extensionCodec,
44+
(options as typeof options & { context: any }).context,
4545
options.maxDepth,
4646
options.initialBufferSize,
4747
options.sortKeys,

‎test/reuse-instances.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const createStream = async function* (...args: any) {
99

1010
describe("shared instances", () => {
1111
context("encode() and decodeSync()", () => {
12-
const encoder = new Encoder(undefined);
13-
const decoder = new Decoder(undefined);
12+
const encoder = new Encoder();
13+
const decoder = new Decoder();
1414

1515
it("runs #1", () => {
1616
const object = {
@@ -46,8 +46,8 @@ describe("shared instances", () => {
4646
});
4747

4848
context("encode() and decodeAsync()", () => {
49-
const encoder = new Encoder(undefined);
50-
const decoder = new Decoder(undefined);
49+
const encoder = new Encoder();
50+
const decoder = new Decoder();
5151

5252
it("runs #1", async () => {
5353
const object = {
@@ -83,8 +83,8 @@ describe("shared instances", () => {
8383
});
8484

8585
context("encode() and decodeStream()", () => {
86-
const encoder = new Encoder(undefined);
87-
const decoder = new Decoder(undefined);
86+
const encoder = new Encoder();
87+
const decoder = new Decoder();
8888

8989
it("runs #1", async () => {
9090
const object = {
@@ -128,8 +128,8 @@ describe("shared instances", () => {
128128
});
129129

130130
context("encode() and decodeArrayStream()", () => {
131-
const encoder = new Encoder(undefined);
132-
const decoder = new Decoder(undefined);
131+
const encoder = new Encoder();
132+
const decoder = new Decoder();
133133

134134
it("runs #1", async () => {
135135
const object = {

0 commit comments

Comments
 (0)
Failed to load comments.