UNPKG

650 BJavaScriptView Raw
1'use strict'
2const cheerio = require('cheerio')
3const fetch = require('node-fetch')
4
5module.exports = function (url, opts) {
6 if (typeof url !== 'string') {
7 throw new TypeError('Expected a string')
8 }
9
10 opts = opts || {}
11 const cheerioOpts = Object.assign({
12 // keep the original unicode chars
13 decodeEntities: false
14 }, opts.cheerio)
15 // normal mode, faster
16 const ret = fetch(url)
17 .then(data => {
18 if (data.status !== 200) {
19 return Promise.reject(new Error('Not Found'))
20 }
21 return data.text()
22 })
23 if (opts.htmlOnly) {
24 return ret
25 }
26 return ret.then(data => cheerio.load(data, cheerioOpts))
27}