Skip to content

Commit

Permalink
improved error objects to include a toString() function, added regexp…
Browse files Browse the repository at this point in the history
… check for localhost(.localdomain)(:###) to go straight to http regardless of tls fallback.
  • Loading branch information
silverbucket committed Oct 20, 2014
1 parent 762ebf3 commit ea64478
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webfinger.js",
"version": "2.0.4",
"version": "2.0.5",
"description": "a simple webfinger record lookup library",
"license": "AGPL",
"private": false,
Expand Down
35 changes: 25 additions & 10 deletions src/webfinger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- mode:js; js-indent-level:2 -*-
/*!
* webfinger.js
* version 2.0.4
* version 2.0.5
* http://github.com/silverbucket/webfinger.js
*
* Developed and Maintained by:
Expand Down Expand Up @@ -61,6 +61,13 @@ if (typeof window === 'undefined') {
LOGABLE = true;
}

function _err(obj) {
obj.toString = function () {
return this.message;
};
return obj;
}

/**
* Function: WebFinger
*
Expand Down Expand Up @@ -98,24 +105,24 @@ if (typeof window === 'undefined') {
if (self._isValidJSON(xhr.responseText)) {
cb(null, xhr.responseText);
} else {
cb({
cb(_err({
message: 'invalid json',
url: url,
status: xhr.status
});
}));
}
} else if (xhr.status === 404) {
cb({
cb(_err({
message: 'webfinger endpoint unreachable',
url: url,
status: xhr.status
});
}));
} else {
cb({
cb(_err({
message: 'error during request',
url: url,
status: xhr.status
});
}));
}
}
};
Expand All @@ -134,6 +141,12 @@ if (typeof window === 'undefined') {
return true;
};

WebFinger.prototype._isLocalhost = function (host) {
console.log('checking: ', host);
var local = /^localhost(\.localdomain)?(\:[0-9]+)?$/;
return local.test(host);
};

WebFinger.prototype._log = function () {
var args = Array.prototype.splice.call(arguments, 0);
if ((this.config.debug) && (LOGABLE)) {
Expand All @@ -149,9 +162,9 @@ if (typeof window === 'undefined') {
if ((typeof parsedJRD !== 'object') ||
(typeof parsedJRD.links !== 'object')) {
if (typeof parsedJRD.error !== 'undefined') {
cb({ message: parsedJRD.error });
cb(_err({ message: parsedJRD.error }));
} else {
cb({ message: 'received unknown response from server' });
cb(_err({ message: 'received unknown response from server' }));
}
return false;
}
Expand Down Expand Up @@ -209,8 +222,10 @@ if (typeof window === 'undefined') {
var protocol = 'https'; // we use https by default

if (parts.length !== 2) {
cb({ message: 'invalid user address ( should be in the format of: [email protected] )' });
cb(_err({ message: 'invalid user address ( should be in the format of: [email protected] )' }));
return false;
} else if (self._isLocalhost(host)) {
protocol = 'http';
}

function _buildURL() {
Expand Down
16 changes: 16 additions & 0 deletions test/01-basics-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,28 @@ define(['require', './../src/webfinger.js'], function (require, amdwf) {
test.throws(env.wf.lookup, Error, 'caught thrown exception');
}
},

{
desc: 'calling with invalid useraddress',
run: function (env, test) {
test.throws(function () { env.wf.lookup('asdfg'); }, Error, 'caught thrown exception');
}
},

{
desc: 'allow for port localhost without ssl',
run: function (env, test) {
env.wf.lookup('me@localhost:8001', function (err, data) {
if (err) {
test.assertAnd(err.url.indexOf('http://'), 0);
test.assert(err.message, 'error during request');
} else {
test.done();
}
});
}
},

{
desc: 'calling with correct useraddress (needs internet connectivity)',
run: function (env, test) {
Expand Down

0 comments on commit ea64478

Please sign in to comment.