Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[refactor] Make `net` a plugin #24

Merged
merged 2 commits into from

3 participants

Maciej Małecki Charlie Robbins Bradley Meck
Maciej Małecki

Some users might not necessarily want to monkey-punch
net.Server#listen method, given pluggable nature of haibu-carapace.

This has to be applied with some changes to haibu, mostly adding
--plugin net to command line used for spawning carapace binary.

mmalecki added some commits
Maciej Małecki mmalecki [refactor] Make `net` a plugin
Some users might not necessarily want to monkey-punch
`net.Server#listen` method, given pluggable nature of `haibu-carapace`.

This has to be applied with some changes to `haibu`, mostly adding
`--plugin net` to command line used for spawning `carapace` binary.
9cd842f
Maciej Małecki mmalecki [test] Adjust test to require `net` plugin and use new APIs
`versions-test` had to be deleted as it's no longer appropriate.
5c505ba
Charlie Robbins
Owner

@mmalecki lgtm

Bradley Meck bmeck merged commit edc01f4 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 6, 2012
  1. Maciej Małecki

    [refactor] Make `net` a plugin

    mmalecki authored
    Some users might not necessarily want to monkey-punch
    `net.Server#listen` method, given pluggable nature of `haibu-carapace`.
    
    This has to be applied with some changes to `haibu`, mostly adding
    `--plugin net` to command line used for spawning `carapace` binary.
  2. Maciej Małecki

    [test] Adjust test to require `net` plugin and use new APIs

    mmalecki authored
    `versions-test` had to be deleted as it's no longer appropriate.
