Skip to content

Commit bbf46ab

Browse files
committed
Initial commit
0 parents  commit bbf46ab

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+

Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
node_modules: package.json
3+
@npm install
4+
5+
test:
6+
@./node_modules/.bin/mocha \
7+
--reporter spec \
8+
--timeout 100
9+
10+
.PHONY: test

Readme.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# gulp-buffer
2+
3+
Some gulp plugins do not support streaming file contents. gulp-buffer and gulp-stream come to the rescue.
4+
5+
There are 3 main reasons why a gulp plugin may not support stream :
6+
7+
* The stream support is on the roadmap but has not been added to the plugin
8+
* The underlying library used for the transformation does yet support a streaming API
9+
* The content transformation is not very well suited for streaming (think of content.reverse() for example where you need the end of the file first)
10+
11+
In all these cases, simply add gulp-buffer in front of such a plugin, and it will make sure that the plugin is presented only with buffers.
12+
13+
When you want to go back to full streaming capabilities, add gulp-stream and it will make sure that all your files are streamed again.
14+
15+
16+
[![build status](https://secure.travis-ci.org/jeromew/gulp-buffer.png)](http://travis-ci.org/jeromew/gulp-buffer)
17+
18+
## Installation
19+
20+
```bash
21+
$ npm install gulp-buffer
22+
```
23+
24+
# Licence
25+
26+
MIT
27+

index.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var through = require('through2');
2+
3+
// Consts
4+
const PLUGIN_NAME = 'gulp-buffer';
5+
6+
function gulpBuffer() {
7+
var stream = through.obj(function(file, enc, callback) {
8+
9+
// keep null files as-is
10+
if (file.isNull()) {
11+
this.push(file);
12+
return callback();
13+
}
14+
15+
// keep buffer files as-is
16+
if (file.isBuffer()) {
17+
this.push(file);
18+
return callback();
19+
}
20+
21+
// transform stream files into buffer
22+
if (file.isStream()) {
23+
var self = this;
24+
var c_stream = file.contents;
25+
var chunks = [];
26+
var onreadable = function() {
27+
var chunk;
28+
while (null !== (chunk = c_stream.read())) {
29+
chunks.push(chunk);
30+
}
31+
};
32+
c_stream.on('readable', onreadable);
33+
c_stream.once('end', function() {
34+
c_stream.removeListener('readable', onreadable);
35+
file.contents = Buffer.concat(chunks);
36+
self.push(file);
37+
callback();
38+
});
39+
return;
40+
}
41+
});
42+
43+
return stream;
44+
};
45+
46+
module.exports = gulpBuffer;

package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "gulp-buffer",
3+
"version": "0.0.1",
4+
"description": "Make sure all gulp file contents are available as buffer",
5+
"repository": "jeromew/gulp-buffer",
6+
"licence": "MIT",
7+
"author": "Jérôme Wagner",
8+
"keywords": [
9+
"gulp"
10+
],
11+
"dependencies": {
12+
"through2": "~0.4.0"
13+
},
14+
"devDependencies": {
15+
"mocha": "~1.17.0",
16+
"vinyl": "~0.2.3"
17+
},
18+
"scripts": {
19+
"test": "make test"
20+
}
21+
}

test/gulp-buffer.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var fs = require('fs');
2+
var File = require('vinyl');
3+
var buffer = require('..');
4+
var through = require('through2');
5+
var assert = require('assert');
6+
var Stream = require('stream');
7+
8+
describe('gulp-buffer', function() {
9+
10+
describe('with null contents', function() {
11+
it('should let null files pass through', function(done) {
12+
var b = buffer();
13+
var n = 0;
14+
var _transform = function(file, enc, callback) {
15+
assert.equal(file.contents, null);
16+
n++;
17+
callback();
18+
}
19+
var _flush = function(callback) {
20+
assert.equal(n, 1);
21+
done();
22+
callback();
23+
}
24+
var t = through.obj(_transform, _flush);
25+
b.pipe(t);
26+
b.end(new File({
27+
contents: null
28+
}));
29+
});
30+
});
31+
32+
describe('with buffer contents', function() {
33+
it('should let buffer files pass through', function(done) {
34+
var b = buffer();
35+
var n = 0;
36+
var c = new Buffer("data");
37+
var _transform = function(file, enc, callback) {
38+
assert.equal(file.contents, c);
39+
n++;
40+
callback();
41+
}
42+
var _flush = function(callback) {
43+
assert.equal(n, 1);
44+
done();
45+
callback();
46+
}
47+
var t = through.obj(_transform, _flush);
48+
b.pipe(t);
49+
b.end(new File({
50+
contents: c
51+
}));
52+
});
53+
});
54+
55+
describe('with stream contents', function() {
56+
it('should transform stream to buffer', function(done) {
57+
var b = buffer();
58+
var n = 0;
59+
var c = Readable();
60+
var _transform = function(file, enc, callback) {
61+
assert.equal(file.isBuffer(), true);
62+
assert.equal(file.contents.toString(), "data");
63+
n++;
64+
callback();
65+
}
66+
var _flush = function(callback) {
67+
assert.equal(n, 1);
68+
done();
69+
callback();
70+
}
71+
var t = through.obj(_transform, _flush);
72+
b.pipe(t);
73+
b.end(new File({
74+
contents: c
75+
}));
76+
});
77+
});
78+
79+
80+
});
81+
82+
function Readable(cb){
83+
var readable = new Stream.Readable();
84+
readable._read = function() {
85+
this.push(new Buffer('data'));
86+
this.push(null); // no more data
87+
};
88+
if (cb) readable.on('end', cb);
89+
return readable;
90+
}
91+
92+

0 commit comments

Comments
 (0)