This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
/
index.js
74 lines (65 loc) · 1.78 KB
/
index.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var stream = require('readable-stream')
var util = require('util')
var rabin = require('bindings')('rabin')
var BufferList = require('bl')
var debug = require('debug')('rabin')
module.exports = Rabin
function Rabin (opts) {
if (!(this instanceof Rabin)) return new Rabin(opts)
this.destroyed = false
this.rabinEnded = false
var avgBits = +opts.bits || 12
var min = +opts.min || 8 * 1024
var max = +opts.max || 32 * 1024
this.rabin = rabin.initialize(avgBits, min, max)
this.nextCb = null
this.buffers = new BufferList()
this.on('finish', function () {
if (this.buffers.length) this.push(this.buffers.slice(0, this.buffers.length))
this.push(null)
})
stream.Duplex.call(this, {objectMode: true})
}
util.inherits(Rabin, stream.Duplex)
Rabin.prototype.rabinEnd = function () {
if (this.rabinEnded) return
this.rabinEnded = true
rabin.end(this.rabin)
}
Rabin.prototype._writev = function (batch, cb) {
var self = this
var drained = true
if (this.destroyed) return cb()
var bufs = batch.map(function (b) {
self.buffers.append(b.chunk)
return b.chunk
})
var lengths = []
rabin.fingerprint(this.rabin, bufs, lengths)
debug('chunks', lengths)
for (var i = 0; i < lengths.length; i++) {
var size = lengths[i]
var buf = this.buffers.slice(0, size)
this.buffers.consume(size)
drained = this.push(buf)
}
if (drained) cb()
else this.nextCb = cb
}
Rabin.prototype._read = function (size) {
var nextCb = this.nextCb
if (nextCb) {
this.nextCb = null
nextCb()
}
}
Rabin.prototype._write = function (data, enc, cb) {
this._writev([{chunk: data}], cb)
}
Rabin.prototype.destroy = function (err) {
if (this.destroyed) return
this.destroyed = true
this.rabinEnd()
if (err) this.emit('error', err)
this.emit('close')
}