UNPKG

zombie-globbies

Version:

A very quick fix for [**Zombie**](https://github.com/assaf/zombie) to permit to crawl correctly webpages with attributes on the html tag (eg: html lang="en").

56 lines (46 loc) 1.5 kB
var HTTP, PortMap; HTTP = require("http"); PortMap = (function() { function PortMap() { this._ports = {}; this._http = HTTP.request; HTTP.request = this._request.bind(this); } PortMap.prototype.map = function(hostname, port) { return this._ports[hostname] = port; }; PortMap.prototype.unmap = function(hostname) { return delete this._ports.hostname; }; PortMap.prototype._request = function(options, callback) { var hostname, mapped, port; hostname = options.hostname || (options.host && options.host.split(":")[0]) || "localhost"; port = options.port || (options.host && options.host.split(":")[1]) || 80; if (port === 80) { mapped = this._find(hostname); if (mapped) { options = Object.create(options); options.hostname = hostname; options.port = mapped; } } return this._http(options, callback); }; PortMap.prototype._find = function(domain) { var domains, i, parts, _i, _ref; parts = domain.split('.'); domains = [domain, "*." + domain]; for (i = _i = 1, _ref = parts.length; 1 <= _ref ? _i < _ref : _i > _ref; i = 1 <= _ref ? ++_i : --_i) { domains.push("*." + parts.slice(i, +parts.length + 1 || 9e9).join('.')); } return domains.map((function(_this) { return function(pattern) { return _this._ports[pattern]; }; })(this)).filter(function(port) { return port; })[0]; }; return PortMap; })(); module.exports = PortMap;