byte-encodings
Version:
Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.
121 lines (116 loc) • 3.79 kB
TypeScript
import { U as Uint8Array_ } from './_types-UoFpvNJp.js';
/**
* Calculate the output size needed to encode a given input size for
* {@linkcode encodeIntoHex}.
*
* @param originalSize The size of the input buffer.
* @returns The size of the output buffer.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import { calcSizeHex } from "@std/encoding/unstable-hex";
*
* assertEquals(calcSizeHex(1), 2);
* ```
*/
declare function calcSizeHex(originalSize: number): number;
/**
* Functions to encode and decode to and from hexadecimal strings.
*
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeHex, type Uint8Array_ } from "@std/encoding/unstable-hex";
*
* assertEquals(encodeHex("Hello World"), "48656c6c6f20576f726c64");
* assertEquals(
* encodeHex(new TextEncoder().encode("Hello World") as Uint8Array_),
* "48656c6c6f20576f726c64",
* );
* ```
*
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-8}
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @module
*/
/**
* `encodeHex` takes an input source and encodes it into a hexadecimal string.
* If a {@linkcode Uint8Array<ArrayBuffer>} or {@linkcode ArrayBuffer} is
* provided, the underlying source will be detached and reused for the encoding.
* If you need the input source after providing it to this function, call
* `.slice()` to pass in a copy.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param input The input source to encode.
* @returns The hexadecimal string representation of the input.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeHex, type Uint8Array_ } from "@std/encoding/unstable-hex";
*
* assertEquals(encodeHex("Hello World"), "48656c6c6f20576f726c64");
* assertEquals(
* encodeHex(new TextEncoder().encode("Hello World") as Uint8Array_),
* "48656c6c6f20576f726c64",
* );
* ```
*/
declare function encodeHex(input: string | Uint8Array_ | ArrayBuffer): string;
/**
* `encodeIntoHex` takes an input source and encodes it as hex into the
* output buffer.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param input the source to encode.
* @param output the buffer to write the encoded source to.
* @returns the number of bytes written to the buffer.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import {
* calcSizeHex,
* encodeHex,
* encodeIntoHex,
* } from "@std/encoding/unstable-hex";
*
* const prefix = "data:url/fake,";
* const input = await Deno.readFile("./deno.lock");
* const output = new Uint8Array(prefix.length + calcSizeHex(input.length));
*
* const o = new TextEncoder().encodeInto(prefix, output).written;
* encodeIntoHex(input, output.subarray(o));
* assertEquals(
* new TextDecoder().decode(output),
* "data:url/fake," +
* encodeHex(await Deno.readFile("./deno.lock")),
* );
* ```
*/
declare function encodeIntoHex(input: string | Uint8Array_ | ArrayBuffer, output: Uint8Array_): number;
/**
* `decodeHex` takes an input source and decodes it into a
* {@linkcode Uint8Array<ArrayBuffer>} using the specified format.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param input The input source to decode.
* @returns The decoded {@linkcode Uint8Array<ArrayBuffer>}.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import { decodeHex } from "@std/encoding/unstable-hex";
*
* assertEquals(
* decodeHex("48656c6c6f20576f726c64"),
* new TextEncoder().encode("Hello World"),
* );
* ```
*/
declare function decodeHex(input: string | Uint8Array_): Uint8Array_;
export { Uint8Array_, calcSizeHex, decodeHex, encodeHex, encodeIntoHex };