Skip to content

Commit dd20baf

Browse files
committed
0.5.0
- adds functionality for event streams and response streams
1 parent 21dc88e commit dd20baf

File tree

6 files changed

+989
-645
lines changed

6 files changed

+989
-645
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.5.0 (11/27/2018)
2+
3+
- adds `log` functionality for event streams and response streams
4+
15
# 0.4.3 (04/30/2018)
26

37
- removes deprecated `Buffer` interface

README.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,109 @@ npm install node-git-server
2020

2121
# Usage
2222

23+
## Simple
24+
25+
```javascript
26+
const path = require('path');
27+
const Server = require('node-git-server');
28+
29+
const repos = new Server(path.resolve(__dirname, 'tmp'), {
30+
autoCreate: true
31+
});
32+
const port = process.env.PORT || 7005;
33+
34+
repos.on('push', (push) => {
35+
console.log(`push ${push.repo}/${push.commit} (${push.branch})`);
36+
push.accept();
37+
});
38+
39+
repos.on('fetch', (fetch) => {
40+
console.log(`fetch ${fetch.commit}`);
41+
fetch.accept();
42+
});
43+
44+
repos.listen(port, () => {
45+
console.log(`node-git-server running at http://localhost:${port}`)
46+
});
47+
```
48+
49+
then start up the node-git-server server...
50+
51+
```
52+
$ node example/index.js
53+
```
54+
55+
meanwhile...
56+
57+
```
58+
$ git push http://localhost:7005/beep master
59+
Counting objects: 356, done.
60+
Delta compression using up to 2 threads.
61+
Compressing objects: 100% (133/133), done.
62+
Writing objects: 100% (356/356), 46.20 KiB, done.
63+
Total 356 (delta 210), reused 355 (delta 210)
64+
To http://localhost:7005/beep
65+
* [new branch] master -> master
66+
```
67+
68+
## Sending logs
69+
70+
```javascript
71+
const path = require('path');
72+
const Server = require('node-git-server');
73+
74+
const repos = new Server(path.resolve(__dirname, 'tmp'), {
75+
autoCreate: true
76+
});
77+
const port = process.env.PORT || 7005;
78+
79+
repos.on('push', (push) => {
80+
console.log(`push ${push.repo}/${push.commit} (${push.branch})`);
81+
82+
repos.list((err, results) => {
83+
push.log(' ');
84+
push.log('Hey!');
85+
push.log('Checkout these other repos:');
86+
for(const repo of results) {
87+
push.log(`- ${repo}`);
88+
}
89+
push.log(' ');
90+
});
91+
92+
push.accept();
93+
});
94+
95+
repos.listen(port, () => {
96+
console.log(`node-git-server running at http://localhost:${port}`)
97+
});
98+
```
99+
100+
then start up the node-git-server server...
101+
102+
```
103+
$ node example/index.js
104+
```
105+
106+
meanwhile...
107+
108+
```
109+
$ git push http://localhost:7005/beep master
110+
Counting objects: 356, done.
111+
Delta compression using up to 2 threads.
112+
Compressing objects: 100% (133/133), done.
113+
Writing objects: 100% (356/356), 46.20 KiB, done.
114+
Total 356 (delta 210), reused 355 (delta 210)
115+
remote:
116+
remote: Hey!
117+
remote: Checkout these other repos:
118+
remote: - test.git
119+
remote:
120+
To http://localhost:7005/test
121+
77bb26e..22918d5 master -> master
122+
```
123+
124+
### Authentication
125+
23126
```javascript
24127
const path = require('path');
25128
const Server = require('node-git-server');
@@ -73,7 +176,7 @@ To http://localhost:7005/beep
73176
* [new branch] master -> master
74177
```
75178

76-
## Example
179+
# Example
77180

78181
Running the following command will start up a simple http server:
79182

example/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@ const repos = new Server(path.normalize(path.resolve(__dirname, 'tmp')), {
3939

4040
repos.on('push', (push) => {
4141
console.log(`push ${push.repo} / ${push.commit} ( ${push.branch} )`); // eslint-disable-line
42-
repos.list((err, result) => {
43-
console.log(result); // eslint-disable-line
42+
43+
repos.list((err, results) => {
44+
push.log(' ');
45+
push.log('Hey!');
46+
push.log('Checkout these other repos:');
47+
for(const repo of results) {
48+
push.log(`- ${repo}`);
49+
}
50+
push.log(' ');
4451
});
52+
4553
push.accept();
4654
});
4755

@@ -53,8 +61,8 @@ repos.on('fetch', (fetch) => {
5361

5462
repos.listen(port, {
5563
type,
56-
key: fs.readFileSync('./privatekey.pem'),
57-
cert: fs.readFileSync('./certificate.pem')
64+
key: fs.readFileSync(path.resolve(__dirname, 'privatekey.pem')),
65+
cert: fs.readFileSync(path.resolve(__dirname, 'certificate.pem'))
5866
}, (error) => {
5967
if(error) return console.error(`failed to start git-server because of error ${error}`); // eslint-disable-line
6068
console.log(`node-git-server running at ${type}://localhost:${port}`); // eslint-disable-line

