@cbortech/cbor 0.23.0 → 0.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.ja.md +6 -3
  2. package/README.md +7 -4
  3. package/dist/ast/CborArray.d.ts +17 -0
  4. package/dist/ast/CborBignum.d.ts +36 -0
  5. package/dist/ast/CborByteString.d.ts +20 -0
  6. package/dist/ast/CborEllipsis.d.ts +11 -0
  7. package/dist/ast/CborEmbeddedCBOR.d.ts +22 -0
  8. package/dist/ast/CborFloat.d.ts +27 -0
  9. package/dist/ast/CborIndefiniteByteString.d.ts +13 -0
  10. package/dist/ast/CborIndefiniteTextString.d.ts +13 -0
  11. package/dist/ast/CborItem.d.ts +91 -0
  12. package/dist/ast/CborMap.d.ts +17 -0
  13. package/dist/ast/CborNint.d.ts +27 -0
  14. package/dist/ast/CborSimple.d.ts +22 -0
  15. package/dist/ast/CborTag.d.ts +16 -0
  16. package/dist/ast/CborTextString.d.ts +15 -0
  17. package/dist/ast/CborUint.d.ts +14 -0
  18. package/dist/ast/CborUnresolvedAppExt.d.ts +16 -0
  19. package/dist/ast/index.cjs +1 -0
  20. package/dist/ast/index.d.ts +15 -0
  21. package/dist/ast/index.js +2 -0
  22. package/dist/cbor/constants.d.ts +14 -0
  23. package/dist/cbor/decoder.d.ts +11 -0
  24. package/dist/cbor/encode.d.ts +35 -0
  25. package/dist/cbor/encoder.d.ts +7 -0
  26. package/dist/cbor.d.ts +129 -0
  27. package/dist/edn/parser.d.ts +7 -0
  28. package/dist/edn/serialize-utils.d.ts +48 -0
  29. package/dist/edn/serializer.d.ts +7 -0
  30. package/dist/edn/tokenizer.d.ts +176 -0
  31. package/dist/extensions/bignum.d.ts +3 -0
  32. package/dist/extensions/builtins.d.ts +2 -0
  33. package/dist/extensions/cbordata.d.ts +4 -0
  34. package/dist/extensions/cri.d.ts +32 -0
  35. package/dist/extensions/dt.d.ts +109 -0
  36. package/dist/extensions/hash.d.ts +17 -0
  37. package/dist/extensions/ip.d.ts +41 -0
  38. package/dist/extensions/types.d.ts +70 -0
  39. package/dist/index.cjs +2 -31
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.ts +8 -695
  42. package/dist/index.js +30 -3913
  43. package/dist/index.js.map +1 -1
  44. package/dist/js/fromJS.d.ts +25 -0
  45. package/dist/js/toJS.d.ts +6 -0
  46. package/dist/mapEntries-C73nWM8o.cjs +31 -0
  47. package/dist/mapEntries-C73nWM8o.cjs.map +1 -0
  48. package/dist/mapEntries-CSjvgq1X.js +3887 -0
  49. package/dist/mapEntries-CSjvgq1X.js.map +1 -0
  50. package/dist/mapEntries.d.ts +3 -0
  51. package/dist/simple.d.ts +25 -0
  52. package/dist/tag.d.ts +45 -0
  53. package/dist/types.d.ts +352 -0
  54. package/dist/utils/float16.d.ts +38 -0
  55. package/dist/utils/hexfloat.d.ts +27 -0
  56. package/dist/utils/ip.d.ts +9 -0
  57. package/package.json +12 -1
