• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nikelborm / fetch-github-folder / 12844191377

18 Jan 2025 12:56PM UTC coverage: 55.882% (+0.5%) from 55.378%
12844191377

push

github

nikelborm
quick save: Sat Jan 18 15:55:13 MSK 2025

53 of 64 branches covered (82.81%)

Branch coverage included in aggregate %.

7 of 51 new or added lines in 7 files covered. (13.73%)

1 existing line in 1 file now uncovered.

327 of 616 relevant lines covered (53.08%)

2.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/src/getPathContents/parseGitLFSObject.ts
1
import { isRight, left, mapLeft, match, right } from 'effect/Either';
1✔
2
import { ParseError } from 'effect/ParseResult';
3
import {
4
  decodeUnknownEither,
5
  NonEmptyTrimmedString,
6
  NumberFromString,
7
  Struct,
8
} from 'effect/Schema';
9
import { outdent } from 'outdent';
10
import { TaggedErrorVerifyingCause } from '../TaggedErrorVerifyingCause.js';
11

12
export const parseGitLFSObject = ({
1✔
13
  contentAsBuffer,
4✔
14
  expectedContentSize,
4✔
15
}: {
4✔
16
  contentAsBuffer: Buffer<ArrayBuffer>;
17
  expectedContentSize: number;
18
}) => {
4✔
19
  // gitLFS info usually is no longer than MAX_GIT_LFS_INFO_SIZE bytes
20
  const contentAsString = contentAsBuffer
4✔
21
    .subarray(0, MAX_GIT_LFS_INFO_SIZE)
4✔
22
    .toString('utf8');
4✔
23

24
  const parsingResult = mapLeft(
4✔
25
    decodeGitLFSInfoSchema(
4✔
26
      contentAsString.match(gitLFSInfoRegexp)?.groups,
4✔
27
    ),
4✔
28
    cause =>
4✔
29
      new FailedToParseGitLFSInfo(cause, {
3✔
30
        partOfContentThatCouldBeGitLFSInfo: contentAsString,
3✔
31
      }),
3✔
32
  );
4✔
33

34
  const matchedByRegexpAndParsedByEffectSchema = isRight(parsingResult);
4✔
35
  const doesSizeFromGitLFSInfoAlignWithExpectedContentSize =
4✔
36
    isRight(parsingResult) &&
4✔
37
    parsingResult.right.size === expectedContentSize;
1✔
38

39
  const shouldFailIfItIsNotGitLFS =
4✔
40
    contentAsBuffer.byteLength !== expectedContentSize;
4✔
41

42
  const isThisAGitLFSObject =
4✔
43
    matchedByRegexpAndParsedByEffectSchema &&
4✔
44
    doesSizeFromGitLFSInfoAlignWithExpectedContentSize;
1✔
45

46
  if (isThisAGitLFSObject)
4✔
47
    return right({
4✔
48
      gitLFSObjectIdSha256: parsingResult.right.oidSha256,
1✔
49
      gitLFSVersion: parsingResult.right.version,
1✔
50
    } as const);
1✔
51

52
  if (shouldFailIfItIsNotGitLFS)
3✔
53
    return left(
4!
54
      new InconsistentExpectedAndRealContentSize({
×
55
        actual: contentAsBuffer.byteLength,
×
56
        expected: expectedContentSize,
×
NEW
57
        gitLFSInfo: match(parsingResult, {
×
NEW
58
          onLeft: left => ({
×
NEW
59
            meta: 'Failed to parse',
×
NEW
60
            error: left,
×
NEW
61
          }),
×
NEW
62
          onRight: right => ({
×
NEW
63
            meta: 'Parsed successfully',
×
NEW
64
            value: right,
×
65
          }),
×
NEW
66
        }),
×
67
      }),
×
68
    );
✔
69

70
  return right('This is not a git LFS object' as const);
3✔
71
};
3✔
72

73
// there are some responses that look like
74
// `version https://git-lfs.github.com/spec/v1
75
// oid sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
76
// size 128
77
// `
78
// and the only variable thing in it is the size at the end, and I assume
79
// that supported file size is not greater than 100 GB
80
const MAX_GIT_LFS_INFO_SIZE = 137;
1✔
81
// Don't add regexp /g modifier, it breaks match groups
82
const gitLFSInfoRegexp =
1✔
83
  /^version (?<version>https:\/\/git-lfs\.github\.com\/spec\/v1)\noid sha256:(?<oidSha256>[0-9a-f]{64})\nsize (?<size>[1-9]\d{0,11})\n$/m;
1✔
84

85
const GitLFSInfoSchema = Struct({
1✔
86
  version: NonEmptyTrimmedString,
1✔
87
  oidSha256: NonEmptyTrimmedString,
1✔
88
  size: NumberFromString,
1✔
89
});
1✔
90

91
const decodeGitLFSInfoSchema = decodeUnknownEither(GitLFSInfoSchema, {
1✔
92
  exact: true,
1✔
93
});
1✔
94

95
export class FailedToParseGitLFSInfo extends TaggedErrorVerifyingCause<{
1✔
96
  partOfContentThatCouldBeGitLFSInfo: string;
97
}>()(
1✔
98
  'FailedToParseGitLFSInfo',
1✔
99
  `Failed to parse git LFS announcement`,
1✔
100
  ParseError,
1✔
101
) {}
1✔
102

103
export class InconsistentExpectedAndRealContentSize extends TaggedErrorVerifyingCause<{
1✔
104
  actual: number;
105
  expected: number;
106
  gitLFSInfo:
107
    | {
108
        meta: 'Parsed successfully';
109
        value: (typeof GitLFSInfoSchema)['Type'];
110
      }
111
    | {
112
        meta: 'Failed to parse';
113
        error: FailedToParseGitLFSInfo;
114
      };
115
}>()(
1✔
116
  'InconsistentExpectedAndRealContentSize',
1✔
117
  ctx =>
1✔
118
    `Got file with size ${ctx.actual} bytes while expecting ${ctx.expected} bytes`,
1✔
119
  void 0,
1✔
120
  {
1✔
121
    comment: outdent({ newline: ' ' })`
1✔
122
      If we weren't successful in parsing it as git LFS object
123
      announcement using RegExp and Effect.Schema, we just do a basic size
124
      consistency check. The check implements the second marker of it
125
      being a Git LFS object as a backup to checking does "content" look
126
      like a Git LFS object. If GitHub API's "size" field is different
127
      from actual size of "content" field, it means either our schema with
128
      regexp fucked up, or GitHub API did. If it doesn't throw, it means
129
      there's no reason to assume it's a Git LFS object.
130
    `,
131
  },
1✔
132
) {}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc