@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
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
|
|
18
|
+
/** The media type of each extension (lower case, without its dot). */
|
|
19
|
+
export const MEDIA_TYPES: Readonly<Record<string, string>> = {
|
|
20
|
+
html: "text/html",
|
|
21
|
+
htm: "text/html",
|
|
22
|
+
xhtml: "application/xhtml+xml",
|
|
23
|
+
css: "text/css",
|
|
24
|
+
js: "text/javascript",
|
|
25
|
+
mjs: "text/javascript",
|
|
26
|
+
cjs: "text/javascript",
|
|
27
|
+
ts: "text/typescript",
|
|
28
|
+
map: "application/json",
|
|
29
|
+
json: "application/json",
|
|
30
|
+
jsonld: "application/ld+json",
|
|
31
|
+
webmanifest: "application/manifest+json",
|
|
32
|
+
xml: "application/xml",
|
|
33
|
+
xsl: "application/xslt+xml",
|
|
34
|
+
rss: "application/rss+xml",
|
|
35
|
+
atom: "application/atom+xml",
|
|
36
|
+
opf: "application/oebps-package+xml",
|
|
37
|
+
ncx: "application/x-dtbncx+xml",
|
|
38
|
+
txt: "text/plain",
|
|
39
|
+
text: "text/plain",
|
|
40
|
+
md: "text/markdown",
|
|
41
|
+
markdown: "text/markdown",
|
|
42
|
+
adoc: "text/asciidoc",
|
|
43
|
+
csv: "text/csv",
|
|
44
|
+
tsv: "text/tab-separated-values",
|
|
45
|
+
ics: "text/calendar",
|
|
46
|
+
vtt: "text/vtt",
|
|
47
|
+
yaml: "application/yaml",
|
|
48
|
+
yml: "application/yaml",
|
|
49
|
+
toml: "application/toml",
|
|
50
|
+
wasm: "application/wasm",
|
|
51
|
+
pdf: "application/pdf",
|
|
52
|
+
epub: "application/epub+zip",
|
|
53
|
+
zip: "application/zip",
|
|
54
|
+
gz: "application/gzip",
|
|
55
|
+
tgz: "application/gzip",
|
|
56
|
+
tar: "application/x-tar",
|
|
57
|
+
xz: "application/x-xz",
|
|
58
|
+
zst: "application/zstd",
|
|
59
|
+
"7z": "application/x-7z-compressed",
|
|
60
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
61
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
62
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
63
|
+
odt: "application/vnd.oasis.opendocument.text",
|
|
64
|
+
rtf: "application/rtf",
|
|
65
|
+
png: "image/png",
|
|
66
|
+
apng: "image/apng",
|
|
67
|
+
jpg: "image/jpeg",
|
|
68
|
+
jpeg: "image/jpeg",
|
|
69
|
+
gif: "image/gif",
|
|
70
|
+
webp: "image/webp",
|
|
71
|
+
avif: "image/avif",
|
|
72
|
+
svg: "image/svg+xml",
|
|
73
|
+
ico: "image/vnd.microsoft.icon",
|
|
74
|
+
bmp: "image/bmp",
|
|
75
|
+
tif: "image/tiff",
|
|
76
|
+
tiff: "image/tiff",
|
|
77
|
+
woff: "font/woff",
|
|
78
|
+
woff2: "font/woff2",
|
|
79
|
+
ttf: "font/ttf",
|
|
80
|
+
otf: "font/otf",
|
|
81
|
+
mp3: "audio/mpeg",
|
|
82
|
+
m4a: "audio/mp4",
|
|
83
|
+
ogg: "audio/ogg",
|
|
84
|
+
oga: "audio/ogg",
|
|
85
|
+
opus: "audio/opus",
|
|
86
|
+
wav: "audio/wav",
|
|
87
|
+
flac: "audio/flac",
|
|
88
|
+
mp4: "video/mp4",
|
|
89
|
+
m4v: "video/mp4",
|
|
90
|
+
webm: "video/webm",
|
|
91
|
+
ogv: "video/ogg",
|
|
92
|
+
mov: "video/quicktime",
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** Whether text of a media type is read as characters, and so served with a charset. */
|
|
96
|
+
export function isTextual(type: string): boolean {
|
|
97
|
+
return (
|
|
98
|
+
type.startsWith("text/") ||
|
|
99
|
+
/^application\/(?:json|ld\+json|manifest\+json|xml|xhtml\+xml|xslt\+xml|rss\+xml|atom\+xml|yaml|toml|javascript)$/.test(type) ||
|
|
100
|
+
type === "image/svg+xml" ||
|
|
101
|
+
type.endsWith("+xml") ||
|
|
102
|
+
type.endsWith("+json")
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 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
|
|
108
|
+
* dot; `""` where a path has none. A name that starts with a dot and has no other (`.epub`) is taken as an extension.
|
|
109
|
+
*/
|
|
110
|
+
export function extensionOf(pathOrExtension: string): string {
|
|
111
|
+
const name = pathOrExtension.slice(Math.max(pathOrExtension.lastIndexOf("/"), pathOrExtension.lastIndexOf("\\")) + 1);
|
|
112
|
+
const dot = name.lastIndexOf(".");
|
|
113
|
+
if (name === pathOrExtension && dot <= 0) return (dot === 0 ? name.slice(1) : name).toLowerCase();
|
|
114
|
+
return dot <= 0 ? "" : name.slice(dot + 1).toLowerCase();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The media type of a path or an extension, or undefined where the table does not hold it. */
|
|
118
|
+
export function mediaType(pathOrExtension: string): string | undefined {
|
|
119
|
+
const ext = extensionOf(pathOrExtension);
|
|
120
|
+
return Object.hasOwn(MEDIA_TYPES, ext) ? MEDIA_TYPES[ext] : undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 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. */
|
|
124
|
+
export function contentType(pathOrExtension: string): string {
|
|
125
|
+
const type = mediaType(pathOrExtension) ?? "application/octet-stream";
|
|
126
|
+
return isTextual(type) ? `${type}; charset=UTF-8` : type;
|
|
127
|
+
}
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
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 {
|
|
17
|
+
ArchiveFailure,
|
|
18
|
+
concat,
|
|
19
|
+
crc32,
|
|
20
|
+
deflateRaw,
|
|
21
|
+
dosDate,
|
|
22
|
+
filesOf,
|
|
23
|
+
formatArchiveError,
|
|
24
|
+
gunzip,
|
|
25
|
+
gzip,
|
|
26
|
+
isSafePath,
|
|
27
|
+
readTarStream,
|
|
28
|
+
sortedPaths,
|
|
29
|
+
splitName,
|
|
30
|
+
tar,
|
|
31
|
+
tarHeader,
|
|
32
|
+
untar,
|
|
33
|
+
untarStream,
|
|
34
|
+
unzip,
|
|
35
|
+
zip,
|
|
36
|
+
ZIP_EPOCH,
|
|
37
|
+
zipDeflated,
|
|
38
|
+
zipEntries,
|
|
39
|
+
zipRead,
|
|
40
|
+
} from "./archive.ts";
|
|
41
|
+
export type { ArchiveError, Files, TarEntry, TarHeader, TarStreamEntry, ZipEntry } from "./archive.ts";
|
|
42
|
+
export { fromBase64, fromBase64Url, toBase64, toBase64Url } from "./base64.ts";
|
|
43
|
+
export * as cbor from "./cbor.ts";
|
|
44
|
+
export { csvField, formatCsvError, parseCsv, parseCsvRecords, toCsv } from "./csv.ts";
|
|
45
|
+
export type { CsvError, CsvOptions } from "./csv.ts";
|
|
46
|
+
export * as der from "./der.ts";
|
|
47
|
+
export * as glob from "./glob.ts";
|
|
48
|
+
export {
|
|
49
|
+
entityTag,
|
|
50
|
+
entityTags,
|
|
51
|
+
ifMatch,
|
|
52
|
+
ifNoneMatch,
|
|
53
|
+
isSafeMethod,
|
|
54
|
+
nextLink,
|
|
55
|
+
parseLinkHeader,
|
|
56
|
+
retryAfterMs,
|
|
57
|
+
SAFE_METHODS,
|
|
58
|
+
} from "./http.ts";
|
|
59
|
+
export type { WebLink } from "./http.ts";
|
|
60
|
+
export { formatJson5Error, isBareKey, isJsonObject, parseJson5, toJson5 } from "./json5.ts";
|
|
61
|
+
export type { Json, Json5Error, JsonObject } from "./json5.ts";
|
|
62
|
+
export { contentType, extensionOf, isTextual, MEDIA_TYPES, mediaType } from "./media-types.ts";
|
|
63
|
+
export {
|
|
64
|
+
atLeast,
|
|
65
|
+
bump,
|
|
66
|
+
bumpSuffices,
|
|
67
|
+
compareDotted,
|
|
68
|
+
compareSemVer,
|
|
69
|
+
dottedParts,
|
|
70
|
+
formatRange,
|
|
71
|
+
formatSemVer,
|
|
72
|
+
maxSatisfying,
|
|
73
|
+
parseRange,
|
|
74
|
+
parseSemVer,
|
|
75
|
+
satisfies,
|
|
76
|
+
versionIn,
|
|
77
|
+
} from "./semver.ts";
|
|
78
|
+
export type { Change, Comparator, Level, Operator, Range, SatisfiesOptions, SemVer } from "./semver.ts";
|
|
79
|
+
export { fromHex, toHex } from "./hex.ts";
|
|
80
|
+
export * as png from "./png.ts";
|
|
81
|
+
export { formatTomlError, isTomlDateTime, isTomlTable, parseToml, tomlKey, tomlString, toToml } from "./toml.ts";
|
|
82
|
+
export type { TomlDateTime, TomlError, TomlTable, TomlValue } from "./toml.ts";
|
|
83
|
+
export { BARE_KEY, exportedLiteral, parseTsLiteral, tsKey, tsLiteral } from "./ts-literal.ts";
|
|
84
|
+
export type { Literal, LiteralError, LiteralShape } from "./ts-literal.ts";
|
|
85
|
+
export {
|
|
86
|
+
attribute,
|
|
87
|
+
child,
|
|
88
|
+
children,
|
|
89
|
+
descendants,
|
|
90
|
+
el,
|
|
91
|
+
escapeXml,
|
|
92
|
+
formatXmlError,
|
|
93
|
+
leaf,
|
|
94
|
+
localName,
|
|
95
|
+
readXml,
|
|
96
|
+
textOf,
|
|
97
|
+
writeNode,
|
|
98
|
+
writeXml,
|
|
99
|
+
} from "./xml.ts";
|
|
100
|
+
export { formatYamlError, isPlainSafe, parseYaml, resolveCore, toYaml } from "./yaml.ts";
|
|
101
|
+
export type { YamlError, YamlOptions } from "./yaml.ts";
|
|
102
|
+
export type { ReadXmlOptions, XmlChild, XmlElement, XmlError, XmlNode, XmlText } from "./xml.ts";
|