diff --git a/_tools/check_docs.ts b/_tools/check_docs.ts index dc9051f1c2c9..e5eb4e1ff032 100644 --- a/_tools/check_docs.ts +++ b/_tools/check_docs.ts @@ -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", diff --git a/msgpack/decode.ts b/msgpack/decode.ts index f7f167ce6dc7..567f258041fd 100644 --- a/msgpack/decode.ts +++ b/msgpack/decode.ts @@ -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"); } diff --git a/msgpack/encode.ts b/msgpack/encode.ts index 53585aac23e1..017ad3a6c7ca 100644 --- a/msgpack/encode.ts +++ b/msgpack/encode.ts @@ -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; } @@ -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", @@ -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. diff --git a/msgpack/mod.ts b/msgpack/mod.ts index dcdb9b11c794..e11a3c2ef07e 100644 --- a/msgpack/mod.ts +++ b/msgpack/mod.ts @@ -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", @@ -23,7 +23,7 @@ * }; * * const encoded = encode(obj); - * console.log(encoded); // Uint8Array(42) [...] + * assertEquals(encoded.length, 42); * * const decoded = decode(encoded); * assertEquals(decoded, obj);