response.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. const util = require('util');
  6. const Stream = require('stream');
  7. const ResponseBase = require('../response-base');
  8. /**
  9. * Expose `Response`.
  10. */
  11. module.exports = Response;
  12. /**
  13. * Initialize a new `Response` with the given `xhr`.
  14. *
  15. * - set flags (.ok, .error, etc)
  16. * - parse header
  17. *
  18. * @param {Request} req
  19. * @param {Object} options
  20. * @constructor
  21. * @extends {Stream}
  22. * @implements {ReadableStream}
  23. * @api private
  24. */
  25. function Response(req) {
  26. Stream.call(this);
  27. const res = (this.res = req.res);
  28. this.request = req;
  29. this.req = req.req;
  30. this.text = res.text;
  31. this.body = res.body !== undefined ? res.body : {};
  32. this.files = res.files || {};
  33. this.buffered = 'string' == typeof this.text;
  34. this.header = this.headers = res.headers;
  35. this._setStatusProperties(res.statusCode);
  36. this._setHeaderProperties(this.header);
  37. this.setEncoding = res.setEncoding.bind(res);
  38. res.on('data', this.emit.bind(this, 'data'));
  39. res.on('end', this.emit.bind(this, 'end'));
  40. res.on('close', this.emit.bind(this, 'close'));
  41. res.on('error', this.emit.bind(this, 'error'));
  42. }
  43. /**
  44. * Inherit from `Stream`.
  45. */
  46. util.inherits(Response, Stream);
  47. ResponseBase(Response.prototype);
  48. /**
  49. * Implements methods of a `ReadableStream`
  50. */
  51. Response.prototype.destroy = function(err){
  52. this.res.destroy(err);
  53. };
  54. /**
  55. * Pause.
  56. */
  57. Response.prototype.pause = function(){
  58. this.res.pause();
  59. };
  60. /**
  61. * Resume.
  62. */
  63. Response.prototype.resume = function(){
  64. this.res.resume();
  65. };
  66. /**
  67. * Return an `Error` representative of this response.
  68. *
  69. * @return {Error}
  70. * @api public
  71. */
  72. Response.prototype.toError = function() {
  73. const req = this.req;
  74. const method = req.method;
  75. const path = req.path;
  76. const msg = `cannot ${method} ${path} (${this.status})`;
  77. const err = new Error(msg);
  78. err.status = this.status;
  79. err.text = this.text;
  80. err.method = method;
  81. err.path = path;
  82. return err;
  83. };
  84. Response.prototype.setStatusProperties = function(status){
  85. console.warn("In superagent 2.x setStatusProperties is a private method");
  86. return this._setStatusProperties(status);
  87. };
  88. /**
  89. * To json.
  90. *
  91. * @return {Object}
  92. * @api public
  93. */
  94. Response.prototype.toJSON = function() {
  95. return {
  96. req: this.request.toJSON(),
  97. header: this.header,
  98. status: this.status,
  99. text: this.text,
  100. };
  101. };