UNPKG

11.7 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * Module dependencies.
5 */
6
7const util = require('node:util');
8const Stream = require('node:stream');
9const ResponseBase = require('../response-base');
10const _require = require('../utils'),
11 mixin = _require.mixin;
12
13/**
14 * Expose `Response`.
15 */
16
17module.exports = Response;
18
19/**
20 * Initialize a new `Response` with the given `xhr`.
21 *
22 * - set flags (.ok, .error, etc)
23 * - parse header
24 *
25 * @param {Request} req
26 * @param {Object} options
27 * @constructor
28 * @extends {Stream}
29 * @implements {ReadableStream}
30 * @api private
31 */
32
33function Response(request) {
34 Stream.call(this);
35 this.res = request.res;
36 const res = this.res;
37 this.request = request;
38 this.req = request.req;
39 this.text = res.text;
40 this.files = res.files || {};
41 this.buffered = request._resBuffered;
42 this.headers = res.headers;
43 this.header = this.headers;
44 this._setStatusProperties(res.statusCode);
45 this._setHeaderProperties(this.header);
46 this.setEncoding = res.setEncoding.bind(res);
47 res.on('data', this.emit.bind(this, 'data'));
48 res.on('end', this.emit.bind(this, 'end'));
49 res.on('close', this.emit.bind(this, 'close'));
50 res.on('error', this.emit.bind(this, 'error'));
51}
52
53// Lazy access res.body.
54// https://github.com/nodejs/node/pull/39520#issuecomment-889697136
55Object.defineProperty(Response.prototype, 'body', {
56 get() {
57 return this._body === undefined ? this.res.body === undefined ? {} : this.res.body : this._body;
58 },
59 set(value) {
60 this._body = value;
61 }
62});
63
64/**
65 * Inherit from `Stream`.
66 */
67
68util.inherits(Response, Stream);
69mixin(Response.prototype, ResponseBase.prototype);
70
71/**
72 * Implements methods of a `ReadableStream`
73 */
74
75Response.prototype.destroy = function (error) {
76 this.res.destroy(error);
77};
78
79/**
80 * Pause.
81 */
82
83Response.prototype.pause = function () {
84 this.res.pause();
85};
86
87/**
88 * Resume.
89 */
90
91Response.prototype.resume = function () {
92 this.res.resume();
93};
94
95/**
96 * Return an `Error` representative of this response.
97 *
98 * @return {Error}
99 * @api public
100 */
101
102Response.prototype.toError = function () {
103 const req = this.req;
104 const method = req.method;
105 const path = req.path;
106 const message = `cannot ${method} ${path} (${this.status})`;
107 const error = new Error(message);
108 error.status = this.status;
109 error.text = this.text;
110 error.method = method;
111 error.path = path;
112 return error;
113};
114Response.prototype.setStatusProperties = function (status) {
115 console.warn('In superagent 2.x setStatusProperties is a private method');
116 return this._setStatusProperties(status);
117};
118
119/**
120 * To json.
121 *
122 * @return {Object}
123 * @api public
124 */
125
126Response.prototype.toJSON = function () {
127 return {
128 req: this.request.toJSON(),
129 header: this.header,
130 status: this.status,
131 text: this.text
132 };
133};
134//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJ1dGlsIiwicmVxdWlyZSIsIlN0cmVhbSIsIlJlc3BvbnNlQmFzZSIsIl9yZXF1aXJlIiwibWl4aW4iLCJtb2R1bGUiLCJleHBvcnRzIiwiUmVzcG9uc2UiLCJyZXF1ZXN0IiwiY2FsbCIsInJlcyIsInJlcSIsInRleHQiLCJmaWxlcyIsImJ1ZmZlcmVkIiwiX3Jlc0J1ZmZlcmVkIiwiaGVhZGVycyIsImhlYWRlciIsIl9zZXRTdGF0dXNQcm9wZXJ0aWVzIiwic3RhdHVzQ29kZSIsIl9zZXRIZWFkZXJQcm9wZXJ0aWVzIiwic2V0RW5jb2RpbmciLCJiaW5kIiwib24iLCJlbWl0IiwiT2JqZWN0IiwiZGVmaW5lUHJvcGVydHkiLCJwcm90b3R5cGUiLCJnZXQiLCJfYm9keSIsInVuZGVmaW5lZCIsImJvZHkiLCJzZXQiLCJ2YWx1ZSIsImluaGVyaXRzIiwiZGVzdHJveSIsImVycm9yIiwicGF1c2UiLCJyZXN1bWUiLCJ0b0Vycm9yIiwibWV0aG9kIiwicGF0aCIsIm1lc3NhZ2UiLCJzdGF0dXMiLCJFcnJvciIsInNldFN0YXR1c1Byb3BlcnRpZXMiLCJjb25zb2xlIiwid2FybiIsInRvSlNPTiJdLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9ub2RlL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