@ref0/git 0.26.0 → 0.29.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 (90) hide show
  1. package/dist/client/git-http.mjs +29 -12
  2. package/dist/client/push-upload.mjs +13 -12
  3. package/dist/client/remote-helper.mjs +1 -1
  4. package/dist/client/remote.mjs +1 -1
  5. package/dist/client/run-remote-helper.mjs +7 -14
  6. package/dist/client/slice-push.d.mts +13 -9
  7. package/dist/client/slice-push.mjs +16 -5
  8. package/dist/client/slice-report.mjs +6 -8
  9. package/dist/client/transport.mjs +27 -5
  10. package/dist/contracts/git.d.mts +17 -2
  11. package/dist/contracts/store.d.mts +1 -1
  12. package/dist/core/forks.mjs +1 -1
  13. package/dist/core/purge.mjs +1 -1
  14. package/dist/http/push-uploads.mjs +10 -15
  15. package/dist/lfs/copy.d.mts +4 -1
  16. package/dist/lfs/copy.mjs +7 -6
  17. package/dist/lfs/layer.mjs +1 -1
  18. package/dist/lfs/objects.mjs +127 -8
  19. package/dist/lfs/upload-most.mjs +19 -3
  20. package/dist/merges/merges.d.mts +3 -2
  21. package/dist/protocol/bytes/buffers.mjs +26 -1
  22. package/dist/protocol/bytes/crc32.mjs +39 -6
  23. package/dist/protocol/bytes/deflate.d.mts +11 -31
  24. package/dist/protocol/bytes/deflate.mjs +133 -72
  25. package/dist/protocol/bytes/inflate.d.mts +11 -6
  26. package/dist/protocol/bytes/inflate.mjs +184 -119
  27. package/dist/protocol/bytes/sha1-hasher.mjs +12 -12
  28. package/dist/protocol/bytes/zlib-ng/LICENSE.md +19 -0
  29. package/dist/protocol/bytes/zlib-ng/README.md +132 -0
  30. package/dist/protocol/bytes/zlib-ng/bundled.d.mts +5 -0
  31. package/dist/protocol/bytes/zlib-ng/bundled.mjs +8 -0
  32. package/dist/protocol/bytes/zlib-ng/compile.d.mts +9 -0
  33. package/dist/protocol/bytes/zlib-ng/compile.mjs +19 -0
  34. package/dist/protocol/bytes/zlib-ng/embedded.mjs +4 -0
  35. package/dist/protocol/bytes/zlib-ng/instance.d.mts +1 -0
  36. package/dist/protocol/bytes/zlib-ng/instance.mjs +111 -0
  37. package/dist/protocol/bytes/zlib-ng/zlib-ng.wasm +0 -0
  38. package/dist/protocol/objects/fsck-names.mjs +27 -7
  39. package/dist/protocol/objects/fsck.mjs +100 -88
  40. package/dist/protocol/objects/object-rules.d.mts +1 -0
  41. package/dist/protocol/objects/tree.d.mts +1 -0
  42. package/dist/protocol/objects/tree.mjs +55 -6
  43. package/dist/protocol/packs/reader.mjs +35 -30
  44. package/dist/protocol/wire/push-uploads.mjs +12 -1
  45. package/dist/server/registry.mjs +12 -0
  46. package/dist/server/server.mjs +7 -1
  47. package/dist/storage/budget.d.mts +2 -0
  48. package/dist/storage/budget.mjs +26 -24
  49. package/dist/storage/calls.d.mts +1 -1
  50. package/dist/storage/calls.mjs +4 -2
  51. package/dist/storage/host-bucket.mjs +15 -5
  52. package/dist/storage/index-bitmaps.d.mts +1 -0
  53. package/dist/storage/index-bitmaps.mjs +15 -14
  54. package/dist/storage/index-build.d.mts +2 -1
  55. package/dist/storage/index-build.mjs +14 -15
  56. package/dist/storage/index-files.mjs +3 -3
  57. package/dist/storage/index-graph.mjs +3 -3
  58. package/dist/storage/index-remap.mjs +6 -7
  59. package/dist/storage/links.mjs +34 -2
  60. package/dist/storage/loose.mjs +2 -1
  61. package/dist/storage/memory-budget.d.mts +1 -0
  62. package/dist/storage/memory-budget.mjs +12 -3
  63. package/dist/storage/merge-most.mjs +2 -1
  64. package/dist/storage/merge-stream.mjs +4 -3
  65. package/dist/storage/merge-write.mjs +2 -2
  66. package/dist/storage/pack-reads.mjs +1 -1
  67. package/dist/storage/pack-receive.mjs +24 -8
  68. package/dist/storage/packs-memory.mjs +2 -1
  69. package/dist/storage/packs.d.mts +3 -1
  70. package/dist/storage/packs.mjs +34 -11
  71. package/dist/storage/put-holds.d.mts +1 -0
  72. package/dist/storage/put-holds.mjs +20 -0
  73. package/dist/storage/ready.mjs +4 -2
  74. package/dist/storage/receive-bounds.d.mts +1 -1
  75. package/dist/storage/receive-bounds.mjs +4 -4
  76. package/dist/storage/receive-links.mjs +31 -15
  77. package/dist/storage/receive-steps.mjs +21 -1
  78. package/dist/storage/runs.d.mts +1 -1
  79. package/dist/storage/runs.mjs +7 -5
  80. package/dist/store/door.mjs +9 -1
  81. package/dist/store/job-try.mjs +5 -5
  82. package/dist/store/repo-record.mjs +1 -1
  83. package/dist/sync/step.mjs +3 -4
  84. package/package.json +12 -9
  85. package/dist/http/node.d.mts +0 -62
  86. package/dist/http/node.mjs +0 -212
  87. package/dist/node.d.mts +0 -3
  88. package/dist/node.mjs +0 -1
  89. package/dist/server/edge-node.d.mts +0 -19
  90. package/dist/server/edge-node.mjs +0 -20
