Socket
Socket
Sign inDemoInstall

request

Package Overview
Dependencies
Maintainers
0
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request - npm Package Compare versions

Comparing version 1.9.0 to 1.9.1

._README.md

2

._package.json

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR0yc��#�#com.macromates.caret{
Mac OS X  2��ATTRZT���#�#com.macromates.caret{
column = 23;
line = 11;
}

@@ -16,3 +16,3 @@ // Copyright 2010-2011 Mikeal Rogers

var http = require('http')
, https = require('https')
, https = false
, url = require('url')

@@ -24,2 +24,6 @@ , util = require('util')

try {
https = require('https');
} catch (e) {}
var toBase64 = function(str) {

@@ -42,2 +46,3 @@ return (new Buffer(str || "", "ascii")).toString("base64");

if (!this.pool) this.pool = globalPool;
this.dests = [];
}

@@ -51,6 +56,2 @@ util.inherits(Request, stream.Stream);

}
Request.prototype.pipe = function (dest) {
this.dest = dest;
}
Request.prototype.request = function () {

@@ -105,7 +106,5 @@ var options = this;

options.host = options.proxy.hostname;
// options.client = http.createClient(options.proxy.port, options.proxy.hostname, options.proxy.protocol === 'https:');
} else {
options.port = options.uri.port;
options.host = options.uri.hostname;
// options.client = http.createClient(options.uri.port, options.uri.hostname, options.uri.protocol === 'https:');
}

@@ -170,16 +169,22 @@

