@intellectual-frontiers/formats 0.0.0-stage → 0.1.0
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/http.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Spec: 0001-formats FR-019
|
|
2
|
+
/**
|
|
3
|
+
* Pure HTTP helpers that API clients and servers keep rewriting (commons 0001-formats FR-019): the `Link` header (RFC 8288) read into its
|
|
4
|
+
* links so a client follows `rel="next"` pages, `Retry-After` (RFC 9110 §10.2.3) read as a wait whether it is seconds or a date, and which
|
|
5
|
+
* methods are safe (RFC 9110 §9.2.1) so a read-only client can refuse the rest, and the entity-tag conditions of `If-Match` and
|
|
6
|
+
* `If-None-Match` (RFC 9110 §13.1) a server answers with 412 or 304. Taken from Stolos's connector SDK (`src/archon/connect/sdk.ts`,
|
|
7
|
+
* `connector.ts`), which matched `rel="next"` with a regular expression in two places, and erga's `lib/ts/http` (on `@std/http/etag`).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { nextLink, retryAfterMs } from "./http.ts";
|
|
12
|
+
* nextLink('<https://api.example.com/items?page=2>; rel="next", <https://api.example.com/items?page=9>; rel="last"');
|
|
13
|
+
* // "https://api.example.com/items?page=2"
|
|
14
|
+
* retryAfterMs("120", Date.now()); // 120000
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
/** The links of a `Link` header, in order; a part that is not `<target>; params` is skipped. Relative targets are resolved against `base`. */
|
|
20
|
+
export function parseLinkHeader(header, base) {
|
|
21
|
+
const out = [];
|
|
22
|
+
const re = /<([^>]*)>((?:\s*;\s*[^;,=\s]+(?:\s*=\s*(?:"(?:[^"\\]|\\.)*"|[^;,\s]*))?)*)\s*(?:,|$)/g;
|
|
23
|
+
for (const m of header.matchAll(re)) {
|
|
24
|
+
const raw = m[1] ?? "";
|
|
25
|
+
let target = raw;
|
|
26
|
+
if (base !== undefined) {
|
|
27
|
+
try {
|
|
28
|
+
target = new URL(raw, base).href;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const params = {};
|
|
35
|
+
for (const p of (m[2] ?? "").matchAll(/;\s*([^;,=\s]+)(?:\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^;,\s]*)))?/g)) {
|
|
36
|
+
const name = (p[1] ?? "").toLowerCase();
|
|
37
|
+
if (!(name in params))
|
|
38
|
+
params[name] = (p[2] ?? p[3] ?? "").replace(/\\(.)/g, "$1");
|
|
39
|
+
}
|
|
40
|
+
out.push({ target, params });
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
/** The target of the first link whose `rel` names `rel` (`next` by default); a `rel` may name several, separated by spaces. */
|
|
45
|
+
export function nextLink(header, rel = "next", base) {
|
|
46
|
+
if (header === null || header === undefined)
|
|
47
|
+
return undefined;
|
|
48
|
+
return parseLinkHeader(header, base).find((l) => (l.params["rel"] ?? "").toLowerCase().split(/\s+/).includes(rel))?.target;
|
|
49
|
+
}
|
|
50
|
+
/** How long a `Retry-After` value asks to wait, in milliseconds from `now`: whole seconds, or an HTTP date (never negative); undefined where it is neither. */
|
|
51
|
+
export function retryAfterMs(value, now) {
|
|
52
|
+
if (value === null || value === undefined)
|
|
53
|
+
return undefined;
|
|
54
|
+
const v = value.trim();
|
|
55
|
+
if (/^\d+$/.test(v))
|
|
56
|
+
return Number(v) * 1000;
|
|
57
|
+
if (!/[A-Za-z]/.test(v))
|
|
58
|
+
return undefined;
|
|
59
|
+
const at = Date.parse(v);
|
|
60
|
+
return Number.isNaN(at) ? undefined : Math.max(0, at - now);
|
|
61
|
+
}
|
|
62
|
+
/** The methods that only read (RFC 9110 §9.2.1). */
|
|
63
|
+
export const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS", "TRACE"]);
|
|
64
|
+
/** Whether a method only reads; case does not matter. */
|
|
65
|
+
export function isSafeMethod(method) {
|
|
66
|
+
return SAFE_METHODS.has(method.toUpperCase());
|
|
67
|
+
}
|
|
68
|
+
/** The entity tags of an `If-Match` or `If-None-Match` header, as written (`"abc"`, `W/"abc"`), or `*`. */
|
|
69
|
+
export function entityTags(header) {
|
|
70
|
+
if (header.trim() === "*")
|
|
71
|
+
return "*";
|
|
72
|
+
return [...header.matchAll(/(?:W\/)?"[^"]*"/g)].map((m) => m[0]);
|
|
73
|
+
}
|
|
74
|
+
const opaque = (tag) => tag.replace(/^W\//, "");
|
|
75
|
+
/** An entity tag for content by its checksum or another stable token: strong (`"token"`), or weak (`W/"token"`). */
|
|
76
|
+
export function entityTag(token, weak = false) {
|
|
77
|
+
return `${weak ? "W/" : ""}"${token.replaceAll('"', "")}"`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Whether a request with this `If-Match` header may go on for content with this entity tag (RFC 9110 §13.1.1): yes where the header is
|
|
81
|
+
* absent, is `*`, or names the tag by strong comparison (a weak tag never matches).
|
|
82
|
+
*/
|
|
83
|
+
export function ifMatch(header, etag) {
|
|
84
|
+
if (header === null || header === undefined)
|
|
85
|
+
return true;
|
|
86
|
+
const tags = entityTags(header);
|
|
87
|
+
if (tags === "*")
|
|
88
|
+
return true;
|
|
89
|
+
return !etag.startsWith("W/") && tags.some((t) => !t.startsWith("W/") && t === etag);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Whether a request with this `If-None-Match` header should get the content again (RFC 9110 §13.1.2), rather than 304 or 412: yes where
|
|
93
|
+
* the header is absent or names no tag equal to this one by weak comparison; no where it is `*`.
|
|
94
|
+
*/
|
|
95
|
+
export function ifNoneMatch(header, etag) {
|
|
96
|
+
if (header === null || header === undefined)
|
|
97
|
+
return true;
|
|
98
|
+
const tags = entityTags(header);
|
|
99
|
+
if (tags === "*")
|
|
100
|
+
return false;
|
|
101
|
+
return !tags.some((t) => opaque(t) === opaque(etag));
|
|
102
|
+
}
|
package/dist/json5.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON5 (https://spec.json5.org/, 1.0.0), read and written with no outside package (commons 0001-formats FR-001): the reader
|
|
3
|
+
* takes the whole grammar (comments, trailing commas, unquoted ECMAScript keys, single-quoted and continued strings, hexadecimal,
|
|
4
|
+
* `Infinity`, `NaN`, a leading `+`, a leading or trailing decimal point) and gives a typed `Json` value or the line and column where the
|
|
5
|
+
* text stops being JSON5; the writer gives the form a person edits: a `//` comment at the head, keys bare where they can be, two spaces of
|
|
6
|
+
* indent, a comma after every member, and a short list of plain values on one line. Taken from erga's `lib/ts/json5`, whose reader was
|
|
7
|
+
* the `json5` package.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { parseJson5, toJson5 } from "./json5.js";
|
|
12
|
+
* parseJson5("{ unquoted: 'and you can quote me on that', trailing: [1, 2,], }");
|
|
13
|
+
* // { kind: "ok", value: { unquoted: "and you can quote me on that", trailing: [1, 2] } }
|
|
14
|
+
* toJson5({ name: "a", tags: ["x", "y"] }, "What this file is");
|
|
15
|
+
* // "// What this file is\n{\n name: \"a\",\n tags: [\"x\", \"y\"],\n}\n"
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
import { type Result } from "@intellectual-frontiers/core";
|
|
21
|
+
/** A JSON value: what a JSON5 file holds once read. An object keeps its members in the order they were written; a number may be `NaN` or infinite. */
|
|
22
|
+
export type Json = null | boolean | number | string | readonly Json[] | JsonObject;
|
|
23
|
+
/** A JSON object. */
|
|
24
|
+
export type JsonObject = {
|
|
25
|
+
readonly [key: string]: Json;
|
|
26
|
+
};
|
|
27
|
+
/** Where a text stops being JSON5, and why. Lines and columns count from 1. */
|
|
28
|
+
export type Json5Error = {
|
|
29
|
+
readonly kind: "json5";
|
|
30
|
+
readonly line: number;
|
|
31
|
+
readonly column: number;
|
|
32
|
+
readonly message: string;
|
|
33
|
+
};
|
|
34
|
+
/** A JSON5 error as one line: `3:14: expected a value`. */
|
|
35
|
+
export declare function formatJson5Error(e: Json5Error): string;
|
|
36
|
+
/** Whether a value is a JSON object. */
|
|
37
|
+
export declare function isJsonObject(v: Json): v is JsonObject;
|
|
38
|
+
/** JSON5 (or JSON) text read as a JSON value, or where and why it is not JSON5. */
|
|
39
|
+
export declare function parseJson5(text: string): Result<Json, Json5Error>;
|
|
40
|
+
/** Whether a key may stand without quotes: an ECMAScript identifier of ASCII letters, digits, `_` and `$`, not starting with a digit. */
|
|
41
|
+
export declare function isBareKey(key: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* A value as JSON5 text: `comment` first, one `//` line for each of its lines (none where it is empty), then the value, ending in a line
|
|
44
|
+
* break. A list of plain values stays on one line while it fits in `width` bytes (100 by default).
|
|
45
|
+
*/
|
|
46
|
+
export declare function toJson5(value: Json, comment?: string, width?: number): string;
|
package/dist/json5.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// Spec: 0001-formats FR-001
|
|
2
|
+
/**
|
|
3
|
+
* JSON5 (https://spec.json5.org/, 1.0.0), read and written with no outside package (commons 0001-formats FR-001): the reader
|
|
4
|
+
* takes the whole grammar (comments, trailing commas, unquoted ECMAScript keys, single-quoted and continued strings, hexadecimal,
|
|
5
|
+
* `Infinity`, `NaN`, a leading `+`, a leading or trailing decimal point) and gives a typed `Json` value or the line and column where the
|
|
6
|
+
* text stops being JSON5; the writer gives the form a person edits: a `//` comment at the head, keys bare where they can be, two spaces of
|
|
7
|
+
* indent, a comma after every member, and a short list of plain values on one line. Taken from erga's `lib/ts/json5`, whose reader was
|
|
8
|
+
* the `json5` package.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { parseJson5, toJson5 } from "./json5.ts";
|
|
13
|
+
* parseJson5("{ unquoted: 'and you can quote me on that', trailing: [1, 2,], }");
|
|
14
|
+
* // { kind: "ok", value: { unquoted: "and you can quote me on that", trailing: [1, 2] } }
|
|
15
|
+
* toJson5({ name: "a", tags: ["x", "y"] }, "What this file is");
|
|
16
|
+
* // "// What this file is\n{\n name: \"a\",\n tags: [\"x\", \"y\"],\n}\n"
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
import { err, ok } from "@intellectual-frontiers/core";
|
|
22
|
+
/** A JSON5 error as one line: `3:14: expected a value`. */
|
|
23
|
+
export function formatJson5Error(e) {
|
|
24
|
+
return `${e.line}:${e.column}: ${e.message}`;
|
|
25
|
+
}
|
|
26
|
+
/** Whether a value is a JSON object. */
|
|
27
|
+
export function isJsonObject(v) {
|
|
28
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
29
|
+
}
|
|
30
|
+
class Stop extends Error {
|
|
31
|
+
at;
|
|
32
|
+
constructor(at, message) {
|
|
33
|
+
super(message);
|
|
34
|
+
this.at = at;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const LINE = /[\n\r\u2028\u2029]/;
|
|
38
|
+
const SPACE = /[\t\v\f \u00a0\ufeff\p{Zs}]/u;
|
|
39
|
+
const ID_START = /[\p{ID_Start}$_]/u;
|
|
40
|
+
const ID_PART = /[\p{ID_Continue}$\u200c\u200d]/u;
|
|
41
|
+
const ESCAPES = { "'": "'", '"': '"', "\\": "\\", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v" };
|
|
42
|
+
/** JSON5 (or JSON) text read as a JSON value, or where and why it is not JSON5. */
|
|
43
|
+
export function parseJson5(text) {
|
|
44
|
+
let i = 0;
|
|
45
|
+
const fail = (message, at = i) => {
|
|
46
|
+
throw new Stop(at, message);
|
|
47
|
+
};
|
|
48
|
+
const peek = () => text[i] ?? "";
|
|
49
|
+
const skip = () => {
|
|
50
|
+
for (;;) {
|
|
51
|
+
const c = peek();
|
|
52
|
+
if (c !== "" && (SPACE.test(c) || LINE.test(c)))
|
|
53
|
+
i++;
|
|
54
|
+
else if (text.startsWith("//", i)) {
|
|
55
|
+
while (i < text.length && !LINE.test(peek()))
|
|
56
|
+
i++;
|
|
57
|
+
}
|
|
58
|
+
else if (text.startsWith("/*", i)) {
|
|
59
|
+
const end = text.indexOf("*/", i + 2);
|
|
60
|
+
if (end < 0)
|
|
61
|
+
fail("a comment that is not closed");
|
|
62
|
+
i = end + 2;
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const hex = (n) => {
|
|
69
|
+
const h = text.slice(i, i + n);
|
|
70
|
+
if (h.length !== n || !/^[0-9a-fA-F]+$/.test(h))
|
|
71
|
+
fail(`${n} hexadecimal digits`);
|
|
72
|
+
i += n;
|
|
73
|
+
return Number.parseInt(h, 16);
|
|
74
|
+
};
|
|
75
|
+
const escape = () => {
|
|
76
|
+
const c = peek();
|
|
77
|
+
i++;
|
|
78
|
+
if (c === "")
|
|
79
|
+
return fail("the rest of an escape");
|
|
80
|
+
const simple = ESCAPES[c];
|
|
81
|
+
if (simple !== undefined)
|
|
82
|
+
return simple;
|
|
83
|
+
if (c === "0") {
|
|
84
|
+
if (/[0-9]/.test(peek()))
|
|
85
|
+
fail("no digit after \\0");
|
|
86
|
+
return "\0";
|
|
87
|
+
}
|
|
88
|
+
if (/[1-9]/.test(c))
|
|
89
|
+
fail("no escaped digit", i - 1);
|
|
90
|
+
if (c === "x")
|
|
91
|
+
return String.fromCharCode(hex(2));
|
|
92
|
+
if (c === "u")
|
|
93
|
+
return String.fromCharCode(hex(4));
|
|
94
|
+
if (c === "\r") {
|
|
95
|
+
if (peek() === "\n")
|
|
96
|
+
i++;
|
|
97
|
+
return "";
|
|
98
|
+
}
|
|
99
|
+
if (LINE.test(c))
|
|
100
|
+
return "";
|
|
101
|
+
return c;
|
|
102
|
+
};
|
|
103
|
+
const string = () => {
|
|
104
|
+
const quote = peek();
|
|
105
|
+
i++;
|
|
106
|
+
let out = "";
|
|
107
|
+
for (;;) {
|
|
108
|
+
const c = peek();
|
|
109
|
+
if (c === "")
|
|
110
|
+
fail("a closing quote");
|
|
111
|
+
i++;
|
|
112
|
+
if (c === quote)
|
|
113
|
+
return out;
|
|
114
|
+
if (c === "\\")
|
|
115
|
+
out += escape();
|
|
116
|
+
else if (c === "\n" || c === "\r")
|
|
117
|
+
fail("no line break inside a string", i - 1);
|
|
118
|
+
else
|
|
119
|
+
out += c;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const identifier = () => {
|
|
123
|
+
let out = "";
|
|
124
|
+
for (let first = true;; first = false) {
|
|
125
|
+
let c = peek();
|
|
126
|
+
if (c === "\\") {
|
|
127
|
+
if (text[i + 1] !== "u")
|
|
128
|
+
fail("\\u in a key");
|
|
129
|
+
i += 2;
|
|
130
|
+
c = String.fromCharCode(hex(4));
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
const cp = text.codePointAt(i);
|
|
134
|
+
c = cp === undefined ? "" : String.fromCodePoint(cp);
|
|
135
|
+
if (c === "" || !(first ? ID_START : ID_PART).test(c)) {
|
|
136
|
+
if (first)
|
|
137
|
+
fail("a key");
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
i += c.length;
|
|
141
|
+
}
|
|
142
|
+
if (!(first ? ID_START : ID_PART).test(c))
|
|
143
|
+
fail("a key");
|
|
144
|
+
out += c;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const number = () => {
|
|
148
|
+
const m = /^([+-]?)(Infinity|NaN|0[xX][0-9a-fA-F]+|(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?|\.[0-9]+(?:[eE][+-]?[0-9]+)?)/.exec(text.slice(i));
|
|
149
|
+
if (m === null)
|
|
150
|
+
return fail("a value");
|
|
151
|
+
const [whole = "", sign = "", body = ""] = m;
|
|
152
|
+
i += whole.length;
|
|
153
|
+
if (ID_PART.test(peek()))
|
|
154
|
+
fail("a number that ends where it should");
|
|
155
|
+
const magnitude = body === "Infinity"
|
|
156
|
+
? Number.POSITIVE_INFINITY
|
|
157
|
+
: body === "NaN"
|
|
158
|
+
? Number.NaN
|
|
159
|
+
: /^0[xX]/.test(body)
|
|
160
|
+
? Number.parseInt(body.slice(2), 16)
|
|
161
|
+
: Number(body);
|
|
162
|
+
return sign === "-" ? -magnitude : magnitude;
|
|
163
|
+
};
|
|
164
|
+
const word = (w) => {
|
|
165
|
+
if (!text.startsWith(w, i) || ID_PART.test(text[i + w.length] ?? ""))
|
|
166
|
+
return false;
|
|
167
|
+
i += w.length;
|
|
168
|
+
return true;
|
|
169
|
+
};
|
|
170
|
+
const value = (depth) => {
|
|
171
|
+
if (depth > 512)
|
|
172
|
+
fail("less nesting (more than 512 levels)");
|
|
173
|
+
skip();
|
|
174
|
+
const c = peek();
|
|
175
|
+
if (c === "{") {
|
|
176
|
+
i++;
|
|
177
|
+
const o = {};
|
|
178
|
+
for (;;) {
|
|
179
|
+
skip();
|
|
180
|
+
if (peek() === "}") {
|
|
181
|
+
i++;
|
|
182
|
+
return o;
|
|
183
|
+
}
|
|
184
|
+
const key = peek() === '"' || peek() === "'" ? string() : identifier();
|
|
185
|
+
skip();
|
|
186
|
+
if (peek() !== ":")
|
|
187
|
+
fail("a colon");
|
|
188
|
+
i++;
|
|
189
|
+
Object.defineProperty(o, key, { value: value(depth + 1), enumerable: true, writable: true, configurable: true });
|
|
190
|
+
skip();
|
|
191
|
+
if (peek() === ",")
|
|
192
|
+
i++;
|
|
193
|
+
else if (peek() !== "}")
|
|
194
|
+
fail("a comma or }");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (c === "[") {
|
|
198
|
+
i++;
|
|
199
|
+
const a = [];
|
|
200
|
+
for (;;) {
|
|
201
|
+
skip();
|
|
202
|
+
if (peek() === "]") {
|
|
203
|
+
i++;
|
|
204
|
+
return a;
|
|
205
|
+
}
|
|
206
|
+
a.push(value(depth + 1));
|
|
207
|
+
skip();
|
|
208
|
+
if (peek() === ",")
|
|
209
|
+
i++;
|
|
210
|
+
else if (peek() !== "]")
|
|
211
|
+
fail("a comma or ]");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (c === '"' || c === "'")
|
|
215
|
+
return string();
|
|
216
|
+
if (word("null"))
|
|
217
|
+
return null;
|
|
218
|
+
if (word("true"))
|
|
219
|
+
return true;
|
|
220
|
+
if (word("false"))
|
|
221
|
+
return false;
|
|
222
|
+
return number();
|
|
223
|
+
};
|
|
224
|
+
try {
|
|
225
|
+
const v = value(0);
|
|
226
|
+
skip();
|
|
227
|
+
if (i < text.length)
|
|
228
|
+
fail("the end of the text");
|
|
229
|
+
return ok(v);
|
|
230
|
+
}
|
|
231
|
+
catch (e) {
|
|
232
|
+
if (!(e instanceof Stop))
|
|
233
|
+
throw e;
|
|
234
|
+
const before = text.slice(0, e.at).split(/\r\n|[\n\r\u2028\u2029]/);
|
|
235
|
+
return err({ kind: "json5", line: before.length, column: (before.at(-1)?.length ?? 0) + 1, message: `expected ${e.message}` });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/** Whether a key may stand without quotes: an ECMAScript identifier of ASCII letters, digits, `_` and `$`, not starting with a digit. */
|
|
239
|
+
export function isBareKey(key) {
|
|
240
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key);
|
|
241
|
+
}
|
|
242
|
+
const plainValue = (v) => !(Array.isArray(v) || isJsonObject(v));
|
|
243
|
+
function scalar(v) {
|
|
244
|
+
if (typeof v === "number") {
|
|
245
|
+
if (Number.isNaN(v))
|
|
246
|
+
return "NaN";
|
|
247
|
+
if (!Number.isFinite(v))
|
|
248
|
+
return v > 0 ? "Infinity" : "-Infinity";
|
|
249
|
+
if (Object.is(v, -0))
|
|
250
|
+
return "-0";
|
|
251
|
+
}
|
|
252
|
+
return JSON.stringify(v);
|
|
253
|
+
}
|
|
254
|
+
function write(v, depth, width) {
|
|
255
|
+
const pad = (n) => " ".repeat(n);
|
|
256
|
+
if (Array.isArray(v)) {
|
|
257
|
+
const items = v;
|
|
258
|
+
if (items.length === 0)
|
|
259
|
+
return "[]";
|
|
260
|
+
const line = items.map(scalar).join(", ");
|
|
261
|
+
if (items.every(plainValue) && new TextEncoder().encode(line).length + depth * 2 <= width)
|
|
262
|
+
return `[${line}]`;
|
|
263
|
+
return `[\n${items.map((item) => `${pad(depth + 1)}${write(item, depth + 1, width)},\n`).join("")}${pad(depth)}]`;
|
|
264
|
+
}
|
|
265
|
+
if (isJsonObject(v)) {
|
|
266
|
+
const entries = Object.entries(v);
|
|
267
|
+
if (entries.length === 0)
|
|
268
|
+
return "{}";
|
|
269
|
+
return `{\n${entries.map(([k, item]) => `${pad(depth + 1)}${isBareKey(k) ? k : JSON.stringify(k)}: ${write(item, depth + 1, width)},\n`).join("")}${pad(depth)}}`;
|
|
270
|
+
}
|
|
271
|
+
return scalar(v);
|
|
272
|
+
}
|
|
273
|
+
/** The lines of a comment: none for empty text, and no last empty line for a final line break. */
|
|
274
|
+
function linesOf(text) {
|
|
275
|
+
if (text === "")
|
|
276
|
+
return [];
|
|
277
|
+
const lines = text.split("\n").map((l) => (l.endsWith("\r") ? l.slice(0, -1) : l));
|
|
278
|
+
if (text.endsWith("\n"))
|
|
279
|
+
lines.pop();
|
|
280
|
+
return lines;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* A value as JSON5 text: `comment` first, one `//` line for each of its lines (none where it is empty), then the value, ending in a line
|
|
284
|
+
* break. A list of plain values stays on one line while it fits in `width` bytes (100 by default).
|
|
285
|
+
*/
|
|
286
|
+
export function toJson5(value, comment = "", width = 100) {
|
|
287
|
+
const head = linesOf(comment)
|
|
288
|
+
.map((l) => (l === "" ? "//\n" : `// ${l}\n`))
|
|
289
|
+
.join("");
|
|
290
|
+
return `${head}${write(value, 0, width)}\n`;
|
|
291
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Media types by file extension (commons 0001-formats FR-018): what a server says a file is, with `charset=UTF-8` on text, so a
|
|
3
|
+
* page, a stylesheet, a feed or a book is served as what it is. The table holds the types the harnesses serve and publish (web, documents,
|
|
4
|
+
* images, fonts, audio and video, archives, data); an extension it does not hold is `application/octet-stream` to `contentType`. Replaces
|
|
5
|
+
* `@std/media-types` in erga.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { contentType, mediaType } from "./media-types.js";
|
|
10
|
+
* contentType("site/index.html"); // "text/html; charset=UTF-8"
|
|
11
|
+
* mediaType(".epub"); // "application/epub+zip"
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
/** The media type of each extension (lower case, without its dot). */
|
|
17
|
+
export declare const MEDIA_TYPES: Readonly<Record<string, string>>;
|
|
18
|
+
/** Whether text of a media type is read as characters, and so served with a charset. */
|
|
19
|
+
export declare function isTextual(type: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The extension of a path (`site/index.html` gives `html`), or an extension given on its own (`.html` or `html`), in lower case without its
|
|
22
|
+
* dot; `""` where a path has none. A name that starts with a dot and has no other (`.epub`) is taken as an extension.
|
|
23
|
+
*/
|
|
24
|
+
export declare function extensionOf(pathOrExtension: string): string;
|
|
25
|
+
/** The media type of a path or an extension, or undefined where the table does not hold it. */
|
|
26
|
+
export declare function mediaType(pathOrExtension: string): string | undefined;
|
|
27
|
+
/** What a `Content-Type` header says for a path or an extension: its media type, with `; charset=UTF-8` for text; `application/octet-stream` where it is not known. */
|
|
28
|
+
export declare function contentType(pathOrExtension: string): string;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Spec: 0001-formats FR-018
|
|
2
|
+
/**
|
|
3
|
+
* Media types by file extension (commons 0001-formats FR-018): what a server says a file is, with `charset=UTF-8` on text, so a
|
|
4
|
+
* page, a stylesheet, a feed or a book is served as what it is. The table holds the types the harnesses serve and publish (web, documents,
|
|
5
|
+
* images, fonts, audio and video, archives, data); an extension it does not hold is `application/octet-stream` to `contentType`. Replaces
|
|
6
|
+
* `@std/media-types` in erga.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { contentType, mediaType } from "./media-types.ts";
|
|
11
|
+
* contentType("site/index.html"); // "text/html; charset=UTF-8"
|
|
12
|
+
* mediaType(".epub"); // "application/epub+zip"
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
/** The media type of each extension (lower case, without its dot). */
|
|
18
|
+
export const MEDIA_TYPES = {
|
|
19
|
+
html: "text/html",
|
|
20
|
+
htm: "text/html",
|
|
21
|
+
xhtml: "application/xhtml+xml",
|
|
22
|
+
css: "text/css",
|
|
23
|
+
js: "text/javascript",
|
|
24
|
+
mjs: "text/javascript",
|
|
25
|
+
cjs: "text/javascript",
|
|
26
|
+
ts: "text/typescript",
|
|
27
|
+
map: "application/json",
|
|
28
|
+
json: "application/json",
|
|
29
|
+
jsonld: "application/ld+json",
|
|
30
|
+
webmanifest: "application/manifest+json",
|
|
31
|
+
xml: "application/xml",
|
|
32
|
+
xsl: "application/xslt+xml",
|
|
33
|
+
rss: "application/rss+xml",
|
|
34
|
+
atom: "application/atom+xml",
|
|
35
|
+
opf: "application/oebps-package+xml",
|
|
36
|
+
ncx: "application/x-dtbncx+xml",
|
|
37
|
+
txt: "text/plain",
|
|
38
|
+
text: "text/plain",
|
|
39
|
+
md: "text/markdown",
|
|
40
|
+
markdown: "text/markdown",
|
|
41
|
+
adoc: "text/asciidoc",
|
|
42
|
+
csv: "text/csv",
|
|
43
|
+
tsv: "text/tab-separated-values",
|
|
44
|
+
ics: "text/calendar",
|
|
45
|
+
vtt: "text/vtt",
|
|
46
|
+
yaml: "application/yaml",
|
|
47
|
+
yml: "application/yaml",
|
|
48
|
+
toml: "application/toml",
|
|
49
|
+
wasm: "application/wasm",
|
|
50
|
+
pdf: "application/pdf",
|
|
51
|
+
epub: "application/epub+zip",
|
|
52
|
+
zip: "application/zip",
|
|
53
|
+
gz: "application/gzip",
|
|
54
|
+
tgz: "application/gzip",
|
|
55
|
+
tar: "application/x-tar",
|
|
56
|
+
xz: "application/x-xz",
|
|
57
|
+
zst: "application/zstd",
|
|
58
|
+
"7z": "application/x-7z-compressed",
|
|
59
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
60
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
61
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
62
|
+
odt: "application/vnd.oasis.opendocument.text",
|
|
63
|
+
rtf: "application/rtf",
|
|
64
|
+
png: "image/png",
|
|
65
|
+
apng: "image/apng",
|
|
66
|
+
jpg: "image/jpeg",
|
|
67
|
+
jpeg: "image/jpeg",
|
|
68
|
+
gif: "image/gif",
|
|
69
|
+
webp: "image/webp",
|
|
70
|
+
avif: "image/avif",
|
|
71
|
+
svg: "image/svg+xml",
|
|
72
|
+
ico: "image/vnd.microsoft.icon",
|
|
73
|
+
bmp: "image/bmp",
|
|
74
|
+
tif: "image/tiff",
|
|
75
|
+
tiff: "image/tiff",
|
|
76
|
+
woff: "font/woff",
|
|
77
|
+
woff2: "font/woff2",
|
|
78
|
+
ttf: "font/ttf",
|
|
79
|
+
otf: "font/otf",
|
|
80
|
+
mp3: "audio/mpeg",
|
|
81
|
+
m4a: "audio/mp4",
|
|
82
|
+
ogg: "audio/ogg",
|
|
83
|
+
oga: "audio/ogg",
|
|
84
|
+
opus: "audio/opus",
|
|
85
|
+
wav: "audio/wav",
|
|
86
|
+
flac: "audio/flac",
|
|
87
|
+
mp4: "video/mp4",
|
|
88
|
+
m4v: "video/mp4",
|
|
89
|
+
webm: "video/webm",
|
|
90
|
+
ogv: "video/ogg",
|
|
91
|
+
mov: "video/quicktime",
|
|
92
|
+
};
|
|
93
|
+
/** Whether text of a media type is read as characters, and so served with a charset. */
|
|
94
|
+
export function isTextual(type) {
|
|
95
|
+
return (type.startsWith("text/") ||
|
|
96
|
+
/^application\/(?:json|ld\+json|manifest\+json|xml|xhtml\+xml|xslt\+xml|rss\+xml|atom\+xml|yaml|toml|javascript)$/.test(type) ||
|
|
97
|
+
type === "image/svg+xml" ||
|
|
98
|
+
type.endsWith("+xml") ||
|
|
99
|
+
type.endsWith("+json"));
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The extension of a path (`site/index.html` gives `html`), or an extension given on its own (`.html` or `html`), in lower case without its
|
|
103
|
+
* dot; `""` where a path has none. A name that starts with a dot and has no other (`.epub`) is taken as an extension.
|
|
104
|
+
*/
|
|
105
|
+
export function extensionOf(pathOrExtension) {
|
|
106
|
+
const name = pathOrExtension.slice(Math.max(pathOrExtension.lastIndexOf("/"), pathOrExtension.lastIndexOf("\\")) + 1);
|
|
107
|
+
const dot = name.lastIndexOf(".");
|
|
108
|
+
if (name === pathOrExtension && dot <= 0)
|
|
109
|
+
return (dot === 0 ? name.slice(1) : name).toLowerCase();
|
|
110
|
+
return dot <= 0 ? "" : name.slice(dot + 1).toLowerCase();
|
|
111
|
+
}
|
|
112
|
+
/** The media type of a path or an extension, or undefined where the table does not hold it. */
|
|
113
|
+
export function mediaType(pathOrExtension) {
|
|
114
|
+
const ext = extensionOf(pathOrExtension);
|
|
115
|
+
return Object.hasOwn(MEDIA_TYPES, ext) ? MEDIA_TYPES[ext] : undefined;
|
|
116
|
+
}
|
|
117
|
+
/** What a `Content-Type` header says for a path or an extension: its media type, with `; charset=UTF-8` for text; `application/octet-stream` where it is not known. */
|
|
118
|
+
export function contentType(pathOrExtension) {
|
|
119
|
+
const type = mediaType(pathOrExtension) ?? "application/octet-stream";
|
|
120
|
+
return isTextual(type) ? `${type}; charset=UTF-8` : type;
|
|
121
|
+
}
|
package/dist/mod.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats, the file and wire formats of Intellectual Frontiers Commons, read and written in pure code with no outside package (its specification 0001-formats): JSON5, YAML, TOML, XML, CSV, base64 and hexadecimal,
|
|
3
|
+
* ASN.1 DER (as `der`), CBOR (as `cbor`), archives (tar, gzip, zip), PNG and ICO pictures (as `png`), TypeScript literals, glob patterns (as `glob`), semantic versions, media types and HTTP headers. It
|
|
4
|
+
* depends on `core` alone, names no host global and runs wherever ECMAScript does: Bun, Node 26, a browser, a Worker.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { parseYaml, toJson5 } from "@intellectual-frontiers/formats";
|
|
9
|
+
* const doc = parseYaml("name: commons\nlevels: [L0, L1]\n");
|
|
10
|
+
* doc.kind === "ok" ? toJson5(doc.value) : doc.error;
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
export { ArchiveFailure, concat, crc32, deflateRaw, dosDate, filesOf, formatArchiveError, gunzip, gzip, isSafePath, readTarStream, sortedPaths, splitName, tar, tarHeader, untar, untarStream, unzip, zip, ZIP_EPOCH, zipDeflated, zipEntries, zipRead, } from "./archive.js";
|
|
16
|
+
export type { ArchiveError, Files, TarEntry, TarHeader, TarStreamEntry, ZipEntry } from "./archive.js";
|
|
17
|
+
export { fromBase64, fromBase64Url, toBase64, toBase64Url } from "./base64.js";
|
|
18
|
+
export * as cbor from "./cbor.js";
|
|
19
|
+
export { csvField, formatCsvError, parseCsv, parseCsvRecords, toCsv } from "./csv.js";
|
|
20
|
+
export type { CsvError, CsvOptions } from "./csv.js";
|
|
21
|
+
export * as der from "./der.js";
|
|
22
|
+
export * as glob from "./glob.js";
|
|
23
|
+
export { entityTag, entityTags, ifMatch, ifNoneMatch, isSafeMethod, nextLink, parseLinkHeader, retryAfterMs, SAFE_METHODS, } from "./http.js";
|
|
24
|
+
export type { WebLink } from "./http.js";
|
|
25
|
+
export { formatJson5Error, isBareKey, isJsonObject, parseJson5, toJson5 } from "./json5.js";
|
|
26
|
+
export type { Json, Json5Error, JsonObject } from "./json5.js";
|
|
27
|
+
export { contentType, extensionOf, isTextual, MEDIA_TYPES, mediaType } from "./media-types.js";
|
|
28
|
+
export { atLeast, bump, bumpSuffices, compareDotted, compareSemVer, dottedParts, formatRange, formatSemVer, maxSatisfying, parseRange, parseSemVer, satisfies, versionIn, } from "./semver.js";
|
|
29
|
+
export type { Change, Comparator, Level, Operator, Range, SatisfiesOptions, SemVer } from "./semver.js";
|
|
30
|
+
export { fromHex, toHex } from "./hex.js";
|
|
31
|
+
export * as png from "./png.js";
|
|
32
|
+
export { formatTomlError, isTomlDateTime, isTomlTable, parseToml, tomlKey, tomlString, toToml } from "./toml.js";
|
|
33
|
+
export type { TomlDateTime, TomlError, TomlTable, TomlValue } from "./toml.js";
|
|
34
|
+
export { BARE_KEY, exportedLiteral, parseTsLiteral, tsKey, tsLiteral } from "./ts-literal.js";
|
|
35
|
+
export type { Literal, LiteralError, LiteralShape } from "./ts-literal.js";
|
|
36
|
+
export { attribute, child, children, descendants, el, escapeXml, formatXmlError, leaf, localName, readXml, textOf, writeNode, writeXml, } from "./xml.js";
|
|
37
|
+
export { formatYamlError, isPlainSafe, parseYaml, resolveCore, toYaml } from "./yaml.js";
|
|
38
|
+
export type { YamlError, YamlOptions } from "./yaml.js";
|
|
39
|
+
export type { ReadXmlOptions, XmlChild, XmlElement, XmlError, XmlNode, XmlText } from "./xml.js";
|
package/dist/mod.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Spec: 0001-formats FR-020
|
|
2
|
+
/**
|
|
3
|
+
* Formats, the file and wire formats of Intellectual Frontiers Commons, read and written in pure code with no outside package (its specification 0001-formats): JSON5, YAML, TOML, XML, CSV, base64 and hexadecimal,
|
|
4
|
+
* ASN.1 DER (as `der`), CBOR (as `cbor`), archives (tar, gzip, zip), PNG and ICO pictures (as `png`), TypeScript literals, glob patterns (as `glob`), semantic versions, media types and HTTP headers. It
|
|
5
|
+
* depends on `core` alone, names no host global and runs wherever ECMAScript does: Bun, Node 26, a browser, a Worker.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { parseYaml, toJson5 } from "@intellectual-frontiers/formats";
|
|
10
|
+
* const doc = parseYaml("name: commons\nlevels: [L0, L1]\n");
|
|
11
|
+
* doc.kind === "ok" ? toJson5(doc.value) : doc.error;
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
export { ArchiveFailure, concat, crc32, deflateRaw, dosDate, filesOf, formatArchiveError, gunzip, gzip, isSafePath, readTarStream, sortedPaths, splitName, tar, tarHeader, untar, untarStream, unzip, zip, ZIP_EPOCH, zipDeflated, zipEntries, zipRead, } from "./archive.js";
|
|
17
|
+
export { fromBase64, fromBase64Url, toBase64, toBase64Url } from "./base64.js";
|
|
18
|
+
export * as cbor from "./cbor.js";
|
|
19
|
+
export { csvField, formatCsvError, parseCsv, parseCsvRecords, toCsv } from "./csv.js";
|
|
20
|
+
export * as der from "./der.js";
|
|
21
|
+
export * as glob from "./glob.js";
|
|
22
|
+
export { entityTag, entityTags, ifMatch, ifNoneMatch, isSafeMethod, nextLink, parseLinkHeader, retryAfterMs, SAFE_METHODS, } from "./http.js";
|
|
23
|
+
export { formatJson5Error, isBareKey, isJsonObject, parseJson5, toJson5 } from "./json5.js";
|
|
24
|
+
export { contentType, extensionOf, isTextual, MEDIA_TYPES, mediaType } from "./media-types.js";
|
|
25
|
+
export { atLeast, bump, bumpSuffices, compareDotted, compareSemVer, dottedParts, formatRange, formatSemVer, maxSatisfying, parseRange, parseSemVer, satisfies, versionIn, } from "./semver.js";
|
|
26
|
+
export { fromHex, toHex } from "./hex.js";
|
|
27
|
+
export * as png from "./png.js";
|
|
28
|
+
export { formatTomlError, isTomlDateTime, isTomlTable, parseToml, tomlKey, tomlString, toToml } from "./toml.js";
|
|
29
|
+
export { BARE_KEY, exportedLiteral, parseTsLiteral, tsKey, tsLiteral } from "./ts-literal.js";
|
|
30
|
+
export { attribute, child, children, descendants, el, escapeXml, formatXmlError, leaf, localName, readXml, textOf, writeNode, writeXml, } from "./xml.js";
|
|
31
|
+
export { formatYamlError, isPlainSafe, parseYaml, resolveCore, toYaml } from "./yaml.js";
|