@@ -1,24 +1,29 @@
1
1
  import { allotOrFail, allotScoped, capacityOf } from "./buffers.mjs";
2
+ import { cString, call, instance, openFailure, zlibStatus } from "./zlib-ng/instance.mjs";
2
3
  import * as Schema from "effect/Schema";
3
4
  import * as Effect from "effect/Effect";
4
- import untyped from "pako/lib/zlib/deflate.js";
5
+ import * as Result from "effect/Result";
5
6
  // src/protocol/bytes/deflate.ts
6
7
  /**
7
8
  * One zlib member deflated at a time, as a pack holds one per entry, at zlib's defaults: level 6,
8
9
  * method 8, window 15, memory level 8, strategy 0, and no flush but the last. Two ways, by size:
9
10
  *
10
- * - An object up to `wholeObjectBytes` is deflated whole by `deflateWhole`, through pako 2.1.0's
11
- * low-level deflater into one buffer of `deflatedMost` its length, in one synchronous call, on
12
- * one deflate state kept and reset between calls. A `CompressionStream` costs about 27 ms of
13
- * waiting in workerd for each one made, written and read, whatever its size, which a pack of
14
- * small objects paid an object (ref0 #3222). A fresh pako state for each one cost a 200-byte
15
- * object about 69 µs of CPU in Node and 53 µs in workerd; the kept one, reset, 18 and 20 µs.
11
+ * - An object up to `wholeObjectBytes` is deflated whole by `deflateWhole`, through zlib-ng
12
+ * 2.3.3's deflate in WebAssembly, `zlib-ng/`, into one buffer of `deflatedMost` its length, in
13
+ * one synchronous call, on one deflater kept and reset between calls. A `CompressionStream`
14
+ * costs about 27 ms of waiting in workerd for each one made, written and read, whatever its
15
+ * size, which a pack of small objects paid an object (ref0 #3222). Deflating push-90mib's
16
+ * 91,258 objects up to 256 KiB took 862 ms of CPU on Node and 933 ms in local workerd, where
17
+ * pako 2.1.0's kept state, which it replaced, took 2,180 and 2,074 ms, for 0.62% more bytes.
16
18
  * - A larger one is streamed by `memberDeflater`, through the runtime's
17
19
  * `CompressionStream('deflate')`, so what the writer holds never grows with an object's size.
20
+ * It stays there because Workers bill CPU, not waiting: push-90mib's 17 objects past 256 KiB
21
+ * took 911 ms of CPU through it in local workerd and 1,182 ms through zlib-ng, 1.3 times as
22
+ * much, to save a wait of about 27 ms a stream.
18
23
  *
19
- * pako is a port of plain zlib, and Node and workerd build Chromium's, so the two can write other
20
- * bytes for one object. Both are zlib members any inflater reads; nothing compares compressed
21
- * bytes, only what they inflate to.
24
+ * zlib-ng's matcher and Node's and workerd's Chromium zlib can write other bytes for one object.
25
+ * Both are zlib members any inflater reads; nothing compares compressed bytes, only what they
26
+ * inflate to.
22
27
  *
23
28
  * The stream's pieces are each awaited with a read always pending, so a stream that accepts input
24
29
  * only as its output is read never stalls, and output the compressor hasn't given yet when a write
@@ -28,12 +33,18 @@ import untyped from "pako/lib/zlib/deflate.js";
28
33
  * it's handed to returns.
29
34
  */
