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

nikelborm / fetch-github-folder / 12884498323

21 Jan 2025 10:01AM UTC coverage: 84.12% (+0.5%) from 83.58%
12884498323

push

github

nikelborm
quick save: Tue Jan 21 13:01:36 MSK 2025

72 of 79 branches covered (91.14%)

Branch coverage included in aggregate %.

516 of 620 relevant lines covered (83.23%)

4.14 hits per line

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

91.25
/src/getPathContents/parseGitLFSObjectEither.ts
1
import { Either, gen, isRight, left, mapLeft, 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 {
11
  ReturnTypeNoCause,
12
  ReturnTypeNoStatic,
13
  TaggedErrorVerifyingCause,
14
} from '../TaggedErrorVerifyingCause.js';
15

16
export const parseGitLFSObjectEither = ({
1✔
17
  contentAsBuffer,
4✔
18
  expectedContentSize,
4✔
19
}: {
4✔
20
  contentAsBuffer: Buffer<ArrayBuffer>;
21
  expectedContentSize: number;
22
}) =>
23
  gen(function* () {
4✔
24
    // gitLFS info usually is no longer than MAX_GIT_LFS_INFO_SIZE bytes
25
    const contentAsString = contentAsBuffer
4✔
26
      .subarray(0, MAX_GIT_LFS_INFO_SIZE)
4✔
27
      .toString('utf8');
4✔
28

29
    const parsingResult = mapLeft(
4✔
30
      decodeGitLFSInfoSchema(contentAsString.match(gitLFSInfoRegexp)?.groups),
4✔
31
      cause =>
4✔
32
        new FailedToParseGitLFSInfo(cause, {
3✔
33
          partOfContentThatCouldBeGitLFSInfo: contentAsString,
3✔
34
        }),
3✔
35
    );
4✔
36

37
    const matchedByRegexpAndParsedByEffectSchema = isRight(parsingResult);
4✔
38
    const doesSizeFromGitLFSInfoAlignWithExpectedContentSize =
4✔
39
      isRight(parsingResult) &&
4✔
40
      parsingResult.right.size === expectedContentSize;
1✔
41

42
    const shouldFailIfItIsNotGitLFS =
4✔
43
      contentAsBuffer.byteLength !== expectedContentSize;
4✔
44

45
    const isThisAGitLFSObject =
4✔
46
      matchedByRegexpAndParsedByEffectSchema &&
4✔
47
      doesSizeFromGitLFSInfoAlignWithExpectedContentSize;
1✔
48

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

55
    if (shouldFailIfItIsNotGitLFS)
3✔
56
      return yield* left(
4!
57
        new InconsistentExpectedAndRealContentSize({
×
58
          actual: contentAsBuffer.byteLength,
×
59
          expected: expectedContentSize,
×
60
          gitLFSInfo: parsingResult,
×
61
        }),
×
62
      );
✔
63

64
    return 'This is not a git LFS object' as const;
3✔
65
  });
3✔
66

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

79
const GitLFSInfoSchema = Struct({
1✔
80
  version: NonEmptyTrimmedString,
1✔
81
  oidSha256: NonEmptyTrimmedString,
1✔
82
  size: NumberFromString,
1✔
83
});
1✔
84

85
const decodeGitLFSInfoSchema = decodeUnknownEither(GitLFSInfoSchema, {
1✔
86
  exact: true,
1✔
87
});
1✔
88

89
// Extracted to a const to please JSR
90
const _Err1: ReturnTypeNoStatic<
1✔
91
  'FailedToParseGitLFSInfo',
92
  typeof ParseError,
93
  { partOfContentThatCouldBeGitLFSInfo: string }
94
> = TaggedErrorVerifyingCause<{ partOfContentThatCouldBeGitLFSInfo: string }>()(
1✔
95
  'FailedToParseGitLFSInfo',
1✔
96
  `Failed to parse git LFS announcement`,
1✔
97
  ParseError,
1✔
98
);
1✔
99

100
export class FailedToParseGitLFSInfo extends _Err1 {}
1✔
101

102
type InconsistentSizesDynamicContext = {
103
  actual: number;
104
  expected: number;
105
  gitLFSInfo: Either<
106
    Readonly<{
107
      version: string;
108
      oidSha256: string;
109
      size: number;
110
    }>,
111
    FailedToParseGitLFSInfo
112
  >;
113
};
114

115
// Extracted to a const to please JSR
116
const _Err2: ReturnTypeNoCause<
1✔
117
  'InconsistentExpectedAndRealContentSize',
118
  { comment: string },
119
  InconsistentSizesDynamicContext
120
> = TaggedErrorVerifyingCause<InconsistentSizesDynamicContext>()(
1✔
121
  'InconsistentExpectedAndRealContentSize',
1✔
122
  ctx =>
1✔
123
    `Got file with size ${ctx.actual} bytes while expecting ${ctx.expected} bytes`,
1✔
124
  void 0,
1✔
125
  {
1✔
126
    comment: outdent({ newline: ' ' })`
1✔
127
      If we weren't successful in parsing it as git LFS object
128
      announcement using RegExp and Effect.Schema, we just do a basic size
129
      consistency check. The check implements the second marker of it
130
      being a Git LFS object as a backup to checking does "content" look
131
      like a Git LFS object. If GitHub API's "size" field is different
132
      from actual size of "content" field, it means either our schema with
133
      regexp fucked up, or GitHub API did. If it doesn't throw, it means
134
      there's no reason to assume it's a Git LFS object.
135
    `,
136
  },
1✔
137
);
1✔
138

139
export class InconsistentExpectedAndRealContentSize extends _Err2 {}
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