lib/service.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
const through = require('through');
2-
const HttpDuplex = require('./http-duplex');
32
const zlib = require('zlib');
4-
3+
const util = require('util');
54
const { spawn } = require('child_process');
65

6+
const HttpDuplex = require('./http-duplex');
7+
78
const headerRE = {
89
'receive-pack': '([0-9a-fA-F]+) ([0-9a-fA-F]+) refs\/(heads|tags)\/(.*?)( |00|\u0000)|^(0000)$', // eslint-disable-line
910
'upload-pack': '^\\S+ ([0-9a-fA-F]+)'
1011
};
1112

13+
const packSideband = s => {
14+
const n = (4 + s.length).toString(16);
15+
return Array(4 - n.length + 1).join('0') + n + s;
16+
};
17+
1218
class Service extends HttpDuplex {
1319
/**
1420
* Handles invoking the git-*-pack binaries
@@ -28,6 +34,7 @@ class Service extends HttpDuplex {
2834
this.repo = opts.repo;
2935
this.service = opts.service;
3036
this.cwd = opts.cwd;
37+
this.logs = [];
3138

3239
var buffered = through().pause();
3340

@@ -96,35 +103,59 @@ class Service extends HttpDuplex {
96103

97104
self.once('accept', function onAccept() {
98105
process.nextTick(function() {
99-
var cmd = ['git-' + opts.service, '--stateless-rpc', opts.cwd];
100-
var ps = spawn(cmd[0], cmd.slice(1));
106+
const cmd = ['git-' + opts.service, '--stateless-rpc', opts.cwd];
107+
const ps = spawn(cmd[0], cmd.slice(1));
108+
101109
ps.on('error', function(err) {
102110
self.emit('error', new Error(`${err.message} running command ${cmd.join(' ')}`));
103111
});
104112

105113
self.emit('service', ps);
106114

107-
var respStream = through(function(c) {
108-
if (self.listeners('response').length === 0) return this.queue(c);
115+
var respStream = through(function write(c) {
116+
if (self.listeners('response').length === 0) {
117+
if(self.logs.length > 0) {
118+
while(self.logs.length > 0) {
119+
this.queue(self.logs.pop());
120+
}
121+
}
122+
123+
return this.queue(c);
124+
}
109125
// prevent git from sending the close signal
110126
if (c.length === 4 && c.toString() === '0000') return;
111127
this.queue(c);
112-
}, function() {
128+
}, function end() {
113129
if (self.listeners('response').length > 0) return;
130+
114131
this.queue(null);
115132
});
116133

134+
respStream.log = function() {
135+
self.log(...arguments);
136+
};
117137

118138
self.emit('response', respStream, function endResponse() {
119139
res.queue(new Buffer.from('0000'));
120140
res.queue(null);
121141
});
142+
122143
ps.stdout.pipe(respStream).pipe(res);
123144

124145
buffered.pipe(ps.stdin);
125146
buffered.resume();
126147

127-
ps.on('exit', self.emit.bind(self, 'exit'));
148+
ps.on('exit', () => {
149+
if(self.logs.length > 0) {
150+
while(self.logs.length > 0) {
151+
respStream.queue(self.logs.pop());
152+
}
153+
respStream.queue(Buffer.from('0000'));
154+
respStream.queue(null);
155+
}
156+
157+
self.emit.bind(self, 'exit');
158+
});
128159
});
129160
});
130161

@@ -133,6 +164,15 @@ class Service extends HttpDuplex {
133164
res.end(msg);
134165
});
135166
}
167+
168+
log() {
169+
const _log = util.format(...arguments);
170+
const SIDEBAND = String.fromCharCode(2); // PROGRESS
171+
const message = `${SIDEBAND}${_log}\n`;
172+
const formattedMessage = Buffer.from(packSideband(message));
173+
174+
this.logs.unshift(formattedMessage);
175+
}
136176
/**
137177
* reject request in flight
138178
* @method reject

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-git-server",
3-
"version": "0.4.3",
3+
"version": "0.5.0",
44
"description": "🎡 A configurable git server written in Node.js",
55
"author": "Gabriel J. Csapo <[email protected]>",
66
"contributors": [

0 commit comments

Comments
 (0)