byte-encodings
Version:
Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.
100 lines (96 loc) • 2.89 kB
TypeScript
import { U as Uint8Array_ } from './_types-UoFpvNJp.js';
/**
* TransformStream classes to encode and decode to and from hexadecimal data in a streaming manner.
*
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeHex } from "@std/encoding/unstable-hex";
* import { HexEncoderStream } from "@std/encoding/unstable-hex-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new HexEncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeHex(await Deno.readFile("./deno.lock")),
* );
* ```
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @module
*/
/**
* Transforms a {@linkcode Uint8Array<ArrayBuffer>} stream into a hexadecimal stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the hexadecimal stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import { encodeHex } from "@std/encoding/unstable-hex";
* import { HexEncoderStream } from "@std/encoding/unstable-hex-stream";
* import { toText } from "@std/streams";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new HexEncoderStream({ output: "string" }));
*
* assertEquals(
* await toText(readable),
* encodeHex(await Deno.readFile("./deno.lock")),
* );
* ```
*/
declare class HexEncoderStream<T extends "string" | "bytes"> extends TransformStream<Uint8Array_, T extends "bytes" ? Uint8Array_ : string> {
/**
* Constructs a new instance.
*
* @param options The options for the hexadecimal stream.
*/
constructor(options?: {
output?: T;
});
}
/**
* Transforms a hexadecimal stream into a {@link Uint8Array<ArrayBuffer>} stream.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @typeParam T The type of the hexadecimal stream.
*
* @example Basic Usage
* ```ts
* import { assertEquals } from "@std/assert";
* import {
* HexDecoderStream,
* HexEncoderStream,
* } from "@std/encoding/unstable-hex-stream";
* import { toBytes } from "@std/streams/unstable-to-bytes";
*
* const readable = (await Deno.open("./deno.lock"))
* .readable
* .pipeThrough(new HexEncoderStream({ output: "bytes" }))
* .pipeThrough(new HexDecoderStream({ input: "bytes" }));
*
* assertEquals(
* await toBytes(readable),
* await Deno.readFile("./deno.lock"),
* );
* ```
*/
declare class HexDecoderStream<T extends "string" | "bytes"> extends TransformStream<T extends "bytes" ? Uint8Array_ : string, Uint8Array_> {
/**
* Constructs a new instance.
*
* @param options The options of the hexadecimal stream.
*/
constructor(options?: {
input?: T;
});
}
export { HexDecoderStream, HexEncoderStream, Uint8Array_ };