Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

minor bug fixes, extra callback and return args, updated readme and n…

…pm version #
  • Loading branch information...
commit 8382733d53c2ac337dc8d19f82645b3fc4ee2719 1 parent 06b687d
@mjpearson authored
Showing with 101 additions and 28 deletions.
  1. +41 −12 README.md
  2. +57 −13 index.js
  3. +3 −3 package.json
View
53 README.md
@@ -10,17 +10,28 @@ This module provides TLD domain extraction and resolution services.
var tldtools = require('tldtools');
+## TLD List Caching Notes and Operation
+
+ The first time tldtools is loaded it will attempt to call out to `http://mxr.mozilla.org/mozilla/source/netwerk/dns/src/effective_tld_names.dat?raw=1`
+to retrieve the latest TLD list. This file is parsed, normalised and stored in `/.tlds`. To override this outbound call and look locally, place your
+own overriding file in `/effective_tld_names.dat`
+
+To force a cache refresh of TLD data in your own running application, you must provide a hook which calls `tldtools.tldCacheRefresh`
+
### tldtools.extract(fqdn)
Extracts tld, domain and subdomain parts from the provided fqdn (supports FQDNs names and URIs).
-Based on John Kurkowski's tldextract python library.
+Based on John Kurkowski's tldextract python library. https://github.com/john-kurkowski/tldextract
Returns an object keyed by
* tld - top level domain (com, gov.uk etc)
* domain - first subdomain of tld
* subdomain - prefixing A records for domain/tld
+* url_tokens - node-url meta structure (convenience)
+* inspect.useful() - closure reporting whether domain and tld parsed correctly
+* inspect.getDomain() - string concatenation of domain + tld
eg:
@@ -29,17 +40,28 @@ eg:
Returns...
- TLD Cache is UP
{ subdomain: 'wagga.wagga',
domain: 'funkjazz',
- tld: 'gov.au' }
+ tld: 'gov.au',
+ url_tokens:
+ { protocol: 'http:',
+ slashes: true,
+ auth: 'bob:funk',
+ host: 'bob:funk@wagga.wagga.funkjazz.gov.au:1234',
+ port: '1234',
+ hostname: 'wagga.wagga.funkjazz.gov.au',
+ href: 'http://bob:funk@wagga.wagga.funkjazz.gov.au:1234/?go=abc&123',
+ search: '?go=abc&123',
+ query: 'go=abc&123',
+ pathname: '/' },
+ inspect: { useful: [Function], getDomain: [Function] } }
### tldtools.tldCacheRefresh(onSuccess, onFail)
-Rebuilds the local in-memory cache from either the remote TLD datasource, or a local copy of effective_tld_names.dat if the local copy exists.
+Rebuilds the local in-memory cache from either the remote TLD datasource, or a local copy of `effective_tld_names.dat` if the local copy exists.
-* onSuccess - success callback
-* onFail(errorMessage) - failure callback
+* onSuccess - success callback `function()`
+* onFail - failure callback `function(errorMessage)`
### tldtools.whois(fqdn, opts = {});
@@ -50,19 +72,26 @@ Available options (opts)
* hostName - whois hostname (default whois.internic.net)
* port - whois port (default 43)
-* onSuccess - whois request complete callback containing utf8 encoded whois payload (first arg)
-* onFail - failure callback containing errormessage and fqdn context
+* stream_encoding - return encoding (default 'utf8')
+* onSuccess - request complete callback `function(whoisData, fqdn, cbPassthrough)`
+* onFail - failure callback `function(errorMessage, fqdn, cbPassthrough)` failure callback
+* cbPassthrough - any extra passthrough parameters to onSuccess or onFail
eg:
tldtools.whois(
'github.com',
{
- 'onSuccess' : function(whoisData) {
+ 'onSuccess' : function(whoisData, fqdn, cbPassthrough) {
console.log(whoisData);
+ console.log(fqdn + ' ultimate success!');
+ console.log(cbPassthrough);
},
- 'onFail' : function(errorMessage, fqdn) {
- console.log(fqdn + ' WHOIS FAILED');
+ 'onFail' : function(errorMessage, fqdn, cbPassthrough) {
console.log(errorMessage);
+ console.log(fqdn + ' WHOIS FAILED');
+ console.log(cbPassthrough);
}
- });
+ },
+ 'cbPassthrough' : ['some data']
+ });
View
70 index.js
@@ -1,3 +1,24 @@
+/*
+Thanks for using tldtools, it's distributed under the MIT License (MIT)
+
+Copyright (c) 2011 Michael Pearson <ossdev@cloudspark.freshbutter.me>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in the
+Software without restriction, including without limitation the rights to use, copy,
+modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
+and to permit persons to whom the Software is furnished to do so, subject to the
+following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
var request = require('request'),
fs = require('fs'),
url = require('url'),
@@ -11,13 +32,15 @@ TLD_TOOLS = {
_whoisDefaultOpts: {
'hostName' : 'whois.internic.net',
'port' : 43,
+ 'stream_encoding' : 'utf8',
'onSuccess' : function(whoisData) {
console.log(whoisData);
},
'onFail' : function(errorMessage, fqdn) {
console.log(fqdn + ' WHOIS FAILED');
console.log(errorMessage);
- }
+ },
+ 'cbPassthrough' : {}
},
_tldCacheStruct: {},
@@ -116,7 +139,7 @@ TLD_TOOLS = {
request(self._tldSource, function(error, res, body) {
if (error) {
if (undefined != onFail) {
- console.log('No local source, retrieving from remote host ' + this._tldSource);
+ console.log('No local source, retrieving from remote host ' + self._tldSource);
onFail(body);
}
} else {
@@ -193,16 +216,20 @@ TLD_TOOLS = {
whois: function(fqdn, opts) {
var self = this;
+ var domainParts = this.extract(fqdn);
+ // ---------------------- Opt parsing
if (undefined == opts) {
opts = this._whoisDefaultOpts;
- }
- var domainParts = this.extract(fqdn);
-
+ }
var onSuccess = (undefined != opts.onSuccess) ? opts.onSuccess : this._whoisDefaultOpts.onSuccess;
var onFail = (undefined != opts.onFail) ? opts.onFail : this._whoisDefaultOpts.onFail;
+ var cbPassthrough = (undefined != opts.cbPassthrough) ? opts.cbPassthrough : this._whoisDefaultOpts.cbPassthrough;
+ var streamEncoding = (undefined != opts.stream_encoding) ? opts.stream_encoding : this._whoisDefaultOpts.stream_encoding;
+
+ // assemble domain
+ if ( domainParts.inspect.useful() ) {
- if (domainParts.domain != '' && domainParts.tld != '') {
- var domainName = domainParts.domain + '.' + domainParts.tld;
+ domainName = domainParts.inspect.getDomain();
if (undefined == this._whoisCacheStruct.domainName) {
@@ -210,27 +237,34 @@ TLD_TOOLS = {
var port = (undefined != opts.port) ? opts.port : this._whoisDefaultOpts.port;
var stream = net.createConnection(port, hostName);
- stream.setEncoding('utf8');
+ stream.setEncoding(streamEncoding);
+
+ // Request whois info
stream.addListener('connect', function() {
stream.write(domainName + "\r\n");
});
+ // Data callback
stream.addListener('data', function(data) {
self._whoisCacheStruct.domainName = data;
- onSuccess(data);
+ // @todo key parsed data
+ onSuccess( { 'data_utf8_raw' : data }, fqdn, cbPassthrough );
});
+ // connection end
stream.addListener('end', function() {
stream.end();
});
+ // error callback
stream.addListener('error', function(exception) {
- onFail(exception.description, fqdn);
+ onFail(exception.description, fqdn, cbPassthrough);
});
} else {
onSuccess(this._whoisCacheStruct.domainName);
}
} else {
+ // doesn't look like a domain we can parse
onFail('Invalid Domain Name', fqdn);
}
},
@@ -238,6 +272,7 @@ TLD_TOOLS = {
// Attempts to extract the tld, domain and subdomain parts from the supplied 'fqdn' string
//
extract: function(fqdn) {
+
var tld = [], subdomain = [], domain = '';
var urlTokens = url.parse(fqdn);
var hostName = (undefined != urlTokens.hostname) ? urlTokens.hostname : urlTokens.pathname;
@@ -258,7 +293,7 @@ TLD_TOOLS = {
} else if (domain == '') {
domain = idxVal;
} else {
- subdomain.push(idxVal);
+ subdomain.unshift(idxVal);
}
--tldDepth;
}
@@ -266,7 +301,16 @@ TLD_TOOLS = {
return {
'subdomain' : subdomain.join('.'),
'domain' : domain,
- 'tld': tld.join('.')
+ 'tld': tld.join('.'),
+ 'url_tokens' : urlTokens,
+ 'inspect' : {
+ 'useful': function() {
+ return (this.domain != '' && tld != '');
+ },
+ 'getDomain' : function() {
+ return this.domain + '.' + this.tld;
+ }
+ }
};
},
@@ -300,4 +344,4 @@ TLD_TOOLS = {
}
}
-module.exports = TLD_TOOLS.init();
+module.exports = TLD_TOOLS.init();
View
6 package.json
@@ -1,7 +1,7 @@
{
"name": "tldtools",
- "description": "Extracts a domain into its component parts (based on John Kurkowski's tldextract python lib)",
- "version": "0.0.4",
+ "description": "Extracts a domain into its component parts (node-url wrapper), performs domain inspection functions",
+ "version": "0.0.5",
"maintainers": [
{
"name": "Michael Pearson",
@@ -16,6 +16,6 @@
"keywords": ["domain", "tld", "extract", "parse", "subdomain", "domain tools", "whois"],
"repository" : {
"type": "git" ,
- "url": "http://github.com/mjpearson/tldextract.git"
+ "url": "http://github.com/mjpearson/tldtools.git"
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.