@ref0/git 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/dist/access/private-refs.mjs +0 -2
  2. package/dist/auth/authorize.mjs +31 -24
  3. package/dist/auth/bridge.mjs +8 -8
  4. package/dist/auth/fences.d.mts +0 -1
  5. package/dist/auth/fences.mjs +1 -1
  6. package/dist/auth/grants.d.mts +0 -1
  7. package/dist/auth/grants.mjs +2 -2
  8. package/dist/auth/host-function.d.mts +1 -7
  9. package/dist/auth/host-function.mjs +38 -38
  10. package/dist/auth/identify.d.mts +0 -1
  11. package/dist/auth/identify.mjs +1 -1
  12. package/dist/auth/policy.d.mts +7 -4
  13. package/dist/auth/policy.mjs +13 -10
  14. package/dist/auth/token-rows.mjs +2 -5
  15. package/dist/auth/tokens-layer.d.mts +0 -2
  16. package/dist/auth/tokens-public.d.mts +3 -3
  17. package/dist/auth/tokens-public.mjs +4 -2
  18. package/dist/auth/tokens.d.mts +6 -1
  19. package/dist/auth/tokens.mjs +7 -1
  20. package/dist/client/client.d.mts +22 -15
  21. package/dist/client/client.mjs +31 -27
  22. package/dist/client/git-http.mjs +22 -5
  23. package/dist/client/local.mjs +1 -1
  24. package/dist/client/namespace.d.mts +2 -0
  25. package/dist/client/push-upload.d.mts +1 -0
  26. package/dist/client/push-upload.mjs +169 -2
  27. package/dist/client/remote-helper.mjs +2 -4
  28. package/dist/client/remote.mjs +37 -15
  29. package/dist/client/run-remote-helper.mjs +4 -4
  30. package/dist/client/transport.mjs +26 -19
  31. package/dist/contracts/actors.d.mts +57 -28
  32. package/dist/contracts/actors.mjs +38 -2
  33. package/dist/contracts/authorize.d.mts +21 -15
  34. package/dist/contracts/callers.mjs +40 -0
  35. package/dist/contracts/client.d.mts +16 -9
  36. package/dist/contracts/events.d.mts +12 -10
  37. package/dist/contracts/events.mjs +8 -6
  38. package/dist/contracts/files.d.mts +42 -42
  39. package/dist/contracts/forks.d.mts +0 -24
  40. package/dist/contracts/git-hooks.d.mts +217 -0
  41. package/dist/contracts/git-hooks.mjs +3 -1
  42. package/dist/contracts/grants.d.mts +46 -27
  43. package/dist/contracts/lfs.d.mts +17 -4
  44. package/dist/contracts/merges.d.mts +7 -7
  45. package/dist/contracts/objects.d.mts +20 -4
  46. package/dist/contracts/opaque.mjs +19 -1
  47. package/dist/contracts/operations.d.mts +8 -1
  48. package/dist/contracts/operations.mjs +209 -0
  49. package/dist/contracts/queue.d.mts +3 -3
  50. package/dist/contracts/queue.mjs +5 -18
  51. package/dist/contracts/remote.d.mts +13 -4
  52. package/dist/contracts/remote.mjs +3 -3
  53. package/dist/contracts/repos.d.mts +11 -40
  54. package/dist/contracts/repos.mjs +11 -10
  55. package/dist/contracts/result.d.mts +28 -11
  56. package/dist/contracts/result.mjs +32 -2
  57. package/dist/contracts/rules.d.mts +706 -46
  58. package/dist/contracts/rules.mjs +81 -2
  59. package/dist/contracts/server.d.mts +5 -275
  60. package/dist/contracts/services.d.mts +0 -3
  61. package/dist/contracts/services.mjs +1 -3
  62. package/dist/contracts/signin.d.mts +2 -2
  63. package/dist/contracts/signin.mjs +4 -5
  64. package/dist/contracts/sync.d.mts +1 -4
  65. package/dist/contracts/tokens.d.mts +29 -17
  66. package/dist/contracts/tokens.mjs +14 -3
  67. package/dist/contracts/topics.d.mts +38 -11
  68. package/dist/contracts/topics.mjs +8 -2
  69. package/dist/core/access.d.mts +0 -2
  70. package/dist/core/access.mjs +15 -4
  71. package/dist/core/events/deliver.mjs +19 -1
  72. package/dist/core/forks.mjs +30 -11
  73. package/dist/core/hooks/groups.d.mts +0 -1
  74. package/dist/core/hooks/groups.mjs +8 -9
  75. package/dist/core/loose-tally.mjs +46 -0
  76. package/dist/core/objects/writing.mjs +1 -0
  77. package/dist/core/promise.mjs +1 -15
  78. package/dist/core/purge.mjs +14 -9
  79. package/dist/core/push.mjs +13 -6
  80. package/dist/core/ref-view.mjs +14 -6
  81. package/dist/core/repack.mjs +19 -14
  82. package/dist/core/repo-info.mjs +8 -6
  83. package/dist/core/repos.mjs +1 -0
  84. package/dist/core/rules/check.mjs +10 -10
  85. package/dist/core/rules/context.d.mts +3 -1
  86. package/dist/core/rules/context.mjs +16 -13
  87. package/dist/core/rules/fetch.mjs +17 -23
  88. package/dist/core/tokens.mjs +2 -1
  89. package/dist/effect.d.mts +2 -4
  90. package/dist/effect.mjs +2 -4
  91. package/dist/engine/commits.mjs +19 -47
  92. package/dist/engine/failures.d.mts +1 -2
  93. package/dist/engine/failures.mjs +1 -3
  94. package/dist/engine/merge-base-walk.mjs +57 -142
  95. package/dist/engine/merge-bases.mjs +5 -5
  96. package/dist/engine/merge.mjs +16 -14
  97. package/dist/extend.d.mts +1 -2
  98. package/dist/extend.mjs +1 -2
  99. package/dist/http/core-parts.mjs +1 -2
  100. package/dist/http/handler.mjs +11 -31
  101. package/dist/http/parts.d.mts +0 -1
  102. package/dist/index.d.mts +28 -21
  103. package/dist/index.mjs +5 -2
  104. package/dist/lfs/base.d.mts +6 -3
  105. package/dist/lfs/base.mjs +2 -1
  106. package/dist/lfs/layer.d.mts +4 -3
  107. package/dist/lfs/layer.mjs +23 -9
  108. package/dist/lfs/objects.mjs +90 -41
  109. package/dist/lfs/pointers.mjs +69 -18
  110. package/dist/lfs/purge.mjs +6 -2
  111. package/dist/lfs/rows.mjs +24 -7
  112. package/dist/lfs/service.d.mts +6 -3
  113. package/dist/lfs/service.mjs +8 -2
  114. package/dist/lfs/upkeep.mjs +44 -26
  115. package/dist/lfs/upload-most.mjs +41 -0
  116. package/dist/lfs/uploaded.mjs +59 -0
  117. package/dist/protocol/bytes/sha256-hasher.mjs +51 -0
  118. package/dist/protocol/bytes/text.mjs +6 -1
  119. package/dist/protocol/objects/fsck.mjs +6 -3
  120. package/dist/queue/advance.mjs +38 -8
  121. package/dist/queue/calls.mjs +4 -14
  122. package/dist/queue/lanes.mjs +1 -1
  123. package/dist/queue/rows.mjs +8 -0
  124. package/dist/queue/schedule.mjs +1 -1
  125. package/dist/queue/search.mjs +3 -4
  126. package/dist/rest/client-namespace.d.mts +2 -0
  127. package/dist/rest/client.d.mts +32 -8
  128. package/dist/rest/client.mjs +308 -382
  129. package/dist/rest/commits.d.mts +240 -240
  130. package/dist/rest/files.d.mts +18 -18
  131. package/dist/rest/forks.d.mts +45 -57
  132. package/dist/rest/handlers.mjs +2 -1
  133. package/dist/rest/listen.d.mts +8 -6
  134. package/dist/rest/listen.mjs +31 -75
  135. package/dist/rest/me.d.mts +2 -2
  136. package/dist/rest/merges.d.mts +7 -7
  137. package/dist/rest/refs.d.mts +139 -139
  138. package/dist/rest/repos.d.mts +54 -66
  139. package/dist/rest/tokens.d.mts +12 -12
  140. package/dist/rest/topics.d.mts +110 -110
  141. package/dist/rest/trees.d.mts +66 -66
  142. package/dist/rest/wire.d.mts +1 -1
  143. package/dist/rest.d.mts +1 -7
  144. package/dist/rest.mjs +1 -5
  145. package/dist/server/git-layer.d.mts +10 -2
  146. package/dist/server/git-layer.mjs +29 -10
  147. package/dist/server/joined.d.mts +1 -1
  148. package/dist/server/server.d.mts +0 -1
  149. package/dist/server/server.mjs +26 -45
  150. package/dist/signin/context.d.mts +0 -1
  151. package/dist/signin/context.mjs +5 -9
  152. package/dist/storage/clean-up.mjs +3 -1
  153. package/dist/storage/fsck.mjs +12 -2
  154. package/dist/storage/graph-rebuild.mjs +262 -0
  155. package/dist/storage/index-build.mjs +100 -12
  156. package/dist/storage/lfs-pointers.d.mts +5 -5
  157. package/dist/storage/lfs-pointers.mjs +37 -11
  158. package/dist/storage/loose.mjs +4 -1
  159. package/dist/storage/memory-budget.mjs +1 -1
  160. package/dist/storage/merge-fence.mjs +81 -19
  161. package/dist/storage/merge-most.mjs +1 -0
  162. package/dist/storage/merge-row.mjs +16 -4
  163. package/dist/storage/merge-stream.mjs +9 -6
  164. package/dist/storage/object-rows.mjs +17 -1
  165. package/dist/storage/pack-receive.mjs +10 -6
  166. package/dist/storage/packs.mjs +58 -27
  167. package/dist/storage/prune.mjs +68 -40
  168. package/dist/storage/ready.d.mts +1 -1
  169. package/dist/storage/ready.mjs +12 -3
  170. package/dist/storage/receive-state.mjs +1 -1
  171. package/dist/storage/repack-contained.mjs +22 -14
  172. package/dist/storage/repack.d.mts +1 -1
  173. package/dist/storage/repack.mjs +3 -2
  174. package/dist/storage/unpack.mjs +7 -0
  175. package/dist/store/call-count.d.mts +23 -3
  176. package/dist/store/call-count.mjs +36 -13
  177. package/dist/store/records.mjs +10 -4
  178. package/dist/store/repo-record.mjs +29 -12
  179. package/dist/store/scopes.mjs +6 -1
  180. package/dist/sync/import.mjs +14 -2
  181. package/dist/sync/layer.d.mts +5 -1
  182. package/dist/sync/layer.mjs +7 -2
  183. package/dist/sync/step.mjs +15 -15
  184. package/dist/topics/advance.mjs +7 -6
  185. package/dist/topics/members.mjs +38 -9
  186. package/package.json +3 -3
  187. package/dist/engine/merge-base-walk.d.mts +0 -3
  188. package/dist/server/routes.d.mts +0 -32
  189. package/dist/server/routes.mjs +0 -69
  190. package/dist/sync/bridge.d.mts +0 -6
  191. package/dist/sync/bridge.mjs +0 -29
