@ref0/git 0.25.0 → 0.27.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.
@@ -0,0 +1,105 @@
1
+ import { inflaterMemoryBytes } from "../buffers.mjs";
2
+ import * as Result from "effect/Result";
3
+ import { zlibNgModule } from "#zlib-ng";
4
+ // src/protocol/bytes/zlib-ng/instance.ts
5
+ /** What `zn_inflate` answers, as `adapter.c` numbers it. */
6
+ const zlibStatus = {
7
+ more: 0,
8
+ full: 1,
9
+ end: 2,
10
+ error: -1,
11
+ noProgress: -2
12
+ };
13
+ let current;
14
+ const failed = (kind, message) => Result.fail({
15
+ kind,
16
+ message
17
+ });
18
+ /** Each function `adapter.c` exports, by name. */
19
+ const functionNames = [
20
+ "zn_alloc",
21
+ "zn_release",
22
+ "zn_open",
23
+ "zn_reset",
24
+ "zn_close",
25
+ "zn_inflate",
26
+ "zn_consumed",
27
+ "zn_produced",
28
+ "zn_message",
29
+ "zn_failure",
30
+ "zn_heap_top"
31
+ ];
32
+ /** The instance's exports, each checked to be what `adapter.c` declares, or a throw saying which isn't. */
33
+ const exportsOf = (made) => {
34
+ const functions = {};
35
+ for (const name of functionNames) {
36
+ const value = made.exports[name];
37
+ if (!(value instanceof Function)) throw new Error(`the module exports no ${name}`);
38
+ functions[name] = value;
39
+ }
40
+ return functions;
41
+ };
42
+ /** The pages the module's memory starts with, as it declares them: its 32 KiB stack and its data. */
43
+ const startPages = 2;
44
+ /**
45
+ * A new instance whose memory grows to `maximumBytes` at most, in whole 64 KiB pages: an
46
+ * allocation past it answers no memory, never a trap. `instance()` makes the isolate's at
47
+ * `inflaterMemoryBytes`.
48
+ */
49
+ const instantiate = (maximumBytes) => {
50
+ const memory = new WebAssembly.Memory({
51
+ initial: startPages,
52
+ maximum: Math.max(startPages, Math.floor(maximumBytes / 65536))
53
+ });
54
+ return {
55
+ exports: exportsOf(new WebAssembly.Instance(zlibNgModule(), { env: { memory } })),
56
+ memory,
57
+ live: true,
58
+ view: new Uint8Array(memory.buffer)
59
+ };
60
+ };
61
+ /**
62
+ * The isolate's instance, made now if there's none or the last one trapped. A trapped instance's
63
+ * memory may be half written, so it's dropped whole: its streams fail, and the next opens anew.
64
+ */
65
+ const instance = () => {
66
+ if (current?.live === true) return Result.succeed(current);
67
+ try {
68
+ const made = instantiate(inflaterMemoryBytes);
69
+ current = made;
70
+ return Result.succeed(made);
71
+ } catch (error) {
72
+ return failed("start", `zlib-ng didn't start: ${String(error)}`);
73
+ }
74
+ };
75
+ /**
76
+ * `run` on the instance, its trap answered as a failure that drops the instance, never thrown.
77
+ * Its view of memory is taken again whenever the memory grew, which detaches the last one.
78
+ */
79
+ const call = (on, run) => {
80
+ if (!on.live) return failed("trapped", "zlib-ng's instance stopped after a trap");
81
+ try {
82
+ const answer = run(on.exports);
83
+ if (on.view.buffer !== on.memory.buffer) on.view = new Uint8Array(on.memory.buffer);
84
+ return Result.succeed(answer);
85
+ } catch (error) {
86
+ on.live = false;
87
+ if (current === on) current = void 0;
88
+ return failed("trapped", `zlib-ng trapped: ${String(error)}`);
89
+ }
90
+ };
91
+ /** The NUL-ended string at `at` in the instance's memory, or `''` at 0. */
92
+ const cString = (on, at) => {
93
+ if (at === 0) return "";
94
+ const end = on.view.indexOf(0, at);
95
+ return String.fromCharCode(...on.view.subarray(at, end < 0 ? at : end));
96
+ };
97
+ /** Why `zn_open` or `zn_alloc` answered 0, as `zn_failure` numbers it. */
98
+ const openFailure = (on) => on.exports.zn_failure() === 1 ? {
99
+ kind: "memory",
100
+ message: "zlib-ng's memory can't grow to place another inflater"
101
+ } : {
102
+ kind: "start",
103
+ message: "zlib-ng's inflater didn't start"
104
+ };
105
+ export { cString, call, instance, instantiate, openFailure, zlibStatus };
@@ -1,7 +1,7 @@
1
1
  import { BufferError, allotOrFail, allotScoped, capacityOf } from "../bytes/buffers.mjs";
