-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcreate-fugu-apis.js
68 lines (62 loc) · 2.45 KB
/
create-fugu-apis.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
const fs = require('fs');
const patterns = fs.readFileSync('./node_modules/fugu-api-data/patterns.js', {encoding: 'utf-8'});
const script = `
const responseBodies = $WPT_BODIES;
// To avoid to match on, e.g., blog posts that contain the patterns,
// ensure that the file names fulfill certain conditions as a heuristic.
// Note that this leaves a slight risk of excluding inline \`<script>\` elements
// using these APIs from being covered, but usage there is expected to be small
// and we prefer to avoid the risk of false positives.
const checkURLConditions = (where, url, mimeType, responseBody) => {
// If the pattern has to occur in JavaScript, make sure the file name
// includes either \`.js\` or \`.mjs\` and uses a correct-ish MIME type
// (https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#textjavascript).
if (
where === "JavaScript" &&
/\\.m?js/.test(url) &&
mimeType.toLowerCase().endsWith("script")
) {
return true;
}
// If the pattern has to occur in the Web App Manifest, make sure the file
// name includes either \`.json\` or \`.webmanifest\`, uses a MIME type that
// ends in "json"
// (https://w3c.github.io/manifest/#:~:text=file%20extension%3A%20.webmanifest%20or%20.json%3F),
// and includes at least \`"start_url"\`.
if (
where === "Web App Manifest" &&
/\\.webmanifest|\\.json/.test(url) &&
mimeType.toLowerCase().endsWith("json") &&
/"start_url"/.test(responseBody)
) {
return true;
}
// Fall-through in all other cases.
return false;
};
// Iterate over all response bodies and over all patterns and populate the
// result object.
const result = {};
responseBodies.forEach((har) => {
for (const [key, value] of Object.entries(patterns)) {
if (value.regEx.test(har.response_body)) {
// Ignore the optional encoding, e.g.,
// \`application/manifest+json; charset=utf-8\`.
const mimeType = har.response_headers["content-type"]
.split(";")[0]
.trim();
if (result[key] && !result[key].includes(har.url)) {
if (checkURLConditions(value.where, har.url, mimeType, har.response_body)) {
result[key].push(har.url);
}
} else {
if (checkURLConditions(value.where, har.url, mimeType, har.response_body)) {
result[key] = [har.url];
}
}
}
}
});
return result;
`;
fs.writeFileSync('./dist/fugu-apis.js', `// [fugu-apis]\n\n${patterns}${script}`, {encoding: 'utf-8', flag: 'w'});