Skip to content

Commit b41a754

Browse files
committed
doc
1 parent 19d4891 commit b41a754

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

README.md

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ npm install @msgpack/msgpack
8888

8989
### `encode(data: unknown, options?: EncodeOptions): Uint8Array`
9090

91-
It encodes `data` and returns a byte array as `Uint8Array`, throwing errors if `data` is, or includes, a non-serializable object such as a `function` or a `symbol`.
91+
It encodes `data` into a single MessagePack-encoded object, and returns a byte array as `Uint8Array`, throwing errors if `data` is, or includes, a non-serializable object such as a `function` or a `symbol`.
9292

9393
for example:
9494

@@ -99,7 +99,7 @@ const encoded: Uint8Array = encode({ foo: "bar" });
9999
console.log(encoded);
100100
```
101101

102-
If you'd like to convert the uint8array to a NodeJS `Buffer`, use `Buffer.from(arrayBuffer, offset, length)` in order not to copy the underlying `ArrayBuffer`, while `Buffer.from(uint8array)` copies it:
102+
If you'd like to convert an `uint8array` to a NodeJS `Buffer`, use `Buffer.from(arrayBuffer, offset, length)` in order not to copy the underlying `ArrayBuffer`, while `Buffer.from(uint8array)` copies it:
103103

104104
```typescript
105105
import { encode } from "@msgpack/msgpack";
@@ -115,7 +115,7 @@ console.log(buffer);
115115

116116
Name|Type|Default
117117
----|----|----
118-
extensionCodec | ExtensionCodec | `ExtensinCodec.defaultCodec`
118+
extensionCodec | ExtensionCodec | `ExtensionCodec.defaultCodec`
119119
maxDepth | number | `100`
120120
initialBufferSize | number | `2048`
121121
sortKeys | boolean | false
@@ -126,10 +126,12 @@ context | user-defined | -
126126

127127
### `decode(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): unknown`
128128

129-
It decodes `buffer` encoded in MessagePack, and returns a decoded object as `unknown`.
129+
It decodes `buffer` that includes a MessagePack-encoded object, and returns the decoded object typed `unknown`.
130130

131131
`buffer` must be an array of bytes, which is typically `Uint8Array` or `ArrayBuffer`. `BufferSource` is defined as `ArrayBuffer | ArrayBufferView`.
132132

133+
In addition, `buffer` can include a single encoded object. If the `buffer` includes extra bytes after an object, it will throw `RangeError`. To decode `buffer` that includes multiple encoded objects, use `decodeMulti()` or `decodeMultiStream()` (recommended) instead.
134+
133135
for example:
134136

135137
```typescript
@@ -158,9 +160,9 @@ You can use `max${Type}Length` to limit the length of each type decoded.
158160

159161
### `decodeMulti(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): Generator<unknown, void, unknown>`
160162

161-
It decodes `buffer` encoded in MessagePack, and returns decoded objects as a generator. That is, this is a synchronous variant for `decodeMultiStream()`.
163+
It decodes `buffer` that includes multiple MessagePack-encoded objects, and returns decoded objects as a generator. That is, this is a synchronous variant for `decodeMultiStream()`.
162164

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.
165+
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, typically spending less time and memory.
164166

165167
for example:
166168

@@ -176,7 +178,7 @@ for (const object of decodeMulti(encoded)) {
176178

177179
### `decodeAsync(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): Promise<unknown>`
178180

179-
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.
181+
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. This is an async variant for `decode()`.
180182

181183
`DecodeAsyncOptions` is the same as `DecodeOptions` for `decode()`.
182184

@@ -197,9 +199,7 @@ if (contentType && contentType.startsWith(MSGPACK_TYPE) && response.body != null
197199

198200
### `decodeArrayStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
199201

200-
It is alike to `decodeAsync()`, but only accepts an array of items as the input `stream`, and emits the decoded item one by one.
201-
202-
It throws errors when the input is not an array-family.
202+
It is alike to `decodeAsync()`, but only accepts a `stream` that includes an array of items, and emits a decoded item one by one.
203203

204204
for example:
205205

@@ -214,12 +214,11 @@ for await (const item of decodeArrayStream(stream)) {
214214
}
215215
```
216216

217-
218217
### `decodeMultiStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`
219218

220-
It is alike to `decodeAsync()` and `decodeArrayStream()`, but the input `stream` consists of multiple MessagePack items.
219+
It is alike to `decodeAsync()` and `decodeArrayStream()`, but the input `stream` must consist of multiple MessagePack-encoded items. This is an asynchronous variant for `decodeMulti()`.
221220

222-
In other words, it could decode an unlimited stream and emits an item one by one.
221+
In other words, it could decode an unlimited stream and emits a decoded item one by one.
223222

224223
for example:
225224

@@ -234,23 +233,7 @@ for await (const item of decodeStream(stream)) {
234233
}
235234
```
236235

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:
238-
239-
```typescript
240-
// A function that generates an AsyncGenerator
241-
const createStream = async function* (): AsyncGenerator<Uint8Array> {
242-
yield encoded;
243-
};
244-
245-
const result: Array<unknown> = [];
246-
247-
// Decodes it with for-await
248-
for await (const item of decodeMultiStream(createStream())) {
249-
result.push(item);
250-
}
251-
```
252-
253-
It is available since v2.4.0; previously it was called as `decodeStream()`.
236+
This function is available since v2.4.0; previously it was called as `decodeStream()`.
254237

255238
### Reusing Encoder and Decoder instances
256239

0 commit comments

Comments
 (0)