Skip to content

Commit 60e0121

Browse files
committed
doc
1 parent 44dcb7b commit 60e0121

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is a JavaScript/ECMA-262 implementation of **MessagePack**, an efficient bi
66

77
https://msgpack.org/
88

9-
This library is a universal JavaScript, meaning it is compatible with all the major browsers and NodeJS. In addition, because it is implemented in [TypeScript](https://www.typescriptlang.org/), type definition files (`d.ts`) are bundled in the distribution.
9+
This library is a universal JavaScript, meaning it is compatible with all the major browsers and NodeJS. In addition, because it is implemented in [TypeScript](https://www.typescriptlang.org/), type definition files (`d.ts`) are always up-to-date and bundled in the distribution.
1010

1111
*Note that this is the second version of MessagePack for JavaScript. The first version, which was implemented in ES5 and was never released to npmjs.com, is tagged as [classic](https://github.com/msgpack/msgpack-javascript/tree/classic).*
1212

@@ -82,7 +82,7 @@ npm install @msgpack/msgpack
8282

8383
### `encode(data: unknown, options?: EncodeOptions): Uint8Array`
8484

85-
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`.
85+
It encodes `data` into a single MessagePack-encoded object, and returns a byte array as `Uint8Array`. It throws errors if `data` is, or includes, a non-serializable object such as a `function` or a `symbol`.
8686

8787
for example:
8888

@@ -124,7 +124,7 @@ It decodes `buffer` that includes a MessagePack-encoded object, and returns the
124124

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

127-
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.
127+
The `buffer` must include a single encoded object. If the `buffer` includes extra bytes after an object or the `buffer` is empty, it throws `RangeError`. To decode `buffer` that includes multiple encoded objects, use `decodeMulti()` or `decodeMultiStream()` (recommended) instead.
128128

129129
for example:
130130

@@ -154,9 +154,9 @@ You can use `max${Type}Length` to limit the length of each type decoded.
154154

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

157-
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()`.
157+
It decodes `buffer` that includes multiple MessagePack-encoded objects, and returns decoded objects as a generator. See also `decodeMultiStream()`, which is an asynchronous variant of this function.
158158

159-
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.
159+
This function is not recommended to decode a MessagePack binary via I/O stream including sockets because it's synchronous. Instead, `decodeMultiStream()` decodes a binary stream asynchronously, typically spending less CPU and memory.
160160

161161
for example:
162162

@@ -172,7 +172,9 @@ for (const object of decodeMulti(encoded)) {
172172

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

175-
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()`.
175+
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`.
176+
177+
This function works asynchronously, and might CPU resources more efficiently compared with synchronous `decode()`, because it doesn't wait for the completion of downloading.
176178

177179
`DecodeAsyncOptions` is the same as `DecodeOptions` for `decode()`.
178180

@@ -231,7 +233,7 @@ This function is available since v2.4.0; previously it was called as `decodeStre
231233

232234
### Reusing Encoder and Decoder instances
233235

234-
`Encoder` and `Decoder` classes is provided for better performance:
236+
`Encoder` and `Decoder` classes is provided to have better performance by reusing instances:
235237

236238
```typescript
237239
import { deepStrictEqual } from "assert";
@@ -253,7 +255,7 @@ and data structure.
253255

254256
To handle [MessagePack Extension Types](https://github.com/msgpack/msgpack/blob/master/spec.md#extension-types), this library provides `ExtensionCodec` class.
255257

256-
Here is an example to setup custom extension types that handles `Map` and `Set` classes in TypeScript:
258+
This is an example to setup custom extension types that handles `Map` and `Set` classes in TypeScript:
257259

258260
```typescript
259261
import { encode, decode, ExtensionCodec } from "@msgpack/msgpack";
@@ -302,7 +304,7 @@ Not that extension types for custom objects must be `[0, 127]`, while `[-1, -128
302304

303305
#### ExtensionCodec context
304306

305-
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:
307+
When you use an extension codec, it might be necessary to have encoding/decoding state to keep track of which objects got encoded/re-created. To do this, pass a `context` to the `EncodeOptions` and `DecodeOptions`:
306308

307309
```typescript
308310
import { encode, decode, ExtensionCodec } from "@msgpack/msgpack";
@@ -423,7 +425,7 @@ const decoded = decode(encoded, { extensionCodec });
423425
deepStrictEqual(decoded, instant);
424426
```
425427

426-
This will be default once the temporal module is standardizied, which is not a near-future, though.
428+
This will become default in this library with major-version increment, if the temporal module is standardized.
427429

428430
## Decoding a Blob
429431

@@ -452,7 +454,7 @@ This library is compatible with the "August 2017" revision of MessagePack specif
452454
* [x] extension types, added at August 2013
453455
* [x] timestamp ext type, added at August 2017
454456

455-
The livinng specification is here:
457+
The living specification is here:
456458

457459
https://github.com/msgpack/msgpack
458460

0 commit comments

Comments
 (0)