# All files / simple-buffer-queue index.js

100% Statements 8/8
0% Branches 0/1
100% Functions 3/3
100% Lines 8/8

    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
    322x

    2x

    2x

    2x
    2x
    2x

    2x
    2x

     const Buffer = require('safe-buffer').Buffer

    module.exports = class BufferPipe {
      /**
       * Creates a new instance of a pipe
       * @param {Buffer} buf - an optional buffer to start with
       */
      constructor (buf = Buffer.from([])) {
        this.buffer = buf
      }

      /**
       * read `num` number of bytes from the pipe
       * @param {Number} num
       * @return {Buffer}
       */
      read (num) {
        const data = this.buffer.subarray(0, num)
        this.buffer = this.buffer.subarray(num)
        return data
      }

      /**
       * Wites a buffer to the pipe
       * @param {Buffer} buf
       */
      write (buf) {
        buf = Buffer.from(buf)
        this.buffer = Buffer.concat([this.buffer, buf])
      }
    }