source: node_modules/simple-get/index.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

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