@ref0/git 0.2.0 → 0.3.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 (113) hide show
  1. package/dist/auth/authorize.d.mts +1 -1
  2. package/dist/auth/fences.d.mts +1 -1
  3. package/dist/auth/host-function.mjs +2 -2
  4. package/dist/auth/identify.d.mts +1 -1
  5. package/dist/auth/tokens.d.mts +1 -1
  6. package/dist/client/client.mjs +2 -3
  7. package/dist/client/local.mjs +13 -3
  8. package/dist/contracts/calls-spent.mjs +3 -2
  9. package/dist/contracts/client.d.mts +2 -2
  10. package/dist/contracts/handlers.d.mts +2 -1
  11. package/dist/contracts/objects.d.mts +2 -2
  12. package/dist/contracts/promises.d.mts +13 -38
  13. package/dist/contracts/promises.mjs +13 -0
  14. package/dist/contracts/queue.d.mts +2 -1
  15. package/dist/contracts/result.d.mts +7 -0
  16. package/dist/contracts/server.d.mts +67 -40
  17. package/dist/contracts/sync.d.mts +1 -1
  18. package/dist/contracts/topics.d.mts +7 -1
  19. package/dist/core/access.d.mts +1 -1
  20. package/dist/core/call-names.d.mts +1 -0
  21. package/dist/core/call-names.mjs +93 -0
  22. package/dist/core/events/deliver.mjs +14 -7
  23. package/dist/core/events/stream.d.mts +1 -1
  24. package/dist/core/events/stream.mjs +8 -1
  25. package/dist/core/hooks/post-receive.mjs +15 -7
  26. package/dist/core/promise.mjs +28 -10
  27. package/dist/core/push.mjs +2 -1
  28. package/dist/core/seam.d.mts +2 -0
  29. package/dist/core/seam.mjs +19 -6
  30. package/dist/extend.d.mts +2 -1
  31. package/dist/extend.mjs +3 -1
  32. package/dist/index.d.mts +2 -2
  33. package/dist/lfs/objects.mjs +1 -1
  34. package/dist/lfs/rows.mjs +1 -1
  35. package/dist/queue/advance.mjs +2 -1
  36. package/dist/queue/host.d.mts +1 -1
  37. package/dist/queue/host.mjs +1 -1
  38. package/dist/queue/schedule.mjs +1 -1
  39. package/dist/rest/blobs.d.mts +15 -15
  40. package/dist/rest/client.mjs +2 -2
  41. package/dist/rest/commits.d.mts +91 -91
  42. package/dist/rest/errors.d.mts +2 -2
  43. package/dist/rest/errors.mjs +2 -2
  44. package/dist/rest/files.d.mts +81 -81
  45. package/dist/rest/handler.d.mts +1 -0
  46. package/dist/rest/handler.mjs +39 -16
  47. package/dist/rest/handlers.mjs +6 -6
  48. package/dist/rest/repos.d.mts +38 -38
  49. package/dist/rest/tags.d.mts +41 -41
  50. package/dist/rest/tokens.d.mts +145 -145
  51. package/dist/rest/work.d.mts +43 -43
  52. package/dist/server/extension.d.mts +6 -3
  53. package/dist/server/extension.mjs +39 -31
  54. package/dist/server/lifecycle.d.mts +3 -0
  55. package/dist/server/lifecycle.mjs +89 -0
  56. package/dist/server/routes.d.mts +7 -4
  57. package/dist/server/routes.mjs +12 -19
  58. package/dist/server/server.d.mts +8 -2
  59. package/dist/server/server.mjs +280 -104
  60. package/dist/signin/context.d.mts +1 -1
  61. package/dist/signin/grants.d.mts +1 -1
  62. package/dist/storage/bits.mjs +41 -0
  63. package/dist/storage/calls.d.mts +1 -1
  64. package/dist/storage/calls.mjs +2 -2
  65. package/dist/storage/fetch.d.mts +3 -1
  66. package/dist/storage/fetch.mjs +230 -24
  67. package/dist/storage/index-bitmaps.d.mts +1 -1
  68. package/dist/storage/index-build.d.mts +1 -1
  69. package/dist/storage/index-build.mjs +27 -13
  70. package/dist/storage/index-fetch.d.mts +1 -0
  71. package/dist/storage/index-fetch.mjs +103 -182
  72. package/dist/storage/index-graph.d.mts +1 -1
  73. package/dist/storage/loose.mjs +3 -2
  74. package/dist/storage/pack-order.d.mts +1 -1
  75. package/dist/storage/pack-order.mjs +3 -1
  76. package/dist/storage/pack-ranges.d.mts +1 -1
  77. package/dist/storage/pack-reads.d.mts +1 -1
  78. package/dist/storage/pack-reads.mjs +17 -0
  79. package/dist/storage/pack-receive.d.mts +2 -2
  80. package/dist/storage/pack-receive.mjs +1 -1
  81. package/dist/storage/packs.d.mts +2 -2
  82. package/dist/storage/packs.mjs +10 -7
  83. package/dist/storage/push-uploads.mjs +2 -1
  84. package/dist/storage/ready.d.mts +2 -2
  85. package/dist/storage/receive-links.mjs +1 -1
  86. package/dist/storage/repack.d.mts +2 -2
  87. package/dist/storage/rows.d.mts +2 -1
  88. package/dist/storage/rows.mjs +1 -1
  89. package/dist/storage/send-list.d.mts +8 -0
  90. package/dist/storage/send-list.mjs +319 -0
  91. package/dist/storage/shallow.d.mts +1 -2
  92. package/dist/storage/unpack.mjs +5 -3
  93. package/dist/storage/walk.d.mts +2 -1
  94. package/dist/storage/walk.mjs +14 -8
  95. package/dist/store/catalogue.d.mts +1 -1
  96. package/dist/store/closed.d.mts +21 -0
  97. package/dist/store/closed.mjs +34 -0
  98. package/dist/store/crossing.mjs +9 -6
  99. package/dist/store/cursor-seal.mjs +4 -1
  100. package/dist/store/errors.mjs +13 -7
  101. package/dist/store/outbox.mjs +4 -2
  102. package/dist/store/promises.d.mts +5 -0
  103. package/dist/store/promises.mjs +258 -0
  104. package/dist/sync/bound.mjs +9 -4
  105. package/dist/sync/jobs.d.mts +3 -3
  106. package/dist/sync/jobs.mjs +28 -25
  107. package/dist/sync/tries.mjs +2 -2
  108. package/dist/topics/advance.mjs +2 -1
  109. package/dist/topics/calls.mjs +3 -3
  110. package/dist/topics/host.d.mts +1 -1
  111. package/dist/topics/host.mjs +1 -1
  112. package/dist/topics/topic.mjs +22 -7
  113. package/package.json +2 -2
