# All files / src/streamComponents reporter.js

100% Statements 29/29
100% Branches 6/6
100% Functions 4/4
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    291x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    12x
    12x
    12x
    12x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2ximport { log } from "../util.js"
    import { PassThrough } from 'node:stream'

    // Buffer.from('1,erick-1,1-text,1').byteLength
    // Buffer.from('{"id":0,"name":"erick-1","desc":"1-text","age":"1"}').byteLength

    const A_HUNDRED_PERCENT = 100
    export default class Reporter {
      #loggerFn
      LINE_LENGTH_AFTER_TURNED_INTO_JSON = 40
      constructor({ logger = log } = {}) {
        this.#loggerFn = logger
      }

      #onData(amount) {
        let totalChunks = 0
        return (chunk) => {
          totalChunks += (chunk.length - this.LINE_LENGTH_AFTER_TURNED_INTO_JSON)
          const processed = A_HUNDRED_PERCENT / amount * totalChunks
          this.#loggerFn(`processed ${processed.toFixed(2)}%`)
        }
      }
      progress(amount) {
        const progress = PassThrough()
        progress.on('data', this.#onData(amount))
        progress.on('end', () => this.#loggerFn(`processed ${A_HUNDRED_PERCENT}.00%`))
        return progress
      }
    }