333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
 
/**
 * Module dependencies.
 */
 
const util = require('util');
const Stream = require('stream');
const 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);
  const res = (this.res = req.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 = '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'));
  res.on('close', this.emit.bind(this, 'close'));
  res.on('error', this.emit.bind(this, 'error'));
}
 
/**
 * Inherit from `Stream`.
 */
 
util.inherits(Response, Stream);
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() {
  const req = this.req;
  const method = req.method;
  const path = req.path;
 
  const msg = `cannot ${method} ${path} (${this.status})`;
  const 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,
  };
};