response.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. const getType = require('cache-content-type');
  3. const isJSON = require('koa-is-json');
  4. const REAL_STATUS = Symbol('Context#realStatus');
  5. module.exports = {
  6. /**
  7. * Get or set the length of content.
  8. *
  9. * For Get: If the original content length is null or undefined, it will read out
  10. * the body's content length as the return value.
  11. *
  12. * @member {Number} Response#type
  13. * @param {Number} len The content-length to be set.
  14. */
  15. set length(len) {
  16. // copy from koa
  17. // change header name to lower case
  18. this.set('content-length', len);
  19. },
  20. get length() {
  21. // copy from koa
  22. const len = this.header['content-length'];
  23. const body = this.body;
  24. if (len == null) {
  25. if (!body) return;
  26. if (typeof body === 'string') return Buffer.byteLength(body);
  27. if (Buffer.isBuffer(body)) return body.length;
  28. if (isJSON(body)) return Buffer.byteLength(JSON.stringify(body));
  29. return;
  30. }
  31. return parseInt(len, 10);
  32. },
  33. /**
  34. * Get or set the content-type.
  35. *
  36. * For Set: If type is null or undefined, this property will be removed.
  37. *
  38. * For Get: If the value is null or undefined, an empty string will be returned;
  39. * if you have multiple values seperated by `;`, ONLY the first one will be returned.
  40. *
  41. * @member {String} Response#type
  42. * @param {String} type The content-type to be set.
  43. */
  44. set type(type) {
  45. // copy from koa
  46. // Different:
  47. // - change header name to lower case
  48. type = getType(type);
  49. if (type) {
  50. this.set('content-type', type);
  51. } else {
  52. this.remove('content-type');
  53. }
  54. },
  55. get type() {
  56. // copy from koa
  57. const type = this.get('content-type');
  58. if (!type) return '';
  59. return type.split(';')[0];
  60. },
  61. /**
  62. * Get or set a real status code.
  63. *
  64. * e.g.: Using 302 status redirect to the global error page
  65. * instead of show current 500 status page.
  66. * And access log should save 500 not 302,
  67. * then the `realStatus` can help us find out the real status code.
  68. * @member {Number} Response#realStatus
  69. * @return {Number} The status code to be set.
  70. */
  71. get realStatus() {
  72. if (this[REAL_STATUS]) {
  73. return this[REAL_STATUS];
  74. }
  75. return this.status;
  76. },
  77. /**
  78. * Set a real status code.
  79. *
  80. * e.g.: Using 302 status redirect to the global error page
  81. * instead of show current 500 status page.
  82. * And access log should save 500 not 302,
  83. * then the `realStatus` can help us find out the real status code.
  84. * @member {Number} Response#realStatus
  85. * @param {Number} status The status code to be set.
  86. */
  87. set realStatus(status) {
  88. this[REAL_STATUS] = status;
  89. },
  90. };