@uxfront/site-checks 0.1.0 → 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/README.md +19 -19
- package/bin/site-checks.js +3 -3
- package/index.d.ts +260 -259
- package/lib/check.js +222 -196
- package/lib/cli.js +59 -56
- package/lib/html.js +166 -137
- package/lib/rules.js +1194 -1046
- package/lib/site.js +534 -501
- package/lib/validate.js +140 -117
- package/lighthouse.d.ts +26 -24
- package/lighthouse.js +52 -48
- package/package.json +2 -2
package/lib/site.js
CHANGED
|
@@ -2,14 +2,14 @@ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
|
2
2
|
import { join, sep } from "node:path";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
countH1,
|
|
6
|
+
decoder,
|
|
7
|
+
escapeRegExp,
|
|
8
|
+
jsonLdBodies,
|
|
9
|
+
jsonLdNodes,
|
|
10
|
+
scoped,
|
|
11
|
+
tags,
|
|
12
|
+
titleTexts,
|
|
13
13
|
} from "./html.js";
|
|
14
14
|
|
|
15
15
|
/** @import { CheckedPage, IndexSource, ParseProfile, PathPattern, Scope, SitemapEntry } from "../index.js" */
|
|
@@ -29,137 +29,140 @@ import {
|
|
|
29
29
|
* @implements {CheckedPage}
|
|
30
30
|
*/
|
|
31
31
|
export class Page {
|
|
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
|
-
|
|
32
|
+
/** @type {Map<string, unknown>} */
|
|
33
|
+
#cache = new Map();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {{ loc: string, path: string, file: string, html: string, profile: Profile, decode: (text: string) => string }} input
|
|
37
|
+
*/
|
|
38
|
+
constructor({ loc, path, file, html, profile, decode }) {
|
|
39
|
+
this.loc = loc;
|
|
40
|
+
this.path = path;
|
|
41
|
+
this.file = file;
|
|
42
|
+
this.html = html;
|
|
43
|
+
this.profile = profile;
|
|
44
|
+
this.decode = decode;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @template T
|
|
49
|
+
* @param {string} key
|
|
50
|
+
* @param {() => T} read
|
|
51
|
+
* @returns {T}
|
|
52
|
+
*/
|
|
53
|
+
#once(key, read) {
|
|
54
|
+
if (!this.#cache.has(key)) {
|
|
55
|
+
this.#cache.set(key, read());
|
|
56
|
+
}
|
|
57
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- each key has one reader and one T
|
|
58
|
+
return /** @type {T} */ (this.#cache.get(key));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Every tag of this name in a scope, the profile's by default. @param {string} name @param {Scope} [scope] */
|
|
62
|
+
tags(name, scope = this.profile.scope) {
|
|
63
|
+
return this.#once(`tags:${scope}:${name}`, () =>
|
|
64
|
+
tags(scoped(this.html, scope), name, this.profile, this.decode),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Every tag of this name in the whole document. @param {string} name */
|
|
69
|
+
documentTags(name) {
|
|
70
|
+
return this.tags(name, "document");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A value as the profile reads it, trimmed when `trimValues` says so. @param {string} value */
|
|
74
|
+
#value(value) {
|
|
75
|
+
return this.profile.trimValues ? value.trim() : value;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The `content` of every `<meta>` whose key is `key`. With `property-first`, a tag's key is its
|
|
80
|
+
* `property`, or else its `name`, and a tag with no `content` is left out.
|
|
81
|
+
* @param {string} key
|
|
82
|
+
* @param {Scope} [scope]
|
|
83
|
+
*/
|
|
84
|
+
meta(key, scope) {
|
|
85
|
+
const metas = this.tags("meta", scope);
|
|
86
|
+
const found =
|
|
87
|
+
this.profile.metaKey === "property-first"
|
|
88
|
+
? metas.filter((tag) => (tag.property ?? tag.name) === key && tag.content !== undefined)
|
|
89
|
+
: metas.filter((tag) => tag.name === key || tag.property === key);
|
|
90
|
+
return found.map((tag) => this.#value(tag.content ?? ""));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The first value of a `<meta>`, or `undefined`. @param {string} key */
|
|
94
|
+
first(key) {
|
|
95
|
+
return this.meta(key)[0];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Whether a `<meta>` is set, to something other than spaces. @param {string} key */
|
|
99
|
+
has(key) {
|
|
100
|
+
return Boolean(this.first(key)?.trim());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get titles() {
|
|
104
|
+
return this.#once("titles", () => {
|
|
105
|
+
const { titleScope, trimTitle } = this.profile;
|
|
106
|
+
return titleTexts(scoped(this.html, titleScope), this.profile).map((title) =>
|
|
107
|
+
trimTitle === "none"
|
|
108
|
+
? this.decode(title)
|
|
109
|
+
: trimTitle === "before-decode"
|
|
110
|
+
? this.decode(title.trim())
|
|
111
|
+
: this.decode(title).trim(),
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get lang() {
|
|
117
|
+
const lang = this.documentTags("html")[0]?.lang;
|
|
118
|
+
return lang === undefined ? undefined : this.#value(lang);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
get h1() {
|
|
122
|
+
return this.#once("h1", () => countH1(this.html, this.profile));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The `href` of each canonical link: `exact` takes `rel="canonical"`, `exact-any-case` the same
|
|
127
|
+
* in any case, and `token` a `rel` with `canonical` among its words. With `requireHref`, a link
|
|
128
|
+
* with no `href` doesn't count.
|
|
129
|
+
* @param {"token" | "exact" | "exact-any-case"} rel
|
|
130
|
+
* @param {boolean} [requireHref]
|
|
131
|
+
*/
|
|
132
|
+
canonicals(rel, requireHref = false) {
|
|
133
|
+
return this.tags("link")
|
|
134
|
+
.filter((tag) =>
|
|
135
|
+
rel === "token"
|
|
136
|
+
? (tag.rel ?? "").toLowerCase().split(/\s+/).includes("canonical")
|
|
137
|
+
: rel === "exact-any-case"
|
|
138
|
+
? tag.rel?.toLowerCase() === "canonical"
|
|
139
|
+
: tag.rel === "canonical",
|
|
140
|
+
)
|
|
141
|
+
.filter((tag) => !requireHref || tag.href !== undefined)
|
|
142
|
+
.map((tag) => tag.href ?? "");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Each JSON-LD block, parsed, or the reason it doesn't parse. */
|
|
146
|
+
get blocks() {
|
|
147
|
+
return this.#once("blocks", () =>
|
|
148
|
+
jsonLdBodies(this.html, this.profile, this.decode).map((body) => {
|
|
149
|
+
try {
|
|
150
|
+
return { ok: true, value: /** @type {unknown} */ (JSON.parse(body)) };
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
153
|
+
}
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get nodes() {
|
|
159
|
+
return this.#once("nodes", () =>
|
|
160
|
+
jsonLdNodes(
|
|
161
|
+
this.blocks.flatMap((block) => (block.ok ? [block.value] : [])),
|
|
162
|
+
this.profile.nodes,
|
|
163
|
+
),
|
|
164
|
+
);
|
|
165
|
+
}
|
|
163
166
|
}
|
|
164
167
|
|
|
165
168
|
/**
|
|
@@ -168,321 +171,345 @@ export class Page {
|
|
|
168
171
|
* @param {readonly PathPattern[]} patterns
|
|
169
172
|
*/
|
|
170
173
|
export const inScope = (path, patterns) =>
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
+
patterns.some((pattern) =>
|
|
175
|
+
pattern.endsWith("*") ? path.startsWith(pattern.slice(0, -1)) : path === pattern,
|
|
176
|
+
);
|
|
174
177
|
|
|
175
178
|
/**
|
|
176
179
|
* Everything the rules read about a built site, each file read once.
|
|
177
180
|
*/
|
|
178
181
|
export class Site {
|
|
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
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
182
|
+
/** @type {Map<string, string | undefined>} */
|
|
183
|
+
#files = new Map();
|
|
184
|
+
/** @type {string[] | undefined} */
|
|
185
|
+
#html;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @param {{ site: string, dist: string, profile: Profile, paths: PathMode, now: Date }} input
|
|
189
|
+
*/
|
|
190
|
+
constructor({ site, dist, profile, paths, now }) {
|
|
191
|
+
this.site = site.replace(/\/+$/, "");
|
|
192
|
+
this.host = new URL(this.site).host;
|
|
193
|
+
this.dist = dist;
|
|
194
|
+
this.profile = profile;
|
|
195
|
+
this.paths = paths;
|
|
196
|
+
this.now = now;
|
|
197
|
+
this.decode = decoder(profile);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** A file in `dist` as text, or `undefined`. @param {string} file */
|
|
201
|
+
read(file) {
|
|
202
|
+
if (!this.#files.has(file)) {
|
|
203
|
+
const path = join(this.dist, file);
|
|
204
|
+
let text;
|
|
205
|
+
try {
|
|
206
|
+
text = statSync(path).isFile() ? readFileSync(path, "utf8") : undefined;
|
|
207
|
+
} catch {
|
|
208
|
+
text = undefined;
|
|
209
|
+
}
|
|
210
|
+
this.#files.set(file, text);
|
|
211
|
+
}
|
|
212
|
+
return this.#files.get(file);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @param {string} file */
|
|
216
|
+
exists(file) {
|
|
217
|
+
return existsSync(join(this.dist, file));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The length of a text in the profile's units. @param {string} text */
|
|
221
|
+
length(text) {
|
|
222
|
+
if (this.profile.count === "utf-16") {
|
|
223
|
+
return text.length;
|
|
224
|
+
}
|
|
225
|
+
// A surrogate pair is one code point in two UTF-16 units.
|
|
226
|
+
return text.length - (text.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g)?.length ?? 0);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Every HTML file in `dist`, with `/` between folders, in order. */
|
|
230
|
+
get htmlFiles() {
|
|
231
|
+
this.#html ??= readdirSync(this.dist, { recursive: true, encoding: "utf8" })
|
|
232
|
+
.map((file) => file.split(sep).join("/"))
|
|
233
|
+
.filter((file) => file.endsWith(".html") && isFile(join(this.dist, file)))
|
|
234
|
+
.toSorted();
|
|
235
|
+
return this.#html;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The path a file is served at, as the `paths` mode says. @param {string} file */
|
|
239
|
+
served(file) {
|
|
240
|
+
if (this.paths === "exact-slash") {
|
|
241
|
+
if (file === "index.html") {
|
|
242
|
+
return "/";
|
|
243
|
+
}
|
|
244
|
+
if (file.endsWith("/index.html")) {
|
|
245
|
+
return `/${file.slice(0, -"index.html".length)}`;
|
|
246
|
+
}
|
|
247
|
+
return `/${file.slice(0, -".html".length)}`;
|
|
248
|
+
}
|
|
249
|
+
return `/${file}`.replace(/(?:\/index)?\.html$/, "") || "/";
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
get #byPath() {
|
|
253
|
+
/** @type {Map<string, string>} */
|
|
254
|
+
const map = new Map();
|
|
255
|
+
for (const file of this.htmlFiles) {
|
|
256
|
+
const path = this.served(file);
|
|
257
|
+
// A `.html` file wins over a folder's `index.html` at the same path.
|
|
258
|
+
if (!map.has(path) || !file.endsWith("/index.html")) {
|
|
259
|
+
map.set(path, file);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return map;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** @type {Map<string, string> | undefined} */
|
|
266
|
+
#paths;
|
|
267
|
+
|
|
268
|
+
/** The file a site path is served from, or `undefined`. @param {string} path */
|
|
269
|
+
resolve(path) {
|
|
270
|
+
if (this.paths === "lookup") {
|
|
271
|
+
const trimmed = path.replaceAll(/^\/+|\/+$/g, "");
|
|
272
|
+
const file = !trimmed
|
|
273
|
+
? "index.html"
|
|
274
|
+
: path.endsWith("/") || this.read(`${trimmed}.html`) === undefined
|
|
275
|
+
? `${trimmed}/index.html`
|
|
276
|
+
: `${trimmed}.html`;
|
|
277
|
+
return this.read(file) === undefined ? undefined : file;
|
|
278
|
+
}
|
|
279
|
+
this.#paths ??= this.#byPath;
|
|
280
|
+
return this.#paths.get(path);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** @type {{ exists: boolean, entries: Entry[] } | undefined} */
|
|
284
|
+
#sitemap;
|
|
285
|
+
|
|
286
|
+
get sitemap() {
|
|
287
|
+
if (!this.#sitemap) {
|
|
288
|
+
const xml = this.read("sitemap.xml");
|
|
289
|
+
/** @param {string | undefined} text */
|
|
290
|
+
const trim = (text) => (this.profile.trimSitemap ? text?.trim() : text);
|
|
291
|
+
const entries = [...(xml ?? "").matchAll(/<url>([\s\S]*?)<\/url>/g)].map(([, body = ""]) => {
|
|
292
|
+
const raw = body.match(/<loc>([^<]*)<\/loc>/)?.[1];
|
|
293
|
+
let loc = trim(raw) ?? "";
|
|
294
|
+
let decodeError;
|
|
295
|
+
try {
|
|
296
|
+
loc = this.decode(loc);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
decodeError = error instanceof Error ? error.message : String(error);
|
|
299
|
+
}
|
|
300
|
+
let url;
|
|
301
|
+
try {
|
|
302
|
+
url = loc ? new URL(loc) : undefined;
|
|
303
|
+
} catch {
|
|
304
|
+
url = undefined;
|
|
305
|
+
}
|
|
306
|
+
/** @type {Entry} */
|
|
307
|
+
const entry = {
|
|
308
|
+
loc,
|
|
309
|
+
lastmod: trim(body.match(/<lastmod>([^<]*)<\/lastmod>/)?.[1]),
|
|
310
|
+
url,
|
|
311
|
+
hasLoc: raw !== undefined && Boolean(loc),
|
|
312
|
+
};
|
|
313
|
+
if (decodeError !== undefined) {
|
|
314
|
+
entry.error = decodeError;
|
|
315
|
+
}
|
|
316
|
+
return entry;
|
|
317
|
+
});
|
|
318
|
+
this.#sitemap = { exists: xml !== undefined, entries };
|
|
319
|
+
}
|
|
320
|
+
return this.#sitemap;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** The decoded `<loc>` values. */
|
|
324
|
+
get locs() {
|
|
325
|
+
return new Set(this.sitemap.entries.map((entry) => entry.loc));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* A page read through the profile.
|
|
330
|
+
* @param {string} loc @param {string} path @param {string} file
|
|
331
|
+
*/
|
|
332
|
+
page(loc, path, file) {
|
|
333
|
+
return new Page({
|
|
334
|
+
loc,
|
|
335
|
+
path,
|
|
336
|
+
file,
|
|
337
|
+
html: this.read(file) ?? "",
|
|
338
|
+
profile: this.profile,
|
|
339
|
+
decode: this.decode,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** @type {HeaderRule[] | undefined} */
|
|
344
|
+
#headers;
|
|
345
|
+
|
|
346
|
+
/** The rules of `_headers`, each a pattern and its headers, with lower-case names. */
|
|
347
|
+
get headers() {
|
|
348
|
+
if (!this.#headers) {
|
|
349
|
+
const text = this.read("_headers") ?? "";
|
|
350
|
+
/** @type {HeaderRule[]} */
|
|
351
|
+
const rules = [];
|
|
352
|
+
if (this.profile.headersParser === "blocks") {
|
|
353
|
+
// A rule is everything from one unindented line to the next, trimmed. A `#` line is a
|
|
354
|
+
// rule of its own, and a line that still ends in `\r` doesn't read as a header.
|
|
355
|
+
for (const block of text.split(/\n(?=\S)/)) {
|
|
356
|
+
const [pattern = "", ...lines] = block.trim().split("\n");
|
|
357
|
+
rules.push({
|
|
358
|
+
pattern,
|
|
359
|
+
headers: lines.flatMap((line) => {
|
|
360
|
+
const [, name, value = ""] = line.match(/^\s+([^:]+):\s*(.*)$/) ?? [];
|
|
361
|
+
return name ? [/** @type {[string, string]} */ ([name.toLowerCase(), value])] : [];
|
|
362
|
+
}),
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
this.#headers = rules;
|
|
366
|
+
return rules;
|
|
367
|
+
}
|
|
368
|
+
for (const line of text.split("\n")) {
|
|
369
|
+
if (!line.trim() || line.trim().startsWith("#")) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (!/^\s/.test(line)) {
|
|
373
|
+
rules.push({ pattern: line.trim(), headers: [] });
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
const at = line.indexOf(":");
|
|
377
|
+
if (at > 0 && line.slice(0, at).trim()) {
|
|
378
|
+
rules
|
|
379
|
+
.at(-1)
|
|
380
|
+
?.headers.push([line.slice(0, at).trim().toLowerCase(), line.slice(at + 1).trim()]);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
this.#headers = rules;
|
|
384
|
+
}
|
|
385
|
+
return this.#headers;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* The headers `_headers` gives a URL, as Cloudflare matches it: a rule's pattern is a path, or
|
|
390
|
+
* a URL with a host, where `*` matches anything and a `:placeholder` anything but `.` in a host
|
|
391
|
+
* or `/` in a path. A request gets the headers of every rule it matches.
|
|
392
|
+
* @param {string} url
|
|
393
|
+
* @returns {[name: string, value: string][]}
|
|
394
|
+
*/
|
|
395
|
+
headersFor(url) {
|
|
396
|
+
let host, pathname;
|
|
397
|
+
try {
|
|
398
|
+
({ host, pathname } = new URL(url, this.site));
|
|
399
|
+
} catch {
|
|
400
|
+
return [];
|
|
401
|
+
}
|
|
402
|
+
return this.headers.flatMap(({ pattern, headers }) => {
|
|
403
|
+
const match = cloudflarePattern(pattern).exec(
|
|
404
|
+
pattern.startsWith("https://") ? `${host}${pathname}` : pathname,
|
|
405
|
+
);
|
|
406
|
+
if (!match) {
|
|
407
|
+
return [];
|
|
408
|
+
}
|
|
409
|
+
/** @param {string} value */
|
|
410
|
+
const fill = (value) =>
|
|
411
|
+
value.replaceAll(
|
|
412
|
+
/:([A-Za-z]\w*)/g,
|
|
413
|
+
(placeholder, /** @type {string} */ name) => match.groups?.[name] ?? placeholder,
|
|
414
|
+
);
|
|
415
|
+
return headers.map(([name, value]) => /** @type {[string, string]} */ ([name, fill(value)]));
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** The sources of `_redirects` rules. */
|
|
420
|
+
get redirected() {
|
|
421
|
+
return new Set(
|
|
422
|
+
(this.read("_redirects") ?? "")
|
|
423
|
+
.split("\n")
|
|
424
|
+
.map((line) => line.trim().split(/\s+/)[0] ?? "")
|
|
425
|
+
.filter((source) => source && !source.startsWith("#")),
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* The first source that takes a page out of the index, or `undefined`.
|
|
431
|
+
* @param {readonly IndexSource[]} sources
|
|
432
|
+
* @param {{ file: string, path: string, host: string }} at
|
|
433
|
+
*/
|
|
434
|
+
outOfIndex(sources, { file, path, host }) {
|
|
435
|
+
for (const source of sources) {
|
|
436
|
+
const found = this.#outBy(source, file, path, host);
|
|
437
|
+
if (found) {
|
|
438
|
+
return found;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return undefined;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* @param {IndexSource} source @param {string} file @param {string} path @param {string} host
|
|
446
|
+
* @returns {string | undefined}
|
|
447
|
+
*/
|
|
448
|
+
#outBy(source, file, path, host) {
|
|
449
|
+
switch (source.type) {
|
|
450
|
+
case "files":
|
|
451
|
+
return source.files.includes(file) ? `it's ${file}` : undefined;
|
|
452
|
+
case "under": {
|
|
453
|
+
const folder = source.paths.find((prefix) => file.startsWith(prefix));
|
|
454
|
+
return folder ? `it's under ${folder}` : undefined;
|
|
455
|
+
}
|
|
456
|
+
case "redirects":
|
|
457
|
+
return this.redirected.has(path) ? "_redirects sends it elsewhere" : undefined;
|
|
458
|
+
case "refresh": {
|
|
459
|
+
const page = this.page("", path, file);
|
|
460
|
+
return page
|
|
461
|
+
.documentTags("meta")
|
|
462
|
+
.some((tag) => tag["http-equiv"]?.toLowerCase() === "refresh")
|
|
463
|
+
? "it's a meta refresh"
|
|
464
|
+
: undefined;
|
|
465
|
+
}
|
|
466
|
+
case "meta": {
|
|
467
|
+
const page = this.page("", path, file);
|
|
468
|
+
const test = valueTest(source.values, source.word, source.caseSensitive);
|
|
469
|
+
const scope = source.scope ?? this.profile.scope;
|
|
470
|
+
// With `property`, a tag is found as the profile finds any `<meta>`, by its `name` or its
|
|
471
|
+
// `property`. Without, by its `name` alone.
|
|
472
|
+
const contents = source.property
|
|
473
|
+
? source.names.flatMap((name) => page.meta(name, scope))
|
|
474
|
+
: page
|
|
475
|
+
.tags("meta", scope)
|
|
476
|
+
.filter((meta) => {
|
|
477
|
+
const name = source.anyCaseNames ? meta.name?.toLowerCase() : meta.name;
|
|
478
|
+
return name !== undefined && source.names.includes(name);
|
|
479
|
+
})
|
|
480
|
+
.map((meta) => meta.content ?? "");
|
|
481
|
+
const hit = contents.find((content) => test(content));
|
|
482
|
+
return hit === undefined ? undefined : `a robots <meta> says "${hit}"`;
|
|
483
|
+
}
|
|
484
|
+
case "headers": {
|
|
485
|
+
const test = valueTest(source.values, source.word, false);
|
|
486
|
+
for (const { pattern, headers } of this.headers) {
|
|
487
|
+
// Host rules end in `/*` and would match every path, so `ignore` reads path rules alone.
|
|
488
|
+
if (source.hosts === "ignore" && !pattern.startsWith("/")) {
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
const target =
|
|
492
|
+
source.hosts === "match" && pattern.startsWith("https://") ? `${host}${path}` : path;
|
|
493
|
+
const regexp =
|
|
494
|
+
source.hosts === "drop" ? droppedHostPattern(pattern) : cloudflarePattern(pattern);
|
|
495
|
+
if (!regexp.test(target)) {
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
const values = headers
|
|
499
|
+
.filter(([name]) => name === "x-robots-tag")
|
|
500
|
+
.map(([, value]) => value);
|
|
501
|
+
// A parser that keeps one value per header name keeps the last.
|
|
502
|
+
const read = source.hosts === "drop" ? values.slice(-1) : values;
|
|
503
|
+
const tag = read.find((value) => test(value));
|
|
504
|
+
if (tag !== undefined) {
|
|
505
|
+
return `_headers ${pattern} sends X-Robots-Tag: ${tag}`;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return undefined;
|
|
512
|
+
}
|
|
486
513
|
}
|
|
487
514
|
|
|
488
515
|
/**
|
|
@@ -491,58 +518,64 @@ export class Site {
|
|
|
491
518
|
* @param {boolean | undefined} caseSensitive
|
|
492
519
|
*/
|
|
493
520
|
function valueTest(values = ["noindex"], word = false, caseSensitive = false) {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
521
|
+
const alternatives = values.map(escapeRegExp).join("|");
|
|
522
|
+
const regexp = new RegExp(
|
|
523
|
+
word ? `\\b(?:${alternatives})\\b` : alternatives,
|
|
524
|
+
caseSensitive ? "" : "i",
|
|
525
|
+
);
|
|
526
|
+
return (/** @type {string} */ value) => regexp.test(value);
|
|
500
527
|
}
|
|
501
528
|
|
|
502
529
|
/** @type {Map<string, RegExp>} */
|
|
503
530
|
const patterns = new Map();
|
|
504
531
|
|
|
532
|
+
/**
|
|
533
|
+
* One part of a `_headers` pattern as a regular expression: `*` is a named splat, and `:name` a
|
|
534
|
+
* named group that stops at any of the delimiters.
|
|
535
|
+
* @param {string} text
|
|
536
|
+
* @param {string} delimiters
|
|
537
|
+
*/
|
|
538
|
+
const segment = (text, delimiters) =>
|
|
539
|
+
text
|
|
540
|
+
.split(/(\*|:[A-Za-z]\w*)/)
|
|
541
|
+
.map((part, index) =>
|
|
542
|
+
part === "*"
|
|
543
|
+
? "(?<splat>.*)"
|
|
544
|
+
: index % 2
|
|
545
|
+
? `(?<${part.slice(1)}>[^${delimiters}]+)`
|
|
546
|
+
: escapeRegExp(part),
|
|
547
|
+
)
|
|
548
|
+
.join("");
|
|
549
|
+
|
|
505
550
|
/**
|
|
506
551
|
* A `_headers` pattern as Cloudflare matches it: `https://host/path` against `host/path`, a path
|
|
507
552
|
* against the path. `*` is a splat, and `:name` a placeholder that stops at `.` in a host or `/`.
|
|
508
553
|
* @param {string} pattern
|
|
509
554
|
*/
|
|
510
555
|
function cloudflarePattern(pattern) {
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
// Two placeholders with one name can't both be groups: match them without names.
|
|
535
|
-
regexp = new RegExp(
|
|
536
|
-
`^${pattern
|
|
537
|
-
.replace(/^https:\/\//, "")
|
|
538
|
-
.split(/(\*|:[A-Za-z]\w*)/)
|
|
539
|
-
.map((part, index) => (part === "*" ? ".*" : index % 2 ? "[^/]+" : escapeRegExp(part)))
|
|
540
|
-
.join("")}$`,
|
|
541
|
-
);
|
|
542
|
-
}
|
|
543
|
-
patterns.set(pattern, regexp);
|
|
544
|
-
}
|
|
545
|
-
return regexp;
|
|
556
|
+
let regexp = patterns.get(pattern);
|
|
557
|
+
if (!regexp) {
|
|
558
|
+
const [, host, path] = pattern.match(/^https:\/\/([^/]+)(\/.*)$/) ?? [];
|
|
559
|
+
let source;
|
|
560
|
+
try {
|
|
561
|
+
source =
|
|
562
|
+
host !== undefined && path !== undefined
|
|
563
|
+
? `^${segment(host, "./")}${segment(path, "/")}$`
|
|
564
|
+
: `^${segment(pattern, "/")}$`;
|
|
565
|
+
regexp = new RegExp(source);
|
|
566
|
+
} catch {
|
|
567
|
+
// Two placeholders with one name can't both be groups: match them without names.
|
|
568
|
+
regexp = new RegExp(
|
|
569
|
+
`^${pattern
|
|
570
|
+
.replace(/^https:\/\//, "")
|
|
571
|
+
.split(/(\*|:[A-Za-z]\w*)/)
|
|
572
|
+
.map((part, index) => (part === "*" ? ".*" : index % 2 ? "[^/]+" : escapeRegExp(part)))
|
|
573
|
+
.join("")}$`,
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
patterns.set(pattern, regexp);
|
|
577
|
+
}
|
|
578
|
+
return regexp;
|
|
546
579
|
}
|
|
547
580
|
|
|
548
581
|
/**
|
|
@@ -550,19 +583,19 @@ function cloudflarePattern(pattern) {
|
|
|
550
583
|
* @param {string} pattern
|
|
551
584
|
*/
|
|
552
585
|
function droppedHostPattern(pattern) {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
586
|
+
const source = pattern
|
|
587
|
+
.replace(/^https?:\/\/[^/]+/, "")
|
|
588
|
+
.replaceAll(/[.+?^${}()|[\]\\]/g, String.raw`\$&`)
|
|
589
|
+
.replaceAll("*", ".*")
|
|
590
|
+
.replaceAll(/:\w+/g, "[^/]+");
|
|
591
|
+
return new RegExp(`^${source}$`);
|
|
559
592
|
}
|
|
560
593
|
|
|
561
594
|
/** Whether a path is a file, and not a folder or a link to nothing. @param {string} path */
|
|
562
595
|
export function isFile(path) {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
596
|
+
try {
|
|
597
|
+
return statSync(path).isFile();
|
|
598
|
+
} catch {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
568
601
|
}
|