@ref0/git 0.7.0 → 0.9.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 (245) 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/calls-spent.d.mts +2 -12
  36. package/dist/contracts/calls-spent.mjs +4 -5
  37. package/dist/contracts/client.d.mts +16 -9
  38. package/dist/contracts/events.d.mts +82 -14
  39. package/dist/contracts/events.mjs +35 -9
  40. package/dist/contracts/files.d.mts +42 -42
  41. package/dist/contracts/forks.d.mts +0 -24
  42. package/dist/contracts/git-hooks.d.mts +217 -0
  43. package/dist/contracts/git-hooks.mjs +3 -1
  44. package/dist/contracts/git.d.mts +13 -3
  45. package/dist/contracts/grants.d.mts +46 -27
  46. package/dist/contracts/lfs.d.mts +17 -4
  47. package/dist/contracts/merges.d.mts +7 -7
  48. package/dist/contracts/objects.d.mts +38 -4
  49. package/dist/contracts/opaque.mjs +19 -1
  50. package/dist/contracts/operations.d.mts +8 -1
  51. package/dist/contracts/operations.mjs +209 -0
  52. package/dist/contracts/queue.d.mts +6 -5
  53. package/dist/contracts/queue.mjs +9 -20
  54. package/dist/contracts/remote.d.mts +13 -4
  55. package/dist/contracts/remote.mjs +3 -3
  56. package/dist/contracts/repos.d.mts +11 -40
  57. package/dist/contracts/repos.mjs +11 -10
  58. package/dist/contracts/result.d.mts +28 -11
  59. package/dist/contracts/result.mjs +32 -2
  60. package/dist/contracts/rules.d.mts +706 -46
  61. package/dist/contracts/rules.mjs +81 -2
  62. package/dist/contracts/server.d.mts +5 -275
  63. package/dist/contracts/services.d.mts +0 -3
  64. package/dist/contracts/services.mjs +1 -3
  65. package/dist/contracts/signin.d.mts +2 -2
  66. package/dist/contracts/signin.mjs +4 -5
  67. package/dist/contracts/sync.d.mts +1 -4
  68. package/dist/contracts/tokens.d.mts +29 -17
  69. package/dist/contracts/tokens.mjs +14 -3
  70. package/dist/contracts/topics.d.mts +38 -11
  71. package/dist/contracts/topics.mjs +8 -2
  72. package/dist/core/access.d.mts +0 -2
  73. package/dist/core/access.mjs +15 -4
  74. package/dist/core/ancestry.mjs +5 -4
  75. package/dist/core/events/deliver.mjs +19 -1
  76. package/dist/core/forks.mjs +30 -11
  77. package/dist/core/hooks/groups.d.mts +0 -1
  78. package/dist/core/hooks/groups.mjs +8 -9
  79. package/dist/core/index-job.mjs +23 -1
  80. package/dist/core/index-lost.d.mts +11 -0
  81. package/dist/core/index-lost.mjs +55 -0
  82. package/dist/core/loose-tally.mjs +46 -0
  83. package/dist/core/objects/commit-writes.mjs +1 -1
  84. package/dist/core/objects/commits.mjs +10 -9
  85. package/dist/core/objects/merges.mjs +1 -1
  86. package/dist/core/objects/reading.mjs +28 -13
  87. package/dist/core/objects/trees.mjs +1 -1
  88. package/dist/core/objects/writing.mjs +1 -0
  89. package/dist/core/parts.d.mts +7 -0
  90. package/dist/core/promise.mjs +1 -15
  91. package/dist/core/publish.mjs +9 -5
  92. package/dist/core/purge.mjs +30 -12
  93. package/dist/core/push.mjs +39 -16
  94. package/dist/core/ref-view.mjs +14 -6
  95. package/dist/core/repack.mjs +54 -27
  96. package/dist/core/repo-info.mjs +8 -6
  97. package/dist/core/repos.mjs +1 -0
  98. package/dist/core/rules/check.mjs +10 -10
  99. package/dist/core/rules/context.d.mts +3 -1
  100. package/dist/core/rules/context.mjs +16 -13
  101. package/dist/core/rules/fetch.mjs +17 -23
  102. package/dist/core/tokens.mjs +2 -1
  103. package/dist/core/view.d.mts +10 -1
  104. package/dist/core/view.mjs +15 -6
  105. package/dist/effect.d.mts +3 -6
  106. package/dist/effect.mjs +3 -6
  107. package/dist/engine/commits.mjs +23 -50
  108. package/dist/engine/failures.d.mts +1 -2
  109. package/dist/engine/failures.mjs +1 -3
  110. package/dist/engine/merge-base-walk.mjs +57 -142
  111. package/dist/engine/merge-bases.mjs +9 -7
  112. package/dist/engine/merge.mjs +20 -17
  113. package/dist/engine/parts.d.mts +1 -0
  114. package/dist/engine/parts.mjs +1 -1
  115. package/dist/engine/walk.mjs +15 -11
  116. package/dist/extend.d.mts +1 -2
  117. package/dist/extend.mjs +1 -2
  118. package/dist/http/core-parts.mjs +18 -10
  119. package/dist/http/handler.mjs +11 -31
  120. package/dist/http/parts.d.mts +0 -1
  121. package/dist/index.d.mts +29 -22
  122. package/dist/index.mjs +5 -2
  123. package/dist/lfs/base.d.mts +6 -3
  124. package/dist/lfs/base.mjs +2 -1
  125. package/dist/lfs/layer.d.mts +4 -3
  126. package/dist/lfs/layer.mjs +23 -9
  127. package/dist/lfs/objects.mjs +93 -41
  128. package/dist/lfs/pointers.mjs +69 -18
  129. package/dist/lfs/purge.mjs +6 -2
  130. package/dist/lfs/rows.mjs +24 -7
  131. package/dist/lfs/service.d.mts +6 -3
  132. package/dist/lfs/service.mjs +8 -2
  133. package/dist/lfs/upkeep.mjs +47 -28
  134. package/dist/lfs/upload-most.mjs +47 -0
  135. package/dist/lfs/uploaded.mjs +59 -0
  136. package/dist/protocol/bytes/sha256-hasher.mjs +51 -0
  137. package/dist/protocol/bytes/streams.mjs +84 -2
  138. package/dist/protocol/bytes/text.mjs +6 -1
  139. package/dist/protocol/diff/read.d.mts +1 -0
  140. package/dist/protocol/diff/read.mjs +5 -2
  141. package/dist/protocol/objects/commit.d.mts +7 -0
  142. package/dist/protocol/objects/commit.mjs +5 -1
  143. package/dist/protocol/objects/fsck.mjs +6 -3
  144. package/dist/queue/advance.mjs +77 -18
  145. package/dist/queue/calls.mjs +4 -14
  146. package/dist/queue/host.d.mts +4 -0
  147. package/dist/queue/lanes.mjs +1 -1
  148. package/dist/queue/layer.mjs +4 -2
  149. package/dist/queue/limits.mjs +18 -2
  150. package/dist/queue/rows.mjs +10 -1
  151. package/dist/queue/schedule.mjs +1 -1
  152. package/dist/queue/search.mjs +3 -4
  153. package/dist/queue/trim.mjs +2 -2
  154. package/dist/rest/client-namespace.d.mts +2 -0
  155. package/dist/rest/client.d.mts +32 -8
  156. package/dist/rest/client.mjs +308 -382
  157. package/dist/rest/commits.d.mts +150 -150
  158. package/dist/rest/files.d.mts +18 -18
  159. package/dist/rest/forks.d.mts +77 -89
  160. package/dist/rest/handlers.mjs +2 -1
  161. package/dist/rest/listen.d.mts +8 -6
  162. package/dist/rest/listen.mjs +31 -75
  163. package/dist/rest/me.d.mts +2 -2
  164. package/dist/rest/merges.d.mts +7 -7
  165. package/dist/rest/refs.d.mts +27 -27
  166. package/dist/rest/repos.d.mts +40 -52
  167. package/dist/rest/tokens.d.mts +12 -12
  168. package/dist/rest/topics.d.mts +13 -13
  169. package/dist/rest/trees.d.mts +66 -66
  170. package/dist/rest/wire.d.mts +20 -2
  171. package/dist/rest/work.d.mts +39 -39
  172. package/dist/rest.d.mts +1 -7
  173. package/dist/rest.mjs +1 -5
  174. package/dist/server/git-layer.d.mts +10 -2
  175. package/dist/server/git-layer.mjs +29 -10
  176. package/dist/server/joined.d.mts +12 -6
  177. package/dist/server/joined.mjs +3 -2
  178. package/dist/server/registry.mjs +11 -5
  179. package/dist/server/server.d.mts +0 -1
  180. package/dist/server/server.mjs +42 -49
  181. package/dist/signin/context.d.mts +0 -1
  182. package/dist/signin/context.mjs +5 -9
  183. package/dist/storage/clean-up.mjs +3 -1
  184. package/dist/storage/fsck.mjs +12 -2
  185. package/dist/storage/graph-rebuild.mjs +262 -0
  186. package/dist/storage/index-bitmaps.mjs +16 -10
  187. package/dist/storage/index-build.mjs +160 -24
  188. package/dist/storage/index-graph.d.mts +1 -0
  189. package/dist/storage/index-graph.mjs +29 -5
  190. package/dist/storage/index-layers.d.mts +1 -0
  191. package/dist/storage/index-layers.mjs +5 -2
  192. package/dist/storage/index-reach.mjs +7 -5
  193. package/dist/storage/index-remap.mjs +5 -5
  194. package/dist/storage/index-visible.mjs +16 -5
  195. package/dist/storage/lfs-pointers.d.mts +5 -5
  196. package/dist/storage/lfs-pointers.mjs +37 -11
  197. package/dist/storage/links.mjs +3 -3
  198. package/dist/storage/loose.mjs +4 -1
  199. package/dist/storage/memory-budget.mjs +1 -1
  200. package/dist/storage/merge-fence.mjs +81 -19
  201. package/dist/storage/merge-most.mjs +1 -0
  202. package/dist/storage/merge-row.mjs +16 -4
  203. package/dist/storage/merge-stream.mjs +9 -6
  204. package/dist/storage/object-rows.mjs +17 -1
  205. package/dist/storage/pack-order.d.mts +1 -0
  206. package/dist/storage/pack-order.mjs +18 -1
  207. package/dist/storage/pack-receive.mjs +10 -6
  208. package/dist/storage/packs.mjs +58 -27
  209. package/dist/storage/prune-walk.mjs +5 -2
  210. package/dist/storage/prune.mjs +168 -70
  211. package/dist/storage/ready.d.mts +1 -1
  212. package/dist/storage/ready.mjs +12 -3
  213. package/dist/storage/receive-bounds.mjs +3 -2
  214. package/dist/storage/receive-state.mjs +1 -1
  215. package/dist/storage/repack-contained.mjs +22 -14
  216. package/dist/storage/repack.d.mts +1 -1
  217. package/dist/storage/repack.mjs +3 -2
  218. package/dist/storage/unpack.mjs +7 -0
  219. package/dist/storage/visible.mjs +3 -2
  220. package/dist/storage/walk.d.mts +1 -0
  221. package/dist/storage/walk.mjs +9 -5
  222. package/dist/storage/wants.mjs +17 -11
  223. package/dist/store/call-count.d.mts +31 -3
  224. package/dist/store/call-count.mjs +51 -20
  225. package/dist/store/errors.d.mts +7 -1
  226. package/dist/store/errors.mjs +7 -1
  227. package/dist/store/job-try.mjs +3 -1
  228. package/dist/store/records.mjs +26 -5
  229. package/dist/store/repo-record.mjs +36 -12
  230. package/dist/store/scopes.mjs +15 -2
  231. package/dist/sync/bound.d.mts +1 -1
  232. package/dist/sync/import.mjs +15 -3
  233. package/dist/sync/layer.d.mts +5 -1
  234. package/dist/sync/layer.mjs +11 -9
  235. package/dist/sync/mirror.mjs +1 -1
  236. package/dist/sync/step.mjs +15 -15
  237. package/dist/topics/advance.mjs +11 -10
  238. package/dist/topics/layer.mjs +1 -1
  239. package/dist/topics/members.mjs +38 -9
  240. package/package.json +3 -3
  241. package/dist/engine/merge-base-walk.d.mts +0 -3
  242. package/dist/server/routes.d.mts +0 -32
  243. package/dist/server/routes.mjs +0 -69
  244. package/dist/sync/bridge.d.mts +0 -6
  245. package/dist/sync/bridge.mjs +0 -29
