-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·50 lines (46 loc) · 1.44 KB
/
build.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
(async function main(retries = 4) {
if (retries < 0) {
throw new Error('Failed to fetch media types');
}
try {
const response = await fetch(
'https://www.iana.org/assignments/media-types/media-types.xml',
{
redirect: 'follow',
},
);
const { ok, status, statusText } = response;
if (!ok) {
throw new Error(`HTTP ${status} ${statusText}`);
}
const textData = await response.text();
const dom =
typeof DOMParser === 'undefined'
? new (await import('jsdom')).JSDOM(textData).window.document
: new DOMParser().parseFromString(textData, 'application/xml');
const mediaTypes = Array.from(
new Set(
(function* () {
for (const registry of dom.querySelectorAll(
'#media-types registry',
)) {
const title = registry.querySelector('title')?.textContent;
if (!title) {
continue;
}
for (const record of registry.getElementsByTagName('record')) {
yield record.querySelector('file[type=template]')?.textContent ??
`${title}/${record.querySelector('name').textContent}`;
}
}
})(),
),
).sort();
const output = mediaTypes;
console.log(JSON.stringify(output, undefined, 2));
} catch (error) {
console.error(error);
console.warn(`There are ${retries} retries left...\n`);
main(--retries);
}
})();