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
'use strict';
 
const getType = require('cache-content-type');
const isJSON = require('koa-is-json');
 
const REAL_STATUS = Symbol('Context#realStatus');
 
module.exports = {
 
  /**
   * Get or set the length of content.
   *
   * For Get: If the original content length is null or undefined, it will read out
   * the body's content length as the return value.
   *
   * @member {Number} Response#type
   * @param {Number} len The content-length to be set.
   */
  set length(len) {
    // copy from koa
    // change header name to lower case
    this.set('content-length', len);
  },
 
  get length() {
    // copy from koa
    const len = this.header['content-length'];
    const body = this.body;
 
    if (len == null) {
      if (!body) return;
      if (typeof body === 'string') return Buffer.byteLength(body);
      if (Buffer.isBuffer(body)) return body.length;
      if (isJSON(body)) return Buffer.byteLength(JSON.stringify(body));
      return;
    }
 
    return parseInt(len, 10);
  },
 
  /**
   * Get or set the content-type.
   *
   * For Set: If type is null or undefined, this property will be removed.
   *
   * For Get: If the value is null or undefined, an empty string will be returned;
   * if you have multiple values seperated by `;`, ONLY the first one will be returned.
   *
   * @member {String} Response#type
   * @param {String} type The content-type to be set.
   */
  set type(type) {
    // copy from koa
    // Different:
    //  - change header name to lower case
    type = getType(type);
    if (type) {
      this.set('content-type', type);
    } else {
      this.remove('content-type');
    }
  },
 
  get type() {
    // copy from koa
    const type = this.get('content-type');
    if (!type) return '';
    return type.split(';')[0];
  },
 
  /**
   * Get or set a real status code.
   *
   * e.g.: Using 302 status redirect to the global error page
   * instead of show current 500 status page.
   * And access log should save 500 not 302,
   * then the `realStatus` can help us find out the real status code.
   * @member {Number} Response#realStatus
   * @return {Number} The status code to be set.
   */
  get realStatus() {
    if (this[REAL_STATUS]) {
      return this[REAL_STATUS];
    }
    return this.status;
  },
 
  /**
   * Set a real status code.
   *
   * e.g.: Using 302 status redirect to the global error page
   * instead of show current 500 status page.
   * And access log should save 500 not 302,
   * then the `realStatus` can help us find out the real status code.
   * @member {Number} Response#realStatus
   * @param {Number} status The status code to be set.
   */
  set realStatus(status) {
    this[REAL_STATUS] = status;
  },
};