forked from mafintosh/length-prefixed-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.js
More file actions
30 lines (22 loc) · 658 Bytes
/
encode.js
File metadata and controls
30 lines (22 loc) · 658 Bytes
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
const varint = require('varint')
const stream = require('readable-stream')
const inherits = require('inherits')
let pool = Buffer.allocUnsafe(10 * 1024)
let used = 0
const Encoder = function () {
if (!(this instanceof Encoder)) return new Encoder()
stream.Transform.call(this)
}
inherits(Encoder, stream.Transform)
Encoder.prototype._transform = function (data, enc, cb) {
varint.encode(data.length, pool, used)
used += varint.encode.bytes
this.push(pool.slice(used - varint.encode.bytes, used))
this.push(data)
if (pool.length - used < 100) {
pool = Buffer.allocUnsafe(10 * 1024)
used = 0
}
cb()
}
module.exports = Encoder