All files / lib/packages/shared-utils s3-url-parser.ts

100% Statements 15/15
100% Branches 8/8
50% Functions 1/2
100% Lines 14/14

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  8x     8x 8x 1x               7x 7x 1x               6x 6x 1x                 5x     5x 2x             3x    
export function s3ParseUrl(url: string) {
  const _decodedUrl = decodeURIComponent(url);
 
  // http://s3.amazonaws.com/bucket/key1/key2
  const _match_1 = _decodedUrl.match(/^https?:\/\/s3.amazonaws.com\/([^/]+)\/?(.*?)$/);
  if (_match_1) {
    return {
      bucket: _match_1[1],
      key: _match_1[2],
      region: "",
    };
  }
 
  // http://s3-aws-region.amazonaws.com/bucket/key1/key2
  const _match_2 = _decodedUrl.match(/^https?:\/\/s3-([^.]+).amazonaws.com\/([^/]+)\/?(.*?)$/);
  if (_match_2) {
    return {
      bucket: _match_2[2],
      key: _match_2[3],
      region: _match_2[1],
    };
  }
 
  // http://bucket.s3.amazonaws.com/key1/key2
  const _match_3 = _decodedUrl.match(/^https?:\/\/([^.]+).s3.amazonaws.com\/?(.*?)$/);
  if (_match_3) {
    return {
      bucket: _match_3[1],
      key: _match_3[2],
      region: "",
    };
  }
 
  // http://bucket.s3-aws-region.amazonaws.com/key1/key2 or,
  // http://bucket.s3.aws-region.amazonaws.com/key1/key2
  const _match_4 = _decodedUrl.match(
    /^https?:\/\/([^.]+).(?:s3-|s3\.)([^.]+).amazonaws.com\/?(.*?)$/,
  );
  if (_match_4) {
    return {
      bucket: _match_4[1],
      key: _match_4[3],
      region: _match_4[2],
    };
  }
 
  return null;
}