superagent
Advanced tools
Comparing version 0.7.0 to 0.8.0
0.8.0 / 2012-08-19 | ||
================== | ||
* add `res.buffered` flag | ||
* add buffering of text/*, json and forms only by default. Closes #61 | ||
* add `.buffer(false)` cancellation | ||
* add cookie jar support [hunterloftis] | ||
* add agent functionality [hunterloftis] | ||
0.7.0 / 2012-08-03 | ||
@@ -3,0 +12,0 @@ ================== |
@@ -24,3 +24,4 @@ | ||
, fs = require('fs') | ||
, qs = require('qs'); | ||
, qs = require('qs') | ||
, util = require('util'); | ||
@@ -34,2 +35,9 @@ /** | ||
/** | ||
* Expose the agent function | ||
*/ | ||
exports.agent = require('./agent'); | ||
/** | ||
* Expose `Part`. | ||
@@ -83,7 +91,7 @@ */ | ||
* Default serialization map. | ||
* | ||
* | ||
* superagent.serialize['application/xml'] = function(obj){ | ||
* return 'generated xml here'; | ||
* }; | ||
* | ||
* | ||
*/ | ||
@@ -98,7 +106,7 @@ | ||
* Default parsers. | ||
* | ||
* | ||
* superagent.parse['application/xml'] = function(res, fn){ | ||
* fn(null, result); | ||
* }; | ||
* | ||
* | ||
*/ | ||
@@ -125,4 +133,5 @@ | ||
this.redirects(5); | ||
this._buffer = true; | ||
this.attachments = []; | ||
this.cookies = ''; | ||
this._redirectList = []; | ||
this.on('response', function(res){ | ||
@@ -234,3 +243,3 @@ self.callback(null, res); | ||
* .end(callback); | ||
* | ||
* | ||
* request.post('/') | ||
@@ -285,3 +294,3 @@ * .type('json') | ||
* .end(callback) | ||
* | ||
* | ||
* // auto json | ||
@@ -291,3 +300,3 @@ * request.post('/user') | ||
* .end(callback) | ||
* | ||
* | ||
* // manual x-www-form-urlencoded | ||
@@ -298,3 +307,3 @@ * request.post('/user') | ||
* .end(callback) | ||
* | ||
* | ||
* // auto x-www-form-urlencoded | ||
@@ -377,3 +386,3 @@ * request.post('/user') | ||
Request.prototype.pipe = function(stream, options){ | ||
this.preventBuffer(); | ||
this.buffer(false); | ||
return this.end().req.on('response', function(res){ | ||
@@ -385,4 +394,5 @@ res.pipe(stream, options); | ||
/** | ||
* Prevent buffering. | ||
* Enable / disable buffering. | ||
* | ||
* @return {Boolean} val | ||
* @return {Request} for chaining | ||
@@ -392,4 +402,4 @@ * @api public | ||
Request.prototype.preventBuffer = function(){ | ||
this._buffer = false; | ||
Request.prototype.buffer = function(val){ | ||
this._buffer = val; | ||
return this; | ||
@@ -407,3 +417,3 @@ }; | ||
Request.prototype.redirect = function(res){ | ||
var url = res.headers.location; | ||
var url = this.protocol + res.headers.location; | ||
delete this.req; | ||
@@ -415,2 +425,3 @@ this.method = 'HEAD' == this.method | ||
this.url = url; | ||
this._redirectList.push(url); | ||
this.emit('redirect', res); | ||
@@ -480,2 +491,3 @@ this.end(this._callback); | ||
var req = this.req = mod.request(options); | ||
this.protocol = url.protocol; | ||
@@ -495,2 +507,5 @@ // expose events | ||
// add cookies | ||
req.setHeader('Cookie', this.cookies); | ||
return req; | ||
@@ -555,4 +570,6 @@ }; | ||
var max = self._maxRedirects | ||
, type = res.headers['content-type'] || '' | ||
, multipart = ~type.indexOf('multipart') | ||
, type = (res.headers['content-type'] || '').split(';')[0].split('/') | ||
, subtype = type[1] | ||
, type = type[0] | ||
, multipart = 'multipart' == type | ||
, redirect = isRedirect(res.statusCode); | ||
@@ -584,2 +601,3 @@ | ||
response.files = files; | ||
response.redirects = self._redirectList; | ||
self.emit('end'); | ||
@@ -591,4 +609,8 @@ self.callback(null, response); | ||
// by default only buffer text/*, json, and messed up thing | ||
if (null == buffer && ('text' == type || 'json' == subtype || 'x-www-form-urlencoded' == subtype)) { | ||
buffer = true; | ||
} | ||
// buffered response | ||
// TODO: optional | ||
if (buffer) { | ||
@@ -609,2 +631,11 @@ res.text = ''; | ||
// unbuffered | ||
if (!buffer) { | ||
self.res = res; | ||
var response = new Response(self.req, self.res); | ||
response.redirects = self._redirectList; | ||
self.emit('response', response); | ||
return; | ||
} | ||
// end event | ||
@@ -614,3 +645,5 @@ self.res = res; | ||
// TODO: unless buffering emit earlier to stream | ||
self.emit('response', new Response(self.req, self.res)); | ||
var response = new Response(self.req, self.res); | ||
response.redirects = self._redirectList; | ||
self.emit('response', response); | ||
self.emit('end'); | ||
@@ -739,2 +772,2 @@ }); | ||
return ~[301, 302, 303, 305, 307].indexOf(code); | ||
} | ||
} |
@@ -6,3 +6,4 @@ | ||
var utils = require('./utils'); | ||
var utils = require('./utils') | ||
, Stream = require('stream'); | ||
@@ -35,8 +36,18 @@ /** | ||
this.files = res.files || {}; | ||
this.buffered = 'string' == typeof this.text; | ||
this.header = this.headers = res.headers; | ||
this.setStatusProperties(res.statusCode); | ||
this.setHeaderProperties(this.header); | ||
this.setEncoding = res.setEncoding.bind(res); | ||
res.on('data', this.emit.bind(this, 'data')); | ||
res.on('end', this.emit.bind(this, 'end')); | ||
} | ||
/** | ||
* Inherits from `Stream.prototype`. | ||
*/ | ||
Response.prototype.__proto__ = Stream.prototype; | ||
/** | ||
* Set header related properties: | ||
@@ -70,2 +81,12 @@ * | ||
/** | ||
* Parse cookies from the header into an array. | ||
*/ | ||
function parseCookies(header) { | ||
return Array.isArray(header) | ||
? header.map(Cookie.parse) | ||
: [Cookie.parse(header)]; | ||
} | ||
/** | ||
* Set flags such as `.ok` based on `status`. | ||
@@ -72,0 +93,0 @@ * |
{ | ||
"name": "superagent" | ||
, "version": "0.7.0" | ||
, "version": "0.8.0" | ||
, "description": "elegant & feature rich browser / node HTTP with a fluent API" | ||
, "keywords": ["http", "ajax", "request", "agent"] | ||
, "author": "TJ Holowaychuk <tj@vision-media.ca>" | ||
, "contributors": ["Hunter Loftis <hunter@hunterloftis.com>"] | ||
, "repository": { "type": "git", "url": "git://github.com/visionmedia/superagent.git" } | ||
@@ -14,5 +15,6 @@ , "dependencies": { | ||
, "methods": "0.0.1" | ||
, "cookiejar": "1.3.0" | ||
} | ||
, "devDependencies": { | ||
"express": "3.0.0beta4" | ||
"express": "3.0.0rc2" | ||
, "should": "*" | ||
@@ -19,0 +21,0 @@ , "mocha": "*" |
@@ -15,3 +15,3 @@ # SuperAgent | ||
$.get('/user/1', function(data, textStatus, xhr){ | ||
}); | ||
@@ -24,3 +24,3 @@ ``` | ||
request.get('/user/1', function(res){ | ||
}); | ||
@@ -40,5 +40,5 @@ ``` | ||
}).success(function(res){ | ||
}).error(function(){ | ||
}); | ||
@@ -56,3 +56,3 @@ ``` | ||
.end(function(res){ | ||
}); | ||
@@ -65,3 +65,3 @@ ``` | ||
request.post('/api/pet', cat, function(res){ | ||
}); | ||
@@ -73,7 +73,7 @@ ``` | ||
Install dependencies: | ||
$ npm install -d | ||
Run em! | ||
$ make test | ||
@@ -93,2 +93,20 @@ | ||
## Persisting an agent (with cookies, ie sessions) | ||
```js | ||
var request = require('superagent'); | ||
var user1 = request.agent(); | ||
user1 | ||
.post('http://localhost:4000/signin') | ||
.send({ user: 'hunter@hunterloftis.com', password: 'password' }) | ||
.end(function(err, res) { | ||
// user1 will manage its own cookies | ||
// res.redirects contains an Array of redirects | ||
}); | ||
``` | ||
Examples: | ||
- [agency tests](superagent/blob/master/test/node/agency.js) | ||
- [express demo app](https://github.com/hunterloftis/component-test/blob/master/lib/users/test/controller.test.js) | ||
## Wiki | ||
@@ -98,3 +116,3 @@ | ||
## License | ||
## License | ||
@@ -101,0 +119,0 @@ (The MIT License) |
Sorry, the diff of this file is not supported yet
89753
22
2668
132
6
+ Addedcookiejar@1.3.0
+ Addedcookiejar@1.3.0(transitive)