Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
  • 2 commits
  • 14 files changed
  • 0 commit comments
  • 2 contributors
View
122 README.md
@@ -1,86 +1,102 @@
m2node
======
-m2node is a mongrel2 handler for node
+m2node is a mongrel2 handler for node.
-install
+Install
-------
- npm install m2node
+ npm install m2node
-example
+If you are having issues building the zmq module because of a missing `ev.h` file, then review the following.
+
+**Note: These instructions are valid on Mac as of node v0.6.0 and 0MQ v2.1.10.**
+
+Node v0.6.0 moved the ev.h file to `/usr/local/include/node/uv-private/ev.h`.
+
+To fix this install `libev` separately:
+
+ # Install Node v0.6.0 from source (brew is currently at Node v4.12)
+ git clone https://github.com/joyent/node.git
+ git checkout v0.6.0
+ cd node && ./configure && make && make install
+
+ # Install libev separately
+ brew install libev
+
+ # Install 0MQ v2.1.10
+ brew install zeromq
+
+ # Install the zmq module
+ npm install -d
+
+Example
-------
-```javascript
-var http = require('http'),
+ var http = require('http'),
m2node = require('m2node');
-var server = http.createServer(function (req, res) {
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('Hello World\n');
-});
+ var server = http.createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('Hello World\n');
+ });
-m2node.run(server, {
- send_spec: 'tcp://127.0.0.1:9996',
- recv_spec: 'tcp://127.0.0.1:9997'
-});
-```
+ m2node.run(server, {
+ send_spec: 'tcp://127.0.0.1:9996',
+ recv_spec: 'tcp://127.0.0.1:9997'
+ });
-configuration
+Configuration
-------------
-The configuration is from the perspective of the handler, so the send_spec in your mongrel2 config should match the recv_spec in your node config.
+The configuration is from the perspective of the handler, so the `send_spec` in your mongrel2 config should match the `recv_spec` in your node config.
-example - express
+Example - express
-----------------
-here's an example of serving an app built using the [express framework](http://expressjs.com/) with m2node
+Here's an example of serving an app built using the [express framework](http://expressjs.com/) with m2node
-```javascript
-var express = require('express'),
+ var express = require('express'),
m2node = require('m2node');
-var app = express.createServer();
+ var app = express.createServer();
-app.get('/', function (req, res) {
- res.send('Hello World')
-});
+ app.get('/', function (req, res) {
+ res.send('Hello World')
+ });
-m2node.run(app, {
- send_spec: 'tcp://127.0.0.1:9996'
- recv_spec: 'tcp://127.0.0.1:9997'
-});
-```
+ m2node.run(app, {
+ send_spec: 'tcp://127.0.0.1:9996'
+ recv_spec: 'tcp://127.0.0.1:9997'
+ });
-example - JSON
+Example - JSON
-----------------
Here's an example of handling a JSON message from mongrel2.
-```javascript
-var express = require('express'),
+ var express = require('express'),
m2node = require('m2node');
-var app = express.createServer();
-
-app.get('/', function (req, res) {
- res.send('Hello World')
-});
-
-json_handler = function(req, res) {
- return res.end({
- message: "hi"
- });
-};
-m2node.run(app, {
- json_handler:
- send_spec: 'tcp://127.0.0.1:9996'
- recv_spec: 'tcp://127.0.0.1:9997'
-});
-```
-
-contributors
+ var app = express.createServer();
+
+ app.get('/', function (req, res) {
+ res.send('Hello World')
+ });
+
+ json_handler = function(req, res) {
+ return res.end({
+ message: "hi"
+ });
+ };
+ m2node.run(app, {
+ json_handler:
+ send_spec: 'tcp://127.0.0.1:9996'
+ recv_spec: 'tcp://127.0.0.1:9997'
+ });
+
+Contributors
------------
-see package.json
+See package.json
View
6 examples/express-server.js
@@ -1,13 +1,19 @@
(function() {
var app, express, m2node;
+
express = require('express');
+
m2node = require('m2node');
+
app = express.createServer();
+
app.get('/', function(req, res) {
return res.send('Hello World');
});
+
m2node.run(app, {
send_spec: 'tcp://127.0.0.1:9996',
recv_spec: 'tcp://127.0.0.1:9997'
});
+
}).call(this);
View
5 examples/node-server.js
@@ -1,16 +1,21 @@
(function() {
var http, m2node, server;
+
http = require('http');
+
m2node = require('m2node');
+
server = http.createServer(function(req, res) {
console.log("" + req.method + " " + req.url);
res.writeHead(200, {
'Content-Type': 'text/plain'
});
return res.end('Hello World\n');
});
+
m2node.run(server, {
send_spec: 'tcp://127.0.0.1:9996',
recv_spec: 'tcp://127.0.0.1:9997'
});
+
}).call(this);
View
6 lib/m2node.js
@@ -1,9 +1,14 @@
(function() {
var FakeSocket, Handler, JSONResponse;
+
FakeSocket = require('./m2node/fake_socket').FakeSocket;
+
JSONResponse = require('./m2node/json_response').JSONResponse;
+
Handler = require('./m2node/handler').Handler;
+
exports.version = '0.1.2';
+
exports.run = function(server, options) {
var handler;
handler = new Handler(options);
@@ -33,4 +38,5 @@
});
return handler;
};
+
}).call(this);
View
34 lib/m2node/fake_socket.js
@@ -1,33 +1,31 @@
(function() {
- var FakeSocket, events, sys, util;
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- };
- sys = require('sys');
+ var FakeSocket, events, util;
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
+
util = require('util');
+
events = require('events');
+
FakeSocket = (function() {
+
__extends(FakeSocket, events.EventEmitter);
+
function FakeSocket() {
this.writeBuffer = new Buffer('');
this.writable = true;
}
+
FakeSocket.prototype.destroy = function() {};
+
FakeSocket.prototype.destroySoon = function() {};
+
FakeSocket.prototype.emitData = function(buffer) {
- if (this._events && this._events['data']) {
- this.emit('data', buffer);
- }
- if (this.ondata) {
- return this.ondata(buffer, 0, buffer.length);
- }
+ if (this._events && this._events['data']) this.emit('data', buffer);
+ if (this.ondata) return this.ondata(buffer, 0, buffer.length);
};
+
FakeSocket.prototype.setTimeout = function(timeout, callback) {};
+
FakeSocket.prototype.write = function(data) {
var combinedBuffer;
data = new Buffer(data.toString());
@@ -37,7 +35,11 @@
this.writeBuffer = combinedBuffer;
return this.emit('write');
};
+
return FakeSocket;
+
})();
+
exports.FakeSocket = FakeSocket;
+
}).call(this);
View
43 lib/m2node/handler.js
@@ -1,39 +1,40 @@
(function() {
var Handler, MongrelRequest, events, zeromq;
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
+
events = require('events');
- zeromq = require('zeromq');
+
+ zeromq = require('zmq');
+
MongrelRequest = require('./mongrel_request').MongrelRequest;
+
Handler = (function() {
+
__extends(Handler, events.EventEmitter);
+
function Handler(options) {
+ var _this = this;
this.pullSocket = zeromq.createSocket('pull');
this.pullSocket.connect(options.recv_spec);
- this.pullSocket.on('message', __bind(function(message) {
+ this.pullSocket.on('message', function(message) {
var req;
req = new MongrelRequest(message);
if (req.headers.METHOD === "JSON") {
- return this.emit('json', req);
+ return _this.emit('json', req);
} else {
- return this.emit('request', req);
+ return _this.emit('request', req);
}
- }, this));
- this.pullSocket.on('error', __bind(function(e) {
- return this.emit('error', e);
- }, this));
+ });
+ this.pullSocket.on('error', function(e) {
+ return _this.emit('error', e);
+ });
this.pubSocket = zeromq.createSocket('pub');
this.pubSocket.connect(options.send_spec);
- this.pubSocket.on('error', __bind(function(e) {
- return this.emit('error', e);
- }, this));
+ this.pubSocket.on('error', function(e) {
+ return _this.emit('error', e);
+ });
}
+
Handler.prototype.sendResponse = function(request, response) {
var header, outBuffer;
header = [request.uuid, ' ', request.connectionId.length, ':', request.connectionId, ', '].join('');
@@ -42,7 +43,11 @@
response.copy(outBuffer, header.length);
return this.pubSocket.send(outBuffer);
};
+
return Handler;
+
})();
+
exports.Handler = Handler;
+
}).call(this);
View
22 lib/m2node/json_response.js
@@ -1,26 +1,28 @@
(function() {
- var JSONResponse, events, sys, util;
- var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
- function ctor() { this.constructor = child; }
- ctor.prototype = parent.prototype;
- child.prototype = new ctor;
- child.__super__ = parent.prototype;
- return child;
- };
- sys = require('sys');
+ var JSONResponse, events, util;
+ var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
+
util = require('util');
+
events = require('events');
+
JSONResponse = (function() {
+
__extends(JSONResponse, events.EventEmitter);
+
function JSONResponse() {
this.writeBuffer = null;
}
+
JSONResponse.prototype.end = function(data) {
this.writeBuffer = new Buffer(JSON.stringify(data));
return this.emit('write');
};
+
return JSONResponse;
+
})();
+
exports.JSONResponse = JSONResponse;
+
}).call(this);
View
26 lib/m2node/mongrel_request.js
@@ -1,6 +1,8 @@
(function() {
var MongrelRequest;
+
MongrelRequest = (function() {
+
function MongrelRequest(messageBuffer) {
var position, rawHeaders, _, _ref, _ref2, _ref3, _ref4, _ref5;
_ref = this._stringUntilDelimiter(messageBuffer), this.uuid = _ref[0], position = _ref[1];
@@ -10,29 +12,25 @@
this.headers = rawHeaders ? JSON.parse(rawHeaders) : {};
_ref5 = this._stringFromNetstring(messageBuffer, position), this.body = _ref5[0], _ = _ref5[1];
}
+
MongrelRequest.prototype.toFullHttpRequest = function() {
var k, request, v, _ref;
request = [];
request.push(this.headers.METHOD + ' ' + this.headers.URI + ' HTTP/1.1\r\n');
_ref = this.headers;
for (k in _ref) {
v = _ref[k];
- if (k.match(/^[^A-Z]+$/)) {
- request.push("" + k + ": " + v + "\r\n");
- }
+ if (k.match(/^[^A-Z]+$/)) request.push("" + k + ": " + v + "\r\n");
}
request.push("\r\n");
request.push(this.body);
return new Buffer(request.join(''));
};
+
MongrelRequest.prototype._stringUntilDelimiter = function(buffer, position, delimiter) {
var index, string;
- if (position == null) {
- position = 0;
- }
- if (delimiter == null) {
- delimiter = 0x20;
- }
+ if (position == null) position = 0;
+ if (delimiter == null) delimiter = 0x20;
string = null;
index = position;
while (index < buffer.length) {
@@ -45,11 +43,10 @@
}
return [string, position];
};
+
MongrelRequest.prototype._stringFromNetstring = function(buffer, position) {
var index, len, string;
- if (position == null) {
- position = 0;
- }
+ if (position == null) position = 0;
string = null;
index = position;
while (++index < buffer.length) {
@@ -63,12 +60,17 @@
}
return [string, position];
};
+
MongrelRequest.prototype._splitString = function(string, delimiter, limit) {
var result;
result = string.split(delimiter);
return result.slice(0, limit - 1).concat([result.slice(limit - 1).join(delimiter)]);
};
+
return MongrelRequest;
+
})();
+
exports.MongrelRequest = MongrelRequest;
+
}).call(this);
View
7 package.json
@@ -10,13 +10,12 @@
[ "Paul Bergeron <paul.d.bergeron@gmail.com>" ],
"main" : "./lib/m2node.js",
"repository" : { "type": "git", "url" : "https://github.com/dan-manges/m2node.git" },
- "engines" : { "node": "~0.4.8" },
+ "engines" : { "node": ">=0.6.0" },
"bundledDependencies" : [ "" ],
"dependencies" : {
- "zeromq": "~0.5.1"
+ "zmq": "1.0.3"
},
"devDependencies" : {
- "coffee-script": "~1.1",
- "vows": "=0.5.8"
+ "vows": "=0.5.13"
}
}
View
1 spec/fake_socket_spec.js
@@ -1,6 +1,5 @@
var vows = require('vows');
var assert = require('assert');
-var sys = require('sys');
var FakeSocket = require('../lib/m2node/fake_socket').FakeSocket;
vows.describe('fake socket').addBatch({
View
1 spec/m2node_spec.js
@@ -2,7 +2,6 @@ var vows = require('vows');
var assert = require('assert');
var http = require('http');
var m2node = require('../lib/m2node');
-var sys = require('sys');
vows.describe('m2node').addBatch({
'smoke test': {
View
1 src/m2node/fake_socket.coffee
@@ -1,4 +1,3 @@
-sys = require 'sys'
util = require 'util'
events = require 'events'
View
2 src/m2node/handler.coffee
@@ -1,5 +1,5 @@
events = require 'events'
-zeromq = require 'zeromq'
+zeromq = require 'zmq'
{MongrelRequest} = require './mongrel_request'
View
1 src/m2node/json_response.coffee
@@ -1,4 +1,3 @@
-sys = require 'sys'
util = require 'util'
events = require 'events'

No commit comments for this range

Something went wrong with that request. Please try again.