@@ -1,16 +1,30 @@
1
- import { guardedCalls, noDoor, promiseCalls } from "../core/promise.mjs";
2
- import { resultIn } from "../store/crossing.mjs";
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.mjs";
2
+ import { guardedCalls, noDoor } from "../core/promise.mjs";
3
3
  import { gitHttp } from "./git-http.mjs";
4
4
  import { localSide } from "./local.mjs";
5
5
  import { fetchInto, pushFrom, remoteRefs } from "./remote.mjs";
6
+ import * as Effect from "effect/Effect";
7
+ import * as Option from "effect/Option";
8
+ import * as Context from "effect/Context";
9
+ import * as Stream from "effect/Stream";
6
10
  // src/client/client.ts
11
+ var client_exports = /* @__PURE__ */ __exportAll({
12
+ GitClient: () => GitClient,
13
+ make: () => make,
14
+ paginate: () => paginate
15
+ });
7
16
  /**
8
- * A client of one remote, as Effects: its remote calls over Git's smart HTTP, and its local
9
- * calls on its own storage, which a fetch fills and a push reads. `GitClient` at `/effect` is
10
- * this, as a service. Each is an edge a host calls, as the server's Effect face is: a spent bound,
11
- * a `CallBudget`'s or the platform's cap, is answered as its refusal, never a defect.
17
+ * A client of one remote as a service, for a program that's handed one: with `GitClient` from
18
+ * `@ref0/git`, `Layer.succeed(GitClient.GitClient, GitClient.make(options))` provides it.
12
19
  */
