summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/tar/node_modules/minipass/bench/lib/numbers.js
blob: bd1593299a636d560093f989ac862508239abed1 (plain)
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
'use strict'
const stream = require('stream')

const numbers = new Array(1000).join(',').split(',').map((v, k) => k)
let acc = ''
const strings = numbers.map(n => acc += n)
const bufs = strings.map(s => new Buffer(s))
const objs = strings.map(s => ({ str: s }))

module.exports = class Numbers {
  constructor (opt) {
    this.objectMode = opt.objectMode
    this.encoding = opt.encoding
    this.ii = 0
    this.done = false
  }
  pipe (dest) {
    this.dest = dest
    this.go()
    return dest
  }

  go () {
    let flowing = true
    while (flowing) {
      if (this.ii >= 1000) {
        this.dest.end()
        this.done = true
        flowing = false
      } else {
        flowing = this.dest.write(
          (this.objectMode ? objs
          : this.encoding ? strings
          : bufs)[this.ii++])
      }
    }

    if (!this.done)
      this.dest.once('drain', _ => this.go())
  }
}