-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode.js
44 lines (40 loc) · 1.32 KB
/
node.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
const lint = require("./lint")
const init = (config, mirrorzRepo) => {
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
global.DOMParser = new JSDOM().window.DOMParser;
Timeout = require("await-timeout");
const timeout = 15000;
global.Timeout = Timeout;
global.timeout = timeout;
fetch_extra = require("node-fetch-extra");
const headers = new fetch_extra.Headers({
'User-Agent': `mirrorz-parser/1.0 (+https://github.com/mirrorz-org/mirrorz-parser) ${config.url} ${mirrorzRepo}`,
});
async function fetchV6First (u, opt) {
const promise = fetch_extra(u, { ...opt, family: 6, headers });
return await Timeout.wrap(promise, timeout/10, 'Timeout').catch(async (e) => {
const promise = fetch_extra(u, { ...opt, headers });
return await Timeout.wrap(promise, timeout/3, 'Timeout').catch(() => null);
});
}
global.fetch = fetchV6First;
};
const load = async (source) => {
if (typeof(source) == "string") {
const resp = await fetch(source);
if (resp === null)
return null;
const json = await resp.json().catch(_ => null);
if (json === null)
return null;
try {
return lint(json);
} catch (e) {
return null;
}
} else {
return await Timeout.wrap(source(), timeout, 'Timeout').catch(() => null);
}
}
module.exports = { init, load };