git ssb

0+

wanderer🌟 / buffer-pipe



Tree: 54caadc458ca1ac4c3d5d42c945f09165d3f15cd

Files: 54caadc458ca1ac4c3d5d42c945f09165d3f15cd / index.js

809 bytesRaw
1const Buffer = require('safe-buffer').Buffer
2
3module.exports = class BufferPipe {
4 /**
5 * Creates a new instance of a pipe
6 * @param {Buffer} buf - an optional buffer to start with
7 */
8 constructor (buf = Buffer.from([])) {
9 this.buffer = buf
10 }
11
12 /**
13 * read `num` number of bytes from the pipe
14 * @param {Number} num
15 * @return {Buffer}
16 */
17 read (num) {
18 const data = this.buffer.subarray(0, num)
19 this.buffer = this.buffer.subarray(num)
20 return data
21 }
22
23 /**
24 * Wites a buffer to the pipe
25 * @param {Buffer} buf
26 */
27 write (buf) {
28 buf = Buffer.from(buf)
29 this.buffer = Buffer.concat([this.buffer, buf])
30 }
31
32 /**
33 * Whether or not there is more data to read from the buffer
34 * returns {Boolean}
35 */
36 get end () {
37 return !this.buffer.length
38 }
39}
40

Built with git-ssb-web