13
- const effectGitClient = (options) => {
20
+ var GitClient = class extends (/* @__PURE__ */ Context.Service()("@ref0/git/GitClient")) {};
21
+ /**
22
+ * A client of `options.url`, as Effects: `remoteRefs`, `fetch` and `push` on the remote over Git's
23
+ * smart HTTP, and the server's calls on its own storage, which a fetch fills and a push reads. Each
24
+ * is an edge a host calls, as the server's Effect face is: a spent bound, a `CallBudget`'s or the
25
+ * platform's cap, fails as its refusal, never a defect. Interrupting a call stops it.
26
+ */
27
+ const make = (options) => {
14
28
  const local = localSide(options);
15
29
  const http = gitHttp(options);
16
30
  return guardedCalls({
@@ -21,30 +35,20 @@ const effectGitClient = (options) => {
21
35
  }, noDoor);
22
36
  };
23
37
  /**
24
- * A client of `options.url`: `remoteRefs`, `fetch` and `push` on the remote, and the server's
25
- * calls on its own storage, with no repo. Each call answers a `Result`, and stops when its
26
- * input's `signal` aborts.
38
+ * Each item `list` answers, a page at a time, from the input's `page` on: the next page asked for
39
+ * only once the last one's items are taken. A refused page fails the stream with its own error,
40
+ * after the items of the pages before it.
27
41
  */
28
- const createGitClient = (options) => promiseCalls(effectGitClient(options), resultIn);
29
- /**
30
- * Each item `list` answers, a page at a time, from the input's `page` on: the next page asked
31
- * for only once the last one's items are taken. A refused page throws its own `GitError`, so a
32
- * loop over it stops there with the error's `code`.
33
- */
34
- async function* paginate(list, ...args) {
42
+ const paginate = (list, ...args) => {
35
43
  const head = args.slice(0, -1);
36
44
  const input = args.at(-1);
37
- for (let after = input.page?.after;;) {
45
+ return Stream.paginate(input.page?.after, (after) => {
38
46
  const page = { ...input.page };
39
47
  if (after !== void 0) page.after = after;
40
- const listed = await list(...head, {
48
+ return Effect.map(list(...head, {
41
49
  ...input,
42
50
  page
43
- });
44
- if (listed.kind === "error") throw listed.error;
45
- yield* listed.value.items;
46
- if (listed.value.next === void 0) return;
47
- after = listed.value.next;
48
- }
49
- }
50
- export { createGitClient, effectGitClient, paginate };
51
+ }), (listed) => [listed.items, Option.fromUndefinedOr(listed.next)]);
52
+ });
53
+ };
54
+ export { GitClient, client_exports, make, paginate };
@@ -166,15 +166,32 @@ const gitHttp = (options) => {
166
166
  method: "POST",
167
167
  ...init
168
168
  })), answered));
