@weborigami/origami 0.0.68 → 0.0.69
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/exports/exports.js +4 -0
- package/package.json +4 -4
- package/src/builtins/@changes.js +24 -14
- package/src/builtins/@crawl.js +31 -455
- package/src/builtins/@keysTree.js +1 -1
- package/src/builtins/@siteAudit.js +19 -0
- package/src/builtins/@slug.js +3 -0
- package/src/builtins/@treeHttp.js +1 -1
- package/src/builtins/@treeHttps.js +1 -1
- package/src/crawler/crawlResources.js +179 -0
- package/src/crawler/findPaths.js +259 -0
- package/src/crawler/utilities.js +38 -0
package/exports/exports.js
CHANGED
|
@@ -86,6 +86,7 @@ export { default as serve } from "../src/builtins/@serve.js";
|
|
|
86
86
|
export { default as setDeep } from "../src/builtins/@setDeep.js";
|
|
87
87
|
export { default as shell } from "../src/builtins/@shell.js";
|
|
88
88
|
export { default as shuffle } from "../src/builtins/@shuffle.js";
|
|
89
|
+
export { default as siteAudit } from "../src/builtins/@siteAudit.js";
|
|
89
90
|
export { default as sitemap } from "../src/builtins/@sitemap.js";
|
|
90
91
|
export * from "../src/builtins/@slash.js";
|
|
91
92
|
export { default as slug } from "../src/builtins/@slug.js";
|
|
@@ -136,6 +137,9 @@ export { default as processUnpackedContent } from "../src/common/processUnpacked
|
|
|
136
137
|
export * from "../src/common/serialize.js";
|
|
137
138
|
export { default as ShuffleTransform } from "../src/common/ShuffleTransform.js";
|
|
138
139
|
export * from "../src/common/utilities.js";
|
|
140
|
+
export { default as crawlResources } from "../src/crawler/crawlResources.js";
|
|
141
|
+
export { default as findPaths } from "../src/crawler/findPaths.js";
|
|
142
|
+
export * from "../src/crawler/utilities.js";
|
|
139
143
|
export { default as assertTreeIsDefined } from "../src/misc/assertTreeIsDefined.js";
|
|
140
144
|
export { default as getTreeArgument } from "../src/misc/getTreeArgument.js";
|
|
141
145
|
export { default as OriCommandTransform } from "../src/misc/OriCommandTransform.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weborigami/origami",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.69",
|
|
4
4
|
"description": "Web Origami language, CLI, framework, and server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"typescript": "5.6.2"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@weborigami/async-tree": "0.0.
|
|
21
|
-
"@weborigami/language": "0.0.
|
|
22
|
-
"@weborigami/types": "0.0.
|
|
20
|
+
"@weborigami/async-tree": "0.0.69",
|
|
21
|
+
"@weborigami/language": "0.0.69",
|
|
22
|
+
"@weborigami/types": "0.0.69",
|
|
23
23
|
"exif-parser": "0.1.12",
|
|
24
24
|
"graphviz-wasm": "3.0.2",
|
|
25
25
|
"highlight.js": "11.10.0",
|
package/src/builtins/@changes.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Tree } from "@weborigami/async-tree";
|
|
1
|
+
import { trailingSlash, Tree } from "@weborigami/async-tree";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Given an old tree and a new tree, return a tree of changes indicated
|
|
@@ -17,36 +17,46 @@ export default async function changes(oldTreelike, newTreelike) {
|
|
|
17
17
|
const oldKeys = Array.from(await oldTree.keys());
|
|
18
18
|
const newKeys = Array.from(await newTree.keys());
|
|
19
19
|
|
|
20
|
-
const
|
|
20
|
+
const oldKeysNormalized = oldKeys.map(trailingSlash.remove);
|
|
21
|
+
const newKeysNormalized = newKeys.map(trailingSlash.remove);
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
let result;
|
|
24
|
+
|
|
25
|
+
for (const oldKey of oldKeys) {
|
|
26
|
+
const oldNormalized = trailingSlash.remove(oldKey);
|
|
27
|
+
if (!newKeysNormalized.includes(oldNormalized)) {
|
|
28
|
+
result ??= {};
|
|
29
|
+
result[oldKey] = "deleted";
|
|
25
30
|
continue;
|
|
26
31
|
}
|
|
27
32
|
|
|
28
|
-
const oldValue = await oldTree.get(
|
|
29
|
-
const newValue = await newTree.get(
|
|
33
|
+
const oldValue = await oldTree.get(oldKey);
|
|
34
|
+
const newValue = await newTree.get(oldKey);
|
|
30
35
|
|
|
31
36
|
if (Tree.isAsyncTree(oldValue) && Tree.isAsyncTree(newValue)) {
|
|
32
37
|
const treeChanges = await changes.call(this, oldValue, newValue);
|
|
33
|
-
if (Object.keys(treeChanges).length > 0) {
|
|
34
|
-
result
|
|
38
|
+
if (treeChanges && Object.keys(treeChanges).length > 0) {
|
|
39
|
+
result ??= {};
|
|
40
|
+
result[oldKey] = treeChanges;
|
|
35
41
|
}
|
|
36
42
|
} else if (oldValue?.toString && newValue?.toString) {
|
|
37
43
|
const oldText = oldValue.toString();
|
|
38
44
|
const newText = newValue.toString();
|
|
39
45
|
if (oldText !== newText) {
|
|
40
|
-
result
|
|
46
|
+
result ??= {};
|
|
47
|
+
result[oldKey] = "changed";
|
|
41
48
|
}
|
|
42
49
|
} else {
|
|
43
|
-
result
|
|
50
|
+
result ??= {};
|
|
51
|
+
result[oldKey] = "changed";
|
|
44
52
|
}
|
|
45
53
|
}
|
|
46
54
|
|
|
47
|
-
for (const
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
for (const newKey of newKeys) {
|
|
56
|
+
const newNormalized = trailingSlash.remove(newKey);
|
|
57
|
+
if (!oldKeysNormalized.includes(newNormalized)) {
|
|
58
|
+
result ??= {};
|
|
59
|
+
result[newKey] = "added";
|
|
50
60
|
}
|
|
51
61
|
}
|
|
52
62
|
|
package/src/builtins/@crawl.js
CHANGED
|
@@ -6,14 +6,10 @@ import {
|
|
|
6
6
|
keysFromPath,
|
|
7
7
|
trailingSlash,
|
|
8
8
|
} from "@weborigami/async-tree";
|
|
9
|
-
import { InvokeFunctionsTransform
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
|
|
14
|
-
// A fake base URL used to handle cases where an href is relative and must be
|
|
15
|
-
// treated relative to some base URL.
|
|
16
|
-
const fakeBaseUrl = new URL("https://fake");
|
|
9
|
+
import { InvokeFunctionsTransform } from "@weborigami/language";
|
|
10
|
+
import crawlResources from "../crawler/crawlResources.js";
|
|
11
|
+
import { normalizeKeys } from "../crawler/utilities.js";
|
|
12
|
+
import getTreeArgument from "../misc/getTreeArgument.js";
|
|
17
13
|
|
|
18
14
|
/**
|
|
19
15
|
* Crawl a tree, starting its root index.html page, and following links to
|
|
@@ -24,23 +20,19 @@ const fakeBaseUrl = new URL("https://fake");
|
|
|
24
20
|
* that obtain the requested value from the original site.
|
|
25
21
|
*
|
|
26
22
|
* @typedef {import("@weborigami/types").AsyncTree} AsyncTree
|
|
27
|
-
* @typedef {import("@weborigami/async-tree").Treelike
|
|
23
|
+
* @typedef {import("@weborigami/async-tree").Treelike} Treelike
|
|
28
24
|
* @this {AsyncTree|null}
|
|
29
25
|
* @param {Treelike} treelike
|
|
30
26
|
* @param {string} [baseHref]
|
|
31
27
|
* @returns {Promise<AsyncTree>}
|
|
32
28
|
*/
|
|
33
|
-
export default async function
|
|
34
|
-
|
|
35
|
-
const tree =
|
|
36
|
-
typeof treelike === "string"
|
|
37
|
-
? treeHttps.call(this, treelike)
|
|
38
|
-
: Tree.from(treelike, { parent: this });
|
|
29
|
+
export default async function crawlBuiltin(treelike, baseHref) {
|
|
30
|
+
const tree = await getTreeArgument(this, arguments, treelike, "@crawl");
|
|
39
31
|
|
|
40
32
|
if (baseHref === undefined) {
|
|
41
33
|
// Ask tree or original treelike if it has an `href` property we can use as
|
|
42
34
|
// the base href to determine whether a link is local within the tree or
|
|
43
|
-
// not. If not, use a fake `local:/` href.
|
|
35
|
+
// not. If not, use a fake `local:/` base href.
|
|
44
36
|
baseHref =
|
|
45
37
|
/** @type {any} */ (tree).href ??
|
|
46
38
|
/** @type {any} */ (treelike).href ??
|
|
@@ -48,7 +40,21 @@ export default async function crawl(treelike, baseHref) {
|
|
|
48
40
|
if (!baseHref?.endsWith("/")) {
|
|
49
41
|
baseHref += "/";
|
|
50
42
|
}
|
|
43
|
+
} else {
|
|
44
|
+
// Is the href already valid?
|
|
45
|
+
let isHrefValid = false;
|
|
46
|
+
try {
|
|
47
|
+
new URL(baseHref);
|
|
48
|
+
isHrefValid = true;
|
|
49
|
+
} catch (e) {
|
|
50
|
+
// Ignore
|
|
51
|
+
}
|
|
52
|
+
if (!isHrefValid) {
|
|
53
|
+
// Use a fake base href.
|
|
54
|
+
baseHref = `local:/${baseHref}`;
|
|
55
|
+
}
|
|
51
56
|
}
|
|
57
|
+
|
|
52
58
|
// @ts-ignore
|
|
53
59
|
const baseUrl = new URL(baseHref);
|
|
54
60
|
|
|
@@ -57,25 +63,18 @@ export default async function crawl(treelike, baseHref) {
|
|
|
57
63
|
const errors = [];
|
|
58
64
|
|
|
59
65
|
// We iterate until there are no more promises to wait for.
|
|
60
|
-
for await (const result of
|
|
61
|
-
const {
|
|
66
|
+
for await (const result of crawlResources(tree, baseUrl)) {
|
|
67
|
+
const { normalizedKeys, resourcePaths, value } = result;
|
|
62
68
|
|
|
63
69
|
// Cache the value
|
|
64
70
|
if (value) {
|
|
65
|
-
addValueToObject(cache,
|
|
71
|
+
addValueToObject(cache, normalizedKeys, value);
|
|
66
72
|
}
|
|
67
|
-
// else if (keys) {
|
|
68
|
-
// // A missing robots.txt isn't an error; anything else missing is.
|
|
69
|
-
// const path = keys.join("/");
|
|
70
|
-
// if (path !== "robots.txt") {
|
|
71
|
-
// errors.push(path);
|
|
72
|
-
// }
|
|
73
|
-
// }
|
|
74
73
|
|
|
75
74
|
// Add indirect resource functions to the resource tree. When requested,
|
|
76
75
|
// these functions will obtain the resource from the original site.
|
|
77
76
|
for (const resourcePath of resourcePaths) {
|
|
78
|
-
const resourceKeys =
|
|
77
|
+
const resourceKeys = normalizeKeys(keysFromPath(resourcePath));
|
|
79
78
|
const fn = () => {
|
|
80
79
|
return Tree.traverse(tree, ...resourceKeys);
|
|
81
80
|
};
|
|
@@ -101,17 +100,6 @@ export default async function crawl(treelike, baseHref) {
|
|
|
101
100
|
return result;
|
|
102
101
|
}
|
|
103
102
|
|
|
104
|
-
// For indexing and storage purposes, treat a path that ends in a trailing slash
|
|
105
|
-
// as if it ends in index.html.
|
|
106
|
-
function adjustKeys(keys) {
|
|
107
|
-
if (keys.length > 0 && !trailingSlash.has(keys.at(-1))) {
|
|
108
|
-
return keys;
|
|
109
|
-
}
|
|
110
|
-
const adjustedKeys = keys.slice();
|
|
111
|
-
adjustedKeys.push("index.html");
|
|
112
|
-
return adjustedKeys;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
103
|
function addValueToObject(object, keys, value) {
|
|
116
104
|
for (let i = 0, current = object; i < keys.length; i++) {
|
|
117
105
|
const key = trailingSlash.remove(keys[i]);
|
|
@@ -128,10 +116,9 @@ function addValueToObject(object, keys, value) {
|
|
|
128
116
|
if (!current[key]) {
|
|
129
117
|
current[key] = {};
|
|
130
118
|
} else if (!isPlainObject(current[key])) {
|
|
131
|
-
// Already have a value at this point. The site has a page
|
|
132
|
-
//
|
|
133
|
-
//
|
|
134
|
-
// current value to "index.html".
|
|
119
|
+
// Already have a value at this point. The site has a page at a route
|
|
120
|
+
// like /foo, and the site also has resources within that at routes like
|
|
121
|
+
// /foo/bar.jpg. We move the current value to "index.html".
|
|
135
122
|
current[key] = { "index.html": current[key] };
|
|
136
123
|
}
|
|
137
124
|
current = current[key];
|
|
@@ -139,416 +126,5 @@ function addValueToObject(object, keys, value) {
|
|
|
139
126
|
}
|
|
140
127
|
}
|
|
141
128
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
// retrieved at a path, along with the paths to other resources found in that
|
|
145
|
-
// text.
|
|
146
|
-
async function* crawlPaths(tree, baseUrl) {
|
|
147
|
-
// We want to kick off requests for new paths as quickly as we find them, then
|
|
148
|
-
// yield whichever result finishes first. Unfortunately, Promise.any() only
|
|
149
|
-
// tells us the result of the first promise to resolve, not which promise that
|
|
150
|
-
// was. So we keep track of a dictionary mapping paths to a promise for the
|
|
151
|
-
// value at that path. When a promise resolves, we mark it as resolved by
|
|
152
|
-
// setting its entry in the dictionary to null.
|
|
153
|
-
const promisesForPaths = {};
|
|
154
|
-
|
|
155
|
-
// Keep track of which resources refer to which paths.
|
|
156
|
-
const mapResourceToPaths = {};
|
|
157
|
-
|
|
158
|
-
let errorPaths = [];
|
|
159
|
-
|
|
160
|
-
// Seed the promise dictionary with robots.txt and the root path.
|
|
161
|
-
const initialPaths = ["/robots.txt", "/"];
|
|
162
|
-
initialPaths.forEach((path) => {
|
|
163
|
-
promisesForPaths[path] = processPath(tree, path, baseUrl);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
while (true) {
|
|
167
|
-
// Get the latest array of promises that haven't been resolved yet.
|
|
168
|
-
const promises = Object.values(promisesForPaths).filter(
|
|
169
|
-
(promise) => promise !== null
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
if (promises.length === 0) {
|
|
173
|
-
// No unresolved promises; we're done.
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Wait for the first promise to resolve.
|
|
178
|
-
const result = await Promise.any(promises);
|
|
179
|
-
|
|
180
|
-
// Mark the promise for that result as resolved.
|
|
181
|
-
promisesForPaths[result.path] = null;
|
|
182
|
-
|
|
183
|
-
// Add the crawlable paths to the map.
|
|
184
|
-
mapResourceToPaths[result.path] = result.crawlablePaths;
|
|
185
|
-
|
|
186
|
-
// Add promises for crawlable paths in the result.
|
|
187
|
-
result.crawlablePaths.forEach((path) => {
|
|
188
|
-
// Only add a promise for this path if we don't already have one.
|
|
189
|
-
if (promisesForPaths[path] === undefined) {
|
|
190
|
-
promisesForPaths[path] = processPath(tree, path, baseUrl);
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// If there was no value, add this to the errors.
|
|
195
|
-
// A missing robots.txt isn't an error; anything else missing is.
|
|
196
|
-
if (result.value === null && result.path !== "/robots.txt") {
|
|
197
|
-
errorPaths.push(result.path);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
yield result;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (errorPaths.length > 0) {
|
|
204
|
-
// Create a map of the resources that refer to each error.
|
|
205
|
-
const errorsMap = {};
|
|
206
|
-
for (const resource in mapResourceToPaths) {
|
|
207
|
-
const paths = mapResourceToPaths[resource];
|
|
208
|
-
for (const path of paths) {
|
|
209
|
-
if (errorPaths.includes(path)) {
|
|
210
|
-
errorsMap[resource] ??= [];
|
|
211
|
-
errorsMap[resource].push(path);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
const errorsJson = JSON.stringify(errorsMap, null, 2);
|
|
216
|
-
yield {
|
|
217
|
-
keys: ["crawl-errors.json"],
|
|
218
|
-
path: "crawl-errors.json",
|
|
219
|
-
resourcePaths: [],
|
|
220
|
-
value: errorsJson,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// Filter the paths to those that are local to the site.
|
|
226
|
-
function filterPaths(paths, baseUrl, localPath) {
|
|
227
|
-
// Convert paths to absolute URLs.
|
|
228
|
-
const localUrl = new URL(localPath, baseUrl);
|
|
229
|
-
const basePathname = baseUrl.pathname;
|
|
230
|
-
// @ts-ignore
|
|
231
|
-
const absoluteUrls = paths.map((path) => new URL(path, localUrl));
|
|
232
|
-
|
|
233
|
-
// Convert the absolute URLs to paths relative to the baseHref. If the URL
|
|
234
|
-
// points outside the tree rooted at the baseHref, the relative path will be
|
|
235
|
-
// null. We ignore the protocol in this test, because in practice sites often
|
|
236
|
-
// fumble the use of http and https, treating them interchangeably.
|
|
237
|
-
const relativePaths = absoluteUrls.map((url) => {
|
|
238
|
-
if (url.host === baseUrl.host && url.pathname.startsWith(basePathname)) {
|
|
239
|
-
return url.pathname.slice(basePathname.length);
|
|
240
|
-
} else {
|
|
241
|
-
return null;
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
// Filter out the null paths.
|
|
246
|
-
/** @type {string[]} */
|
|
247
|
-
// @ts-ignore
|
|
248
|
-
const filteredPaths = relativePaths.filter((path) => path);
|
|
249
|
-
return filteredPaths;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function findPaths(value, key, baseUrl, localPath) {
|
|
253
|
-
const text = utilities.toString(value);
|
|
254
|
-
|
|
255
|
-
// We guess the value is HTML is if its key has an .html extension or
|
|
256
|
-
// doesn't have an extension, or the value starts with `<`.
|
|
257
|
-
const ext = key ? extname(key).toLowerCase() : "";
|
|
258
|
-
const maybeHtml = ext === "" || text?.trim().startsWith("<");
|
|
259
|
-
let foundPaths;
|
|
260
|
-
if (ext === ".html" || ext === ".htm") {
|
|
261
|
-
foundPaths = findPathsInHtml(text);
|
|
262
|
-
} else if (ext === ".css") {
|
|
263
|
-
foundPaths = findPathsInCss(text);
|
|
264
|
-
} else if (ext === ".js") {
|
|
265
|
-
foundPaths = findPathsInJs(text);
|
|
266
|
-
} else if (ext === ".map") {
|
|
267
|
-
foundPaths = findPathsInImageMap(text);
|
|
268
|
-
} else if (key === "robots.txt") {
|
|
269
|
-
foundPaths = findPathsInRobotsTxt(text);
|
|
270
|
-
} else if (key === "sitemap.xml") {
|
|
271
|
-
foundPaths = findPathsInSitemapXml(text);
|
|
272
|
-
} else if (maybeHtml) {
|
|
273
|
-
foundPaths = findPathsInHtml(text);
|
|
274
|
-
} else {
|
|
275
|
-
// Doesn't have an extension we want to process
|
|
276
|
-
return {
|
|
277
|
-
crawlablePaths: [],
|
|
278
|
-
resourcePaths: [],
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const crawlablePaths = filterPaths(
|
|
283
|
-
foundPaths.crawlablePaths,
|
|
284
|
-
baseUrl,
|
|
285
|
-
localPath
|
|
286
|
-
);
|
|
287
|
-
const resourcePaths = filterPaths(
|
|
288
|
-
foundPaths.resourcePaths,
|
|
289
|
-
baseUrl,
|
|
290
|
-
localPath
|
|
291
|
-
);
|
|
292
|
-
return {
|
|
293
|
-
crawlablePaths,
|
|
294
|
-
resourcePaths,
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
function findPathsInCss(css) {
|
|
299
|
-
const resourcePaths = [];
|
|
300
|
-
let match;
|
|
301
|
-
|
|
302
|
-
// Find `url()` functions.
|
|
303
|
-
const urlRegex = /url\(["']?(?<href>[^"')]*?)["']?\)/g;
|
|
304
|
-
while ((match = urlRegex.exec(css))) {
|
|
305
|
-
const href = normalizeHref(match.groups?.href);
|
|
306
|
-
if (href) {
|
|
307
|
-
resourcePaths.push();
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
return {
|
|
312
|
-
crawlablePaths: [],
|
|
313
|
-
resourcePaths,
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// These are ancient server-side image maps. They're so old that it's hard to
|
|
318
|
-
// find documentation on them, but they're used on the reference Space Jam
|
|
319
|
-
// website we use for testing the crawler. Example:
|
|
320
|
-
// https://www.spacejam.com/1996/bin/bball.map
|
|
321
|
-
function findPathsInImageMap(imageMap) {
|
|
322
|
-
const resourcePaths = [];
|
|
323
|
-
let match;
|
|
324
|
-
|
|
325
|
-
// Find hrefs as the second column in each line.
|
|
326
|
-
const hrefRegex = /^\w+ (?<href>\S+)(\s*$| [\d, ]+$)/gm;
|
|
327
|
-
while ((match = hrefRegex.exec(imageMap))) {
|
|
328
|
-
const href = normalizeHref(match.groups?.href);
|
|
329
|
-
if (href) {
|
|
330
|
-
resourcePaths.push(href);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return {
|
|
335
|
-
crawlablePaths: [],
|
|
336
|
-
resourcePaths,
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
function findPathsInJs(js) {
|
|
341
|
-
const crawlablePaths = [];
|
|
342
|
-
let match;
|
|
343
|
-
|
|
344
|
-
// Find `import` statements.
|
|
345
|
-
const importRegex = /import [\s\S]+?from\s+["'](?<import>[^"']*)["'];/g;
|
|
346
|
-
while ((match = importRegex.exec(js))) {
|
|
347
|
-
const href = normalizeHref(match.groups?.import);
|
|
348
|
-
if (href) {
|
|
349
|
-
crawlablePaths.push(href);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return {
|
|
354
|
-
crawlablePaths,
|
|
355
|
-
resourcePaths: [],
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
function findPathsInHtml(html) {
|
|
360
|
-
const crawlablePaths = [];
|
|
361
|
-
const resourcePaths = [];
|
|
362
|
-
let match;
|
|
363
|
-
|
|
364
|
-
// Find `href` attributes in anchor and link tags.
|
|
365
|
-
const linkRegex =
|
|
366
|
-
/<(?:a|A|link|LINK) [^>]*?(?:href|HREF)=["'](?<link>[^>]*?)["'][^>]*>/g;
|
|
367
|
-
while ((match = linkRegex.exec(html))) {
|
|
368
|
-
// Links can point to be other crawlable paths and resource paths.
|
|
369
|
-
// We guess the type based on the extension.
|
|
370
|
-
const href = normalizeHref(match.groups?.link);
|
|
371
|
-
if (href) {
|
|
372
|
-
if (isCrawlableHref(href)) {
|
|
373
|
-
crawlablePaths.push(href);
|
|
374
|
-
} else {
|
|
375
|
-
resourcePaths.push(href);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// Find `src` attributes in img and script tags.
|
|
381
|
-
const srcRegex =
|
|
382
|
-
/<(?<tag>img|IMG|script|SCRIPT) [^>]*?(?:src|SRC)=["'](?<src>[^>]*?)["'][^>]*>/g;
|
|
383
|
-
while ((match = srcRegex.exec(html))) {
|
|
384
|
-
const tag = match.groups?.tag;
|
|
385
|
-
const src = normalizeHref(match.groups?.src);
|
|
386
|
-
if (src) {
|
|
387
|
-
if (tag === "script" || tag === "SCRIPT") {
|
|
388
|
-
crawlablePaths.push(src);
|
|
389
|
-
} else {
|
|
390
|
-
resourcePaths.push(src);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// Find `url()` functions in CSS.
|
|
396
|
-
const urlRegex = /url\(["']?(?<href>[^"')]*?)["']?\)/g;
|
|
397
|
-
while ((match = urlRegex.exec(html))) {
|
|
398
|
-
const href = normalizeHref(match.groups?.href);
|
|
399
|
-
if (href) {
|
|
400
|
-
resourcePaths.push(href);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
// Find `src` attribute on frame tags.
|
|
405
|
-
const frameRegex =
|
|
406
|
-
/<(?:frame|FRAME) [^>]*?(?:src|SRC)=["'](?<href>[^>]*?)["'][^>]*>/g;
|
|
407
|
-
while ((match = frameRegex.exec(html))) {
|
|
408
|
-
const href = normalizeHref(match.groups?.href);
|
|
409
|
-
if (href) {
|
|
410
|
-
crawlablePaths.push(href);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// Find ancient `background` attribute on body tag.
|
|
415
|
-
const backgroundRegex =
|
|
416
|
-
/<(?:body|BODY) [^>]*?(?:background|BACKGROUND)=["'](?<href>[^>]*?)["'][^>]*>/g;
|
|
417
|
-
while ((match = backgroundRegex.exec(html))) {
|
|
418
|
-
const href = normalizeHref(match.groups?.href);
|
|
419
|
-
if (href) {
|
|
420
|
-
resourcePaths.push(href);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
// Find `href` attribute on area tags.
|
|
425
|
-
const areaRegex =
|
|
426
|
-
/<(?:area|AREA) [^>]*?(?:href|HREF)=["'](?<href>[^>]*?)["'][^>]*>/g;
|
|
427
|
-
while ((match = areaRegex.exec(html))) {
|
|
428
|
-
const href = normalizeHref(match.groups?.href);
|
|
429
|
-
if (href) {
|
|
430
|
-
crawlablePaths.push(href);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
return { crawlablePaths, resourcePaths };
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
function findPathsInRobotsTxt(txt) {
|
|
438
|
-
const crawlablePaths = [];
|
|
439
|
-
let match;
|
|
440
|
-
|
|
441
|
-
// Find `Sitemap` directives.
|
|
442
|
-
const sitemapRegex = /Sitemap:\s*(?<href>[^\s]*)/g;
|
|
443
|
-
while ((match = sitemapRegex.exec(txt))) {
|
|
444
|
-
const href = normalizeHref(match.groups?.href);
|
|
445
|
-
if (href) {
|
|
446
|
-
crawlablePaths.push(href);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
return {
|
|
451
|
-
crawlablePaths,
|
|
452
|
-
resourcePaths: [],
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
function findPathsInSitemapXml(xml) {
|
|
457
|
-
const crawlablePaths = [];
|
|
458
|
-
let match;
|
|
459
|
-
|
|
460
|
-
// Find `loc` elements.
|
|
461
|
-
const locRegex = /<loc>(?<href>[^<]*)<\/loc>/g;
|
|
462
|
-
while ((match = locRegex.exec(xml))) {
|
|
463
|
-
const href = normalizeHref(match.groups?.href);
|
|
464
|
-
if (href) {
|
|
465
|
-
crawlablePaths.push(href);
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
return {
|
|
470
|
-
crawlablePaths,
|
|
471
|
-
resourcePaths: [],
|
|
472
|
-
};
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
function isCrawlableHref(href) {
|
|
476
|
-
// Use a fake base URL to cover the case where the href is relative.
|
|
477
|
-
const url = new URL(href, fakeBaseUrl);
|
|
478
|
-
const pathname = url.pathname;
|
|
479
|
-
const lastKey = pathname.split("/").pop() ?? "";
|
|
480
|
-
if (lastKey === "robots.txt" || lastKey === "sitemap.xml") {
|
|
481
|
-
return true;
|
|
482
|
-
}
|
|
483
|
-
const ext = extname(lastKey);
|
|
484
|
-
// We assume an empty extension is HTML.
|
|
485
|
-
const crawlableExtensions = [".html", ".css", ".js", ".map", ""];
|
|
486
|
-
return crawlableExtensions.includes(ext);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
// Remove any search parameters or hash from the href. Preserve absolute or
|
|
490
|
-
// relative nature of URL. If the URL only has a search or hash, return null.
|
|
491
|
-
function normalizeHref(href) {
|
|
492
|
-
// Remove everything after a `#` or `?` character.
|
|
493
|
-
const normalized = href.split(/[?#]/)[0];
|
|
494
|
-
return normalized === "" ? null : normalized;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
async function processPath(tree, path, baseUrl) {
|
|
498
|
-
if (path === undefined) {
|
|
499
|
-
return {
|
|
500
|
-
crawlablePaths: [],
|
|
501
|
-
keys: null,
|
|
502
|
-
path,
|
|
503
|
-
resourcePaths: [],
|
|
504
|
-
value: null,
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
// Convert path to keys
|
|
509
|
-
let keys = keysFromPath(path);
|
|
510
|
-
|
|
511
|
-
// Paths (including those created by the filterPaths function above) will have
|
|
512
|
-
// spaces, etc., escaped. In general, these need to be unescaped so we can
|
|
513
|
-
// find them in the tree.
|
|
514
|
-
keys = keys.map(decodeURIComponent);
|
|
515
|
-
|
|
516
|
-
// Traverse tree to get value.
|
|
517
|
-
let value = await Tree.traverse(tree, ...keys);
|
|
518
|
-
if (Tree.isAsyncTree(value)) {
|
|
519
|
-
// Path is actually a directory; see if it has an index.html
|
|
520
|
-
value = await Tree.traverse(value, "index.html");
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
const adjustedKeys = adjustKeys(keys);
|
|
524
|
-
|
|
525
|
-
if (value === undefined) {
|
|
526
|
-
return {
|
|
527
|
-
crawlablePaths: [],
|
|
528
|
-
keys: adjustedKeys,
|
|
529
|
-
path,
|
|
530
|
-
resourcePaths: [],
|
|
531
|
-
value: null,
|
|
532
|
-
};
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
// Find paths in the value
|
|
536
|
-
const key = adjustedKeys.at(-1);
|
|
537
|
-
const { crawlablePaths, resourcePaths } = await findPaths(
|
|
538
|
-
value,
|
|
539
|
-
key,
|
|
540
|
-
baseUrl,
|
|
541
|
-
path
|
|
542
|
-
);
|
|
543
|
-
|
|
544
|
-
return {
|
|
545
|
-
crawlablePaths,
|
|
546
|
-
keys: adjustedKeys,
|
|
547
|
-
path,
|
|
548
|
-
resourcePaths,
|
|
549
|
-
value,
|
|
550
|
-
};
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
crawl.usage = `@crawl <tree>\tCrawl a tree`;
|
|
554
|
-
crawl.documentation = "https://weborigami.org/language/@crawl.html";
|
|
129
|
+
crawlBuiltin.usage = `@crawl <tree>\tCrawl a tree`;
|
|
130
|
+
crawlBuiltin.documentation = "https://weborigami.org/language/@crawl.html";
|
|
@@ -8,7 +8,7 @@ import assertTreeIsDefined from "../misc/assertTreeIsDefined.js";
|
|
|
8
8
|
*
|
|
9
9
|
* @this {AsyncTree|null}
|
|
10
10
|
* @param {string} host
|
|
11
|
-
* @param {...string
|
|
11
|
+
* @param {...string} keys
|
|
12
12
|
*/
|
|
13
13
|
export default function keysTree(host, ...keys) {
|
|
14
14
|
assertTreeIsDefined(this, "keysTree");
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Tree } from "@weborigami/async-tree";
|
|
2
|
+
import getTreeArgument from "../misc/getTreeArgument.js";
|
|
3
|
+
import crawl from "./@crawl.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @this {import("@weborigami/types").AsyncTree|null}
|
|
7
|
+
* @param {import("@weborigami/async-tree").Treelike} treelike
|
|
8
|
+
*/
|
|
9
|
+
export default async function siteAudit(treelike) {
|
|
10
|
+
const tree = await getTreeArgument(this, arguments, treelike, "@siteAudit");
|
|
11
|
+
const crawled = await crawl.call(this, tree);
|
|
12
|
+
let crawlErrorsJson = await crawled.get("crawl-errors.json");
|
|
13
|
+
if (!crawlErrorsJson) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const errors = Tree.from(JSON.parse(crawlErrorsJson), { deep: true });
|
|
17
|
+
errors.parent = this;
|
|
18
|
+
return errors;
|
|
19
|
+
}
|
package/src/builtins/@slug.js
CHANGED
|
@@ -8,7 +8,7 @@ import assertTreeIsDefined from "../misc/assertTreeIsDefined.js";
|
|
|
8
8
|
*
|
|
9
9
|
* @this {AsyncTree|null}
|
|
10
10
|
* @param {string} host
|
|
11
|
-
* @param {...string
|
|
11
|
+
* @param {...string} keys
|
|
12
12
|
*/
|
|
13
13
|
export default function treeHttp(host, ...keys) {
|
|
14
14
|
assertTreeIsDefined(this, "treeHttp");
|
|
@@ -8,7 +8,7 @@ import assertTreeIsDefined from "../misc/assertTreeIsDefined.js";
|
|
|
8
8
|
*
|
|
9
9
|
* @this {AsyncTree|null}
|
|
10
10
|
* @param {string} host
|
|
11
|
-
* @param {...string
|
|
11
|
+
* @param {...string} keys
|
|
12
12
|
*/
|
|
13
13
|
export default function treeHttps(host, ...keys) {
|
|
14
14
|
assertTreeIsDefined(this, "treeHttps");
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {
|
|
2
|
+
keysFromPath,
|
|
3
|
+
pathFromKeys,
|
|
4
|
+
trailingSlash,
|
|
5
|
+
Tree,
|
|
6
|
+
} from "@weborigami/async-tree";
|
|
7
|
+
import findPaths from "./findPaths.js";
|
|
8
|
+
import { normalizeKeys } from "./utilities.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Crawl the paths for the given tree, starting at the given base URL, and yield
|
|
12
|
+
* the crawled resources.
|
|
13
|
+
*
|
|
14
|
+
* Each will include the HTML/script/stylesheet value retrieved at a given path.
|
|
15
|
+
*/
|
|
16
|
+
export default async function* crawlResources(tree, baseUrl) {
|
|
17
|
+
// We want to kick off requests for new paths as quickly as we find them, then
|
|
18
|
+
// yield whichever result finishes first. Unfortunately, Promise.any() only
|
|
19
|
+
// tells us the result of the first promise to resolve, not which promise that
|
|
20
|
+
// was. So we keep track of a dictionary mapping paths to a promise for the
|
|
21
|
+
// value at that path. When a promise resolves, we mark it as resolved by
|
|
22
|
+
// setting its entry in the dictionary to null.
|
|
23
|
+
const promisesForPaths = {};
|
|
24
|
+
|
|
25
|
+
// Keep track of which resources make which outbound links.
|
|
26
|
+
const resourceOutboundReferences = {};
|
|
27
|
+
|
|
28
|
+
let errorPaths = [];
|
|
29
|
+
|
|
30
|
+
// Seed the promise dictionary with robots.txt at the root and an empty path
|
|
31
|
+
// indicating the current directory (relative to the baseUrl).
|
|
32
|
+
const initialPaths = ["/robots.txt", ""];
|
|
33
|
+
initialPaths.forEach((path) => {
|
|
34
|
+
promisesForPaths[path] = processPath(tree, path, baseUrl);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
while (true) {
|
|
38
|
+
// Get the latest array of promises that haven't been resolved yet.
|
|
39
|
+
const promises = Object.values(promisesForPaths).filter(
|
|
40
|
+
(promise) => promise !== null
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
if (promises.length === 0) {
|
|
44
|
+
// No unresolved promises; we're done.
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Wait for the first promise to resolve.
|
|
49
|
+
const result = await Promise.any(promises);
|
|
50
|
+
|
|
51
|
+
// Mark the promise for that result as resolved.
|
|
52
|
+
promisesForPaths[result.path] = null;
|
|
53
|
+
|
|
54
|
+
if (result.value === null) {
|
|
55
|
+
// Expected resource doesn't exist; add this to the errors. Exception: a
|
|
56
|
+
// path in the set of initialPaths that doesn't exist is not an error.
|
|
57
|
+
if (!initialPaths.includes(result.path)) {
|
|
58
|
+
errorPaths.push(result.path);
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Add the crawlable paths to the map. Use the normalized keys (will include
|
|
64
|
+
// "index.html" if the path ends in a trailing slash).
|
|
65
|
+
const normalizedPath = pathFromKeys(result.normalizedKeys);
|
|
66
|
+
resourceOutboundReferences[normalizedPath] = result.crawlablePaths;
|
|
67
|
+
|
|
68
|
+
// Add promises for crawlable paths in the result.
|
|
69
|
+
result.crawlablePaths.forEach((path) => {
|
|
70
|
+
// Only add a promise for this path if we don't already have one.
|
|
71
|
+
if (promisesForPaths[path] === undefined) {
|
|
72
|
+
promisesForPaths[path] = processPath(tree, path, baseUrl);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
yield result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (errorPaths.length > 0) {
|
|
80
|
+
// Create a map of the resources that refer to each missing resource.
|
|
81
|
+
const errorsMap = {};
|
|
82
|
+
for (const sourcePath in resourceOutboundReferences) {
|
|
83
|
+
// Does this resource refer to any of the error paths?
|
|
84
|
+
const targetPaths = resourceOutboundReferences[sourcePath];
|
|
85
|
+
for (const targetPath of targetPaths) {
|
|
86
|
+
if (errorPaths.includes(targetPath)) {
|
|
87
|
+
errorsMap[sourcePath] ??= [];
|
|
88
|
+
errorsMap[sourcePath].push(targetPath);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Review the errors map to find any paths that could not be traced back to
|
|
94
|
+
// a referring resource. These are internal crawler errors. We log them so
|
|
95
|
+
// that the use can report them and we can investigate them.
|
|
96
|
+
for (const errorPath of errorPaths) {
|
|
97
|
+
if (!Object.values(errorsMap).flat().includes(errorPath)) {
|
|
98
|
+
errorsMap["(unknown)"] ??= [];
|
|
99
|
+
errorsMap["(unknown)"].push(errorPath);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const errorsJson = JSON.stringify(errorsMap, null, 2);
|
|
104
|
+
yield {
|
|
105
|
+
normalizedKeys: ["crawl-errors.json"],
|
|
106
|
+
path: "crawl-errors.json",
|
|
107
|
+
resourcePaths: [],
|
|
108
|
+
value: errorsJson,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function processPath(tree, path, baseUrl) {
|
|
114
|
+
// Don't process any path outside the baseUrl.
|
|
115
|
+
const url = new URL(path, baseUrl);
|
|
116
|
+
if (!url.pathname.startsWith(baseUrl.pathname)) {
|
|
117
|
+
return {
|
|
118
|
+
path,
|
|
119
|
+
value: null,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Convert path to keys
|
|
124
|
+
let keys = keysFromPath(path);
|
|
125
|
+
|
|
126
|
+
// Paths (including those created by the filterPaths function above) will have
|
|
127
|
+
// spaces, etc., escaped. In general, these need to be unescaped so we can
|
|
128
|
+
// find them in the tree.
|
|
129
|
+
keys = keys.map(decodeURIComponent);
|
|
130
|
+
|
|
131
|
+
// Traverse tree to get value.
|
|
132
|
+
let value = await Tree.traverse(tree, ...keys);
|
|
133
|
+
const normalizedKeys = normalizeKeys(keys);
|
|
134
|
+
let normalizedPath = path;
|
|
135
|
+
if (Tree.isTreelike(value)) {
|
|
136
|
+
// Path is actually a directory; see if it has an index.html
|
|
137
|
+
value = await Tree.traverse(value, "index.html");
|
|
138
|
+
if (value !== undefined) {
|
|
139
|
+
if (path.length > 0) {
|
|
140
|
+
// Mark the path as ending in a slash
|
|
141
|
+
normalizedPath = trailingSlash.add(path);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Add index.html to keys if it's not already there
|
|
145
|
+
if (normalizedKeys.at(-1) !== "index.html") {
|
|
146
|
+
normalizedKeys.push("index.html");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (value === undefined) {
|
|
152
|
+
return {
|
|
153
|
+
crawlablePaths: [],
|
|
154
|
+
keys,
|
|
155
|
+
normalizedKeys,
|
|
156
|
+
path,
|
|
157
|
+
resourcePaths: [],
|
|
158
|
+
value: null,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Find paths in the value
|
|
163
|
+
const key = normalizedKeys.at(-1);
|
|
164
|
+
const { crawlablePaths, resourcePaths } = await findPaths(
|
|
165
|
+
value,
|
|
166
|
+
key,
|
|
167
|
+
baseUrl,
|
|
168
|
+
normalizedPath
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
crawlablePaths,
|
|
173
|
+
keys,
|
|
174
|
+
normalizedKeys,
|
|
175
|
+
path,
|
|
176
|
+
resourcePaths,
|
|
177
|
+
value,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { toString } from "@weborigami/async-tree";
|
|
2
|
+
import { extname } from "@weborigami/language";
|
|
3
|
+
import { isCrawlableHref, normalizeHref } from "./utilities.js";
|
|
4
|
+
|
|
5
|
+
// Filter the paths to those that are local to the site.
|
|
6
|
+
function filterPaths(paths, baseUrl, localPath) {
|
|
7
|
+
// Convert paths to absolute URLs.
|
|
8
|
+
const localUrl = new URL(localPath, baseUrl);
|
|
9
|
+
const basePathname = baseUrl.pathname;
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const absoluteUrls = paths.map((path) => new URL(path, localUrl));
|
|
12
|
+
|
|
13
|
+
// Convert the absolute URLs to paths relative to the baseHref. If the URL
|
|
14
|
+
// points outside the tree rooted at the baseHref, the relative path will be
|
|
15
|
+
// null. We ignore the protocol in this test, because in practice sites often
|
|
16
|
+
// fumble the use of http and https, treating them interchangeably.
|
|
17
|
+
const relativePaths = absoluteUrls.map((url) => {
|
|
18
|
+
if (url.host === baseUrl.host && url.pathname.startsWith(basePathname)) {
|
|
19
|
+
return url.pathname.slice(basePathname.length);
|
|
20
|
+
} else {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Filter out the null paths.
|
|
26
|
+
/** @type {string[]} */
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
const filteredPaths = relativePaths.filter((path) => path);
|
|
29
|
+
return filteredPaths;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Given a value retrieved from a site using a given key (name), determine what
|
|
34
|
+
* kind of file it is and, based on that, find the paths it references.
|
|
35
|
+
*/
|
|
36
|
+
export default function findPaths(value, key, baseUrl, localPath) {
|
|
37
|
+
const text = toString(value);
|
|
38
|
+
|
|
39
|
+
// We guess the value is HTML is if its key has an .html extension or
|
|
40
|
+
// doesn't have an extension, or the value starts with `<`.
|
|
41
|
+
const ext = key ? extname(key).toLowerCase() : "";
|
|
42
|
+
const maybeHtml = ext === "" || text?.trim().startsWith("<");
|
|
43
|
+
let foundPaths;
|
|
44
|
+
if (ext === ".html" || ext === ".htm" || ext === ".xhtml") {
|
|
45
|
+
foundPaths = findPathsInHtml(text);
|
|
46
|
+
} else if (ext === ".css") {
|
|
47
|
+
foundPaths = findPathsInCss(text);
|
|
48
|
+
} else if (ext === ".js") {
|
|
49
|
+
foundPaths = findPathsInJs(text);
|
|
50
|
+
} else if (ext === ".map") {
|
|
51
|
+
foundPaths = findPathsInImageMap(text);
|
|
52
|
+
} else if (key === "robots.txt") {
|
|
53
|
+
foundPaths = findPathsInRobotsTxt(text);
|
|
54
|
+
} else if (key === "sitemap.xml") {
|
|
55
|
+
foundPaths = findPathsInSitemapXml(text);
|
|
56
|
+
} else if (maybeHtml) {
|
|
57
|
+
foundPaths = findPathsInHtml(text);
|
|
58
|
+
} else {
|
|
59
|
+
// Doesn't have an extension we want to process
|
|
60
|
+
return {
|
|
61
|
+
crawlablePaths: [],
|
|
62
|
+
resourcePaths: [],
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const crawlablePaths = filterPaths(
|
|
67
|
+
foundPaths.crawlablePaths,
|
|
68
|
+
baseUrl,
|
|
69
|
+
localPath
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const resourcePaths = filterPaths(
|
|
73
|
+
foundPaths.resourcePaths,
|
|
74
|
+
baseUrl,
|
|
75
|
+
localPath
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
crawlablePaths,
|
|
80
|
+
resourcePaths,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function findPathsInCss(css) {
|
|
85
|
+
const resourcePaths = [];
|
|
86
|
+
let match;
|
|
87
|
+
|
|
88
|
+
// Find `url()` functions.
|
|
89
|
+
const urlRegex = /url\(["']?(?<href>[^"')]*?)["']?\)/g;
|
|
90
|
+
while ((match = urlRegex.exec(css))) {
|
|
91
|
+
const href = normalizeHref(match.groups?.href);
|
|
92
|
+
if (href) {
|
|
93
|
+
resourcePaths.push();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
crawlablePaths: [],
|
|
99
|
+
resourcePaths,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// These are ancient server-side image maps. They're so old that it's hard to
|
|
104
|
+
// find documentation on them, but they're used on the reference Space Jam
|
|
105
|
+
// website we use for testing the crawler. Example:
|
|
106
|
+
// https://www.spacejam.com/1996/bin/bball.map
|
|
107
|
+
function findPathsInImageMap(imageMap) {
|
|
108
|
+
const resourcePaths = [];
|
|
109
|
+
let match;
|
|
110
|
+
|
|
111
|
+
// Find hrefs as the second column in each line.
|
|
112
|
+
const hrefRegex = /^\w+ (?<href>\S+)(\s*$| [\d, ]+$)/gm;
|
|
113
|
+
while ((match = hrefRegex.exec(imageMap))) {
|
|
114
|
+
const href = normalizeHref(match.groups?.href);
|
|
115
|
+
if (href) {
|
|
116
|
+
resourcePaths.push(href);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
crawlablePaths: [],
|
|
122
|
+
resourcePaths,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function findPathsInJs(js) {
|
|
127
|
+
const crawlablePaths = [];
|
|
128
|
+
let match;
|
|
129
|
+
|
|
130
|
+
// Find `import` statements.
|
|
131
|
+
const importRegex = /import [\s\S]+?from\s+["'](?<import>[^"']*)["'];/g;
|
|
132
|
+
while ((match = importRegex.exec(js))) {
|
|
133
|
+
const href = normalizeHref(match.groups?.import);
|
|
134
|
+
if (href) {
|
|
135
|
+
crawlablePaths.push(href);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
crawlablePaths,
|
|
141
|
+
resourcePaths: [],
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function findPathsInHtml(html) {
|
|
146
|
+
const crawlablePaths = [];
|
|
147
|
+
const resourcePaths = [];
|
|
148
|
+
let match;
|
|
149
|
+
|
|
150
|
+
// Find `href` attributes in anchor and link tags.
|
|
151
|
+
const linkRegex =
|
|
152
|
+
/<(?:a|A|link|LINK)[\s][^>]*?(?:href|HREF)=["'](?<link>[^>]*?)["'][^>]*>/g;
|
|
153
|
+
while ((match = linkRegex.exec(html))) {
|
|
154
|
+
// Links can point to be other crawlable paths and resource paths.
|
|
155
|
+
// We guess the type based on the extension.
|
|
156
|
+
const href = normalizeHref(match.groups?.link);
|
|
157
|
+
if (href) {
|
|
158
|
+
if (isCrawlableHref(href)) {
|
|
159
|
+
crawlablePaths.push(href);
|
|
160
|
+
} else {
|
|
161
|
+
resourcePaths.push(href);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Find `src` attributes in img and script tags.
|
|
167
|
+
const srcRegex =
|
|
168
|
+
/<(?<tag>img|IMG|script|SCRIPT)[\s][^>]*?(?:src|SRC)=["'](?<src>[^>]*?)["'][^>]*>/g;
|
|
169
|
+
while ((match = srcRegex.exec(html))) {
|
|
170
|
+
const tag = match.groups?.tag;
|
|
171
|
+
const src = normalizeHref(match.groups?.src);
|
|
172
|
+
if (src) {
|
|
173
|
+
if (tag === "script" || tag === "SCRIPT") {
|
|
174
|
+
crawlablePaths.push(src);
|
|
175
|
+
} else {
|
|
176
|
+
resourcePaths.push(src);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Find `url()` functions in CSS.
|
|
182
|
+
const urlRegex = /url\(["']?(?<href>[^"')]*?)["']?\)/g;
|
|
183
|
+
while ((match = urlRegex.exec(html))) {
|
|
184
|
+
const href = normalizeHref(match.groups?.href);
|
|
185
|
+
if (href) {
|
|
186
|
+
resourcePaths.push(href);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Find `src` attribute on frame tags.
|
|
191
|
+
const frameRegex =
|
|
192
|
+
/<(?:frame|FRAME)[\s][^>]*?(?:src|SRC)=["'](?<href>[^>]*?)["'][^>]*>/g;
|
|
193
|
+
while ((match = frameRegex.exec(html))) {
|
|
194
|
+
const href = normalizeHref(match.groups?.href);
|
|
195
|
+
if (href) {
|
|
196
|
+
crawlablePaths.push(href);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Find ancient `background` attribute on body tag.
|
|
201
|
+
const backgroundRegex =
|
|
202
|
+
/<(?:body|BODY)[\s][^>]*?(?:background|BACKGROUND)=["'](?<href>[^>]*?)["'][^>]*>/g;
|
|
203
|
+
while ((match = backgroundRegex.exec(html))) {
|
|
204
|
+
const href = normalizeHref(match.groups?.href);
|
|
205
|
+
if (href) {
|
|
206
|
+
resourcePaths.push(href);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Find `href` attribute on area tags.
|
|
211
|
+
const areaRegex =
|
|
212
|
+
/<(?:area|AREA)[\s][^>]*?(?:href|HREF)=["'](?<href>[^>]*?)["'][^>]*>/g;
|
|
213
|
+
while ((match = areaRegex.exec(html))) {
|
|
214
|
+
const href = normalizeHref(match.groups?.href);
|
|
215
|
+
if (href) {
|
|
216
|
+
crawlablePaths.push(href);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return { crawlablePaths, resourcePaths };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function findPathsInRobotsTxt(txt) {
|
|
224
|
+
const crawlablePaths = [];
|
|
225
|
+
let match;
|
|
226
|
+
|
|
227
|
+
// Find `Sitemap` directives.
|
|
228
|
+
const sitemapRegex = /Sitemap:\s*(?<href>[^\s]*)/g;
|
|
229
|
+
while ((match = sitemapRegex.exec(txt))) {
|
|
230
|
+
const href = normalizeHref(match.groups?.href);
|
|
231
|
+
if (href) {
|
|
232
|
+
crawlablePaths.push(href);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
crawlablePaths,
|
|
238
|
+
resourcePaths: [],
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function findPathsInSitemapXml(xml) {
|
|
243
|
+
const crawlablePaths = [];
|
|
244
|
+
let match;
|
|
245
|
+
|
|
246
|
+
// Find `loc` elements.
|
|
247
|
+
const locRegex = /<loc>(?<href>[^<]*)<\/loc>/g;
|
|
248
|
+
while ((match = locRegex.exec(xml))) {
|
|
249
|
+
const href = normalizeHref(match.groups?.href);
|
|
250
|
+
if (href) {
|
|
251
|
+
crawlablePaths.push(href);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
crawlablePaths,
|
|
257
|
+
resourcePaths: [],
|
|
258
|
+
};
|
|
259
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { trailingSlash } from "@weborigami/async-tree";
|
|
2
|
+
import { extname } from "@weborigami/language";
|
|
3
|
+
|
|
4
|
+
// A fake base URL used to handle cases where an href is relative and must be
|
|
5
|
+
// treated relative to some base URL.
|
|
6
|
+
const fakeBaseUrl = new URL("https://fake");
|
|
7
|
+
|
|
8
|
+
export function isCrawlableHref(href) {
|
|
9
|
+
// Use a fake base URL to cover the case where the href is relative.
|
|
10
|
+
const url = new URL(href, fakeBaseUrl);
|
|
11
|
+
const pathname = url.pathname;
|
|
12
|
+
const lastKey = pathname.split("/").pop() ?? "";
|
|
13
|
+
if (lastKey === "robots.txt" || lastKey === "sitemap.xml") {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
const ext = extname(lastKey);
|
|
17
|
+
// We assume an empty extension is HTML.
|
|
18
|
+
const crawlableExtensions = [".html", ".css", ".js", ".map", ".xhtml", ""];
|
|
19
|
+
return crawlableExtensions.includes(ext);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Remove any search parameters or hash from the href. Preserve absolute or
|
|
23
|
+
// relative nature of URL. If the URL only has a search or hash, return null.
|
|
24
|
+
export function normalizeHref(href) {
|
|
25
|
+
// Remove everything after a `#` or `?` character.
|
|
26
|
+
const normalized = href.split(/[?#]/)[0];
|
|
27
|
+
return normalized === "" ? null : normalized;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// For indexing and storage purposes, treat a path that ends in a trailing slash
|
|
31
|
+
// as if it ends in index.html.
|
|
32
|
+
export function normalizeKeys(keys) {
|
|
33
|
+
const normalized = keys.slice();
|
|
34
|
+
if (normalized.length === 0 || trailingSlash.has(normalized.at(-1))) {
|
|
35
|
+
normalized.push("index.html");
|
|
36
|
+
}
|
|
37
|
+
return normalized;
|
|
38
|
+
}
|