function Logger(options, extra) { if(!(this instanceof Logger)) { return new Logger(options, extra) } Readable.call(this) this.name = options.name this._level = this.coerce(options.level) this.hostname = options.hostname || os.hostname() this.extra = extra || {} this.buffer = '' this.enabled = options.enabled === undefined ? true : options.enabled this.reading = false if(options.stream){ this.pipe(options.stream) } }
/** * Manages a stream of offers for a given storage contract publication * @constructor * @param {Contract} contract - Storage contract published to network * @param {Object} [options] * @param {Number} [options.maxOffers] - Maximum number of offers to process * @param {Array.<String>} [options.farmerBlacklist] - Reject offers from nodeID */ function OfferStream(contract, options) { if (!(this instanceof OfferStream)) { return new OfferStream(contract, options); } ReadableStream.call(this, { objectMode: true }); assert(contract instanceof Contract, 'Invalid contract supplied'); this.options = merge(Object.create(OfferStream.DEFAULTS), options); this._contract = contract; this._queue = []; this._offersQueued = 0; this._offersProccessed = 0; this._farmersDidOffer = []; this._isDestroyed = false; }
function Next(streams, opts) { if(!(this instanceof Next)) return new Next(streams, opts); Readable.call(this); opts = opts || {}; this._open = (typeof opts.open === 'undefined') ? true : opts.open; this._current = null; this._next = [].concat(streams); // propagate errors in all streams. var self = this; this._onInnerError = function(e) { self.emit('error', e); } this._next.forEach(this._propagateErrors.bind(this)); this._shift(); }
function Speech (opts) { if (!(this instanceof Speech)) { return new Speech(opts) } if (!('webkitSpeechRecognition' in window)) { this.emit('error', 'no speech api support') return } opts = defined(opts, {}) EventEmitter.call(this) Readable.call(this) this.recognition = new webkitSpeechRecognition() this.recognition.lang = defined(opts.lang, 'en-US') this.continuous = defined(opts.continuous, false) this._read = function (size) {} }
function Logger(options, extra) { if (!(this instanceof Logger)) { return new Logger(options, extra) } Readable.call(this) var passedInLevel = this.coerce(options.level) this.options = { _level: passedInLevel, enabled: options.enabled === undefined ? true : options.enabled } this._nestedLog = false this.name = options.name this.hostname = options.hostname || os.hostname() this.extra = extra || Object.create(null) this.buffer = '' this.reading = false if (options.stream) { this.pipe(options.stream) } }
function ReqStream (options){ var stream = this, haveReaders = false, servers = ((arguments.length < 2)? ["http"]: [].slice.call(arguments, 1)); if(!stream instanceof ReqStream){ return new ReqStream().forServers(servers); } stream = Readable.call(this, { objectMode: true, highWaterMark: options.highWaterMark || 1024 // <-- this is arbitrary, but I suspect we need a high one }); // all arguments should be or define either http(s) or http2 servers servers = servers.map( function(arg){ var httplike, protocol, host, port; if( arg && !'function' === typeof arg.on ) { if('string' === typeof arg) { arg = url.parse(arg, true, true); } // we need a protocol, hostname, and port to listen on protocol = arg.module || arg.protocol.replace(":", "") || options.module; httplike = require(protocol); host = arg.hostname || options.hostname || '0.0.0.0'; port = arg.port || options.port || 80; arg = httplike.createServer(arg.options || arg.query || options[protocol]); setImmediate(function(){ // TODO: plan to refactor this whole server definition API - it exists // only as a convenience and may be of questionable value arg.listen(port, host); }); } if( !(arg && 'function' === typeof arg.on) ) { throw new Error('ReqStream expects an http, https, or http2 server.'); } return arg; }); stream._read = function ReqStream_read(){ // listen for requests on our servers until we hit the highWaterMark, // then remove the listeners. servers.forEach( connectServerToStream ); }; function connectServerToStream(aServer) { // remove the listener before readding it, just to make sure that // we don't add it more than once. aServer.removeListener('request', sendRequstFromServerToStream); aServer.on('request', sendRequstFromServerToStream); } function sendRequstFromServerToStream(req, res) { var context = { request: req, response: res }; if(!stream.push(context)) { // we couldn't put the request context on the stream - we are over // the highWaterMark. Stop listening. This should only happen if we // are recieving more traffic than our server can handle. (Note that // the next call to _read will cause us to start listening again.) servers.forEach( disconnectServerFromStream ); // emit an event, in case the user wants to log that this has happened // or handle the response in their own way. stream.emit('unavailble', context); if(!res.finished) { res.writeHead(503, "Service Unavailble"); res.end(); } } } function disconnectServerFromStream(aServer){ aServer.removeListener('request', sendRequstFromServerToStream); } return stream; }
function FileStream() { if (!(this instanceof FileStream)) return new FileStream(); ReadableStream.call(this); }