forked from Mirrors/doipjs
126 lines
9.7 KiB
JavaScript
126 lines
9.7 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
/**
|
||
|
* Module dependencies.
|
||
|
*/
|
||
|
var util = require('util');
|
||
|
|
||
|
var Stream = require('stream');
|
||
|
|
||
|
var ResponseBase = require('../response-base');
|
||
|
/**
|
||
|
* Expose `Response`.
|
||
|
*/
|
||
|
|
||
|
|
||
|
module.exports = Response;
|
||
|
/**
|
||
|
* Initialize a new `Response` with the given `xhr`.
|
||
|
*
|
||
|
* - set flags (.ok, .error, etc)
|
||
|
* - parse header
|
||
|
*
|
||
|
* @param {Request} req
|
||
|
* @param {Object} options
|
||
|
* @constructor
|
||
|
* @extends {Stream}
|
||
|
* @implements {ReadableStream}
|
||
|
* @api private
|
||
|
*/
|
||
|
|
||
|
function Response(req) {
|
||
|
Stream.call(this);
|
||
|
this.res = req.res;
|
||
|
var res = this.res;
|
||
|
this.request = req;
|
||
|
this.req = req.req;
|
||
|
this.text = res.text;
|
||
|
this.body = res.body === undefined ? {} : res.body;
|
||
|
this.files = res.files || {};
|
||
|
this.buffered = req._resBuffered;
|
||
|
this.headers = res.headers;
|
||
|
this.header = this.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'));
|
||
|
res.on('close', this.emit.bind(this, 'close'));
|
||
|
res.on('error', this.emit.bind(this, 'error'));
|
||
|
}
|
||
|
/**
|
||
|
* Inherit from `Stream`.
|
||
|
*/
|
||
|
|
||
|
|
||
|
util.inherits(Response, Stream); // eslint-disable-next-line new-cap
|
||
|
|
||
|
ResponseBase(Response.prototype);
|
||
|
/**
|
||
|
* Implements methods of a `ReadableStream`
|
||
|
*/
|
||
|
|
||
|
Response.prototype.destroy = function (err) {
|
||
|
this.res.destroy(err);
|
||
|
};
|
||
|
/**
|
||
|
* Pause.
|
||
|
*/
|
||
|
|
||
|
|
||
|
Response.prototype.pause = function () {
|
||
|
this.res.pause();
|
||
|
};
|
||
|
/**
|
||
|
* Resume.
|
||
|
*/
|
||
|
|
||
|
|
||
|
Response.prototype.resume = function () {
|
||
|
this.res.resume();
|
||
|
};
|
||
|
/**
|
||
|
* Return an `Error` representative of this response.
|
||
|
*
|
||
|
* @return {Error}
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
|
||
|
Response.prototype.toError = function () {
|
||
|
var req = this.req;
|
||
|
var method = req.method;
|
||
|
var path = req.path;
|
||
|
var msg = "cannot ".concat(method, " ").concat(path, " (").concat(this.status, ")");
|
||
|
var err = new Error(msg);
|
||
|
err.status = this.status;
|
||
|
err.text = this.text;
|
||
|
err.method = method;
|
||
|
err.path = path;
|
||
|
return err;
|
||
|
};
|
||
|
|
||
|
Response.prototype.setStatusProperties = function (status) {
|
||
|
console.warn('In superagent 2.x setStatusProperties is a private method');
|
||
|
return this._setStatusProperties(status);
|
||
|
};
|
||
|
/**
|
||
|
* To json.
|
||
|
*
|
||
|
* @return {Object}
|
||
|
* @api public
|
||
|
*/
|
||
|
|
||
|
|
||
|
Response.prototype.toJSON = function () {
|
||
|
return {
|
||
|
req: this.request.toJSON(),
|
||
|
header: this.header,
|
||
|
status: this.status,
|
||
|
text: this.text
|
||
|
};
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ub2RlL3Jlc3BvbnNlLmpzIl0sIm5hbWVzIjpbInV0aWwiLCJyZXF1aXJlIiwiU3RyZWFtIiwiUmVzcG9uc2VCYXNlIiwibW9kdWxlIiwiZXhwb3J0cyIsIlJlc3BvbnNlIiwicmVxIiwiY2FsbCIsInJlcyIsInJlcXVlc3QiLCJ0ZXh0IiwiYm9keSIsInVuZGVmaW5lZCIsImZpbGVzIiwiYnV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