forked from laurengarcia/url-metadata
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
64 lines (60 loc) · 1.93 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
const q = require('q')
const request = require('request')
const parse = require('./lib/parse')
module.exports = function (url, options) {
const dfd = q.defer()
if (!options || typeof options !== 'object') options = {}
const opts = Object.assign(
// defaults
{
userAgent: 'MetadataScraper',
fromEmail: 'example@example.com',
maxRedirects: 10,
timeout: 10000,
descriptionLength: 750,
ensureSecureImageRequest: true,
sourceMap: {},
decode: undefined,
encode: undefined,
customHeaders: {}
},
// options passed in override defaults
options
)
const headers = {
'User-Agent': opts.userAgent,
'From': opts.fromEmail
};
Object.keys(opts.customHeaders).forEach(function (key) {
headers[key] = opts.customHeaders[key];
});
const requestOpts = {
url: url,
headers: headers,
maxRedirects: opts.maxRedirects,
encoding: opts.decode ? null : 'utf8',
timeout: opts.timeout
}
request.get(requestOpts, function (err, response, body) {
if (!err && response && response.statusCode) {
if (response.statusCode === 200) {
if (response.headers && response.headers['content-type'] && response.headers['content-type'].match(/^text\//)) {
// rewrite url if our request had to follow redirects to resolve the
// final link destination (for example: links shortened by bit.ly)
if (response.request.uri.href) url = response.request.uri.href
if (opts.decode) {
body = opts.decode(body)
}
return dfd.resolve(parse(url, body, opts))
} else {
return dfd.reject({ type: 'NOT_TEXT', detail: (response.headers && response.headers['content-type']) })
}
} else {
return dfd.reject({ type: 'NOT_200', detail: response.statusCode })
}
} else {
return dfd.reject({ type: 'UNKNOWN', detail: err })
}
})
return dfd.promise
}