-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
46 lines (39 loc) · 1018 Bytes
/
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
var through = require('through2');
// Consts
const PLUGIN_NAME = 'gulp-buffer';
function gulpBuffer() {
var stream = through.obj(function(file, enc, callback) {
// keep null files as-is
if (file.isNull()) {
this.push(file);
return callback();
}
// keep buffer files as-is
if (file.isBuffer()) {
this.push(file);
return callback();
}
// transform stream files into buffer
if (file.isStream()) {
var self = this;
var c_stream = file.contents;
var chunks = [];
var onreadable = function() {
var chunk;
while (null !== (chunk = c_stream.read())) {
chunks.push(chunk);
}
};
c_stream.on('readable', onreadable);
c_stream.once('end', function() {
c_stream.removeListener('readable', onreadable);
file.contents = Buffer.concat(chunks);
self.push(file);
callback();
});
return;
}
});
return stream;
};
module.exports = gulpBuffer;