2
2
  import { crc32Update } from "../bytes/crc32.mjs";
3
3
  import { hexOf } from "../bytes/hex.mjs";
4
- import { inflaterBytes, memberInflater, openMemberInflater } from "../bytes/inflate.mjs";
4
+ import { chunkInflater, inflateWindow, inflaterBytes, openChunkInflater } from "../bytes/inflate.mjs";
5
5
  import { flushPkt } from "../framing/pkt-line.mjs";
6
6
  import { objectTypes } from "../objects/object.mjs";
7
7
  import { stepEffect } from "./step.mjs";
@@ -60,6 +60,8 @@ const entryKinds = [
60
60
  /** How much a reader asks of its transport at a time, and the size of its inflate output. */
61
61
  const packReadBytes = 65536;
62
62
  const sizeOfFlush = flushPkt.byteLength;
63
+ /** What an inflater takes in place of a chunk that's used up, so it keeps none. */
64
+ const noBytes = /* @__PURE__ */ new Uint8Array(0);
63
65
  /** A transport's read that failed: a broken or cancelled body. */
64
66
  var PackTransportError = class extends Schema.TaggedError()("PackTransportError", { cause: Schema.Unknown }) {};
65
67
  /**
@@ -275,7 +277,7 @@ const readPackFrom = (source, hasher, sink, limits, meter, resume) => Effect.sco
275
277
  yield* allotScoped(meter, "inflater", inflaterBytes);
276
278
  yield* allotScoped(meter, "inflated", packReadBytes);
277
279
  yield* allotScoped(meter, "header", 96 + sizeOfFlush);
278
- const inflater = yield* Effect.mapError(memberInflater, (error) => fail("zlib", error.message));
280
+ const inflater = yield* Effect.mapError(chunkInflater, (error) => fail("zlib", error.message));
279
281
  const output = new Uint8Array(packReadBytes);
280
282
  const header = new Uint8Array(headerBytesMost);
281
283
  const fixed = /* @__PURE__ */ new Uint8Array(12);
@@ -306,6 +308,7 @@ const readPackFrom = (source, hasher, sink, limits, meter, resume) => Effect.sco
306
308
  }
307
309
  let at = 0;
308
310
  let hashFrom = 0;
311
+ inflater.take(chunk);
309
312
  while (at < chunk.byteLength) {
310
313
  if (phase === "header") {
311
314
  const take = Math.min(12 - fixedLength, chunk.byteLength - at);
@@ -388,7 +391,7 @@ const readPackFrom = (source, hasher, sink, limits, meter, resume) => Effect.sco
388
391
  continue;
389
392
  }
390
393
  if (phase === "data" && entry !== void 0) {
391
- const inflatedStep = inflater.inflate(chunk.subarray(at), output);
394
+ const inflatedStep = inflater.inflateAt(at, output.byteLength);
392
395
  if (Result.isFailure(inflatedStep)) return yield* fail("zlib", inflatedStep.failure.message);
393
396
  const inflated = inflatedStep.success;
394
397
  if (crcs) crc = crc32Update(crc, chunk.subarray(at, at + inflated.consumed));
@@ -396,6 +399,7 @@ const readPackFrom = (source, hasher, sink, limits, meter, resume) => Effect.sco
396
399
  compressed += inflated.consumed;
397
400
  produced += inflated.produced;
398
401
  if (produced > entry.size) return yield* fail("length", `an entry inflates past its ${entry.size} bytes`);
402
+ if (inflated.produced > 0) output.set(inflater.output(inflated.produced));
399
403
  const took = inflated.produced > 0 ? sink.data(output.subarray(0, inflated.produced)) : void 0;
400
404
  if (took !== void 0) yield* stepEffect(took);
401
405
  if (inflated.kind !== "end") continue;
@@ -446,6 +450,7 @@ const readPackFrom = (source, hasher, sink, limits, meter, resume) => Effect.sco
446
450
  hashFrom = at;
447
451
  }
448
452
  if (phase !== "trailer" && phase !== "after" && hashFrom < chunk.byteLength) yield* hasher.write(chunk.subarray(hashFrom));
453
+ inflater.take(noBytes);
449
454
  }
450
455
  return {
451
456
  version,
@@ -456,14 +461,12 @@ const readPackFrom = (source, hasher, sink, limits, meter, resume) => Effect.sco
456
461
  };
457
462
  }));
458
463
  const readUint32 = (bytes, at) => (bytes[at] ?? 0) * 2 ** 24 + ((bytes[at + 1] ?? 0) << 16) + ((bytes[at + 2] ?? 0) << 8) + (bytes[at + 3] ?? 0);
