-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (179 loc) · 5.64 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var stream = require('stream');
/**
* A concurrent writable stream
*
* @param {function} work - a function to process a single chunk. Function
* signature should be `process(chunk, enc, callback)`. When finished processing,
* fire the provided `callback`.
* @param {function} [flush=undefined] - a function to run once all chunks have been
* processed, but before the stream emits a `finished` event. Function signature
* should be `flush(callback)`, fire the provided `callback` when complete.
* @param {object} [options=undefined] - options to pass to the writable stream.
* @param {number} [options.concurrency=1] - number of chunks to process concurrently.
* @returns {object} a writable stream. **Do not** override the `._write` function.
* @example
* var parallel = require('parallel-stream');
*
* var writable = parallel.writable(function(chunk, enc, callback) {
* processAsync(chunk)
* .on('done', callback);
* }, { objectMode: true, concurrency: 15 });
*
* readable.pipe(writable)
* .on('finish', function() {
* console.log('complete!');
* });
*/
module.exports.writable = function(work, flush, options) {
if (typeof flush === 'object') {
options = flush;
flush = null;
}
if (!flush) flush = function(callback) { callback(); };
options = options || {};
var concurrency = options.concurrency || 1;
var writable = new stream.Writable(options);
writable.setMaxListeners(Infinity);
function internal() {
// conditional to cover various versions of node.js
return writable._writableState.getBuffer ?
writable._writableState.getBuffer() : writable._writableState.buffer;
}
writable.pending = 0;
function fail(err) {
if (!writable._writableState.errorEmitted) {
writable._writableState.errorEmitted = true;
writable.emit('error', err);
}
}
writable._write = function(chunk, enc, callback) {
if (writable.pending >= concurrency) {
return writable.once('free', function() {
writable._write(chunk, enc, callback);
});
}
writable.pending++;
work.call(writable, chunk, enc, function(err) {
writable.pending--;
writable.emit('free');
if (err) fail(err);
});
callback();
if (internal().length === 0) writable.emit('empty');
};
var end = writable.end.bind(writable);
writable.end = function(chunk, enc, callback) {
if (internal().length) {
return writable.once('empty', function() {
writable.end(chunk, enc, callback);
});
}
if (writable.pending) {
return writable.once('free', function() {
writable.end(chunk, enc, callback);
});
}
if (typeof chunk === 'function') {
callback = chunk;
chunk = null;
}
if (typeof enc === 'function') {
callback = enc;
enc = null;
}
if (chunk) {
writable.write(chunk, enc);
return writable.once('free', function() {
writable.end(callback);
});
}
if (writable._writableState.errorEmitted) return;
if (callback) writable.on('finish', callback);
flush(function(err) {
if (err) return fail(err);
end();
});
};
return writable;
};
/**
* A concurrent transform stream
*
* @param {function} work - a function to process a single chunk. Function
* signature should be `process(chunk, enc, callback)`. When finished processing,
* fire the provided `callback`.
* @param {object} [options = undefined] - options to pass to the transform stream.
* @param {number} [options.concurrency = 1] - number of chunks to process concurrently.
* @returns {object} a transform stream. **Do not** override the `._transform` function.
* @example
* var parallel = require('parallel-stream');
*
* var transform = parallel.transform(function(chunk, enc, callback) {
* processAsync(chunk)
* .on('done', function(processedData) {
* callback(null, processedData);
* });
* }, { objectMode: true, concurrency: 15 });
*
* readable.pipe(transform)
* .on('data', function(data) {
* console.log('got processed data: %j', data);
* })
* .on('end', function() {
* console.log('complete!');
* });
*/
module.exports.transform = function(work, options) {
options = options || {};
var concurrency = options.concurrency || 1;
var transform = new stream.Transform(options);
transform.setMaxListeners(Infinity);
transform.pending = 0;
function fail(err) {
if (!transform._writableState.errorEmitted) {
transform._writableState.errorEmitted = true;
transform.emit('error', err);
}
}
transform._transform = function(chunk, enc, callback) {
if (transform.pending >= concurrency) {
return transform.once('free', function() {
transform._transform(chunk, enc, callback);
});
}
transform.pending++;
work.call(transform, chunk, enc, function(err, data) {
transform.pending--;
if (err) fail(err);
else if (data) transform.push(data);
transform.emit('free');
});
callback();
};
var end = transform.end.bind(transform);
transform.end = function(chunk, enc, callback) {
if (transform.pending) {
return transform.once('free', function() {
transform.end(chunk, enc, callback);
});
}
if (typeof chunk === 'function') {
callback = chunk;
chunk = null;
}
if (typeof enc === 'function') {
callback = enc;
enc = null;
}
if (chunk) {
transform.write(chunk, enc);
return transform.once('free', function() {
transform.end(callback);
});
}
if (callback) transform.on('finish', callback);
if (transform._writableState.errorEmitted) return;
end();
};
return transform;
};