forked from forwardemail/superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.js
More file actions
201 lines (168 loc) · 4.11 KB
/
response.js
File metadata and controls
201 lines (168 loc) · 4.11 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Module dependencies.
*/
var utils = require('./utils');
var Stream = require('stream');
/**
* Expose `Response`.
*/
module.exports = Response;
/**
* Initialize a new `Response` with the given `xhr`.
*
* - set flags (.ok, .error, etc)
* - parse header
*
* @param {ClientRequest} req
* @param {IncomingMessage} res
* @param {Object} options
* @constructor
* @extends {Stream}
* @implements {ReadableStream}
* @api private
*/
function Response(req, res, options) {
options = options || {};
this.req = req;
this.res = res;
this.links = {};
this.text = res.text;
this.body = 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'));
}
/**
* Inherits from `Stream.prototype`.
*/
Response.prototype.__proto__ = Stream.prototype;
/**
* Get case-insensitive `field` value.
*
* @param {String} field
* @return {String}
* @api public
*/
Response.prototype.get = function(field){
return this.header[field.toLowerCase()];
};
/**
* 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 ' + method + ' ' + path + ' (' + this.status + ')';
var err = new Error(msg);
err.status = this.status;
err.method = method;
err.path = path;
return err;
};
/**
* Set header related properties:
*
* - `.type` the content type without params
*
* A response of "Content-Type: text/plain; charset=utf-8"
* will provide you with a `.type` of "text/plain".
*
* @param {Object} header
* @api private
*/
Response.prototype.setHeaderProperties = function(header){
// TODO: moar!
// TODO: make this a util
// content-type
var ct = this.header['content-type'] || '';
// params
var params = utils.params(ct);
for (var key in params) this[key] = params[key];
this.type = utils.type(ct);
// links
try {
if (header.link) this.links = utils.parseLinks(header.link);
} catch (err) {
// ignore
}
};
/**
* 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`.
*
* For example a 2xx response will give you a `.ok` of __true__
* whereas 5xx will be __false__ and `.error` will be __true__. The
* `.clientError` and `.serverError` are also available to be more
* specific, and `.statusType` is the class of error ranging from 1..5
* sometimes useful for mapping respond colors etc.
*
* "sugar" properties are also defined for common cases. Currently providing:
*
* - .noContent
* - .badRequest
* - .unauthorized
* - .notAcceptable
* - .notFound
*
* @param {Number} status
* @api private
*/
Response.prototype.setStatusProperties = function(status){
var type = status / 100 | 0;
// status / class
this.status = this.statusCode = status;
this.statusType = type;
// basics
this.info = 1 == type;
this.ok = 2 == type;
this.redirect = 3 == type;
this.clientError = 4 == type;
this.serverError = 5 == type;
this.error = (4 == type || 5 == type)
? this.toError()
: false;
// sugar
this.accepted = 202 == status;
this.noContent = 204 == status;
this.badRequest = 400 == status;
this.unauthorized = 401 == status;
this.notAcceptable = 406 == status;
this.forbidden = 403 == status;
this.notFound = 404 == status;
};