client-request
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -1,9 +0,9 @@ | ||
"use strict" | ||
'use strict' | ||
module.exports = Concat | ||
var Writable = require("stream").Writable | ||
var inherits = require("util").inherits | ||
var Writable = require('stream').Writable | ||
var inherits = require('util').inherits | ||
function Concat(options, callback) { | ||
function Concat (options, callback) { | ||
if (!(this instanceof Concat)) { | ||
@@ -13,3 +13,3 @@ return new Concat(options, callback) | ||
if (typeof options == "function") { | ||
if (typeof options === 'function') { | ||
callback = options | ||
@@ -19,5 +19,5 @@ options = {} | ||
if (typeof callback != "function") { | ||
if (typeof callback !== 'function') { | ||
// This stream goes nowhere... | ||
callback = function devnull() { | ||
callback = function devnull () { | ||
this._collection = [] | ||
@@ -35,7 +35,7 @@ } | ||
this.on("finish", this._done) | ||
this.on('finish', this._done) | ||
} | ||
inherits(Concat, Writable) | ||
Concat.prototype._write = function (chunk, encoding, callback) { | ||
Concat.prototype._write = function _write (chunk, encoding, callback) { | ||
this._collection.push(chunk) | ||
@@ -45,9 +45,8 @@ callback() | ||
Concat.prototype._done = function _done() { | ||
Concat.prototype._done = function _done () { | ||
if (this.options.objectMode) { | ||
this.callback.call(this, this._collection) | ||
this.callback(this._collection) | ||
} else { | ||
this.callback(Buffer.concat(this._collection)) | ||
} | ||
else { | ||
this.callback.call(this, Buffer.concat(this._collection)) | ||
} | ||
} |
{ | ||
"name": "client-request", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "A zero-dependency stripped-down http client request module based on the http://npm.im/request API", | ||
@@ -10,3 +10,3 @@ "main": "request.js", | ||
"scripts": { | ||
"test": "node test/" | ||
"test": "standard && node test/" | ||
}, | ||
@@ -22,4 +22,5 @@ "keywords": [ | ||
"devDependencies": { | ||
"standard": "~10.0.3", | ||
"stream-spigot": "~3.0.5", | ||
"tape": "~4.0.0" | ||
"tape": "~4.8.0" | ||
}, | ||
@@ -26,0 +27,0 @@ "dependencies": {}, |
@@ -5,4 +5,4 @@ const request = require('./request') | ||
function requestPromise(opts) { | ||
return new Promise(function (resolve, reject) { | ||
function requestPromise (opts) { | ||
return new Promise(function wrapped (resolve, reject) { | ||
request(opts, function (err, response, body) { | ||
@@ -9,0 +9,0 @@ if (err) { |
@@ -21,3 +21,2 @@ client-request | ||
If you want... | ||
* streaming -- use [hyperquest](http://npm.im/hyperquest) instead of this library (it's awesome!) | ||
* `request.form` -- use [form-urlencoded](https://www.npmjs.com/package/form-urlencoded), a zero-deps form body encoder. (example below) | ||
@@ -111,2 +110,3 @@ * `options.qs` -- use the core `querystring` library or [qs](http://npm.im/qs) and append the querystring to your url path prior to sending it to request | ||
* `body` -- the raw body to send to the server (e.g. PUT or POST) -- if `body` is a string/buffer, it will send that, if `body` quacks like a stream, stream it, otherwise it will send it JSON serialized. | ||
* `stream` -- if `true` callback will return raw `http.ServerResponse` stream. Stream error handling will be up to you. | ||
@@ -113,0 +113,0 @@ Extensions |
141
request.js
@@ -1,9 +0,8 @@ | ||
"use strict" | ||
'use strict' | ||
module.exports = request | ||
var http = require("http") | ||
var https = require("https") | ||
var url = require("url") | ||
var path = require("path") | ||
var http = require('http') | ||
var https = require('https') | ||
var url = require('url') | ||
@@ -22,11 +21,9 @@ var CORE_HTTPS_OPTS = [ | ||
var clientId = require("crypto").randomBytes(3).toString("hex") | ||
var clientId = require('crypto').randomBytes(3).toString('hex') | ||
var PassThrough = require("stream").PassThrough | ||
var concat = require("./concat") | ||
var util = require("util") | ||
var debug = function noop() {} | ||
var concat = require('./concat') | ||
var util = require('util') | ||
var debug = function noop () {} | ||
if (util.debuglog != null) { | ||
debug = util.debuglog("client-request") | ||
debug = util.debuglog('client-request') | ||
} | ||
@@ -36,17 +33,16 @@ | ||
function request(requestOptions, callback) { | ||
function request (requestOptions, callback) { | ||
if (requestOptions == null) { | ||
throw new TypeError("Options parameter is required") | ||
throw new TypeError('Options parameter is required') | ||
} | ||
if (callback == null || (typeof callback != "function")) { | ||
throw new TypeError("callback is required") | ||
if (callback == null || (typeof callback !== 'function')) { | ||
throw new TypeError('callback is required') | ||
} | ||
var requestId = clientId + "-" + requestCount | ||
var requestId = clientId + '-' + requestCount | ||
var options = formatOptions(requestOptions) | ||
var transport = http | ||
if (options.scheme == "https") { | ||
debug(requestId, "using https transport") | ||
if (options.scheme === 'https') { | ||
debug(requestId, 'using https transport') | ||
transport = https | ||
@@ -58,14 +54,12 @@ } | ||
if (requestOptions.body != null) { | ||
if (typeof requestOptions.body == "string" || Buffer.isBuffer(requestOptions.body)) { | ||
debug("Detected simple body") | ||
if (typeof requestOptions.body === 'string' || Buffer.isBuffer(requestOptions.body)) { | ||
debug('Detected simple body') | ||
body = requestOptions.body | ||
} | ||
// Stream duck typing | ||
else if (requestOptions.body.pipe != null && (typeof requestOptions.body.pipe == "function")) { | ||
debug("Detected stream body") | ||
} else if (requestOptions.body.pipe != null && (typeof requestOptions.body.pipe === 'function')) { | ||
// stream duck typing | ||
debug('Detected stream body') | ||
body = requestOptions.body | ||
isStream = true | ||
} | ||
else { | ||
debug("Detected object body, will JSON-serialize") | ||
} else { | ||
debug('Detected object body, will JSON-serialize') | ||
body = JSON.stringify(requestOptions.body) | ||
@@ -77,6 +71,6 @@ } | ||
var calledBack = false | ||
function reply(err, response) { | ||
debug(requestId, "in reply", err, response, calledBack) | ||
function reply (err, response) { | ||
debug(requestId, 'in reply', err, response, calledBack) | ||
if (calledBack) { | ||
debug(requestId, "REFUSING TO CALL CALLBACK TWICE") | ||
debug(requestId, 'REFUSING TO CALL CALLBACK TWICE') | ||
return | ||
@@ -86,11 +80,13 @@ } | ||
clearTimeout(totalTimeout) | ||
debug(requestId, "calling callback err(" + err + ")") | ||
debug(requestId, "statusCode:", metadata.statusCode) | ||
debug(requestId, "reply headers:", metadata.headers) | ||
debug(requestId, "content:", Buffer.isBuffer(response) ? response.toString() : response) | ||
debug(requestId, 'calling callback err(' + err + ')') | ||
debug(requestId, 'statusCode:', metadata.statusCode) | ||
debug(requestId, 'reply headers:', metadata.headers) | ||
if (!options.stream) { | ||
debug(requestId, 'content:', Buffer.isBuffer(response) ? response.toString() : response) | ||
} | ||
return callback(err, metadata, response) | ||
} | ||
function collect(content) { | ||
debug(requestId, "in collect") | ||
function collect (content) { | ||
debug(requestId, 'in collect') | ||
if (!requestOptions.json) { | ||
@@ -101,4 +97,3 @@ return reply(null, content) | ||
var response = JSON.parse(content) | ||
} | ||
catch (e) { | ||
} catch (e) { | ||
return reply(e) | ||
@@ -109,5 +104,5 @@ } | ||
debug(requestId, "About to send request", options) | ||
debug(requestId, "calledBack:", calledBack) | ||
var req = transport.request(options, function onResponse(res) { | ||
debug(requestId, 'About to send request', options) | ||
debug(requestId, 'calledBack:', calledBack) | ||
var req = transport.request(options, function onResponse (res) { | ||
metadata = res | ||
@@ -119,15 +114,17 @@ | ||
return reply(null, null) | ||
} else if (requestOptions.stream) { | ||
// return the raw response stream | ||
return reply(null, res) | ||
} else { | ||
res.pipe(concat(collect)) | ||
res.once('error', function resError (err) { | ||
return reply(err) | ||
}) | ||
} | ||
res.once("error", function resError(err) { | ||
return reply(err) | ||
}) | ||
}) | ||
var origAbort = req.abort | ||
req.abort = function abort() { | ||
req.abort = function abort () { | ||
clearTimeout(totalTimeout) | ||
debug(requestId, "aborting request") | ||
debug(requestId, 'aborting request') | ||
origAbort.call(this) | ||
@@ -138,5 +135,5 @@ } | ||
if (requestOptions.timeout) { | ||
totalTimeout = setTimeout(function onTimeout() { | ||
totalTimeout = setTimeout(function onTimeout () { | ||
req.abort() | ||
var err = new Error("client request timeout") | ||
var err = new Error('client request timeout') | ||
return reply(err) | ||
@@ -146,5 +143,5 @@ }, requestOptions.timeout) | ||
// socket timeout | ||
req.setTimeout(requestOptions.timeout, function onSocketTimeout() { | ||
req.setTimeout(requestOptions.timeout, function onSocketTimeout () { | ||
req.abort() | ||
var err = new Error("Socket Timeout on client request") | ||
var err = new Error('Socket Timeout on client request') | ||
return reply(err) | ||
@@ -154,8 +151,8 @@ }) | ||
req.once("abort", function onAbort() { | ||
debug(requestId, "ABORT", this.aborted) | ||
req.once('abort', function onAbort () { | ||
debug(requestId, 'ABORT', this.aborted) | ||
}) | ||
req.once("error", function reqError(err) { | ||
debug(requestId, "req error event") | ||
req.once('error', function reqError (err) { | ||
debug(requestId, 'req error event') | ||
return reply(err) | ||
@@ -165,8 +162,7 @@ }) | ||
if (isStream) { | ||
debug("sending stream body") | ||
debug('sending stream body') | ||
body.pipe(req) | ||
} | ||
else { | ||
} else { | ||
if (body != null) { | ||
debug(requestId, "data:", body) | ||
debug(requestId, 'data:', body) | ||
req.write(body) | ||
@@ -181,3 +177,3 @@ } | ||
function formatOptions(options) { | ||
function formatOptions (options) { | ||
var headers = options.headers || {} | ||
@@ -187,19 +183,18 @@ var u = url.parse(options.uri || options.url) | ||
if (au) { | ||
headers.authorization = "Basic " + Buffer(au).toString("base64") | ||
headers.authorization = 'Basic ' + Buffer.from(au).toString('base64') | ||
} | ||
var protocol = u.protocol || "" | ||
var iface = protocol === "https:" ? https : http | ||
var protocol = u.protocol || '' | ||
var opts = { | ||
scheme: protocol.replace(/:$/, ""), | ||
method: options.method || "GET", | ||
host: u.hostname, | ||
port: Number(u.port) || (protocol === "https:" ? 443 : 80), | ||
path: u.path, | ||
headers: headers, | ||
withCredentials: options.withCredentials || false | ||
scheme: protocol.replace(/:$/, ''), | ||
method: options.method || 'GET', | ||
host: u.hostname, | ||
port: Number(u.port) || (protocol === 'https:' ? 443 : 80), | ||
path: u.path, | ||
headers: headers, | ||
withCredentials: options.withCredentials || false | ||
} | ||
if (protocol === 'https:') { | ||
CORE_HTTPS_OPTS.forEach(function ea(key) { | ||
CORE_HTTPS_OPTS.forEach(function ea (key) { | ||
if (options[key]) { | ||
@@ -206,0 +201,0 @@ opts[key] = options[key] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
13578
3
231