forked from nodeca/probe-image-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.js
80 lines (58 loc) · 1.76 KB
/
stream.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
'use strict';
var once = require('./lib/common').once;
var error = require('./lib/common').error;
var parsers = require('./lib/parsers_stream');
function unrecognizedFormat() {
return error('unrecognized file format', 'ECONTENT');
}
module.exports = function probeStream(stream, _callback) {
var callback = once(function () {
// We should postpone callback to allow all piped parsers accept .write().
// In other case, if stream is closed from callback that can cause
// exceptions like "write after end".
var args = Array.prototype.slice.call(arguments);
process.nextTick(function () {
_callback.apply(null, args);
});
});
var pStreams = [];
// prevent "possible EventEmitter memory leak" warnings
stream.setMaxListeners(0);
function cleanup(strm) {
var i;
if (strm) {
stream.unpipe(strm);
strm.end();
i = pStreams.indexOf(strm);
if (i >= 0) pStreams.splice(i, 1);
return;
}
for (i = 0; i < pStreams.length; i++) {
stream.unpipe(pStreams[i]);
pStreams[i].end();
}
pStreams.length = 0;
}
stream.on('error', function (err) { cleanup(); callback(err); });
Object.keys(parsers).forEach(function (type) {
var pStream = parsers[type]();
pStream.on('data', function (result) {
callback(null, result);
cleanup();
});
pStream.on('error', function () {
// silently ignore errors because user does not need to know
// that something wrong is happening here
});
pStream.on('end', function () {
cleanup(pStream);
if (pStreams.length === 0) {
cleanup();
callback(unrecognizedFormat());
}
});
stream.pipe(pStream);
pStreams.push(pStream);
});
};
module.exports.parsers = parsers;