{"draft":null,"from":116,"to":318,"files":[{"path":"server/scripts/capacity-git-bounded-fifo.mjs","added":61,"removed":0,"diff":"--- /dev/null\n+++ b/server/scripts/capacity-git-bounded-fifo.mjs\n@@ -0,0 +1,61 @@\n+/** One pending chunk; a producer cannot advance until the consumer takes it. */\n+export class BoundedPackFIFO {\n+\tconstructor(cancel = async () => {}) {\n+\t\tthis.cancel = cancel;\n+\t\tthis.ended = false;\n+\t\tthis.error = undefined;\n+\t\tthis.pending = undefined;\n+\t\tthis.reader = undefined;\n+\t\tthis.writer = undefined;\n+\t}\n+\tasync write(value) {\n+\t\tif (this.ended) throw this.error ?? Error(\"Pack stream is closed\");\n+\t\tif (this.pending !== undefined || this.writer) throw Error(\"Concurrent pack writes\");\n+\t\tif (this.reader) {\n+\t\t\tconst reader = this.reader;\n+\t\t\tthis.reader = undefined;\n+\t\t\treader.resolve({ value, done: false });\n+\t\t\treturn;\n+\t\t}\n+\t\tthis.pending = value;\n+\t\tawait new Promise((resolve, reject) => {\n+\t\t\tthis.writer = { resolve, reject };\n+\t\t});\n+\t}\n+\tasync next() {\n+\t\tif (this.error) throw this.error;\n+\t\tif (this.pending !== undefined) {\n+\t\t\tconst value = this.pending;\n+\t\t\tthis.pending = undefined;\n+\t\t\tthis.writer?.resolve();\n+\t\t\tthis.writer = undefined;\n+\t\t\treturn { value, done: false };\n+\t\t}\n+\t\tif (this.ended) return { done: true };\n+\t\tif (this.reader) throw Error(\"Concurrent pack reads\");\n+\t\treturn new Promise((resolve, reject) => {\n+\t\t\tthis.reader = { resolve, reject };\n+\t\t});\n+\t}\n+\tend() {\n+\t\tthis.ended = true;\n+\t\tthis.reader?.resolve({ done: true });\n+\t\tthis.reader = undefined;\n+\t}\n+\tdestroy(error) {\n+\t\tthis.error = error;\n+\t\tthis.ended = true;\n+\t\tthis.pending = undefined;\n+\t\tthis.reader?.reject(error);\n+\t\tthis.writer?.reject(error);\n+\t\tthis.reader = this.writer = undefined;\n+\t}\n+\tasync return() {\n+\t\tthis.destroy(Error(\"Pack consumer cancelled\"));\n+\t\tawait this.cancel();\n+\t\treturn { done: true };\n+\t}\n+\t[Symbol.asyncIterator]() {\n+\t\treturn this;\n+\t}\n+}"}],"unmatched":[]}