@intellectual-frontiers/formats 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/archive.d.ts +147 -0
- package/dist/archive.js +765 -0
- package/dist/base64.d.ts +24 -0
- package/dist/base64.js +52 -0
- package/dist/cbor.d.ts +39 -0
- package/dist/cbor.js +244 -0
- package/dist/csv.d.ts +38 -0
- package/dist/csv.js +130 -0
- package/dist/der.d.ts +67 -0
- package/dist/der.js +221 -0
- package/dist/glob.d.ts +64 -0
- package/dist/glob.js +218 -0
- package/dist/hex.d.ts +17 -0
- package/dist/hex.js +30 -0
- package/dist/http.d.ts +46 -0
- package/dist/http.js +102 -0
- package/dist/json5.d.ts +46 -0
- package/dist/json5.js +291 -0
- package/dist/media-types.d.ts +28 -0
- package/dist/media-types.js +121 -0
- package/dist/mod.d.ts +39 -0
- package/dist/mod.js +31 -0
- package/dist/png.d.ts +124 -0
- package/dist/png.js +607 -0
- package/dist/semver.d.ts +94 -0
- package/dist/semver.js +331 -0
- package/dist/toml.d.ts +51 -0
- package/dist/toml.js +583 -0
- package/dist/ts-literal.d.ts +62 -0
- package/dist/ts-literal.js +173 -0
- package/dist/xml.d.ts +73 -0
- package/dist/xml.js +282 -0
- package/dist/yaml.d.ts +44 -0
- package/dist/yaml.js +706 -0
- package/package.json +118 -4
- package/src/archive.ts +831 -0
- package/src/base64.ts +51 -0
- package/src/cbor.ts +235 -0
- package/src/csv.ts +132 -0
- package/src/der.ts +224 -0
- package/src/glob.ts +210 -0
- package/src/hex.ts +28 -0
- package/src/http.ts +104 -0
- package/src/json5.ts +267 -0
- package/src/media-types.ts +127 -0
- package/src/mod.ts +102 -0
- package/src/png.ts +625 -0
- package/src/semver.ts +373 -0
- package/src/toml.ts +525 -0
- package/src/ts-literal.ts +183 -0
- package/src/xml.ts +291 -0
- package/src/yaml.ts +616 -0
package/dist/base64.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base64 and base64url (RFC 4648 §4 and §5), both ways, on bytes (commons 0001-formats FR-008): what DSSE envelopes, JWTs, data
|
|
3
|
+
* URLs, PEM and signed URLs carry. Decoding refuses what is not base64 of the alphabet asked for, rather than skipping it; base64url is
|
|
4
|
+
* written without padding and read with or without it. Taken from the helpers Stolos wrote in `src/archon/export/intoto.ts` and S/MIME.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { fromBase64, toBase64, toBase64Url } from "./base64.js";
|
|
9
|
+
* toBase64(new TextEncoder().encode("foobar")); // "Zm9vYmFy"
|
|
10
|
+
* toBase64Url(Uint8Array.of(0xfb, 0xff)); // "-_8"
|
|
11
|
+
* fromBase64("Zm9v"); // { kind: "ok", value: Uint8Array [102, 111, 111] }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import { type Result } from "@intellectual-frontiers/core";
|
|
17
|
+
/** Bytes as standard base64, padded. */
|
|
18
|
+
export declare function toBase64(bytes: Uint8Array): string;
|
|
19
|
+
/** Standard base64 as bytes; white space between the characters is ignored, and anything else that is not base64 refused. */
|
|
20
|
+
export declare function fromBase64(text: string): Result<Uint8Array, string>;
|
|
21
|
+
/** Bytes as base64url, without padding. */
|
|
22
|
+
export declare function toBase64Url(bytes: Uint8Array): string;
|
|
23
|
+
/** Base64url (padded or not) as bytes; anything else refused. */
|
|
24
|
+
export declare function fromBase64Url(text: string): Result<Uint8Array, string>;
|
package/dist/base64.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Spec: 0001-formats FR-008
|
|
2
|
+
/**
|
|
3
|
+
* Base64 and base64url (RFC 4648 §4 and §5), both ways, on bytes (commons 0001-formats FR-008): what DSSE envelopes, JWTs, data
|
|
4
|
+
* URLs, PEM and signed URLs carry. Decoding refuses what is not base64 of the alphabet asked for, rather than skipping it; base64url is
|
|
5
|
+
* written without padding and read with or without it. Taken from the helpers Stolos wrote in `src/archon/export/intoto.ts` and S/MIME.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { fromBase64, toBase64, toBase64Url } from "./base64.ts";
|
|
10
|
+
* toBase64(new TextEncoder().encode("foobar")); // "Zm9vYmFy"
|
|
11
|
+
* toBase64Url(Uint8Array.of(0xfb, 0xff)); // "-_8"
|
|
12
|
+
* fromBase64("Zm9v"); // { kind: "ok", value: Uint8Array [102, 111, 111] }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
import { err, ok } from "@intellectual-frontiers/core";
|
|
18
|
+
/** Bytes as standard base64, padded. */
|
|
19
|
+
export function toBase64(bytes) {
|
|
20
|
+
let s = "";
|
|
21
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
22
|
+
s += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
23
|
+
return btoa(s);
|
|
24
|
+
}
|
|
25
|
+
/** Standard base64 as bytes; white space between the characters is ignored, and anything else that is not base64 refused. */
|
|
26
|
+
export function fromBase64(text) {
|
|
27
|
+
const t = text.replace(/[\t\n\r ]/g, "");
|
|
28
|
+
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(t))
|
|
29
|
+
return err("the text is not padded standard base64");
|
|
30
|
+
const s = atob(t);
|
|
31
|
+
const out = new Uint8Array(s.length);
|
|
32
|
+
for (let i = 0; i < s.length; i++)
|
|
33
|
+
out[i] = s.charCodeAt(i);
|
|
34
|
+
return ok(out);
|
|
35
|
+
}
|
|
36
|
+
/** Bytes as base64url, without padding. */
|
|
37
|
+
export function toBase64Url(bytes) {
|
|
38
|
+
return toBase64(bytes).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
39
|
+
}
|
|
40
|
+
/** Base64url (padded or not) as bytes; anything else refused. */
|
|
41
|
+
export function fromBase64Url(text) {
|
|
42
|
+
if (!/^[A-Za-z0-9_-]*={0,2}$/.test(text))
|
|
43
|
+
return err("the text is not base64url");
|
|
44
|
+
const bare = text.replace(/=+$/, "");
|
|
45
|
+
if (bare.length % 4 === 1)
|
|
46
|
+
return err("the text is not base64url (its length cannot be)");
|
|
47
|
+
const padded = bare
|
|
48
|
+
.replaceAll("-", "+")
|
|
49
|
+
.replaceAll("_", "/")
|
|
50
|
+
.padEnd(Math.ceil(bare.length / 4) * 4, "=");
|
|
51
|
+
return fromBase64(padded);
|
|
52
|
+
}
|
package/dist/cbor.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Result } from "@intellectual-frontiers/core";
|
|
2
|
+
/** A map key this module reads and writes: an integer or a text string. */
|
|
3
|
+
export type CborKey = number | bigint | string;
|
|
4
|
+
/** A tagged value (major type 6): its tag number and the value it tags. */
|
|
5
|
+
export type CborTagged = {
|
|
6
|
+
readonly tag: number | bigint;
|
|
7
|
+
readonly value: CborValue;
|
|
8
|
+
};
|
|
9
|
+
/** A CBOR data item as a value: integers (a `bigint` beyond the safe range), floats, strings, bytes, arrays, maps, tags and simple values. */
|
|
10
|
+
export type CborValue = number | bigint | string | Uint8Array | boolean | null | undefined | readonly CborValue[] | ReadonlyMap<CborKey, CborValue> | CborTagged;
|
|
11
|
+
/** Why bytes are not CBOR this reader takes: what, and at which byte. */
|
|
12
|
+
export type CborError = {
|
|
13
|
+
readonly kind: "cbor";
|
|
14
|
+
readonly at: number;
|
|
15
|
+
readonly message: string;
|
|
16
|
+
};
|
|
17
|
+
/** One data item read from a longer input: its value and the offset just after it. */
|
|
18
|
+
export type CborItem = {
|
|
19
|
+
readonly value: CborValue;
|
|
20
|
+
readonly end: number;
|
|
21
|
+
};
|
|
22
|
+
/** The deepest nesting of arrays, maps and tags read. */
|
|
23
|
+
export declare const MAX_DEPTH = 64;
|
|
24
|
+
/** A CBOR error as one line. */
|
|
25
|
+
export declare function formatCborError(e: CborError): string;
|
|
26
|
+
/** Whether a value is a tagged value (rather than a map, an array or bytes). */
|
|
27
|
+
export declare function isTagged(value: CborValue): value is CborTagged;
|
|
28
|
+
/** Whether a value is a map. */
|
|
29
|
+
export declare function isMap(value: CborValue): value is ReadonlyMap<CborKey, CborValue>;
|
|
30
|
+
/** The data item that starts at `offset`, and the offset just after it: for CBOR followed by more bytes (WebAuthn's authenticator data). */
|
|
31
|
+
export declare function decodeCborItem(bytes: Uint8Array, offset?: number): Result<CborItem, CborError>;
|
|
32
|
+
/** The one data item the bytes hold; bytes left after it are an error. */
|
|
33
|
+
export declare function decodeCbor(bytes: Uint8Array): Result<CborValue, CborError>;
|
|
34
|
+
/**
|
|
35
|
+
* A value as CBOR, in the core deterministic encoding: integers (safe `number`s or `bigint`s) in their shortest head, other numbers as
|
|
36
|
+
* doubles, strings as UTF-8, `Uint8Array` as a byte string, arrays, maps with their keys sorted by their encoding, tags and the four
|
|
37
|
+
* simple values. A value no CBOR item can hold (an integer beyond 64 bits) is a programming error and throws a `RangeError`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function encodeCbor(value: CborValue): Uint8Array;
|
package/dist/cbor.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// Spec: 0001-formats FR-014
|
|
2
|
+
/**
|
|
3
|
+
* CBOR (RFC 8949), read and written with no outside package (commons 0014-webauthn FR-001): what WebAuthn's attestation objects and COSE
|
|
4
|
+
* keys are made of. The reader takes the definite-length forms of every major type (unsigned and negative integers, byte and text
|
|
5
|
+
* strings, arrays, maps, tags, the simple values `false`, `true`, `null` and `undefined`, and half, single and double floats) and returns a
|
|
6
|
+
* `Result` whose error says what and at which byte: an indefinite length, a reserved additional value, text that is not UTF-8, a map key
|
|
7
|
+
* that is neither an integer nor a text string, a key given twice, nesting deeper than 64 levels, or bytes left over. An integer beyond
|
|
8
|
+
* `Number.MAX_SAFE_INTEGER` is a `bigint`. The writer gives the core deterministic encoding (RFC 8949 §4.2.1): the shortest heads, definite
|
|
9
|
+
* lengths and map keys sorted by their encoded bytes, so the same value is the same bytes every time.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { type CborValue, decodeCbor, encodeCbor } from "./cbor.ts";
|
|
14
|
+
* const bytes = encodeCbor(new Map<number | string, CborValue>([[1, 2], ["a", [true, null]]]));
|
|
15
|
+
* decodeCbor(bytes); // { kind: "ok", value: Map { 1 => 2, "a" => [true, null] } }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @limits
|
|
19
|
+
* Indefinite-length strings, arrays and maps are refused (WebAuthn and COSE never use them); simple values other than the four named are
|
|
20
|
+
* refused; a float is written only as a double, and a map key may only be an integer or a text string.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import { concat } from "./archive.js";
|
|
25
|
+
import { compareBytes } from "./der.js";
|
|
26
|
+
import { err, ok } from "@intellectual-frontiers/core";
|
|
27
|
+
/** The deepest nesting of arrays, maps and tags read. */
|
|
28
|
+
export const MAX_DEPTH = 64;
|
|
29
|
+
/** A CBOR error as one line. */
|
|
30
|
+
export function formatCborError(e) {
|
|
31
|
+
return `CBOR at byte ${e.at}: ${e.message}`;
|
|
32
|
+
}
|
|
33
|
+
/** Whether a value is a tagged value (rather than a map, an array or bytes). */
|
|
34
|
+
export function isTagged(value) {
|
|
35
|
+
return typeof value === "object" && value !== null && !(value instanceof Uint8Array) && !(value instanceof Map) && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
/** Whether a value is a map. */
|
|
38
|
+
export function isMap(value) {
|
|
39
|
+
return value instanceof Map;
|
|
40
|
+
}
|
|
41
|
+
const UTF8 = new TextDecoder("utf-8", { fatal: true });
|
|
42
|
+
function readHead(bytes, at) {
|
|
43
|
+
const fail = (where, message) => err({ kind: "cbor", at: where, message });
|
|
44
|
+
const first = bytes[at];
|
|
45
|
+
if (first === undefined)
|
|
46
|
+
return fail(at, "no data item (the bytes end)");
|
|
47
|
+
const major = first >> 5;
|
|
48
|
+
const info = first & 0x1f;
|
|
49
|
+
if (info < 24)
|
|
50
|
+
return ok({ major, info, arg: info, end: at + 1 });
|
|
51
|
+
if (info === 31)
|
|
52
|
+
return fail(at, "an indefinite length (not read)");
|
|
53
|
+
if (info > 27)
|
|
54
|
+
return fail(at, `the reserved additional information ${info}`);
|
|
55
|
+
const size = 1 << (info - 24);
|
|
56
|
+
if (at + 1 + size > bytes.length)
|
|
57
|
+
return fail(at + 1, `${size} bytes of argument (only ${bytes.length - at - 1} are left)`);
|
|
58
|
+
let n = 0n;
|
|
59
|
+
for (let k = 0; k < size; k++)
|
|
60
|
+
n = (n << 8n) | BigInt(bytes[at + 1 + k] ?? 0);
|
|
61
|
+
const arg = n <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(n) : n;
|
|
62
|
+
return ok({ major, info, arg, end: at + 1 + size });
|
|
63
|
+
}
|
|
64
|
+
function halfToNumber(h) {
|
|
65
|
+
const sign = h & 0x8000 ? -1 : 1;
|
|
66
|
+
const exp = (h >> 10) & 0x1f;
|
|
67
|
+
const mant = h & 0x3ff;
|
|
68
|
+
if (exp === 0)
|
|
69
|
+
return sign * mant * 2 ** -24;
|
|
70
|
+
if (exp === 31)
|
|
71
|
+
return mant === 0 ? sign * Infinity : NaN;
|
|
72
|
+
return sign * (1 + mant / 1024) * 2 ** (exp - 15);
|
|
73
|
+
}
|
|
74
|
+
function readItem(bytes, at, depth) {
|
|
75
|
+
const fail = (where, message) => err({ kind: "cbor", at: where, message });
|
|
76
|
+
if (depth > MAX_DEPTH)
|
|
77
|
+
return fail(at, `nesting deeper than ${MAX_DEPTH} levels`);
|
|
78
|
+
const h = readHead(bytes, at);
|
|
79
|
+
if (h.kind === "err")
|
|
80
|
+
return h;
|
|
81
|
+
const { major, info, arg, end } = h.value;
|
|
82
|
+
const length = () => typeof arg === "bigint" || end + arg > bytes.length
|
|
83
|
+
? fail(end, `${String(arg)} bytes of content (only ${bytes.length - end} are left)`)
|
|
84
|
+
: ok(arg);
|
|
85
|
+
switch (major) {
|
|
86
|
+
case 0:
|
|
87
|
+
return ok({ value: arg, end });
|
|
88
|
+
case 1:
|
|
89
|
+
return ok({ value: typeof arg === "bigint" || arg === Number.MAX_SAFE_INTEGER ? -1n - BigInt(arg) : -1 - arg, end });
|
|
90
|
+
case 2: {
|
|
91
|
+
const n = length();
|
|
92
|
+
if (n.kind === "err")
|
|
93
|
+
return n;
|
|
94
|
+
return ok({ value: bytes.slice(end, end + n.value), end: end + n.value });
|
|
95
|
+
}
|
|
96
|
+
case 3: {
|
|
97
|
+
const n = length();
|
|
98
|
+
if (n.kind === "err")
|
|
99
|
+
return n;
|
|
100
|
+
try {
|
|
101
|
+
return ok({ value: UTF8.decode(bytes.subarray(end, end + n.value)), end: end + n.value });
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return fail(end, "a text string that is not UTF-8");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
case 4: {
|
|
108
|
+
// each element takes at least one byte, so a count beyond what is left cannot be met
|
|
109
|
+
if (typeof arg === "bigint" || arg > bytes.length - end)
|
|
110
|
+
return fail(at, `an array of ${String(arg)} elements (the bytes end first)`);
|
|
111
|
+
const out = [];
|
|
112
|
+
let i = end;
|
|
113
|
+
for (let k = 0; k < arg; k++) {
|
|
114
|
+
const e = readItem(bytes, i, depth + 1);
|
|
115
|
+
if (e.kind === "err")
|
|
116
|
+
return e;
|
|
117
|
+
out.push(e.value.value);
|
|
118
|
+
i = e.value.end;
|
|
119
|
+
}
|
|
120
|
+
return ok({ value: out, end: i });
|
|
121
|
+
}
|
|
122
|
+
case 5: {
|
|
123
|
+
if (typeof arg === "bigint" || arg * 2 > bytes.length - end)
|
|
124
|
+
return fail(at, `a map of ${String(arg)} pairs (the bytes end first)`);
|
|
125
|
+
const out = new Map();
|
|
126
|
+
let i = end;
|
|
127
|
+
for (let k = 0; k < arg; k++) {
|
|
128
|
+
const key = readItem(bytes, i, depth + 1);
|
|
129
|
+
if (key.kind === "err")
|
|
130
|
+
return key;
|
|
131
|
+
const kv = key.value.value;
|
|
132
|
+
if (typeof kv !== "number" && typeof kv !== "bigint" && typeof kv !== "string")
|
|
133
|
+
return fail(i, "a map key that is neither an integer nor a text string");
|
|
134
|
+
if (typeof kv === "number" && !Number.isInteger(kv))
|
|
135
|
+
return fail(i, "a map key that is a float");
|
|
136
|
+
if (out.has(kv))
|
|
137
|
+
return fail(i, `the map key ${String(kv)} given twice`);
|
|
138
|
+
const value = readItem(bytes, key.value.end, depth + 1);
|
|
139
|
+
if (value.kind === "err")
|
|
140
|
+
return value;
|
|
141
|
+
out.set(kv, value.value.value);
|
|
142
|
+
i = value.value.end;
|
|
143
|
+
}
|
|
144
|
+
return ok({ value: out, end: i });
|
|
145
|
+
}
|
|
146
|
+
case 6: {
|
|
147
|
+
const inner = readItem(bytes, end, depth + 1);
|
|
148
|
+
if (inner.kind === "err")
|
|
149
|
+
return inner;
|
|
150
|
+
return ok({ value: { tag: arg, value: inner.value.value }, end: inner.value.end });
|
|
151
|
+
}
|
|
152
|
+
case 7: {
|
|
153
|
+
if (info === 20)
|
|
154
|
+
return ok({ value: false, end });
|
|
155
|
+
if (info === 21)
|
|
156
|
+
return ok({ value: true, end });
|
|
157
|
+
if (info === 22)
|
|
158
|
+
return ok({ value: null, end });
|
|
159
|
+
if (info === 23)
|
|
160
|
+
return ok({ value: undefined, end });
|
|
161
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset + at + 1);
|
|
162
|
+
if (info === 25)
|
|
163
|
+
return ok({ value: halfToNumber(view.getUint16(0)), end });
|
|
164
|
+
if (info === 26)
|
|
165
|
+
return ok({ value: view.getFloat32(0), end });
|
|
166
|
+
if (info === 27)
|
|
167
|
+
return ok({ value: view.getFloat64(0), end });
|
|
168
|
+
return fail(at, `the simple value ${String(arg)} (not read)`);
|
|
169
|
+
}
|
|
170
|
+
default:
|
|
171
|
+
return fail(at, `the major type ${major}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/** The data item that starts at `offset`, and the offset just after it: for CBOR followed by more bytes (WebAuthn's authenticator data). */
|
|
175
|
+
export function decodeCborItem(bytes, offset = 0) {
|
|
176
|
+
return readItem(bytes, offset, 0);
|
|
177
|
+
}
|
|
178
|
+
/** The one data item the bytes hold; bytes left after it are an error. */
|
|
179
|
+
export function decodeCbor(bytes) {
|
|
180
|
+
const r = readItem(bytes, 0, 0);
|
|
181
|
+
if (r.kind === "err")
|
|
182
|
+
return r;
|
|
183
|
+
if (r.value.end !== bytes.length)
|
|
184
|
+
return err({ kind: "cbor", at: r.value.end, message: `${bytes.length - r.value.end} bytes after the data item` });
|
|
185
|
+
return ok(r.value.value);
|
|
186
|
+
}
|
|
187
|
+
function head(major, n) {
|
|
188
|
+
const v = BigInt(n);
|
|
189
|
+
if (v < 0n || v > 0xffffffffffffffffn)
|
|
190
|
+
throw new RangeError(`CBOR: no argument ${String(n)}`);
|
|
191
|
+
const m = major << 5;
|
|
192
|
+
if (v < 24n)
|
|
193
|
+
return Uint8Array.of(m | Number(v));
|
|
194
|
+
const size = v <= 0xffn ? 1 : v <= 0xffffn ? 2 : v <= 0xffffffffn ? 4 : 8;
|
|
195
|
+
const out = new Uint8Array(1 + size);
|
|
196
|
+
out[0] = m | (24 + Math.log2(size));
|
|
197
|
+
for (let k = 0; k < size; k++)
|
|
198
|
+
out[size - k] = Number((v >> BigInt(8 * k)) & 0xffn);
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
function encodeInteger(n) {
|
|
202
|
+
const v = BigInt(n);
|
|
203
|
+
return v >= 0n ? head(0, v) : head(1, -1n - v);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* A value as CBOR, in the core deterministic encoding: integers (safe `number`s or `bigint`s) in their shortest head, other numbers as
|
|
207
|
+
* doubles, strings as UTF-8, `Uint8Array` as a byte string, arrays, maps with their keys sorted by their encoding, tags and the four
|
|
208
|
+
* simple values. A value no CBOR item can hold (an integer beyond 64 bits) is a programming error and throws a `RangeError`.
|
|
209
|
+
*/
|
|
210
|
+
export function encodeCbor(value) {
|
|
211
|
+
if (value === false)
|
|
212
|
+
return Uint8Array.of(0xf4);
|
|
213
|
+
if (value === true)
|
|
214
|
+
return Uint8Array.of(0xf5);
|
|
215
|
+
if (value === null)
|
|
216
|
+
return Uint8Array.of(0xf6);
|
|
217
|
+
if (value === undefined)
|
|
218
|
+
return Uint8Array.of(0xf7);
|
|
219
|
+
if (typeof value === "bigint")
|
|
220
|
+
return encodeInteger(value);
|
|
221
|
+
if (typeof value === "number") {
|
|
222
|
+
if (Number.isSafeInteger(value) && !Object.is(value, -0))
|
|
223
|
+
return encodeInteger(value);
|
|
224
|
+
const out = new Uint8Array(9);
|
|
225
|
+
out[0] = 0xfb;
|
|
226
|
+
new DataView(out.buffer).setFloat64(1, value);
|
|
227
|
+
return out;
|
|
228
|
+
}
|
|
229
|
+
if (typeof value === "string") {
|
|
230
|
+
const b = new TextEncoder().encode(value);
|
|
231
|
+
return concat([head(3, b.length), b]);
|
|
232
|
+
}
|
|
233
|
+
if (value instanceof Uint8Array)
|
|
234
|
+
return concat([head(2, value.length), value]);
|
|
235
|
+
if (Array.isArray(value))
|
|
236
|
+
return concat([head(4, value.length), ...value.map((v) => encodeCbor(v))]);
|
|
237
|
+
if (isMap(value)) {
|
|
238
|
+
const pairs = [...value].map(([k, v]) => [encodeCbor(k), encodeCbor(v)]).sort((a, b) => compareBytes(a[0], b[0]));
|
|
239
|
+
return concat([head(5, pairs.length), ...pairs.flat()]);
|
|
240
|
+
}
|
|
241
|
+
if (isTagged(value))
|
|
242
|
+
return concat([head(6, value.tag), encodeCbor(value.value)]);
|
|
243
|
+
throw new RangeError("CBOR: a value that is not a CBOR value");
|
|
244
|
+
}
|
package/dist/csv.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSV by RFC 4180, read and written with no outside package (commons 0001-formats FR-005): fields separated by a comma (or
|
|
3
|
+
* another single character), a field quoted where it holds the separator, a quote or a line break, a quote inside one doubled, records
|
|
4
|
+
* ended by CRLF or LF. The reader refuses a quote in an unquoted field and a quoted field that is not closed, and says the line and column;
|
|
5
|
+
* it can give each record as an object by the header's names. Replaces `@std/csv` in erga.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { parseCsv, toCsv } from "./csv.js";
|
|
10
|
+
* parseCsv('name,quote\nAda,"Hello, ""world"""\n'); // { kind: "ok", value: [["name", "quote"], ["Ada", 'Hello, "world"']] }
|
|
11
|
+
* toCsv([["a", "b,c"]]); // 'a,"b,c"\r\n'
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import { type Result } from "@intellectual-frontiers/core";
|
|
17
|
+
/** Where a text stops being CSV, and why. Lines and columns count from 1. */
|
|
18
|
+
export type CsvError = {
|
|
19
|
+
readonly kind: "csv";
|
|
20
|
+
readonly line: number;
|
|
21
|
+
readonly column: number;
|
|
22
|
+
readonly message: string;
|
|
23
|
+
};
|
|
24
|
+
/** A CSV error as one line: `3:14: a quote in a field that is not quoted`. */
|
|
25
|
+
export declare function formatCsvError(e: CsvError): string;
|
|
26
|
+
/** How to read and write: the separator (a comma by default), and for writing the line ending (CRLF by default, as RFC 4180 has it). */
|
|
27
|
+
export type CsvOptions = {
|
|
28
|
+
readonly separator?: string;
|
|
29
|
+
readonly lineEnding?: "\r\n" | "\n";
|
|
30
|
+
};
|
|
31
|
+
/** The records of a CSV text, each a list of fields; a final line break ends the last record and makes no empty one. */
|
|
32
|
+
export declare function parseCsv(text: string, options?: CsvOptions): Result<readonly (readonly string[])[], CsvError>;
|
|
33
|
+
/** The records of a CSV text with a header, each an object by the header's names; a record whose length is not the header's is an error. */
|
|
34
|
+
export declare function parseCsvRecords(text: string, options?: CsvOptions): Result<readonly Readonly<Record<string, string>>[], CsvError>;
|
|
35
|
+
/** A field as CSV writes it: quoted, with its quotes doubled, where it holds the separator, a quote, a line break or leading or trailing space. */
|
|
36
|
+
export declare function csvField(value: string, separator?: string): string;
|
|
37
|
+
/** Records as CSV text, each ended by the line ending. */
|
|
38
|
+
export declare function toCsv(records: readonly (readonly string[])[], options?: CsvOptions): string;
|
package/dist/csv.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Spec: 0001-formats FR-005
|
|
2
|
+
/**
|
|
3
|
+
* CSV by RFC 4180, read and written with no outside package (commons 0001-formats FR-005): fields separated by a comma (or
|
|
4
|
+
* another single character), a field quoted where it holds the separator, a quote or a line break, a quote inside one doubled, records
|
|
5
|
+
* ended by CRLF or LF. The reader refuses a quote in an unquoted field and a quoted field that is not closed, and says the line and column;
|
|
6
|
+
* it can give each record as an object by the header's names. Replaces `@std/csv` in erga.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { parseCsv, toCsv } from "./csv.ts";
|
|
11
|
+
* parseCsv('name,quote\nAda,"Hello, ""world"""\n'); // { kind: "ok", value: [["name", "quote"], ["Ada", 'Hello, "world"']] }
|
|
12
|
+
* toCsv([["a", "b,c"]]); // 'a,"b,c"\r\n'
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
import { err, ok } from "@intellectual-frontiers/core";
|
|
18
|
+
/** A CSV error as one line: `3:14: a quote in a field that is not quoted`. */
|
|
19
|
+
export function formatCsvError(e) {
|
|
20
|
+
return `${e.line}:${e.column}: ${e.message}`;
|
|
21
|
+
}
|
|
22
|
+
/** The records of a CSV text, each a list of fields; a final line break ends the last record and makes no empty one. */
|
|
23
|
+
export function parseCsv(text, options = {}) {
|
|
24
|
+
const sep = options.separator ?? ",";
|
|
25
|
+
if (sep.length !== 1 || sep === '"' || sep === "\n" || sep === "\r")
|
|
26
|
+
throw new Error(`a CSV separator is one character that is not a quote or a line break: ${JSON.stringify(sep)}`);
|
|
27
|
+
const records = [];
|
|
28
|
+
let record = [];
|
|
29
|
+
let field = "";
|
|
30
|
+
let line = 1;
|
|
31
|
+
let column = 1;
|
|
32
|
+
let quoted = false;
|
|
33
|
+
let wasQuoted = false;
|
|
34
|
+
let fieldStart = true;
|
|
35
|
+
let i = text.startsWith("\ufeff") ? 1 : 0;
|
|
36
|
+
const fail = (message) => err({ kind: "csv", line, column, message });
|
|
37
|
+
const endField = () => {
|
|
38
|
+
record.push(field);
|
|
39
|
+
field = "";
|
|
40
|
+
fieldStart = true;
|
|
41
|
+
wasQuoted = false;
|
|
42
|
+
};
|
|
43
|
+
for (; i < text.length; i++, column++) {
|
|
44
|
+
const c = text[i] ?? "";
|
|
45
|
+
if (quoted) {
|
|
46
|
+
if (c === '"') {
|
|
47
|
+
if (text[i + 1] === '"') {
|
|
48
|
+
field += '"';
|
|
49
|
+
i++;
|
|
50
|
+
column++;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
quoted = false;
|
|
54
|
+
wasQuoted = true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
if (c === "\n") {
|
|
59
|
+
line++;
|
|
60
|
+
column = 0;
|
|
61
|
+
}
|
|
62
|
+
field += c;
|
|
63
|
+
}
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (c === sep) {
|
|
67
|
+
endField();
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (c === "\n" || c === "\r") {
|
|
71
|
+
if (c === "\r" && text[i + 1] === "\n")
|
|
72
|
+
i++;
|
|
73
|
+
endField();
|
|
74
|
+
records.push(record);
|
|
75
|
+
record = [];
|
|
76
|
+
line++;
|
|
77
|
+
column = 0;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (wasQuoted)
|
|
81
|
+
return fail(`the separator or a line break after a quoted field, not ${JSON.stringify(c)}`);
|
|
82
|
+
if (c === '"') {
|
|
83
|
+
if (!fieldStart)
|
|
84
|
+
return fail("a quote in a field that is not quoted");
|
|
85
|
+
quoted = true;
|
|
86
|
+
fieldStart = false;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
field += c;
|
|
90
|
+
fieldStart = false;
|
|
91
|
+
}
|
|
92
|
+
if (quoted)
|
|
93
|
+
return fail("the closing quote of a quoted field");
|
|
94
|
+
if (!fieldStart || record.length > 0) {
|
|
95
|
+
endField();
|
|
96
|
+
records.push(record);
|
|
97
|
+
}
|
|
98
|
+
return ok(records);
|
|
99
|
+
}
|
|
100
|
+
/** The records of a CSV text with a header, each an object by the header's names; a record whose length is not the header's is an error. */
|
|
101
|
+
export function parseCsvRecords(text, options = {}) {
|
|
102
|
+
const rows = parseCsv(text, options);
|
|
103
|
+
if (rows.kind === "err")
|
|
104
|
+
return rows;
|
|
105
|
+
const [header, ...body] = rows.value;
|
|
106
|
+
if (header === undefined)
|
|
107
|
+
return ok([]);
|
|
108
|
+
if (new Set(header).size !== header.length)
|
|
109
|
+
return err({ kind: "csv", line: 1, column: 1, message: "a header whose names differ" });
|
|
110
|
+
const out = [];
|
|
111
|
+
for (const [n, row] of body.entries()) {
|
|
112
|
+
if (row.length !== header.length)
|
|
113
|
+
return err({ kind: "csv", line: n + 2, column: 1, message: `${header.length} fields, as the header has, not ${row.length}` });
|
|
114
|
+
const record = {};
|
|
115
|
+
for (const [k, name] of header.entries())
|
|
116
|
+
Object.defineProperty(record, name, { value: row[k] ?? "", enumerable: true, writable: true, configurable: true });
|
|
117
|
+
out.push(record);
|
|
118
|
+
}
|
|
119
|
+
return ok(out);
|
|
120
|
+
}
|
|
121
|
+
/** A field as CSV writes it: quoted, with its quotes doubled, where it holds the separator, a quote, a line break or leading or trailing space. */
|
|
122
|
+
export function csvField(value, separator = ",") {
|
|
123
|
+
return value.includes(separator) || /["\r\n]/.test(value) || value !== value.trim() ? `"${value.replaceAll('"', '""')}"` : value;
|
|
124
|
+
}
|
|
125
|
+
/** Records as CSV text, each ended by the line ending. */
|
|
126
|
+
export function toCsv(records, options = {}) {
|
|
127
|
+
const sep = options.separator ?? ",";
|
|
128
|
+
const end = options.lineEnding ?? "\r\n";
|
|
129
|
+
return records.map((r) => `${r.length === 1 && r[0] === "" ? '""' : r.map((f) => csvField(f, sep)).join(sep)}${end}`).join("");
|
|
130
|
+
}
|
package/dist/der.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type Result } from "@intellectual-frontiers/core";
|
|
2
|
+
/** The universal tags used (class universal, with the constructed bit where it applies). */
|
|
3
|
+
export declare const TAG: {
|
|
4
|
+
readonly boolean: 1;
|
|
5
|
+
readonly integer: 2;
|
|
6
|
+
readonly bitString: 3;
|
|
7
|
+
readonly octetString: 4;
|
|
8
|
+
readonly null: 5;
|
|
9
|
+
readonly oid: 6;
|
|
10
|
+
readonly utf8String: 12;
|
|
11
|
+
readonly printableString: 19;
|
|
12
|
+
readonly ia5String: 22;
|
|
13
|
+
readonly utcTime: 23;
|
|
14
|
+
readonly generalizedTime: 24;
|
|
15
|
+
readonly sequence: 48;
|
|
16
|
+
readonly set: 49;
|
|
17
|
+
};
|
|
18
|
+
/** Why bytes are not DER this reader knows: what, and at which byte. */
|
|
19
|
+
export type DerError = {
|
|
20
|
+
readonly kind: "der";
|
|
21
|
+
readonly at: number;
|
|
22
|
+
readonly message: string;
|
|
23
|
+
};
|
|
24
|
+
/** One element read: its tag byte, its content, and its whole encoding (to copy it into another structure). */
|
|
25
|
+
export type Tlv = {
|
|
26
|
+
readonly tag: number;
|
|
27
|
+
readonly content: Uint8Array;
|
|
28
|
+
readonly raw: Uint8Array;
|
|
29
|
+
};
|
|
30
|
+
/** A DER length: the short form below 128, else `0x80` with the number of length bytes, then the bytes. */
|
|
31
|
+
export declare function encodeLength(n: number): Uint8Array;
|
|
32
|
+
/** One element: the tag byte, the content's length, the content. */
|
|
33
|
+
export declare function tlv(tag: number, content: Uint8Array): Uint8Array;
|
|
34
|
+
/** A SEQUENCE of these encodings, in order. */
|
|
35
|
+
export declare function sequence(...items: readonly Uint8Array[]): Uint8Array;
|
|
36
|
+
/** Two byte strings compared as DER sorts them: byte by byte, a prefix first. */
|
|
37
|
+
export declare function compareBytes(a: Uint8Array, b: Uint8Array): number;
|
|
38
|
+
/** A SET OF these encodings, sorted as DER requires (X.690 §11.6). */
|
|
39
|
+
export declare function setOf(...items: readonly Uint8Array[]): Uint8Array;
|
|
40
|
+
/** An OCTET STRING. */
|
|
41
|
+
export declare function octetString(b: Uint8Array): Uint8Array;
|
|
42
|
+
/** A BIT STRING of whole bytes (no unused bits). */
|
|
43
|
+
export declare function bitString(b: Uint8Array): Uint8Array;
|
|
44
|
+
/** A UTF8String. */
|
|
45
|
+
export declare function utf8String(s: string): Uint8Array;
|
|
46
|
+
/** A BOOLEAN. */
|
|
47
|
+
export declare function boolean(v: boolean): Uint8Array;
|
|
48
|
+
/** NULL. */
|
|
49
|
+
export declare function derNull(): Uint8Array;
|
|
50
|
+
/** An INTEGER: a JavaScript integer or a bigint (negative too), or big-endian unsigned magnitude bytes; always in its minimal form. */
|
|
51
|
+
export declare function integer(v: number | bigint | Uint8Array): Uint8Array;
|
|
52
|
+
/** An OBJECT IDENTIFIER from its dotted form (`1.2.840.113549`). */
|
|
53
|
+
export declare function oid(dotted: string): Uint8Array;
|
|
54
|
+
/** A context-specific EXPLICIT tag `[n]` around one encoding. */
|
|
55
|
+
export declare function explicit(n: number, inner: Uint8Array): Uint8Array;
|
|
56
|
+
/** A context-specific IMPLICIT tag `[n]`: this content under the tag, constructed where the underlying type is. */
|
|
57
|
+
export declare function implicit(n: number, content: Uint8Array, constructed: boolean): Uint8Array;
|
|
58
|
+
/** An AlgorithmIdentifier: `SEQUENCE { algorithm OID, parameters ANY OPTIONAL }`. */
|
|
59
|
+
export declare function algorithm(id: string, params?: Uint8Array): Uint8Array;
|
|
60
|
+
/** The element that starts at `offset`: its tag, its content and its whole encoding; or why it is not DER this reader knows. */
|
|
61
|
+
export declare function readTlv(bytes: Uint8Array, offset?: number): Result<Tlv, DerError>;
|
|
62
|
+
/** The elements inside a constructed element's content, in order. */
|
|
63
|
+
export declare function children(content: Uint8Array): Result<readonly Tlv[], DerError>;
|
|
64
|
+
/** The dotted form of an OBJECT IDENTIFIER's content. */
|
|
65
|
+
export declare function readOid(content: Uint8Array): Result<string, DerError>;
|
|
66
|
+
/** The value of an INTEGER's content, as a bigint (two's complement). */
|
|
67
|
+
export declare function readInteger(content: Uint8Array): Result<bigint, DerError>;
|