@@ -2,18 +2,28 @@ import { gitError } from "../contracts/result.mjs";
2
2
  import { bytesOf } from "../storage/wants.mjs";
3
3
  import { deadlineOf, timed, within } from "../engine/reads.mjs";
4
4
  import { decodeCommit } from "../protocol/objects/commit.mjs";
5
- import { isTreeMode } from "../protocol/objects/object.mjs";
5
+ import { isTreeMode, objectId } from "../protocol/objects/object.mjs";
6
6
  import { parseTreeBytes } from "../protocol/objects/tree.mjs";
7
- import { lfsOidAt, pointersOfBlob } from "../storage/lfs-pointers.mjs";
7
+ import { ownWriter } from "../core/own-writes.mjs";
8
+ import { utf8 } from "../protocol/bytes/text.mjs";
9
+ import { lfsPointerText } from "../protocol/objects/lfs-pointer.mjs";
10
+ import { lfsOidAt, lfsSizeAt, pointersOfBlob } from "../storage/lfs-pointers.mjs";
8
11
  import { listedOf } from "./objects.mjs";
12
+ import { uploadedOf } from "./uploaded.mjs";
13
+ import * as Clock$1 from "effect/Clock";
9
14
  import * as Effect from "effect/Effect";
10
15
  // src/lfs/pointers.ts
