Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(msgpack): complete documentation #5029

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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