169
+ /** A POST to `path` of a body `open` streams, a request made afresh for each send. */
170
+ const sendStreamed = (path, init, open) => Effect.suspend(() => {
171
+ const at = `${base}/${path}`;
172
+ const made = () => {
173
+ const streamed = {
174
+ method: "POST",
175
+ ...init,
176
+ body: open(),
177
+ duplex: "half"
178
+ };
179
+ return new Request(at, streamed);
180
+ };
181
+ return Effect.flatMap(answer(made), answered);
182
+ });
169
183
  return {
170
184
  advertisement,
171
- post: (service, protocol, body) => send(service, {
172
- headers: headers(protocol, {
185
+ post: (service, protocol, body) => {
186
+ const init = { headers: headers(protocol, {
173
187
  accept: `application/x-${service}-result`,
174
188
  "content-type": `application/x-${service}-request`
175
- }),
176
- body
177
- }),
189
+ }) };
190
+ return body instanceof Uint8Array ? send(service, {
191
+ ...init,
192
+ body
193
+ }) : sendStreamed(service, init, body);
194
+ },
178
195
  postJson: (path, body) => Effect.flatMap(requestJson(body), (json) => send(path, {
179
196
  headers: { "content-type": "application/json" },
180
197
  body: json
@@ -120,7 +120,7 @@ const localLayer = (options) => Layer.effect(Local, Effect.gen(function* () {
120
120
  hiddenRefs: () => gitsOwnRefs,
121
121
  publishedRefs: () => [],
122
122
  grants: heldGrants(),
123
- checks: { reports: {} },
123
+ checks: {},
124
124
  repackJobRow: (at) => repackJobRow(repack, at),
125
125
  jobs: noRepackRetry,
126
126
  events: void 0
@@ -0,0 +1,2 @@
1
+ import { client_d_exports } from "./client.mjs";
2
+ export { client_d_exports as GitClient };
@@ -1,6 +1,7 @@
1
1
  import { GitError } from "../contracts/result.mjs";
2
2
  import "./git-http.mjs";
3
3
  import * as Effect from "effect/Effect";
4
+ import "effect/Context";
4
5
  /**
5
6
  * A push's pack uploaded straight to the remote's bucket, as `push-upload=<above>` offers: begin,
6
7
  * then for each part a signed link and the part PUT to it, then complete, which answers as
@@ -1,15 +1,182 @@
1
1
  import { PushUploadPart, PushUploadStarted } from "../contracts/push-uploads.mjs";
2
- import { fail } from "../contracts/result.mjs";
2
+ import { tryUnlessSpent } from "../contracts/calls-spent.mjs";
3
+ import { fail, gitError, isGitError } from "../contracts/result.mjs";
4
+ import { scopedReader } from "../protocol/bytes/reader.mjs";
3
5
  import { decodeJson } from "../protocol/wire/json.mjs";
4
6
  import { pushUploadAbove } from "../protocol/wire/push-uploads.mjs";
7
+ import { readableIn } from "../store/crossing.mjs";
8
+ import { emptyReadsMost } from "./answer.mjs";
5
9
  import { heldBody } from "./git-http.mjs";
6
10
  import * as Effect from "effect/Effect";
11
+ import * as Option from "effect/Option";
12
+ import * as Cause from "effect/Cause";
7
13
  import * as Result from "effect/Result";
14
+ import * as Context from "effect/Context";
15
+ import * as Stream from "effect/Stream";
8
16
  // src/client/push-upload.ts
9
17
  /** The most of a begin's or a part's JSON answer read. */
10
18
  const answerMost = 65536;
11
19
  /** The most parts a push upload takes, as S3's multipart does. */
12
20
  const partsMost = 1e4;
21
+ /**
22
+ * The most of a pack `git-remote-ref0` holds in memory: past it, the pack is spooled to disk and
23
+ * streamed into the push, as Git's own `http.postBuffer` holds 1 MiB before it streams.
24
+ */
25
+ const packHeldMost = 1048576;
26
+ /** How much of a pack a streamed push reads at a time. */
27
+ const packChunk = 65536;
28
+ /**
29
+ * `start`, then `pack` read `packChunk` at a time from its first byte: a plain push's body,
30
+ * streamed afresh each time it's opened, so a send made again reads the pack again. Its pulls run
31
+ * in no call's context: a client reads its own spool and makes no call through Git's doors.
32
+ */
33
+ const packBody = (start, pack) => () => {
34
+ const chunks = Stream.paginate(0, (offset) => {
35
+ const length = Math.min(packChunk, pack.bytes - offset);
36
+ if (length <= 0) return Effect.succeed([[], Option.none()]);
37
+ return Effect.map(pack.read(offset, length), (bytes) => {
38
+ const next = offset + length;
39
+ return [[bytes], next < pack.bytes ? Option.some(next) : Option.none()];
40
+ });
41
+ });
42
+ return readableIn(Stream.concat(Stream.make(start), chunks), Context.empty());
43
+ };
44
+ /** A read of the local repo's pack that failed, for `cause`. */
45
+ const packUnread = (cause) => gitError("UNAVAILABLE", "The local repo's pack couldn't be read", {
46
+ cause,
47
+ hint: "Try again"
48
+ });
49
+ /** A pack read again that came out other than it first did, so an upload of it would be wrong. */
50
+ const packChanged = /* @__PURE__ */ gitError("UNAVAILABLE", "The local repo's pack changed between its reads", { hint: "Push again" });
51
+ /** A read of the local repo's pack that gave more than `emptyReadsMost` empty reads in a row. */
52
+ const packStalled = /* @__PURE__ */ gitError("UNAVAILABLE", `The local repo's pack gave ${emptyReadsMost} empty reads in a row`, { hint: "Push again" });
53
+ /** A read of `reader` that tells a spent call bound from the rest, as `heldBody`'s does. */
54
+ const readOf = (reader) => tryUnlessSpent(() => reader.read(), packUnread);
55
+ /**
56
+ * `pack` read to its end: its chunks held when it ends within `most` bytes, else only counted, the
57
+ * chunks dropped once past `most`, so it holds at most `most` and a chunk. More than
58
+ * `emptyReadsMost` empty reads in a row refuse it.
59
+ */
60
+ const heldOrCounted = (pack, most) => Effect.scoped(Effect.gen(function* () {
61
+ const reader = yield* scopedReader(pack);
62
+ let held = [];
63
+ let bytes = 0;
64
+ let empties = 0;
65
+ for (;;) {
66
+ const next = yield* readOf(reader);
67
+ if (next.done) break;
68
+ if (next.value.byteLength === 0) {
69
+ empties += 1;
70
+ if (empties > 4096) return yield* Effect.fail(packStalled);
71
+ continue;
72
+ }
73
+ empties = 0;
74
+ bytes += next.value.byteLength;
75
+ if (bytes > most) held = void 0;
76
+ else held?.push(next.value);
77
+ }
78
+ return held === void 0 ? {
79
+ kind: "counted",
80
+ bytes
81
+ } : {
82
+ kind: "held",
83
+ chunks: held
84
+ };
85
+ }));
86
+ /**
87
+ * `pack`, a run of the same fetch that `bytes` counted, as a `PackSource` read forward only, as an
88
+ * upload reads its parts: each read starts where the last ended. A run that ends short of `bytes`,
89
+ * or runs past it, is `packChanged`. Its reader is the scope's.
90
+ */
91
+ const packInOrder = /* @__PURE__ */ Effect.fnUntraced(function* (pack, bytes) {
92
+ const reader = yield* scopedReader(pack);
93
+ let at = 0;
94
+ let left = /* @__PURE__ */ new Uint8Array(0);
95
+ return {
96
+ bytes,
97
+ read: Effect.fnUntraced(function* (offset, length) {
98
+ if (offset !== at) return yield* Effect.fail(packChanged);
99
+ const part = new Uint8Array(length);
100
+ let filled = 0;
101
+ let empties = 0;
102
+ while (filled < length) {
103
+ if (left.byteLength === 0) {
104
+ const next = yield* readOf(reader);
105
+ if (next.done) return yield* Effect.fail(packChanged);
106
+ empties = next.value.byteLength === 0 ? empties + 1 : 0;
107
+ if (empties > 4096) return yield* Effect.fail(packStalled);
108
+ left = next.value;
109
+ continue;
110
+ }
111
+ const taken = Math.min(length - filled, left.byteLength);
112
+ part.set(left.subarray(0, taken), filled);
113
+ left = left.subarray(taken);
114
+ filled += taken;
115
+ }
116
+ at += length;
117
+ if (at === bytes && (left.byteLength > 0 || !(yield* endsHere(reader)))) return yield* Effect.fail(packChanged);
118
+ return part;
119
+ })
120
+ };
121
+ });
122
+ /** Whether `reader` has nothing more but empty reads, within `emptyReadsMost` of them. */
123
+ const endsHere = /* @__PURE__ */ Effect.fnUntraced(function* (reader) {
124
+ for (let empties = 0; empties <= emptyReadsMost; empties += 1) {
125
+ const next = yield* readOf(reader);
126
+ if (next.done) return true;
127
+ if (next.value.byteLength > 0) return false;
128
+ }
129
+ return yield* Effect.fail(packStalled);
130
+ });
131
+ /**
132
+ * `pack` as a stream that keeps the first thing that breaks it in `broke`: a failure, or a defect
133
+ * such as a spent call bound's, which dies past the stream's crossing. An interrupt alone, a
134
+ * send that stopped reading, breaks nothing.
135
+ */
136
+ const keptBreak = (pack, broke) => Stream.tapCause(pack, (cause) => Effect.sync(() => {
137
+ if (broke.broke || Cause.hasInterruptsOnly(cause)) return;
138
+ broke.broke = true;
139
+ broke.cause = Cause.squash(cause);
140
+ }));
141
+ const streamOf = (pack) => Stream.fromReadableStream({
142
+ evaluate: () => pack,
143
+ onError: (cause) => cause
144
+ });
145
+ /**
146
+ * `start`, then a pack: a plain push's body, made afresh for each send. The first send takes
147
+ * `first`, the fetch already run, and each later one, a retry or a 401's second send, runs `fetch`
148
+ * again. Its pulls run in `context`, the push's own, so the store calls a run makes count where
149
+ * the push's do. What breaks the last body's pack is kept in `broke`, each body opened afresh, and
150
+ * `unsent` cancels `first` when no send took it.
151
+ */
152
+ const fetchedBody = (start, first, fetch, context) => {
153
+ const broke = {
154
+ broke: false,
155
+ cause: void 0
156
+ };
157
+ let untaken = first;
158
+ const open = () => {
159
+ const taken = untaken;
160
+ untaken = void 0;
161
+ broke.broke = false;
162
+ broke.cause = void 0;
163
+ const pack = taken === void 0 ? Stream.unwrap(Effect.map(fetch, streamOf)) : streamOf(taken);
164
+ return readableIn(Stream.concat(Stream.make(start), keptBreak(pack, broke)), context);
165
+ };
166
+ return {
167
+ open,
168
+ broke,
169
+ unsent: Effect.promise(async () => {
170
+ await untaken?.cancel().catch(() => void 0);
171
+ untaken = void 0;
172
+ })
173
+ };
174
+ };
175
+ /**
176
+ * What a push answers when its pack's read broke, whatever its send answered: the read's own
177
+ * failure, a spent call bound dying as it does, not the send's or the remote's report.
178
+ */
179
+ const brokenBy = (cause) => isGitError(cause) ? Effect.fail(cause) : tryUnlessSpent(() => Promise.reject(cause), packUnread);
13
180
  /** `pack` in memory, as a `PackSource`. */
14
181
  const packInMemory = (pack) => ({
15
182
  bytes: pack.length,
@@ -50,4 +217,4 @@ const uploadPush = /* @__PURE__ */ Effect.fnUntraced(function* (http, begin, pac
50
217
  }
51
218
  return yield* http.postJson(`${upload}/complete`, { parts });
52
219
  });
53
- export { packInMemory, takesUpload, uploadPush };
220
+ export { brokenBy, fetchedBody, heldOrCounted, packBody, packHeldMost, packInMemory, packInOrder, takesUpload, uploadPush };
@@ -1,5 +1,4 @@
1
1
  import { fail } from "../contracts/result.mjs";
2
- import { joinedBytes } from "../protocol/bytes/joined.mjs";
3
2
  import { advertisedSource, pastServiceLine } from "../protocol/wire/advertisement.mjs";
4
3
  import { pushReasonsOf } from "../protocol/wire/push-limits.mjs";
5
4
  import { readPushStart } from "../protocol/wire/receive-pack.mjs";
@@ -7,7 +6,7 @@ import { encodePushReport, readPushReport } from "../protocol/wire/report-status
7
6
  import { advertisementRefused } from "./answer.mjs";
8
7
  import { heldBody } from "./git-http.mjs";
9
8
  import { decodedBy, encodedBy } from "./negotiate.mjs";
10
- import { takesUpload, uploadPush } from "./push-upload.mjs";
9
+ import { packBody, takesUpload, uploadPush } from "./push-upload.mjs";
11
10
  import * as Effect from "effect/Effect";
12
11
  // src/client/remote-helper.ts
13
12
  /**
@@ -119,8 +118,7 @@ const relayPush = /* @__PURE__ */ Effect.fnUntraced(function* (http, capabilitie
119
118
  const answer = yield* uploadPush(http, beginOf(start, pack.bytes), pack);
120
119
  return yield* askedReport(start, yield* heldBody(answer, reportMost, "report"));
121
120
  }
122
- const packed = pack.bytes === 0 ? /* @__PURE__ */ new Uint8Array() : yield* pack.read(0, pack.bytes);
123
- const answer = yield* http.post("git-receive-pack", "v0", joinedBytes([startBytes, packed]));
121
+ const answer = yield* http.post("git-receive-pack", "v0", packBody(startBytes, pack));
124
122
  return yield* plainReport(start, yield* heldBody(answer, reportMost, "report"));
125
123
  });
126
124
  export { helperAdvertisement, pushStartMost, readHelperStart, relayPush };
@@ -11,6 +11,7 @@ import { joinedBytes } from "../protocol/bytes/joined.mjs";
11
11
  import { isOid } from "../protocol/bytes/text.mjs";
12
12
  import { advertisedSource } from "../protocol/wire/advertisement.mjs";
13
13
  import { encodePushStart, pushOptionsRefusal } from "../protocol/wire/receive-pack.mjs";
14
+ import { pushUploadAbove } from "../protocol/wire/push-uploads.mjs";
14
15
  import { readPushReport } from "../protocol/wire/report-status.mjs";
15
16
  import { encodeV2Request, lsRefsArguments, readLsRefsAnswer, readV2Capabilities } from "../protocol/wire/v2.mjs";
16
17
  import { objectsFace, oneShotReads } from "../storage/calls.mjs";
@@ -20,7 +21,7 @@ import { collections } from "../store/scopes.mjs";
20
21
  import { advertisementRefused } from "./answer.mjs";
21
22
  import { heldBody } from "./git-http.mjs";
22
23
  import { localRepo } from "./local.mjs";
23
- import { packInMemory, takesUpload, uploadPush } from "./push-upload.mjs";
24
+ import { brokenBy, fetchedBody, heldOrCounted, packInOrder, uploadPush } from "./push-upload.mjs";
24
25
  import { decodedBy, encodedBy, negotiate } from "./negotiate.mjs";
25
26
  import * as Effect from "effect/Effect";
26
27
  // src/client/remote.ts
@@ -330,7 +331,11 @@ const receiveRefs = (http) => Effect.flatMap(advertisement(http, "git-receive-pa
330
331
  * Each of `updates`, checked, sent with `from` as the tip it expects, and a pack of what their
331
332
  * tips reach and `advertised`'s refs don't, from `source`: to the remote's bucket in parts when it
332
333
  * offers `push-upload=<above>` and the pack is larger, else in the push's own body. The remote
333
- * decides each ref, or all of them with `atomic`, and its report answers each.
334
+ * decides each ref, or all of them with `atomic`, and its report answers each. With no upload
335
+ * offered the pack streams into the body, made again for each send, and a pack whose read broke
336
+ * fails the push for it, whatever the remote answered. With one, it holds at most twice `above`:
337
+ * within it, the pack's chunks and the body they're joined into; past it a first run counts it,
338
+ * since the upload's begin names its size, and a second run's parts go up in order.
334
339
  */
335
340
  const sendPush = /* @__PURE__ */ Effect.fnUntraced(function* (http, source, advertised, updates, terms) {
336
341
  const pushOptions = terms.pushOptions ?? [];
@@ -348,19 +353,18 @@ const sendPush = /* @__PURE__ */ Effect.fnUntraced(function* (http, source, adve
348
353
  shallows: [],
349
354
  includeTag: false
350
355
  };
351
- const pack = wants.length === 0 ? /* @__PURE__ */ new Uint8Array() : yield* Effect.flatMap(objects.fetch(listed, {
356
+ const fetched = Effect.map(objects.fetch(listed, {
352
357
  request,
353
358
  limits
354
- }), (answer) => heldBody(answer.pack, limits.bytes, "pack"));
355
- if (pack.length > 0 && takesUpload(advertised.capabilities, pack.length)) {
356
- const begin = {
357
- bytes: pack.length,
358
- updates
359
- };
360
- if (pushOptions.length > 0) begin.pushOptions = pushOptions;
361
- if (terms.atomic === true) begin.atomic = true;
362
- const answer = yield* uploadPush(http, begin, packInMemory(pack));
363
- return yield* reportedPush(answer, true, updates);
359
+ }), (answer) => answer.pack);
360
+ const above = pushUploadAbove(advertised.capabilities);
361
+ let first;
362
+ let chunks = [];
363
+ if (wants.length > 0 && above === void 0) first = yield* fetched;
364
+ else if (wants.length > 0 && above !== void 0) {
365
+ const read = yield* Effect.flatMap(fetched, (pack) => heldOrCounted(pack, above));
366
+ if (read.kind === "counted") return yield* uploaded(http, Effect.flatMap(fetched, (pack) => packInOrder(pack, read.bytes)), updates, terms);
367
+ chunks = read.chunks;
364
368
  }
365
369
  const sideBand = offered("side-band-64k");
366
370
  const capabilities = [
@@ -381,8 +385,26 @@ const sendPush = /* @__PURE__ */ Effect.fnUntraced(function* (http, source, adve
381
385
  capabilities,
382
386
  pushOptions
383
387
  }));
384
- const body = yield* http.post("git-receive-pack", "v0", joinedBytes([start, pack]));
385
- return yield* reportedPush(body, sideBand, updates);
388
+ if (first === void 0) {
389
+ const body = yield* http.post("git-receive-pack", "v0", joinedBytes([start, ...chunks]));
390
+ return yield* reportedPush(body, sideBand, updates);
391
+ }
392
+ const streamed = fetchedBody(start, first, fetched, yield* Effect.context());
393
+ const brokeOr = (answer) => streamed.broke.broke ? brokenBy(streamed.broke.cause) : answer;
394
+ return yield* http.post("git-receive-pack", "v0", streamed.open).pipe(Effect.flatMap((body) => reportedPush(body, sideBand, updates)), Effect.ensuring(streamed.unsent), Effect.catch((error) => brokeOr(Effect.fail(error))), Effect.flatMap((updated) => brokeOr(Effect.succeed(updated))));
395
+ });
396
+ /** `pack` uploaded to the remote's bucket for `updates`, as `terms` send them, and its report. */
397
+ const uploaded = /* @__PURE__ */ Effect.fnUntraced(function* (http, pack, updates, terms) {
398
+ const answer = yield* Effect.scoped(Effect.flatMap(pack, (source) => {
399
+ const begin = {
400
+ bytes: source.bytes,
401
+ updates
402
+ };
403
+ if (terms.pushOptions !== void 0 && terms.pushOptions.length > 0) begin.pushOptions = terms.pushOptions;
404
+ if (terms.atomic === true) begin.atomic = true;
405
+ return uploadPush(http, begin, source);
406
+ }));
407
+ return yield* reportedPush(answer, true, updates);
386
408
  });
387
409
  /** Each of `updates` as the remote's report in `body` answers it, on side-band-64k or bare. */
388
410
  const reportedPush = /* @__PURE__ */ Effect.fnUntraced(function* (body, sideBand, updates) {
@@ -4,7 +4,7 @@ import { onBand } from "../protocol/framing/band.mjs";
4
4
  import { pushUploadAbove } from "../protocol/wire/push-uploads.mjs";
5
5
  import { gitHttp } from "./git-http.mjs";
6
6
  import { helperNode, nodeLocalGit, spooled } from "./helper-node.mjs";
7
- import { packInMemory } from "./push-upload.mjs";
7
+ import { packHeldMost, packInMemory } from "./push-upload.mjs";
8
8
  import { helperAdvertisement, readHelperStart, relayPush } from "./remote-helper.mjs";
9
9
  import { controlsAllowed, maskedControls, secretScrubber } from "./scrub.mjs";
10
10
  import { packPastLimits } from "./slice-push.mjs";
@@ -78,14 +78,14 @@ const spoolFile = Effect.gen(function* () {
78
78
  const writeAll = /* @__PURE__ */ Effect.fnUntraced(function* (file, chunk) {
79
79
  for (let at = 0; at < chunk.length;) at += (yield* Effect.promise(() => file.write(chunk.subarray(at)))).bytesWritten;
80
80
  });
81
- /** The pack, the input's rest: in memory, or spooled once it's past `spillAbove`. */
81
+ /** The pack, the input's rest: in memory, or spooled once it's past `spillAbove` bytes. */
82
82
  const packOf = /* @__PURE__ */ Effect.fnUntraced(function* (input, spillAbove) {
83
83
  const chunks = [];
84
84
  let bytes = 0;
85
85
  let file;
86
86
  const add = Effect.fnUntraced(function* (chunk) {
87
87
  bytes += chunk.length;
88
- if (file === void 0 && spillAbove !== void 0 && bytes > spillAbove) {
88
+ if (file === void 0 && bytes > spillAbove) {
89
89
  file = yield* spoolFile;
90
90
  for (const held of chunks.splice(0)) yield* writeAll(file, held);
91
91
  }
@@ -174,7 +174,7 @@ const push = (calls, input, url, { out, err }, hide) => {
174
174
  const { start, packFollows } = read;
175
175
  const startBytes = input.held.subarray(0, start.packOffset);
176
176
  input.held = input.held.subarray(start.packOffset);
177
- const spillAbove = pushUploadAbove(advertised.capabilities);
177
+ const spillAbove = Math.min(pushUploadAbove(advertised.capabilities) ?? Infinity, packHeldMost);
178
178
  const pack = packFollows ? yield* packOf(input, spillAbove) : packInMemory(/* @__PURE__ */ new Uint8Array());
179
179
  const sliced = packFollows && (yield* packPastLimits(advertised.capabilities, pack));
180
180
  const sideBand = start.capabilities.includes("side-band-64k");
@@ -39,23 +39,33 @@ const authorization = (credential) => "token" in credential ? `Bearer ${credenti
39
39
  const credentialsFailed = /* @__PURE__ */ gitError("UNAUTHENTICATED", "The credentials function failed, so nothing was sent", { hint: "Check the credential helper or the function the client was given" });
40
40
  const credentialOf = (credentials) => Predicate.isFunction(credentials) ? Effect.tryPromise({
41
41
  try: async (signal) => credentials({ signal }),
42
- catch: () => ({
43
- kind: "refused",
44
- error: credentialsFailed
45
- })
42
+ catch: () => credentialsFailed
46
43
  }) : Effect.succeed(credentials);
47
44
  /**
48
- * A copy of `request` to send, with the credential's header value, aborted with `signal` or with
49
- * `request`'s own: the caller's, which still holds while the answer's body is read.
45
+ * The `authorization` header `credentials` sends, read afresh from a function: none without
46
+ * credentials. One that fails is `credentialsFailed`, and one no header carries `credentialRefused`,
47
+ * both before anything is sent.
48
+ */
49
+ const credentialHeader = (credentials) => Effect.flatMap(credentialOf(credentials), (credential) => {
50
+ if (credential === void 0) return Effect.succeed(void 0);
51
+ const value = authorization(credential);
52
+ return unsendable.test(value) ? Effect.fail(credentialRefused) : Effect.succeed(value);
53
+ });
54
+ /**
55
+ * The request to send, with the credential's header value, aborted with `signal` or with the
56
+ * request's own: the caller's, which still holds while the answer's body is read. A request is
57
+ * sent as a copy, so it's never read; one made afresh is sent itself.
50
58
  */
51
59
  const signed = (request, credential, signal) => {
52
- const headers = new Headers(request.headers);
60
+ const made = request instanceof Request ? request.clone() : request();
61
+ const headers = new Headers(made.headers);
53
62
  if (credential !== void 0) headers.set("authorization", credential);
54
- return new Request(request.clone(), {
63
+ return new Request(made, {
55
64
  headers,
56
- signal: AbortSignal.any([request.signal, signal])
65
+ signal: AbortSignal.any([made.signal, signal])
57
66
  });
58
67
  };
68
+ /** Whether `retry` sends a request again after `failure`, its try `attempt`. */
59
69
  const shouldRetry = (retry, attempt, failure) => {
60
70
  if (retry === void 0 || failure.status === 401) return false;
61
71
  if (Predicate.isFunction(retry)) return retry({
@@ -181,7 +191,8 @@ const refusalOf = (response, failureOf) => {
181
191
  * The remote at `options`: each request sent through `options.transport`, or `fetch`, with the
182
192
  * credential, again as `retry` says, and once more with a fresh credential after a 401 when
183
193
  * `credentials` is a function. `failureOf` reads what a refused answer said: REST's JSON, or
184
- * Git's line of text. Each send is a clone, so `request` itself is never read.
194
+ * Git's line of text. Each send is a clone, so `request` itself is never read, or a request made
195
+ * afresh, when `request` makes one.
185
196
  */
186
197
  const remoteFetch = (options, failureOf) => {
187
198
  const send = options.transport ?? (async (request) => fetch(request));
@@ -202,14 +213,10 @@ const remoteFetch = (options, failureOf) => {
202
213
  onSuccess: (response) => response.status < 400 ? Effect.succeed(response) : Effect.flatMap(refusalOf(response, failureOf), (failure) => again(failure, Effect.succeed(response), dropped(response)))
203
214
  }));
204
215
  };
205
- const signedTries = (request) => Effect.flatMap(credentialOf(options.credentials), (credential) => {
206
- const value = credential === void 0 ? void 0 : authorization(credential);
207
- if (value !== void 0 && unsendable.test(value)) return Effect.fail({
208
- kind: "refused",
209
- error: credentialRefused
210
- });
211
- return tries(request, value, 1);
212
- });
216
+ const signedTries = (request) => credentialHeader(options.credentials).pipe(Effect.mapError((error) => ({
217
+ kind: "refused",
218
+ error
219
+ })), Effect.flatMap((value) => tries(request, value, 1)));
213
220
  return (request) => Effect.flatMap(signedTries(request), (response) => response.status === 401 && Predicate.isFunction(options.credentials) ? Effect.andThen(dropped(response), signedTries(request)) : Effect.succeed(response));
214
221
  };
215
- export { abortable, bodyStart, bodyText, credentialRefused, credentialsFailed, dropped, remoteFetch };
222
+ export { abortable, bodyStart, bodyText, credentialHeader, credentialRefused, credentialsFailed, dropped, pauseOf, remoteFetch, shouldRetry, unanswered };