Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 1x 14x 13x 82x 82x 82x 13x 13x 14x 14x 3x 3x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 32x 32x 1x 1x 1x 1x 1x 1x 12x 12x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 11x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 11x 11x 17x 17x 17x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 18x 18x 29x 29x 29x 29x 18x 18x 29x 29x 18x 18x 18x 29x 18x 18x 18x 15x 15x 15x 15x 18x 18x 18x 29x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 6x 6x 6x 2x 2x 29x 12x 12x 29x 29x 11x 11x 11x 11x 11x 29x 29x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 13x 13x 13x 13x 13x 13x 11x 29x 29x 29x 29x 29x 29x 29x 29x 29x 51x 51x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 15x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 5x 5x 4x 5x 5x 5x 1x | import fs from 'fs';
import path from 'path';
import http, { ServerOptions } from 'http';
import https from 'https';
import url from 'url';
import qs from 'querystring';
import { HttpDuplex } from './http-duplex';
import { spawn } from 'child_process';
import { EventEmitter } from 'events';
import {
parseGitName,
createAction,
infoResponse,
basicAuth,
noCache,
} from './util';
import { ServiceString } from './types';
const services = ['upload-pack', 'receive-pack'];
interface GitServerOptions extends ServerOptions {
type: 'http' | 'https';
}
export interface GitOptions {
autoCreate?: boolean;
authenticate?: (
options: GitAuthenticateOptions,
callback: (error?: Error) => void | undefined
) => void | Promise<Error | undefined | void> | undefined;
checkout?: boolean;
}
export interface GitAuthenticateOptions {
type: string;
repo: string;
user: (() => Promise<[string | undefined, string | undefined]>) &
((
callback: (
username?: string | undefined,
password?: string | undefined
) => void
) => void);
headers: http.IncomingHttpHeaders;
}
/**
* An http duplex object (see below) with these extra properties:
*/
export interface TagData extends HttpDuplex {
repo: string; // The string that defines the repo
commit: string; // The string that defines the commit sha
version: string; // The string that defines the tag being pushed
}
/**
* Is a http duplex object (see below) with these extra properties
*/
export interface PushData extends HttpDuplex {
repo: string; // The string that defines the repo
commit: string; // The string that defines the commit sha
branch: string; // The string that defines the branch
}
/**
* an http duplex object (see below) with these extra properties
*/
export interface FetchData extends HttpDuplex {
repo: string; // The string that defines the repo
commit: string; // The string that defines the commit sha
}
/**
* an http duplex object (see below) with these extra properties
*/
export interface InfoData extends HttpDuplex {
repo: string; // The string that defines the repo
}
/**
* an http duplex object (see below) with these extra properties
*/
export interface HeadData extends HttpDuplex {
repo: string; // The string that defines the repo
}
export interface GitEvents {
/**
* @example
* repos.on('push', function (push) { ... }
*
* Emitted when somebody does a `git push` to the repo.
*
* Exactly one listener must call `push.accept()` or `push.reject()`. If there are
* no listeners, `push.accept()` is called automatically.
**/
on(event: 'push', listener: (push: PushData) => void): this;
/**
* @example
* repos.on('tag', function (tag) { ... }
*
* Emitted when somebody does a `git push --tags` to the repo.
* Exactly one listener must call `tag.accept()` or `tag.reject()`. If there are
* No listeners, `tag.accept()` is called automatically.
**/
on(event: 'tag', listener: (tag: TagData) => void): this;
/**
* @example
* repos.on('fetch', function (fetch) { ... }
*
* Emitted when somebody does a `git fetch` to the repo (which happens whenever you
* do a `git pull` or a `git clone`).
*
* Exactly one listener must call `fetch.accept()` or `fetch.reject()`. If there are
* no listeners, `fetch.accept()` is called automatically.
**/
on(event: 'fetch', listener: (fetch: FetchData) => void): this;
/**
* @example
* repos.on('info', function (info) { ... }
*
* Emitted when the repo is queried for info before doing other commands.
*
* Exactly one listener must call `info.accept()` or `info.reject()`. If there are
* no listeners, `info.accept()` is called automatically.
**/
on(event: 'info', listener: (info: InfoData) => void): this;
/**
* @example
* repos.on('head', function (head) { ... }
*
* Emitted when the repo is queried for HEAD before doing other commands.
*
* Exactly one listener must call `head.accept()` or `head.reject()`. If there are
* no listeners, `head.accept()` is called automatically.
*
**/
on(event: 'head', listener: (head: HeadData) => void): this;
}
export class Git extends EventEmitter implements GitEvents {
dirMap: (dir?: string) => string;
authenticate:
| ((
options: GitAuthenticateOptions,
callback: (error?: Error) => void | undefined
) => void | Promise<Error | undefined | void> | undefined)
| undefined;
autoCreate: boolean;
checkout: boolean | undefined;
server: https.Server | http.Server | undefined;
/**
*
* Handles invoking the git-*-pack binaries
* @param repoDir - Create a new repository collection from the directory `repoDir`. `repoDir` should be entirely empty except for git repo directories. If `repoDir` is a function, `repoDir(repo)` will be used to dynamically resolve project directories. The return value of `repoDir(repo)` should be a string path specifying where to put the string `repo`. Make sure to return the same value for `repo` every time since `repoDir(repo)` will be called multiple times.
* @param options - options that can be applied on the new instance being created
* @param options.autoCreate - By default, repository targets will be created if they don't exist. You can
disable that behavior with `options.autoCreate = true`
* @param 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
*
authenticate: ({ type, repo, username, password, headers }, next) => {
console.log(type, repo, username, password);
next();
}
// alternatively you can also pass authenticate a promise
authenticate: ({ type, repo, username, password, headers }, next) => {
console.log(type, repo, username, password);
return new Promise((resolve, reject) => {
if(username === 'foo') {
return resolve();
}
return reject("sorry you don't have access to this content");
});
}
* @param options.checkout - If `opts.checkout` is true, create and expected checked-out repos instead of bare repos
*/
constructor(
repoDir: string | ((dir?: string) => string),
options: GitOptions = {}
) {
super();
if (typeof repoDir === 'function') {
this.dirMap = repoDir;
} else {
this.dirMap = (dir?: string): string => {
return path.normalize(
(dir ? path.join(repoDir, dir) : repoDir) as string
);
};
}
if (options.authenticate) {
this.authenticate = options.authenticate;
}
this.autoCreate = options.autoCreate === false ? false : true;
this.checkout = options.checkout;
}
/**
* Get a list of all the repositories
* @param {Function} callback function to be called when repositories have been found `function(error, repos)`
*/
list(callback: (error: Error | undefined, repos?: string[]) => void): void;
list(): Promise<string[]>;
list(
callback?: (error: Error | undefined, repos?: string[]) => void
): Promise<string[]> | void {
const execf = (res: (repos: string[]) => void, rej: (err: Error) => void) =>
fs.readdir(this.dirMap(), (error, results) => {
if (error) return rej(error);
const repos = results.filter((r) => {
return r.substring(r.length - 3, r.length) == 'git';
}, []);
res(repos);
});
if (callback)
return execf(
(repos) => callback(void 0, repos),
(err) => callback(err, void 0)
);
else return new Promise<string[]>((res, rej) => execf(res, rej));
}
/**
* Find out whether `repoName` exists in the callback `cb(exists)`.
* @param repo - name of the repo
* @param callback - function to be called when finished
*/
exists(repo: string): boolean {
return fs.existsSync(this.dirMap(repo));
}
/**
* Create a subdirectory `dir` in the repo dir with a callback.
* @param dir - directory name
* @param callback - callback to be called when finished
*/
mkdir(dir: string) {
fs.mkdirSync(path.dirname(dir), { recursive: true });
}
/**
* Create a new bare repository `repoName` in the instance repository directory.
* @param repo - the name of the repo
* @param callback - Optionally get a callback `cb(err)` to be notified when the repository was created.
*/
create(repo: string, callback: (error?: Error) => void) {
function next(self: Git) {
let ps;
let _error = '';
const dir = self.dirMap(repo);
if (self.checkout) {
ps = spawn('git', ['init', dir]);
} else {
ps = spawn('git', ['init', '--bare', dir]);
}
ps.stderr.on('data', function (chunk: string) {
_error += chunk;
});
ps.on('exit', (code) => {
if (!callback) {
return;
} else if (code) {
callback(new Error(_error));
} else {
callback();
}
});
}
if (typeof callback !== 'function')
callback = () => {
return;
};
if (!/\.git$/.test(repo)) repo += '.git';
const exists = this.exists(repo);
if (!exists) {
this.mkdir(repo);
}
next(this);
}
/**
* returns the typeof service being process. This will respond with either fetch or push.
* @param service - the service type
*/
getType(service: string): string {
switch (service) {
case 'upload-pack':
return 'fetch';
case 'receive-pack':
return 'push';
default:
return 'unknown';
}
}
/**
* Handle incoming HTTP requests with a connect-style middleware
* @param http request object
* @param http response object
*/
handle(req: http.IncomingMessage, res: http.ServerResponse) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const handlers = [
(req: http.IncomingMessage, res: http.ServerResponse) => {
if (req.method !== 'GET') return false;
// eslint-disable-next-line @typescript-eslint/no-this-alias
const u = url.parse(req?.url || '');
const m = u.pathname?.match(/\/(.+)\/info\/refs$/);
if (!m) return false;
if (/\.\./.test(m[1])) return false;
const repo = m[1];
const params = qs.parse(u?.query || '');
if (!params.service || typeof params.service !== 'string') {
res.statusCode = 400;
res.end('service parameter required');
return;
}
const service = params.service.replace(/^git-/, '');
if (services.indexOf(service) < 0) {
res.statusCode = 405;
res.end('service not available');
return;
}
const repoName = parseGitName(m[1]);
const next = (error?: Error | void) => {
if (error) {
res.setHeader('Content-Type', 'text/plain');
res.setHeader(
'WWW-Authenticate',
'Basic realm="authorization needed"'
);
res.writeHead(401);
res.end(typeof error === 'string' ? error : error.toString());
return;
} else {
return infoResponse(this, repo, service as ServiceString, req, res);
}
};
// check if the repo is authenticated
if (this.authenticate) {
const type = this.getType(service);
const headers = req.headers;
const user = (
callback?: (username?: string, password?: string) => void
) =>
callback
? basicAuth(req, res, callback)
: new Promise<[string | undefined, string | undefined]>(
(resolve) => basicAuth(req, res, (u, p) => resolve([u, p]))
);
const promise = this.authenticate(
{
type,
repo: repoName,
user: user as unknown as GitAuthenticateOptions['user'],
headers,
},
(error?: Error) => {
return next(error);
}
);
if (promise instanceof Promise) {
return promise.then(next).catch(next);
}
} else {
return next();
}
},
(req: http.IncomingMessage, res: http.ServerResponse) => {
if (req.method !== 'GET') return false;
const u = url.parse(req.url || '');
const m = u.pathname?.match(/^\/(.+)\/HEAD$/);
if (!m) return false;
if (/\.\./.test(m[1])) return false;
const repo = m[1];
const next = () => {
const file = this.dirMap(path.join(m[1], 'HEAD'));
const exists = this.exists(file);
if (exists) {
fs.createReadStream(file).pipe(res);
} else {
res.statusCode = 404;
res.end('not found');
}
};
const exists = this.exists(repo);
const anyListeners = self.listeners('head').length > 0;
const dup = new HttpDuplex(req, res);
dup.exists = exists;
dup.repo = repo;
dup.cwd = this.dirMap(repo);
dup.accept = dup.emit.bind(dup, 'accept');
dup.reject = dup.emit.bind(dup, 'reject');
dup.once('reject', (code: number) => {
dup.statusCode = code || 500;
dup.end();
});
if (!exists && self.autoCreate) {
dup.once('accept', (dir: string) => {
self.create(dir || repo, next);
});
self.emit('head', dup);
if (!anyListeners) dup.accept();
} else if (!exists) {
res.statusCode = 404;
res.setHeader('content-type', 'text/plain');
res.end('repository not found');
} else {
dup.once('accept', next);
self.emit('head', dup);
if (!anyListeners) dup.accept();
}
},
(req: http.IncomingMessage, res: http.ServerResponse) => {
if (req.method !== 'POST') return false;
const m = req.url?.match(/\/(.+)\/git-(.+)/);
if (!m) return false;
if (/\.\./.test(m[1])) return false;
const repo = m[1],
service = m[2];
if (services.indexOf(service) < 0) {
res.statusCode = 405;
res.end('service not available');
return;
}
res.setHeader(
'content-type',
'application/x-git-' + service + '-result'
);
noCache(res);
const action = createAction(
{
repo: repo,
service: service as ServiceString,
cwd: self.dirMap(repo),
},
req,
res
);
action.on('header', () => {
const evName = action.evName;
if (evName) {
const anyListeners = self.listeners(evName).length > 0;
self.emit(evName, action);
if (!anyListeners) action.accept();
}
});
},
(req: http.IncomingMessage, res: http.ServerResponse) => {
if (req.method !== 'GET' && req.method !== 'POST') {
res.statusCode = 405;
res.end('method not supported');
} else {
return false;
}
},
(req: http.IncomingMessage, res: http.ServerResponse) => {
res.statusCode = 404;
res.end('not found');
},
];
res.setHeader('connection', 'close');
(function next(ix) {
const x = handlers[ix].call(self, req, res);
if (x === false) next(ix + 1);
})(0);
}
/**
* starts a git server on the given port
* @param port - the port to start the server on
* @param options - the options to add extended functionality to the server
* @param options.type - this is either https or http (the default is http)
* @param options.key - the key file for the https server
* @param options.cert - the cert file for the https server
* @param callback - the function to call when server is started or error has occurred
*/
listen(port: number, options?: GitServerOptions, callback?: () => void): Git {
if (!options) {
options = { type: 'http' };
}
const createServer =
options.type == 'http'
? http.createServer
: https.createServer.bind(this, options);
this.server = createServer((req, res) => {
this.handle(req, res);
});
this.server.listen(port, callback);
return this;
}
/**
* closes the server instance
* @param will resolve or reject when the server closes or fails to close.
*/
close(): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.server?.close((err) => {
err ? reject(err) : resolve('Success');
});
});
}
}
|