forked from ForbesLindesay-Unmaintained/reports
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
77 lines (71 loc) · 1.86 KB
/
Copy pathutils.js
File metadata and controls
77 lines (71 loc) · 1.86 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
var Stream = require('stream');
exports.chain = chain;
function chain(write, read) {
var stream = new Stream();
stream.writable = true;
stream.readable = true;
var oldOn = stream.on.bind(stream);
stream.on = function (name, fn) {
oldOn(name, fn);
switch (name) {
case 'data':
case 'end':
return read.on(name, fn);
case 'drain':
case 'pipe':
return write.on(name, fn);
case 'close':
var i = 2;
write.on('close', function () {
if (0 === --i) {
fn();
}
});
read.on('close', function () {
if (0 === --i) {
fn();
}
});
return;
case 'error':
write.on(name, fn);
read.on(name, fn);
return;
}
};
stream.write = write.write.bind(write);
stream.end = write.end.bind(write);
stream.setEncoding = read.setEncoding.bind(read);
stream.pause = read.pause.bind(read);
stream.resume = read.resume.bind(read);
stream.pipe = read.pipe.bind(read);
stream.destroy = function () {
read.destroy.apply(read, arguments);
write.destroy.apply(write, arguments);
};
stream.destroySoon = function () {
write.destroySoon.apply(write, arguments);
read.destroy.apply(read, arguments);
};
return stream;
}
exports.withContentType = withContentType;
function withContentType(stream, contentType) {
var pipe = stream.pipe;
stream.pipe = function (dest) {
if (dest.headers) {
dest.headers['content-type'] = contentType;
} else if (dest.setHeader) {
dest.setHeader('content-type', contentType);
}
pipe.apply(this, arguments);
};
return stream;
}
exports.transform = transform;
function transform(transform, flush) {
var stream = new Stream.Transform({objectMode: true});
stream._transform = transform;
stream._flush = flush;
return stream;
}