@@ -1,3 +1,4 @@
1
+ import { millis } from "../contracts/promises.mjs";
1
2
  import { accessOf, audienceOf, authorizer, hostAuthorize } from "../auth/authorize.mjs";
2
3
  import { grantFences } from "../auth/fences.mjs";
3
4
  import { grantAgain } from "../auth/grants.mjs";
@@ -9,9 +10,10 @@ import { fileCalls } from "../core/objects/files.mjs";
9
10
  import { mergeCalls } from "../core/objects/merges.mjs";
10
11
  import { tagCalls } from "../core/objects/tags.mjs";
11
12
  import { treeCalls } from "../core/objects/trees.mjs";
13
+ import { callNames } from "../core/call-names.mjs";
12
14
  import { clocksOf } from "../core/clock.mjs";
13
- import { deliverJob, eventDelivery, workFailed } from "../core/events/deliver.mjs";
14
- import { postReceiveJob } from "../core/hooks/post-receive.mjs";
15
+ import { checkDeliveryTimeout, deliverJob, eventDelivery, workFailed } from "../core/events/deliver.mjs";
16
+ import { checkPostReceiveTimeout, postReceiveJob } from "../core/hooks/post-receive.mjs";
15
17
  import { servedSources } from "../core/rules/protect.mjs";
16
18
  import { refCalls } from "../core/refs.mjs";
17
19
  import { repackJob, repackJobRow, upkeepsOf } from "../core/repack.mjs";
@@ -23,23 +25,28 @@ import { attachParts, renamedTo, seamAuthorizer } from "../core/seam.mjs";
23
25
  import { callerTokenCalls } from "../core/tokens.mjs";
24
26
  import { workCalls } from "../core/work.mjs";
25
27
  import { coreHttpParts } from "../http/core-parts.mjs";
28
+ import { refusal } from "../http/answers.mjs";
26
29
  import { lfsCalls } from "../lfs/calls.mjs";
27
30
  import { lfsService } from "../lfs/service.mjs";
28
31
  import { pointerCheck } from "../lfs/pointers.mjs";
29
32
  import { lfsUpkeep } from "../lfs/upkeep.mjs";
30
33
  import { gitHttp } from "../http/handler.mjs";
31
- import { normalBase } from "../http/paths.mjs";
34
+ import { gitUrlOf, normalBase } from "../http/paths.mjs";
32
35
  import { pushUploads } from "../http/push-uploads.mjs";
33
36
  import { engineParts } from "../engine/parts.mjs";
34
37
  import { decideRejection } from "../signin/context.mjs";
35
38
  import { signInRoute } from "../signin/route.mjs";
39
+ import { signInPaths } from "../signin/wire.mjs";
40
+ import { answerOf } from "../contracts/calls-spent.mjs";
41
+ import { closedError } from "../store/closed.mjs";
36
42
  import { clientWentAway } from "../store/errors.mjs";
37
43
  import { cursorSeal } from "../store/cursor-seal.mjs";
38
44
  import { gitStore } from "../store/git-store.mjs";
45
+ import { promisesBucket, promisesClock, promisesQueue, promisesStore } from "../store/promises.mjs";
39
46
  import { defaultFetchLimits } from "../storage/fetch.mjs";
40
47
  import { pushUploadUpkeep, signedUploadOf } from "../storage/push-uploads.mjs";
41
- import { checkedLimits, countedMost } from "../store/call-count.mjs";
42
- import { exitOn, noCall, resultOn, runOn, syncIn, uncounted } from "../store/crossing.mjs";
48
+ import { checkedLimits, countedMost, handedScope } from "../store/call-count.mjs";
49
+ import { exitOn, noCall, syncIn, uncounted } from "../store/crossing.mjs";
43
50
  import { pruneFloor } from "../storage/rows.mjs";
44
51
  import { queueJobs } from "../queue/advance.mjs";