package/dist/cbor.d.ts ADDED
@@ -0,0 +1,129 @@
1
+ import { CborItem } from './ast/CborItem';
2
+ import { CBOROptions, FromCBOROptions, FromHexDumpOptions, FromEDNOptions, FromJSOptions, ToCBOROptions, ToEDNOptions, ToJSOptions, CBOR_OMIT } from './types';
3
+ import { dt_as_Date as _dt_as_Date } from './extensions/dt';
4
+ import { MapEntries as _MapEntries } from './mapEntries';
5
+ import { Simple as _Simple } from './simple';
6
+ import { CBOR_TAG, Tag as _Tag } from './tag';
7
+ /**
8
+ * Main facade class.
9
+ *
10
+ * Provides factory methods for constructing AST nodes from the three
11
+ * supported input formats, and shortcut methods that mirror the
12
+ * `JSON.parse` / `JSON.stringify` API.
13
+ *
14
+ * @example
15
+ * // CBOR binary → AST → CBOR binary
16
+ * const ast = CBOR.fromCBOR(bytes);
17
+ * const reencoded = ast.toCBOR();
18
+ *
19
+ * @example
20
+ * // JS value → CBOR binary (shortcut)
21
+ * const bytes = CBOR.encode({ hello: 'world' });
22
+ *
23
+ * @example
24
+ * // CBOR binary → JS value (shortcut)
25
+ * const value = CBOR.decode(bytes);
26
+ */
27
+ export declare class CBOR {
28
+ #private;
29
+ /**
30
+ * Sentinel returned from a replacer or reviver to omit the key/element from
31
+ * the output. Use this instead of `undefined` when `undefinedOmits` is
32
+ * `false` (the default) and you need to drop a specific entry.
33
+ */
34
+ static readonly OMIT: typeof CBOR_OMIT;
35
+ /** Unique symbol used to attach a CBOR tag number to a JS value. */
36
+ static readonly TAG: typeof CBOR_TAG;
37
+ /** Namespace for CBOR tag annotation utilities. */
38
+ static readonly Tag: typeof _Tag;
39
+ /** Wrapper for CBOR simple values other than false/true/null/undefined. */
40
+ static readonly Simple: typeof _Simple;
41
+ /** Array subclass used to preserve CBOR map entries, including duplicates. */
42
+ static readonly MapEntries: typeof _MapEntries;
43
+ /** Extension that maps CBOR-EDN dt/DT values to JavaScript Date objects. */
44
+ static readonly dt_as_Date: typeof _dt_as_Date;
45
+ /**
46
+ * Create a reusable instance with default options applied to every method call.
47
+ * Per-call options always override these defaults.
48
+ *
49
+ * @example
50
+ * const cbor = new CBOR({ extensions: [CBOR.dt_as_Date] });
51
+ * const obj = cbor.parse('{ "dt": DT\'2024-01-01T00:00:00Z\' }');
52
+ * const text = cbor.stringify(obj);
53
+ */
54
+ constructor(defaults?: CBOROptions);
55
+ fromCBOR(input: ArrayBufferView | ArrayBufferLike, options?: FromCBOROptions): CborItem;
56
+ fromEDN(text: string, options?: FromEDNOptions): CborItem;
57
+ fromJS(value: unknown, options?: FromJSOptions): CborItem;
58
+ fromHexDump(text: string, options?: FromHexDumpOptions): CborItem;
59
+ decode(input: ArrayBufferView | ArrayBufferLike, options?: FromCBOROptions & ToJSOptions): unknown;
60
+ encode(value: unknown, options?: FromJSOptions & ToCBOROptions): Uint8Array;
61
+ cborToCborEdn(input: ArrayBufferView | ArrayBufferLike, options?: FromCBOROptions & ToEDNOptions): string;
62
+ cborEdnToCbor(text: string, options?: FromEDNOptions & ToCBOROptions): Uint8Array;
63
+ parse(text: string): unknown;
64
+ parse(text: string, reviver: (this: unknown, key: unknown, value: unknown) => unknown): unknown;
65
+ parse(text: string, options: FromEDNOptions & ToJSOptions): unknown;
66
+ stringify(value: unknown): string;
67
+ stringify(value: unknown, replacer: ((this: unknown, key: unknown, value: unknown) => unknown) | (string | number)[] | null, space?: string | number): string;
68
+ stringify(value: unknown, options: FromJSOptions & ToEDNOptions): string;
69
+ format(text: string, options?: FromEDNOptions & ToEDNOptions): string;
70
+ /** Decode CBOR binary data into an AST node. */
71
+ static fromCBOR(input: ArrayBufferView | ArrayBufferLike, options?: FromCBOROptions): CborItem;
72
+ /** Parse a CBOR-EDN text string into an AST node. */
73
+ static fromEDN(text: string, options?: FromEDNOptions): CborItem;
74
+ /** Convert a JavaScript value into an AST node. */
75
+ static fromJS(value: unknown, options?: FromJSOptions): CborItem;
76
+ /**
77
+ * Parse an annotated hex dump (as produced by {@link CborItem#toHexDump})
78
+ * into an AST node.
79
+ *
80
+ * Each line is expected to have the form:
81
+ * `[whitespace] HH [HH …] -- comment`
82
+ * `[whitespace] HH [HH …] # comment`
83
+ * `[whitespace] HH [HH …] // comment`
84
+ * Block comments may also be written as `/ comment /` or `/* comment *\/`.
85
+ * Lines with no hex content before the comment marker are ignored.
86
+ */
87
+ static fromHexDump(text: string, options?: FromHexDumpOptions): CborItem;
88
+ /** Decode CBOR binary data directly to a JavaScript value. */
89
+ static decode(input: ArrayBufferView | ArrayBufferLike, options?: FromCBOROptions & ToJSOptions): unknown;
90
+ /** Encode a JavaScript value directly to CBOR binary data. */
91
+ static encode(value: unknown, options?: FromJSOptions & ToCBOROptions): Uint8Array;
92
+ /** Convert CBOR binary data directly to a CBOR-EDN text string. */
93
+ static cborToCborEdn(input: ArrayBufferView | ArrayBufferLike, options?: FromCBOROptions & ToEDNOptions): string;
94
+ /** Convert a CBOR-EDN text string directly to CBOR binary data. */
95
+ static cborEdnToCbor(text: string, options?: FromEDNOptions & ToCBOROptions): Uint8Array;
96
+ /**
97
+ * Parse a CBOR-EDN text string directly to a JavaScript value.
98
+ *
99
+ * Accepts either a JSON-compatible `reviver` function as the second argument,
100
+ * or a plain options object (existing API).
101
+ *
102
+ * When a `reviver` is supplied it is applied bottom-up after the EDN text has
103
+ * been parsed and converted to a JS value, matching the semantics of
104
+ * `JSON.parse(text, reviver)`.
105
+ *
106
+ * Note: CBOR-specific value types such as `bigint` are passed to the reviver
107
+ * as-is; the reviver is responsible for handling them.
108
+ */
109
+ static parse(text: string): unknown;
110
+ static parse(text: string, reviver: (this: unknown, key: unknown, value: unknown) => unknown): unknown;
111
+ static parse(text: string, options: FromEDNOptions & ToJSOptions): unknown;
112
+ /**
113
+ * Serialize a JavaScript value directly to a CBOR-EDN text string.
114
+ *
115
+ * Accepts either JSON-compatible `replacer` + `space` arguments, or a plain
116
+ * options object (existing API).
117
+ *
118
+ * - `replacer` may be a function (transforms each key/value before encoding)
119
+ * or an array of strings/numbers (allowlist of object keys to include).
120
+ * Pass `null` to skip filtering.
121
+ * - `space` controls indentation, mapping to `ToEDNOptions.indent`.
122
+ * Numbers are clamped to `[0, 10]`; strings are truncated to 10 characters.
123
+ */
124
+ static stringify(value: unknown): string;
125
+ static stringify(value: unknown, replacer: ((this: unknown, key: unknown, value: unknown) => unknown) | (string | number)[] | null, space?: string | number): string;
126
+ static stringify(value: unknown, options: FromJSOptions & ToEDNOptions): string;
127
+ /** Normalize a CBOR-EDN text string by parsing and re-serializing it. */
128
+ static format(text: string, options?: FromEDNOptions & ToEDNOptions): string;
129
+ }
@@ -0,0 +1,7 @@
1
+ import { CborItem } from '../ast/CborItem';
2
+ import { FromEDNOptions } from '../types';
3
+ /**
4
+ * Parse a CBOR-EDN diagnostic notation string into a CborItem AST node.
5
+ * Throws SyntaxError on invalid input.
6
+ */
7
+ export declare function parseEDN(text: string, options?: FromEDNOptions): CborItem;
@@ -0,0 +1,48 @@
1
+ import { CborComments, ToEDNOptions } from '../types';
2
+ /** Resolve indent option to a string, or null for single-line output. */
3
+ export declare function resolveIndent(options: ToEDNOptions | undefined): string | null;
4
+ /** Build the indent prefix for a given depth. */
5
+ export declare function indentOf(indentStr: string, depth: number): string;
6
+ export interface Commented {
7
+ comments?: CborComments;
8
+ }
9
+ export declare function hasPreservedComments(item: Commented): boolean;
10
+ export declare function hasContainerLayoutComments(item: Commented): boolean;
11
+ export declare function formatLeadingComments(item: Commented, indent: string): string[];
12
+ export declare function formatTrailingComments(item: Commented): string;
13
+ export declare function formatDanglingComments(item: Commented, indent: string): string[];
14
+ /**
15
+ * Resolve separator options into concrete strings.
16
+ *
17
+ * @param compact - When `true` (no `indent` option), omit spaces around
18
+ * separators to produce compact single-line output (like `JSON.stringify`).
19
+ *
20
+ * @returns
21
+ * - `inlineSep` – between items on a single line
22
+ * - `multilineSep` – appended after each non-last line in multi-line mode
23
+ * - `trailSep` – appended after the last item (empty string or `,`)
24
+ * - `colSep` – between map key and value (`': '` or `':'`)
25
+ */
26
+ export declare function resolveSeparators(options: ToEDNOptions | undefined, compact?: boolean): {
27
+ inlineSep: string;
28
+ multilineSep: string;
29
+ trailSep: string;
30
+ colSep: string;
31
+ };
32
+ export declare function serializeBytes(bytes: Uint8Array, encoding?: 'hex' | 'base64' | 'base64url' | 'base32' | 'base32hex', sqstr?: 'printable-string' | 'string' | 'none'): string;
33
+ /**
34
+ * Produce a single-quoted EDN app-string content `'...'` from a string value.
35
+ * Exported for use by app-extension `_toEDN` implementations.
36
+ */
37
+ export declare function escapeAppString(s: string): string;
38
+ /**
39
+ * Produce an EDN double-quoted string literal `"..."` from a string value.
40
+ */
41
+ export declare function escapeString(s: string): string;
42
+ /** Produce the numeric string for a float value (with decimal point if needed). */
43
+ export declare function floatValueToString(value: number): string;
44
+ /**
45
+ * EDN encoding-indicator suffix for a float precision.
46
+ * Returns '' when the auto-selected precision matches (no suffix needed).
47
+ */
48
+ export declare function floatSuffix(_value: number, precision: 'half' | 'single' | 'double' | undefined, autoSelected: 'half' | 'single' | 'double'): string;
@@ -0,0 +1,7 @@
1
+ import { CborItem } from '../ast/CborItem';
2
+ import { ToEDNOptions } from '../types';
3
+ /**
4
+ * Serialize a CborItem AST node to CBOR-EDN diagnostic notation.
5
+ * Equivalent to calling value.toEDN(options) directly.
6
+ */
7
+ export declare function toEDN(value: CborItem, options?: ToEDNOptions): string;
@@ -0,0 +1,176 @@
1
+ /**
2
+ * EDN lexer (internal).
3
+ *
4
+ * Used by parser.ts for parsing and by CborTextString serialization to collect
5
+ * source offsets after parseEDN() has already validated embedded CBOR-EDN.
6
+ */
7
+ export type TokenType = 'INTEGER' | 'FLOAT' | 'TSTR' | 'SQSTR' | 'RAWSTRING' | 'BYTES_HEX' | 'BYTES_HEX_ELIDED' | 'BYTES_B64' | 'BYTES_B32' | 'BYTES_H32' | 'APP_STRING' | 'APP_SEQUENCE' | 'EMPTY_INDEF_BYTES' | 'EMPTY_INDEF_TEXT' | 'TRUE' | 'FALSE' | 'NULL' | 'UNDEFINED' | 'SIMPLE' | 'LBRACKET' | 'RBRACKET' | 'LBRACE' | 'RBRACE' | 'LPAREN' | 'RPAREN' | 'COLON' | 'COMMA' | 'PLUS' | 'UNDERSCORE' | 'ENCODING_INDICATOR' | 'LT_LT' | 'GT_GT' | 'ELLIPSIS' | 'EOF';
8
+ export interface Token {
9
+ type: TokenType;
10
+ /** Processed value: decoded string content, raw number text, raw byte content, etc. */
11
+ value: string;
12
+ /** Original source text for this token. */
13
+ raw: string;
14
+ line: number;
15
+ col: number;
16
+ /** Character offset of the first character of this token in the source input. */
17
+ offset: number;
18
+ /** Character offset just past the last character of this token in the source input. */
19
+ endOffset: number;
20
+ /** Only set when type === 'APP_STRING': the extension prefix (e.g. 'dt', 'DT'). */
21
+ appPrefix?: string;
22
+ }
23
+ export interface TokenizerOptions {
24
+ /** Character offset at which tokenization starts. */
25
+ offset?: number;
26
+ }
27
+ export interface EdnComment {
28
+ kind: 'line' | 'block';
29
+ marker: '#' | '//' | '/*' | '/';
30
+ text: string;
31
+ start: number;
32
+ end: number;
33
+ line: number;
34
+ col: number;
35
+ }
36
+ export declare class Tokenizer {
37
+ private readonly input;
38
+ private pos;
39
+ private line;
40
+ private col;
41
+ private _peeked;
42
+ private _lastConsumedEndOffset;
43
+ /** Comments encountered while scanning, appended in source order. */
44
+ readonly comments: EdnComment[];
45
+ constructor(input: string, options?: TokenizerOptions);
46
+ peek(): Token;
47
+ consume(): Token;
48
+ /** Character offset just past the last character of the most recently consumed token. */
49
+ get lastEndOffset(): number;
50
+ private _ch;
51
+ private _eof;
52
+ private _advance;
53
+ private _fail;
54
+ private _skipWS;
55
+ /**
56
+ * Skip a comment in a quoted byte string literal (h'', b32'', h32'').
57
+ * Returns true if a comment was consumed, false if the current char is not a
58
+ * comment start. `quote` is the closing delimiter character.
59
+ *
60
+ * Supports / ... /, /* *\/, //, and # comment forms (§2.2).
61
+ */
62
+ private _skipByteStringComment;
63
+ /**
64
+ * Skip a comment in a raw byte string (h``, b32``, h32``).
65
+ * Called with `i` pointing at the comment-start character.
66
+ * Returns the index after the comment, or -1 if no comment was found.
67
+ *
68
+ * Supports / ... /, /* *\/, //, and # comment forms (§2.2).
69
+ * `context` is used in unterminated-comment error messages.
70
+ */
71
+ private _skipRawComment;
72
+ /** Skip content until a closing `/` (CBOR-EDN block comment). */
73
+ private _skipBlockCommentSlash;
74
+ /** Skip content until a closing `*\/` (JSONC block comment). */
75
+ private _skipBlockCommentStar;
76
+ /**
77
+ * Read content between `quote` delimiters, processing escape sequences.
78
+ *
79
+ * Strict spec compliance:
80
+ * - Literal LF (U+000A) is allowed; all other C0 controls and U+007F are rejected.
81
+ * - Literal CR (U+000D) is silently stripped (source-level CRLF normalisation).
82
+ * - Only spec-defined escape sequences are accepted; `\q` etc. throw SyntaxError.
83
+ * - `\/` is valid in both single- and double-quoted strings.
84
+ * - `\\` (backslash) is always a valid escape.
85
+ * - `\uXXXX` for a high surrogate must be immediately followed by `\uXXXX` for
86
+ * the corresponding low surrogate; lone surrogates are rejected.
87
+ * - `\u{N}` … `\u{10FFFF}` extended syntax is supported; surrogates are rejected.
88
+ * - In single-quoted strings, `\u` escapes to printable ASCII (U+0020–U+007E)
89
+ * are forbidden (hexchar-s restriction, draft §2.5.2 / §5.1).
90
+ */
91
+ private _readStringContent;
92
+ /**
93
+ * Parse a Unicode escape immediately after `\u` has been consumed.
94
+ *
95
+ * @param quote - The enclosing string delimiter (`"` or `'`).
96
+ *
97
+ * Handles two forms:
98
+ * - `\u{N}` … `\u{10FFFF}`: direct Unicode scalar value (surrogates rejected)
99
+ * - `\uXXXX`: exactly four hex digits; a high surrogate must be followed by
100
+ * `\uXXXX` for the matching low surrogate to form a valid surrogate pair,
101
+ * which is then decoded into the corresponding non-BMP code point.
102
+ *
103
+ * In single-quoted strings (`quote === "'"`), `\u` escapes that resolve to
104
+ * printable ASCII (U+0020–U+007E) are rejected per draft §2.5.2 / §5.1
105
+ * hexchar-s, except for backslash (U+005C) and single-quote (U+0027), which
106
+ * require escaping and cannot be written literally.
107
+ */
108
+ private _readUnicodeEscape;
109
+ /**
110
+ * Read raw text-string content between N-backtick delimiters (§2.5.3).
111
+ *
112
+ * - The opening delimiter is the maximal run of consecutive backticks (N ≥ 1).
113
+ * - A single leading newline (LF or CRLF) immediately after the opening is stripped.
114
+ * - No escape sequences are processed — content is taken verbatim.
115
+ * - Literal CR is stripped for source-level CRLF normalisation.
116
+ * - The closing delimiter is the first run of M ≥ N backticks; any excess
117
+ * M-N backticks are appended to the content before closing.
118
+ */
119
+ private _readRawStringContent;
120
+ /**
121
+ * Post-process raw hex content from a `h``…``\` raw string (§5.3.3).
122
+ *
123
+ * Skips:
124
+ * - lblank whitespace (LF, SP; also CR for source-level normalisation)
125
+ * - `/ … /` block comments
126
+ * - `# …` line comments (up to but not including LF)
127
+ * Detects `...` ellipsis sequences.
128
+ *
129
+ * A trailing `# comment` immediately before the closing delimiter is allowed
130
+ * per §5.3.3 `r-app-string-h`.
131
+ *
132
+ * Returns { value: hex-string-with-ellipsis-markers, elided: boolean }
133
+ */
134
+ private _processRawHexContent;
135
+ /**
136
+ * Post-process raw base64 content from a `b64``…``\` raw string (§5.3.4).
137
+ *
138
+ * Skips:
139
+ * - lblank whitespace (LF, SP; also CR for source-level normalisation)
140
+ * - `# …` line comments (up to but not including LF)
141
+ *
142
+ * Returns the stripped base64 string.
143
+ */
144
+ private _processRawB64Content;
145
+ /**
146
+ * Post-process raw base32/base32hex content from `b32``…``\` / `h32``…``\` raw strings.
147
+ *
148
+ * Supports all comment forms (§2.2): `/ ... /`, `/*...*\/`, `//`, `#`.
149
+ * HT is still forbidden (rawchars excludes %x09).
150
+ */
151
+ private _processRawB32Content;
152
+ /**
153
+ * Read raw byte-string content between `quote` chars (b64 / b64url).
154
+ *
155
+ * Strips whitespace and skips `# ...` line comments per §2.5.5.
156
+ * `/` is NOT treated as a comment delimiter because it is a valid base64 character.
157
+ */
158
+ private _readByteContent;
159
+ /**
160
+ * Read base32 / base32hex byte-string content (`b32'...'` / `h32'...'`).
161
+ *
162
+ * Supports all comment forms (§2.2): `/ ... /`, `/*...*\/`, `//`, `#`.
163
+ */
164
+ private _readB32Content;
165
+ /**
166
+ * Read hex byte-string content, recognising `...` ellipsis sequences (§4.2).
167
+ *
168
+ * Returns the raw hex string (with `...` markers embedded) and a flag
169
+ * indicating whether any ellipsis was found.
170
+ */
171
+ private _readHexByteContentElisionAware;
172
+ private _readNext;
173
+ private _readNextCore;
174
+ private _readNumber;
175
+ private _readIdent;
176
+ }
@@ -0,0 +1,3 @@
1
+ import { CborExtension } from './types';
2
+ export declare const bignum: CborExtension;
3
+ export default bignum;
@@ -0,0 +1,2 @@
1
+ import { CborExtension } from './types';
2
+ export declare const BUILTIN_EXTENSIONS: CborExtension[];
@@ -0,0 +1,4 @@
1
+ import { CborExtension } from './types';
2
+ export declare const TAG_CBOR_DATA = 24n;
3
+ declare const cbordata: CborExtension;
4
+ export default cbordata;
@@ -0,0 +1,32 @@
1
+ import { ToEDNOptions } from '../types';
2
+ import { CborExtension } from './types';
3
+ import { CborArray } from '../ast/CborArray';
4
+ import { CborTag } from '../ast/CborTag';
5
+ /**
6
+ * CBOR tag number for the tagged CRI variant (draft-ietf-cbor-edn-literals-21 §3.4 / §5.2.5).
7
+ */
8
+ export declare const TAG_CRI = 99n;
9
+ /**
10
+ * Bare CRI array whose toEDN() emits cri'…' notation.
11
+ * Falls back to generic array notation if the content cannot be expressed as a URI.
12
+ */
13
+ export declare class CborCriExt extends CborArray {
14
+ _toEDN(options: ToEDNOptions | undefined, depth: number): string;
15
+ }
16
+ /**
17
+ * tag(99, CRI array) whose toEDN() emits CRI'…' notation.
18
+ * Falls back to generic tag notation if the content cannot be expressed as a URI.
19
+ */
20
+ export declare class CborTaggedCriExt extends CborTag {
21
+ constructor(content: CborArray);
22
+ _toEDN(options: ToEDNOptions | undefined, depth: number): string;
23
+ }
24
+ /**
25
+ * Create the cri/CRI CborExtension (§5.2.5 draft-ietf-cbor-edn-literals-20).
26
+ *
27
+ * - `cri'uri'` → CborCriExt (bare CRI array, no CBOR tag)
28
+ * - `CRI'uri'` → CborTaggedCriExt tag(99, CRI array)
29
+ * - parseTag(99n, …) → CborTaggedCriExt (roundtrip from CBOR binary)
30
+ */
31
+ export declare const cri: CborExtension;
32
+ export default cri;
@@ -0,0 +1,109 @@
1
+ import { ToEDNOptions, ToJSOptions } from '../types';
2
+ import { CborExtension } from './types';
3
+ import { CborUint } from '../ast/CborUint';
4
+ import { CborNint } from '../ast/CborNint';
5
+ import { CborFloat } from '../ast/CborFloat';
6
+ import { CborTag } from '../ast/CborTag';
7
+ import { EncodingWidth } from '../cbor/encode';
8
+ /**
9
+ * Convert epoch seconds to an RFC 3339 string with appropriate precision.
10
+ *
11
+ * - Integer epoch → "…Z" (no fractional part)
12
+ * - Millisecond-precision → "….SSSZ" (3 decimal places, e.g. ".500Z")
13
+ * - Sub-millisecond → "….S…Z" (minimal digits, e.g. ".0001Z")
14
+ *
15
+ * Millisecond precision is used whenever `Math.round(epochSeconds * 1000)`
16
+ * round-trips back to the same float64 value, which is the common case for
17
+ * timestamps stored as integer milliseconds (the JavaScript Date range).
18
+ * Otherwise the shortest decimal representation of the fractional seconds is
19
+ * used so that the float64 value is faithfully represented.
20
+ */
21
+ export declare function epochToRfc3339(epochSeconds: number): string;
22
+ /**
23
+ * Parse an RFC 3339 string and produce the appropriate epoch CborItem subclass.
24
+ * Integer seconds → CborEpochDtExtUint or CborEpochDtExtNint.
25
+ * Fractional seconds → CborEpochDtExtFloat.
26
+ *
27
+ * Fractional seconds are extracted from the string directly (via parseFloat)
28
+ * before passing the remainder to Date.parse, so sub-millisecond precision
29
+ * is preserved rather than being rounded to the nearest millisecond.
30
+ */
31
+ export declare function parseDtAppString(str: string): CborEpochDtExtUint | CborEpochDtExtNint | CborEpochDtExtFloat;
32
+ export declare const PREFIX_DT = "dt";
33
+ export declare const PREFIX_DT_TAGGED = "DT";
34
+ export declare const TAG_EPOCH = 1n;
35
+ /**
36
+ * Unsigned epoch timestamp whose toEDN() emits dt'…' notation.
37
+ * The RFC 3339 string is re-derived from the numeric value on each call.
38
+ */
39
+ export declare class CborEpochDtExtUint extends CborUint {
40
+ constructor(value: number | bigint, options?: {
41
+ encodingWidth?: EncodingWidth;
42
+ });
43
+ _toEDN(options: ToEDNOptions | undefined, _depth: number): string;
44
+ }
45
+ /**
46
+ * Negative epoch timestamp whose toEDN() emits dt'…' notation.
47
+ * The RFC 3339 string is re-derived from the numeric value on each call.
48
+ */
49
+ export declare class CborEpochDtExtNint extends CborNint {
50
+ constructor(value: number | bigint, options?: {
51
+ encodingWidth?: EncodingWidth;
52
+ });
53
+ _toEDN(options: ToEDNOptions | undefined, _depth: number): string;
54
+ }
55
+ /**
56
+ * Float epoch timestamp whose toEDN() emits dt'…' notation.
57
+ * The RFC 3339 string is re-derived from the numeric value on each call.
58
+ */
59
+ export declare class CborEpochDtExtFloat extends CborFloat {
60
+ constructor(value: number, options?: {
61
+ precision?: 'half' | 'single' | 'double';
62
+ });
63
+ _toEDN(options: ToEDNOptions | undefined, _depth: number): string;
64
+ }
65
+ /**
66
+ * CBOR tag(1, epoch) whose toEDN() emits DT'…' notation.
67
+ * The RFC 3339 string is re-derived from the numeric content on each call.
68
+ */
69
+ export declare class CborTaggedEpochDtExt extends CborTag {
70
+ constructor(datetime: string, options?: {
71
+ encodingWidth?: EncodingWidth;
72
+ });
73
+ _toEDN(options: ToEDNOptions | undefined, depth: number): string;
74
+ }
75
+ /**
76
+ * CBOR tag(1, epoch) whose toJS() returns a plain Date object.
77
+ * Use dt_as_Date (or createDtExtension({ jsDate: true })) to produce these nodes.
78
+ */
79
+ export declare class CborTaggedEpochDtAsDateExt extends CborTaggedEpochDtExt {
80
+ constructor(datetime: string, options?: {
81
+ encodingWidth?: EncodingWidth;
82
+ });
83
+ _toJS(_options?: ToJSOptions): Date;
84
+ }
85
+ /**
86
+ * Create a dt/DT CborExtension.
87
+ *
88
+ * - `createDtExtension()` — tagged DT values produce `CborTaggedEpochDtExt`;
89
+ * toJS() returns a number (epoch seconds).
90
+ * - `createDtExtension({ jsDate: true })` — tagged DT values produce
91
+ * `CborTaggedEpochDtAsDateExt`; toJS() returns a `Date` object, and
92
+ * `fromJS(Date)` converts `Date` instances back to tagged epoch values.
93
+ */
94
+ export declare function createDtExtension(options?: {
95
+ jsDate?: boolean;
96
+ }): CborExtension;
97
+ /**
98
+ * Standard dt/DT CborExtension.
99
+ * Tagged DT values produce CborTaggedEpochDtExt; toJS() returns a number.
100
+ * For Date-based toJS() use dt_as_Date or createDtExtension({ jsDate: true }).
101
+ */
102
+ export declare const dt: CborExtension;
103
+ /**
104
+ * Full-featured dt/DT CborExtension with Date support.
105
+ * Tagged DT values produce CborTaggedEpochDtAsDateExt; toJS() returns a Date.
106
+ * fromJS(Date) converts Date instances to tagged epoch values.
107
+ */
108
+ export declare const dt_as_Date: CborExtension;
109
+ export default dt;
@@ -0,0 +1,17 @@
1
+ import { ToEDNOptions } from '../types';
2
+ import { CborExtension } from './types';
3
+ import { CborByteString } from '../ast/CborByteString';
4
+ import { CborTextString } from '../ast/CborTextString';
5
+ /**
6
+ * A byte string produced by a hash'…' or hash<<…>> literal.
7
+ * Remembers the original input and algorithm so toEDN() can reconstruct
8
+ * the hash notation when appStrings is not false.
9
+ */
10
+ export declare class CborHashExt extends CborByteString {
11
+ private readonly _input;
12
+ private readonly _algorithmId;
13
+ constructor(output: Uint8Array, input: CborTextString | CborByteString, algorithmId: number);
14
+ _toEDN(options: ToEDNOptions | undefined, _depth: number): string;
15
+ }
16
+ export declare const hash: CborExtension;
17
+ export default hash;
@@ -0,0 +1,41 @@
1
+ import { ToEDNOptions } from '../types';
2
+ import { CborExtension } from './types';
3
+ import { CborItem } from '../ast/CborItem';
4
+ import { CborByteString } from '../ast/CborByteString';
5
+ import { CborTag } from '../ast/CborTag';
6
+ import { CborArray } from '../ast/CborArray';
7
+ /**
8
+ * Bare IP address byte string whose toEDN() emits ip'…' notation.
9
+ */
10
+ export declare class CborIpExt extends CborByteString {
11
+ _toEDN(options: ToEDNOptions | undefined, _depth: number): string;
12
+ }
13
+ /**
14
+ * Bare IP address prefix (CIDR) whose toEDN() emits ip'…/prefix' notation.
15
+ * Encoded as [prefixLen, truncatedBytes] per RFC 9164 §2.3, without a tag.
16
+ */
17
+ export declare class CborIpPrefixExt extends CborArray {
18
+ private readonly _isV4;
19
+ constructor(prefixLen: number, truncated: Uint8Array, isV4: boolean);
20
+ _toEDN(options: ToEDNOptions | undefined, depth: number): string;
21
+ }
22
+ /**
23
+ * CBOR tag(52/54, …) IP address whose toEDN() emits IP'…' notation.
24
+ * Content may be a byte string (plain address) or an array [prefix, bytes]
25
+ * (CIDR prefix per RFC 9164).
26
+ */
27
+ export declare class CborTaggedIpExt extends CborTag {
28
+ constructor(tag: bigint, content: CborItem);
29
+ _toEDN(options: ToEDNOptions | undefined, depth: number): string;
30
+ }
31
+ /**
32
+ * Create an ip/IP CborExtension (RFC 9164 / §3.3 draft-ietf-cbor-edn-literals-20).
33
+ *
34
+ * - `ip'addr'` → CborIpExt (bare byte string, 4 or 16 bytes)
35
+ * - `IP'addr'` → CborTaggedIpExt tag(52 or 54, bytes)
36
+ * - `IP'addr/prefix'` → CborTaggedIpExt tag(52 or 54, [prefix_len, bytes])
37
+ * - parseTag(52/54, …) → CborTaggedIpExt (reversible via fromCBOR)
38
+ * - fromJS(tagged obj) → CborTaggedIpExt (reversible via fromJS)
39
+ */
40
+ export declare const ip: CborExtension;
41
+ export default ip;
@@ -0,0 +1,70 @@
1
+ import { CborItem } from '../ast/CborItem';
2
+ import { FromJSOptions } from '../types';
3
+ /**
4
+ * Plugin that extends EDN parsing, CBOR decoding, and `fromJS()` for specific
5
+ * application-string prefixes or CBOR tag numbers.
6
+ *
7
+ * Pass instances via `FromEDNOptions.extensions`, `FromCBOROptions.extensions`,
8
+ * or `FromJSOptions.extensions`.
9
+ *
10
+ * @example
11
+ * // Custom "ip" extension: ip'192.0.2.1' → CborByteString of 4 bytes
12
+ * const ipExtension: CborExtension = {
13
+ * appStringPrefixes: ['ip'],
14
+ * parseAppString(_prefix, content) {
15
+ * return new CborByteString(parseIPv4(content));
16
+ * },
17
+ * };
18
+ * parseEDN("ip'192.0.2.1'", { extensions: [ipExtension] });
19
+ */
20
+ export interface CborExtension {
21
+ /**
22
+ * App-string prefixes this extension handles (e.g. `['dt', 'DT']`).
23
+ * The tokenizer recognises these as `APP_STRING` / `APP_SEQUENCE` tokens.
24
+ */
25
+ readonly appStringPrefixes?: readonly string[];
26
+ /**
27
+ * CBOR tag numbers this extension handles (e.g. `[0n, 1n]`).
28
+ * Extensions with `parseTag()` are invoked for these tag numbers during
29
+ * `fromCBOR()` and integer-tagged EDN items (`1(…)`) in `fromEDN()`.
30
+ */
31
+ readonly tagNumbers?: readonly bigint[];
32
+ /**
33
+ * Parse an app-string literal: `prefix'content'` or `prefix"content"`.
34
+ * Receives the matched `prefix` and the decoded string `content`.
35
+ * Throw `SyntaxError` to report invalid content.
36
+ */
37
+ parseAppString?(prefix: string, content: string): CborItem;
38
+ /**
39
+ * Parse an app-sequence literal: `prefix<<item, ...>>`.
40
+ * Receives the matched `prefix` and the array of parsed CBOR values.
41
+ * If omitted, the `<<...>>` form is rejected with a `SyntaxError`.
42
+ */
43
+ parseAppSequence?(prefix: string, items: CborItem[]): CborItem;
44
+ /**
45
+ * Called when a `CborTag` is encountered during CBOR decode (`fromCBOR`)
46
+ * or EDN integer-tag parsing (`fromEDN`).
47
+ * Return `undefined` to fall back to the default `CborTag` representation.
48
+ */
49
+ parseTag?(tag: bigint, value: CborItem): CborItem | undefined;
50
+ /**
51
+ * Called during `fromJS()` for every value before the default conversion
52
+ * logic. Return `undefined` to fall through to the default behaviour.
53
+ * Typical uses: intercept `Date` instances, or CBOR-tagged plain objects
54
+ * that carry a `Symbol.for('cbor.tag')` key for a registered tag number.
55
+ */
56
+ fromJS?(value: unknown, options: FromJSOptions): CborItem | undefined;
57
+ /**
58
+ * Returns `true` if the given JS value is of a type that this extension's
59
+ * `fromJS()` converts. When `true`, the replacer pipeline passes the value
60
+ * through as-is instead of decomposing it with `Object.keys()` traversal.
61
+ *
62
+ * Implement this alongside `fromJS` for any class instance type (e.g.
63
+ * `Date`) that must survive the replacer pipeline intact so that `fromJS`
64
+ * can convert it correctly.
65
+ *
66
+ * Implementations may narrow the return type to a type predicate
67
+ * (e.g. `value is Date`) for better static type inference at the call site.
68
+ */
69
+ isJSType?(value: unknown): boolean;
70
+ }