Skip to content

Commit

Permalink
docs(msgpack): complete documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua committed Jun 11, 2024
1 parent 1f359c4 commit daaf2b2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const ENTRY_POINTS = [
"../internal/mod.ts",
"../jsonc/mod.ts",
"../media_types/mod.ts",
"../msgpack/mod.ts",
"../net/mod.ts",
"../path/mod.ts",
"../path/posix/mod.ts",
Expand Down
23 changes: 12 additions & 11 deletions msgpack/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@
import type { ValueType } from "./encode.ts";

/**
* Decode a value from the MessagePack binary format.
* Decode a value from the {@link https://msgpack.org/ | MessagePack} binary format.
*
* If the input is not in valid message pack format, an error will be thrown.
*
* @example Decode a value from the MessagePack binary format
* @example Usage
* ```ts
* import { decode } from "@std/msgpack/decode";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const encoded = new Uint8Array([1, 2, 3])
* const encoded = new Uint8Array([163, 72, 105, 33]);
*
* console.log(decode(encoded))
* assertEquals(decode(encoded), "Hi!");
* ```
*
* @param uint8 Uint8Array containing the MessagePack binary data.
* @param data MessagePack binary data.
* @returns Decoded value from the MessagePack binary data.
*/
export function decode(uint8: Uint8Array): ValueType {
export function decode(data: Uint8Array): ValueType {
const pointer = { consumed: 0 };
const dataView = new DataView(
uint8.buffer,
uint8.byteOffset,
uint8.byteLength,
data.buffer,
data.byteOffset,
data.byteLength,
);
const value = decodeSlice(uint8, dataView, pointer);
const value = decodeSlice(data, dataView, pointer);

if (pointer.consumed < uint8.length) {
if (pointer.consumed < data.length) {
throw new EvalError("Messagepack decode did not consume whole array");
}

Expand Down
10 changes: 7 additions & 3 deletions msgpack/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type ValueType =
* Value map that can be encoded to MessagePack.
*/
export interface ValueMap {
/** Value map entry */
[index: string | number]: ValueType;
}

Expand All @@ -37,11 +38,12 @@ const SIXTY_FOUR_BITS = 18446744073709551616n;
const encoder = new TextEncoder();

/**
* Encode a value to MessagePack binary format.
* Encode a value to {@link https://msgpack.org/ | MessagePack} binary format.
*
* @example Encode a value to MessagePack binary format
* @example Usage
* ```ts
* import { encode } from "@std/msgpack/encode";
* import { assertEquals } from "@std/assert/assert-equals";
*
* const obj = {
* str: "deno",
Expand All @@ -51,7 +53,9 @@ const encoder = new TextEncoder();
* }
* }
*
* console.log(encode(obj))
* const encoded = encode(obj);
*
* assertEquals(encoded.length, 31);
* ```
*
* @param object Value to encode to MessagePack binary format.
Expand Down
10 changes: 5 additions & 5 deletions msgpack/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// This module is browser compatible.

/**
* This module provides functions to encode and decode MessagePack.
*
* MessagePack is an efficient binary serialization format that is language
* agnostic. It is like JSON, but generally produces much smaller payloads.
* [Learn more about MessagePack](https://msgpack.org/).
*
* This module provides functions to encode and decode MessagePack.
* {@link https://msgpack.org/ | Learn more about MessagePack}.
*
* ```ts
* import { decode, encode } from "@std/msgpack";
* import { assertEquals } from "@std/assert"
* import { assertEquals } from "@std/assert/assert-equals";
*
* const obj = {
* str: "deno",
Expand All @@ -23,7 +23,7 @@
* };
*
* const encoded = encode(obj);
* console.log(encoded); // Uint8Array(42) [...]
* assertEquals(encoded.length, 42);
*
* const decoded = decode(encoded);
* assertEquals(decoded, obj);
Expand Down

0 comments on commit daaf2b2

Please sign in to comment.