var extend = require('util-extend');
var through = require('through2');
var gutil = require('gulp-util');

module.exports = function(data) {
  'use strict';

  // if necessary check for required param(s), e.g. options hash, etc.
  if (!data) {
    throw new gutil.PluginError('gulp-data', 'No data supplied');
  }

  // see 'Writing a plugin'
  // https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/README.md

  function gulpData(file, enc, callback) {
    /*jshint validthis:true*/
    var self = this, called = false;

    function handle(err, result){
      // extra guard in case
      if (called) {
        return;
      }
      called = true;
      if (err) {
        self.emit('error', new gutil.PluginError('gulp-data', { message: err }));
        return callback();
      }
      file.data = extend(file.data || {}, result);
      self.push(file);
      callback();
    }

    function local(data) {
      if (data && typeof data.then === 'function') {
        data.then(function(data){
          return handle(undefined, data);
        }, function(err) { return handle(err); });
      }
      else {
        handle(undefined, data);
      }
    }

    // Do nothing if no contents
    if (file.isNull()) {
      this.push(file);
      return callback();
    }

    if (file.isStream()) {

      // http://nodejs.org/api/stream.html
      // http://nodejs.org/api/child_process.html
      // https://github.com/dominictarr/event-stream

      // accepting streams is optional
      this.emit('error', new gutil.PluginError('gulp-data', 'Stream content is not supported'));
      return callback();
    }

    // check if file.contents is a `Buffer`
    if (file.isBuffer()) {
      var res = null;
      if (typeof data === 'function') {
        try {
          res = data(file, handle);
          if (data.length <= 1) {
            return local(res);
          }

        } catch(e) {
          return handle(e);
        }
      }
      else {
        local(data);
      }
    }
  }

  return through.obj(gulpData);
};

@@ -1169,537 +1169,8 @@
  %7D%0A%0A
-    // Do nothing if no contents%0A    if (file.isNull()) %7B%0A      this.push(file);%0A      return callback();%0A    %7D%0A%0A    if (file.isStream()) %7B%0A%0A      // http://nodejs.org/api/stream.html%0A      // http://nodejs.org/api/child_process.html%0A      // https://github.com/dominictarr/event-stream%0A%0A      // accepting streams is optional%0A      this.emit('error', new gutil.PluginError('gulp-data', 'Stream content is not supported'));%0A      return callback();%0A    %7D%0A%0A    // check if file.contents is a %60Buffer%60%0A    if (file.isBuffer()) %7B%0A  
     
@@ -1185,18 +1185,16 @@
 = null;%0A
-  
     if (
@@ -1219,26 +1219,24 @@
 unction') %7B%0A
-  
       try %7B%0A
@@ -1243,18 +1243,16 @@
         
-  
 res = da
@@ -1265,26 +1265,24 @@
 e, handle);%0A
-  
         if (
@@ -1311,25 +1311,16 @@
         
-  return 
 local(re
@@ -1331,23 +1331,19 @@
         
-  
 %7D%0A%0A
-  
       %7D 
@@ -1357,33 +1357,24 @@
 ) %7B%0A        
-  return 
 handle(e);%0A 
@@ -1378,26 +1378,24 @@
 ;%0A      
-  
 %7D%0A      
 %7D%0A      
@@ -1386,35 +1386,31 @@
 %7D%0A      
-%7D%0A      else %7B%0A    
+return;%0A    %7D%0A%0A
     loca
@@ -1418,30 +1418,16 @@
 (data);%0A
-      %7D%0A    %7D%0A
   %7D%0A%0A  r

support null files and stream contents