forked from gabrielcsapo/node-git-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js.html
More file actions
241 lines (202 loc) · 10.2 KB
/
util.js.html
File metadata and controls
241 lines (202 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>util.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Git.html">Git</a><ul class='methods'><li data-type='method'><a href="Git.html#.close">close</a></li><li data-type='method'><a href="Git.html#.create">create</a></li><li data-type='method'><a href="Git.html#.exists">exists</a></li><li data-type='method'><a href="Git.html#.handle">handle</a></li><li data-type='method'><a href="Git.html#.list">list</a></li><li data-type='method'><a href="Git.html#.listen">listen</a></li><li data-type='method'><a href="Git.html#.mkdir">mkdir</a></li></ul></li><li><a href="HttpDuplex.html">HttpDuplex</a><ul class='methods'><li data-type='method'><a href="HttpDuplex.html#addTrailers">addTrailers</a></li><li data-type='method'><a href="HttpDuplex.html#cork">cork</a></li><li data-type='method'><a href="HttpDuplex.html#destroy">destroy</a></li><li data-type='method'><a href="HttpDuplex.html#end">end</a></li><li data-type='method'><a href="HttpDuplex.html#getHeader">getHeader</a></li><li data-type='method'><a href="HttpDuplex.html#pause">pause</a></li><li data-type='method'><a href="HttpDuplex.html#removeHeader">removeHeader</a></li><li data-type='method'><a href="HttpDuplex.html#resume">resume</a></li><li data-type='method'><a href="HttpDuplex.html#setDefaultEncoding">setDefaultEncoding</a></li><li data-type='method'><a href="HttpDuplex.html#setEncoding">setEncoding</a></li><li data-type='method'><a href="HttpDuplex.html#setHeader">setHeader</a></li><li data-type='method'><a href="HttpDuplex.html#uncork">uncork</a></li><li data-type='method'><a href="HttpDuplex.html#write">write</a></li><li data-type='method'><a href="HttpDuplex.html#writeContinue">writeContinue</a></li><li data-type='method'><a href="HttpDuplex.html#writeHead">writeHead</a></li><li data-type='method'><a href="HttpDuplex.html#writeHeader">writeHeader</a></li></ul></li><li><a href="Service.html">Service</a><ul class='methods'><li data-type='method'><a href="Service.html#.accept">accept</a></li><li data-type='method'><a href="Service.html#.reject">reject</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-lib_util.html">lib/util</a><ul class='methods'><li data-type='method'><a href="module-lib_util.html#~basicAuth">basicAuth</a></li><li data-type='method'><a href="module-lib_util.html#~createAction">createAction</a></li><li data-type='method'><a href="module-lib_util.html#~infoResponse">infoResponse</a></li><li data-type='method'><a href="module-lib_util.html#~noCache">noCache</a></li><li data-type='method'><a href="module-lib_util.html#~onExit">onExit</a></li><li data-type='method'><a href="module-lib_util.html#~parseGitName">parseGitName</a></li><li data-type='method'><a href="module-lib_util.html#~serviceRespond">serviceRespond</a></li></ul></li></ul><h3>Events</h3><ul><li><a href="HttpDuplex.html#event:close">close</a></li><li><a href="HttpDuplex.html#event:data">data</a></li><li><a href="HttpDuplex.html#event:drain">drain</a></li><li><a href="HttpDuplex.html#event:end">end</a></li><li><a href="HttpDuplex.html#event:error">error</a></li><li><a href="Git.html#event:fetch">fetch</a></li><li><a href="Git.html#event:head">head</a></li><li><a href="Git.html#event:info">info</a></li><li><a href="Git.html#event:push">push</a></li><li><a href="Git.html#event:tag">tag</a></li></ul><h3>Global</h3><ul><li><a href="global.html#getType">getType</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">util.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @module lib/util
*/
const { spawn } = require('child_process');
const httpDuplex = require('./http-duplex');
const Service = require('./service');
const Util = {
/**
* adds headers to the response object to add cache control
* @method noCache
* @param {http.ServerResponse} res - http response
*/
noCache: 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');
},
/**
* sets and parses basic auth headers if they exist
* @method basicAuth
* @param {http.IncomingMessage } req - http request object
* @param {http.ServerResponse} res - http response
* @param {Function} callback - function(username, password, error)
*/
basicAuth: function basicAuth(req, res, callback) {
if(!req.headers["authorization"]) {
res.setHeader("Content-Type", 'text/plain');
res.setHeader("WWW-Authenticate", 'Basic realm="authorization needed"');
res.writeHead(401);
res.end('401 Unauthorized');
} else {
const tokens = req.headers["authorization"].split(" ");
if (tokens[0] === "Basic") {
const splitHash = new Buffer(tokens[1], 'base64').toString('utf8').split(":");
const username = splitHash.shift();
const password = splitHash.join(":");
callback(username, password, null);
}
}
},
/**
* returns when process has fully exited
* @method onExit
* @param {EventEmitter} ps - event emitter to listen to
* @param {Function} callback - function(code, signature)
*/
onExit: function onExit(ps, callback) {
let code;
let sig;
let pending = 3;
const onend = () => {
if (--pending === 0) {
callback(code, sig);
}
};
ps.on('exit', (c, s) => {
code = c;
sig = s;
});
ps.on('exit', onend);
ps.stdout.on('end', onend);
ps.stderr.on('end', onend);
},
/**
* execute given git operation and respond
* @method serviceRespond
* @param {HttpDuplex} dup - duplex object to catch errors
* @param {String} service - the method that is responding infoResponse (push, pull, clone)
* @param {String} repoLocation - the repo path on disk
* @param {http.ServerResponse} res - http response
*/
serviceRespond: function serviceRespond(dup, service, repoLocation, res) {
const pack = (s) => {
var n = (4 + s.length).toString(16);
return Array(4 - n.length + 1).join('0') + n + s;
};
res.write(pack('# service=git-' + service + '\n'));
res.write('0000');
const cmd = ['git-' + service, '--stateless-rpc', '--advertise-refs', repoLocation];
const ps = spawn(cmd[0], cmd.slice(1));
ps.on('error', (err) => {
dup.emit('error', new Error(`${err.message} running command ${cmd.join(' ')}`));
});
ps.stdout.pipe(res);
},
/**
* sends http response using the appropriate output from service call
* @method infoResponse
* @param {Git} git - an instance of git object
* @param {String} repo - the repository
* @param {String} service - the method that is responding infoResponse (push, pull, clone)
* @param {http.IncomingMessage } req - http request object
* @param {http.ServerResponse} res - http response
*/
infoResponse: function infoResponse(git, repo, service, req, res) {
var dup = new httpDuplex(req, res);
dup.cwd = git.dirMap(repo);
dup.repo = repo;
dup.accept = dup.emit.bind(dup, 'accept');
dup.reject = dup.emit.bind(dup, 'reject');
dup.once('reject', (code) => {
res.statusCode = code || 500;
res.end();
});
var anyListeners = git.listeners('info').length > 0;
git.exists(repo, (ex) => {
dup.exists = ex;
if (!ex && git.autoCreate) {
dup.once('accept', () => {
git.create(repo, next);
});
git.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.once('accept', next);
git.emit('info', dup);
if (!anyListeners) dup.accept();
}
});
function next() {
res.setHeader(
'content-type',
'application/x-git-' + service + '-advertisement'
);
Util.noCache(res);
Util.serviceRespond(
git,
service,
git.dirMap(repo),
res
);
}
},
/**
* parses a git string and returns the repo name
* @method parseGitName
* @param {String} repo - the raw repo name containing .git
* @return {String} returns the name of the repo
*/
parseGitName: function parseGitName(repo) {
const locationOfGit = repo.lastIndexOf('.git');
return repo.substr(0, locationOfGit > 0 ? locationOfGit : repo.length);
},
/**
* responds with the correct service depending on the action
* @method createAction
* @param {Object} opts - options to pass Service
* @param {http.IncomingMessage } req - http request object
* @param {http.ServerResponse} res - http response
* @return {Service}
*/
createAction: function createAction(opts, req, res) {
let service = new Service(opts, req, res);
Object.keys(opts).forEach((key) => {
service[key] = opts[key];
});
return service;
}
};
module.exports = Util;
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>