1 | /*
|
2 | * Copyright 2018 Adobe. All rights reserved.
|
3 | * This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
4 | * you may not use this file except in compliance with the License. You may obtain a copy
|
5 | * of the License at http://www.apache.org/licenses/LICENSE-2.0
|
6 | *
|
7 | * Unless required by applicable law or agreed to in writing, software distributed under
|
8 | * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
9 | * OF ANY KIND, either express or implied. See the License for the specific language
|
10 | * governing permissions and limitations under the License.
|
11 | */
|
12 |
|
13 | ;
|
14 |
|
15 | const { spawn } = require('child_process');
|
16 | const zlib = require('zlib');
|
17 |
|
18 | const backend = require('git-http-backend');
|
19 | const { info, error } = require('@adobe/helix-log');
|
20 |
|
21 | const { resolveRepositoryPath } = require('./utils');
|
22 |
|
23 | /**
|
24 | * Export the transfer protocol handler (express middleware) through a parameterizable function
|
25 | *
|
26 | * @param {object} options configuration hash
|
27 | * @returns {function(*, *, *)} handler function
|
28 | */
|
29 | function createMiddleware(options) {
|
30 | /**
|
31 | * Express middleware handling Git (Smart) Transfer Protocol requests
|
32 | *
|
33 | * @param {Request} req Request object
|
34 | * @param {Response} res Response object
|
35 | * @param {callback} next next middleware in chain
|
36 | *
|
37 | * @see https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols
|
38 | * @see https://github.com/git/git/blob/master/Documentation/technical/http-protocol.txt
|
39 | */
|
40 | return (req, res, next) => {
|
41 | const { owner, repo } = req.params;
|
42 |
|
43 | const repPath = resolveRepositoryPath(options, owner, repo);
|
44 | const reqStream = req.headers['content-encoding'] === 'gzip' ? req.pipe(zlib.createGunzip()) : req;
|
45 | reqStream.pipe(backend(req.originalUrl, (err, service) => {
|
46 | if (err) {
|
47 | error(err);
|
48 | next(err);
|
49 | return;
|
50 | }
|
51 |
|
52 | res.setHeader('content-type', service.type);
|
53 | info(service.action, repo, service.fields);
|
54 |
|
55 | const ps = spawn(service.cmd, service.args.concat(repPath));
|
56 | ps.stdout.pipe(service.createStream()).pipe(ps.stdin);
|
57 | })).pipe(res);
|
58 | };
|
59 | }
|
60 | module.exports = createMiddleware;
|