This page is out of date. Refresh to see the latest.
20 lib/carapace.js
View
@@ -31,21 +31,6 @@ carapace.onAny(function (data) {
});
//
-// Cache the version of node running in case a user-space
-// script attempts to overwrite it;
-//
-var nodeVersion = process.version;
-
-//
-// Require the `net` module for observing and overriding
-// relevant events and functions in the core
-// node.js `net` module
-// Do the require prior to any chroot or chdir
-// Only override once all plugins have been loaded
-//
-var overrideNet = require('./net').getOverride(nodeVersion);
-
-//
// Expose the `cli` module for default options
// and liberal arguments parsing
//
@@ -164,11 +149,6 @@ carapace.run = function (override, callback) {
carapace.cli.rewrite(carapace.script, carapace.argv, override);
//
- // Start up the hooks for the net module
- //
- overrideNet();
-
- //
// Clear the module cache so anything required by `haibu-carapace`
// is reloaded as necessary.
//
58 lib/net.js → lib/plugins/net.js
View
@@ -1,3 +1,4 @@
+
/*
* net.js: Wrapper around node.js core `net` module for observing relevant events
*
@@ -7,7 +8,29 @@
var net = require('net'),
semver = require('semver'),
- carapace = require('./carapace');
+ carapace = require('../carapace');
+
+//
+// ### function override (version)
+// Helper function which overrides with the correct `net` module
+// override for current node version.
+//
+module.exports = function (carapace) {
+ carapace.net = function (argv, next) {
+ var version = process.version,
+ methods = Object.keys(module.exports),
+ method;
+
+ for (var i = 0; i < methods.length; i++) {
+ method = module.exports[methods[i]];
+ if (method.nodeVersion && semver.satisfies(version, method.nodeVersion)) {
+ method();
+ return next();
+ }
+ }
+ throw new Error('No appropiate net override for node version ' + version);
+ };
+};
//
// ### function toPort(x)
@@ -68,7 +91,7 @@ function reservedPort(desired) {
// ### function _doListen ()
// Override for `node@0.4.x`.
//
-exports._doListen = function overrideNet() {
+module.exports._doListen = function overrideNet() {
var binding = process.binding('net'),
socket = binding.socket,
listen = binding.listen,
@@ -196,7 +219,7 @@ exports._doListen = function overrideNet() {
// ### function _listen2 ()
// Override for `node@0.5.x` and `node@0.6.x`.
//
-exports._listen2 = function overrideNet() {
+module.exports._listen2 = function overrideNet() {
var _listen2 = net.Server.prototype._listen2;
net.Server.prototype._listen2 = function ourListen(address, port, addressType, desired) {
@@ -321,31 +344,6 @@ exports._listen2 = function overrideNet() {
// Setup the valid `nodeVersion` for all of the
// monkey patches to `require('net')`.
//
-exports._listen2.nodeVersion = '0.5.x || 0.6.x || 0.7.x || 0.8.x';
-exports._doListen.nodeVersion = '0.4.x';
+module.exports._listen2.nodeVersion = '0.5.x || 0.6.x || 0.7.x || 0.8.x';
+module.exports._doListen.nodeVersion = '0.4.x';
-//
-// ### function getOverride (version)
-// #### @version {string} Node version to get `net` override.
-// Helper function which responds with the correct `net` module
-// override for the specified `version`.
-//
-exports.getOverride = function (version) {
- var matches = Object.keys(exports).filter(function (method) {
- if (!exports[method].nodeVersion) {
- return false;
- }
-
- return semver.satisfies(version, exports[method].nodeVersion);
- }).map(function (method) {
- return exports[method];
- });
-
- if (!matches.length) {
- return null;
- }
-
- return matches.length > 1
- ? matches
- : matches[0];
-};
1  test/helper/macros.js
View
@@ -86,6 +86,7 @@ macros.assertSpawn = function (PORT, script, argv, vows) {
macros.assertParentSpawn = function (options, /*PORT, script, argv, cwd,*/ vows) {
options.argv = options.argv.slice(0);
+ options.argv.push('--plugin', 'net');
options.argv.push(options.script);
var context = {
2  test/net/errors-test.js
View
@@ -14,7 +14,7 @@ var assert = require('assert'),
carapace = require('../../lib/carapace');
var script = path.join(__dirname, '..', 'fixtures', 'eacces.js'),
- argv = ['--setuid', 'nobody', script];
+ argv = ['--plugin', 'net', '--setuid', 'nobody', script];
vows.describe('carapace/net/dolisten').addBatch({
"When using haibu-carapace": {
2  test/net/net-multiple-servers-test.js
View
@@ -15,7 +15,7 @@ var assert = require('assert'),
var script = path.join(__dirname, '..', 'fixtures', 'multi-server.js'),
testPort = 8000,
- argv = [script];
+ argv = ['--plugin', 'net', script];
vows.describe('carapace/net/dolisten').addBatch({
"When using haibu-carapace": {
32 test/net/versions-test.js
View
@@ -1,32 +0,0 @@
-/*
- * versions-test.js: Tests asserting that the correct override is set.
- *
- * (C) 2011 Nodejitsu Inc.
- * MIT LICENCE
- *
- */
-
-var assert = require('assert'),
- vows = require('vows'),
- carapace = require('../../lib/carapace'),
- net = require('../../lib/net');
-
-function assertOverride(fnName) {
- var context = {};
- context['should respond with ' + fnName] = function () {
- var override = net.getOverride(this.context.name);
- assert.equal(override, net[fnName]);
- };
-
- return context;
-}
-
-vows.describe('carapace/net/multiple-versions').addBatch({
- "When using the haibu-carapace net module": {
- "the getOverride() method": {
- "v0.4.12": assertOverride('_doListen'),
- "v0.5.2": assertOverride('_listen2'),
- "v0.6.6": assertOverride('_listen2')
- }
- }
-}).export(module);
40 test/spawn/local-run-test.js
View
@@ -19,27 +19,33 @@ var script = path.join(__dirname, '..', '..', 'examples', 'chroot-jail', 'server
vows.describe('carapace/spawn/local').addBatch({
"When using haibu-carapace": {
- "and running `./server.js` with no plugins": helper.assertRun(script, null, {
- "should set the correct exports on carapace._module": function (_, _) {
- assert.equal(carapace._module.exports.port, 1337);
+ "with `net` plugin": {
+ topic: function () {
+ carapace.use(path.resolve(__dirname, '..', '..', 'lib', 'plugins', 'net'));
+ carapace.net([], this.callback);
},
- "should emit the `port` event": {
- topic: function () {
- carapace.on('port', this.callback.bind(carapace, null));
+ "and running `./server.js`": helper.assertRun(script, null, {
+ "should set the correct exports on carapace._module": function (_, _) {
+ assert.equal(carapace._module.exports.port, 1337);
},
- "with the correct port": function (err, info) {
- assert.equal(carapace.event, 'port');
- assert.equal(info.desired, 1337);
- },
- "should correctly start the HTTP server": {
- topic: function (info) {
- request({ uri: 'http://localhost:' + info.port }, this.callback);
+ "should emit the `port` event": {
+ topic: function () {
+ carapace.on('port', this.callback.bind(carapace, null));
+ },
+ "with the correct port": function (err, info) {
+ assert.equal(carapace.event, 'port');
+ assert.equal(info.desired, 1337);
},
- "that responds with a cwd": function (err, res, body) {
- assert.equal(body, process.cwd());
+ "should correctly start the HTTP server": {
+ topic: function (info) {
+ request({ uri: 'http://localhost:' + info.port }, this.callback);
+ },
+ "that responds with a cwd": function (err, res, body) {
+ assert.equal(body, process.cwd());
+ }
}
}
- }
- })
+ })
+ }
}
}).export(module);
Something went wrong with that request. Please try again.