30
35
  /**
31
- * The bytes one compressor keeps in the runtime's native memory, which no buffer here shows: zlib's
32
- * deflate state at window 15 and memory level 8, as `deflate.c` sizes it: a 64 KiB window, a
33
- * 64 KiB `prev` and a 64 KiB `head` table, and a 64 KiB pending buffer, with its state struct.
34
- * An estimate, held on the meter under `deflater` while a compressor lives.
36
+ * Each of the kept deflater's two buffers in zlib-ng's memory: the one `deflateWhole` copies its
37
+ * input into, a part at a time, and the one it deflates into, copied out as it fills.
35
38
  */
36
- const deflaterBytes = 262144;
39
+ const deflateWindow = 65536;
40
+ /**
41
+ * The bytes one compressor keeps outside any buffer here, held on the meter under `deflater` while
42
+ * it lives: the kept deflater's stream and state in zlib-ng's memory, 350,304 bytes with zlib-ng
43
+ * 2.3.3 at window 15 and memory level 8, and its two buffers, `deflateWindow` each, which
44
+ * `zlib-ng/instance.test.ts` measures. It covers a `CompressionStream`'s too, zlib's state in the
45
+ * runtime's native memory, which `deflate.c` sizes at about 256 KiB.
46
+ */
47
+ const deflaterBytes = 482304;
37
48
  /**
38
49
  * The most bytes zlib deflates `size` bytes into at these settings, above `deflateBound`'s in
39
50
  * `deflate.c`: bytes it can't shrink go in stored blocks of a few bytes' header each, and the
@@ -42,12 +53,11 @@ const deflaterBytes = 262144;
42
53
  const deflatedMost = (size) => size + Math.ceil(size / 2048) + 64;
43
54
  /**
44
55
  * The largest object deflated whole: its body and one output buffer of `deflatedMost` its length,
45
- * beside pako's state, about 768 KiB at most. Deflating 256 KiB at level 6 costs a few
56
+ * beside the deflater, about 1 MiB at most. Deflating 256 KiB at level 6 costs a few
46
57
  * milliseconds of CPU, well under the stream's wait it replaces, and a call that size keeps a cut
47
58
  * by the writer's turn or time bound within milliseconds.
48
59
  */
49
60
  const wholeObjectBytes = 262144;
50
- const zlib = untyped;
51
61
  /**
52
62
  * A compressor that failed: `compressor` for the runtime's error, and `early end` for a stream
53
63
  * that ended before it was closed.
@@ -141,71 +151,122 @@ const memberDeflater = /* @__PURE__ */ Effect.fnUntraced(function* (meter, emit)
141
151
  })
142
152
  };
143
153
  });
144
- /** zlib's codes and settings, as `lib/zlib/constants.js` gives them. */
145
- const zOk = 0;
146
- const zStreamEnd = 1;
147
- const zFinish = 4;
148
- const zDeflated = 8;
149
154
  /**
150
- * The one deflate state `deflateWhole` keeps between calls, at zlib's defaults: about 256 KiB at
151
- * window 15 and memory level 8, as `deflaterBytes` sizes a state, resident for the isolate's or
152
- * thread's life and held on no meter between calls: while one runs, its caller holds
153
- * `deflaterBytes` for it, as it would for a fresh state. A call takes it
154
- * out while it runs, so no two calls ever share it, and puts it back only once it ends whole and
155
- * none is kept; a call that finds none starts its own. `deflateReset` gives the bytes a fresh
156
- * state would: it clears the hash table, so no match reaches into an earlier call's input, and
157
- * the window bytes past the input that it keeps are never matched, as zlib's `deflate.c` says of
158
- * the `WIN_INIT` zeroing that pako leaves out.
155
+ * The deflater `deflateWhole` keeps between calls, `deflaterBytes` in zlib-ng's memory, placed the
156
+ * first time one runs, and kept for the instance's life, held on no meter between calls: while one
157
+ * runs, its caller holds `deflaterBytes` for it. A call takes it out while it runs, so no two
158
+ * calls ever share it, and puts it back only once it ends whole; a call that finds none, or one on
159
+ * an instance that trapped, places its own. Its blocks are freed only when a call fails, so no
160
+ * inflater takes them otherwise, and an isolate's memory holds one deflater at most. `zd_reset` gives the bytes a fresh deflater
161
+ * would: no match reaches into an earlier call's input.
159
162
  */
160
163
  let kept;
