pax_global_header00006660000000000000000000000064124666236640014531gustar00rootroot0000000000000052 comment=baaf30c5e6291c5838461902bab9098b1dcb4a30 vinyl-bufferstream-1.0.1/000077500000000000000000000000001246662366400153545ustar00rootroot00000000000000vinyl-bufferstream-1.0.1/.editorconfig000077500000000000000000000002741246662366400200370ustar00rootroot00000000000000root = true [*] charset = utf-8 end_of_line = lf indent_style = space indent_size = 2 insert_final_newline = true trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false vinyl-bufferstream-1.0.1/.eslintrc000077500000000000000000000001321246662366400171770ustar00rootroot00000000000000env: node: true rules: eqeqeq: 2 block-scoped-var: 2 quotes: - 2 - single vinyl-bufferstream-1.0.1/.gitattributes000077500000000000000000000000141246662366400202450ustar00rootroot00000000000000* text=auto vinyl-bufferstream-1.0.1/.gitignore000077500000000000000000000000261246662366400173450ustar00rootroot00000000000000node_modules coverage vinyl-bufferstream-1.0.1/.travis.yml000077500000000000000000000002241246662366400174660ustar00rootroot00000000000000sudo: false git: depth: 2 language: node_js node_js: - iojs - '0.12' after_script: - npm run-script coveralls notifications: email: false vinyl-bufferstream-1.0.1/LICENSE000077500000000000000000000021051246662366400163620ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 - 2015 Shinnosuke Watanabe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. vinyl-bufferstream-1.0.1/README.md000077500000000000000000000104461246662366400166430ustar00rootroot00000000000000# vinyl-bufferstream [![NPM version](https://img.shields.io/npm/v/vinyl-bufferstream.svg?style=flat)](https://www.npmjs.com/package/vinyl-bufferstream) [![Build Status](https://img.shields.io/travis/shinnn/vinyl-bufferstream.svg?style=flat)](https://travis-ci.org/shinnn/vinyl-bufferstream) [![Build status](https://ci.appveyor.com/api/projects/status/gqc8t4mju49p6fkn?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/vinyl-bufferstream) [![Coverage Status](https://img.shields.io/coveralls/shinnn/vinyl-bufferstream.svg?style=flat)](https://coveralls.io/r/shinnn/vinyl-bufferstream) [![Dependency Status](https://img.shields.io/david/shinnn/vinyl-bufferstream.svg?style=flat&label=deps)](https://david-dm.org/shinnn/vinyl-bufferstream) [![devDependency Status](https://img.shields.io/david/dev/shinnn/vinyl-bufferstream.svg?style=flat&label=devDeps)](https://david-dm.org/shinnn/vinyl-bufferstream#info=devDependencies) Deal with [vinyl file](https://github.com/wearefractal/vinyl) contents, regardless of whether it is Buffer/Stream ```javascript var through = require('through2'); var VinylBufferStream = require('vinyl-bufferstream'); function yourGulpPlugin() { var vinylBufferStream = new VinylBufferStream(function(buf, done) { syncOrAsyncFn(buf, done); }); return through.obj(function(file, enc, cb) { vinylBufferStream(file, function(err, contents) { if (err) { self.emit('error', err); } else { file.contents = contents; self.push(file); } cb(); }); }); } ``` ## Installation [Use npm.](https://docs.npmjs.com/cli/install) ```sh npm install vinyl-bufferstream ``` ## API ```javascript var VinylBufferStream = require('vinyl-bufferstream'); ``` ### vinylBufferStream = new VinylBufferStream(*transformFunction*) (`new` operator is optional.) *transformFunction*: `Function` Return: `Function` The argument must be a function taking a [`Buffer`][buffer] and a callback function as its first and second argument, which calls the callback function with passing [Node](http://nodejs.org/)-style callback arguments (`error, result`). #### vinylBufferStream(*file*, *callback*) *file*: `Object` ([vinyl file](https://github.com/wearefractal/vinyl#file) object) *callback*: `Function` When the [`file.contents`](https://github.com/wearefractal/vinyl#optionscontents) is a [`Buffer`][buffer], it will call the *transformFunction* with passing file.contents to the first argument. When the `file.contents` is a [`Stream`][buffer], it will call the *transformFunction* with passing the buffered stream of file.contents to the first argument. When the `file.contents` is a [`Stream`][stream], it won't call the *transformFunction*. ##### callback(err, contents) *error*: `Error` or `null` *contents*: [`Buffer`][buffer] or [`Stream`][stream] When the `file.contents` is a [`Buffer`][buffer], *contents* will be a result that *transformFunction* produces. When the `file.contents` is a [`Stream`][stream], *contents* will be a stream that emits a data *transformFunction* produces. When the `file.contents` is `null`, *contents* will be `null`. ```javascript var gulp = require('gulp'); var SVGO = require('svgo'); var through = require('through2'); var VinylBufferStream = require('vinyl-bufferstream'); function svgminPlugin(options) { var svgo = new SVGO(options); var vinylBufferStream = new VinylBufferStream(function(buf, done) { svgo.optimize(String(buf), function(result) { if (result.error) { done(result.error); return; } done(null, result.data); }); }); return through.obj(function(file, enc, cb) { vinylBufferStream(file, function(err, contents) { if (err) { self.emit('error', err); } else { file.contents = contents; self.push(file); } cb(); }); }); } gulp.task('buffer', function() { return gulp.src('*.svg') .pipe(svgminPlugin()) .pipe(gulp.dest('dest')); }); gulp.task('stream', function() { return gulp.src('*.svg', {buffer: false}) .pipe(svgminPlugin()) .pipe(gulp.dest('dest')); }); ``` ## License Copyright (c) 2014 - 2015 [Shinnosuke Watanabe](https://github.com/shinnn) Licensed under [the MIT License](./LICENSE). [buffer]: https://iojs.org/api/buffer.html#buffer_class_buffer [stream]: https://iojs.org/api/stream.html vinyl-bufferstream-1.0.1/appveyor.yml000077500000000000000000000006401246662366400177470ustar00rootroot00000000000000init: - git config --global core.autocrlf input shallow_clone: true version: '{build}' environment: matrix: - nodejs_version: '0.10' - nodejs_version: '0.11' install: - ps: Install-Product node $env:nodejs_version - npm install --production - npm install simple-bufferstream tape vinyl build: off test_script: - ps: 'node test.js #PowerShell' - cmd: node test.js cache: - node_modules vinyl-bufferstream-1.0.1/index.js000077500000000000000000000022731246662366400170300ustar00rootroot00000000000000/*! * vinyl-bufferstream | MIT (c) Shinnosuke Watanabe * https://github.com/shinnn/vinyl-bufferstream */ 'use strict'; var BufferStreams = require('bufferstreams'); module.exports = function VinylBufferStream(fn) { if (typeof fn !== 'function') { throw new TypeError( fn + ' is not a function. The argument to VinylBufferStream constructor must be a function.' ); } return function vinylBufferStream(file, cb) { if (typeof cb !== 'function') { throw new TypeError( cb + ' is not a function. ' + 'The second argument to VinylBufferStream instance must be a function.' ); } if (!file || typeof file.isNull !== 'function') { cb(new TypeError('Expecting a vinyl file object.')); return; } if (file.isNull()) { cb(null, null); return; } if (file.isStream()) { var stream = file.contents.pipe(new BufferStreams(function(none, buf, done) { fn(buf, function(err, result) { done(err, result); if (err) { cb(err); return; } cb(null, stream); }); })); return; } fn(file.contents, cb); }; }; vinyl-bufferstream-1.0.1/package.json000066400000000000000000000024031246662366400176410ustar00rootroot00000000000000{ "name": "vinyl-bufferstream", "version": "1.0.1", "description": "Deal with vinyl file contents, regardless of whether it is Buffer/Stream", "repository": "shinnn/vinyl-bufferstream", "author": { "name": "Shinnosuke Watanabe", "url": "https://github.com/shinnn" }, "scripts": { "pretest": "jscs *.js && eslint *.js", "test": "node test.js | tap-spec", "coverage": "istanbul cover test.js", "coveralls": "${npm_package_scripts_coverage} && istanbul-coveralls" }, "licenses": [ { "type": "MIT", "url": "https://github.com/shinnn/vinyl-bufferstream/blob/master/LICENSE" } ], "files": [ "index.js" ], "keywords": [ "gulpfriendly", "gulp", "vinyl", "buffer", "stream", "compatibility", "interchangeability", "internal", "helper" ], "dependencies": { "bufferstreams": "1.0.1" }, "devDependencies": { "eslint": "^0.14.1", "istanbul": "^0.3.5", "istanbul-coveralls": "^1.0.1", "jscs": "^1.11.2", "simple-bufferstream": "0.0.4", "tap-spec": "^2.2.1", "tape": "^3.5.0", "vinyl": "^0.4.6" }, "jscsConfig": { "preset": "google", "maximumLineLength": 98, "requireBlocksOnNewline": true, "validateLineBreaks": "LF" } } vinyl-bufferstream-1.0.1/test.js000077500000000000000000000055201246662366400166760ustar00rootroot00000000000000/* eslint new-cap:0 no-new:0 */ 'use strict'; var bufferToStream = require('simple-bufferstream'); var File = require('vinyl'); var test = require('tape'); var VinylBufferStream = require('./'); var tmpError = new Error('Error.'); test('VinylBufferStream()', function(t) { t.plan(5); t.equal(VinylBufferStream.name, 'VinylBufferStream', 'should have a function name.'); t.doesNotThrow(function() { new VinylBufferStream(t.fail); }, 'should create an instance as a function.'); t.doesNotThrow(function() { VinylBufferStream(t.fail); }, 'should create an instance without `new` operator.'); t.throws( function() { new VinylBufferStream('foo'); }, /TypeError.*foo is not a function/, 'should throw a type error when the first argument is not a function.' ); t.throws( function() { new VinylBufferStream(); }, /TypeError.*must be a function\./, 'should throw a type error when it takes no arguments.' ); }); test('instance of VinylBufferStream()', function(t) { t.plan(9); var vinylBufferStream = new VinylBufferStream(function(buf, cb) { if (String(buf) === 'error') { cb(tmpError); return; } cb(null, Buffer.concat([new Buffer('prefix-'), buf])); }); t.equal(vinylBufferStream.name, 'vinylBufferStream', 'should have a function name.'); vinylBufferStream(new File(), function(err, contents) { t.deepEqual( [err, contents], [null, null], 'should pass null to the callback when the file.contents is null.' ); }); vinylBufferStream(new File({contents: new Buffer('a')}), function(err, contents) { t.strictEqual(err, null, 'should accept vinyl file in buffer mode.'); t.equal( String(contents), 'prefix-a', 'should pass a buffer of contents to the function.' ); }); vinylBufferStream(new File({contents: bufferToStream('a')}), function(err, contents) { t.strictEqual(err, null, 'should accept vinyl file in stream mode.'); contents.on('data', function(data) { t.equal( String(data), 'prefix-a', 'should pass a stream to the callback when the contents is a stream.' ); }); }); vinylBufferStream(new File({contents: bufferToStream('error')}), function(err, contents) { t.deepEqual( [err, contents], [tmpError, undefined], 'should not pass contents to the callback when the the callback takes an error.' ); }); vinylBufferStream({}, function(err) { t.equal( err.message, 'Expecting a vinyl file object.', 'should pass an error to the callback when the first argument is not a vinyl file.' ); }); t.throws( vinylBufferStream.bind(null, new File(), 'foo'), /TypeError.*foo is not a function.*must be a function\./, 'should throw a type error when the second argument is not a function.' ); });