45
52
  import { queueCalls } from "../queue/calls.mjs";
@@ -50,18 +57,21 @@ import { topicCalls } from "../topics/calls.mjs";
50
57
  import { serveTopics, topicsOptions } from "../topics/host.mjs";
51
58
  import { listJob } from "../store/list-job.mjs";
52
59
  import { attachExtensions, openExtensions } from "./extension.mjs";
53
- import { aroundCalls, guardedCalls, promiseCalls } from "../core/promise.mjs";
60
+ import { bridgeRefusal, serverLife, throughDoor, unavailable } from "./lifecycle.mjs";
61
+ import { aroundCalls, callOf, guardedCalls, lazyGroups, promiseCalls, signalOf } from "../core/promise.mjs";
54
62
  import { withAudienceMemo } from "../core/sight.mjs";
55
63
  import { SealedObjects } from "../core/view.mjs";
56
- import { millis } from "@ref0/infra";
64
+ import { Bucket, BucketLinks, BucketUploads, CallLimits, JobQueue, Records } from "@ref0/infra";
57
65
  import * as Effect from "effect/Effect";
66
+ import * as EffectResult from "effect/Result";
67
+ import * as Option from "effect/Option";
68
+ import * as Predicate from "effect/Predicate";
58
69
  import * as Cause from "effect/Cause";
59
70
  import * as Exit from "effect/Exit";
60
- import * as Predicate from "effect/Predicate";
71
+ import * as Context from "effect/Context";
61
72
  import * as Layer from "effect/Layer";
62
73
  import * as ManagedRuntime from "effect/ManagedRuntime";
63
74
  // src/server/server.ts
64
- const closedError = () => /* @__PURE__ */ new Error("This Git server is closed");
65
75
  /**
66
76
  * The server's `fetchLimits`, or a `TypeError` naming the one that isn't a whole number above 0,
67
77
  * or a time under an hour.
@@ -91,11 +101,6 @@ const preReceiveOf = (hook) => {
91
101
  timeout: millis(hook.timeout)
92
102
  };
93
103
  };
94
- /**
95
- * A Git server over one adapter: its calls, `identify`, and `fetch` and `route`, Git's Smart HTTP
96
- * over its core and its sign-in, which answer `404` and `undefined` outside their URLs. Git
97
- * registers its own jobs on the adapter's queue, and never runs it: the host starts it.
98
- */
99
104
  /** `factory`'s objects, as they keep their index: `ServerSetup` takes `access` only with one. */
100
105
  const indexedOpen = (factory, input) => factory.open(input);
