import stream from 'stream'

export default class HtmlWriterStream extends stream.Transform {
  constructor(options = {}) {
    super(options)
    Object.assign(this, options, {
      started: false
    })
  }

  head(data) {
    this.push(this.header)
    this.push(data.toString())
    this.started = true
  }

  body(data) {
    this.push(data.toString())
  }

  foot() {
    this.push(this.footer)
    this.push(null)
  }

  _transform(chunk, encoding, done) {
    const data = chunk.toString()
    this.started ? this.body(data) : this.head(data)
    done()
  }

  _flush(done) {
    this.foot()
    this._lastLineData = null
    done()
  }
}

import stream from 'stream'

export default class HtmlWriterStream extends stream.Transform {
  started = false

  constructor(options) {
    super()
    Object.assign(this, options)
  }

  head() {
    this.push(this.header)
    this.started = true
  }

  body(data) {
    this.push(data)
  }

  foot() {
    this.push(this.footer)
    this.push(null)
  }

  _transform(chunk, encoding, done) {
    this.started || this.head(chunk)
    this.body(chunk)
    done()
  }

  _flush(done) {
    this.foot()
    this._lastLineData = null
    done()
  }
}

refactor of HtmlWriterStream