react / wstein / node_modules / browserify / node_modules / read-only-stream / node_modules / readable-wrap / index.js
80537 viewsvar Readable = require('readable-stream').Readable;12// wrap an old-style stream as the async data source.3// This is *not* part of the readable stream interface.4// It is an ugly unfortunate mess of history.5module.exports = wrap;6module.exports.obj = function (stream, opts) {7if (!opts) opts = {};8opts.objectMode = true;9return wrap(stream, opts);10};1112function wrap (stream, opts) {13var self = new Readable(opts)14var state = self._readableState;15var paused = false;1617stream.on('end', function() {18if (state.decoder && !state.ended) {19var chunk = state.decoder.end();20if (chunk && chunk.length)21self.push(chunk);22}2324self.push(null);25});2627stream.on('data', function(chunk) {28if (state.decoder)29chunk = state.decoder.write(chunk);3031// don't skip over falsy values in objectMode32//if (state.objectMode && util.isNullOrUndefined(chunk))33if (state.objectMode && (chunk === null || chunk === undefined))34return35else if (!state.objectMode && (!chunk || !chunk.length))36return;3738var ret = self.push(chunk);39if (!ret) {40paused = true;41stream.pause();42}43});4445// proxy all the other methods.46// important when wrapping filters and duplexes.47for (var i in stream) {48if (typeof stream[i] === 'function' &&49typeof self[i] === 'undefined') {50self[i] = function(method) { return function() {51return stream[method].apply(stream, arguments);52}}(i);53}54}5556// proxy certain important events.57var events = ['error', 'close', 'destroy', 'pause', 'resume'];58for (var i = 0; i < events.length; i++) (function (ev) {59stream.on(ev, function () {60var args = [ ev ].concat([].slice.call(arguments));61self.emit.apply(self, args);62})63})(events[i]);6465// when we try to consume some more bytes, simply unpause the66// underlying stream.67self._read = function(n) {68if (paused) {69paused = false;70stream.resume();71}72};7374return self;75};767778