index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 originalHost = opts.hostname // hostname before potential redirect
  39. const protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
  40. const req = protocol.request(opts, res => {
  41. if (opts.followRedirects !== false && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
  42. opts.url = res.headers.location // Follow 3xx redirects
  43. delete opts.headers.host // Discard `host` header on redirect (see #32)
  44. res.resume() // Discard response
  45. const redirectHost = url.parse(opts.url).hostname // eslint-disable-line node/no-deprecated-api
  46. // If redirected host is different than original host, drop headers to prevent cookie leak (#73)
  47. if (redirectHost !== null && redirectHost !== originalHost) {
  48. delete opts.headers.cookie
  49. delete opts.headers.authorization
  50. }
  51. if (opts.method === 'POST' && [301, 302].includes(res.statusCode)) {
  52. opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35)
  53. delete opts.headers['content-length']; delete opts.headers['content-type']
  54. }
  55. if (opts.maxRedirects-- === 0) return cb(new Error('too many redirects'))
  56. else return simpleGet(opts, cb)
  57. }
  58. const tryUnzip = typeof decompressResponse === 'function' && opts.method !== 'HEAD'
  59. cb(null, tryUnzip ? decompressResponse(res) : res)
  60. })
  61. req.on('timeout', () => {
  62. req.abort()
  63. cb(new Error('Request timed out'))
  64. })
  65. req.on('error', cb)
  66. if (isStream(body)) body.on('error', cb).pipe(req)
  67. else req.end(body)
  68. return req
  69. }
  70. simpleGet.concat = (opts, cb) => {
  71. return simpleGet(opts, (err, res) => {
  72. if (err) return cb(err)
  73. concat(res, (err, data) => {
  74. if (err) return cb(err)
  75. if (opts.json) {
  76. try {
  77. data = JSON.parse(data.toString())
  78. } catch (err) {
  79. return cb(err, res, data)
  80. }
  81. }
  82. cb(null, res, data)
  83. })
  84. })
  85. }
  86. ;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(method => {
  87. simpleGet[method] = (opts, cb) => {
  88. if (typeof opts === 'string') opts = { url: opts }
  89. return simpleGet(Object.assign({ method: method.toUpperCase() }, opts), cb)
  90. }
  91. })