This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url-util.js
51 lines (42 loc) · 1.52 KB
/
url-util.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
const {URL} = require('url')
const normalizeUrlBase = require('normalize-url')
const normalizeUrlConfig = {
stripWWW: false,
removeTrailingSlash: false,
removeQueryParameters: [/[\s\S]*/ig]
}
const normalizeUrl = (url) => normalizeUrlBase(url, normalizeUrlConfig)
const {domain, ssl, start} = require(process.argv[2])
const HTTP = 'http'
const HTTPS = 'https'
const ENC_SCHEME = (ssl ? HTTPS : HTTP)
const BASE = `${ENC_SCHEME}://${domain}/`
const getRealHref = (url, pathname) => new URL(url, BASE + pathname).href
const domainRegex = new RegExp('^' + ENC_SCHEME + '?://' + domain + '[^\\.]')
const subDomainRegex = new RegExp('^' + ENC_SCHEME + '?://.*\\.' + domain + '[^\\.]')
const urlMatchesDomain = (url) => domainRegex.test(url) || subDomainRegex.test(url)
const XHR = 'xhr'
const GET = 'GET'
const fix = (url, pathname) => {
let newUrl = url.trim()
if (newUrl.startsWith(HTTP)) {
return normalizeUrl(newUrl)
}
return normalizeUrl(getRealHref(newUrl, pathname))
}
const getUrlDownloadKey = (url) => url + ' DOWNLOAD'
const shouldDownload = (url) => url
&& !global.urlsTodo[getUrlDownloadKey(url)]
&& urlMatchesDomain(url)
module.exports = {
getUrlDownloadKey,
fix,
shouldVisitFixed: (url) => url
&& urlMatchesDomain(url)
&& !global.urlsTodo[url],
shouldDownload,
shouldDownloadXhr: (resourceType, requestMethod, url) => shouldDownload(url)
&& resourceType === XHR
&& requestMethod === GET,
startUrl: normalizeUrl(ENC_SCHEME + '://' + start),
}