Skip to content

Commit 617da28

Browse files
committed
add tests and examples to decode multi-value MessagePack binaries with decodeStream()
1 parent 9a84aa6 commit 617da28

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ deepStrictEqual(decode(encoded), object);
5757
- [Decoding a Blob](#decoding-a-blob)
5858
- [MessagePack Specification](#messagepack-specification)
5959
- [MessagePack Mapping Table](#messagepack-mapping-table)
60-
- [Prerequsites](#prerequsites)
60+
- [Prerequisites](#prerequisites)
6161
- [ECMA-262](#ecma-262)
6262
- [NodeJS](#nodejs)
6363
- [TypeScript](#typescript)
@@ -215,6 +215,22 @@ for await (const item of decodeStream(stream)) {
215215
}
216216
```
217217

218+
If you have a multi-values MessagePack binary, you can use `decodeStream()`, but you need to convert it to a stream or an async generator like this:
219+
220+
```typescript
221+
// A function that generates an AsyncGenerator
222+
const createStream = async function* (): AsyncGenerator<Uint8Array> {
223+
yield encoded;
224+
};
225+
226+
const result: Array<unknown> = [];
227+
228+
// Decodes it with for-await
229+
for await (const item of decodeStream(createStream())) {
230+
result.push(item);
231+
}
232+
```
233+
218234
### Reusing Encoder and Decoder instances
219235

220236
`Encoder` and `Decoder` classes is provided for better performance:
@@ -461,7 +477,7 @@ Date|timestamp ext family|Date (*4)
461477
* *3 In handling `Object`, it is regarded as `Record<string, unknown>` in terms of TypeScript
462478
* *4 MessagePack timestamps may have nanoseconds, which will lost when it is decoded into JavaScript `Date`. This behavior can be overridden by registering `-1` for the extension codec.
463479

464-
## Prerequsites
480+
## Prerequisites
465481

466482
This is a universal JavaScript library that supports major browsers and NodeJS.
467483

test/decodeStream.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "assert";
22
import { encode, decodeStream } from "@msgpack/msgpack";
33

44
describe("decodeStream", () => {
5-
it("decodes stream (array8)", async () => {
5+
it("decodes stream", async () => {
66
const items = [
77
"foo",
88
10,
@@ -12,7 +12,7 @@ describe("decodeStream", () => {
1212
[1, 2, 3],
1313
];
1414

15-
const createStream = async function* () {
15+
const createStream = async function* (): AsyncGenerator<Uint8Array> {
1616
for (const item of items) {
1717
yield encode(item);
1818
}
@@ -26,4 +26,35 @@ describe("decodeStream", () => {
2626

2727
assert.deepStrictEqual(result, items);
2828
});
29+
30+
it("decodes stream with a single binary", async () => {
31+
const items = [
32+
"foo",
33+
10,
34+
{
35+
name: "bar",
36+
},
37+
[1, 2, 3],
38+
];
39+
40+
const encodedItems = items.map((item) => encode(item));
41+
const encoded = new Uint8Array(encodedItems.reduce((p, c) => p + c.byteLength, 0));
42+
let offset = 0;
43+
for (const encodedItem of encodedItems) {
44+
encoded.set(encodedItem, offset);
45+
offset += encodedItem.byteLength;
46+
}
47+
48+
const createStream = async function* (): AsyncGenerator<Uint8Array> {
49+
yield encoded;
50+
};
51+
52+
const result: Array<unknown> = [];
53+
54+
for await (const item of decodeStream(createStream())) {
55+
result.push(item);
56+
}
57+
58+
assert.deepStrictEqual(result, items);
59+
});
2960
});

0 commit comments

Comments
 (0)