index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*! simple-get. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  2. module.exports = simpleGet
  3. const concat = require('simple-concat')
  4. const decompressResponse = require('decompress-response') // excluded from browser build
  5. const http = require('http')
  6. const https = require('https')
  7. const once = require('once')
  8. const querystring = require('querystring')
  9. const url = require('url')
  10. const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'
  11. function simpleGet (opts, cb) {
  12. opts = Object.assign({ maxRedirects: 10 }, typeof opts === 'string' ? { url: opts } : opts)
  13. cb = once(cb)
  14. if (opts.url) {
  15. const { hostname, port, protocol, auth, path } = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
  16. delete opts.url
  17. if (!hostname && !port && !protocol && !auth) opts.path = path // Relative redirect
  18. else Object.assign(opts, { hostname, port, protocol, auth, path }) // Absolute redirect
  19. }
  20. const headers = { 'accept-encoding': 'gzip, deflate' }
  21. if (opts.headers) Object.keys(opts.headers).forEach(k => (headers[k.toLowerCase()] = opts.headers[k]))
  22. opts.headers = headers
  23. let body
  24. if (opts.body) {
  25. body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body
  26. } else if (opts.form) {
  27. body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form)
  28. opts.headers['content-type'] = 'application/x-www-form-urlencoded'
  29. }
  30. if (body) {
  31. if (!opts.method) opts.method = 'POST'
  32. if (!isStream(body)) opts.headers['content-length'] = Buffer.byteLength(body)
  33. if (opts.json && !opts.form) opts.headers['content-type'] = 'application/json'
  34. }
  35. delete opts.body; delete opts.form
  36. if (opts.json) opts.headers.accept = 'application/json'
  37. if (opts.method) opts.method = opts.method.toUpperCase()
  38. const protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
  39. const req = protocol.request(opts, res => {
  40. if (opts.followRedirects !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
  41. opts.url = res.headers.location // Follow 3xx redirects
  42. delete opts.headers.host // Discard `host` header on redirect (see #32)
  43. res.resume() // Discard response
  44. if (opts.method === 'POST' && [301, 302].includes(res.statusCode)) {
  45. opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35)
  46. delete opts.headers['content-length']; delete opts.headers['content-type']
  47. }
  48. if (opts.maxRedirects-- === 0) return cb(new Error('too many redirects'))
  49. else return simpleGet(opts, cb)
  50. }
  51. const tryUnzip = typeof decompressResponse === 'function' && opts.method !== 'HEAD'
  52. cb(null, tryUnzip ? decompressResponse(res) : res)
  53. })
  54. req.on('timeout', () => {
  55. req.abort()
  56. cb(new Error('Request timed out'))
  57. })
  58. req.on('error', cb)
  59. if (isStream(body)) body.on('error', cb).pipe(req)
  60. else req.end(body)
  61. return req
  62. }
  63. simpleGet.concat = (opts, cb) => {
  64. return simpleGet(opts, (err, res) => {
  65. if (err) return cb(err)
  66. concat(res, (err, data) => {
  67. if (err) return cb(err)
  68. if (opts.json) {
  69. try {
  70. data = JSON.parse(data.toString())
  71. } catch (err) {
  72. return cb(err, res, data)
  73. }
  74. }
  75. cb(null, res, data)
  76. })
  77. })
  78. }
  79. ;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(method => {
  80. simpleGet[method] = (opts, cb) => {
  81. if (typeof opts === 'string') opts = { url: opts }
  82. return simpleGet(Object.assign({ method: method.toUpperCase() }, opts), cb)
  83. }
  84. })