Skip to content

Commit 19d4891

Browse files
committed
doc
1 parent 89bc323 commit 19d4891

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ deepStrictEqual(decode(encoded), object);
4747
- [`EncodeOptions`](#encodeoptions)
4848
- [`decode(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): unknown`](#decodebuffer-arraylikenumber--buffersource-options-decodeoptions-unknown)
4949
- [`DecodeOptions`](#decodeoptions)
50+
- [`decodeMulti(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): Generator<unknown, void, unknown>`](#decodemultibuffer-arraylikenumber--buffersource-options-decodeoptions-generatorunknown-void-unknown)
5051
- [`decodeAsync(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): Promise<unknown>`](#decodeasyncstream-readablestreamlikearraylikenumber--buffersource-options-decodeasyncoptions-promiseunknown)
5152
- [`decodeArrayStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`](#decodearraystreamstream-readablestreamlikearraylikenumber--buffersource-options-decodeasyncoptions-asynciterableunknown)
52-
- [`decodeStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`](#decodestreamstream-readablestreamlikearraylikenumber--buffersource-options-decodeasyncoptions-asynciterableunknown)
53+
- [`decodeMultiStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`](#decodemultistreamstream-readablestreamlikearraylikenumber--buffersource-options-decodeasyncoptions-asynciterableunknown)
5354
- [Reusing Encoder and Decoder instances](#reusing-encoder-and-decoder-instances)
5455
- [Extension Types](#extension-types)
5556
- [ExtensionCodec context](#extensioncodec-context)
@@ -155,6 +156,24 @@ context | user-defined | -
155156

156157
You can use `max${Type}Length` to limit the length of each type decoded.
157158

159+
### `decodeMulti(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): Generator<unknown, void, unknown>`
160+
161+
It decodes `buffer` encoded in MessagePack, and returns decoded objects as a generator. That is, this is a synchronous variant for `decodeMultiStream()`.
162+
163+
This function is not recommended to decode a MessagePack binary via I/O stream including sockets because it's synchronous. Instead, `decodeMultiStream()` decodes it asynchronously, likely spending less time and memory.
164+
165+
for example:
166+
167+
```typescript
168+
import { decode } from "@msgpack/msgpack";
169+
170+
const encoded: Uint8Array;
171+
172+
for (const object of decodeMulti(encoded)) {
173+
console.log(object);
174+
}
175+
```
176+
158177
### `decodeAsync(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): Promise<unknown>`
159178

160179
It decodes `stream`, where `ReadableStreamLike<T>` is defined as `ReadableStream<T> | AsyncIterable<T>`, in an async iterable of byte arrays, and returns decoded object as `unknown` type, wrapped in `Promise`. This function works asynchronously.
@@ -196,11 +215,11 @@ for await (const item of decodeArrayStream(stream)) {
196215
```
197216

198217

199-
### `decodeStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
218+
### `decodeMultiStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
200219

201-
It is alike to `decodeAsync()` and `decodeArrayStream()`, but the input `stream` consists of independent MessagePack items.
220+
It is alike to `decodeAsync()` and `decodeArrayStream()`, but the input `stream` consists of multiple MessagePack items.
202221

203-
In other words, it decodes an unlimited stream and emits an item one by one.
222+
In other words, it could decode an unlimited stream and emits an item one by one.
204223

205224
for example:
206225

@@ -215,7 +234,7 @@ for await (const item of decodeStream(stream)) {
215234
}
216235
```
217236

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:
237+
If you have a multi-values MessagePack binary, you can use `decodeMultiStream()`, but you need to convert it to a stream or an async generator like this:
219238

220239
```typescript
221240
// A function that generates an AsyncGenerator
@@ -226,11 +245,13 @@ const createStream = async function* (): AsyncGenerator<Uint8Array> {
226245
const result: Array<unknown> = [];
227246

228247
// Decodes it with for-await
229-
for await (const item of decodeStream(createStream())) {
248+
for await (const item of decodeMultiStream(createStream())) {
230249
result.push(item);
231250
}
232251
```
233252

253+
It is available since v2.4.0; previously it was called as `decodeStream()`.
254+
234255
### Reusing Encoder and Decoder instances
235256

236257
`Encoder` and `Decoder` classes is provided for better performance:

0 commit comments

Comments
 (0)