{"http:":http, "https:":https}[options.proxy ? options.proxy.protocol : options.uri.protocol]
if (!options.httpModule) throw new Error("Invalid protocol");
if (options.maxSockets) {
options.agent = options.getAgent(options.host, options.port);
options.agent.maxSockets = options.maxSockets;
if (options.pool === false) {
options.agent = false;
} else {
if (options.maxSockets) {
options.agent = options.getAgent(options.host, options.port);
options.agent.maxSockets = options.maxSockets;
}
if (options.pool.maxSockets) {
options.agent = options.getAgent(options.host, options.port);
options.agent.maxSockets = options.pool.maxSockets;
}
}
if (options.pool.maxSockets) {
options.agent = options.getAgent(options.host, options.port);
options.agent.maxSockets = options.pool.maxSockets;
}
options.req = options.httpModule.request(options, function (response) {
options.response = response;
if (setHost) delete options.headers.host;
if (response.statusCode >= 300 &&

@@ -200,2 +205,3 @@ response.statusCode < 400 &&

delete options.req;
delete options.agent;
if (options.headers) {

@@ -208,5 +214,14 @@ delete options.headers.host;

options._redirectsFollowed = 0;
if (options.encoding) response.setEncoding(options.encoding);
if (options.dest) {
response.pipe(options.dest);
if (options.encoding) {
if (options.dests.length !== 0) {
console.error("Ingoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.");
} else {
response.setEncoding(options.encoding);
}
}
if (options.dests.length !== 0) {
options.dests.forEach(function (dest) {
response.pipe(dest);
})
if (options.onResponse) options.onResponse(null, response);

@@ -220,5 +235,8 @@ if (options.callback) options.callback(null, response, options.responseBodyStream);

var buffer = '';
response
.on("data", function (chunk) { buffer += chunk; })
.on("end", function () { options.callback(null, response, buffer); })
response.on("data", function (chunk) {
buffer += chunk;
})
response.on("end", function () {
options.callback(null, response, buffer);
})
;

@@ -232,4 +250,8 @@ }

this.once('pipe', function (src) {
options.once('pipe', function (src) {
if (options.ntick) throw new Error("You cannot pipe to this stream after the first nextTick() after creation of the request stream.")
options.src = src;
options.on('pipe', function () {
console.error("You have already piped to this stream. Pipeing twice is likely to break the request.")
})
})

@@ -247,15 +269,26 @@

}
options.ntick = true;
})
}
Request.prototype.write = function (chunk) {
Request.prototype.pipe = function (dest) {
if (this.response) throw new Error("You cannot pipe after the response event.")
this.dests.push(dest);
}
Request.prototype.write = function () {
if (!this.req) throw new Error("This request has been piped before http.request() was called.");
this.req.write(chunk);
this.req.write.apply(this.req, arguments);
}
Request.prototype.end = function () {
this.req.end();
if (!this.req) throw new Error("This request has been piped before http.request() was called.");
this.req.end.apply(this.req, arguments);
}
Request.prototype.pause = function () {
if (!this.req) throw new Error("This request has been piped before http.request() was called.");
this.req.pause.apply(this.req, arguments);
}
Request.prototype.resume = function () {
if (!this.req) throw new Error("This request has been piped before http.request() was called.");
this.req.resume.apply(this.req, arguments);
}
function request (options, callback) {

@@ -268,3 +301,2 @@ if (callback) options.callback = callback;

module.exports = request;

@@ -287,2 +319,3 @@

de.head = def(request.head);
de.del = def(request.del);
return d;

@@ -293,21 +326,19 @@ }

request.post = function (options, callback) {
options.method = 'POST';
if (!options.body && !options.requestBodyStream && !options.json && !options.multipart) {
console.error("HTTP POST requests need a body or requestBodyStream");
}
request(options, callback);
options.method = 'POST';
return request(options, callback);
};
request.put = function (options, callback) {
options.method = 'PUT';
if (!options.body && !options.requestBodyStream && !options.json && !options.multipart) {
console.error("HTTP PUT requests need a body or requestBodyStream");
}
request(options, callback);
options.method = 'PUT';
return request(options, callback);
};
request.head = function (options, callback) {
options.method = 'HEAD';
options.method = 'HEAD';
if (options.body || options.requestBodyStream || options.json || options.multipart) {
throw new Error("HTTP HEAD requests MUST NOT include a request body.");
}
request(options, callback);
return request(options, callback);
};
request.del = function (options, callback) {
options.method = 'DELETE';
return request(options, callback);
}
{ "name" : "request"
, "description" : "Simplified HTTP request client."
, "tags" : ["http", "simple", "util", "utility"]
, "version" : "1.9.0"
, "version" : "1.9.1"
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>"

@@ -6,0 +6,0 @@ , "repository" :

@@ -21,2 +21,11 @@ # Request -- Simplified HTTP request method

<pre>
var request = require('request');
request({uri:'http://www.google.com'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
sys.puts(body) // Print the google web page.
}
})
</pre>
#### request(options, callback)

@@ -32,3 +41,2 @@

* `multipart` - (experimental) array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See example below.
* `client` - existing http client object (when undefined a new one will be created and assigned to this property so you can keep around a reference to it if you would like use keep-alive on later request)
* `followRedirect` - follow HTTP 3xx responses as redirects. defaults to true.

@@ -38,4 +46,4 @@ * `maxRedirects` - the maximum number of redirects to follow, defaults to 10.

* `encoding` - Encoding to be used on response.setEncoding when buffering the response data.
* `requestBodyStream` - Stream to read request body chunks from.
* `responseBodyStream` - Stream to write body chunks to. When set this option will be passed as the last argument to the callback instead of the entire body.
* `pool` - A hash object containing the agents for these requests. If omitted this request will use the global pool which is set to node's default maxSockets.
* `pool.maxSockets` - Integer containing the maximum amount of sockets in the pool.

@@ -45,10 +53,2 @@ The callback argument gets 3 arguments. The first is an error when applicable (usually from the http.Client option not the http.ClientRequest object). The second in an http.ClientResponse object. The third is the response body buffer.

Examples:
<pre>
var request = require('request');
request({uri:'http://www.google.com'}, function (error, response, body) {
if (!error && response.statusCode == 200) {
sys.puts(body) // Print the google web page.
}
})
</pre>

@@ -79,2 +79,49 @@ <pre>

It's also worth noting that the options argument will mutate. When following a redirect the uri values will change. After setting up client options it will set options.client.
**Notice for 2.0**
You should no longer recycle mutations in the options object. Because node 0.4.0 has an internal pooling mechanism the preferred way of sharing a connection is using agents which request simplifies with it's new pool API. Therefor options.client and some other mutations have been deprecated.
requestBodyStream and responseBodyStream are also deprecated in favor of a more standard pipe interface documented below.
### stream.pipe(request(options)) and request(options).pipe(stream)
Previous versions of request had no return value and only accepted callbacks and streams for pumping in the options object.
Node has solidified it's Stream interface and request 2.0 is now compliant with that interface.
The return value of request() is now a Request object, which is a valid stream.
As a writable stream it accepts the body of an HTTP request. As a readable stream it emits the data events for the response.
<pre>
var r = request(
{ url: "http://mysite.com/image.png"
, method: 'PUT'
, headers: {'content-type': 'image/png'}
}
)
fs.createReadStream('image.png').pipe(r)
r.pipe(fs.createWriteStream('pushlog.txt'))
</pre>
# Convenience methods
### request.defaults(options)
This method returns a wrapper around the normal request API that defaults to whatever options you pass in to it.
### request.put
Same as request() but defaults to `method: "PUT"`.
### request.post
Same as request() but defaults to `method: "POST"`.
### request.head
Same as request() but defaults to `method: "HEAD"`.
### request.get
Alias to normal request method for uniformity.

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR0yg��!�!com.macromates.caret{
column = 0;
line = 5;
Mac OS X  2��ATTRZT���!�!com.macromates.caret{
column = 3;
line = 4;
}

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR0yi��"�"com.macromates.caret{
column = 7;
line = 79;
Mac OS X  2��ATTRZT���#�#com.macromates.caret{
column = 33;
line = 27;
}

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR0yk��"�"com.macromates.caret{
Mac OS X  2��ATTRZT���"�"com.macromates.caret{
column = 0;
line = 11;
}

@@ -1,2 +0,6 @@

var http = require('http');
var http = require('http')
, events = require('events')
, stream = require('stream')
, assert = require('assert')
;

@@ -12,1 +16,31 @@ exports.createServer = function (port) {

}
exports.createPostStream = function (text) {
var postStream = new stream.Stream();
postStream.writeable = true;
postStream.readable = true;
setTimeout(function () {postStream.emit('data', new Buffer(text)); postStream.emit('end')}, 0);
return postStream;
}
exports.createPostValidator = function (text) {
var l = function (req, resp) {
var r = '';
req.on('data', function (chunk) {r += chunk})
req.on('end', function () {
if (r !== text) console.log(r, text);
assert.ok(r === text)
resp.writeHead(200, {'content-type':'text/plain'})
resp.write('OK')
resp.end()
})
}
return l;
}
exports.createGetResponse = function (text) {
var l = function (req, resp) {
resp.writeHead(200, {'content-type':'text/plain'})
resp.write(text)
resp.end()
}
return l;
}

@@ -10,39 +10,9 @@ var server = require('./server')

var createPostStream = function (text) {
var postStream = new stream.Stream();
postStream.writeable = true;
postStream.readable = true;
setTimeout(function () {postStream.emit('data', new Buffer(text)); postStream.emit('end')}, 0);
return postStream;
}
var createPostValidator = function (text) {
var l = function (req, resp) {
var r = '';
req.on('data', function (chunk) {r += chunk})
req.on('end', function () {
if (r !== text) console.log(r, text);
assert.ok(r === text)
resp.writeHead(200, {'content-type':'text/plain'})
resp.write('OK')
resp.end()
})
}
return l;
}
var createGetResponse = function (text) {
var l = function (req, resp) {
resp.writeHead(200, {'content-type':'text/plain'})
resp.write(text)
resp.end()
}
return l;
}
var tests =
{ testGet :
{ resp : createGetResponse("TESTING!")
{ resp : server.createGetResponse("TESTING!")
, expectBody: "TESTING!"
}
, testPutString :
{ resp : createPostValidator("PUTTINGDATA")
{ resp : server.createPostValidator("PUTTINGDATA")
, method : "PUT"

@@ -52,3 +22,3 @@ , body : "PUTTINGDATA"

, testPutBuffer :
{ resp : createPostValidator("PUTTINGDATA")
{ resp : server.createPostValidator("PUTTINGDATA")
, method : "PUT"

@@ -58,8 +28,8 @@ , body : new Buffer("PUTTINGDATA")

, testPutStream :
{ resp : createPostValidator("PUTTINGDATA")
{ resp : server.createPostValidator("PUTTINGDATA")
, method : "PUT"
, requestBodyStream : createPostStream("PUTTINGDATA")
, requestBodyStream : server.createPostStream("PUTTINGDATA")
}
, testPutJSON :
{ resp : createPostValidator(JSON.stringify({foo: 'bar'}))
{ resp : server.createPostValidator(JSON.stringify({foo: 'bar'}))
, method: "PUT"

@@ -69,3 +39,3 @@ , json: {foo: 'bar'}

, testPutMultipart :
{ resp: createPostValidator(
{ resp: server.createPostValidator(
'--frontier\r\n' +

@@ -72,0 +42,0 @@ 'content-type: text/html\r\n' +

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc