aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gulp-stream/index.js
blob: 5c3f1ece01319ab7e38a95223ea879176838d471 (plain)
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
var through = require('through2');
var Stream = require('stream');

// Consts
const PLUGIN_NAME = 'gulp-stream';

function gulpStream() {
  var stream = through.obj(function(file, enc, callback) {

    // keep null files as-is
    if (file.isNull()) {
      this.push(file);
      return callback();
    }

    // keep stream files as-is
    if (file.isStream()) {
      this.push(file);
      return callback();
    }

    // transform buffer files into buffers
    if (file.isBuffer()) {
      var cstream = Readable(file.contents);
      cstream.on('error', this.emit.bind(this, 'error'));
      file.contents = cstream;
      this.push(file);
      return callback();
    }

  });

  return stream;
};

function Readable(buffer){
  var readable = new Stream.Readable();
  var idx = 0;
  readable._read = function(size) {
    var chunk = buffer.slice(0, size);
    buffer = buffer.slice(size);
    var finished = (chunk.length == 0 && buffer.length == 0);
    this.push(finished ? null : chunk);
  };
  return readable;
}



module.exports = gulpStream;