Permalink
Browse files

Merge branches 'sockjs' and 'master'

  • Loading branch information...
2 parents 9a63d7f + 51aa577 commit a5a3d1c7f19d3faf6fe4a8ca4d5ea71a3a10481a @reid committed Jun 12, 2012
View
@@ -1,3 +1,4 @@
+/lib/hub/view/public/sockjs-*
/dep/
/doc/quick-start/index.mustache
/api/
View
@@ -37,7 +37,7 @@ html-api:
lint:
# Problem? Type `make install` first.
- find lib test -name "*.js" -print0 | xargs -0 ./go lint
+ find lib test -name "*.js" \! -name "*min.js" -print0 | xargs -0 ./go lint
.PHONY: lint
clean:
View
@@ -4,6 +4,8 @@ var http = require("http");
var util = require("util");
var urlParser = require("url");
+var hijack = require("../hijack");
+
var BlizzardSession = require("./session");
var EventEmitter2 = require("eventemitter2").EventEmitter2;
@@ -177,50 +179,10 @@ Blizzard.prototype.connect = function (url, cb) {
* @param {HTTPServer} httpServer
*/
Blizzard.prototype.listen = function (httpServer) {
- var self = this,
- listeners = httpServer.listeners("upgrade");
-
- httpServer.removeAllListeners("upgrade");
-
- httpServer.on("upgrade", function (req, socket, head) {
-
- // First, attempt to upgrade to Blizzard.
- var success = self.serverUpgrade(req, socket, head),
- written = false,
- originalWrite;
-
- if (!success) {
- // Blizzard was not the protocol asked for.
-
- // If data is written to the socket,
- // the connection was upgraded.
- originalWrite = socket.write;
- socket.write = function (string, encoding, fd) {
- written = true;
- originalWrite.call(this, string, encoding, fd);
- };
-
- // Try other upgrade listeners, e.g. Socket.io.
- listeners.forEach(function (fn) {
- fn(req, socket, head);
- });
-
- // Restore original write.
- socket.write = originalWrite;
-
- // No listener wrote to the socket.
- // Destroy the connection.
- if (!written && socket.writable) {
- socket.write([
- "HTTP/1.1 400 Bad Request",
- "X-Reason: Protocol not supported",
- "Connection: close",
- "Content-Length: 0",
- "", ""
- ].join("\r\n"));
- socket.end();
- }
- }
+ var self = this;
+
+ hijack(httpServer, "upgrade", function (req, socket, head) {
+ return self.serverUpgrade(req, socket, head);
});
return this;
View
@@ -0,0 +1,58 @@
+"use strict";
+
+/**
+ * @module hijack
+ */
+
+/**
+ * Attach the provided function as the first
+ * listener of the given EventEmitter event.
+ *
+ * All listeners of the event will be removed
+ * and replaced with a listener that will run
+ * the provided function first, followed by
+ * the original listeners if the function
+ * returned false.
+ *
+ * Works with Node.js v0.7+ where the array
+ * returned by `ee.listeners()` is a reference.
+ *
+ * @method hijack
+ * @param {EventEmitter} ee Emitter.
+ * @param {String} event Event name.
+ * @param {Function} firstFn Function to run first.
+ */
+module.exports = function hijack(ee, event, firstFn) {
+ var listeners = ee.listeners(event),
+ i = 0,
+ length = listeners.length,
+ originalListeners = [];
+
+ // Note: listeners is a reference in Node v0.7.
+ // Calling `removeAllListeners` no longer destroys
+ // the listener array, which causes it survive
+ // as a reference. See joyent/node commits:
+ // - 78dc13fbf97e2e3003e6f3baacdd5ff60e8de3f7
+ // - 928ea564d16da47e615ddac627e0b4d4a40d8196
+ //
+ // Make a copy first.
+ for (; i < length; i += 1) {
+ originalListeners[i] = listeners[i];
+ }
+
+ ee.removeAllListeners(event);
+
+ ee.on(event, function () {
+ var args = Array.prototype.slice.call(arguments),
+ stack = [firstFn].concat(originalListeners),
+ handled;
+
+ handled = firstFn.apply(ee, args);
+
+ if (!handled) {
+ originalListeners.forEach(function (fn) {
+ fn.apply(ee, args);
+ });
+ }
+ });
+};
View
@@ -49,7 +49,7 @@ util.inherits(Agent, EventEmitter2);
Agent.prototype.setupEvents = function () {
var self = this;
- self.socketEmitter.on("disconnect", function () {
+ self.socketEmitter.on("close", function () {
self.socketEmitter.remove(this.child);
});
View
@@ -32,8 +32,7 @@ function Batch(manager, id, session, tests, useProxy) {
}
this.testServer = new TestServer(
- '<script src="' + mountpoint + '/socket.io/socket.io.js"></script>' +
- '<script src="' + mountpoint + '/public/inject.js"></script>'
+ '<script src="' + mountpoint + '/public/inject.js"></script>'
);
EventEmitter2.call(this);
Oops, something went wrong.

0 comments on commit a5a3d1c

Please sign in to comment.