superagent
Advanced tools
Comparing version 2.0.0 to 2.1.0-beta.1
@@ -500,19 +500,19 @@ /** | ||
if (err) { | ||
return self.callback(err, res); | ||
} | ||
var new_err; | ||
try { | ||
if (res.status >= 200 && res.status < 300) { | ||
return self.callback(err, res); | ||
if (res.status < 200 || res.status >= 300) { | ||
new_err = new Error(res.statusText || 'Unsuccessful HTTP response'); | ||
new_err.original = err; | ||
new_err.response = res; | ||
new_err.status = res.status; | ||
} | ||
} catch(e) { | ||
new_err = e; // #985 touching res may cause INVALID_STATE_ERR on old Android | ||
} | ||
var new_err = new Error(res.statusText || 'Unsuccessful HTTP response'); | ||
new_err.original = err; | ||
new_err.response = res; | ||
new_err.status = res.status; | ||
// #1000 don't catch errors from the callback to avoid double calling it | ||
if (new_err) { | ||
self.callback(new_err, res); | ||
} catch(e) { | ||
self.callback(e); // #985 touching res may cause INVALID_STATE_ERR on old Android | ||
} else { | ||
self.callback(null, res); | ||
} | ||
@@ -519,0 +519,0 @@ }); |
@@ -355,6 +355,3 @@ | ||
self.res = res; | ||
var response = new Response(self); | ||
self.response = response; | ||
self.emit('response', response); | ||
response.redirects = self._redirectList; | ||
self._emitResponse(); | ||
if (self._aborted) return; | ||
@@ -383,5 +380,3 @@ | ||
Request.prototype.buffer = function(val){ | ||
this._buffer = false === val | ||
? false | ||
: true; | ||
this._buffer = (false !== val); | ||
return this; | ||
@@ -637,2 +632,14 @@ }; | ||
Request.prototype._emitResponse = function(body, files){ | ||
var response = new Response(this); | ||
this.response = response; | ||
response.redirects = this._redirectList; | ||
if (undefined !== body) { | ||
response.body = body; | ||
} | ||
response.files = files; | ||
this.emit('response', response); | ||
return response; | ||
}; | ||
Request.prototype.end = function(fn){ | ||
@@ -697,6 +704,3 @@ var self = this; | ||
var mime = utils.type(res.headers['content-type'] || '') || 'text/plain'; | ||
var len = res.headers['content-length']; | ||
var type = mime.split('/'); | ||
var subtype = type[1]; | ||
var type = type[0]; | ||
var type = mime.split('/')[0]; | ||
var multipart = 'multipart' == type; | ||
@@ -714,8 +718,4 @@ var redirect = isRedirect(res.statusCode); | ||
if ('HEAD' == self.method) { | ||
var response = new Response(self); | ||
self.response = response; | ||
response.redirects = self._redirectList; | ||
self.emit('response', response); | ||
self.callback(null, response); | ||
self.emit('end'); | ||
self.callback(null, self._emitResponse()); | ||
return; | ||
@@ -729,60 +729,46 @@ } | ||
if (!parser) { | ||
if (multipart) { | ||
var form = new formidable.IncomingForm(); | ||
parser = form.parse.bind(form); | ||
buffer = true; | ||
} else if (isImage(mime)) { | ||
parser = exports.parse.image; | ||
buffer = true; // For backwards-compatibility buffering default is ad-hoc MIME-dependent | ||
} else if ('text' == type) { | ||
parser = exports.parse.text; | ||
buffer = (buffer !== false); | ||
} else if (exports.parse[mime]) { | ||
parser = exports.parse[mime]; | ||
// don't buffer multipart | ||
if (multipart) buffer = false; | ||
// TODO: make all parsers take callbacks | ||
if (!parser && multipart) { | ||
var form = new formidable.IncomingForm; | ||
form.parse(res, function(err, fields, files){ | ||
if (err) return self.callback(err); | ||
var response = new Response(self); | ||
self.response = response; | ||
response.body = fields; | ||
response.files = files; | ||
response.redirects = self._redirectList; | ||
self.emit('end'); | ||
self.callback(null, response); | ||
}); | ||
return; | ||
// everyone wants their own white-labeled json | ||
} else if (isJSON(mime)) { | ||
parser = exports.parse['application/json']; | ||
buffer = (buffer !== false); | ||
} else if (buffer) { | ||
parser = exports.parse.text; | ||
} | ||
} | ||
// check for images, one more special treatment | ||
if (!parser && isImage(mime)) { | ||
exports.parse.image(res, function(err, obj){ | ||
if (err) return self.callback(err); | ||
var response = new Response(self); | ||
self.response = response; | ||
response.body = obj; | ||
response.redirects = self._redirectList; | ||
self.emit('end'); | ||
self.callback(null, response); | ||
}); | ||
return; | ||
} | ||
// by default only buffer text/*, json and messed up thing from hell | ||
if (null == buffer && isText(mime) || isJSON(mime)) buffer = true; | ||
if (undefined === buffer && isText(mime) || isJSON(mime)) buffer = true; | ||
// parser | ||
var parse = 'text' == type | ||
? exports.parse.text | ||
: exports.parse[mime]; | ||
var parserHandlesEnd = false; | ||
if (parser) { | ||
try { | ||
// Unbuffered parsers are supposed to emit response early, | ||
// which is weird BTW, because response.body won't be there. | ||
parserHandlesEnd = buffer; | ||
// everyone wants their own white-labeled json | ||
if (!parse && isJSON(mime)) parse = exports.parse['application/json']; | ||
parser(res, function(err, obj, files) { | ||
if (err && !self._aborted) { | ||
return self.callback(err); | ||
} | ||
// buffered response | ||
if (buffer) parse = parse || exports.parse.text; | ||
res.body = obj; // Deprecated. For backwards compat only. It's not the response object user sees. | ||
// explicit parser | ||
if (parser) parse = parser; | ||
// parse | ||
if (parse) { | ||
try { | ||
parse(res, function(err, obj){ | ||
if (err && !self._aborted) self.callback(err); | ||
res.body = obj; | ||
if (parserHandlesEnd) { | ||
self.emit('end'); | ||
self.callback(null, self._emitResponse(obj, files)); | ||
} | ||
}); | ||
@@ -795,11 +781,8 @@ } catch (err) { | ||
self.res = res; | ||
// unbuffered | ||
if (!buffer) { | ||
debug('unbuffered %s %s', self.method, self.url); | ||
self.res = res; | ||
var response = new Response(self); | ||
self.response = response; | ||
response.redirects = self._redirectList; | ||
self.emit('response', response); | ||
self.callback(null, response); | ||
self.callback(null, self._emitResponse()); | ||
if (multipart) return // allow multipart to handle end event | ||
@@ -814,15 +797,10 @@ res.on('end', function(){ | ||
// terminating events | ||
self.res = res; | ||
res.on('error', function(err){ | ||
self.callback(err, null); | ||
}); | ||
res.on('end', function(){ | ||
if (!parserHandlesEnd) res.on('end', function(){ | ||
debug('end %s %s', self.method, self.url); | ||
// TODO: unless buffering emit earlier to stream | ||
var response = new Response(self); | ||
self.response = response; | ||
response.redirects = self._redirectList; | ||
self.emit('response', response); | ||
self.callback(null, response); | ||
self.emit('end'); | ||
self.callback(null, self._emitResponse()); | ||
}); | ||
@@ -829,0 +807,0 @@ }); |
{ | ||
"name": "superagent", | ||
"version": "2.0.0", | ||
"version": "2.1.0-beta.1", | ||
"description": "elegant & feature rich browser / node HTTP with a fluent API", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -1088,19 +1088,19 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.superagent = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
if (err) { | ||
return self.callback(err, res); | ||
} | ||
var new_err; | ||
try { | ||
if (res.status >= 200 && res.status < 300) { | ||
return self.callback(err, res); | ||
if (res.status < 200 || res.status >= 300) { | ||
new_err = new Error(res.statusText || 'Unsuccessful HTTP response'); | ||
new_err.original = err; | ||
new_err.response = res; | ||
new_err.status = res.status; | ||
} | ||
} catch(e) { | ||
new_err = e; // #985 touching res may cause INVALID_STATE_ERR on old Android | ||
} | ||
var new_err = new Error(res.statusText || 'Unsuccessful HTTP response'); | ||
new_err.original = err; | ||
new_err.response = res; | ||
new_err.status = res.status; | ||
// #1000 don't catch errors from the callback to avoid double calling it | ||
if (new_err) { | ||
self.callback(new_err, res); | ||
} catch(e) { | ||
self.callback(e); // #985 touching res may cause INVALID_STATE_ERR on old Android | ||
} else { | ||
self.callback(null, res); | ||
} | ||
@@ -1107,0 +1107,0 @@ }); |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance 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
325746
4283
2