11
16
  /**
12
17
  * LFS's push rule: a ref whose new tip holds, in a tree the push carries, a Git LFS pointer the
13
- * push carries to an object the repo never listed is refused, so a clone never meets a pointer it
14
- * can't download. It starts from the pointers the receive found among the push's blobs, and reads
15
- * the listed rows of the oids they name. Only when one is unlisted does it walk each new tip's
16
- * trees, the push's own alone, for where that pointer sits.
18
+ * push carries to an object the repo doesn't hold for the pusher is refused, so a clone never
19
+ * meets a pointer it can't download. The repo holds it for the pusher when it lists the object at
20
+ * the size the pointer names, and, for a pusher whose view is narrowed, as under `access` one who
21
+ * doesn't read every pattern is, when a ref it sees reaches the object's pointer or it sent the
22
+ * object's bytes within `uploadedKept` (#1943). So a hidden object's oid and size alone push no
23
+ * pointer to it, and the refusal is the one an unlisted object gets, telling nothing of it. It
24
+ * starts from the pointers the receive found among the push's blobs, and reads the listed rows of
25
+ * the oids they name. Only when one is refused does it walk each new tip's trees, the push's own
26
+ * alone, for where that pointer sits.
17
27
  */
18
28
  /**
19
29
  * How long the walk for an unlisted pointer may take, across every ref of one push. A push's
@@ -27,7 +37,10 @@ const pushCheckMilliseconds = 6e4;
27
37
  const fileModes = /* @__PURE__ */ new Set(["100644", "100755"]);
28
38
  /**
29
39
  * The check, over the repo's listed rows in LFS's registration's `rows`: each ref it refuses, with why.
30
- * It answers only for the refs it's asked of, and may be asked twice.
40
+ * It answers only for the refs it's asked of, and may be asked twice. It checks every push,
41
+ * whatever the repo's `.gitattributes` say: one can come in the same push, sit on another ref or in
42
+ * a folder, so none says which files are pointers. Its cost is bounded already, as the receive
43
+ * reads only the push's new blobs of at most 1,024 bytes for pointers, in its own pass.
31
44
  */
