-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (128 loc) · 4.13 KB
/
index.js
File metadata and controls
157 lines (128 loc) · 4.13 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var EventEmitter = require('events').EventEmitter,
util = require('util'),
async = require('async');
function extendObj (a, b) {
for (var x in b) a[x] = b[x]
return a;
}
function ChunkIt(stream, options, cb) {
if (typeof options == 'function') {
cb = options;
options = {};
}
var self = this;
// Lets hook our error event in the start so we can easily emit errors.
this.on('error', function (e) {
if (self.failed || self.finished) return;
self.reading = false;
self.finished = false;
self.failed = true;
// Remove our event handlers if any.
if (self.stream) {
self.streamErrorHandler && self.stream.removeListener('error', self.streamErrorHandler);
self.streamDataHandler && self.stream.removeListener('data', self.streamDataHandler);
self.streamEndHandler && self.stream.removeListener('end', self.streamEndHandler);
}
self.cb && self.cb(e);
// prevent any further callbacks
self.cb = null;
});
var defaultOptions = {
bytes: 1024, // byte size of each chunk.
start: 0, // start chunking after this byte.
end: -1 // stop chunking after this byte
}
this.options = extendObj(defaultOptions, options);
this.stream = stream;
// States
this.initiated = false;
this.failed = false;
this.reading = false;
this.finished = false;
// Stats
this.stats = {bytes: 0, chunks: 0};
// Chunking and buffering
this.buffer = new Buffer(0);
this.cb = cb;
// Pause the stream until we hook our events.
if (stream) stream.pause();
else this.emit('error', new Error('You must provide a readable stream.'));
// if callback provided then begin, else wait for call to begin.
cb && this.begin();
return this;
}
util.inherits(ChunkIt, EventEmitter);
ChunkIt.prototype.begin = function() {
if (this.initiated || this.finished) return;
var self = this;
this.streamErrorHandler = function (err) {
self.emit('error', err);
}
this.streamDataHandler = function (chunk) {
self.reading = true;
if (typeof chunk === 'string') chunk = new Buffer(chunk, 'utf-8');
self.stats.bytes += chunk.length;
self.buffer = Buffer.concat([self.buffer, chunk]);
self.stream.pause();
self.flushChunk(function (e) {
if (e) return self.emit('error', e);
self.stream.resume();
});
}
this.streamEndHandler = function () {
self.reading = false;
self.flushChunk(true, function (e) {
if (e) return self.emit('error', e);
self.finished = true;
self.cb && self.cb(null, self.stats);
self.emit('end', self.stats);
});
}
this.initiated = true;
this.stream.on('error', this.streamErrorHandler);
this.stream.on('data', this.streamDataHandler);
this.stream.on('end', this.streamEndHandler);
this.stream.resume();
}
ChunkIt.prototype.flushChunk = function(last, cb) {
if (!this.initiated || this.finished) return cb();
if (typeof last == 'function') {
cb = last;
last = false;
}
var self = this;
var newChunks = [];
while (this.buffer.length >= this.options.bytes) {
var newChunk = {};
if (this.buffer.length > this.options.bytes) {
newChunk.data = this.buffer.slice(0, this.options.bytes);
this.buffer = new Buffer(this.buffer.slice(this.options.bytes));
newChunk.last = false;
} else {
newChunk.data = this.buffer.slice(0, this.options.bytes);
this.buffer = this.buffer.slice(this.options.bytes);
newChunk.last = last;
}
newChunk.index = this.stats.chunks++;
newChunks.push(newChunk);
}
// Last chunk
if (!this.reading && last && this.buffer.length) {
var newChunk = {};
newChunk.data = this.buffer.slice(0, this.options.bytes);
this.buffer = this.buffer.slice(this.options.bytes);
newChunk.index = this.stats.chunks++;
newChunk.last = true;
newChunks.push(newChunk);
}
async.eachSeries(newChunks, function (chunk, next) {
if (!chunk) return next();
self.cb && self.cb(null, chunk);
self.emit('chunk', chunk);
next();
}, function (e) {
if (e) return cb(e);
cb();
});
}
module.exports = ChunkIt;