[6a3a178] | 1 | 'use strict'
|
---|
| 2 | const http = require('http')
|
---|
| 3 | const { STATUS_CODES } = http
|
---|
| 4 |
|
---|
| 5 | const Headers = require('./headers.js')
|
---|
| 6 | const Body = require('./body.js')
|
---|
| 7 | const { clone, extractContentType } = Body
|
---|
| 8 |
|
---|
| 9 | const INTERNALS = Symbol('Response internals')
|
---|
| 10 |
|
---|
| 11 | class Response extends Body {
|
---|
| 12 | constructor (body = null, opts = {}) {
|
---|
| 13 | super(body, opts)
|
---|
| 14 |
|
---|
| 15 | const status = opts.status || 200
|
---|
| 16 | const headers = new Headers(opts.headers)
|
---|
| 17 |
|
---|
| 18 | if (body !== null && body !== undefined && !headers.has('Content-Type')) {
|
---|
| 19 | const contentType = extractContentType(body)
|
---|
| 20 | if (contentType)
|
---|
| 21 | headers.append('Content-Type', contentType)
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | this[INTERNALS] = {
|
---|
| 25 | url: opts.url,
|
---|
| 26 | status,
|
---|
| 27 | statusText: opts.statusText || STATUS_CODES[status],
|
---|
| 28 | headers,
|
---|
| 29 | counter: opts.counter,
|
---|
| 30 | trailer: Promise.resolve(opts.trailer || new Headers()),
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | get trailer () {
|
---|
| 35 | return this[INTERNALS].trailer
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | get url () {
|
---|
| 39 | return this[INTERNALS].url || ''
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | get status () {
|
---|
| 43 | return this[INTERNALS].status
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | get ok () {
|
---|
| 47 | return this[INTERNALS].status >= 200 && this[INTERNALS].status < 300
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | get redirected () {
|
---|
| 51 | return this[INTERNALS].counter > 0
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | get statusText () {
|
---|
| 55 | return this[INTERNALS].statusText
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | get headers () {
|
---|
| 59 | return this[INTERNALS].headers
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | clone () {
|
---|
| 63 | return new Response(clone(this), {
|
---|
| 64 | url: this.url,
|
---|
| 65 | status: this.status,
|
---|
| 66 | statusText: this.statusText,
|
---|
| 67 | headers: this.headers,
|
---|
| 68 | ok: this.ok,
|
---|
| 69 | redirected: this.redirected,
|
---|
| 70 | trailer: this.trailer,
|
---|
| 71 | })
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | get [Symbol.toStringTag] () {
|
---|
| 75 | return 'Response'
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | module.exports = Response
|
---|
| 80 |
|
---|
| 81 | Object.defineProperties(Response.prototype, {
|
---|
| 82 | url: { enumerable: true },
|
---|
| 83 | status: { enumerable: true },
|
---|
| 84 | ok: { enumerable: true },
|
---|
| 85 | redirected: { enumerable: true },
|
---|
| 86 | statusText: { enumerable: true },
|
---|
| 87 | headers: { enumerable: true },
|
---|
| 88 | clone: { enumerable: true },
|
---|
| 89 | })
|
---|