/
utils.ts
355 lines (296 loc) Β· 9.4 KB
/
utils.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { DOMParser, HTMLDocument } from "../deps/dom.ts";
import { posix, SEP, toFileUrl } from "../deps/path.ts";
export const baseUrl = new URL("../", import.meta.url);
// TODO: Remove this once Deno.ImportMap is available
interface ImportMap {
imports: Record<string, string>;
scopes?: Record<string, Record<string, string>>;
}
/** Run a callback concurrently with all the elements of an Iterable */
export async function concurrent<Type>(
iterable: AsyncIterable<Type> | Iterable<Type>,
iteratorFn: (arg: Type) => Promise<unknown>,
limit = 200,
) {
const executing: Promise<unknown>[] = [];
for await (const item of iterable) {
const p: Promise<unknown> = iteratorFn(item).then(() =>
executing.splice(executing.indexOf(p), 1)
);
executing.push(p);
if (executing.length >= limit) {
await Promise.race(executing);
}
}
await Promise.all(executing);
}
const decoder = new TextDecoder();
const encoder = new TextEncoder();
/** Encode a message using SHA-1 algorithm */
export async function sha1(message: string | Uint8Array): Promise<string> {
if (typeof message === "string") {
message = encoder.encode(message);
}
const hash = await crypto.subtle.digest("SHA-1", message);
return decoder.decode(hash);
}
/**
* The list of supported MIME types.
* It's used by the server and some plugins.
*/
export const mimes = new Map<string, string>([
[".aac", "audio/x-aac"],
[".apng", "image/apng"],
[".atom", "application/atom+xml; charset=utf-8"],
[".avif", "image/avif"],
[".bmp", "image/bmp"],
[".css", "text/css; charset=utf-8"],
[".es", "application/ecmascript"],
[".eps", "application/postscript"],
[".epub", "application/epub+zip"],
[".flac", "audio/x-flac"],
[".gif", "image/gif"],
[".gz", "aplication/gzip"],
[".heic", "image/heic"],
[".heif", "image/heif"],
[".html", "text/html; charset=utf-8"],
[".htm", "text/html; charset=utf-8"],
[".ico", "image/x-icon"],
[".jpeg", "image/jpg"],
[".jpg", "image/jpg"],
[".js", "text/javascript; charset=utf-8"],
[".json", "application/json"],
[".kml", "application/vnd.google-earth.kml+xml"],
[".kmz", "application/vnd.google-earth.kmz"],
[".map", "application/json"],
[".md", "text/markdown; charset=utf-8"],
[".mid", "audio/midi"],
[".midi", "audio/midi"],
[".mjs", "application/javascript"],
[".mkv", "video/x-matroska"],
[".mov", "video/quicktime"],
[".mp3", "audio/mp3"],
[".mp4", "video/mp4"],
[".mp4a", "video/mp4"],
[".mp4v", "video/mp4"],
[".m4a", "video/mp4"],
[".ogg", "audio/ogg"],
[".opus", "audio/ogg"],
[".otf", "font/otf"],
[".pdf", "application/pdf"],
[".png", "image/png"],
[".ps", "application/postscript"],
[".rar", "application/vnd.rar"],
[".rdf", "application/rdf+xml; charset=utf-8"],
[".rss", "application/rss+xml; charset=utf-8"],
[".rtf", "application/rtf"],
[".svg", "image/svg+xml; charset=utf-8"],
[".tiff", "image/tiff"],
[".ttf", "font/ttf"],
[".txt", "text/plain; charset=utf-8"],
[".vtt", "text/vtt; charset=utf-8"],
[".wasm", "application/wasm"],
[".wav", "audio/wav"],
[".webm", "video/webm"],
[".webmanifest", "application/manifest+json"],
[".webp", "image/webp"],
[".woff", "font/woff"],
[".woff2", "font/woff2"],
[".yaml", "text/yaml; charset=utf-8"],
[".yml", "text/yaml; charset=utf-8"],
[".xml", "text/xml"],
[".zip", "application/zip"],
]);
/**
* Merge two objects recursively.
* It's used to merge user options with default options.
*/
export function merge<Type>(
defaults: Type,
user?: Partial<Type>,
) {
const merged = { ...defaults };
if (!user) {
return merged;
}
for (const [key, value] of Object.entries(user)) {
// @ts-ignore: No index signature with a parameter of type 'string' was found on type 'unknown'
if (isPlainObject(merged[key]) && isPlainObject(value)) {
// @ts-ignore: Type 'string' cannot be used to index type 'Type'
merged[key] = merge(merged[key], value);
continue;
}
// @ts-ignore: Type 'string' cannot be used to index type 'Type'
merged[key] = value;
}
return merged;
}
const reactElement = Symbol.for("react.element");
/** Check if the argument passed is a plain object */
export function isPlainObject(obj: unknown) {
return typeof obj === "object" && obj !== null && !Array.isArray(obj) &&
obj.toString() === "[object Object]" &&
// @ts-ignore: Check if the argument passed is a React element
obj["$$typeof"] !== reactElement;
}
/**
* Convert the Windows paths (that use the separator "\")
* to Posix paths (with the separator "/")
* and ensure it starts with "/".
*/
export function normalizePath(path: string) {
if (SEP !== "/") {
path = path.replaceAll(SEP, "/");
// Is absolute Windows path (C:/...)
if (path.includes(":/")) {
return path;
}
}
return posix.join("/", path);
}
/** Convert an HTMLDocument instance to a string */
export function documentToString(document: HTMLDocument) {
const { doctype, documentElement } = document;
if (!doctype) {
return documentElement?.outerHTML || "";
}
return `<!DOCTYPE ${doctype.name}` +
(doctype.publicId ? ` PUBLIC "${doctype.publicId}"` : "") +
(!doctype.publicId && doctype.systemId ? " SYSTEM" : "") +
(doctype.systemId ? ` "${doctype.systemId}"` : "") +
`>\n${documentElement?.outerHTML}`;
}
const parser = new DOMParser();
/** Parse a string with HTML code and return an HTMLDocument */
export function stringToDocument(string: string): HTMLDocument {
const document = parser.parseFromString(string, "text/html");
if (!document) {
throw new Error("Unable to parse the HTML code");
}
return document;
}
/** Return the current installed version */
export function getLumeVersion(url = baseUrl): string {
const { pathname } = url;
return pathname.match(/@([^/]+)/)?.[1] ?? `local (${pathname})`;
}
/** Return the latest stable version from the deno.land/x repository */
export async function getLatestVersion(): Promise<string> {
const response = await fetch("https://cdn.deno.land/lume/meta/versions.json");
const versions = await response.json();
return versions.latest;
}
/** Return the hash of the latest commit from the GitHub repository */
export async function getLatestDevelopmentVersion(): Promise<string> {
const response = await fetch(
"https://api.github.com/repos/lumeland/lume/commits?per_page=1",
);
const commits = await response.json();
return commits[0].sha;
}
export interface UpgradeInfo {
current: string;
latest: string;
command: string;
}
export async function mustNotifyUpgrade(): Promise<undefined | UpgradeInfo> {
const current = getLumeVersion();
// It's a local version
if (current.startsWith("local ")) {
return;
}
const stable = !!current.match(/^v\d+\./);
const expires = 1000 * 60 * 60 * 24; // 1 day
const interval = localStorage.getItem("lume-upgrade");
if (interval && parseInt(interval) + expires > Date.now()) {
return;
}
localStorage.setItem("lume-upgrade", Date.now().toString());
const latest = stable
? await getLatestVersion()
: await getLatestDevelopmentVersion();
if (current === latest) {
return;
}
const command = stable ? "lume upgrade" : "lume upgrade --dev";
return { current, latest, command };
}
/** Basic options for deno.json file */
export interface DenoConfig {
importMap?: string;
tasks?: Record<string, string>;
[key: string]: unknown;
}
export async function getDenoConfig(): Promise<DenoConfig | undefined> {
try {
const content = await Deno.readTextFile("deno.json");
return JSON.parse(content) as DenoConfig;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return;
}
throw err;
}
}
export async function loadImportMap(url: URL): Promise<ImportMap> {
return await (await fetch(url)).json() as ImportMap;
}
/**
* Return a data url with the import map of Lume
* Optionally merge it with a custom import map from the user
*/
export async function getImportMap(mapFile?: string): Promise<ImportMap> {
const map: ImportMap = {
imports: {
"lume/": new URL("./", baseUrl).href,
},
};
if (mapFile) {
const importMap = await loadImportMap(await toUrl(mapFile));
map.imports = { ...importMap.imports, ...map.imports };
map.scopes = importMap.scopes;
}
return map;
}
export type SpecifierMap = Record<string, string>;
/** Check the compatibility with the current Deno version */
export interface DenoInfo {
current: string;
minimum: string;
command: string;
}
export function checkDenoVersion(): DenoInfo | undefined {
const minimum = "1.20.1";
const current = Deno.version.deno;
if (current < minimum) {
return { current, minimum, command: "deno upgrade" };
}
}
export async function toUrl(path: string): Promise<URL> {
if (path.match(/https?:\/\//)) {
return new URL(path);
}
return toFileUrl(await Deno.realPath(path));
}
export function createDate(str: string | number): Date | undefined {
if (typeof str === "number") {
return new Date(str);
}
const datetime = str.match(
/^(\d{4})-(\d\d)-(\d\d)(?:-(\d\d)-(\d\d)(?:-(\d\d))?)?$/,
);
if (datetime) {
const [, year, month, day, hour, minute, second] = datetime;
return new Date(Date.UTC(
parseInt(year),
parseInt(month) - 1,
parseInt(day),
hour ? parseInt(hour) : 0,
minute ? parseInt(minute) : 0,
second ? parseInt(second) : 0,
));
}
if (str.match(/^\d+$/)) {
return new Date(parseInt(str));
}
}