Skip to content

Commit e0bfae6

Browse files
authored
[feature] adds the ability to introspect on the header for the authenticate function (#52)
1 parent 70ad208 commit e0bfae6

13 files changed

+336
-4253
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 0.6.0 (03/03/2019)
2+
3+
- Augments the authenticate function declaration to accept an object as the first argument and a callback for the second. This allows us to make changes without having to cause breaking changes.
4+
- Adds the ability to introspect on the header (fixes #49)
5+
16
# 0.5.1 (03/03/2019)
27

38
- bump dependencies

docs/code/Git.html

+75-6
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ <h6>Properties</h6>
271271

272272

273273

274-
<td class="description last"><p>a function that has the following arguments (repo, username, password, next) and will be called when a request comes through if set</p>
275-
<pre class="prettyprint source"><code> authenticate: (type, repo, username, password, next) => {
274+
<td class="description last"><p>a function that has the following arguments ({ type, repo, username, password, headers }, next) and will be called when a request comes through if set</p>
275+
<pre class="prettyprint source"><code> authenticate: ({ type, repo, username, password, headers }, next) => {
276276
console.log(type, repo, username, password);
277277
next();
278278
}
279279
// alternatively you can also pass authenticate a promise
280-
authenticate: (type, repo, username, password, next) => {
280+
authenticate: ({ type, repo, username, password, headers }, next) => {
281281
console.log(type, repo, username, password);
282282
return new Promise((resolve, reject) => {
283283
if(username === 'foo') {
@@ -393,7 +393,7 @@ <h4 class="name" id=".close"><span class="type-signature">(static) </span>close<
393393

394394
<dt class="tag-source">Source:</dt>
395395
<dd class="tag-source"><ul class="dummy"><li>
396-
<a href="git.js.html">git.js</a>, <a href="git.js.html#line451">line 451</a>
396+
<a href="git.js.html">git.js</a>, <a href="git.js.html#line456">line 456</a>
397397
</li></ul></dd>
398398

399399

@@ -444,6 +444,51 @@ <h4 class="name" id=".close"><span class="type-signature">(static) </span>close<
444444

445445

446446

447+
448+
449+
<h5>Parameters:</h5>
450+
451+
452+
<table class="params">
453+
<thead>
454+
<tr>
455+
456+
457+
<th>Type</th>
458+
459+
460+
461+
462+
463+
<th class="last">Description</th>
464+
</tr>
465+
</thead>
466+
467+
<tbody>
468+
469+
470+
<tr>
471+
472+
473+
<td class="type">
474+
475+
476+
<span class="param-type">Promise</span>
477+
478+
479+
480+
</td>
481+
482+
483+
484+
485+
486+
<td class="description last"><p>will resolve or reject when the server closes or fails to close.</p></td>
487+
</tr>
488+
489+
490+
</tbody>
491+
</table>
447492

448493

449494

@@ -1106,7 +1151,7 @@ <h5>Parameters:</h5>
11061151

11071152

11081153

1109-
<h4 class="name" id=".listen"><span class="type-signature">(static) </span>listen<span class="signature">(port, options<span class="signature-attributes">opt</span>, callback)</span><span class="type-signature"></span></h4>
1154+
<h4 class="name" id=".listen"><span class="type-signature">(static) </span>listen<span class="signature">(port, options<span class="signature-attributes">opt</span>, callback)</span><span class="type-signature"> &rarr; {<a href="Git.html">Git</a>}</span></h4>
11101155

11111156

11121157

@@ -1118,7 +1163,7 @@ <h4 class="name" id=".listen"><span class="type-signature">(static) </span>liste
11181163

11191164
<dt class="tag-source">Source:</dt>
11201165
<dd class="tag-source"><ul class="dummy"><li>
1121-
<a href="git.js.html">git.js</a>, <a href="git.js.html#line426">line 426</a>
1166+
<a href="git.js.html">git.js</a>, <a href="git.js.html#line429">line 429</a>
11221167
</li></ul></dd>
11231168

11241169

@@ -1435,6 +1480,30 @@ <h6>Properties</h6>
14351480

14361481

14371482

1483+
<h5>Returns:</h5>
1484+
1485+
1486+
<div class="param-desc">
1487+
<ul>
1488+
<li>the Git instance, useful for chaining</li>
1489+
</ul>
1490+
</div>
1491+
1492+
1493+
1494+
<dl class="param-type">
1495+
<dt>
1496+
Type
1497+
</dt>
1498+
<dd>
1499+
1500+
<span class="param-type"><a href="Git.html">Git</a></span>
1501+
1502+
1503+
</dd>
1504+
</dl>
1505+
1506+
14381507

14391508

14401509

docs/code/Service.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ <h4 class="name" id="Service"><span class="type-signature"></span>new Service<sp
6565

6666
<dt class="tag-source">Source:</dt>
6767
<dd class="tag-source"><ul class="dummy"><li>
68-
<a href="service.js.html">service.js</a>, <a href="service.js.html#line13">line 13</a>
68+
<a href="service.js.html">service.js</a>, <a href="service.js.html#line20">line 20</a>
6969
</li></ul></dd>
7070

7171

@@ -1253,7 +1253,7 @@ <h4 class="name" id=".accept"><span class="type-signature">(static) </span>accep
12531253

12541254
<dt class="tag-source">Source:</dt>
12551255
<dd class="tag-source"><ul class="dummy"><li>
1256-
<a href="service.js.html">service.js</a>, <a href="service.js.html#line153">line 153</a>
1256+
<a href="service.js.html">service.js</a>, <a href="service.js.html#line198">line 198</a>
12571257
</li></ul></dd>
12581258

12591259

@@ -1337,7 +1337,7 @@ <h4 class="name" id=".reject"><span class="type-signature">(static) </span>rejec
13371337

13381338
<dt class="tag-source">Source:</dt>
13391339
<dd class="tag-source"><ul class="dummy"><li>
1340-
<a href="service.js.html">service.js</a>, <a href="service.js.html#line136">line 136</a>
1340+
<a href="service.js.html">service.js</a>, <a href="service.js.html#line181">line 181</a>
13411341
</li></ul></dd>
13421342

13431343

docs/code/git.js.html

+15-5
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ <h1 class="page-title">git.js</h1>
157157
* @param {Object} options - options that can be applied on the new instance being created
158158
* @param {Boolean=} options.autoCreate - By default, repository targets will be created if they don't exist. You can
159159
disable that behavior with `options.autoCreate = true`
160-
* @param {Function} options.authenticate - a function that has the following arguments (repo, username, password, next) and will be called when a request comes through if set
160+
* @param {Function} options.authenticate - a function that has the following arguments ({ type, repo, username, password, headers }, next) and will be called when a request comes through if set
161161
*
162-
authenticate: (type, repo, username, password, next) => {
162+
authenticate: ({ type, repo, username, password, headers }, next) => {
163163
console.log(type, repo, username, password);
164164
next();
165165
}
166166
// alternatively you can also pass authenticate a promise
167-
authenticate: (type, repo, username, password, next) => {
167+
authenticate: ({ type, repo, username, password, headers }, next) => {
168168
console.log(type, repo, username, password);
169169
return new Promise((resolve, reject) => {
170170
if(username === 'foo') {
@@ -341,9 +341,12 @@ <h1 class="page-title">git.js</h1>
341341
// check if the repo is authenticated
342342
if(this.authenticate) {
343343
const type = this.getType(service);
344-
const promise = this.authenticate(type, repoName, basicAuth.bind(null, req, res), (error) => {
344+
const headers = req.headers;
345+
const user = basicAuth.bind(null, req, res);
346+
const promise = this.authenticate({ type, repo: repoName, user, headers }, (error) => {
345347
return next(error);
346348
});
349+
347350
if(promise instanceof Promise) {
348351
return promise
349352
.then(next)
@@ -472,6 +475,7 @@ <h1 class="page-title">git.js</h1>
472475
* @param {Buffer|String=} options.key - the key file for the https server
473476
* @param {Buffer|String=} options.cert - the cert file for the https server
474477
* @param {Function} callback - the function to call when server is started or error has occured
478+
* @return {Git} - the Git instance, useful for chaining
475479
*/
476480
listen(port, options, callback) {
477481
const self = this;
@@ -486,14 +490,20 @@ <h1 class="page-title">git.js</h1>
486490
});
487491

488492
this.server.listen(port, callback);
493+
return this;
489494
}
490495
/**
491496
* closes the server instance
492497
* @method close
493498
* @memberof Git
499+
* @param {Promise} - will resolve or reject when the server closes or fails to close.
494500
*/
495501
close() {
496-
this.server.close();
502+
return new Promise((resolve, reject) => {
503+
this.server.close((err) => {
504+
err ? reject(err) : resolve();
505+
});
506+
});
497507
}
498508
}
499509

docs/code/index.html

+73-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Git.html"
4848
<section class="readme">
4949
<article><h1>node-git-server</h1><blockquote>
5050
<p>🎡 A configurable git server written in Node.js</p>
51+
</blockquote>
52+
<blockquote>
5153
<blockquote>
5254
<p>there be 🐲 here! The API's and functionality are still be cemented, anything before a 1.0.0 release will be subject to change.</p>
5355
</blockquote>
@@ -59,7 +61,76 @@ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Git.html"
5961
<a href="https://starbuck.gabrielcsapo.com/github/gabrielcsapo/node-git-server#info=devDependencies"><img src="https://starbuck.gabrielcsapo.com/badge/github/gabrielcsapo/node-git-server/dev-status.svg" alt="devDependency Status"></a>
6062
<a href=""><img src="https://img.shields.io/npm/dt/node-git-server.svg" alt="npm"></a>
6163
<a href=""><img src="https://img.shields.io/npm/dm/node-git-server.svg" alt="npm"></a></p>
62-
<h1>Install</h1><pre class="prettyprint source"><code>npm install node-git-server</code></pre><h1>Usage</h1><pre class="prettyprint source lang-javascript"><code>const path = require('path');
64+
<h1>Install</h1><pre class="prettyprint source"><code>npm install node-git-server</code></pre><h1>Usage</h1><h2>Simple</h2><pre class="prettyprint source lang-javascript"><code>const path = require('path');
65+
const Server = require('node-git-server');
66+
67+
const repos = new Server(path.resolve(__dirname, 'tmp'), {
68+
autoCreate: true
69+
});
70+
const port = process.env.PORT || 7005;
71+
72+
repos.on('push', (push) => {
73+
console.log(`push ${push.repo}/${push.commit} (${push.branch})`);
74+
push.accept();
75+
});
76+
77+
repos.on('fetch', (fetch) => {
78+
console.log(`fetch ${fetch.commit}`);
79+
fetch.accept();
80+
});
81+
82+
repos.listen(port, () => {
83+
console.log(`node-git-server running at http://localhost:${port}`)
84+
});</code></pre><p>then start up the node-git-server server...</p>
85+
<pre class="prettyprint source"><code>$ node example/index.js</code></pre><p>meanwhile...</p>
86+
<pre class="prettyprint source"><code>$ git push http://localhost:7005/beep master
87+
Counting objects: 356, done.
88+
Delta compression using up to 2 threads.
89+
Compressing objects: 100% (133/133), done.
90+
Writing objects: 100% (356/356), 46.20 KiB, done.
91+
Total 356 (delta 210), reused 355 (delta 210)
92+
To http://localhost:7005/beep
93+
* [new branch] master -> master</code></pre><h2>Sending logs</h2><pre class="prettyprint source lang-javascript"><code>const path = require('path');
94+
const Server = require('node-git-server');
95+
96+
const repos = new Server(path.resolve(__dirname, 'tmp'), {
97+
autoCreate: true
98+
});
99+
const port = process.env.PORT || 7005;
100+
101+
repos.on('push', (push) => {
102+
console.log(`push ${push.repo}/${push.commit} (${push.branch})`);
103+
104+
repos.list((err, results) => {
105+
push.log(' ');
106+
push.log('Hey!');
107+
push.log('Checkout these other repos:');
108+
for(const repo of results) {
109+
push.log(`- ${repo}`);
110+
}
111+
push.log(' ');
112+
});
113+
114+
push.accept();
115+
});
116+
117+
repos.listen(port, () => {
118+
console.log(`node-git-server running at http://localhost:${port}`)
119+
});</code></pre><p>then start up the node-git-server server...</p>
120+
<pre class="prettyprint source"><code>$ node example/index.js</code></pre><p>meanwhile...</p>
121+
<pre class="prettyprint source"><code>$ git push http://localhost:7005/beep master
122+
Counting objects: 356, done.
123+
Delta compression using up to 2 threads.
124+
Compressing objects: 100% (133/133), done.
125+
Writing objects: 100% (356/356), 46.20 KiB, done.
126+
Total 356 (delta 210), reused 355 (delta 210)
127+
remote:
128+
remote: Hey!
129+
remote: Checkout these other repos:
130+
remote: - test.git
131+
remote:
132+
To http://localhost:7005/test
133+
77bb26e..22918d5 master -> master</code></pre><h3>Authentication</h3><pre class="prettyprint source lang-javascript"><code>const path = require('path');
63134
const Server = require('node-git-server');
64135

65136
const repos = new Server(path.resolve(__dirname, 'tmp'), {
@@ -98,7 +169,7 @@ <h1>Install</h1><pre class="prettyprint source"><code>npm install node-git-serve
98169
Writing objects: 100% (356/356), 46.20 KiB, done.
99170
Total 356 (delta 210), reused 355 (delta 210)
100171
To http://localhost:7005/beep
101-
* [new branch] master -> master</code></pre><h2>Example</h2><p>Running the following command will start up a simple http server:</p>
172+
* [new branch] master -> master</code></pre><h1>Example</h1><p>Running the following command will start up a simple http server:</p>
102173
<pre class="prettyprint source"><code>node example/index.js</code></pre><p>If you want to try using https run the following</p>
103174
<pre class="prettyprint source"><code>node example/index.js --https</code></pre><blockquote>
104175
<p>When running https with self-signed certs there are two ways to override the git-clients behavior using <code>git config http.sslVerify false</code> or <code>git config --global http.sslCAInfo /path/to/cert.pem</code></p>

docs/code/module-lib_util.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ <h4 class="name" id="~createAction"><span class="type-signature">(inner) </span>
263263

264264
<dt class="tag-source">Source:</dt>
265265
<dd class="tag-source"><ul class="dummy"><li>
266-
<a href="util.js.html">util.js</a>, <a href="util.js.html#line164">line 164</a>
266+
<a href="util.js.html">util.js</a>, <a href="util.js.html#line171">line 171</a>
267267
</li></ul></dd>
268268

269269

@@ -460,7 +460,7 @@ <h4 class="name" id="~infoResponse"><span class="type-signature">(inner) </span>
460460

461461
<dt class="tag-source">Source:</dt>
462462
<dd class="tag-source"><ul class="dummy"><li>
463-
<a href="util.js.html">util.js</a>, <a href="util.js.html#line94">line 94</a>
463+
<a href="util.js.html">util.js</a>, <a href="util.js.html#line101">line 101</a>
464464
</li></ul></dd>
465465

466466

@@ -974,7 +974,7 @@ <h4 class="name" id="~parseGitName"><span class="type-signature">(inner) </span>
974974

975975
<dt class="tag-source">Source:</dt>
976976
<dd class="tag-source"><ul class="dummy"><li>
977-
<a href="util.js.html">util.js</a>, <a href="util.js.html#line154">line 154</a>
977+
<a href="util.js.html">util.js</a>, <a href="util.js.html#line161">line 161</a>
978978
</li></ul></dd>
979979

980980

0 commit comments

Comments
 (0)