32
45
  const pointerCheck = /* @__PURE__ */ Effect.fnUntraced(function* (rows, input) {
33
46
  const refused = /* @__PURE__ */ new Map();
@@ -40,7 +53,7 @@ const pointerCheck = /* @__PURE__ */ Effect.fnUntraced(function* (rows, input) {
40
53
  }
41
54
  const { lfsPointers } = input.objects;
42
55
  if (lfsPointers.count === 0 || tips.size === 0) return refused;
43
- const unlisted = yield* unlistedOf(rows, input.objects.repo, lfsPointers);
56
+ const unlisted = yield* unlistedOf(rows, input);
44
57
  if (unlisted === void 0) return refused;
45
58
  const deadline = yield* deadlineOf(pushCheckMilliseconds, "walk-limit");
46
59
  const walk = Effect.forEach(tips, ([ref, tip]) => Effect.map(missingAt(input, unlisted, tip, deadline), (missing) => [ref, missing]));
@@ -49,28 +62,66 @@ const pointerCheck = /* @__PURE__ */ Effect.fnUntraced(function* (rows, input) {
49
62
  return refused;
50
63
  });
51
64
  /**
52
- * Which of `pointers` name an object the repo never listed, one bit an entry, or `undefined` when
53
- * every one is listed. It reads a chunk of entries at a time, so it keeps no more than one read's
54
- * rows beside the bits.
65
+ * Which of the push's pointers name an object the repo doesn't hold for the pusher, one bit an
66
+ * entry, or `undefined` when it holds every one: unlisted, or listed at another size, then, of the
67
+ * rest, those `unprovenOf` leaves. It reads a chunk of entries at a time, so it keeps no more than
68
+ * one read's rows beside the bits and the entries left to prove.
55
69
  */
56
- const unlistedOf = /* @__PURE__ */ Effect.fnUntraced(function* (rows, repo, pointers) {
70
+ const unlistedOf = /* @__PURE__ */ Effect.fnUntraced(function* (rows, input) {
71
+ const pointers = input.objects.lfsPointers;
57
72
  const unlisted = new Uint8Array(Math.ceil(pointers.count / 8));
73
+ const listedAt = [];
58
74
  let any = false;
75
+ const refuse = (at) => {
76
+ unlisted[at >>> 3] = (unlisted[at >>> 3] ?? 0) | 1 << (at & 7);
77
+ any = true;
78
+ };
59
79
  for (let from = 0; from < pointers.count; from += 100) {
60
80
  const to = Math.min(from + 100, pointers.count);
61
81
  const oids = Array.from({ length: to - from }, (_, at) => lfsOidAt(pointers, from + at));
62
- const listed = yield* listedOf(rows, repo, [...new Set(oids)]);
63
- for (const [at, oid] of oids.entries()) {
64
- if (listed.has(oid)) continue;
65
- unlisted[from + at >>> 3] = (unlisted[from + at >>> 3] ?? 0) | 1 << (from + at & 7);
66
- any = true;
67
- }
82
+ const listed = yield* listedOf(rows, input.objects.repo, [...new Set(oids)]);
83
+ for (const [at, oid] of oids.entries()) if (listed.get(oid)?.size === lfsSizeAt(pointers, from + at)) listedAt.push(from + at);
84
+ else refuse(from + at);
68
85
  }
86
+ const unproven = yield* unprovenOf(rows, input, listedAt);
87
+ for (const at of listedAt) if (unproven.has(lfsOidAt(pointers, at))) refuse(at);
69
88
  return any ? {
70
89
  pointers,
71
90
  unlisted
72
91
  } : void 0;
73
92
  });
93
+ /**
94
+ * The oids of `entries`, each listed at its pointer's size, that the pusher hasn't shown it holds:
95
+ * none for a pusher whose view isn't narrowed. Else those whose pointer, as git-lfs writes it, no
96
+ * ref the pusher sees reaches, as `seenIn` asks of every one at once, less those the pusher
97
+ * uploaded within `uploadedKept`.
98
+ */
99
+ const unprovenOf = /* @__PURE__ */ Effect.fnUntraced(function* (rows, input, entries) {
100
+ const { lfsPointers: pointers, repo } = input.objects;
101
+ const unproven = /* @__PURE__ */ new Set();
102
+ if (entries.length === 0) return unproven;
103
+ const blobs = /* @__PURE__ */ new Map();
104
+ const sized = new Map(entries.map((at) => [lfsOidAt(pointers, at), lfsSizeAt(pointers, at)]));
105
+ for (const [oid, size] of sized) {
106
+ const text = lfsPointerText({
107
+ oid,
108
+ size
109
+ });
110
+ blobs.set(yield* objectId({
111
+ type: "blob",
112
+ bytes: utf8(text)
113
+ }), oid);
114
+ }
115
+ const seen = yield* input.seen([...blobs.keys()]);
116
+ if (seen === void 0) return unproven;
117
+ const unseen = [...blobs].flatMap(([blob, oid]) => seen.has(blob) ? [] : [oid]);
118
+ if (unseen.length === 0) return unproven;
119
+ const writer = yield* ownWriter(input.caller);
120
+ const at = yield* Clock$1.currentTimeMillis;
121
+ const own = writer === void 0 ? /* @__PURE__ */ new Set() : yield* uploadedOf(rows, repo, writer, unseen, at);
122
+ for (const oid of unseen) if (!own.has(oid)) unproven.add(oid);
123
+ return unproven;
124
+ });
74
125
  /** The unlisted LFS oids the pointers of the blob `blob` name, if any. */
75
126
  const unlistedNamed = ({ pointers, unlisted }, blob) => {
76
127
  const { from, to } = pointersOfBlob(pointers, blob);
@@ -24,11 +24,15 @@ const lfsPurge = (rows, bucket) => /* @__PURE__ */ Effect.fnUntraced(function* (
24
24
  {
25
25
  collection: lfsCollections.locks,
26
26
  limit: 500
27
+ },
28
+ {
29
+ collection: lfsCollections.uploaded,
30
+ limit: 500
27
31
  }
28
32
  ]);
29
- const [uploads = [], staging = [], ending = [], listed = [], locks = []] = snapshot.results;
33
+ const [uploads = [], staging = [], ending = [], listed = [], locks = [], uploaded = []] = snapshot.results;
30
34
  if (uploads.length + staging.length + ending.length > 0) return "waits";
31
- const found = listed.length > 0 ? listed : locks;
35
+ const found = listed.length > 0 ? listed : locks.length > 0 ? locks : uploaded;
32
36
  if (found.length === 0) return "done";
33
37
  const keys = yield* Effect.forEach(listed, (row) => Effect.map(decodeRow(ListedRow, row, `LFS object ${row.id}`), (object) => object.key));
34
38
  yield* Effect.forEach(keys, (key) => bucketDo(bucket.remove(key)), {
package/dist/lfs/rows.mjs CHANGED
@@ -11,14 +11,16 @@ import * as Schema from "effect/Schema";
11
11
  const lfsCollections = {
12
12
  /** Each LFS object the repo lists, by its oid. Only a deleted repo unlists. */
13
13
  objects: "lfs/objects",
14
- /** Each LFS object an upload link was given for, by its oid, until listed. */
14
+ /** Each LFS object an upload link was given for, by its oid and its uploader, until listed. */
15
15
  uploads: "lfs/uploads",
16
16
  /** A Worker upload's staging key, by its due `removeAt` and its ID. */
17
17
  staging: "lfs/staging",
18
18
  /** An LFS object upkeep is deleting, by its oid, which no verify lists. */
19
19
  ending: "lfs/ending",
20
20
  /** Each LFS lock, by its path's SHA-256. */
21
- locks: "lfs/locks"
21
+ locks: "lfs/locks",
22
+ /** Each object a caller sent bytes for, by the day, the caller's writer and the oid. */
23
+ uploaded: "lfs/uploaded"
22
24
  };
23
25
  /**
24
26
  * The largest object LFS takes: R2's largest put in one request, 5 GiB less 5 MiB, since a
@@ -33,6 +35,11 @@ const linkLife = /* @__PURE__ */ millis("15 minutes");
33
35
  */
34
36
  const uploadGrace = /* @__PURE__ */ millis("1 day");
35
37
  /**
38
+ * How long a caller's upload counts as its own, for the pointer rule: a day from its verified
39
+ * bytes, so a pointer it pushes to them passes though no ref it sees reaches them yet.
40
+ */
41
+ const uploadedKept = /* @__PURE__ */ millis("1 day");
42
+ /**
36
43
  * A listed object: its bytes are at `key`, checked. A signed upload's are at its upload row's
37
44
  * `objectKey`, and a Worker upload's stay at the staging key they streamed to, which only they
38
45
  * were ever written to.
@@ -86,10 +93,20 @@ const listedKey = (oid) => ({
86
93
  collection: lfsCollections.objects,
87
94
  id: oid
88
95
  });
89
- const uploadKey = (oid) => ({
96
+ /**
97
+ * Who an upload row is for: `ownWriter`'s SHA-256 of the caller's actor ID, or this for a caller
98
+ * with none. Each uploader's row has its own generation, so its signed links write a key no other
99
+ * caller's links write, and a verify heads only the caller's own: bytes another caller sent prove
100
+ * nothing of this one (#1943).
101
+ */
102
+ const anonymousUploader = "anonymous";
103
+ const uploadId = (oid, uploader) => `${oid}.${uploader}`;
104
+ const uploadKey = (oid, uploader) => ({
90
105
  collection: lfsCollections.uploads,
91
- id: oid
106
+ id: uploadId(oid, uploader)
92
107
  });
108
+ /** The oid an upload row's ID names, before its uploader. */
109
+ const uploadOid = (id) => id.slice(0, id.indexOf("."));
93
110
  /** An ending row's key: its upload's oid and generation, so two generations' rows never meet. */
94
111
  const endingKey = (oid, generation) => ({
95
112
  collection: lfsCollections.ending,
@@ -113,8 +130,8 @@ const listedRead = (oid) => ({
113
130
  collection: lfsCollections.objects,
114
131
  id: oid
115
132
  });
116
- const uploadRead = (oid) => ({
133
+ const uploadRead = (oid, uploader) => ({
117
134
  collection: lfsCollections.uploads,
118
- id: oid
135
+ id: uploadId(oid, uploader)
119
136
  });
120
- export { EndingRow, ListedRow, LockRow, StagingRow, UploadRow, endingKey, largestObject, lfsCollections, linkLife, listedKey, listedRead, lockKey, lockRead, objectKey, stagingId, stagingKey, stagingRead, streamMost, uploadGrace, uploadKey, uploadRead };
137
+ export { EndingRow, ListedRow, LockRow, StagingRow, UploadRow, anonymousUploader, endingKey, largestObject, lfsCollections, linkLife, listedKey, listedRead, lockKey, lockRead, objectKey, stagingId, stagingKey, stagingRead, streamMost, uploadGrace, uploadKey, uploadOid, uploadRead, uploadedKept };
@@ -1,4 +1,4 @@
1
- import { LfsFeature } from "../contracts/lfs.mjs";
1
+ import { LfsFeature, LfsOptions } from "../contracts/lfs.mjs";
2
2
  import { GitError } from "../contracts/result.mjs";
3
3
  import "../core/parts.mjs";
4
4
  import { LfsCall } from "./objects.mjs";
@@ -10,5 +10,8 @@ export interface LfsRoute extends LfsCall {
10
10
  }
11
11
  /** Git LFS's API for a server's repos, its objects in `bucket`. */
12
12
  export type LfsService = (call: LfsRoute) => Effect.Effect<Response, GitError>;
13
- /** Turns on Git LFS, its objects in the adapter's bucket: a server without it has no LFS. */
14
- export declare const lfs: () => LfsFeature;
13
+ /**
14
+ * Turns on Git LFS, its objects in the adapter's bucket: a server without it has no LFS. `options`
15
+ * win over the adapter's `RequestLimits`, as `LfsOptions` says.
16
+ */
17
+ export declare const lfs: (options?: LfsOptions) => LfsFeature;
@@ -4,8 +4,14 @@ import { batch, receive, send, verify } from "./objects.mjs";
4
4
  import { lfsAnswer } from "./wire.mjs";
5
5
  import * as Effect from "effect/Effect";
6
6
  // src/lfs/service.ts
7
- /** Turns on Git LFS, its objects in the adapter's bucket: a server without it has no LFS. */
8
- const lfs = () => ({ kind: "lfs" });
7
+ /**
8
+ * Turns on Git LFS, its objects in the adapter's bucket: a server without it has no LFS. `options`
9
+ * win over the adapter's `RequestLimits`, as `LfsOptions` says.
10
+ */
11
+ const lfs = (options = {}) => ({
12
+ ...options,
13
+ kind: "lfs"
14
+ });
9
15
  const methodNotAllowed = (allow) => Effect.succeed(lfsAnswer(405, { message: "Method not allowed" }, { allow }));
10
16
  /** LFS's routes over `parts`, provided `needs`, the server's services, which `openRepo` reads. */
11
17
  const lfsService = (needs, parts) => (call) => Effect.provideContext(served(parts, call), needs);
@@ -1,6 +1,7 @@
1
1
  import { bucketDo } from "../storage/calls.mjs";
2
2
  import { decodeRow } from "../store/errors.mjs";
3
- import { EndingRow, ListedRow, StagingRow, UploadRow, endingKey, lfsCollections, listedRead, objectKey, uploadKey, uploadRead } from "./rows.mjs";
3
+ import { EndingRow, ListedRow, StagingRow, UploadRow, endingKey, lfsCollections, listedRead, objectKey, uploadOid } from "./rows.mjs";
4
+ import { uploadedSweepOf, uploadedSweepRead } from "./uploaded.mjs";
4
5
  import * as Effect from "effect/Effect";
5
6
  // src/lfs/upkeep.ts
6
7
  /**
@@ -13,7 +14,7 @@ import * as Effect from "effect/Effect";
13
14
  * lists it again, and an upload after gets a key of its own. A delete that lands late, after a try
14
15
  * that sent it timed out, reaches only bytes no listing names. A key is deleted before the row
15
16
  * that names it is dropped, so a crash between leaves the row, and the next step deletes the key
16
- * again.
17
+ * again. Own uploads go once their days are past, as receipts do.
17
18
  */
18
19
  /** The most rows of each kind a step ends. */
19
20
  const upkeepPage = 500;
@@ -31,18 +32,23 @@ const lfsUpkeep = (rows, bucket, signed) => ({ repo, at }) => upkeepStep({
31
32
  const did = { more: true };
32
33
  const earliest = (a, b) => a === void 0 ? b : b === void 0 ? a : Math.min(a, b);
33
34
  /**
34
- * One step: the ending rows, else the due staging rows, else the due upload rows. Each does a page
35
- * and answers `more`; with nothing due, it answers when the soonest row is.
35
+ * One step: the ending rows, else the due staging rows, else the due upload rows, else the own
36
+ * uploads past their days. Each does a page and answers `more`; with nothing due, it answers when
37
+ * the soonest row is.
36
38
  */
37
39
  const upkeepStep = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep) {
38
- const snapshot = yield* upkeep.rows.read(upkeep.repo, [{
39
- collection: lfsCollections.ending,
40
- limit: upkeepPage
41
- }, {
42
- collection: lfsCollections.staging,
43
- limit: upkeepPage
44
- }]);
45
- const [ending = [], staging = []] = snapshot.results;
40
+ const snapshot = yield* upkeep.rows.read(upkeep.repo, [
41
+ {
42
+ collection: lfsCollections.ending,
43
+ limit: upkeepPage
44
+ },
45
+ {
46
+ collection: lfsCollections.staging,
47
+ limit: upkeepPage
48
+ },
49
+ uploadedSweepRead
50
+ ]);
51
+ const [ending = [], staging = [], uploaded = []] = snapshot.results;
46
52
  const endings = yield* Effect.forEach(ending, (row) => Effect.map(decodeRow(EndingRow, row, `LFS ending row ${row.id}`), (decoded) => ({
47
53
  id: row.id,
48
54
  ...decoded
@@ -55,8 +61,13 @@ const upkeepStep = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep) {
55
61
  const dueStages = stages.filter((stage) => stage.removeAt <= upkeep.at);
56
62
  if (dueStages.length > 0) return yield* endStages(upkeep, snapshot.version, dueStages);
57
63
  const uploads = yield* dueUploads(upkeep);
58
- if (uploads.oids.length > 0) return yield* endUploads(upkeep, uploads.oids);
59
- const due = stages.map((stage) => stage.removeAt).reduce(earliest, uploads.next);
64
+ if (uploads.ids.length > 0) return yield* endUploads(upkeep, uploads.ids);
65
+ const sweep = uploadedSweepOf(uploaded, upkeep.at);
66
+ if (sweep.delete.length > 0) {
67
+ yield* upkeep.rows.commit(upkeep.repo, snapshot.version, { delete: sweep.delete });
68
+ return did;
69
+ }
70
+ const due = stages.map((stage) => stage.removeAt).reduce(earliest, earliest(uploads.next, sweep.due));
60
71
  return due === void 0 ? { more: false } : {
61
72
  more: false,
62
73
  due
@@ -86,9 +97,9 @@ const endStages = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep, version,
86
97
  })) });
87
98
  return did;
88
99
  });
89
- /** Up to a page of the upload rows that are due, and when the soonest of the rest is. */
100
+ /** Up to a page of the upload rows that are due, by ID, and when the soonest of the rest is. */
90
101
  const dueUploads = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep) {
91
- const oids = [];
102
+ const ids = [];
92
103
  let next;
93
104
  let after;
94
105
  for (;;) {
@@ -103,11 +114,11 @@ const dueUploads = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep) {
103
114
  for (const row of rows) {
104
115
  const upload = yield* uploadOf(row);
105
116
  if (upload.removeAt > upkeep.at) next = earliest(next, upload.removeAt);
106
- else if (oids.length < upkeepPage) oids.push(row.id);
117
+ else if (ids.length < upkeepPage) ids.push(row.id);
107
118
  }
108
119
  after = rows.at(-1)?.id;
109
120
  if (rows.length < upkeepPage || after === void 0) return {
110
- oids,
121
+ ids,
111
122
  next
112
123
  };
113
124
  }
@@ -115,20 +126,24 @@ const dueUploads = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep) {
115
126
  const uploadOf = (row) => decodeRow(UploadRow, row, `LFS object ${row.id}'s upload`);
116
127
  const listedOf = (row) => decodeRow(ListedRow, row, `LFS object ${row.id}`);
117
128
  /**
118
- * Drops each due upload row, read again at one version. On a signed bucket, an object still
119
- * unlisted, or listed at another key, gets its ending row in the same commit, then its bytes go; a
120
- * Worker's bytes are at staging keys, which their own rows end.
129
+ * Drops each due upload row, by its ID, read again at one version. On a signed bucket, an object
130
+ * still unlisted, or listed at another key, as another uploader's bytes are, gets its ending row in
131
+ * the same commit, then its bytes go; a Worker's bytes are at staging keys, which their own rows end.
121
132
  */
122
- const endUploads = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep, oids) {
123
- const snapshot = yield* upkeep.rows.read(upkeep.repo, oids.flatMap((oid) => [uploadRead(oid), listedRead(oid)]));
133
+ const endUploads = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep, ids) {
134
+ const snapshot = yield* upkeep.rows.read(upkeep.repo, ids.flatMap((id) => [{
135
+ collection: lfsCollections.uploads,
136
+ id
137
+ }, listedRead(uploadOid(id))]));
124
138
  const dropped = [];
125
139
  const ended = [];
126
- for (const [index, oid] of oids.entries()) {
140
+ for (const [index, id] of ids.entries()) {
141
+ const oid = uploadOid(id);
127
142
  const uploadRow = snapshot.results[index * 2]?.[0];
128
143
  const listedRow = snapshot.results[index * 2 + 1]?.[0];
129
144
  const upload = uploadRow === void 0 ? void 0 : yield* uploadOf(uploadRow);
130
145
  if (upload === void 0 || upload.removeAt > upkeep.at) continue;
131
- dropped.push(oid);
146
+ dropped.push(id);
132
147
  const key = objectKey(upkeep.repo, oid, upload.generation);
133
148
  const listed = listedRow === void 0 ? void 0 : yield* listedOf(listedRow);
134
149
  if (upkeep.signed && listed?.key !== key) ended.push({
@@ -143,7 +158,10 @@ const endUploads = /* @__PURE__ */ Effect.fnUntraced(function* (upkeep, oids) {
143
158
  id,
144
159
  value: { key }
145
160
  })),
146
- delete: dropped.map(uploadKey)
161
+ delete: dropped.map((id) => ({
162
+ collection: lfsCollections.uploads,
163
+ id
164
+ }))
147
165
  });
148
166
  if (!committed.ok || ended.length === 0) return did;
149
167
  return yield* endObjects(upkeep, committed.version, ended);
@@ -0,0 +1,41 @@
1
+ import { largestObject } from "./rows.mjs";
2
+ // src/lfs/upload-most.ts
3
+ /**
4
+ * The largest Git LFS upload through the server, `workerUploadMost`, for a bucket that signs no
5
+ * links: the least of what the platform's body cap passes on, what its CPU cap lets the upload
6
+ * hash and store, and `largestObject`. A larger one is refused before a byte streams, at the
7
+ * batch, so git-lfs never starts it.
8
+ */
9
+ /** The share of a request's CPU an upload may spend: the rest is its rows', its put's and the runtime's. */
10
+ const cpuShare = .8;
11
+ /** The bytes `cpuMs` of CPU hashes and stores, at `uploadMsPerMiB` within `cpuShare` of it. */
12
+ const cpuBytes = (cpuMs) => Math.floor(cpuShare * cpuMs / 64 * 1024 ** 2);
13
+ /** The largest upload `caps` let finish: `largestObject` where neither binds below it. */
14
+ const uploadMostOf = (caps) => {
15
+ const bounds = [{
16
+ bytes: largestObject,
17
+ by: "object"
18
+ }];
19
+ if (caps.bodyCap !== void 0) bounds.push({
20
+ bytes: caps.bodyCap,
21
+ by: "body"
22
+ });
23
+ if (caps.cpuMs !== void 0) bounds.push({
24
+ bytes: cpuBytes(caps.cpuMs),
25
+ by: "cpu"
26
+ });
27
+ return bounds.reduce((least, bound) => bound.bytes < least.bytes ? bound : least);
28
+ };
29
+ /** Why `caps` aren't caps: each must be a whole number of bytes or milliseconds above 0. */
30
+ const capsRefusal = (caps) => {
31
+ for (const [name, value] of Object.entries(caps)) if (value !== void 0 && !(Number.isSafeInteger(value) && value > 0)) return `An LFS upload's ${name} is a whole number above 0, not ${String(value)}`;
32
+ };
33
+ /** What each cap that sets the largest upload is, as a refusal names it. */
34
+ const setBy = {
35
+ body: "the largest body its platform passes on",
36
+ cpu: "what its CPU cap lets it hash and store",
37
+ object: "R2's largest single put"
38
+ };
39
+ /** Why an upload of more than `most` is refused, and what takes it. */
40
+ const pastMost = (most) => `An upload through this server is at most ${most.bytes} bytes, ${setBy[most.by]}: a bucket that signs its links takes a larger one straight`;
41
+ export { capsRefusal, cpuShare, pastMost, uploadMostOf };
@@ -0,0 +1,59 @@
1
+ import { dayMs, dayOf, sweepOf } from "../core/receipts.mjs";
2
+ import { decodeRow } from "../store/errors.mjs";
3
+ import { lfsCollections, uploadedKept } from "./rows.mjs";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Schema from "effect/Schema";
6
+ // src/lfs/uploaded.ts
7
+ /**
8
+ * A caller's own upload: an object whose bytes it sent, checked against the oid, which its pushes'
9
+ * pointers may name for `uploadedKept` after, though no ref it sees reaches the object yet. So a
10
+ * caller who sees only some refs proves it holds an object's bytes by sending them, and the
11
+ * pointer rule never takes a hidden object's oid alone as proof (#1943). Only the caller's writer,
12
+ * `ownWriter`'s SHA-256 of its actor's ID, names it, so the row's ID is one length; an anonymous
13
+ * caller keeps none.
14
+ */
15
+ const UploadedRow = /* @__PURE__ */ Schema.Struct({ at: Schema.Int });
16
+ /** An own upload's ID: its day, padded so the oldest read first, its writer and its oid. */
17
+ const uploadedId = (at, writer, oid) => `${dayOf(at)}:${writer}:${oid}`;
18
+ /** The row a verified upload of `oid` by `writer` at `at` puts, in its commit. */
19
+ const uploadedRow = (writer, oid, at) => ({
20
+ collection: lfsCollections.uploaded,
21
+ id: uploadedId(at, writer, oid),
22
+ value: { at }
23
+ });
24
+ /**
25
+ * Which of `oids` `writer` uploaded to `repo` within `uploadedKept` before `at`: one read of each
26
+ * one's rows from today and yesterday, at most `sweepPage` oids a read.
27
+ */
28
+ const uploadedOf = /* @__PURE__ */ Effect.fnUntraced(function* (rows, repo, writer, oids, at) {
29
+ const days = [at, at - dayMs];
30
+ const own = /* @__PURE__ */ new Set();
31
+ for (let from = 0; from < oids.length; from += 500) {
32
+ const chunk = oids.slice(from, from + 500);
33
+ const reads = chunk.flatMap((oid) => days.map((day) => ({
34
+ collection: lfsCollections.uploaded,
35
+ id: uploadedId(day, writer, oid)
36
+ })));
37
+ const snapshot = yield* rows.read(repo, reads);
38
+ for (const [index, found] of snapshot.results.entries()) {
39
+ const oid = chunk[Math.floor(index / days.length)];
40
+ for (const row of found) {
41
+ const upload = yield* decodeRow(UploadedRow, row, `LFS upload '${row.id}'`);
42
+ if (oid !== void 0 && at < upload.at + uploadedKept) own.add(oid);
43
+ }
44
+ }
45
+ }
46
+ return own;
47
+ });
48
+ /** What LFS's upkeep reads to sweep own uploads: the oldest first. */
49
+ const uploadedSweepRead = {
50
+ collection: lfsCollections.uploaded,
51
+ limit: 500
52
+ };
53
+ const uploadedKey = (id) => [{
54
+ collection: lfsCollections.uploaded,
55
+ id
56
+ }];
57
+ /** A sweep of own uploads, of `rows` read by `uploadedSweepRead`: those whose days are past at `at`. */
58
+ const uploadedSweepOf = (rows, at) => sweepOf(rows, at, uploadedKey);
59
+ export { UploadedRow, uploadedOf, uploadedRow, uploadedSweepOf, uploadedSweepRead };
@@ -0,0 +1,51 @@
1
+ import { hexOf } from "./hex.mjs";
2
+ import { Sha256 } from "./sha256.mjs";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Exit from "effect/Exit";
5
+ // src/protocol/bytes/sha256-hasher.ts
6
+ const platform = globalThis;
7
+ /** This runtime's hashes. */
8
+ const runtime = {
9
+ DigestStream: platform.crypto?.DigestStream,
10
+ createHash: platform.process?.getBuiltinModule?.("node:crypto").createHash
11
+ };
12
+ /**
13
+ * A `Sha256Hasher` through the first hash `on` has: `DigestStream`, whose writes are each awaited
14
+ * so that it holds none of the body's bytes, then `createHash`, then `Sha256`. Its sum is read once.
15
+ *
16
+ * A `DigestStream`'s writer is held only while one of its Effects runs, and let go however that
17
+ * ends: a hasher dropped between them, as by an upload whose put failed, locks nothing. One
18
+ * interrupted at a write or its sum is aborted, without waiting, and is done.
19
+ */
20
+ const sha256HasherOn = (on) => {
21
+ if (on.DigestStream !== void 0) {
22
+ const stream = new on.DigestStream("SHA-256");
23
+ stream.digest.catch(() => void 0);
24
+ /** `use` on the stream's writer, taken for it alone, the stream aborted if it's interrupted. */
25
+ const held = (use) => Effect.acquireUseRelease(Effect.sync(() => stream.getWriter()), (writer) => Effect.promise(() => use(writer)), (writer, exit) => Effect.sync(() => {
26
+ writer.releaseLock();
27
+ if (Exit.hasInterrupts(exit)) stream.abort().catch(() => void 0);
28
+ }));
29
+ return {
30
+ write: (bytes) => held((writer) => writer.write(bytes)),
31
+ hex: held((writer) => writer.close().then(() => stream.digest.then((sum) => hexOf(new Uint8Array(sum)))))
32
+ };
33
+ }
34
+ if (on.createHash !== void 0) {
35
+ const hash = on.createHash("sha256");
36
+ return {
37
+ write: (bytes) => Effect.sync(() => {
38
+ hash.update(bytes);
39
+ }),
40
+ hex: Effect.sync(() => hash.digest("hex"))
41
+ };
42
+ }
43
+ const sha = new Sha256();
44
+ return {
45
+ write: (bytes) => Effect.sync(() => sha.update(bytes)),
46
+ hex: Effect.sync(() => sha.hex())
47
+ };
48
+ };
49
+ /** A `Sha256Hasher` through this runtime's own hash, or `Sha256` where it has none. */
50
+ const nativeSha256Hasher = () => sha256HasherOn(runtime);
51
+ export { nativeSha256Hasher, sha256HasherOn };
@@ -16,4 +16,9 @@ const isOid = (oid) => /^[0-9a-f]{40}$/.test(oid);
16
16
  * message. JSON's string form, which never throws for a string.
17
17
  */
18
18
  const quoted = (text) => JSON.stringify(text);
19
- export { isOid, isWellFormed, quoted, utf8 };
19
+ /** `text`'s first `most` UTF-16 units, the first half of a surrogate pair the cut splits dropped. */
20
+ const cutWords = (text, most) => {
21
+ const cut = text.slice(0, most);
22
+ return /[\uD800-\uDBFF]$/u.test(cut) ? cut.slice(0, -1) : cut;
23
+ };
24
+ export { cutWords, isOid, isWellFormed, quoted, utf8 };
@@ -202,7 +202,10 @@ const entryFault = (entry) => {
202
202
  if (entry.name.length > nameMost) return finding("largePathname", "contains excessively large pathname");
203
203
  if (entry.zeroPadded) return finding("zeroPaddedFilemode", "contains zero-padded file modes");
204
204
  };
205
- const duplicate = () => finding("duplicateEntries", "contains duplicate file entries");
205
+ const duplicate = (name) => ({
206
+ ...finding("duplicateEntries", "contains duplicate file entries"),
207
+ name: new TextDecoder().decode(name)
208
+ });
206
209
  /** Whether an entry before `end` is a file, any kind but a folder, named `name`. */
207
210
  const fileBefore = (bytes, end, name) => {
208
211
  for (let at = 0; at < end;) {
@@ -229,9 +232,9 @@ const orderReader = (bytes) => {
229
232
  const tree = entry.kind === "tree";
230
233
  const before = previous;
231
234
  previous = entry;
232
- if (before !== void 0 && compareNames(before.name, before.kind === "tree", entry.name, tree) >= 0) return sameName(before.name, entry.name) || tree && fileBefore(bytes, at, entry.name) ? duplicate() : finding("treeNotSorted", "not properly sorted");
235
+ if (before !== void 0 && compareNames(before.name, before.kind === "tree", entry.name, tree) >= 0) return sameName(before.name, entry.name) || tree && fileBefore(bytes, at, entry.name) ? duplicate(entry.name) : finding("treeNotSorted", "not properly sorted");
233
236
  for (let top = files.at(-1); top !== void 0; top = files.at(-1)) {
234
- if (tree && sameName(top, entry.name)) return duplicate();
237
+ if (tree && sameName(top, entry.name)) return duplicate(entry.name);
235
238
  if (top.length < entry.name.length && (entry.name[top.length] ?? slash) < slash && sameName(top, entry.name.subarray(0, top.length))) break;
236
239
  files.pop();
237
240
  }