OverviewFilesCommitsIssuesDocsReleasesPeers

mirall-cli / node_modules / bare-http1 / lib / outgoing-message.js

88 lines · javascript ref:
const { Writable } = require('bare-stream')
const errors = require('./errors')
const validate = require('./validate')

module.exports = class HTTPOutgoingMessage extends Writable {
  constructor(socket = null) {
    super()

    this._socket = socket
    this._upgrade = false
    this._headersSent = false
    this._headers = {}
  }

  get socket() {
    return this._socket
  }

  get upgrade() {
    return this._upgrade
  }

  get headersSent() {
    return this._headersSent
  }

  get headers() {
    return this._headers
  }

  set headers(value) {
    if (value) {
      for (const name of Object.keys(value)) {
        const n = name.toLowerCase()

        validate.validateHeaderName(n)
        validate.validateHeaderValue(n, value[name])
      }
    }

    this._headers = value
  }

  getHeader(name) {
    return this._headers[name.toLowerCase()]
  }

  getHeaders() {
    return { ...this._headers }
  }

  hasHeader(name) {
    return name.toLowerCase() in this._headers
  }

  setHeader(name, value) {
    const n = name.toLowerCase()

    validate.validateHeaderName(n)
    validate.validateHeaderValue(n, value)

    this._headers[n] = value
  }

  flushHeaders() {
    if (this._headersSent === true || this._socket === null) return

    this._socket.write(Buffer.from(this._header()))
    this._headersSent = true
  }

  setTimeout(ms, ontimeout) {
    if (ontimeout) this.once('timeout', ontimeout)

    this._socket.setTimeout(ms)

    return this
  }

  _header() {
    throw errors.NOT_IMPLEMENTED()
  }

  _predestroy() {
    if (this._upgrade === false && this._socket !== null) this._socket.destroy()
  }
}