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 { debug } = require('@adobe/helix-log');
|
16 | const { getObject } = require('./git');
|
17 | const { resolveRepositoryPath } = require('./utils');
|
18 |
|
19 | /**
|
20 | * Export the api handler (express middleware) through a parameterizable function
|
21 | *
|
22 | * @param {object} options configuration hash
|
23 | * @returns {function(*, *, *)} handler function
|
24 | */
|
25 | function createMiddleware(options) {
|
26 | /**
|
27 | * Express middleware handling Git API Blob requests
|
28 | *
|
29 | * Only a small subset will be implemented
|
30 | *
|
31 | * @param {Request} req Request object
|
32 | * @param {Response} res Response object
|
33 | * @param {callback} next next middleware in chain
|
34 | *
|
35 | * @see https://developer.github.com/v3/git/blobs/#get-a-blob
|
36 | */
|
37 | return (req, res, next) => {
|
38 | // GET /repos/:owner/:repo/git/blobs/:file_sha
|
39 | const { owner } = req.params;
|
40 | const repoName = req.params.repo;
|
41 | const sha = req.params.file_sha;
|
42 | const host = req.mappedSubDomain ? `localhost:${options.listen[req.protocol].port}` : req.headers.host;
|
43 |
|
44 | const repPath = resolveRepositoryPath(options, owner, repoName);
|
45 |
|
46 | if (sha.match(/[0-9a-f]/g).length !== 40) {
|
47 | // invalid sha format
|
48 | res.status(422).json({
|
49 | message: 'The sha parameter must be exactly 40 characters and contain only [0-9a-f].',
|
50 | documentation_url: 'https://developer.github.com/v3/git/blobs/#get-a-blob',
|
51 | });
|
52 | return;
|
53 | }
|
54 | getObject(repPath, sha)
|
55 | .then(({ object: content }) => {
|
56 | res.json({
|
57 | sha,
|
58 | size: content.length,
|
59 | url: `${req.protocol}://${host}${req.path}`,
|
60 | content: `${content.toString('base64')}\n`,
|
61 | encoding: 'base64',
|
62 | });
|
63 | })
|
64 | .catch((err) => {
|
65 | // TODO: use generic errors
|
66 | if (err.code === 'ReadObjectFail') {
|
67 | debug(`[blobHandler] resource not found: ${err.message}`);
|
68 | res.status(404).json({
|
69 | message: 'Not Found',
|
70 | documentation_url: 'https://developer.github.com/v3/git/blobs/#get-a-blob',
|
71 | });
|
72 | } else {
|
73 | debug(`[blobHandler] code: ${err.code} message: ${err.message} stack: ${err.stack}`);
|
74 | next(err);
|
75 | }
|
76 | });
|
77 | };
|
78 | }
|
79 | module.exports = createMiddleware;
|