This repository has been archived by the owner on Jan 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
162 lines (137 loc) · 3.99 KB
/
index.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
var q = require('q')
var isCss = require('is-css')
var isPresent = require('is-present')
var isBlank = require('is-blank')
var isUrl = require('is-url-superb')
var request = require('requestretry')
var cheerio = require('cheerio')
var normalizeUrl = require('normalize-url')
var stripHtmlComments = require('strip-html-comments')
var stripWaybackToolbar = require('strip-wayback-toolbar')
var resolveCssImportUrls = require('resolve-css-import-urls')
var ua = require('ua-string')
var getLinkContents = require('./utils/get-link-contents')
var createLink = require('./utils/create-link')
module.exports = function(url, options, html) {
var deferred = q.defer()
var options = options || {}
options.headers = options.headers || {}
options.headers['User-Agent'] = options.headers['User-Agent'] || ua
options.timeout = options.timeout || 5000
options.stripWayback = options.stripWayback || false
options.gzip = true
if (typeof url !== 'string' || isBlank(url) || !isUrl(url)) {
throw new TypeError('get-css expected a url as a string')
}
url = normalizeUrl(url, { stripWWW: false })
options.url = url
if (options.ignoreCerts) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
}
var status = {
parsed: 0,
total: 0
}
var result = {
links: [],
styles: [],
css: ''
}
function handleResolve() {
if (status.parsed >= status.total) {
deferred.resolve(result)
}
}
function parseHtml(html) {
if (options.stripWayback) {
html = stripWaybackToolbar(html)
}
var $ = cheerio.load(html)
result.pageTitle = $('head > title').text()
result.html = html
$('[rel=stylesheet]').each(function() {
var link = $(this).attr('href')
if (isPresent(link)) {
result.links.push(createLink(link, url))
} else {
result.styles.push(stripHtmlComments($(this).text()))
}
})
$('style').each(function() {
result.styles.push(stripHtmlComments($(this).text()))
})
status.total = result.links.length + result.styles.length
if (!status.total) {
deferred.resolve(false)
}
result.links.forEach(function(link) {
getLinkContents(link.url, options)
.then(function(css) {
handleCssFromLink(link, css)
})
.catch(function(error) {
link.error = error
status.parsed++
handleResolve()
})
})
result.styles.forEach(function(css) {
result.css += css
status.parsed++
handleResolve()
})
}
function handleCssFromLink(link, css) {
link.css += css
parseCssForImports(link, css)
status.parsed++
handleResolve()
}
// Handle potential @import url(foo.css) statements in the CSS.
function parseCssForImports(link, css) {
link.imports = resolveCssImportUrls(link.url, css)
status.total += link.imports.length
result.css += css
link.imports.forEach(function(importUrl) {
var importLink = createLink(importUrl, importUrl)
result.links.push(importLink)
getLinkContents(importLink.url, options)
.then(function(css) {
handleCssFromLink(importLink, css)
})
.catch(function(error) {
link.error = error
status.parsed++
handleResolve()
})
})
}
function handleBody(body) {
if (isCss(url)) {
var link = createLink(url, url)
result.links.push(link)
handleCssFromLink(link, body)
} else {
parseHtml(body)
}
}
if (html) {
handleBody(html)
} else {
request(options, function(error, response, body) {
if (error) {
if (options.verbose) console.log('Error from ' + url + ' ' + error)
deferred.reject(error)
return
}
if (response && response.statusCode != 200) {
if (options.verbose)
console.log('Received a ' + response.statusCode + ' from: ' + url)
deferred.reject({ url: url, statusCode: response.code })
return
}
handleBody(body)
})
}
return deferred.promise
}