-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
utils.js
112 lines (93 loc) 路 2.74 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { DOMParser } from "./deps/dom.js";
import { SEP } from "./deps/path.js";
export async function concurrent(iterable, iteratorFn, limit = 200) {
const executing = [];
for await (const item of iterable) {
const p = iteratorFn(item).then(() =>
executing.splice(executing.indexOf(p), 1)
);
executing.push(p);
if (executing.length >= limit) {
await Promise.race(executing);
}
}
await Promise.all(executing);
}
export const mimes = new Map([
[".apng", "image/apng"],
[".atom", "application/atom+xml; charset=utf-8"],
[".avif", "image/avif"],
[".css", "text/css; charset=utf-8"],
[".gif", "image/gif"],
[".html", "text/html; charset=utf-8"],
[".ico", "image/x-icon"],
[".jpeg", "image/jpg"],
[".jpg", "image/jpg"],
[".js", "text/javascript; charset=utf-8"],
[".json", "application/json"],
[".map", "application/json"],
[".mp3", "audio/mpeg"],
[".mp4", "video/mp4"],
[".ogg", "audio/ogg"],
[".otf", "font/otf"],
[".pdf", "application/pdf"],
[".png", "image/png"],
[".rss", "application/rss+xml; charset=utf-8"],
[".svg", "image/svg+xml; charset=utf-8"],
[".ttf", "font/ttf"],
[".txt", "text/plain; charset=utf-8"],
[".wasm", "application/wasm"],
[".webm", "video/webm"],
[".webmanifest", "application/manifest+json"],
[".webp", "image/webp"],
[".woff", "font/woff"],
[".woff2", "font/woff2"],
[".xml", "text/xml"],
[".zip", "application/zip"],
]);
export function merge(defaults, user) {
const merged = { ...defaults };
if (!user) {
return merged;
}
for (const [key, value] of Object.entries(user)) {
if (isPlainObject(merged[key]) && isPlainObject(value)) {
merged[key] = merge(merged[key], value);
continue;
}
merged[key] = value;
}
return merged;
}
function isPlainObject(obj) {
return typeof obj === "object" && obj.toString() === "[object Object]";
}
export function normalizePath(path) {
return SEP === "/" ? path : path.replaceAll(SEP, "/");
}
export function searchByExtension(path, extensions) {
for (const [key, value] of extensions) {
if (path.endsWith(key)) {
return [key, value];
}
}
}
export function documentToString(document) {
const { doctype } = document;
return `<!DOCTYPE ${doctype.name}` +
(doctype.publicId ? ` PUBLIC "${doctype.publicId}"` : "") +
(!doctype.publicId && doctype.systemId ? " SYSTEM" : "") +
(doctype.systemId ? ` "${doctype.systemId}"` : "") +
`>\n${document.documentElement.outerHTML}`;
}
const parser = new DOMParser();
export function stringToDocument(string) {
return parser.parseFromString(string, "text/html");
}
export class Exception extends Error {
constructor(message, data, error) {
super(message);
this.data = data;
this.error = error;
}
}