UNPKG

cbor-edn

Version:

Parse CBOR Extended Diagnostic Notation as defined by [draft-ietf-cbor-edn-literals-16](https://www.ietf.org/archive/id/draft-ietf-cbor-edn-literals-16.html) and some CBOR working group discussions.

83 lines (82 loc) 3.12 kB
import { ByteTree } from './byteTree.js'; import type { EDNoptions } from './index.js'; export declare const EDN_EMBEDDED_RANGES: unique symbol; export interface StringChunk { mt: number; str: ByteTree | Uint8Array; spec?: string; prefix?: Uint8Array | string; v?: 4 | 6; } export type ChunkOrEllipsis = StringChunk | ByteTree; export type ChunkTree = (ChunkOrEllipsis | ChunkTree)[]; /** * Capture the rules about concatenated bstr's, tstr's, ellipsis, and * app-strings. * * @param chunks The chunks to be combined. * @param opts Options for UTF8-checking. * @returns Corresponding bytes. * @throws On invalid combinations. */ export declare function combineStrings(chunks: ChunkTree, opts?: EDNoptions): ByteTree; /** * Convert string to pre-encoded Uint8Array. * * @param prefix DT or dt. * @param dt ISO date string. * @returns Obj ready for processing with combineStrings. */ export declare function encodeDate(prefix: string, dt: unknown): Uint8Array | StringChunk; /** * Coalesce all valid bytes together into chunks, and all adjacent ellipses * into a single ellipsis. * * @param _prefix Ignored. * @param chunks Chunks. * @returns Coalesced array. */ export declare function encodeHex(_prefix: string, chunks: unknown): Uint8Array | (StringChunk | ByteTree)[]; export interface IPbytes { bytes: Uint8Array; v: 4 | 6; } /** * Given an IPv4 or IPv6 address, possible with a mask, trim the address * if needed. * * @param prefix IP or ip. * @param str Array of [IPbytes, number]. * @returns Chunk with either the address or [mask, address] pre-encoded, * as well as v set. */ export declare function encodeIP(prefix: string, str: unknown): StringChunk; /** * Convert the parsed version of an IPv6 address into a single buffer. * * @param bytes Array of '::', bytes as Uint8Array, or an IPv6 address as * IPbytes. * @returns IPbytes with v:6. */ export declare function encodeIPv6(bytes: (string | Uint8Array | IPbytes)[]): IPbytes; export type ParsedAppStrFunc = (prefix: string, parsed: unknown) => Uint8Array | StringChunk | ChunkTree | (StringChunk | ByteTree)[]; export type PossibleResults = [rule: string, ParsedAppStrFunc?] | [rule: null, StringChunk | ChunkTree | Uint8Array]; export type AppStrFunc = (prefix: string, str: string) => PossibleResults; /** * Register an app-string decoder. * * @param prefix The string before the first squote. * @param fun Function to process the string. */ export declare function registerAppString(prefix: string, fun: AppStrFunc | null | undefined): void; /** * Two-step processing for app-string plugins. If the first step returns null * for the first item in the array, decoding happens all in the first step. * Otherwise, return `[grammarRuleName, callback]`. The named grammar * rule will be called, and the results passed to `callback(prefix, results)`. * * @param prefix Results of app_prefix. * @param str The matched sqstr, with squotes unescaped. * @returns One of the possible approaches. */ export declare function parseAppString(prefix: string, str: string): PossibleResults;