Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

using through in lib/service to get proper backpressure

  • Loading branch information...
commit 1de8d17fd34357cc099c9209e494dfc93f9fac0a 1 parent 0a4c3c9
@substack authored
Showing with 23 additions and 34 deletions.
  1. +21 −28 lib/service.js
  2. +2 −6 package.json
View
49 lib/service.js
@@ -1,6 +1,7 @@
-var HttpDuplex = require('http-duplex');
-var inherits = require('inherits');
var spawn = require('child_process').spawn;
+var through = require('through');
+var EventEmitter = require('events').EventEmitter;
+var inherits = require('inherits');
module.exports = function (opts, req, res) {
var service = new Service(opts, req, res);
@@ -19,28 +20,25 @@ var headerRE = {
'upload-pack' : '^\\S+ ([0-9a-fA-F]+)'
};
+inherits(Service, EventEmitter);
+
function Service (opts, req, res) {
var self = this;
- HttpDuplex.call(self, req, res);
+
+ self.headers = req.headers;
+ self.method = req.method;
+ self.url = req.url;
self.status = 'pending';
self.repo = opts.repo;
self.service = opts.service;
self.cwd = opts.cwd;
- var piped = false;
- self.on('pipe', function () {
- piped = true;
- });
+ var buffered = through().pause();
+ req.pipe(buffered);
- var buffered = [];
var data = '';
- self.on('data', bufferOnData);
- function bufferOnData (buf) {
- buffered.push(buf);
- }
-
- self.on('data', function ondata (buf) {
+ req.on('data', function ondata (buf) {
data += buf;
var ops = data.match(new RegExp(headerRE[self.service], 'gi'));
@@ -83,22 +81,20 @@ function Service (opts, req, res) {
opts.cwd
]);
self.emit('service', ps);
- ps.stdout.pipe(self, { end : !piped });
-
- self.removeListener('data', bufferOnData);
- buffered.forEach(function (buf) {
- ps.stdin.write(buf);
- });
- buffered = undefined;
+ ps.stdout.pipe(res);
- self.pipe(ps.stdin);
+ buffered.pipe(ps.stdin);
+ buffered.resume();
ps.on('exit', self.emit.bind(self, 'exit'));
});
});
+
+ self.once('reject', function (code, msg) {
+ res.statusCode = code;
+ res.end(msg);
+ });
}
-inherits(Service, HttpDuplex);
-
Service.prototype.accept = function (dir) {
if (this.status !== 'pending') return;
@@ -113,9 +109,6 @@ Service.prototype.reject = function (code, msg) {
msg = code;
code = 500;
}
- this.statusCode = code || 500;
- if (msg) this.write(msg);
-
this.status = 'rejected';
- this.emit('reject');
+ this.emit('reject', code || 500, msg);
};
View
8 package.json
@@ -15,21 +15,17 @@
"web",
"repository"
],
- "directories" : {
- "lib" : ".",
- "example" : "example",
- "test" : "test"
- },
"scripts" : {
"test" : "tap test/*.js"
},
"dependencies" : {
"http-duplex" : "~0.0.2",
+ "through" : "~2.2.7",
"inherits" : "~1.0.0",
"mkdirp" : "~0.3.4"
},
"devDependencies" : {
- "tap" : "~0.2.5",
+ "tap" : "~0.4.0",
"seq" : "~0.3.5"
},
"engines" : {
Please sign in to comment.
Something went wrong with that request. Please try again.