index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. module.exports = simpleGet
  2. const concat = require('simple-concat')
  3. const decompressResponse = require('decompress-response') // excluded from browser build
  4. const http = require('http')
  5. const https = require('https')
  6. const once = require('once')
  7. const querystring = require('querystring')
  8. const url = require('url')
  9. const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'
  10. function simpleGet (opts, cb) {
  11. opts = Object.assign({ maxRedirects: 10 }, typeof opts === 'string' ? { url: opts } : opts)
  12. cb = once(cb)
  13. if (opts.url) {
  14. const { hostname, port, protocol, auth, path } = url.parse(opts.url) // eslint-disable-line node/no-deprecated-api
  15. delete opts.url
  16. if (!hostname && !port && !protocol && !auth) opts.path = path // Relative redirect
  17. else Object.assign(opts, { hostname, port, protocol, auth, path }) // Absolute redirect
  18. }
  19. const headers = { 'accept-encoding': 'gzip, deflate' }
  20. if (opts.headers) Object.keys(opts.headers).forEach(k => (headers[k.toLowerCase()] = opts.headers[k]))
  21. opts.headers = headers
  22. let body
  23. if (opts.body) {
  24. body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body
  25. } else if (opts.form) {
  26. body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form)
  27. opts.headers['content-type'] = 'application/x-www-form-urlencoded'
  28. }
  29. if (body) {
  30. if (!opts.method) opts.method = 'POST'
  31. if (!isStream(body)) opts.headers['content-length'] = Buffer.byteLength(body)
  32. if (opts.json && !opts.form) opts.headers['content-type'] = 'application/json'
  33. }
  34. delete opts.body; delete opts.form
  35. if (opts.json) opts.headers.accept = 'application/json'
  36. if (opts.method) opts.method = opts.method.toUpperCase()
  37. const originalHost = opts.hostname // hostname before potential redirect
  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. const redirectHost = url.parse(opts.url).hostname // eslint-disable-line node/no-deprecated-api
  45. // If redirected host is different than original host, drop headers to prevent cookie leak (#73)
  46. if (redirectHost !== null && redirectHost !== originalHost) {
  47. delete opts.headers.cookie
  48. delete opts.headers.authorization
  49. }
  50. if (opts.method === 'POST' && [301, 302].includes(res.statusCode)) {
  51. opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35)
  52. delete opts.headers['content-length']; delete opts.headers['content-type']
  53. }
  54. if (opts.maxRedirects-- === 0) return cb(new Error('too many redirects'))
  55. else return simpleGet(opts, cb)
  56. }
  57. const tryUnzip = typeof decompressResponse === 'function' && opts.method !== 'HEAD'
  58. cb(null, tryUnzip ? decompressResponse(res) : res)
  59. })
  60. req.on('timeout', () => {
  61. req.abort()
  62. cb(new Error('Request timed out'))
  63. })
  64. req.on('error', cb)
  65. if (isStream(body)) body.on('error', cb).pipe(req)
  66. else req.end(body)
  67. return req
  68. }
  69. simpleGet.concat = (opts, cb) => {
  70. return simpleGet(opts, (err, res) => {
  71. if (err) return cb(err)
  72. concat(res, (err, data) => {
  73. if (err) return cb(err)
  74. if (opts.json) {
  75. try {
  76. data = JSON.parse(data.toString())
  77. } catch (err) {
  78. return cb(err, res, data)
  79. }
  80. }
  81. cb(null, res, data)
  82. })
  83. })
  84. }
  85. ;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(method => {
  86. simpleGet[method] = (opts, cb) => {
  87. if (typeof opts === 'string') opts = { url: opts }
  88. return simpleGet(Object.assign({ method: method.toUpperCase() }, opts), cb)
  89. }
  90. })