Permalink
Browse files

info events, first half

  • Loading branch information...
1 parent 96a737f commit 07026123efa719dcdac35bdb47acca2899dec1b0 @substack committed Sep 23, 2012
Showing with 58 additions and 25 deletions.
  1. +57 −24 lib/handle.js
  2. +1 −1 package.json
View
@@ -5,8 +5,10 @@ var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var services = [ 'upload-pack', 'receive-pack' ]
+
var createAction = require('./service');
var onexit = require('./onexit');
+var noCache = require('./no_cache');
module.exports = function (req, res) {
var self = this;
@@ -27,8 +29,8 @@ handlers.push(function (req, res) {
var self = this;
var repo = m[1];
-
var params = qs.parse(u.query);
+
if (!params.service) {
res.statusCode = 400;
res.end('service parameter required');
@@ -42,23 +44,11 @@ handlers.push(function (req, res) {
return;
}
- var next = function () {
- res.setHeader('content-type',
- 'application/x-git-' + service + '-advertisement'
- );
- noCache(res);
- serviceRespond(service, path.join(self.repoDir, repo), res);
- };
-
- self.exists(repo, function (ex) {
- if (!ex && self.autoCreate) self.create(repo, next)
- else if (!ex) {
- res.statusCode = 404;
- res.setHeader('content-type', 'text/plain');
- res.end('repository not found');
- }
- else next()
- });
+ infoResponse({
+ repos : self,
+ repo : repo,
+ service : service,
+ }, req, res);
});
handlers.push(function (req, res) {
@@ -139,12 +129,6 @@ handlers.push(function (req, res) {
res.end('not found');
});
-function noCache (res) {
- res.setHeader('expires', 'Fri, 01 Jan 1980 00:00:00 GMT');
- res.setHeader('pragma', 'no-cache');
- res.setHeader('cache-control', 'no-cache, max-age=0, must-revalidate');
-}
-
function serviceRespond (service, file, res) {
function pack (s) {
var n = (4 + s.length).toString(16);
@@ -163,3 +147,52 @@ function serviceRespond (service, file, res) {
onexit(ps, function () { res.end() });
}
+
+var httpDuplex = require('http-duplex');
+
+function infoResponse (opts, req, res) {
+ var self = opts.repos;
+ var dup = httpDuplex(req, res);
+
+ dup.accept = function () { dup.emit('accept') };
+ dup.reject = function (code) { dup.emit('reject', code) };
+
+ dup.once('reject', function (code) {
+ res.statusCode = code || 500;
+ res.end();
+ });
+
+ self.exists(opts.repo, function (ex) {
+ var anyListeners = self.listeners('info').length > 0;
+
+ if (!ex && self.autoCreate) {
+ dup.exists = ex;
+
+ dup.on('accept', function () {
+ self.create(opts.repo, next)
+ });
+
+ self.emit('info', dup);
+ if (!anyListeners) dup.accept();
+ }
+ else if (!ex) {
+ res.statusCode = 404;
+ res.setHeader('content-type', 'text/plain');
+ res.end('repository not found');
+ }
+ else {
+ dup.on('accept', next);
+ self.emit('info', dup);
+
+ if (!anyListeners) dup.accept();
+ }
+ });
+
+ function next () {
+ res.setHeader('content-type',
+ 'application/x-git-' + opts.service + '-advertisement'
+ );
+ noCache(res);
+ serviceRespond(opts.service, path.join(self.repoDir, opts.repo), res);
+ }
+}
View
@@ -1,7 +1,7 @@
{
"name" : "pushover",
"description" : "git push deploy server over http",
- "version" : "0.1.6",
+ "version" : "1.0.0",
"repository" : {
"type" : "git",
"url" : "git://github.com/substack/pushover.git"

0 comments on commit 0702612

Please sign in to comment.