index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*!
  2. * vinyl-bufferstream | MIT (c) Shinnosuke Watanabe
  3. * https://github.com/shinnn/vinyl-bufferstream
  4. */
  5. 'use strict';
  6. var BufferStreams = require('bufferstreams');
  7. module.exports = function VinylBufferStream(fn) {
  8. if (typeof fn !== 'function') {
  9. throw new TypeError(
  10. fn +
  11. ' is not a function. The argument to VinylBufferStream constructor must be a function.'
  12. );
  13. }
  14. return function vinylBufferStream(file, cb) {
  15. if (typeof cb !== 'function') {
  16. throw new TypeError(
  17. cb +
  18. ' is not a function. ' +
  19. 'The second argument to VinylBufferStream instance must be a function.'
  20. );
  21. }
  22. if (!file || typeof file.isNull !== 'function') {
  23. cb(new TypeError('Expecting a vinyl file object.'));
  24. return;
  25. }
  26. if (file.isNull()) {
  27. cb(null, null);
  28. return;
  29. }
  30. if (file.isStream()) {
  31. var stream = file.contents.pipe(new BufferStreams(function(none, buf, done) {
  32. fn(buf, function(err, result) {
  33. done(err, result);
  34. if (err) {
  35. cb(err);
  36. return;
  37. }
  38. cb(null, stream);
  39. });
  40. }));
  41. return;
  42. }
  43. fn(file.contents, cb);
  44. };
  45. };