Socket
Socket
Sign inDemoInstall

simple-get

Package Overview
Dependencies
5
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.8.1 to 3.0.0

82

index.js
module.exports = simpleGet
var concat = require('simple-concat')
var http = require('http')
var https = require('https')
var once = require('once')
var querystring = require('querystring')
var decompressResponse = require('decompress-response') // excluded from browser build
var url = require('url')
const concat = require('simple-concat')
const decompressResponse = require('decompress-response') // excluded from browser build
const http = require('http')
const https = require('https')
const once = require('once')
const querystring = require('querystring')
const url = require('url')
const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'
function simpleGet (opts, cb) {
opts = typeof opts === 'string' ? {url: opts} : Object.assign({}, opts)
opts = typeof opts === 'string' ? { url: opts } : { ...opts }
opts = {maxRedirects: 10, ...opts}
cb = once(cb)
opts.headers = Object.assign({}, opts.headers)
Object.keys(opts.headers).forEach(function (h) {
if (h.toLowerCase() !== h) {
opts.headers[h.toLowerCase()] = opts.headers[h]
delete opts.headers[h]
}
})
if (opts.url) {
var loc = url.parse(opts.url)
if (loc.hostname) opts.hostname = loc.hostname
if (loc.port) opts.port = loc.port
if (loc.protocol) opts.protocol = loc.protocol
if (loc.auth) opts.auth = loc.auth
opts.path = loc.path
const { hostname, port, protocol, auth, path } = url.parse(opts.url)
delete opts.url
if (!hostname && !port && !protocol && !auth) opts.path = path // Relative redirect
else Object.assign(opts, { hostname, port, protocol, auth, path }) // Absolute redirect
}
if (opts.maxRedirects == null) opts.maxRedirects = 10
if (opts.method) opts.method = opts.method.toUpperCase()
const headers = {'accept-encoding': 'gzip, deflate'}
if (opts.headers) Object.entries(opts.headers).forEach(([k, v]) => (headers[k.toLowerCase()] = v))
opts.headers = headers
var body
let body
if (opts.body) {

@@ -51,9 +43,8 @@ body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body

}
if (opts.json) opts.headers.accept = 'application/json'
if (!opts.headers['accept-encoding']) opts.headers['accept-encoding'] = 'gzip, deflate' // Prefer gzip
if (opts.method) opts.method = opts.method.toUpperCase()
var protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
var req = protocol.request(opts, function (res) {
if (res.statusCode >= 300 && res.statusCode < 400 && 'location' in res.headers) {
const protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
const req = protocol.request(opts, res => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
opts.url = res.headers.location // Follow 3xx redirects

@@ -63,17 +54,15 @@ delete opts.headers.host // Discard `host` header on redirect (see #32)

if ((res.statusCode === 301 || res.statusCode === 302) && opts.method === 'POST') {
if (opts.method === 'POST' && [301, 302].includes(res.statusCode)) {
opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35)
delete opts.headers['content-length']
delete opts.headers['content-type']
delete opts.headers['content-length']; delete opts.headers['content-type']
}
if (opts.maxRedirects === 0) return cb(new Error('too many redirects'))
opts.maxRedirects -= 1
if (opts.maxRedirects-- === 0) return cb(new Error('too many redirects'))
return simpleGet(opts, cb)
}
var tryUnzip = typeof decompressResponse === 'function' && opts.method !== 'HEAD'
const tryUnzip = typeof decompressResponse === 'function' && opts.method !== 'HEAD'
cb(null, tryUnzip ? decompressResponse(res) : res)
})
req.on('timeout', function () {
req.on('timeout', () => {
req.abort()

@@ -84,3 +73,3 @@ cb(new Error('Request timed out'))

if (body && isStream(body)) body.on('error', cb).pipe(req)
if (isStream(body)) body.on('error', cb).pipe(req)
else req.end(body)

@@ -91,6 +80,6 @@

simpleGet.concat = function (opts, cb) {
return simpleGet(opts, function (err, res) {
simpleGet.concat = (opts, cb) => {
return simpleGet(opts, (err, res) => {
if (err) return cb(err)
concat(res, function (err, data) {
concat(res, (err, data) => {
if (err) return cb(err)

@@ -109,10 +98,7 @@ if (opts.json) {

;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(function (method) {
simpleGet[method] = function (opts, cb) {
;['get', 'post', 'put', 'patch', 'head', 'delete'].forEach(method => {
simpleGet[method] = (opts, cb) => {
if (typeof opts === 'string') opts = {url: opts}
opts.method = method.toUpperCase()
return simpleGet(opts, cb)
return simpleGet({method: method.toUpperCase(), ...opts}, cb)
}
})
function isStream (obj) { return typeof obj.pipe === 'function' }
{
"name": "simple-get",
"description": "Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines.",
"version": "2.8.1",
"version": "3.0.0",
"author": {

@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh",

@@ -25,3 +25,3 @@ # simple-get [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]

All this in < 120 lines of code.
All this in < 100 lines of code.

@@ -28,0 +28,0 @@ ## install

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc