Permalink
Browse files

make up.pipe() work

  • Loading branch information...
1 parent b65dd60 commit 461c0f16793be63967304712a680ff425dd9471d @substack committed Jul 29, 2012
Showing with 43 additions and 13 deletions.
  1. +30 −1 index.js
  2. +13 −12 lib/server_handle.js
View
@@ -15,11 +15,40 @@ var upnode = module.exports = function (cons) {
return connect.apply(null, [ up, cons ].concat(args));
};
+ self.pipe = function (target) {
+ var d = serverHandle(cons);
+ self._serverHandle = d;
+ return d.pipe(target);
+ };
+
+ self.writable = true;
+ self.readable = true;
+
+ [ 'write', 'end', 'destroy' ].forEach(function (name) {
+ self[name] = function (buf) {
+ var h = self._serverHandle;
+ if (!h) h = self._serverHandle = serverHandle(cons);
+ return h[name].apply(h, arguments);
+ };
+ });
+
self.listen = function () {
var args = parseArgs(arguments);
var server = net.createServer();
- server.on('connection', serverHandle(server, cons));
+ server._ds = [];
+
+ server.on('connection', function (stream) {
+ var d = serverHandle(cons);
+ d.stream = stream;
+ d.pipe(stream).pipe(d);
+
+ server._ds.push(d);
+ d.once('done', function () {
+ var ix = server._ds.indexOf(d);
+ if (ix >= 0) server._ds.splice(ix, 1);
+ });
+ });
if (args.port) {
server.listen(args.port, args.host, args.block);
View
@@ -1,13 +1,7 @@
var dnode = require('dnode');
-module.exports = function (server, cons) {
- return handle.bind(null, server, cons);
-};
-
-function handle (server, cons, stream) {
+module.exports = function (cons) {
var d = dnode(cons);
- d.stream = stream;
- d.pipe(stream).pipe(d);
d.on('local', function (local) {
if (local.ping === undefined) {
@@ -33,16 +27,23 @@ function handle (server, cons, stream) {
var onend = function () {
stream.destroy();
clearInterval(iv);
- var ix = server._ds.indexOf(d);
- if (ix >= 0) server._ds.splice(ix, 1);
+
+ if (!d._done) {
+ d._done = true;
+ d.emit('done');
+ }
};
- if (!server._ds) server._ds = [];
- server._ds.push(d);
-
stream.once('end', onend);
stream.once('disconnect', onend);
stream.once('close', onend);
stream.once('error', onend);
});
+
+ var stream;
+ d.on('pipe', function (target) {
+ if (!stream) stream = target;
+ });
+
+ return d;
}

0 comments on commit 461c0f1

Please sign in to comment.