All files util.ts

94.73% Statements 162/171
88.23% Branches 15/17
100% Functions 8/8
94.73% Lines 162/171

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 1721x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x         15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x     15x 15x 15x 15x 15x 15x 15x 15x 12x 12x 12x 12x 12x 12x 15x       3x 3x 3x 3x 3x 3x 15x 1x 1x 1x 1x 21x 21x 21x 21x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x  
import http from 'http';
import { spawn } from 'child_process';
 
import { Git } from './git';
import { HttpDuplex } from './http-duplex';
import { Service, ServiceOptions } from './service';
import { ServiceString } from './types';
 
export function packSideband(s: string): string {
  const n = (4 + s.length).toString(16);
  return Array(4 - n.length + 1).join('0') + n + s;
}
 
/**
 * adds headers to the response object to add cache control
 * @param  res  - http response
 */
export function noCache(res: http.ServerResponse) {
  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
 * @param  req  - http request object
 * @param  res  - http response
 * @param  callback - function(username, password)
 */
export function basicAuth(
  req: http.IncomingMessage,
  res: http.ServerResponse,
  callback: (username?: string, password?: string) => void
) {
  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 = Buffer.from(tokens[1], 'base64')
        .toString('utf8')
        .split(':');
      const username = splitHash.shift();
      const password = splitHash.join(':');
 
      callback(username, password);
    }
  }
}
/**
 * execute given git operation and respond
 * @param  dup  - duplex object to catch errors
 * @param  service - the method that is responding infoResponse (push, pull, clone)
 * @param  repoLocation - the repo path on disk
 * @param  res  - http response
 */
export function serviceRespond(
  dup: HttpDuplex | Git,
  service: ServiceString,
  repoLocation: string,
  res: http.ServerResponse
) {
  res.write(packSideband('# service=git-' + service + '\n'));
  res.write('0000');
 
  const isWin = /^win/.test(process.platform);
 
  const cmd = isWin
    ? ['git', service, '--stateless-rpc', '--advertise-refs', repoLocation]
    : ['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
 * @param  git     - an instance of git object
 * @param  repo    - the repository
 * @param  service - the method that is responding infoResponse (push, pull, clone)
 * @param  req  - http request object
 * @param  res  - http response
 */
export function infoResponse(
  git: Git,
  repo: string,
  service: ServiceString,
  req: http.IncomingMessage,
  res: http.ServerResponse
) {
  function next() {
    res.setHeader(
      'content-type',
      'application/x-git-' + service + '-advertisement'
    );
    noCache(res);
    serviceRespond(git, service, git.dirMap(repo), res);
  }
 
  const 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: number) => {
    res.statusCode = code || 500;
    res.end();
  });
 
  const anyListeners = git.listeners('info').length > 0;
 
  const exists = git.exists(repo);
  dup.exists = exists;
 
  if (!exists && git.autoCreate) {
    dup.once('accept', () => {
      git.create(repo, next);
    });
 
    git.emit('info', 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);
    git.emit('info', dup);
 
    if (!anyListeners) dup.accept();
  }
}
/**
 * parses a git string and returns the repo name
 * @param  repo - the raw repo name containing .git
 */
export function parseGitName(repo: string): string {
  const locationOfGit = repo.lastIndexOf('.git');
  return repo.slice(0, locationOfGit > 0 ? locationOfGit : repo.length);
}
/**
 * responds with the correct service depending on the action
 * @param  opts - options to pass Service
 * @param  req  - http request object
 * @param  res  - http response
 */
export function createAction(
  opts: ServiceOptions,
  req: http.IncomingMessage,
  res: http.ServerResponse
): Service {
  const service = new Service(opts, req, res);
 
  // TODO: see if this works or not
  // Object.keys(opts).forEach((key) => {
  //   service[key] = opts[key];
  // });
 
  return service;
}