459
- const entryParts = [
460
- ["inflater", inflaterBytes],
461
- ["inflated", packReadBytes],
462
- ["header", headerBytesMost]
463
- ];
464
+ const entryParts = [["inflater", inflaterBytes], ["header", headerBytesMost]];
464
465
  /**
465
- * Opens a reusable single-entry reader charged to `meter` (`inflater`, `inflated`, `header`) until
466
- * `close()`. Resets its inflater on every exit, closing itself if reset fails so no state leaks.
466
+ * Opens a reusable single-entry reader charged to `meter` (`inflater`, `header`) until
467
+ * `close()`. It holds an inflater only while it reads: one opens at a read's first data and ends
468
+ * on every exit. An inflater's stream is in zlib-ng's memory, which a collection never frees, so
469
+ * a reader its owner never closes, as `PackBlocks`' readers aren't, still holds none of it.
467
470
  */
468
471
  const openEntryReader = (meter) => {
469
472
  const taken = [];
@@ -483,35 +486,33 @@ const openEntryReader = (meter) => {
483
486
  }
484
487
  taken.push(part);
485
488
  }
486
- const opened = openMemberInflater();
487
- if (Result.isFailure(opened)) {
488
- freeAll();
489
- return Result.fail(fail("zlib", opened.failure.message));
490
- }
491
- const inflater = opened.success;
492
- let output = new Uint8Array(packReadBytes);
489
+ let inflater;
493
490
  let header = new Uint8Array(headerBytesMost);
494
491
  let closed = false;
495
- let inflaterDirty = false;
492
+ /** The read's inflater, opened at its first data, or a failure saying why it couldn't be. */
493
+ const reading = () => {
494
+ if (inflater !== void 0) return Result.succeed(inflater);
495
+ const opened = openChunkInflater();
496
+ if (Result.isSuccess(opened)) inflater = opened.success;
497
+ return opened;
498
+ };
496
499
  const close = () => {
497
500
  if (closed) return;
498
501
  closed = true;
499
- inflaterDirty = false;
500
- inflater.end();
501
- output = void 0;
502
+ inflater?.end();
503
+ inflater = void 0;
502
504
  header = void 0;
503
505
  freeAll();
504
506
  };
507
+ /** Ends the read's inflater, if it opened one: the next read opens its own. */
505
508
  const reset = () => {
506
- if (!inflaterDirty || closed) return;
507
- inflaterDirty = false;
508
- if (Result.isFailure(inflater.reset())) close();
509
+ inflater?.end();
510
+ inflater = void 0;
509
511
  };
510
512
  /** Shared header + zlib inflate stepper used by both `tryReadSync` and `read`. */
