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

nikelborm / fetch-github-folder / 12823494188

17 Jan 2025 05:51AM UTC coverage: 55.378%. Remained the same
12823494188

push

github

nikelborm
quick save: Fri Jan 17 06:13:09 MSK 2025

53 of 64 branches covered (82.81%)

Branch coverage included in aggregate %.

39 of 89 new or added lines in 9 files covered. (43.82%)

24 existing lines in 4 files now uncovered.

328 of 624 relevant lines covered (52.56%)

2.37 hits per line

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

80.0
/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 { TaggedErrorVerifyingCause } from '../TaggedErrorVerifyingCause.js';
10
import { outdent } from 'outdent';
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,
×
57
        gitLFSInfo: parsingResult.pipe(
×
UNCOV
58
          match({
×
UNCOV
59
            onLeft: left => ({
×
UNCOV
60
              meta: 'Failed to parse',
×
UNCOV
61
              error: left,
×
UNCOV
62
            }),
×
UNCOV
63
            onRight: right => ({
×
UNCOV
64
              meta: 'Parsed successfully',
×
UNCOV
65
              value: right,
×
UNCOV
66
            }),
×
UNCOV
67
          }),
×
UNCOV
68
        ),
×
UNCOV
69
      }),
×
UNCOV
70
    );
✔
71

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

75
// there are some responses that look like
76
// `version https://git-lfs.github.com/spec/v1
77
// oid sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
78
// size 128
79
// `
80
// and the only variable thing in it is the size at the end, and I assume
81
// that supported file size is not greater than 100 GB
82
const MAX_GIT_LFS_INFO_SIZE = 137;
1✔
83
// Don't add regexp /g modifier, it breaks match groups
84
const gitLFSInfoRegexp =
1✔
85
  /^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✔
86

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

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

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

105
export class InconsistentExpectedAndRealContentSize extends TaggedErrorVerifyingCause<{
1✔
106
  actual: number;
107
  expected: number;
108
  gitLFSInfo:
109
    | {
110
        meta: 'Parsed successfully';
111
        value: (typeof GitLFSInfoSchema)['Type'];
112
      }
113
    | {
114
        meta: 'Failed to parse';
115
        error: FailedToParseGitLFSInfo;
116
      };
117
}>()(
1✔
118
  'InconsistentExpectedAndRealContentSize',
1✔
119
  ctx =>
1✔
120
    `Got file with size ${ctx.actual} bytes while expecting ${ctx.expected} bytes`,
1✔
121
  void 0,
1✔
122
  {
1✔
123
    comment: outdent({ newline: ' ' })`
1✔
124
      If we weren't successful in parsing it as git LFS object
125
      announcement using RegExp and Effect.Schema, we just do a basic size
126
      consistency check. The check implements the second marker of it
127
      being a Git LFS object as a backup to checking does "content" look
128
      like a Git LFS object. If GitHub API's "size" field is different
129
      from actual size of "content" field, it means either our schema with
130
      regexp fucked up, or GitHub API did. If it doesn't throw, it means
131
      there's no reason to assume it's a Git LFS object.
132
    `,
133
  },
1✔
134
) {}
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