UNPKG

te_nsqjs

Version:
125 lines (105 loc) 3.12 kB
// Generated by CoffeeScript 1.12.3 var Debug, _, async, dedupeOnHostPort, dedupedRequests, lookup, lookupdRequest, request, url; _ = require('underscore'); async = require('async'); request = require('request'); url = require('url'); Debug = require('debug'); /* lookupdRequest returns the list of producers from a lookupd given a URL to query. The callback will not return an error since it's assumed that there might be transient issues with lookupds. */ lookupdRequest = function(url, callback) { var options; options = { url: url, method: 'GET', json: true, timeout: 2000 }; return request(options, function(err, response, data) { var error, producers, ref, status_code; if (err) { callback(null, []); return; } try { status_code = data.status_code, (ref = data.data, producers = ref.producers); } catch (error1) { error = error1; callback(null, []); return; } if (status_code !== 200) { callback(null, []); return; } return callback(null, producers); }); }; /* Takes a list of responses from lookupds and dedupes the nsqd hosts based on host / port pair. Arguments: results: list of lists of nsqd node objects. */ dedupeOnHostPort = function(results) { return _.chain(results).flatten().indexBy(function(item) { return item.hostname + ":" + item.tcp_port; }).values().value(); }; dedupedRequests = function(lookupdEndpoints, urlFn, callback) { var endpoint, urls; this.debug = Debug("nsqjs:reader"); if (_.isString(lookupdEndpoints)) { lookupdEndpoints = [lookupdEndpoints]; } urls = (function() { var i, len, results1; results1 = []; for (i = 0, len = lookupdEndpoints.length; i < len; i++) { endpoint = lookupdEndpoints[i]; results1.push(urlFn(endpoint)); } return results1; })(); return async.map(urls, lookupdRequest, function(err, results) { if (err) { return callback(err, null); } else { this.debug("results " + results); return callback(null, dedupeOnHostPort(results)); } }); }; /* Queries lookupds for known nsqd nodes given a topic and returns a deduped list. Arguments: lookupdEndpoints: a string or a list of strings of lookupd HTTP endpoints. eg. ['127.0.0.1:4161'] topic: a string of the topic name. callback: with signature `(err, nodes) ->`. `nodes` is a list of objects return by lookupds and deduped. */ lookup = function(lookupdEndpoints, topic, callback) { var endpointURL; endpointURL = function(endpoint) { var parsedUrl; if (endpoint.indexOf('://') === -1) { endpoint = "http://" + endpoint; } parsedUrl = url.parse(endpoint, true); if ((!parsedUrl.pathname) || (parsedUrl.pathname === '/')) { parsedUrl.pathname = "/lookup"; } parsedUrl.query.topic = topic; delete parsedUrl.search; return url.format(parsedUrl); }; this.debug = Debug("nsqjs:reader"); this.debug("endpointURL " + endpointURL); return dedupedRequests(lookupdEndpoints, endpointURL, callback); }; module.exports = lookup;