forked from graphile/graphile.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link-check.js
270 lines (254 loc) · 8.16 KB
/
link-check.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
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
/* eslint-disable no-console */
// Don't fail on self-signed SSL
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const chalk = require("chalk");
const fetch = require("node-fetch");
const pMap = require("p-map");
const Entities = require("html-entities").AllHtmlEntities;
const entities = new Entities();
const base = `${__dirname}/public`;
const files = glob.sync(`${base}/**/*.html`);
const validLinks = files.map(f =>
f.substr(base.length).replace(/index.html$/, "")
);
const memoize = fn => {
const cache = new Map();
return function memoized(arg) {
if (cache.has(arg)) {
return cache.get(arg);
} else {
const res = fn.call(this, arg);
cache.set(arg, res);
return res;
}
};
};
function defer() {
let _resolve, _reject;
const p = new Promise((resolve, reject) => {
_resolve = resolve;
_reject = reject;
});
p.resolve = _resolve;
p.reject = _reject;
return p;
}
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
/**
* This prevents us placing multiple fetches to the same URL
*/
const queueByHost = {};
const cachedCheckByUrl = {};
const checkLinkResolution = memoize(url => {
if (!cachedCheckByUrl[url]) {
cachedCheckByUrl[url] = (async () => {
const { host } = new URL(url);
if (!queueByHost[host]) {
queueByHost[host] = Promise.resolve();
}
const deferred = defer();
const previous = queueByHost[host];
queueByHost[host] = previous
.finally(() => sleep(200))
.then(() =>
fetch(url, {
headers: {
"user-agent": "GraphileLinkChecker/0.1",
},
})
)
.then(deferred.resolve, deferred.reject);
return deferred;
})();
}
return cachedCheckByUrl[url];
});
function allMatches(str, regex) {
const all = [];
// eslint-disable-next-line no-constant-condition
while (true) {
const matches = regex.exec(str);
if (!matches) break;
all.push(matches[1]);
}
return all;
}
let invalid = 0;
const fileLinks = files.flatMap(file => {
const filePretty = chalk.bold(
path
.relative(__dirname, file)
.replace(/^public/, "")
.replace(/index.html$/, "")
);
const contents = fs.readFileSync(file, "utf8");
if (contents.indexOf("Postgraphile") >= 0) {
invalid++;
console.error(
`${filePretty} mentions 'Postgraphile'; please change to 'PostGraphile' or 'postgraphile' for consistency.`
);
}
// WARNING! Cardinal sin below, shield eyes
return allMatches(contents, /<a[^>]+href="([^"]+)"/g).map(link => {
return {
link: entities.decode(link),
filePretty,
};
});
});
pMap(
fileLinks,
async ({ filePretty, link }) => {
const trimmed = link.replace(/[?#].*$/, "");
const isHTTP = /^https?:\/\//.test(trimmed);
const isMailto = /^mailto:/.test(trimmed);
const isGraphile = /^https?:\/\/(www\.)?graphile\.(org|meh)/.test(trimmed);
const isYoutube = /^https?:\/\/(www\.)?youtube.com(\/|$)/.test(trimmed);
const isLocalhost = /^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)/.test(
trimmed
);
const isLocalhost5000 = /^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0):5000/.test(
trimmed
);
const isGitHubEditLink = /^https?:\/\/github\.com\/graphile\/graphile\.github\.io\/edit/.test(
trimmed
);
if (trimmed === "") {
// Anchor link (#section-name), continue
return;
} else if (trimmed.match(/\.(css|png|svg|webmanifest)$/)) {
// Resources
return;
} else if (isLocalhost5000) {
/*
* PostGraphile serves at http://localhost:5000 by default, so this will
* be legitimately referenced in the docs. All other localhost URLs are
* invalid.
*/
return;
} else if (isGitHubEditLink) {
// Don't check this since the page may not exist yet
return;
} else if (isLocalhost) {
invalid++;
console.error(
`${filePretty} has disallowed link to '${link}' (no localhost links allowed, except localhost:5000)`
);
return;
} else if (isGraphile) {
invalid++;
console.error(
`${filePretty} has disallowed link to '${link}' (Graphile internal links should start with \`/\` so that they point to the correct location in development/staging, do not include https://graphile.org)`
);
return;
} else if (isMailto) {
// mailto:, continue
return;
} else if (isYoutube) {
// YouTube sometimes gets upset and returns 429; we'll just allow it.
return;
} else if (isHTTP) {
const matches = trimmed.match(
/^https?:\/\/(?:www\.)?postgresql.org\/docs\/([^/]+)\/[^#]*(#.*)?$/
);
if (matches) {
const [, docVersion, hash] = matches;
const CURRENT_VERSION = "11";
/*
* It's better to link to /docs/current/ in general, but when there's a
* hash we want to ensure the links don't break when we go up a major
* version so linking to /docs/11/ is appropriate then. Linking to
* older docs is currently not accepted, but we may need to add
* exceptions in future.
*/
const isOkay =
docVersion === CURRENT_VERSION || (!hash && docVersion === "current");
if (!isOkay) {
invalid++;
if (hash) {
console.error(
`${filePretty} has disallowed link to '${link}' (please ensure PostgreSQL documentation links with hashes link to the '/docs/${CURRENT_VERSION}/...' documentation, you linked to '/docs/${docVersion}/...')`
);
} else {
console.error(
`${filePretty} has disallowed link to '${link}' (please ensure PostgreSQL documentation links link to the '/docs/current/...' documentation, you linked to '/docs/${docVersion}/...')`
);
}
// Handled above
return;
}
}
if (/^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)/.test(trimmed)) {
return;
}
try {
/*
* We use 'trimmed' rather than 'link' here since our check doesn't
* factor in anchors yet.
*/
const res = await checkLinkResolution(trimmed);
if (res.ok) {
return;
}
invalid++;
console.error(
`${filePretty} has broken link to '${link}' (link is returning a disallowed status code of ${res.status})`
);
return;
} catch (err) {
if (err.code === "ENOTFOUND") {
invalid++;
console.error(
`${filePretty} has broken link to '${link}' (there may be nothing wrong with the link, but the host is currently not resolving as expected)`
);
return;
} else if (err.code === "ECONNREFUSED") {
invalid++;
console.error(
`${filePretty} has broken link to '${link}' (there may be nothing wrong with the link, but the host is currently refusing the connection)`
);
return;
} else if (err.code === "ECONNRESET") {
invalid++;
console.error(
`${filePretty} has broken link to '${link}' (some network instability has been detected between this device and the host, maybe just try again)`
);
return;
} else {
invalid++;
console.error(
`${filePretty} has broken link to '${link}' (an error we didn't understand occurred: ${err.message})`
);
return;
}
}
} else if (validLinks.indexOf(trimmed) >= 0) {
// Cool, looks legit
return;
}
invalid++;
console.error(
`${filePretty} has disallowed link to '${link}' (none of the other validation rules matched)`
);
},
{ concurrency: 6 }
)
.catch(e => {
console.error();
console.error(`An uncaught error occurred during link validation:`);
console.error(e);
process.exit(2);
})
.then(() => {
if (invalid > 0) {
console.log();
console.log(`${invalid} errors found 😔`);
process.exit(1);
} else {
console.log();
console.log(`${fileLinks.length} links checked - all passed 💪`);
}
});