@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,7 +1,8 @@
1
+ import { millis } from "../contracts/promises.mjs";
1
2
  import { decodeRow, storeCall } from "../store/errors.mjs";
2
3
  import { collections, newChangeId, repoScope } from "../store/scopes.mjs";
3
4
  import { bucketCall } from "./calls.mjs";
4
- import { limits, millis } from "@ref0/infra";
5
+ import { limits } from "@ref0/infra";
5
6
  import * as Effect from "effect/Effect";
6
7
  import * as Schema from "effect/Schema";
7
8
  // src/storage/push-uploads.ts
@@ -1,11 +1,11 @@
1
1
  import "../index.mjs";
2
+ import "../contracts/promises.mjs";
2
3
  import "../contracts/objects.mjs";
3
4
  import "../contracts/services.mjs";
4
5
  import "../contracts/store.mjs";
5
6
  import "./fetch.mjs";
6
7
  import "./walk.mjs";
7
- import "../contracts/promises.mjs";
8
- import { Duration, Key, Moment } from "@ref0/infra";
8
+ import { Key, Moment } from "@ref0/infra";
9
9
  import "effect/Effect";
10
10
  import "effect/Schema";
11
11
  /**
@@ -45,7 +45,7 @@ const receiveLinkLimits = {
45
45
  lookupsEach: 4
46
46
  };
47
47
  /** The refusal of a push whose links asked of the repo `lookups` times, past `most`. */