101
106
  /**
@@ -121,41 +126,35 @@ const lessBefore = (limits) => {
121
126
  if (subrequests !== void 0) lessened.subrequests = subrequests;
122
127
  return lessened;
123
128
  };
124
- const createGitServer = (options) => {
125
- const { adapter } = options;
129
+ /** The server's wiring, in its runtime: built once, with the runtime. */
130
+ const Wired = Context.Service("@ref0/git/server/Wired");
131
+ /** The groups the server's face names: every group, less the optional ones `optional` leaves off. */
132
+ const namesOf = (optional) => {
133
+ const names = { ...callNames };
134
+ if (optional.tokens === void 0) delete names.tokens;
135
+ if (optional.queue === void 0) delete names.queue;
136
+ if (optional.topics === void 0) delete names.topics;
137
+ return names;
138
+ };
139
+ /** Git's own wiring over the adapter's services, as the runtime's build runs it. */
140
+ const wireServer = (options, wiring) => {
141
+ const { adapter, engine, optional, fetchLimits, reports, extensions, run, guard, through, closing } = wiring;
142
+ const queueOf = optional.queue;
126
143
  const { store, jobs } = adapter;
127
- if (jobs.store !== store) throw new TypeError("adapter.store and adapter.jobs must share one store");
128
- const engine = engineParts(options.engine);
129
- if (engine === void 0) throw new TypeError("options.engine must be one engine() made");
130
- if (options.signIn !== void 0 && options.tokens === void 0) throw new TypeError("signIn mints tokens: pass tokens() too");
131
- if (options.access !== void 0 && options.objects.indexed !== true) throw new TypeError("access hides refs, which needs objects that follow them: pass packs()");
132
144
  const hostRules = options.rules?.push ?? [];
133
- const queueOf = options.queue === void 0 ? void 0 : queueOptions(options.queue);
134
- if (queueOf === void 0) servedSources(hostRules);
135
- const fetchLimits = fetchLimitsOf(options.fetchLimits);
136
145
  const { clock, now } = clocksOf(adapter.clock);
137
- const reports = {};
138
- if (options.onError !== void 0) reports.onError = options.onError;
139
- if (options.onRefused !== void 0) reports.onRefused = options.onRefused;
140
- const runtime = ManagedRuntime.make(Layer.empty);
141
- let closed = false;
142
- const run = async (effect, crossing, signal) => {
143
- if (closed) throw closedError();
144
- started();
145
- return resultOn(runtime, withAudienceMemo(effect), crossing, signal);
146
- };
147
- const callLimits = adapter.callLimits === void 0 ? void 0 : lessBefore(checkedLimits(adapter.callLimits, "The adapter's callLimits."));
146
+ const callLimits = lessBefore(checkedLimits(adapter.callLimits, "The adapter's callLimits."));
148
147
  const opening = {
149
148
  store: promiseCalls(gitStore(store), run),
150
149
  bucket: adapter.bucket,
151
150
  clock,
152
- fetchLimits
151
+ fetchLimits,
152
+ callLimits
153
153
  };
154
- if (callLimits !== void 0) opening.callLimits = callLimits;
155
154
  const indexed = options.access === void 0 ? void 0 : indexedOpen(options.objects, opening);
156
155
  const objects = indexed ?? options.objects.open(opening);
157
156
  const sealed = indexed === void 0 ? new SealedObjects(objects) : SealedObjects.indexed(indexed);
158
- const cleanup = options.tokens === void 0 ? void 0 : tokenCleanupJob(now);
157
+ const cleanup = optional.tokens === void 0 ? void 0 : tokenCleanupJob(now);
159
158
  const delivery = options.events === void 0 ? void 0 : deliverJob({
160
159
  store,
161
160
  now,
@@ -185,16 +184,11 @@ const createGitServer = (options) => {
185
184
  if (cleanup !== void 0) own.push(cleanup);
186
185
  if (delivery !== void 0) own.push(delivery.definition);
187
186
  if (received !== void 0) own.push(received.definition);
187
+ for (const job of own) {
188
+ if (extensions.jobs.has(job.name)) throw new TypeError(`A job is named '${job.name}' already`);
189
+ extensions.jobs.set(job.name, job);
190
+ }
188
191
  jobs.register(...own);
189
- const extensions = openExtensions(adapter, own, queueOf !== void 0);
190
- const started = () => {
191
- extensions.started = true;
192
- };
193
- const guard = Effect.suspend(() => {
194
- if (closed) return Effect.die(closedError());
195
- started();
196
- return Effect.void;
197
- });
198
192
  const decides = options.authorize === void 0 ? { reports } : {
199
193
  authorize: options.authorize,
200
194
  reports
@@ -272,9 +266,8 @@ const createGitServer = (options) => {
272
266
  jobs: queueJobList
273
267
  });
274
268
  }
275
- if (options.topics !== void 0 && queueHost === void 0) throw new TypeError("topics merge through each repo’s queue: pass queue too");
276
- const topicsHost = options.topics === void 0 || queueHost === void 0 ? void 0 : {
277
- ...topicsOptions(options.topics),
269
+ const topicsHost = optional.topics === void 0 || queueHost === void 0 ? void 0 : {
270
+ ...topicsOptions(optional.topics),
278
271
  queue: queueHost
279
272
  };
280
273
  if (topicsHost !== void 0) {
@@ -294,7 +287,7 @@ const createGitServer = (options) => {
294
287
  }
295
288
  const identify = identifier({
296
289
  store,
297
- tokens: options.tokens !== void 0,
290
+ tokens: optional.tokens !== void 0,
298
291
  auth: options.auth,
299
292
  grants: () => extensions.grants,
300
293
  reports,
@@ -308,7 +301,8 @@ const createGitServer = (options) => {
308
301
  });
309
302
  /**
310
303
  * Every group of the server's calls over `on`, deciding for `bound`: the one list `callsAs` and
311
- * `hostCalls` both build from, so a group added to one is in the other.
304
+ * `hostCalls` both build from, so a group added to one is in the other. An optional group is
305
+ * there when its host is, which `optional` alone decides, as it does the server's face's names.
312
306
  */
313
307
  const groupsOf = (on, bound) => {
314
308
  const groups = {
@@ -328,13 +322,16 @@ const createGitServer = (options) => {
328
322
  ...queueHost,
329
323
  core: on
330
324
  }, bound);
331
- if (topicsHost !== void 0 && queueHost !== void 0) groups.topics = topicCalls({
332
- ...topicsHost,
333
- queue: {
334
- ...queueHost,
325
+ if (topicsHost !== void 0) {
326
+ const queue = {
327
+ ...topicsHost.queue,
335
328
  core: on
336
- }
337
- }, bound);
329
+ };
330
+ groups.topics = topicCalls({
331
+ ...topicsHost,
332
+ queue
333
+ }, bound);
334
+ }
338
335
  return groups;
339
336
  };
340
337
  /** The calls deciding for `bound`, its tokens' too, or, with none, for the actor each input names. */
@@ -352,18 +349,201 @@ const createGitServer = (options) => {
352
349
  options: options.signIn,
353
350
  reports,
354
351
  tokens,
355
- runDecide: (effect) => {
356
- if (closed) return Promise.reject(closedError());
357
- return runOn(runtime, effect.pipe(Effect.mapError(decideRejection)), uncounted("sign-in's decide: its route sets no count"));
352
+ runDecide: async (effect) => {
353
+ const answer = await run(effect, uncounted("sign-in's decide: its route sets no count"), void 0);
354
+ if (answer.kind === "error") throw decideRejection(answer.error);
355
+ return answer.value;
358
356
  }
359
357
  });
358
+ const seamAuthorize = seamAuthorizer(core);
359
+ const redirect = renamedTo(core);
360
+ const { basePath } = wiring;
361
+ const parts = {
362
+ basePath,
363
+ core,
364
+ callsFor: (caller) => aroundCalls(guardedCalls(callsAs(caller), through), withAudienceMemo),
365
+ identify: (request) => through(identify(request)),
366
+ authorize: (input) => through(seamAuthorize(input)),
367
+ renamedTo: (caller, repo) => through(redirect(caller, repo)),
368
+ listen: (caller, repo, after) => through(openFeed({
369
+ core,
370
+ clock,
371
+ guard,
372
+ closing
373
+ }, caller, repo, after))
374
+ };
375
+ const httpOptions = { queue: queuePush(queueHost) };
376
+ if (options.resolve !== void 0) httpOptions.resolve = options.resolve;
377
+ if (options.maxDuration !== void 0) httpOptions.maxDuration = millis(options.maxDuration);
378
+ const unbound = Number.POSITIVE_INFINITY;
379
+ const boundOf = (most) => ({
380
+ bucket: most?.bucket ?? unbound,
381
+ subrequests: most?.subrequests ?? unbound
382
+ });
383
+ httpOptions.calls = boundOf(countedMost(objects) ?? callLimits);
384
+ httpOptions.pushCalls = boundOf(callLimits);
385
+ if (adapter.bucket !== void 0) httpOptions.lfs = lfsService(core, adapter.bucket);
386
+ const signed = adapter.bucket === void 0 ? void 0 : signedUploadOf(adapter.bucket);
387
+ const above = options.pushes?.upload.above;
388
+ if (signed !== void 0 && above !== void 0) httpOptions.uploads = pushUploads(core, signed, above);
389
+ const http = gitHttp(coreHttpParts(parts, httpOptions));
390
+ return {
391
+ parts,
392
+ adapter: {
393
+ store,
394
+ bucket: adapter.bucket,
395
+ clock: adapter.clock
396
+ },
397
+ calls: callsAs(),
398
+ callsAs,
399
+ hostCalls,
400
+ identify,
401
+ http,
402
+ signIn,
403
+ access: (caller, repo) => accessOf(reports, store, caller, repo),
404
+ grantAgain: (resolver, input) => grantAgain(reports, extensions.grants, resolver, input)
405
+ };
406
+ };
407
+ /** Whether `adapter` is several Layers. */
408
+ const isLayers = (adapter) => Array.isArray(adapter);
409
+ const signInPathSet = new Set(Object.values(signInPaths));
410
+ /**
411
+ * A Git server over one adapter: its calls, `identify`, and `fetch` and `route`, Git's Smart HTTP
412
+ * over its core and its sign-in, which answer `404` and `undefined` outside their URLs. Its
413
+ * runtime builds the adapter's Layers and Git's wiring once the tick that made it ends, which
414
+ * registers Git's jobs on the adapter's queue, and never runs them: the host starts them.
415
+ */
416
+ const createGitServer = (options) => {
417
+ const setup = options;
418
+ const engine = engineParts(setup.engine);
419
+ if (engine === void 0) throw new TypeError("options.engine must be one engine() made");
420
+ if (setup.signIn !== void 0 && setup.tokens === void 0) throw new TypeError("signIn mints tokens: pass tokens() too");
421
+ if (setup.access !== void 0 && setup.objects.indexed !== true) throw new TypeError("access hides refs, which needs objects that follow them: pass packs()");
422
+ if (setup.events !== void 0) checkDeliveryTimeout(eventDelivery(setup.events));
423
+ const postReceive = setup.hooks?.postReceive;
424
+ if (postReceive !== void 0) checkPostReceiveTimeout(postReceive);
425
+ const queueOf = setup.queue === void 0 ? void 0 : queueOptions(setup.queue);
426
+ if (queueOf === void 0) servedSources(setup.rules?.push ?? []);
427
+ if (setup.topics !== void 0 && queueOf === void 0) throw new TypeError("topics merge through each repo’s queue: pass queue too");
428
+ const fetchLimits = fetchLimitsOf(setup.fetchLimits);
429
+ const reports = {};
430
+ if (setup.onError !== void 0) reports.onError = setup.onError;
431
+ if (setup.onRefused !== void 0) reports.onRefused = setup.onRefused;
432
+ const extensions = openExtensions(queueOf !== void 0);
433
+ const basePath = normalBase(setup.basePath ?? "");
434
+ const life = serverLife();
435
+ const isClosed = () => life.admit("call") === "closed";
436
+ const guard = Effect.suspend(() => isClosed() ? Effect.die(closedError()) : Effect.void);
437
+ const door = {
438
+ refusal: () => bridgeRefusal(life),
439
+ closing: life.closing
440
+ };
441
+ const through = throughDoor(life);
442
+ const closingWith = (signal) => signal === void 0 ? life.closing : AbortSignal.any([signal, life.closing]);
443
+ const optional = {
444
+ queue: queueOf,
445
+ topics: setup.topics,
446
+ tokens: setup.tokens
447
+ };
448
+ const names = namesOf(optional);
449
+ const runCall = async (effect, crossing, signal) => {
450
+ if (isClosed()) throw closedError();
451
+ const exit = await exitOn(runtime, Effect.result(withAudienceMemo(effect)), crossing, closingWith(signal));
452
+ if (isClosed()) throw closedError();
453
+ const answer = Exit.isFailure(exit) ? Exit.failCause(Cause.map(exit.cause, (error) => unavailable(Cause.fail(error)))) : EffectResult.isSuccess(exit.value) ? Exit.succeed(exit.value.success) : Exit.fail(exit.value.failure);
454
+ return answerOf(answer, signal);
455
+ };
456
+ const wiring = Layer.effect(Wired, Effect.gen(function* () {
457
+ const records = yield* Records.Records;
458
+ const queue = yield* JobQueue.JobQueue;
459
+ const callLimits = yield* CallLimits.CallLimits;
460
+ const bucket = yield* Effect.serviceOption(Bucket.Bucket);
461
+ const links = yield* Effect.serviceOption(BucketLinks.BucketLinks);
462
+ const uploads = yield* Effect.serviceOption(BucketUploads.BucketUploads);
463
+ const clock = yield* Effect.clockWith(Effect.succeed);
464
+ const context = yield* Effect.context();
465
+ const adapter = {
466
+ store: promisesStore(records, context, door),
467
+ jobs: promisesQueue(queue, context, door),
468
+ bucket: Option.isSome(bucket) ? promisesBucket(bucket.value, links, uploads, context, door) : void 0,
469
+ clock: promisesClock(clock, context, door),
470
+ callLimits
471
+ };
472
+ const given = {
473
+ adapter,
474
+ engine,
475
+ optional,
476
+ fetchLimits,
477
+ reports,
478
+ extensions,
479
+ basePath
480
+ };
481
+ return yield* Effect.sync(() => {
482
+ const wired = wireServer(setup, {
483
+ ...given,
484
+ run: runCall,
485
+ guard,
486
+ closing: life.closing,
487
+ through,
488
+ runtime
489
+ });
490
+ for (const bind of extensions.binds) bind(wired);
491
+ adapter.jobs.register(...extensions.added);
492
+ return wired;
493
+ });
494
+ }));
495
+ const { adapter } = setup;
496
+ const host = isLayers(adapter) ? Layer.mergeAll(Layer.empty, ...adapter) : adapter;
497
+ const runtime = ManagedRuntime.make(Layer.suspend(() => {
498
+ life.move({ kind: "building" });
499
+ return Layer.provideMerge(wiring, host);
500
+ }).pipe(Layer.tap((built) => Effect.sync(() => {
501
+ life.move({
502
+ kind: "built",
503
+ wired: Context.get(built, Wired)
504
+ });
505
+ })), Layer.tapCause((cause) => life.move({
506
+ kind: "failed",
507
+ cause
508
+ }) ? Effect.logError("The Git server failed to start", cause) : Effect.void)));
509
+ /**
510
+ * The server's build as the door answers it, every call's way in: built, or failed, starting or
511
+ * joining the build while it isn't settled. A closed server throws, before the build or after.
512
+ */
513
+ const enter = async () => {
514
+ const settled = () => {
515
+ const admitted = life.admit("call");
516
+ if (admitted === "closed") throw closedError();
517
+ const now = life.now();
518
+ if (admitted === "run" && now.kind === "built") return Exit.succeed(now.wired);
519
+ return admitted === "unavailable" && now.kind === "failed" ? Exit.failCause(now.cause) : void 0;
520
+ };
521
+ const before = settled();
522
+ if (before !== void 0) return before;
523
+ const exit = await exitOn(runtime, Effect.service(Wired), noCall);
524
+ return settled() ?? exit;
525
+ };
526
+ const wired = Effect.flatMap(Effect.promise(enter), (exit) => Exit.isSuccess(exit) ? Effect.succeed(exit.value) : Effect.fail(unavailable(exit.cause)));
527
+ /** `use`'s Effect over the built server, its `Result`, or `UNAVAILABLE` once the build failed. */
528
+ const run = async (use, crossing, signal) => {
529
+ const built = await enter();
530
+ if (Exit.isFailure(built)) return {
531
+ kind: "error",
532
+ error: unavailable(built.cause)
533
+ };
534
+ return runCall(use(built.value), crossing, signal);
535
+ };
536
+ /** The calls over the built server's `calls`, made before it's built. */
537
+ const callsOver = (calls) => lazyGroups(names, (group, name, args) => run((built) => callOf(calls(built), group, name)(...args), handedScope(args.at(-1)), signalOf(args)));
360
538
  /**
361
539
  * `answering`'s answer to `request`. A client that went away, by its signal or mid-body, gets an
362
540
  * empty 499, as Effect's HTTP server answers one, logged once at info with the path alone: an
363
541
  * archive's link carries its token in the query. Any other interrupt throws, as it did before.
364
542
  */
365
543
  const answerTo = async (request, answering) => {
366
- const exit = await exitOn(runtime, withAudienceMemo(answering), uncounted("a request: each Git route sets its own count, and sign-in's and the event stream's none"), request.signal);
544
+ if (isClosed()) throw closedError();
545
+ const exit = await exitOn(runtime, withAudienceMemo(answering), uncounted("a request: each Git route sets its own count, and sign-in's and the event stream's none"), closingWith(request.signal));
546
+ if (isClosed()) throw closedError();
367
547
  if (Exit.isSuccess(exit)) return exit.value;
368
548
  if (!Cause.hasInterruptsOnly(exit.cause) || !clientWentAway(request)) throw Cause.squash(exit.cause);
369
549
  const path = new URL(request.url).pathname;
@@ -375,65 +555,61 @@ const createGitServer = (options) => {
375
555
  };
376
556
  /** Git's HTTP answer to `request`, else sign-in's, or `undefined` outside both's URLs. */
377
557
  const route = async (request) => {
378
- if (closed) throw closedError();
379
- started();
558
+ const built = await enter();
559
+ if (Exit.isFailure(built)) {
560
+ const url = new URL(request.url);
561
+ return gitUrlOf(url, basePath) !== void 0 || setup.signIn !== void 0 && signInPathSet.has(url.pathname) ? refusal(unavailable(built.cause)) : void 0;
562
+ }
563
+ const { http, signIn } = built.value;
380
564
  const answered = await answerTo(request, http.route(request));
381
565
  if (answered !== void 0 || signIn === void 0) return answered;
382
566
  return answerTo(request, signIn(request));
383
567
  };
384
568
  const server = {
385
- ...promiseCalls(callsAs(), run),
569
+ ...callsOver((built) => built.calls),
386
570
  fetch: async (request) => await route(request) ?? new Response("Not found", { status: 404 }),
387
571
  route,
388
- identify: (request) => run(identify(request), uncounted("a host's identify: no Git request"), request.signal),
572
+ identify: (request) => run((built) => built.identify(request), uncounted("a host's identify: no Git request"), request.signal),
573
+ runtime,
389
574
  close: async () => {
390
- closed = true;
575
+ life.move({ kind: "closed" });
391
576
  await runtime.dispose();
392
577
  }
393
578
  };
394
- const seamAuthorize = seamAuthorizer(core);
395
- const redirect = renamedTo(core);
396
- const basePath = normalBase(options.basePath ?? "");
397
- const effectFace = (calls) => aroundCalls(guardedCalls(calls, guard), withAudienceMemo);
398
579
  attachExtensions(server, {
399
580
  extensions,
581
+ admit: life.admit,
400
582
  basePath,
401
- guard,
402
- callsFor: (caller) => promiseCalls(callsAs(caller), run),
403
- hostCalls: promiseCalls(hostCalls, run),
404
- effectHostCalls: effectFace(hostCalls),
405
- access: (caller, repo) => accessOf(reports, store, caller, repo),
406
- grantAgain: (resolver, input) => grantAgain(reports, extensions.grants, resolver, input)
583
+ built: wired,
584
+ ready: async () => {
585
+ const built = await enter();
586
+ return Exit.isFailure(built) ? {
587
+ kind: "error",
588
+ error: unavailable(built.cause)
589
+ } : {
590
+ kind: "success",
591
+ value: void 0
592
+ };
593
+ },
594
+ callsFor: (caller) => {
595
+ let calls;
596
+ return callsOver((built) => calls ??= built.callsAs(caller));
597
+ },
598
+ hostCalls: callsOver((built) => built.hostCalls),
599
+ effectHostCalls: aroundCalls(guardedCalls(lazyGroups(names, (group, name, args) => Effect.flatMap(wired, (built) => callOf(built.hostCalls, group, name)(...args))), through), withAudienceMemo),
600
+ through,
601
+ access: (caller, repo) => Effect.flatMap(wired, (built) => built.access(caller, repo)),
602
+ grantAgain: (resolver, input) => Effect.flatMap(wired, (built) => built.grantAgain(resolver, input))
407
603
  });
408
- const parts = {
604
+ attachParts(server, {
409
605
  basePath,
410
- core,
411
- callsFor: (caller) => effectFace(callsAs(caller)),
412
- identify: (request) => Effect.andThen(guard, identify(request)),
413
- authorize: (input) => Effect.andThen(guard, seamAuthorize(input)),
414
- renamedTo: (caller, repo) => Effect.andThen(guard, redirect(caller, repo)),
415
- listen: (caller, repo, after) => openFeed({
416
- core,
417
- clock,
418
- guard
419
- }, caller, repo, after)
420
- };
421
- attachParts(server, parts);
422
- const httpOptions = { queue: queuePush(queueHost) };
423
- if (options.resolve !== void 0) httpOptions.resolve = options.resolve;
424
- if (adapter.maxDuration !== void 0) httpOptions.maxDuration = millis(adapter.maxDuration);
425
- const unbound = Number.POSITIVE_INFINITY;
426
- const boundOf = (most) => ({
427
- bucket: most?.bucket ?? unbound,
428
- subrequests: most?.subrequests ?? unbound
606
+ names,
607
+ door,
608
+ parts: Effect.map(wired, (built) => built.parts)
609
+ });
610
+ queueMicrotask(() => {
611
+ if (life.admit("join") === "join") exitOn(runtime, Effect.void, noCall);
429
612
  });
430
- httpOptions.calls = boundOf(countedMost(objects) ?? callLimits);
431
- httpOptions.pushCalls = boundOf(callLimits);
432
- if (adapter.bucket !== void 0) httpOptions.lfs = lfsService(core, adapter.bucket);
433
- const signed = adapter.bucket === void 0 ? void 0 : signedUploadOf(adapter.bucket);
434
- const above = options.pushes?.upload.above;
435
- if (signed !== void 0 && above !== void 0) httpOptions.uploads = pushUploads(core, signed, above);
436
- const http = gitHttp(coreHttpParts(parts, httpOptions));
437
613
  return server;
438
614
  };
439
615
  export { createGitServer, requestCallsBefore };
@@ -1,8 +1,8 @@
1
+ import "../contracts/promises.mjs";
1
2
  import "../contracts/signin.mjs";
2
3
  import "../auth/host-function.mjs";
3
4
  import "../auth/tokens.mjs";
4
5
  import "./grants.mjs";
5
- import "../contracts/promises.mjs";
6
6
  import { Moment } from "@ref0/infra";
7
7
  import "effect/Effect";
8
8
  /** `gh`'s OAuth client ID, which the device flow takes beside the registered clients. */
@@ -1,5 +1,5 @@
1
- import "../contracts/tokens.mjs";
2
1
  import "../contracts/promises.mjs";
2
+ import "../contracts/tokens.mjs";
3
3
  import { Moment } from "@ref0/infra";
4
4
  import "effect/Effect";
5
5
  import "effect/Schema";
@@ -0,0 +1,41 @@
1
+ // src/storage/bits.ts
2
+ /** Bitsets of positions, a bit a position in 32-bit words, as the index's walks and send list keep them. */
3
+ const isSet = (bits, at) => ((bits[at >>> 5] ?? 0) >>> (at & 31) & 1) === 1;
4
+ const setBit = (bits, at) => {
5
+ bits[at >>> 5] = ((bits[at >>> 5] ?? 0) | 1 << (at & 31)) >>> 0;
6
+ };
7
+ const clearBit = (bits, at) => {
8
+ bits[at >>> 5] = ((bits[at >>> 5] ?? 0) & ~(1 << (at & 31))) >>> 0;
9
+ };
10
+ const bitCount = (word) => {
11
+ let bits = word - (word >>> 1 & 1431655765);
12
+ bits = (bits & 858993459) + (bits >>> 2 & 858993459);
13
+ return (bits + (bits >>> 4) & 252645135) * 16843009 >>> 24;
14
+ };
15
+ /** Whether any bit of `bits` in `[from, to)` is set. */
16
+ const anySet = (bits, from, to) => {
17
+ if (from >= to) return false;
18
+ const first = from >>> 5;
19
+ const last = to - 1 >>> 5;
20
+ for (let at = first; at <= last; at += 1) {
21
+ let word = bits[at] ?? 0;
22
+ if (at === first) word &= -1 << (from & 31);
23
+ if (at === last && (to & 31) !== 0) word &= (1 << (to & 31)) - 1;
24
+ if (word !== 0) return true;
25
+ }
26
+ return false;
27
+ };
28
+ /** The first set bit of `bits` at or past `from` and before `to`, or `to` when there's none. */
29
+ const nextSet = (bits, from, to) => {
30
+ for (let at = from; at < to;) {
31
+ const word = (bits[at >>> 5] ?? 0) >>> (at & 31);
32
+ if (word === 0) {
33
+ at = ((at >>> 5) + 1) * 32;
34
+ continue;
35
+ }
36
+ const next = at + 31 - Math.clz32(word & -word);
37
+ return next < to ? next : to;
38
+ }
39
+ return to;
40
+ };
41
+ export { anySet, bitCount, clearBit, isSet, nextSet, setBit };
@@ -1,9 +1,9 @@
1
+ import "../contracts/promises.mjs";
1
2
  import "../contracts/services.mjs";
2
3
  import "../contracts/objects.mjs";
3
4
  import { GitError } from "../contracts/result.mjs";
4
5
  import "../contracts/store.mjs";
5
6
  import "../store/call-count.mjs";
6
- import "../contracts/promises.mjs";
7
7
  import { PutOptions } from "@ref0/infra";
8
8
  import * as Effect from "effect/Effect";
9
9
  import "effect/Scope";
@@ -1,8 +1,8 @@
1
1
  import { allotmentWithin, objectsReadMost } from "../contracts/objects.mjs";
2
2
  import { callScope, chargeCall, chargeCalls, handedOn, handedScope, inCallScope } from "../store/call-count.mjs";
3
+ import { tryClosing } from "../store/closed.mjs";
3
4
  import { resultIn, runIn } from "../store/crossing.mjs";
4
5
  import { fromInfra, gitError, resultCall } from "../store/errors.mjs";
5
- import { tryUnlessSpent } from "../contracts/calls-spent.mjs";
6
6
  import { isInfraError } from "@ref0/infra";
7
7
  import * as Effect from "effect/Effect";
8
8
  import * as Scope$1 from "effect/Scope";
@@ -191,7 +191,7 @@ const bucketCall = (run) => Effect.andThen(chargeCall("bucket"), bucketTry(run))
191
191
  * and its end, whose calls `bucketStart`'s charges counted first. Never a call of its own. `run`
192
192
  * is handed the fiber's signal, as `bucketCall`'s is.
193
193
  */
194
- const bucketTry = (run) => tryUnlessSpent(run, (cause) => isInfraError(cause) ? fromInfra(cause) : gitError("UNAVAILABLE", "The bucket failed", {
194
+ const bucketTry = (run) => tryClosing(run, (cause) => isInfraError(cause) ? fromInfra(cause) : gitError("UNAVAILABLE", "The bucket failed", {
195
195
  cause,
196
196
  hint: "Try again"
197
197
  }));
@@ -7,7 +7,9 @@ import "../contracts/calls-spent.mjs";
7
7
  import "../store/call-count.mjs";
8
8
  import "./object-rows.mjs";
9
9
  import "./pack-reads.mjs";
10
+ import "./send-list.mjs";
10
11
  import "./index-fetch.mjs";
11
12
  import "./shallow.mjs";
12
13
  import "./walk.mjs";
13
- import "effect/Effect";
14
+ import "effect/Effect";
15
+ import "effect/Types";