Skip to content
Browse files

Merge git@github.com:banksean/node-pshb

  • Loading branch information...
2 parents 3c719cf + 8312847 commit 5a1ac233ef24d58e571e02826698edb143536a96 @banksean committed Feb 26, 2010
Showing with 99 additions and 138 deletions.
  1. +13 −112 lib/pshb-client.js
  2. +86 −26 test.js
View
125 lib/pshb-client.js
@@ -14,7 +14,7 @@ var Subscriber = function(callbackPort, async) {
}
this.callbackPort = callbackPort;
if (async) {
- this.verify = 'async';
+ this.verify = 'async'; // TODO
} else {
this.verify = 'sync';
}
@@ -25,6 +25,7 @@ var Subscriber = function(callbackPort, async) {
exports.Subscriber = Subscriber;
Subscriber.prototype.createCallbackUri = function() {
+ // TODO: use an actual hostname.
return "http://localhost:" + this.callbackPort + "/";
};
@@ -46,14 +47,16 @@ Subscriber.prototype.subscribe = function(topicUri, hubUri, callbackUri) {
"hub.secret=" + querystring.escape(secret),
"hub.verify_token=" + querystring.escape(verify_token)
].join("&");
- sys.puts(hubUri.href);
- sys.puts(requestBody);
-
+
+ hubUri = url.parse(hubUri.href);
+
var promise = new process.Promise();
+
var port = hubUri.port;
if (!port) {
port = 80;
}
+
var hubHost = hubUri.host;
if (hubHost.indexOf(":") != -1) {
hubHost = hubHost.split(":")[0];
@@ -121,8 +124,7 @@ Subscriber.prototype.listen = function(topicUri) {
if (topicUri.search) {
topicPath += topicUri.search;
}
- sys.puts('fetching ' + topicHost + ":" + topicPort + topicPath);
-
+
var topicClient = http.createClient(topicPort, topicHost);
var topicRequest = topicClient.request("GET", topicPath, {"Host": topicHost});
var topicEvents = new events.EventEmitter();
@@ -131,23 +133,15 @@ Subscriber.prototype.listen = function(topicUri) {
var body = "";
response.setBodyEncoding("utf8");
response.addListener('data', function (chunk) {
- //sys.puts("data: " + chunk);
body += chunk;
});
response.addListener('end', function() {
- sys.puts("end");
var atomPromise = atom.parse(body);
atomPromise.addCallback(function(feed) {
- sys.puts("Feed: " + feed.title)
var hubUri = feed.getLinksByRel('hub')[0];
- sys.puts("hub: " + hubUri.href);
- // TODO: undo this
- hubUri = url.parse('http://localhost:8086/subscribe');
- topicUri = url.parse('http://localhost/foo');
var callbackUri = url.parse(subscriber.createCallbackUri());
- sys.puts('callback: ' + subscriber.createCallbackUri());
var subPromise = subscriber.subscribe(topicUri, hubUri, callbackUri);
subPromise.addCallback(function() {
topicEvents.emit('subscribed', topicUri.href);
@@ -160,23 +154,11 @@ Subscriber.prototype.listen = function(topicUri) {
});
});
topicRequest.close();
- /*
- var subscribePromise = this.subscribe(topicUri, hubUri, callbackUri);
- // TODO: fetch hub URI from the atom feed at topicUri instead of
- // assuming the specified hub works for the topicUri.
-
- subscribePromise.addCallback(function() {
- this.registerEventEmitter(topicUri, topicEvents);
- });
- subscribePromise.addErrback(function(body) {
- topicEvents.emit('error', body);
- })
- */
+
return topicEvents;
}
Subscriber.prototype.registerEventEmitter = function(topicUri, topicEvents) {
- sys.puts('registering listener for topic: [' + topicUri.href + ']');
// TODO: make this be a collection of event emitters instead of a single one.
this.topicEventEmitters[topicUri.href] = topicEvents;
}
@@ -185,14 +167,12 @@ Subscriber.prototype.registerEventEmitter = function(topicUri, topicEvents) {
* You have to call this method to start listening for subscription
* confirmation and update requests from hubs.
*/
-Subscriber.prototype.startServer = function() {
+Subscriber.prototype.startCallbackServer = function() {
// start the server waiting for subscription confirmations and
// feed updates.
- sys.puts("starting server on port " +this.callbackPort);
+ sys.puts("starting callback server on port " + this.callbackPort);
var subscriber = this;
http.createServer(function (request, response) {
- sys.puts("REQ: " + request.url);
- sys.puts("method: " + request.method);
if (request.method == 'GET') {
var parsedUrl = url.parse(request.url);
var paramList = parsedUrl.query.split("&");
@@ -214,105 +194,26 @@ Subscriber.prototype.startServer = function() {
response.write(params['hub.challenge']);
response.close();
} else if (request.method == 'POST') {
+ sys.puts("POST from hub");
var body= '';
request.setBodyEncoding("utf8");
request.addListener('data', function(data) {
body += data;
});
request.addListener('end', function() {
- sys.puts("UPDATE: " + body);
+ response.sendHeader(200);
response.close();
var atomPromise = atom.parse(body);
atomPromise.addCallback(function(feed) {
var topicId = trim(feed.id);
- sys.puts("feed ID: [" + topicId + ']');
var events = subscriber.topicEventEmitters[topicId];
- sys.puts('events: ' + events);
events.emit('update', feed);
});
});
}
}).listen(this.callbackPort);
-
- // This is just for testing purposes, to have a dummy Atom feed
- // that always has 'updates' ready.
- http.createServer(function (request, response) {
- var uri = request.uri;
- sys.puts("REQ: " + request.url);
- response.sendHeader(200);
- var nowStr = new Date().toISO8601String();
- response.write('\
- <feed xmlns="http://www.w3.org/2005/Atom"\
- xml:lang="en"\
- xml:base="http://www.example.org">\
- <id>http://localhost/foo</id>\
- <title>My Simple Feed</title>\
- <updated>' + nowStr + '</updated>\
- <link href="/foo" />\
- <link rel="self" href="/foo" />\
- <entry>\
- <id>http://www.example.org/entries/1</id>\
- <title>A simple blog entry</title>\
- <link href="foo/entries/1" />\
- <updated>' + nowStr + '</updated>\
- <summary>This is a simple blog entry</summary>\
- </entry>\
- </feed>\
- ');
- response.close();
- }).listen(80);
}
function trim(str) {
return str.replace(/^\s+|\s+$/g,"");
}
-
-//////
-
-Date.prototype.toISO8601String = function (format, offset) {
- /* accepted values for the format [1-6]:
- 1 Year:
- YYYY (eg 1997)
- 2 Year and month:
- YYYY-MM (eg 1997-07)
- 3 Complete date:
- YYYY-MM-DD (eg 1997-07-16)
- 4 Complete date plus hours and minutes:
- YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
- 5 Complete date plus hours, minutes and seconds:
- YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
- 6 Complete date plus hours, minutes, seconds and a decimal
- fraction of a second
- YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
- */
- if (!format) { var format = 6; }
- if (!offset) {
- var offset = 'Z';
- var date = this;
- } else {
- var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
- var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
- offsetnum *= ((d[1] == '-') ? -1 : 1);
- var date = new Date(Number(Number(this) + (offsetnum * 60000)));
- }
-
- var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; }
-
- var str = "";
- str += date.getUTCFullYear();
- if (format > 1) { str += "-" + zeropad(date.getUTCMonth() + 1); }
- if (format > 2) { str += "-" + zeropad(date.getUTCDate()); }
- if (format > 3) {
- str += "T" + zeropad(date.getUTCHours()) +
- ":" + zeropad(date.getUTCMinutes());
- }
- if (format > 5) {
- var secs = Number(date.getUTCSeconds() + "." +
- ((date.getUTCMilliseconds() < 100) ? '0' : '') +
- zeropad(date.getUTCMilliseconds()));
- str += ":" + zeropad(secs);
- } else if (format > 4) { str += ":" + zeropad(date.getUTCSeconds()); }
-
- if (format > 3) { str += offset; }
- return str;
-}
View
112 test.js
@@ -4,35 +4,17 @@ var sys = require('sys'),
http = require("http"),
events = require("events"),
url = require("url"),
- pshb = require("./lib/pshb-client"),
- webfinger = require("./lib/webfinger-client");
-
+ pshb = require("./lib/pshb-client");
+
var callbackPort = 4443;
var subscriber = new pshb.Subscriber(callbackPort);
-subscriber.startServer(); // Start listening for subscription confirmation callbacks.
-
-
-var hubUri = url.parse("http://localhost:8086/subscribe");
-var topicUri = url.parse("http://www.blogger.com/feeds/3462658744634470242/posts/default");
-//var callbackUri = url.parse("http://pubsubhubbub-subscriber.appspot.com/subscriber.banksean");
-/*
-var callbackUri = url.parse(subscriber.createCallbackUri());
+subscriber.startCallbackServer(); // Start listening for subscription confirmation callbacks.
-var subscribePromise = subscriber.subscribe(topicUri, hubUri, callbackUri);
+var topicUri = url.parse("http://localhost/foo"); // Dummy feed, created below.
-subscribePromise.addCallback(function() {
- sys.puts('success');
-});
+var feedEvents = subscriber.listen(topicUri);
-subscribePromise.addErrback(function(msg) {
- sys.puts('ERROR');
- sys.puts(msg);
- sys.puts('');
-});
-*/
-
-var feedEvents = subscriber.listen(url.parse("http://www.blogger.com/feeds/3462658744634470242/posts/default"));
feedEvents.addListener('subscribed',
function(feed) {
sys.puts('subscribed: ' + feed);
@@ -45,8 +27,86 @@ feedEvents.addListener('error',
feedEvents.addListener('update',
function(atomFeed) {
- sys.puts('update: ' + atomFeed)
+ sys.puts('got a PubSubHubub update: ' + atomFeed.id);
}
);
-
-
+
+// Set up a dummy feed that's always got "updates"
+// This is just for testing purposes, to have a dummy Atom feed
+// that always has 'updates' ready.
+http.createServer(function (request, response) {
+ var uri = request.uri;
+ sys.puts("REQ: " + request.url);
+ response.sendHeader(200);
+ var nowStr = new Date().toISO8601String();
+ response.write('\
+ <feed xmlns="http://www.w3.org/2005/Atom"\
+ xml:lang="en"\
+ xml:base="http://www.example.org">\
+ <id>http://localhost/foo</id>\
+ <title>My Simple Feed</title>\
+ <updated>' + nowStr + '</updated>\
+ <link rel="hub" href="http://localhost:8086/subscribe" />\
+ <link href="/foo" />\
+ <link rel="self" href="/foo" />\
+ <entry>\
+ <id>http://www.example.org/entries/1</id>\
+ <title>A simple blog entry</title>\
+ <link href="foo/entries/1" />\
+ <updated>' + nowStr + '</updated>\
+ <summary>This is a simple blog entry</summary>\
+ </entry>\
+ </feed>\
+ ');
+ response.close();
+}).listen(80);
+
+////// js dates. meh.
+
+Date.prototype.toISO8601String = function (format, offset) {
+ /* accepted values for the format [1-6]:
+ 1 Year:
+ YYYY (eg 1997)
+ 2 Year and month:
+ YYYY-MM (eg 1997-07)
+ 3 Complete date:
+ YYYY-MM-DD (eg 1997-07-16)
+ 4 Complete date plus hours and minutes:
+ YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
+ 5 Complete date plus hours, minutes and seconds:
+ YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
+ 6 Complete date plus hours, minutes, seconds and a decimal
+ fraction of a second
+ YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
+ */
+ if (!format) { var format = 6; }
+ if (!offset) {
+ var offset = 'Z';
+ var date = this;
+ } else {
+ var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
+ var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
+ offsetnum *= ((d[1] == '-') ? -1 : 1);
+ var date = new Date(Number(Number(this) + (offsetnum * 60000)));
+ }
+
+ var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; }
+
+ var str = "";
+ str += date.getUTCFullYear();
+ if (format > 1) { str += "-" + zeropad(date.getUTCMonth() + 1); }
+ if (format > 2) { str += "-" + zeropad(date.getUTCDate()); }
+ if (format > 3) {
+ str += "T" + zeropad(date.getUTCHours()) +
+ ":" + zeropad(date.getUTCMinutes());
+ }
+ if (format > 5) {
+ var secs = Number(date.getUTCSeconds() + "." +
+ ((date.getUTCMilliseconds() < 100) ? '0' : '') +
+ zeropad(date.getUTCMilliseconds()));
+ str += ":" + zeropad(secs);
+ } else if (format > 4) { str += ":" + zeropad(date.getUTCSeconds()); }
+
+ if (format > 3) { str += offset; }
+ return str;
+}

0 comments on commit 5a1ac23

Please sign in to comment.
Something went wrong with that request. Please try again.