48
- const tooManyLookups = (lookups, objects, most) => gitError("RESOURCE_EXHAUSTED", `push's trees link about ${objects.toLocaleString("en-US")} objects outside the pack ${lookups.toLocaleString("en-US")} times; packs() storage looks them up at most ${most.toLocaleString("en-US")} times`, {
48
+ const tooManyLookups = (lookups, objects, most) => gitError("RESOURCE_EXHAUSTED", `push's trees link about ${objects.toLocaleString("en-US")} objects outside the pack ${lookups.toLocaleString("en-US")} times; this server looks them up at most ${most.toLocaleString("en-US")} times`, {
49
49
  hint: "Push fewer commits at a time",
50
50
  context: {
51
51
  reason: "too-many-repeated-files",
@@ -1,6 +1,7 @@
1
1
  import "../index.mjs";
2
2
  import "./pack-reads.mjs";
3
3
  import "../protocol.mjs";
4
+ import "../contracts/promises.mjs";
4
5
  import "../contracts/objects.mjs";
5
6
  import { RepoStats } from "../contracts/repos.mjs";
6
7
  import "../contracts/services.mjs";
@@ -8,8 +9,7 @@ import "../contracts/store.mjs";
8
9
  import "./index-remap.mjs";
9
10
  import "./object-rows.mjs";
10
11
  import "./rows.mjs";
11
- import "../contracts/promises.mjs";
12
- import { Duration, Key, Moment, Row } from "@ref0/infra";
12
+ import { Key, Moment, Row } from "@ref0/infra";
13
13
  import "effect/Effect";
14
14
  import "effect/Types";
15
15
  /**
@@ -1,3 +1,4 @@
1
- import { Duration, Key, Moment, Row } from "@ref0/infra";
1
+ import "../contracts/promises.mjs";
2
+ import { Key, Moment, Row } from "@ref0/infra";
2
3
  import "effect/Effect";
3
4
  import "effect/Schema";
@@ -1,6 +1,6 @@
1
+ import { millis } from "../contracts/promises.mjs";
1
2
  import { decodeRow, fail } from "../store/errors.mjs";
2
3
  import { isPackId, objectCollections } from "./layout.mjs";
3
- import { millis } from "@ref0/infra";
4
4
  import * as Effect from "effect/Effect";
5
5
  import * as Schema from "effect/Schema";
6
6
  // src/storage/rows.ts
@@ -0,0 +1,8 @@
1
+ import "../contracts/promises.mjs";
2
+ import "../contracts/store.mjs";
3
+ import "./fetch.mjs";
4
+ import "./index-build.mjs";
5
+ import "./pack-reads.mjs";
6
+ import "./pack-ranges.mjs";
7
+ import "./walk.mjs";
8
+ import "effect/Effect";
@@ -0,0 +1,319 @@
1
+ import { gitError } from "../store/errors.mjs";
2
+ import { anySet, clearBit, isSet, nextSet, setBit } from "./bits.mjs";
3
+ import { bitmapWords } from "./ewah.mjs";
4
+ import { basesBlock, openBases } from "./index-files.mjs";
5
+ import { indexFiles } from "./layout.mjs";
6
+ import * as Effect from "effect/Effect";
7
+ // src/storage/send-list.ts
8
+ /** A pack's entry at `position`: its ID, offset, CRC-32 and length, from the loaded index alone. */
9
+ const entryAt = (pack, position) => {
10
+ const { index, reverse, pack: stored } = pack.indexed;
11
+ const place = position - pack.base;
12
+ const { oid, offset, crc32 } = index.object(reverse.rankAt(place));
13
+ return {
14
+ oid,
15
+ pack: stored,
16
+ offset,
17
+ bytes: (place + 1 < pack.count ? index.offsetAt(reverse.rankAt(place + 1)) : stored.bytes - 20) - offset,
18
+ crc32
19
+ };
20
+ };
21
+ /** The size of the list's `at`th object. */
22
+ const sizeOf = (list, at) => list.large.get(at) ?? list.sizes[at] ?? 0;
23
+ /** The list's objects in order, from its `ordinal`th: each one's place, position and pack. */
24
+ var ListCursor = class ListCursor {
25
+ list;
26
+ ordinal = 0;
27
+ position = 0;
28
+ pack = 0;
29
+ constructor(list, from) {
30
+ this.list = list;
31
+ if (from === void 0) this.#seek(0);
32
+ else ({ordinal: this.ordinal, position: this.position, pack: this.pack} = from);
33
+ }
34
+ /** Another cursor at the same object. */
35
+ copy() {
36
+ return new ListCursor(this.list, this);
37
+ }
38
+ get done() {
39
+ return this.ordinal >= this.list.count;
40
+ }
41
+ /** The pack the object is in. */
42
+ get inPack() {
43
+ const pack = this.list.packs[this.pack];
44
+ if (pack === void 0) throw new Error(`no pack holds position ${this.position}`);
45
+ return pack;
46
+ }
47
+ next() {
48
+ this.ordinal += 1;
49
+ this.#seek(this.position + 1);
50
+ }
51
+ #seek(from) {
52
+ const { packs, send } = this.list;
53
+ for (let at = from; this.pack < packs.length;) {
54
+ const pack = packs[this.pack];
55
+ if (pack === void 0) break;
56
+ const found = nextSet(send, Math.max(at, pack.base), pack.base + pack.count);
57
+ if (found < pack.base + pack.count) {
58
+ this.position = found;
59
+ return;
60
+ }
61
+ this.pack += 1;
62
+ at = found;
63
+ }
64
+ this.position = Number.POSITIVE_INFINITY;
65
+ }
66
+ };
67
+ /** The entries `sizesAt` sizes at once. */
68
+ const sizedAtOnce = 1024;
69
+ /** The most `.bases` blocks one read takes: 65,536 positions, 512 KiB of rows at most. */
70
+ const basesRun = 16;
71
+ /**
72
+ * What a run of `.bases` rows holds as it's read, charged once before the first: its bytes, the
73
+ * pairs `rowsOf` builds from them and their array, about 2 MiB measured, with the last run's still
74
+ * held as the next is read.
75
+ */
76
+ const basesRunBytes = basesRun * basesBlock * 8 * 6;
77
+ /** What a chain's step holds: its entry in `chains`. */
78
+ const chainStepBytes = 64;
79
+ const loss = (message) => gitError("DATA_LOSS", message);
80
+ /** The runs of `.bases` blocks in `[from, to)` that hold a position of `wanted`, as read ranges. */
81
+ const basesRuns = (wanted, from, to) => {
82
+ const runs = [];
83
+ for (let block = Math.floor(from / basesBlock); block * basesBlock < to; block += 1) {
84
+ if (!wanted(block)) continue;
85
+ const start = Math.max(from, block * basesBlock);
86
+ const end = Math.min(to, (block + 1) * basesBlock);
87
+ const last = runs.at(-1);
88
+ if (last !== void 0 && last.to === start && end - last.from <= basesRun * 4096) last.to = end;
89
+ else runs.push({
90
+ from: start,
91
+ to: end
92
+ });
93
+ }
94
+ return runs;
95
+ };
96
+ /**
97
+ * The send list of `input.send`'s pack positions: sized through the reader's blocks by place,
98
+ * `blob:limit`'s too-large blobs left out, each delta found in its layer's `.bases` and copied
99
+ * when its base is sent, and its bytes. Each `.bases` read is checked against the fetch's calls
100
+ * before it's made, as the walk checks its levels.
101
+ */
102
+ const listOf = Effect.fnUntraced(function* (input) {
103
+ const { send, kinds, read, ahead } = input;
104
+ const checked = ahead({
105
+ bucket: 0,
106
+ store: 0
107
+ });
108
+ const indexed = read.indexed;
109
+ const sizesAt = read.sizesAt;
110
+ if (indexed === void 0 || sizesAt === void 0) return yield* Effect.die(/* @__PURE__ */ new Error("a send list needs the reader to have loaded its packs"));
111
+ const packs = [];
112
+ for (const ranges of input.ranges) {
113
+ if (!anySet(send, ranges.base, ranges.base + ranges.count)) continue;
114
+ const pack = input.listed.packs.find(({ id }) => id === ranges.pack.id);
115
+ if (pack === void 0) return yield* Effect.fail(loss(`Pack ${ranges.pack.id} isn't listed`));
116
+ packs.push({
117
+ base: ranges.base,
118
+ count: ranges.count,
119
+ indexed: yield* indexed(pack)
120
+ });
121
+ }
122
+ let count = input.count;
123
+ const sizes = new Uint32Array(count);
124
+ const large = /* @__PURE__ */ new Map();
125
+ const dropped = new Uint32Array(bitmapWords(count));
126
+ let dropping = false;
127
+ let ordinal = 0;
128
+ for (const pack of packs) {
129
+ const end = pack.base + pack.count;
130
+ for (let at = nextSet(send, pack.base, end); at < end;) {
131
+ const batch = [];
132
+ for (; at < end && batch.length < sizedAtOnce; at = nextSet(send, at + 1, end)) {
133
+ const { oid, offset } = entryAt(pack, at);
134
+ batch.push({
135
+ oid,
136
+ offset
137
+ });
138
+ }
139
+ yield* checked;
140
+ const sized = yield* sizesAt(pack.indexed, batch);
141
+ for (let slot = 0; slot < batch.length; slot += 1) {
142
+ const size = sized[slot] ?? 0;
143
+ if (kinds[ordinal] === 2 && input.blobsUnder !== void 0 && size >= input.blobsUnder) {
144
+ setBit(dropped, ordinal);
145
+ dropping = true;
146
+ }
147
+ if (size > 4294967295) large.set(ordinal, size);
148
+ else sizes[ordinal] = size;
149
+ ordinal += 1;
150
+ }
151
+ }
152
+ }
153
+ if (dropping) {
154
+ const out = new ListCursor({
155
+ ...emptyList(input, packs),
156
+ sizes,
157
+ large
158
+ });
159
+ let kept = 0;
160
+ const moved = /* @__PURE__ */ new Map();
161
+ for (; !out.done; out.next()) {
162
+ if (isSet(dropped, out.ordinal)) {
163
+ clearBit(send, out.position);
164
+ continue;
165
+ }
166
+ kinds[kept] = kinds[out.ordinal] ?? 0;
167
+ const size = large.get(out.ordinal);
168
+ if (size === void 0) sizes[kept] = sizes[out.ordinal] ?? 0;
169
+ else moved.set(kept, size);
170
+ kept += 1;
171
+ }
172
+ large.clear();
173
+ for (const [at, size] of moved) large.set(at, size);
174
+ count = kept;
175
+ }
176
+ const listed = {
177
+ ...emptyList({
178
+ ...input,
179
+ count
180
+ }, packs),
181
+ sizes,
182
+ large
183
+ };
184
+ const { deltas, copied, bases, chains } = yield* copiesOf(input, listed);
185
+ const list = {
186
+ ...listed,
187
+ deltas,
188
+ copied,
189
+ bases,
190
+ chains
191
+ };
192
+ let bytes = 0;
193
+ for (const cursor = new ListCursor(list); !cursor.done; cursor.next()) bytes += isSet(copied, cursor.ordinal) ? entryAt(cursor.inPack, cursor.position).bytes : sizeOf(list, cursor.ordinal);
194
+ return {
195
+ ...list,
196
+ bytes
197
+ };
198
+ });
199
+ /** A list of `input`'s send set over `packs`, with nothing known of its entries yet. */
200
+ const emptyList = (input, packs) => {
201
+ const words = bitmapWords(input.count);
202
+ return {
203
+ send: input.send,
204
+ count: input.count,
205
+ kinds: input.kinds,
206
+ sizes: /* @__PURE__ */ new Uint32Array(0),
207
+ large: /* @__PURE__ */ new Map(),
208
+ deltas: new Uint32Array(words),
209
+ copied: new Uint32Array(words),
210
+ bases: /* @__PURE__ */ new Uint32Array(0),
211
+ packs,
212
+ chains: /* @__PURE__ */ new Map(),
213
+ bytes: 0
214
+ };
215
+ };
216
+ /**
217
+ * Which of `list`'s objects are deltas and which are copied, from each layer's `.bases`, and each
218
+ * written-whole delta's chain of bases down to a whole entry.
219
+ */
220
+ const copiesOf = Effect.fnUntraced(function* (input, list) {
221
+ const { send, read, ahead } = input;
222
+ const deltas = new Uint32Array(bitmapWords(list.count));
223
+ const copied = new Uint32Array(bitmapWords(list.count));
224
+ const bases = new Uint32Array(list.count);
225
+ const chains = /* @__PURE__ */ new Map();
226
+ const opened = /* @__PURE__ */ new Map();
227
+ const packOf = (position) => list.packs.find((pack) => position >= pack.base && position < pack.base + pack.count);
228
+ yield* read.hold(basesRunBytes);
229
+ const basesOf = Effect.fnUntraced(function* (layer) {
230
+ const known = opened.get(layer);
231
+ if (known !== void 0) return known;
232
+ yield* ahead({
233
+ bucket: 2,
234
+ store: 0
235
+ });
236
+ const key = indexFiles(input.listed.repo, layer.build).bases;
237
+ const bases = yield* openBases(input.bucket, key, layer.first + layer.positions);
238
+ opened.set(layer, bases);
239
+ return bases;
240
+ });
241
+ /** A base checked: before its delta, in the same pack. */
242
+ const baseOf = (position, base) => {
243
+ const pack = packOf(position);
244
+ return pack === void 0 || base >= position || base < pack.base ? Effect.fail(loss(`The index's .bases names ${base} as ${position}'s base, not before it in its pack`)) : Effect.succeed(base);
245
+ };
246
+ let pending = [];
247
+ const cursor = new ListCursor(list);
248
+ for (const layer of input.layers) {
249
+ const end = layer.first + layer.positions;
250
+ if (cursor.done || cursor.position >= end) continue;
251
+ const runs = layer.deltas === 0 ? [] : basesRuns((block) => anySet(send, block * basesBlock, (block + 1) * basesBlock), layer.first, end);
252
+ let rows = /* @__PURE__ */ new Uint32Array(0);
253
+ let row = 0;
254
+ let run = 0;
255
+ let readRun = -1;
256
+ for (; !cursor.done && cursor.position < end; cursor.next()) {
257
+ const { position, ordinal } = cursor;
258
+ while (run < runs.length && (runs[run]?.to ?? 0) <= position) run += 1;
259
+ const reading = runs[run];
260
+ if (reading !== void 0 && reading.from <= position && run !== readRun) {
261
+ const bases = yield* basesOf(layer);
262
+ yield* ahead({
263
+ bucket: 1,
264
+ store: 0
265
+ });
266
+ rows = yield* bases.rowsOf(reading.from, reading.to);
267
+ row = 0;
268
+ readRun = run;
269
+ }
270
+ while (row < rows.length && (rows[row] ?? 0) < position) row += 2;
271
+ if (row < rows.length && rows[row] === position) {
272
+ const base = yield* baseOf(position, rows[row + 1] ?? 0);
273
+ setBit(deltas, ordinal);
274
+ if (isSet(send, base)) {
275
+ setBit(copied, ordinal);
276
+ bases[ordinal] = base;
277
+ } else {
278
+ yield* read.hold(chainStepBytes);
279
+ chains.set(position, base);
280
+ pending.push(base);
281
+ }
282
+ } else setBit(copied, ordinal);
283
+ }
284
+ }
285
+ while (pending.length > 0) {
286
+ const asked = [...new Set(pending)].filter((base) => !chains.has(base)).sort((a, b) => a - b);
287
+ pending = [];
288
+ for (const layer of input.layers) {
289
+ const end = layer.first + layer.positions;
290
+ const inLayer = asked.filter((base) => base >= layer.first && base < end);
291
+ if (inLayer.length === 0 || layer.deltas === 0) continue;
292
+ const blocks = new Set(inLayer.map((base) => Math.floor(base / basesBlock)));
293
+ const bases = yield* basesOf(layer);
294
+ for (const reading of basesRuns((block) => blocks.has(block), layer.first, end)) {
295
+ yield* ahead({
296
+ bucket: 1,
297
+ store: 0
298
+ });
299
+ const rows = yield* bases.rowsOf(reading.from, reading.to);
300
+ const wanted = new Set(inLayer.filter((base) => base >= reading.from && base < reading.to));
301
+ for (let row = 0; row < rows.length; row += 2) {
302
+ const position = rows[row] ?? 0;
303
+ if (!wanted.has(position)) continue;
304
+ const base = yield* baseOf(position, rows[row + 1] ?? 0);
305
+ yield* read.hold(chainStepBytes);
306
+ chains.set(position, base);
307
+ pending.push(base);
308
+ }
309
+ }
310
+ }
311
+ }
312
+ return {
313
+ deltas,
314
+ copied,
315
+ bases,
316
+ chains
317
+ };
318
+ });
319
+ export { ListCursor, entryAt, listOf, sizeOf };
@@ -1,5 +1,4 @@
1
1
  import "../index.mjs";
2
- import "./pack-reads.mjs";
3
- import "../contracts/objects.mjs";
4
2
  import "./walk.mjs";
3
+ import "../contracts/objects.mjs";
5
4
  import "effect/Effect";
@@ -18,17 +18,19 @@ const commitBytes = 8388608;
18
18
  /** What the reader may hold besides the entries it hands on: its inflater and buffers. */
19
19
  const readerBytes = 1048576;
20
20
  const tooLarge = (bytes, most) => gitError("RESOURCE_EXHAUSTED", `push takes ${bytes.toLocaleString("en-US")} bytes as rows; one commit holds ${most.toLocaleString("en-US")}`, {
21
- hint: "Use packs() storage, or push fewer objects at a time",
21
+ hint: "Push fewer objects at a time",
22
+ hostHint: "Use packs() storage, which keeps a push's objects in the bucket",
22
23
  context: { reason: "too-large" }
23
24
  });
24
25
  /** A push whose objects, resolved whole, pass a commit before their rows are made: as `tooLarge`. */
25
26
  const resolvedTooLarge = (bytes, most) => gitError("RESOURCE_EXHAUSTED", `push's objects take more than ${bytes.toLocaleString("en-US")} bytes whole; one commit holds ${most.toLocaleString("en-US")}`, {
26
- hint: "Use packs() storage, or push fewer objects at a time",
27
+ hint: "Push fewer objects at a time",
28
+ hostHint: "Use packs() storage, which keeps a push's objects in the bucket",
27
29
  context: { reason: "too-large" }
28
30
  });
29
31
  /** An object whose row passes one row's 1 MiB: `RESOURCE_EXHAUSTED`, reason `object-too-large`. */
30
32
  const rowTooLarge = (oid, bytes) => gitError("RESOURCE_EXHAUSTED", `object ${oid} takes ${bytes.toLocaleString("en-US")} bytes as a row; one row holds ${limits.valueBytes.toLocaleString("en-US")}`, {
31
- hint: "Use packs() storage, which keeps a large object in the bucket",
33
+ hostHint: "Use packs() storage, which keeps a large object in the bucket",
32
34
  context: { reason: "object-too-large" }
33
35
  });
34
36
  const malformedPack = (message) => gitError("INVALID_ARGUMENT", `The pack is malformed: ${message}`, { hint: "Push again" });
@@ -4,4 +4,5 @@ import "../protocol/bytes/errors.mjs";
4
4
  import "./object-rows.mjs";
5
5
  import "./pack-reads.mjs";
6
6
  import "effect/Effect";
7
- import "effect/Result";
7
+ import "effect/Result";
8
+ import "effect/Types";
@@ -84,7 +84,7 @@ const tooManyReads = (readsMost) => gitError("RESOURCE_EXHAUSTED", `This fetch's
84
84
  * held, each charged less `fetchIdBytes` while the tree held its string: their strings move out of
85
85
  * what's paid for trees and into what's paid for them, so each string is counted once.
86
86
  */
87
- const countedReads = (reads, readsMost, charge, calls) => {
87
+ const countedReads = (reads, readsMost, charge, calls, release) => {
88
88
  let counted = 0;
89
89
  let entryBytesHeld = 0;
90
90
  let entryBytesPaid = 0;
@@ -123,16 +123,22 @@ const countedReads = (reads, readsMost, charge, calls) => {
123
123
  yield* count(oids.length, charged);
124
124
  return yield* reads.sizes(oids);
125
125
  });
126
- return Object.assign(read, reads.indexed === void 0 ? {
127
- sizes,
128
- hold,
129
- letGo
130
- } : {
126
+ /** Gives back `bytes` that `hold` charged, once what they paid for is let go. */
127
+ const given = (bytes) => release === void 0 ? Effect.void : release(bytes);
128
+ /** Gives back what `read` charged for `objects` it read, once a caller has let each go. */
129
+ const letGoReads = (objects) => given(objects * 530);
130
+ const placed = {};
131
+ if (reads.indexed !== void 0) placed.indexed = reads.indexed;
132
+ if (reads.heldAt !== void 0) placed.heldAt = reads.heldAt;
133
+ const { sizesAt } = reads;
134
+ if (sizesAt !== void 0) placed.sizesAt = (pack, at) => Effect.andThen(count(at.length, true), sizesAt(pack, at));
135
+ return Object.assign(read, {
131
136
  sizes,
132
137
  hold,
133
138
  letGo,
134
- indexed: reads.indexed
135
- });
139
+ letGoReads,
140
+ release: given
141
+ }, placed);
136
142
  };
137
143
  /**
138
144
  * A walk over the repo's objects, a frontier at a time, each object read once: `next` says which
@@ -1,7 +1,7 @@
1
+ import "../contracts/promises.mjs";
1
2
  import "../contracts/repos.mjs";
2
3
  import "./outbox.mjs";
3
4
  import "./records.mjs";
4
- import "../contracts/promises.mjs";
5
5
  import { Key, Listing, Page, Read, Row, Snapshot } from "@ref0/infra";
6
6
  import "effect/Effect";
7
7
  import "effect/Types";
@@ -0,0 +1,21 @@
1
+ import "effect/Effect";
2
+ /** The mark every copy of the package gives its closed rejection, by the one global symbol. */
3
+ declare const closedMark: unique symbol;
4
+ /**
5
+ * What a run on a closed Git server rejects with, wherever it runs: a value of its own, so no
6
+ * mapper turns it into a store failure to try again. Each passes it through as it is.
7
+ */
8
+ export declare class ServerClosed extends Error {
9
+ readonly name = "ServerClosed";
10
+ readonly [closedMark] = true;
11
+ constructor();
12
+ }
13
+ /**
14
+ * Whether `cause` is a closed server's rejection, which a mapper passes through as it is.
15
+ *
16
+ * A predicate, not the class, because `instanceof` fails when a host loads two copies of the
17
+ * package. A bridge under Q787, as `ready()` is: SV's step 2b deletes it with `ready()`, once the
18
+ * drop-in runs in Git's runtime.
19
+ */
20
+ export declare const isServerClosed: (cause: unknown) => cause is ServerClosed;
21
+ export {};
@@ -0,0 +1,34 @@
1
+ import { tryUnlessSpent } from "../contracts/calls-spent.mjs";
2
+ import * as Effect from "effect/Effect";
3
+ import * as Predicate from "effect/Predicate";
4
+ // src/store/closed.ts
5
+ /** The mark every copy of the package gives its closed rejection, by the one global symbol. */
6
+ const closedMark = Symbol.for("@ref0/git/ServerClosed");
7
+ /**
8
+ * What a run on a closed Git server rejects with, wherever it runs: a value of its own, so no
9
+ * mapper turns it into a store failure to try again. Each passes it through as it is.
10
+ */
11
+ var ServerClosed = class extends Error {
12
+ name = "ServerClosed";
13
+ [closedMark] = true;
14
+ constructor() {
15
+ super("This Git server is closed");
16
+ }
17
+ };
18
+ /** What a call on a closed server rejects with. */
19
+ const closedError = () => new ServerClosed();
20
+ /**
21
+ * Whether `cause` is a closed server's rejection, which a mapper passes through as it is.
22
+ *
23
+ * A predicate, not the class, because `instanceof` fails when a host loads two copies of the
24
+ * package. A bridge under Q787, as `ready()` is: SV's step 2b deletes it with `ready()`, once the
25
+ * drop-in runs in Git's runtime.
26
+ */
27
+ const isServerClosed = (cause) => Predicate.hasProperty(cause, closedMark) && cause[closedMark] === true;
28
+ /**
29
+ * `run` as an Effect, its rejection as `mapped` reads it, but a closed server's, and one past a call
30
+ * bound, as the defect each is, so it reaches the caller as itself: `tryUnlessSpent` that also
31
+ * knows the closed server. Every mapper on Git's store and host path runs through here.
32
+ */
33
+ const tryClosing = (run, mapped) => Effect.catchIf(tryUnlessSpent(run, (cause) => isServerClosed(cause) ? cause : mapped(cause)), isServerClosed, (closed) => Effect.die(closed));
34
+ export { ServerClosed, closedError, isServerClosed, tryClosing };
@@ -2,6 +2,7 @@ import { inCallScope } from "./call-count.mjs";
2
2
  import { answerOf } from "../contracts/calls-spent.mjs";
3
3
  import { thrownError } from "./errors.mjs";
4
4
  import * as Effect from "effect/Effect";
5
+ import * as Stream from "effect/Stream";
5
6
  // src/store/crossing.ts
6
7
  /**
7
8
  * Why a run takes no call scope: a job's try is no request's, so only the platform's cap bounds
@@ -29,12 +30,8 @@ const exitIn = (effect, crossing, signal) => Effect.runPromiseExit(scoped(effect
29
30
  * interrupted throws the signal's reason, and a defect throws as it is.
30
31
  */
31
32
  const resultIn = async (effect, crossing, signal) => answerOf(await exitIn(effect, crossing, signal), signal);
32
- /** `runIn` on `runtime`, a server's own, which runs each call until it's disposed. */
33
- const runOn = (runtime, effect, crossing, signal) => runtime.runPromise(scoped(effect, crossing), optionsOf(signal));
34
- /** `exitIn` on `runtime`, a server's own. */
33
+ /** `exitIn` on `runtime`, a server's own: its build's failure, `ER`, beside the run's. */
35
34
  const exitOn = (runtime, effect, crossing, signal) => runtime.runPromiseExit(scoped(effect, crossing), optionsOf(signal));
36
- /** `resultIn` on `runtime`, a server's own. */
37
- const resultOn = async (runtime, effect, crossing, signal) => answerOf(await exitOn(runtime, effect, crossing, signal), signal);
38
35
  /** `effect` started on a fiber of its own, run in `crossing`. */
39
36
  const forkIn = (effect, crossing) => Effect.runFork(scoped(effect, crossing));
40
37
  /** `effect`, which must not wait, run now in `crossing`. */
@@ -46,6 +43,12 @@ const syncExitIn = (effect, crossing) => Effect.runSyncExit(scoped(effect, cross
46
43
  * caller that hands its own context on, such as a memory budget's or an allotment's waits.
47
44
  */
48
45
  const runnerIn = (context) => (effect, signal) => Effect.runPromiseWith(context)(effect, optionsOf(signal));
46
+ /**
47
+ * `stream` as a web stream, each pull run in `context`: for a body whose call the door that read it
48
+ * counted, so its pulls make none. It reads nothing ahead of its reader: each pull of `stream`
49
+ * waits for a read, so a body no one reads yet holds no chunk of the bucket's.
50
+ */
51
+ const readableIn = (stream, context) => Stream.toReadableStreamWith(stream, context, { strategy: { highWaterMark: 0 } });
49
52
  /** A job's try as a Promise: Git's error, thrown with it as the cause, so the queue retries. */
50
53
  const runJobTry = (effect) => runIn(Effect.mapError(effect, thrownError), uncounted("a job's try: no request's, so only the platform's cap bounds it"));
51
54
  /**
@@ -63,4 +66,4 @@ const untilAborted = (effect, signal) => {
63
66
  });
64
67
  return Effect.raceFirst(effect, aborted);
65
68
  };
66
- export { Uncounted, exitIn, exitOn, forkIn, noCall, resultIn, resultOn, runIn, runJobTry, runOn, runnerIn, syncExitIn, syncIn, uncounted, untilAborted };
69
+ export { Uncounted, exitIn, exitOn, forkIn, noCall, readableIn, resultIn, runIn, runJobTry, runnerIn, syncExitIn, syncIn, uncounted, untilAborted };
@@ -3,6 +3,7 @@ import { bytesFromBase64Url, randomText, toBase64Url, utf8Of } from "./random.mj
3
3
  import { catalogueScope, collections } from "./scopes.mjs";
4
4
  import * as Effect from "effect/Effect";
5
5
  import * as Schema from "effect/Schema";
6
+ import * as Semaphore from "effect/Semaphore";
6
7
  // src/store/cursor-seal.ts
7
8
  /** The row in `git/repos` that holds the key every list cursor is sealed with. */
8
9
  const cursorKeyRow = {
@@ -46,13 +47,15 @@ const listBound = (list, ...parts) => JSON.stringify([list, ...parts]);
46
47
  /** One server's seal over `store`: it reads or makes the key on its first use, then keeps it. */
47
48
  const cursorSeal = (store) => {
48
49
  let key;
49
- const keyNow = Effect.fnUntraced(function* () {
50
+ const turn = Semaphore.makeUnsafe(1).withPermits(1);
51
+ const load = Effect.fnUntraced(function* () {
50
52
  if (key !== void 0) return key;
51
53
  const bytes = yield* loadKey(store);
52
54
  const imported = yield* Effect.promise(() => crypto.subtle.importKey("raw", bytes, "AES-GCM", false, ["encrypt", "decrypt"]));
53
55
  key = imported;
54
56
  return imported;
55
57
  });
58
+ const keyNow = () => key === void 0 ? turn(load()) : Effect.succeed(key);
56
59
  return {
57
60
  seal: Effect.fnUntraced(function* (bound, text) {
58
61
  const using = yield* keyNow();
@@ -1,9 +1,11 @@
1
- import { callLimitIn, callLimitTry, fromAnswer, spentIn, spentRefusal, tryUnlessSpent } from "../contracts/calls-spent.mjs";
1
+ import { callLimitIn, callLimitTry, fromAnswer, spentIn, spentRefusal } from "../contracts/calls-spent.mjs";
2
2
  import { chargeCall } from "./call-count.mjs";
3
+ import { tryClosing } from "./closed.mjs";
3
4
  import { isInfraError } from "@ref0/infra";
4
5
  import * as Effect from "effect/Effect";
5
6
  import * as Schema from "effect/Schema";
6
7
  import * as Option from "effect/Option";
8
+ import * as Predicate from "effect/Predicate";
7
9
  // src/store/errors.ts
8
10
  const gitError = (code, message, input = {}) => ({
9
11
  code,
@@ -21,7 +23,7 @@ const fail = (code, message, input) => Effect.fail(gitError(code, message, input
21
23
  * a door fails with it as the defect `CallsSpent` instead, which an edge answers with this.
22
24
  */
23
25
  const fromInfra = (cause) => {
24
- if (!isInfraError(cause)) return gitError("UNAVAILABLE", "The store failed", {
26
+ if (!isInfraError(cause) || Predicate.isTagged(cause, "Unavailable")) return gitError("UNAVAILABLE", "The store failed", {
25
27
  cause,
26
28
  hint: "Try again"
27
29
  });
@@ -52,10 +54,14 @@ const pastCallLimit = (cause) => callLimitIn(cause) !== void 0;
52
54
  */
53
55
  const isLimitRefusal = (error) => error.code === "RESOURCE_EXHAUSTED";
54
56
  function bestEffort(effect, failed) {
55
- const dropped = (error) => failed === void 0 ? Effect.logWarning("A step after a commit was dropped").pipe(Effect.annotateLogs({
56
- code: error.code,
57
- message: error.message
58
- })) : failed(error);
57
+ const dropped = (error) => {
58
+ if (failed !== void 0) return failed(error);
59
+ const logged = Effect.logWarning("A step after a commit was dropped").pipe(Effect.annotateLogs({
60
+ code: error.code,
61
+ message: error.message
62
+ }));
63
+ return error.hostHint === void 0 ? logged : logged.pipe(Effect.annotateLogs("hostHint", error.hostHint));
64
+ };
59
65
  return Effect.catch(answerSpent(effect), dropped);
60
66
  }
61
67
  /**
@@ -79,7 +85,7 @@ const answerSpent = (effect) => Effect.catchDefect(effect, (defect) => {
79
85
  * either's bound (Q799): past one, it dies with `CallsSpent`, which an edge answers, as
80
86
  * `spentRefusalOf` does.
81
87
  */
82
- const storeCall = (run) => Effect.andThen(chargeCall("store"), tryUnlessSpent(run, fromInfra));
88
+ const storeCall = (run) => Effect.andThen(chargeCall("store"), tryClosing(run, fromInfra));
83
89
  /** A stored row whose value should match `schema`; one that doesn't is `DATA_LOSS`. */
84
90
  const decodeRow = (schema, row, what) => {
85
91
  const decoded = Schema.decodeUnknownOption(schema)(row.value);