511
513
  const entryDecoder = (offset, limits, before, resume) => {
512
- const outBuf = output;
513
514
  const headBuf = header;
514
- if (closed || outBuf === void 0 || headBuf === void 0) throw new Error("a closed entry reader cannot read");
515
+ if (closed || headBuf === void 0) throw new Error("a closed entry reader cannot read");
515
516
  let headerLength = resume?.headerLength ?? 0;
516
517
  let active = resume === void 0 ? void 0 : {
517
518
  entry: resume.entry,
@@ -555,16 +556,19 @@ const openEntryReader = (meter) => {
555
556
  if (active === void 0) return void 0;
556
557
  }
557
558
  const { entry, data } = active;
559
+ const opened = reading();
560
+ if (Result.isFailure(opened)) return fail("zlib", opened.failure.message);
561
+ const inflating = opened.success;
562
+ inflating.take(chunk);
558
563
  while (at < chunk.byteLength) {
559
- inflaterDirty = true;
560
- const step = inflater.inflate(chunk.subarray(at), outBuf);
564
+ const step = inflating.inflateAt(at, inflateWindow);
561
565
  if (Result.isFailure(step)) return fail("zlib", step.failure.message);
562
566
  const inflated = step.success;
563
567
  at += inflated.consumed;
564
568
  compressed += inflated.consumed;
565
569
  if (produced + inflated.produced > entry.size) return fail("length", `an entry inflates past its ${entry.size} bytes`);
566
570
  if (inflated.produced > 0) {
567
- data.set(outBuf.subarray(0, inflated.produced), produced);
571
+ data.set(inflating.output(inflated.produced), produced);
568
572
  produced += inflated.produced;
569
573
  }
570
574
  if (inflated.kind === "end") return produced === entry.size ? {
@@ -572,8 +576,9 @@ const openEntryReader = (meter) => {
572
576
  data,
573
577
  stored: compressed
574
578
  } : fail("length", `an entry inflated to ${produced}, not ${entry.size}`);
575
- if (inflated.kind === "more") return void 0;
579
+ if (inflated.kind === "more") break;
576
580
  }
581
+ inflating.take(noBytes);
577
582
  };
578
583
  return {
579
584
  stepChunk,
@@ -0,0 +1 @@
1
+ import "../protocol/bytes/buffers.mjs";
@@ -1,26 +1,12 @@
1
+ import { allottableOf, defaultMemory, headroomBytes, idleHeapBytes } from "../protocol/bytes/buffers.mjs";
1
2
  import { receiveCheckedBytes } from "./fsck.mjs";
2
3
  import { receiveIndexHeldOf, receiveRoomAdmitted } from "./receive-admission.mjs";
3
4
  import { receiveTableMost } from "./receive-bounds.mjs";
4
5
  import { blockBytes, blockHeld, blocksHeld, packIndexerLimits, readCacheBytes } from "./pack-reads.mjs";
5
6
  // src/storage/budget.ts
6
7
  const mib = 1048576;
7
- /** A Worker isolate's 128 MB, read as decimal: the memory `packs()` runs in unless told. */
8
- const defaultMemory = 128e6;
9
- /**
10
- * What an `examples/git-worker` isolate holds outside any allotment, so the allotted and the
11
- * headroom fit the most a request's work holds on Workers before the isolate is replaced:
12
- * 128,000,000 less 120,061,952, read in SJC at 3795e0349 on a3ac998ae (Q893, 25 Nov 2026). That's
13
- * a cold isolate's lowest fill of JS heap, 123,207,680, less the 3,145,728 a Git request's or a
14
- * `git.repack` try's own state keeps on top. ArrayBuffers aren't counted at once, so nothing is
15
- * sized from their fills.
16
- */
17
- const idleHeapBytes = 7938048;
18
- /** What `packs()` leaves free of `memory`, for what no allotment counts. */
19
- const headroomBytes = 16 * mib;
20
8
  /** The part a bucket's upload holds, R2's for a push of unknown size: 8 MiB. */
21
9
  const uploadPartBytes = 8 * mib;
22
- /** What operations are allotted of `memory`: all of it but an idle heap and the headroom. */
23
- const allottableOf = (memory) => memory - idleHeapBytes - headroomBytes;
24
10
  /** A reader's floors: a read cache of 2 MiB and one block. */
25
11
  const readerFloors = {
26
12
  cache: 2 * mib,
@@ -1,5 +1,6 @@
1
1
  import { gitError } from "../contracts/result.mjs";
2
2
  import { hexOf } from "../protocol/bytes/hex.mjs";
3
+ import { emptyTree } from "../protocol/objects/object.mjs";
3
4
  import { indexHeadBytes } from "../protocol/packs/idx.mjs";
4
5
  import { checkTypesHeader, checkTypesPack, typeAt, typeOfByte, typesPackAt } from "../protocol/packs/types-file.mjs";
5
6
  import { readWhole } from "./calls.mjs";
@@ -135,7 +136,7 @@ const storedOf = (pack) => isPackId(pack.id) ? Effect.succeed({
135
136
  * push asks its links, they make at most `heldTypesCalls` of the listed packs in all.
136
137
  */
137
138
  const repoTypes = (input) => {
138
- const { store, bucket, indexes, listed, memory, reader, visible } = input;
139
+ const { store, bucket, indexes, listed, memory, reader, visible, hasLooseRows = true } = input;
139
140
  const rows = rowHeaders(store, listed);
140
141
  const forward = /* @__PURE__ */ new Map();
141
142
  const opened = /* @__PURE__ */ new Map();
@@ -204,11 +205,18 @@ const repoTypes = (input) => {
204
205
  const oids = yield* shownOf(asked);
205
206
  yield* charge(oids.length, sizes);
206
207
  const answer = /* @__PURE__ */ new Map();
207
- for (const [oid, { type }] of yield* rows(oids)) answer.set(oid, {
208
+ const addRows = Effect.fnUntraced(function* (ids) {
209
+ for (const [oid, { type }] of yield* rows(ids)) answer.set(oid, {
210
+ pack: void 0,
211
+ type
212
+ });
213
+ });
214
+ if (hasLooseRows) yield* addRows(oids);
215
+ else if (oids.includes("4b825dc642cb6eb9a060e54bf8d69288fbee4904")) answer.set(emptyTree, {
208
216
  pack: void 0,
209
- type
217
+ type: "tree"
210
218
  });
211
- let left = oids.filter((oid) => !answer.has(oid));
219
+ let left = answer.size === 0 ? oids : oids.filter((oid) => !answer.has(oid));
212
220
  for (let from = 0; from < listed.packs.length && left.length > 0; from += 6) {
213
221
  const window = listed.packs.slice(from, from + 6);
214
222
  const asked = left;
@@ -226,6 +234,7 @@ const repoTypes = (input) => {
226
234
  });
227
235
  left = left.filter((oid) => !answer.has(oid));
228
236
  }
237
+ if (!hasLooseRows && left.length > 0) yield* addRows(left);
229
238
  return answer;
230
239
  }),
231
240
  beyond: Effect.fnUntraced(function* (asked) {