161
- /** The kept state, reset, or a fresh one in its place; none when pako's deflater won't start. */
162
- const takenState = () => {
164
+ const deflateFailed = (failure) => compressorFailed(failure.message);
165
+ /**
166
+ * A deflater and its two buffers, placed together; none if one can't be. `instance.test.ts`
167
+ * measures what it places against `deflaterBytes`.
168
+ */
169
+ const placeDeflater = (on) => {
170
+ const opened = call(on, (exports) => exports.zd_open());
171
+ if (Result.isFailure(opened)) return Result.fail(opened.failure);
172
+ const stream = opened.success;
173
+ if (stream === 0) return Result.fail(openFailure(on, "deflater"));
174
+ const placed = call(on, (exports) => exports.zn_alloc(2 * deflateWindow));
175
+ if (Result.isFailure(placed)) return Result.fail(placed.failure);
176
+ if (placed.success === 0) {
177
+ const failure = openFailure(on, "deflater");
178
+ call(on, (exports) => exports.zd_close(stream));
179
+ return Result.fail(failure);
180
+ }
181
+ return Result.succeed({
182
+ stream,
183
+ buffers: placed.success
184
+ });
185
+ };
186
+ /** The kept deflater, reset, or a new one in its place, on the isolate's instance. */
187
+ const takenDeflater = () => {
163
188
  const taken = kept;
164
189
  kept = void 0;
165
- if (taken !== void 0 && zlib.deflateReset(taken) === zOk) return taken;
166
- const stream = {
167
- input: null,
168
- next_in: 0,
169
- avail_in: 0,
170
- total_in: 0,
171
- output: null,
172
- next_out: 0,
173
- avail_out: 0,
174
- total_out: 0,
175
- msg: "",
176
- state: null,
177
- data_type: 2,
178
- adler: 0
179
- };
180
- return zlib.deflateInit2(stream, 6, zDeflated, 15, 8, 0) === zOk ? stream : void 0;
190
+ if (taken !== void 0 && taken.on.live) {
191
+ const reset = call(taken.on, (exports) => exports.zd_reset(taken.stream));
192
+ if (Result.isSuccess(reset) && reset.success === 0) return Result.succeed(taken);
193
+ ended(taken);
194
+ }
195
+ const made = instance();
196
+ if (Result.isFailure(made)) return Result.fail(made.failure);
197
+ const on = made.success;
198
+ const placed = placeDeflater(on);
199
+ return Result.map(placed, (deflater) => ({
200
+ on,
201
+ ...deflater
202
+ }));
203
+ };
204
+ /** Frees a deflater's stream and buffers, unless its instance trapped and was dropped whole. */
205
+ const ended = (deflater) => {
206
+ if (!deflater.on.live) return;
207
+ call(deflater.on, (exports) => {
208
+ exports.zn_release(deflater.buffers);
209
+ exports.zd_close(deflater.stream);
210
+ });
211
+ };
212
+ /**
213
+ * `bytes` deflated on `deflater` into `output`, copied in and out `deflateWindow` at a time: how
214
+ * many bytes it wrote, or why it couldn't. Each call into zlib-ng uses input or makes output, or
215
+ * it fails as no progress, and what it makes can't pass `output`'s end.
216
+ */
217
+ const deflateInto = ({ on, stream, buffers }, bytes, output) => {
218
+ const inputAt = buffers;
219
+ const outputAt = buffers + deflateWindow;
220
+ let loadedFrom = 0;
221
+ let loadedTo = 0;
222
+ let consumed = 0;
223
+ let written = 0;
224
+ for (;;) {
225
+ if (consumed === loadedTo && loadedTo < bytes.byteLength) {
226
+ loadedFrom = consumed;
227
+ loadedTo = Math.min(bytes.byteLength, consumed + deflateWindow);
228
+ on.view.set(bytes.subarray(loadedFrom, loadedTo), inputAt);
229
+ }
230
+ const last = loadedTo === bytes.byteLength ? 1 : 0;
231
+ const stepped = call(on, (exports) => {
232
+ return {
233
+ status: exports.zd_deflate(stream, inputAt + consumed - loadedFrom, loadedTo - consumed, outputAt, deflateWindow, last),
234
+ used: exports.zn_consumed(),
235
+ made: exports.zn_produced()
236
+ };
237
+ });
238
+ if (Result.isFailure(stepped)) return Result.fail(deflateFailed(stepped.failure));
239
+ const { status, used, made } = stepped.success;
240
+ if (status === zlibStatus.error) {
241
+ const message = cString(on, on.exports.zn_message(stream));
242
+ return Result.fail(compressorFailed(message === "" ? "zlib-ng refused" : message));
243
+ }
244
+ if (status !== zlibStatus.end && used === 0 && made === 0) return Result.fail(compressorFailed("zlib-ng made no progress"));
245
+ if (written + made > output.byteLength) return Result.fail(compressorFailed(`the member passed its ${output.byteLength} bytes`));
246
+ output.set(on.view.subarray(outputAt, outputAt + made), written);
247
+ consumed += used;
248
+ written += made;
249
+ if (status === zlibStatus.end) return Result.succeed(written);
250
+ }
181
251
  };
182
252
  /**
183
253
  * One member of `bytes`, deflated whole into one buffer of `deflatedMost` their length and given
184
- * as a view of what it wrote: a call that can't finish in it fails as `compressor`. Its caller
185
- * holds `deflaterBytes` and `deflatedMost` of their length on its meter while it runs.
254
+ * as a view of what it wrote: a call that can't finish in it, or a deflater that can't be placed,
255
+ * fails as `compressor`. Its caller holds `deflaterBytes` and `deflatedMost` of their length on
256
+ * its meter while it runs.
186
257
  */
187
258
  const deflateWhole = (bytes) => Effect.suspend(() => {
188
- const stream = takenState();
189
- if (stream === void 0) return Effect.fail(compressorFailed(`pako's deflater didn't start`));
259
+ const taken = takenDeflater();
260
+ if (Result.isFailure(taken)) return Effect.fail(deflateFailed(taken.failure));
261
+ const deflater = taken.success;
190
262
  const output = new Uint8Array(deflatedMost(bytes.byteLength));
191
- stream.input = bytes;
192
- stream.next_in = 0;
193
- stream.avail_in = bytes.byteLength;
194
- stream.output = output;
195
- stream.next_out = 0;
196
- stream.avail_out = output.byteLength;
197
- stream.msg = "";
198
- const status = zlib.deflate(stream, zFinish);
199
- const written = stream.next_out;
200
- const message = stream.msg;
201
- stream.input = null;
202
- stream.output = null;
203
- if (status !== zStreamEnd) {
204
- zlib.deflateEnd(stream);
205
- return Effect.fail(compressorFailed(message === "" ? `zlib ${status}` : message));
263
+ const written = deflateInto(deflater, bytes, output);
264
+ if (Result.isFailure(written)) {
265
+ ended(deflater);
266
+ return Effect.fail(written.failure);
206
267
  }
207
- if (kept === void 0) kept = stream;
208
- else zlib.deflateEnd(stream);
209
- return Effect.succeed(output.subarray(0, written));
268
+ if (kept === void 0) kept = deflater;
269
+ else ended(deflater);
270
+ return Effect.succeed(output.subarray(0, written.success));
210
271
  });
211
- export { DeflateError, deflateWhole, deflatedMost, deflaterBytes, memberDeflater, wholeObjectBytes };
272
+ export { DeflateError, deflateWhole, deflatedMost, deflaterBytes, memberDeflater, placeDeflater, wholeObjectBytes };
@@ -1,17 +1,22 @@
1
+ import "./zlib-ng/instance.mjs";
1
2
  import * as Schema from "effect/Schema";
2
3
  import * as Effect from "effect/Effect";
3
4
  import * as Result from "effect/Result";
4
5
  import * as Scope from "effect/Scope";
5
- /** The bytes an inflater keeps between calls, at most: its window, its tables and its state. */
6
+ /**
7
+ * The bytes an inflater keeps between calls, at most, all in the instance's memory: its stream,
8
+ * window and state, 42,144 bytes with zlib-ng 2.3.3, and its two buffers, `inflateWindow` each.
9
+ * `zlib-ng/instance.test.ts` measures it.
10
+ */
6
11
  export declare const inflaterBytes: number;
7
12
  declare const InflateError_base: Schema.Class<InflateError, Schema.TaggedStruct<"InflateError", {
8
13
  readonly fault: Schema.Literals<readonly ["zlib", "no progress", "start"]>;
9
14
  readonly message: Schema.String;
10
15
  }>, import("effect/Cause").YieldableError>;
11
16
  /**
12
- * A member that couldn't be inflated: `zlib` for bytes zlib refused, `no progress` for a call
13
- * that neither used its input nor filled its output, and `start` for an inflater that didn't
14
- * start.
17
+ * A member that couldn't be inflated: `zlib` for bytes zlib refused or an instance that trapped,
18
+ * `no progress` for a call that neither used its input nor filled its output, and `start` for an
19
+ * inflater that didn't start.
15
20
  */
16
21
  export declare class InflateError extends InflateError_base {}
17
22
  /**
@@ -34,8 +39,8 @@ export interface MemberInflater {
34
39
  readonly reset: () => Result.Result<void, InflateError>;
35
40
  }
36
41
  /**
37
- * An inflater of zlib members, one after another, `reset` between them, its window and tables let
38
- * go when its scope closes.
42
+ * An inflater of zlib members, one after another, `reset` between them, its stream freed when
43
+ * its scope closes.
39
44
  */
40
45
  export declare const memberInflater: Effect.Effect<MemberInflater, InflateError, Scope.Scope>;
41
46
  export {};
@@ -1,24 +1,34 @@
1
+ import { cString, call, instance, openFailure, zlibStatus } from "./zlib-ng/instance.mjs";
1
2
  import * as Schema from "effect/Schema";
2
3
  import * as Effect from "effect/Effect";
3
4
  import * as Result from "effect/Result";
4
- import untyped from "pako/lib/zlib/inflate.js";
5
5
  // src/protocol/bytes/inflate.ts
6
- const zlib = untyped;
7
- /** zlib's codes, as `lib/zlib/constants.js` gives them. */
8
- const zOk = 0;
9
- const zStreamEnd = 1;
10
- const zBufError = -5;
11
- const zNoFlush = 0;
12
- /** pako's mode waiting for a member's zlib header, `HEAD`, as `lib/zlib/inflate.js` numbers it. */
13
- const headMode = 16180;
14
- /** A zlib wrapper and the largest window, 32 KiB: Git's, and the only kind a pack holds. */
15
- const zlibWindowBits = 15;
16
- /** The bytes an inflater keeps between calls, at most: its window, its tables and its state. */
17
- const inflaterBytes = 49152;
18
6
  /**
19
- * A member that couldn't be inflated: `zlib` for bytes zlib refused, `no progress` for a call
20
- * that neither used its input nor filled its output, and `start` for an inflater that didn't
21
- * start.
7
+ * One zlib member inflated at a time, as a pack holds one per entry, through zlib-ng 2.3.3's
8
+ * inflate in WebAssembly, `zlib-ng/`: each call reports exactly the input it consumed, so the
9
+ * next entry's header is the next byte, and stops at a member's end, its Adler-32 checked, without
10
+ * reading on. Its consumption contract is zlib's own: `next_in` and `avail_in` say what's left of
11
+ * the input, `next_out` and `avail_out` of the output. Output goes into a buffer of a fixed size,
12
+ * never one that grows; the inflater keeps its 32 KiB window, its state and its two buffers, in
13
+ * the instance's memory. No high-level inflater, `DecompressionStream` or search for a zlib
14
+ * signature is used: a high-level one gathers its output and joins members.
15
+ */
16
+ /**
17
+ * Each of an inflater's two buffers in the instance's memory: the one it copies a chunk's bytes
18
+ * into, and the one it inflates into. The pack reader's input comes in chunks of about this, and
19
+ * its output goes out in pieces of `packReadBytes`, the same.
20
+ */
21
+ const inflateWindow = 65536;
22
+ /**
23
+ * The bytes an inflater keeps between calls, at most, all in the instance's memory: its stream,
24
+ * window and state, 42,144 bytes with zlib-ng 2.3.3, and its two buffers, `inflateWindow` each.
25
+ * `zlib-ng/instance.test.ts` measures it.
26
+ */
27
+ const inflaterBytes = 174080;
28
+ /**
29
+ * A member that couldn't be inflated: `zlib` for bytes zlib refused or an instance that trapped,
30
+ * `no progress` for a call that neither used its input nor filled its output, and `start` for an
31
+ * inflater that didn't start.
22
32
  */
23
33
  var InflateError = class extends Schema.TaggedError()("InflateError", {
24
34
  fault: Schema.Literals([
@@ -29,116 +39,171 @@ var InflateError = class extends Schema.TaggedError()("InflateError", {
29
39
  message: Schema.String
30
40
  }) {};
31
41
  const voidResult = Result.succeed(void 0);
32
- const freshStream = () => ({
33
- input: null,
34
- next_in: 0,
35
- avail_in: 0,
36
- total_in: 0,
37
- output: null,
38
- next_out: 0,
39
- avail_out: 0,
40
- total_out: 0,
41
- msg: "",
42
- state: null,
43
- data_type: 2,
44
- adler: 0
42
+ /** A failure inside the instance, as `InflateError` names it. */
43
+ const inflateError = (failure) => new InflateError({
44
+ fault: failure.kind === "start" || failure.kind === "memory" ? "start" : "zlib",
45
+ message: failure.message
45
46
  });
46
- /** One call of zlib's `inflate`, its outcome named, neither buffer kept after it. */
47
- const inflateOnce = (stream, input, output) => {
48
- stream.input = input;
49
- stream.next_in = 0;
50
- stream.avail_in = input.byteLength;
51
- stream.output = output;
52
- stream.next_out = 0;
53
- stream.avail_out = output.byteLength;
54
- const status = zlib.inflate(stream, zNoFlush);
55
- const consumed = stream.next_in;
56
- const produced = stream.next_out;
57
- const left = stream.avail_in;
58
- const room = stream.avail_out;
59
- const message = stream.msg;
60
- stream.input = null;
61
- stream.output = null;
62
- stream.avail_in = 0;
63
- if (status === zStreamEnd) return Result.succeed({
64
- kind: "end",
65
- consumed,
66
- produced
67
- });
68
- if (status === zOk || status === zBufError) {
69
- if (left === 0) return Result.succeed({
70
- kind: "more",
71
- consumed,
72
- produced
73
- });
74
- if (room === 0) return Result.succeed({
75
- kind: "full",
76
- consumed,
77
- produced
78
- });
79
- return Result.fail(new InflateError({
80
- fault: "no progress",
81
- message: "zlib made no progress"
82
- }));
47
+ const noProgress = () => Result.fail(new InflateError({
48
+ fault: "no progress",
49
+ message: "zlib made no progress"
50
+ }));
51
+ const empty = /* @__PURE__ */ new Uint8Array(0);
52
+ const ended = () => Result.fail(new InflateError({
53
+ fault: "start",
54
+ message: "the inflater ended"
55
+ }));
56
+ /**
57
+ * A stream and its two buffers, placed together; none if one can't be. `instance.test.ts` measures
58
+ * what it places against `inflaterBytes`.
59
+ */
60
+ const placeInflater = (on) => {
61
+ const opened = call(on, (exports) => exports.zn_open());
62
+ if (Result.isFailure(opened)) return Result.fail(opened.failure);
63
+ const stream = opened.success;
64
+ if (stream === 0) return Result.fail(openFailure(on, "inflater"));
65
+ const placed = call(on, (exports) => exports.zn_alloc(2 * inflateWindow));
66
+ if (Result.isFailure(placed)) return Result.fail(placed.failure);
67
+ if (placed.success === 0) {
68
+ const failure = openFailure(on, "inflater");
69
+ call(on, (exports) => exports.zn_close(stream));
70
+ return Result.fail(failure);
83
71
  }
84
- return Result.fail(new InflateError({
85
- fault: "zlib",
86
- message: message === "" ? `zlib ${status}` : message
87
- }));
88
- };
89
- const resetStream = (stream) => {
90
- const state = stream.state;
91
- if (state === null || state.lendyn === null || state.distdyn === null) return Result.fail(new InflateError({
92
- fault: "start",
93
- message: "pako's inflater didn't reset"
94
- }));
95
- state.wsize = 0;
96
- state.whave = 0;
97
- state.wnext = 0;
98
- stream.total_in = 0;
99
- stream.total_out = 0;
100
- state.total = 0;
101
- stream.msg = "";
102
- stream.adler = state.wrap === 0 ? stream.adler : state.wrap & 1;
103
- state.mode = headMode;
104
- state.last = 0;
105
- state.havedict = 0;
106
- state.flags = -1;
107
- state.dmax = 32768;
108
- state.head = null;
109
- state.hold = 0;
110
- state.bits = 0;
111
- state.lencode = state.lendyn;
112
- state.distcode = state.distdyn;
113
- state.sane = 1;
114
- state.back = -1;
115
- if (state.window !== null) state.wsize = 1 << state.wbits;
116
- return voidResult;
72
+ return Result.succeed({
73
+ stream,
74
+ buffers: placed.success
75
+ });
117
76
  };
118
- /** Opens a member inflater synchronously; the caller must call `end()` when finished with it. */
119
- const openMemberInflater = () => {
120
- const stream = freshStream();
121
- if (zlib.inflateInit2(stream, zlibWindowBits) !== zOk) return Result.fail(new InflateError({
122
- fault: "start",
123
- message: "pako's inflater didn't start"
124
- }));
125
- let ended = false;
77
+ /** Opens a chunk inflater synchronously; the caller must call `end()` when finished with it. */
78
+ const openChunkInflater = () => {
79
+ const made = instance();
80
+ if (Result.isFailure(made)) return Result.fail(inflateError(made.failure));
81
+ const on = made.success;
82
+ const placed = placeInflater(on);
83
+ if (Result.isFailure(placed)) return Result.fail(inflateError(placed.failure));
84
+ const { stream, buffers } = placed.success;
85
+ const inputAt = buffers;
86
+ const outputAt = buffers + inflateWindow;
87
+ let chunk = empty;
88
+ let loadedFrom = 0;
89
+ let loadedLength = 0;
90
+ let over = false;
91
+ const take = (next) => {
92
+ chunk = next;
93
+ loadedFrom = 0;
94
+ loadedLength = 0;
95
+ };
96
+ /**
97
+ * As many calls of `zn_inflate` as it takes for the chunk or `capacity` to run out or the
98
+ * member to end, each part of the chunk copied in once, when a call first reads it. Only a
99
+ * call that leaves both unfinished, with the input buffer still holding bytes, made no
100
+ * progress.
101
+ */
102
+ const inflateAt = (at, capacity) => {
103
+ if (over) return ended();
104
+ const room = Math.min(capacity, inflateWindow);
105
+ let consumed = 0;
106
+ let produced = 0;
107
+ for (;;) {
108
+ const from = at + consumed;
109
+ if (from < loadedFrom || from >= loadedFrom + loadedLength) {
110
+ loadedFrom = from;
111
+ loadedLength = Math.min(chunk.byteLength - from, inflateWindow);
112
+ on.view.set(chunk.subarray(from, from + loadedLength), inputAt);
113
+ }
114
+ const stepped = call(on, (exports) => {
115
+ return {
116
+ status: exports.zn_inflate(stream, inputAt + from - loadedFrom, loadedFrom + loadedLength - from, outputAt + produced, room - produced),
117
+ used: exports.zn_consumed(),
118
+ made: exports.zn_produced()
119
+ };
120
+ });
121
+ if (Result.isFailure(stepped)) return Result.fail(inflateError(stepped.failure));
122
+ const { status, used, made } = stepped.success;
123
+ consumed += used;
124
+ produced += made;
125
+ if (status === zlibStatus.end) return Result.succeed({
126
+ kind: "end",
127
+ consumed,
128
+ produced
129
+ });
130
+ if (status === zlibStatus.error) {
131
+ const message = cString(on, on.exports.zn_message(stream));
132
+ return Result.fail(new InflateError({
133
+ fault: "zlib",
134
+ message: message || "zlib refused"
135
+ }));
136
+ }
137
+ if (at + consumed >= chunk.byteLength) return Result.succeed({
138
+ kind: "more",
139
+ consumed,
140
+ produced
141
+ });
142
+ if (produced === room) return Result.succeed({
143
+ kind: "full",
144
+ consumed,
145
+ produced
146
+ });
147
+ if (status === zlibStatus.noProgress) return noProgress();
148
+ }
149
+ };
126
150
  return Result.succeed({
127
- inflate: (input, output) => inflateOnce(stream, input, output),
128
- reset: () => resetStream(stream),
151
+ take,
152
+ inflateAt,
153
+ output: (length) => on.view.subarray(outputAt, outputAt + length),
154
+ inflate: (input, output) => {
155
+ take(input);
156
+ let consumed = 0;
157
+ let produced = 0;
158
+ for (;;) {
159
+ const step = inflateAt(consumed, Math.min(output.byteLength - produced, inflateWindow));
160
+ if (Result.isFailure(step)) {
161
+ take(empty);
162
+ return step;
163
+ }
164
+ const { kind } = step.success;
165
+ output.set(on.view.subarray(outputAt, outputAt + step.success.produced), produced);
166
+ consumed += step.success.consumed;
167
+ produced += step.success.produced;
168
+ if (kind === "end" || kind === "more" || produced === output.byteLength) {
169
+ take(empty);
170
+ return Result.succeed({
171
+ kind,
172
+ consumed,
173
+ produced
174
+ });
175
+ }
176
+ }
177
+ },
178
+ reset: () => {
179
+ if (over) return ended();
180
+ const reset = call(on, (exports) => exports.zn_reset(stream));
181
+ if (Result.isFailure(reset)) return Result.fail(inflateError(reset.failure));
182
+ return reset.success === 0 ? voidResult : Result.fail(new InflateError({
183
+ fault: "start",
184
+ message: "zlib-ng's inflater didn't reset"
185
+ }));
186
+ },
129
187
  end: () => {
130
- if (ended) return;
131
- ended = true;
132
- zlib.inflateEnd(stream);
188
+ if (over) return;
189
+ over = true;
190
+ chunk = empty;
191
+ if (!on.live) return;
192
+ call(on, (exports) => {
193
+ exports.zn_release(buffers);
194
+ exports.zn_close(stream);
195
+ });
133
196
  }
134
197
  });
135
198
  };
136
- /**
137
- * An inflater of zlib members, one after another, `reset` between them, its window and tables let
138
- * go when its scope closes.
139
- */
140
- const memberInflater = Effect.acquireRelease(Effect.suspend(() => {
141
- const opened = openMemberInflater();
199
+ /** A chunk inflater, its stream and buffers freed when its scope closes. */
200
+ const chunkInflater = Effect.acquireRelease(Effect.suspend(() => {
201
+ const opened = openChunkInflater();
142
202
  return Result.isSuccess(opened) ? Effect.succeed(opened.success) : Effect.fail(opened.failure);
143
203
  }), (inflater) => Effect.sync(() => inflater.end()));
144
- export { InflateError, inflaterBytes, memberInflater, openMemberInflater };
204
+ /**
205
+ * An inflater of zlib members, one after another, `reset` between them, its stream freed when
206
+ * its scope closes.
207
+ */
208
+ const memberInflater = chunkInflater;
209
+ export { InflateError, chunkInflater, inflateWindow, inflaterBytes, memberInflater, openChunkInflater, placeInflater };