@warmhub/sdk-ts 0.92.0 → 0.93.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.
@@ -1,2 +1,2 @@
1
- import {G as G$1,F,H as H$1,L as L$1,D as D$1,I as I$1,J as J$1,K as K$1,E as E$1}from'./chunk-7BY5T6XV.js';import {mkdtemp,chmod,rm,open}from'fs/promises';import {tmpdir}from'os';import {join}from'path';import {createHash}from'crypto';import {Writable}from'stream';import {pipeline}from'stream/promises';import {createInflateRaw,crc32}from'zlib';import {once}from'events';import {createWriteStream,createReadStream}from'fs';var u=class extends Error{constructor(e){super(tt(e));this.reason=e;}reason;name="RepositoryCheckpointVerificationError"};function tt(r){switch(r){case "source_failed":return "Repository checkpoint source could not be read";case "archive_invalid":return "Repository checkpoint archive is invalid";case "archive_integrity_mismatch":return "Repository checkpoint archive integrity does not match";case "manifest_invalid":return "Repository checkpoint manifest is invalid";case "chunk_invalid":return "Repository checkpoint chunk is invalid";case "duplicate_durable_id":return "Repository checkpoint contains a duplicate durable identity";case "scratch_cleanup_failed":return "Repository checkpoint verifier scratch cleanup failed"}}var z=67324752,ot=33639248,at=134695760,ct=101075792,ht=117853008,dt=101010256,H=2056,Y=8,A=45,Z=813,D=65535,l=4294967295,q=33,ut=33188,L=64*1024,ft=new RegExp(`^records/[0-9a-f]{64}/part-\\d{${E$1}}\\.jsonl$`),yt=new TextDecoder("utf-8",{fatal:true}),w=class extends Error{};async function W(r,t){let e=new S(r);try{let i=[],n=new Set,s=!1;for(;await e.peekU32()===z;){if(s)throw new Error("manifest must be the final entry");let a=BigInt(e.position),d=await e.readExact(30);wt(d);let o=d.readUInt16LE(26),y=d.readUInt16LE(28);if(o===0||y!==20)throw new Error("local entry metadata is invalid");let c=G(await e.readExact(o));if(bt(c),n.has(c))throw new Error("archive entry is duplicated");n.add(c),s=c===F,Et(await e.readExact(y));let p=t(c),P=await lt(e,p),Q=await e.readExact(24);gt(Q,P),await p.finish(),i.push({name:c,localOffset:a,...P});}if(!s||i.length===0)throw new Error("archive manifest entry is missing");return await mt(e,i),{...await e.finish(),entryNames:i.map(a=>a.name)}}finally{await e.close();}}async function lt(r,t){let e=createInflateRaw({chunkSize:L}),i=0n,n=0n,s=0,h=new Writable({highWaterMark:L,write(o,y,c){t.write(o).then(()=>{s=crc32(o,s),n+=BigInt(o.byteLength),c();}).catch(p=>{c(p instanceof Error?p:new Error("checkpoint sink failed"));});}}),a=pipeline(e,h),d=a.then(()=>new Promise(()=>{}),o=>Promise.reject(o));d.catch(()=>{});try{for(;!e.readableEnded;){let o=await r.readSome(L);if(!o)throw new Error("archive entry is truncated");let y=e.bytesWritten;await pt(e,o,d);let c=e.bytesWritten-y;if(!Number.isSafeInteger(c)||c<0||c>o.byteLength)throw new Error("archive inflater consumed invalid input");if(i+=BigInt(c),c<o.byteLength&&r.unread(o.subarray(c)),e.readableEnded)break;if(c===0)throw new Error("archive inflater made no progress")}return await a,{crc:s>>>0,compressedSize:i,uncompressedSize:n}}finally{e.destroyed||e.destroy(),await a.catch(()=>{});}}async function pt(r,t,e){let i=new Promise((n,s)=>{r.write(t,h=>{h?s(h):n();});});await Promise.race([i,e]);}async function mt(r,t){let e=BigInt(r.position);for(let d of t){let o=await r.readExact(46);vt(o,d);let y=o.readUInt16LE(28),c=o.readUInt16LE(30);if(c!==28||o.readUInt16LE(32)!==0)throw new Error("central entry metadata is invalid");if(G(await r.readExact(y))!==d.name)throw new Error("central entry order differs");It(await r.readExact(c),d);}let i=BigInt(r.position)-e,n=BigInt(r.position),s=await r.readExact(56);if(m(s,0,ct),s.readBigUInt64LE(4)!==44n||s.readUInt16LE(12)!==Z||s.readUInt16LE(14)!==A||s.readUInt32LE(16)!==0||s.readUInt32LE(20)!==0||s.readBigUInt64LE(24)!==BigInt(t.length)||s.readBigUInt64LE(32)!==BigInt(t.length)||s.readBigUInt64LE(40)!==i||s.readBigUInt64LE(48)!==e)throw new Error("ZIP64 end record is invalid");let h=await r.readExact(20);if(m(h,0,ht),h.readUInt32LE(4)!==0||h.readBigUInt64LE(8)!==n||h.readUInt32LE(16)!==1)throw new Error("ZIP64 locator is invalid");let a=await r.readExact(22);if(m(a,0,dt),a.readUInt16LE(4)!==0||a.readUInt16LE(6)!==0||a.readUInt16LE(8)!==D||a.readUInt16LE(10)!==D||a.readUInt32LE(12)!==l||a.readUInt32LE(16)!==l||a.readUInt16LE(20)!==0)throw new Error("classic ZIP end record is invalid")}function wt(r){if(m(r,0,z),r.readUInt16LE(4)!==A||r.readUInt16LE(6)!==H||r.readUInt16LE(8)!==Y||r.readUInt16LE(10)!==0||r.readUInt16LE(12)!==q||r.readUInt32LE(14)!==0||r.readUInt32LE(18)!==l||r.readUInt32LE(22)!==l)throw new Error("local entry header is invalid")}function Et(r){if(r.readUInt16LE(0)!==1||r.readUInt16LE(2)!==16||r.readBigUInt64LE(4)!==0n||r.readBigUInt64LE(12)!==0n)throw new Error("local ZIP64 extra is invalid")}function gt(r,t){if(m(r,0,at),r.readUInt32LE(4)!==t.crc||r.readBigUInt64LE(8)!==t.compressedSize||r.readBigUInt64LE(16)!==t.uncompressedSize)throw new Error("archive data descriptor is invalid")}function vt(r,t){if(m(r,0,ot),r.readUInt16LE(4)!==Z||r.readUInt16LE(6)!==A||r.readUInt16LE(8)!==H||r.readUInt16LE(10)!==Y||r.readUInt16LE(12)!==0||r.readUInt16LE(14)!==q||r.readUInt32LE(16)!==t.crc||r.readUInt32LE(20)!==l||r.readUInt32LE(24)!==l||r.readUInt16LE(34)!==0||r.readUInt16LE(36)!==0||(r.readUInt32LE(38)>>>16&65535)!==ut||r.readUInt32LE(42)!==l)throw new Error("central entry header is invalid")}function It(r,t){if(r.readUInt16LE(0)!==1||r.readUInt16LE(2)!==24||r.readBigUInt64LE(4)!==t.uncompressedSize||r.readBigUInt64LE(12)!==t.compressedSize||r.readBigUInt64LE(20)!==t.localOffset)throw new Error("central ZIP64 extra is invalid")}function bt(r){if(r!==F&&!ft.test(r))throw new Error("archive entry name is invalid")}function G(r){let t=yt.decode(r);if(Buffer.byteLength(t,"utf8")!==r.byteLength)throw new Error("archive entry name is not canonical UTF-8");return t}function m(r,t,e){if(r.readUInt32LE(t)!==e)throw new Error("archive signature is invalid")}var S=class{#i;#e=createHash("sha256");#t=[];#r=0;#n=false;#o=0;position=0;constructor(t){this.#i=kt(t)[Symbol.asyncIterator]();}async peekU32(){let t=await this.readExact(4);return this.unread(t),t.readUInt32LE(0)}async readExact(t){let e=Buffer.allocUnsafe(t),i=0;for(;i<t;){let n=await this.readSome(t-i);if(!n)throw new Error("archive is truncated");e.set(n,i),i+=n.byteLength;}return e}async readSome(t){for(;this.#t.length===0;)if(!await this.#s())return;let e=this.#t[0],i=e.byteLength-this.#r,n=Math.min(t,i),s=e.subarray(this.#r,this.#r+n);return this.#r+=n,this.position+=n,this.#r===e.byteLength&&(this.#t.shift(),this.#r=0),s}unread(t){if(t.byteLength!==0){if(this.#r>0){let e=this.#t[0].subarray(this.#r);this.#t[0]=e,this.#r=0;}this.#t.unshift(t),this.position-=t.byteLength;}}async finish(){if(this.#t.length>0||await this.#s())throw new Error("archive contains trailing bytes");return {byteLength:this.#o,sha256:this.#e.digest("hex")}}async close(){try{await this.#i.return?.();}catch{}}async#s(){if(this.#n)return false;for(;;){let t;try{t=await this.#i.next();}catch{throw new w}if(t.done)return this.#n=true,false;if(!(t.value instanceof Uint8Array))throw new w;if(t.value.byteLength!==0){if(this.#e.update(t.value),this.#o+=t.value.byteLength,!Number.isSafeInteger(this.#o))throw new Error("archive byte length is unsafe");return this.#t.push(t.value),true}}}};function kt(r){return Symbol.asyncIterator in r?r:{async*[Symbol.asyncIterator](){let t=r.getReader(),e=false;try{for(;;){let{value:i,done:n}=await t.read();if(n){e=!0;return}yield i;}}finally{e||await t.cancel().catch(()=>{}),t.releaseLock();}}}}var _t=4*1024*1024,Bt=8,K=64*1024,Pt=16*1024,f=4,j=4,Nt=1,xt=f+Nt,J=xt+j,E=class extends Error{},b=class{#i;#e;#t=[];#r=0;#n;constructor(t,e={}){this.#i=t;let i=e.sortBudgetBytes??_t;if(this.#n=e.mergeFanIn??Bt,!Number.isSafeInteger(i)||i<J||!Number.isSafeInteger(this.#n)||this.#n<2)throw new Error("checkpoint identity sort limits are invalid");this.#e=new R(i);}add(t){let e=Buffer.byteLength(t,"utf8");if(!this.#e.canFit(e))throw new Error("checkpoint durable identity exceeds the sort budget");if(this.#e.canAdd(e)){this.#e.add(t,e);return}return this.#o(t,e)}async finish(){if(this.#t.length===0){this.#e.sortAndAssertUnique(),this.#e.clear();return}for(this.#e.isEmpty||await this.#o();this.#t.length>this.#n;){let e=[];for(let i=0;i<this.#t.length;i+=this.#n){let n=this.#t.slice(i,i+this.#n),s=this.#s();await Vt(n,s),await Promise.all(n.map(h=>rm(h,{force:true}))),e.push(s);}this.#t=e;}let t;await X(this.#t,e=>{if(t&&_(t,e)===0)throw new E;t=e;});}async#o(t,e){let i=this.#s();this.#e.sortAndAssertUnique(),await Tt(i,this.#e),this.#t.push(i),this.#e.clear(),t!==void 0&&e!==void 0&&this.#e.add(t,e);}#s(){let t=join(this.#i,`identity-run-${String(this.#r).padStart(8,"0")}.bin`);return this.#r+=1,t}},R=class{byteBudget;#i;#e;#t=0;#r=0;constructor(t){this.byteBudget=t;let e=Math.floor(t/J);this.#e=new Uint32Array(e),this.#i=Buffer.allocUnsafe(t-e*j);}get isEmpty(){return this.#r===0}canAdd(t){return this.canFit(t)&&this.#r<this.#e.length&&this.#t+f+t<=this.#i.length}canFit(t){return t<=4294967295&&this.#e.length>0&&f+t<=this.#i.length}add(t,e){if(!this.canAdd(e))throw new Error("checkpoint durable identity exceeds the sort budget");let i=this.#t;this.#e[this.#r]=i,this.#i.writeUInt32BE(e,i),this.#i.write(t,i+f,e,"utf8"),this.#t+=f+e,this.#r+=1;}sortAndAssertUnique(){let t=this.#e.subarray(0,this.#r);t.sort((e,i)=>this.#n(e,i));for(let e=1;e<t.length;e+=1)if(this.#n(t[e-1],t[e])===0)throw new E}clear(){this.#t=0,this.#r=0;}forEachSorted(t){return Ot(this.#e.subarray(0,this.#r),e=>t(this.#s(e)))}#n(t,e){return _(this.#o(t),this.#o(e))}#o(t){let e=this.#i.readUInt32BE(t);return this.#i.subarray(t+f,t+f+e)}#s(t){let e=this.#i.readUInt32BE(t);return this.#i.subarray(t,t+f+e)}};async function Tt(r,t){await $(r,async e=>{let i=new k(e);await t.forEachSorted(n=>i.write(n)),await i.finish();});}async function Vt(r,t){await $(t,async e=>{let i=new k(e);await X(r,n=>i.writeValue(n)),await i.finish();});}async function $(r,t){await(await open(r,"wx",384)).close();let i=createWriteStream(r,{flags:"a",mode:384,highWaterMark:K});try{await t(i),i.end(),await once(i,"close");}catch(n){throw i.destroy(),n}}var k=class{#i;#e=Buffer.allocUnsafe(K);#t=0;constructor(t){this.#i=t;}write(t){if(t.byteLength>this.#e.length)return this.#r(t);if(this.#t+t.byteLength>this.#e.length)return this.#n(t);this.#e.set(t,this.#t),this.#t+=t.byteLength;}writeValue(t){let e=f+t.byteLength;if(e>this.#e.length)return this.#o(t);if(this.#t+e>this.#e.length)return this.#s(t);this.#c(t);}async finish(){await this.#a();}async#r(t){await this.#a(),await I(this.#i,t);}async#n(t){await this.#a(),this.#e.set(t,this.#t),this.#t+=t.byteLength;}async#o(t){await this.#a();let e=Buffer.allocUnsafe(f);e.writeUInt32BE(t.byteLength,0),await I(this.#i,e),await I(this.#i,t);}async#s(t){await this.#a(),this.#c(t);}async#a(){if(this.#t===0)return;let t=this.#e.subarray(0,this.#t);await I(this.#i,t),this.#t=0;}#c(t){this.#e.writeUInt32BE(t.byteLength,this.#t),this.#e.set(t,this.#t+f),this.#t+=f+t.byteLength;}};async function X(r,t){let e=r.map(i=>new C(i));try{for(await Promise.all(e.map(i=>i.advance()));;){let i;for(let s of e)s.current&&(!i||_(s.current,i.current)<0)&&(i=s);if(!i?.current)return;let n=t(i.current);n&&await n,await i.advance();}}finally{for(let i of e)i.close();}}var C=class{#i;#e;#t=Buffer.alloc(0);#r=0;current;constructor(t){this.#i=createReadStream(t,{highWaterMark:Pt}),this.#e=this.#i[Symbol.asyncIterator]();}async advance(){let t=await this.#n(f);if(!t){this.current=void 0;return}let e=t.readUInt32BE(0);if(e===0)throw new Error("checkpoint identity run is invalid");let i=await this.#n(e);if(!i)throw new Error("checkpoint identity run is truncated");this.current=i;}close(){this.#i.destroy();}async#n(t){if(t===0)return Buffer.alloc(0);if(this.#t.length-this.#r>=t){let n=this.#t.subarray(this.#r,this.#r+t);return this.#r+=t,n}let e=Buffer.allocUnsafe(t),i=0;for(;i<t;){if(this.#r<this.#t.length){let s=Math.min(t-i,this.#t.length-this.#r);this.#t.copy(e,i,this.#r,this.#r+s),this.#r+=s,i+=s;continue}let n=await this.#e.next();if(n.done){if(i===0)return;throw new Error("checkpoint identity run is truncated")}this.#t=n.value,this.#r=0;}return e}};async function Ot(r,t){for(let e of r){let i=t(e);i&&await i;}}async function I(r,t){await new Promise((e,i)=>{r.write(t,n=>{n?i(n):e();});});}function _(r,t){let e=Math.min(r.length,t.length);for(let i=0;i<e;i+=1){let n=r[i]-t[i];if(n!==0)return n}return r.length-t.length}var U=class{#i;#e=new Map;#t=new Map;#r=[];#n;constructor(t,e={}){this.#i=new b(t,{sortBudgetBytes:e.identitySortBudgetBytes,mergeFanIn:e.identityMergeFanIn});}openEntry(t){if(t===F){let e=[];return {write:async i=>{e.push(i.slice());},finish:async()=>{this.#n=Buffer.concat(e);}}}return new B(t,this.#e,this.#t,this.#i,e=>this.#r.push(e))}async finish(){if(!this.#n)throw new Error("checkpoint manifest is missing");let t=await H$1(this.#n),e=t.streams.flatMap(i=>i.chunks.map(n=>({...n,shapeName:i.shapeName})));if(e.length!==this.#r.length)throw new Error("checkpoint chunk inventory does not match");for(let[i,n]of e.entries()){let s=this.#r[i];if(s.path!==n.path||s.shapeName!==n.shapeName||s.recordCount!==n.recordCount||s.byteLength!==n.byteLength||s.sha256!==n.sha256)throw new Error("checkpoint chunk integrity does not match")}for(let i of t.streams){let n=this.#e.get(i.shapeName);if(!n||L$1(n)!==i.recordCount)throw new Error("checkpoint stream does not match its manifest")}if(this.#e.size!==t.streams.length)throw new Error("checkpoint contains an undeclared stream");return await this.#i.finish(),{manifest:t,chunkCount:e.length}}},B=class{#i;#e;#t;#r;#n;#o=createHash("sha256");#s=[];#a=0;#c;#h=0;#d=0;constructor(t,e,i,n,s){this.#i=t,this.#e=e,this.#t=i,this.#r=n,this.#n=s;}async write(t){if(this.#o.update(t),this.#d+=t.byteLength,!Number.isSafeInteger(this.#d))throw new Error("checkpoint chunk byte length is unsafe");let e=0;for(;e<t.byteLength;){let i=t.indexOf(10,e);if(i<0)break;let n=this.#u(this.#y(t.subarray(e,i+1)));n&&await n,e=i+1;}e<t.byteLength&&this.#f(t.subarray(e));}async finish(){if(this.#s.length!==0||this.#h===0||!this.#c)throw new Error("checkpoint chunk framing is invalid");if(this.#d>D$1&&this.#h!==1)throw new Error("oversized checkpoint chunk has multiple rows");this.#n({path:this.#i,shapeName:this.#c,recordCount:this.#h,byteLength:this.#d,sha256:this.#o.digest("hex")});let t=this.#t.get(this.#c);if(!t)throw new Error("checkpoint stream chunk state is missing");t.previous={byteLength:this.#d};}#u(t){let e=I$1(t),i=e.kind==="shape"?e.wref:e.shapeName;if(this.#c!==void 0&&this.#c!==i)throw new Error("checkpoint chunk mixes Shape streams");this.#c=i,this.#h===0&&this.#l(i,t.byteLength);let n=this.#e.get(i)??J$1(i);this.#e.set(i,n),K$1(n,e);let s=this.#r.add(e.durableId);if(this.#h+=1,!Number.isSafeInteger(this.#h))throw new Error("checkpoint chunk row count is unsafe");return s}#f(t){if(t.byteLength!==0&&(this.#s.push(t.slice()),this.#a+=t.byteLength,!Number.isSafeInteger(this.#a)))throw new Error("checkpoint row byte length is unsafe")}#y(t){if(this.#s.length===0)return t;let e=Buffer.concat([...this.#s,t],this.#a+t.byteLength);return this.#s=[],this.#a=0,e}#l(t,e){let i=Number(this.#i.match(/part-(\d{10})\.jsonl$/)?.[1]??-1),n=this.#t.get(t)??{nextPart:0};if(i!==n.nextPart)throw new Error("checkpoint stream chunk parts are not contiguous");if(n.previous&&n.previous.byteLength<=D$1&&n.previous.byteLength+e<=D$1)throw new Error("checkpoint stream chunk closed before the byte limit");n.nextPart+=1,this.#t.set(t,n);}};async function Zt(r,t){return qt(r,t)}async function qt(r,t,e={}){let i=await mkdtemp(join(e.scratchParent??tmpdir(),"warmhub-checkpoint-verify-"));await chmod(i,448);let n,s;try{let h=new U(i,e),a=await W(r,y=>h.openEntry(y)),{manifest:d,chunkCount:o}=await h.finish();Wt(t,a,d),n={valid:!0,checkpointId:d.checkpointId,repoSeq:d.repoSeq,recordCount:d.recordCount,chunkCount:o,totalBytes:a.byteLength,rootDigest:d.contentSha256,archiveDigest:a.sha256};}catch(h){s=Gt(h);}try{await rm(i,{recursive:!0,force:!0});}catch{s??=new u("scratch_cleanup_failed");}if(s)throw s;if(!n)throw new u("archive_invalid");return n}function Wt(r,t,e){if(r&&(G$1.parse(r.sha256),r.checkpointId!==e.checkpointId||r.repoSeq!==e.repoSeq||r.byteLength!==t.byteLength||r.sha256!==t.sha256))throw new u("archive_integrity_mismatch")}function Gt(r){if(r instanceof u)return r;if(r instanceof w)return new u("source_failed");if(r instanceof E)return new u("duplicate_durable_id");let t=r instanceof Error?r.message:"";return t.includes("manifest")?new u("manifest_invalid"):t.includes("chunk")||t.includes("row")||t.includes("Shape stream")?new u("chunk_invalid"):new u("archive_invalid")}export{u as RepositoryCheckpointVerificationError,Zt as verifyRepositoryCheckpointArchive};//# sourceMappingURL=checkpoint.js.map
1
+ import {G as G$1,F,H as H$1,L as L$1,D as D$1,I as I$1,J as J$1,K as K$1,E as E$1}from'./chunk-CC45V5YE.js';import {mkdtemp,chmod,rm,open}from'fs/promises';import {tmpdir}from'os';import {join}from'path';import {createHash}from'crypto';import {Writable}from'stream';import {pipeline}from'stream/promises';import {createInflateRaw,crc32}from'zlib';import {once}from'events';import {createWriteStream,createReadStream}from'fs';var u=class extends Error{constructor(e){super(tt(e));this.reason=e;}reason;name="RepositoryCheckpointVerificationError"};function tt(r){switch(r){case "source_failed":return "Repository checkpoint source could not be read";case "archive_invalid":return "Repository checkpoint archive is invalid";case "archive_integrity_mismatch":return "Repository checkpoint archive integrity does not match";case "manifest_invalid":return "Repository checkpoint manifest is invalid";case "chunk_invalid":return "Repository checkpoint chunk is invalid";case "duplicate_durable_id":return "Repository checkpoint contains a duplicate durable identity";case "scratch_cleanup_failed":return "Repository checkpoint verifier scratch cleanup failed"}}var z=67324752,ot=33639248,at=134695760,ct=101075792,ht=117853008,dt=101010256,H=2056,Y=8,A=45,Z=813,D=65535,l=4294967295,q=33,ut=33188,L=64*1024,ft=new RegExp(`^records/[0-9a-f]{64}/part-\\d{${E$1}}\\.jsonl$`),yt=new TextDecoder("utf-8",{fatal:true}),w=class extends Error{};async function W(r,t){let e=new S(r);try{let i=[],n=new Set,s=!1;for(;await e.peekU32()===z;){if(s)throw new Error("manifest must be the final entry");let a=BigInt(e.position),d=await e.readExact(30);wt(d);let o=d.readUInt16LE(26),y=d.readUInt16LE(28);if(o===0||y!==20)throw new Error("local entry metadata is invalid");let c=G(await e.readExact(o));if(bt(c),n.has(c))throw new Error("archive entry is duplicated");n.add(c),s=c===F,Et(await e.readExact(y));let p=t(c),P=await lt(e,p),Q=await e.readExact(24);gt(Q,P),await p.finish(),i.push({name:c,localOffset:a,...P});}if(!s||i.length===0)throw new Error("archive manifest entry is missing");return await mt(e,i),{...await e.finish(),entryNames:i.map(a=>a.name)}}finally{await e.close();}}async function lt(r,t){let e=createInflateRaw({chunkSize:L}),i=0n,n=0n,s=0,h=new Writable({highWaterMark:L,write(o,y,c){t.write(o).then(()=>{s=crc32(o,s),n+=BigInt(o.byteLength),c();}).catch(p=>{c(p instanceof Error?p:new Error("checkpoint sink failed"));});}}),a=pipeline(e,h),d=a.then(()=>new Promise(()=>{}),o=>Promise.reject(o));d.catch(()=>{});try{for(;!e.readableEnded;){let o=await r.readSome(L);if(!o)throw new Error("archive entry is truncated");let y=e.bytesWritten;await pt(e,o,d);let c=e.bytesWritten-y;if(!Number.isSafeInteger(c)||c<0||c>o.byteLength)throw new Error("archive inflater consumed invalid input");if(i+=BigInt(c),c<o.byteLength&&r.unread(o.subarray(c)),e.readableEnded)break;if(c===0)throw new Error("archive inflater made no progress")}return await a,{crc:s>>>0,compressedSize:i,uncompressedSize:n}}finally{e.destroyed||e.destroy(),await a.catch(()=>{});}}async function pt(r,t,e){let i=new Promise((n,s)=>{r.write(t,h=>{h?s(h):n();});});await Promise.race([i,e]);}async function mt(r,t){let e=BigInt(r.position);for(let d of t){let o=await r.readExact(46);vt(o,d);let y=o.readUInt16LE(28),c=o.readUInt16LE(30);if(c!==28||o.readUInt16LE(32)!==0)throw new Error("central entry metadata is invalid");if(G(await r.readExact(y))!==d.name)throw new Error("central entry order differs");It(await r.readExact(c),d);}let i=BigInt(r.position)-e,n=BigInt(r.position),s=await r.readExact(56);if(m(s,0,ct),s.readBigUInt64LE(4)!==44n||s.readUInt16LE(12)!==Z||s.readUInt16LE(14)!==A||s.readUInt32LE(16)!==0||s.readUInt32LE(20)!==0||s.readBigUInt64LE(24)!==BigInt(t.length)||s.readBigUInt64LE(32)!==BigInt(t.length)||s.readBigUInt64LE(40)!==i||s.readBigUInt64LE(48)!==e)throw new Error("ZIP64 end record is invalid");let h=await r.readExact(20);if(m(h,0,ht),h.readUInt32LE(4)!==0||h.readBigUInt64LE(8)!==n||h.readUInt32LE(16)!==1)throw new Error("ZIP64 locator is invalid");let a=await r.readExact(22);if(m(a,0,dt),a.readUInt16LE(4)!==0||a.readUInt16LE(6)!==0||a.readUInt16LE(8)!==D||a.readUInt16LE(10)!==D||a.readUInt32LE(12)!==l||a.readUInt32LE(16)!==l||a.readUInt16LE(20)!==0)throw new Error("classic ZIP end record is invalid")}function wt(r){if(m(r,0,z),r.readUInt16LE(4)!==A||r.readUInt16LE(6)!==H||r.readUInt16LE(8)!==Y||r.readUInt16LE(10)!==0||r.readUInt16LE(12)!==q||r.readUInt32LE(14)!==0||r.readUInt32LE(18)!==l||r.readUInt32LE(22)!==l)throw new Error("local entry header is invalid")}function Et(r){if(r.readUInt16LE(0)!==1||r.readUInt16LE(2)!==16||r.readBigUInt64LE(4)!==0n||r.readBigUInt64LE(12)!==0n)throw new Error("local ZIP64 extra is invalid")}function gt(r,t){if(m(r,0,at),r.readUInt32LE(4)!==t.crc||r.readBigUInt64LE(8)!==t.compressedSize||r.readBigUInt64LE(16)!==t.uncompressedSize)throw new Error("archive data descriptor is invalid")}function vt(r,t){if(m(r,0,ot),r.readUInt16LE(4)!==Z||r.readUInt16LE(6)!==A||r.readUInt16LE(8)!==H||r.readUInt16LE(10)!==Y||r.readUInt16LE(12)!==0||r.readUInt16LE(14)!==q||r.readUInt32LE(16)!==t.crc||r.readUInt32LE(20)!==l||r.readUInt32LE(24)!==l||r.readUInt16LE(34)!==0||r.readUInt16LE(36)!==0||(r.readUInt32LE(38)>>>16&65535)!==ut||r.readUInt32LE(42)!==l)throw new Error("central entry header is invalid")}function It(r,t){if(r.readUInt16LE(0)!==1||r.readUInt16LE(2)!==24||r.readBigUInt64LE(4)!==t.uncompressedSize||r.readBigUInt64LE(12)!==t.compressedSize||r.readBigUInt64LE(20)!==t.localOffset)throw new Error("central ZIP64 extra is invalid")}function bt(r){if(r!==F&&!ft.test(r))throw new Error("archive entry name is invalid")}function G(r){let t=yt.decode(r);if(Buffer.byteLength(t,"utf8")!==r.byteLength)throw new Error("archive entry name is not canonical UTF-8");return t}function m(r,t,e){if(r.readUInt32LE(t)!==e)throw new Error("archive signature is invalid")}var S=class{#i;#e=createHash("sha256");#t=[];#r=0;#n=false;#o=0;position=0;constructor(t){this.#i=kt(t)[Symbol.asyncIterator]();}async peekU32(){let t=await this.readExact(4);return this.unread(t),t.readUInt32LE(0)}async readExact(t){let e=Buffer.allocUnsafe(t),i=0;for(;i<t;){let n=await this.readSome(t-i);if(!n)throw new Error("archive is truncated");e.set(n,i),i+=n.byteLength;}return e}async readSome(t){for(;this.#t.length===0;)if(!await this.#s())return;let e=this.#t[0],i=e.byteLength-this.#r,n=Math.min(t,i),s=e.subarray(this.#r,this.#r+n);return this.#r+=n,this.position+=n,this.#r===e.byteLength&&(this.#t.shift(),this.#r=0),s}unread(t){if(t.byteLength!==0){if(this.#r>0){let e=this.#t[0].subarray(this.#r);this.#t[0]=e,this.#r=0;}this.#t.unshift(t),this.position-=t.byteLength;}}async finish(){if(this.#t.length>0||await this.#s())throw new Error("archive contains trailing bytes");return {byteLength:this.#o,sha256:this.#e.digest("hex")}}async close(){try{await this.#i.return?.();}catch{}}async#s(){if(this.#n)return false;for(;;){let t;try{t=await this.#i.next();}catch{throw new w}if(t.done)return this.#n=true,false;if(!(t.value instanceof Uint8Array))throw new w;if(t.value.byteLength!==0){if(this.#e.update(t.value),this.#o+=t.value.byteLength,!Number.isSafeInteger(this.#o))throw new Error("archive byte length is unsafe");return this.#t.push(t.value),true}}}};function kt(r){return Symbol.asyncIterator in r?r:{async*[Symbol.asyncIterator](){let t=r.getReader(),e=false;try{for(;;){let{value:i,done:n}=await t.read();if(n){e=!0;return}yield i;}}finally{e||await t.cancel().catch(()=>{}),t.releaseLock();}}}}var _t=4*1024*1024,Bt=8,K=64*1024,Pt=16*1024,f=4,j=4,Nt=1,xt=f+Nt,J=xt+j,E=class extends Error{},b=class{#i;#e;#t=[];#r=0;#n;constructor(t,e={}){this.#i=t;let i=e.sortBudgetBytes??_t;if(this.#n=e.mergeFanIn??Bt,!Number.isSafeInteger(i)||i<J||!Number.isSafeInteger(this.#n)||this.#n<2)throw new Error("checkpoint identity sort limits are invalid");this.#e=new R(i);}add(t){let e=Buffer.byteLength(t,"utf8");if(!this.#e.canFit(e))throw new Error("checkpoint durable identity exceeds the sort budget");if(this.#e.canAdd(e)){this.#e.add(t,e);return}return this.#o(t,e)}async finish(){if(this.#t.length===0){this.#e.sortAndAssertUnique(),this.#e.clear();return}for(this.#e.isEmpty||await this.#o();this.#t.length>this.#n;){let e=[];for(let i=0;i<this.#t.length;i+=this.#n){let n=this.#t.slice(i,i+this.#n),s=this.#s();await Vt(n,s),await Promise.all(n.map(h=>rm(h,{force:true}))),e.push(s);}this.#t=e;}let t;await X(this.#t,e=>{if(t&&_(t,e)===0)throw new E;t=e;});}async#o(t,e){let i=this.#s();this.#e.sortAndAssertUnique(),await Tt(i,this.#e),this.#t.push(i),this.#e.clear(),t!==void 0&&e!==void 0&&this.#e.add(t,e);}#s(){let t=join(this.#i,`identity-run-${String(this.#r).padStart(8,"0")}.bin`);return this.#r+=1,t}},R=class{byteBudget;#i;#e;#t=0;#r=0;constructor(t){this.byteBudget=t;let e=Math.floor(t/J);this.#e=new Uint32Array(e),this.#i=Buffer.allocUnsafe(t-e*j);}get isEmpty(){return this.#r===0}canAdd(t){return this.canFit(t)&&this.#r<this.#e.length&&this.#t+f+t<=this.#i.length}canFit(t){return t<=4294967295&&this.#e.length>0&&f+t<=this.#i.length}add(t,e){if(!this.canAdd(e))throw new Error("checkpoint durable identity exceeds the sort budget");let i=this.#t;this.#e[this.#r]=i,this.#i.writeUInt32BE(e,i),this.#i.write(t,i+f,e,"utf8"),this.#t+=f+e,this.#r+=1;}sortAndAssertUnique(){let t=this.#e.subarray(0,this.#r);t.sort((e,i)=>this.#n(e,i));for(let e=1;e<t.length;e+=1)if(this.#n(t[e-1],t[e])===0)throw new E}clear(){this.#t=0,this.#r=0;}forEachSorted(t){return Ot(this.#e.subarray(0,this.#r),e=>t(this.#s(e)))}#n(t,e){return _(this.#o(t),this.#o(e))}#o(t){let e=this.#i.readUInt32BE(t);return this.#i.subarray(t+f,t+f+e)}#s(t){let e=this.#i.readUInt32BE(t);return this.#i.subarray(t,t+f+e)}};async function Tt(r,t){await $(r,async e=>{let i=new k(e);await t.forEachSorted(n=>i.write(n)),await i.finish();});}async function Vt(r,t){await $(t,async e=>{let i=new k(e);await X(r,n=>i.writeValue(n)),await i.finish();});}async function $(r,t){await(await open(r,"wx",384)).close();let i=createWriteStream(r,{flags:"a",mode:384,highWaterMark:K});try{await t(i),i.end(),await once(i,"close");}catch(n){throw i.destroy(),n}}var k=class{#i;#e=Buffer.allocUnsafe(K);#t=0;constructor(t){this.#i=t;}write(t){if(t.byteLength>this.#e.length)return this.#r(t);if(this.#t+t.byteLength>this.#e.length)return this.#n(t);this.#e.set(t,this.#t),this.#t+=t.byteLength;}writeValue(t){let e=f+t.byteLength;if(e>this.#e.length)return this.#o(t);if(this.#t+e>this.#e.length)return this.#s(t);this.#c(t);}async finish(){await this.#a();}async#r(t){await this.#a(),await I(this.#i,t);}async#n(t){await this.#a(),this.#e.set(t,this.#t),this.#t+=t.byteLength;}async#o(t){await this.#a();let e=Buffer.allocUnsafe(f);e.writeUInt32BE(t.byteLength,0),await I(this.#i,e),await I(this.#i,t);}async#s(t){await this.#a(),this.#c(t);}async#a(){if(this.#t===0)return;let t=this.#e.subarray(0,this.#t);await I(this.#i,t),this.#t=0;}#c(t){this.#e.writeUInt32BE(t.byteLength,this.#t),this.#e.set(t,this.#t+f),this.#t+=f+t.byteLength;}};async function X(r,t){let e=r.map(i=>new C(i));try{for(await Promise.all(e.map(i=>i.advance()));;){let i;for(let s of e)s.current&&(!i||_(s.current,i.current)<0)&&(i=s);if(!i?.current)return;let n=t(i.current);n&&await n,await i.advance();}}finally{for(let i of e)i.close();}}var C=class{#i;#e;#t=Buffer.alloc(0);#r=0;current;constructor(t){this.#i=createReadStream(t,{highWaterMark:Pt}),this.#e=this.#i[Symbol.asyncIterator]();}async advance(){let t=await this.#n(f);if(!t){this.current=void 0;return}let e=t.readUInt32BE(0);if(e===0)throw new Error("checkpoint identity run is invalid");let i=await this.#n(e);if(!i)throw new Error("checkpoint identity run is truncated");this.current=i;}close(){this.#i.destroy();}async#n(t){if(t===0)return Buffer.alloc(0);if(this.#t.length-this.#r>=t){let n=this.#t.subarray(this.#r,this.#r+t);return this.#r+=t,n}let e=Buffer.allocUnsafe(t),i=0;for(;i<t;){if(this.#r<this.#t.length){let s=Math.min(t-i,this.#t.length-this.#r);this.#t.copy(e,i,this.#r,this.#r+s),this.#r+=s,i+=s;continue}let n=await this.#e.next();if(n.done){if(i===0)return;throw new Error("checkpoint identity run is truncated")}this.#t=n.value,this.#r=0;}return e}};async function Ot(r,t){for(let e of r){let i=t(e);i&&await i;}}async function I(r,t){await new Promise((e,i)=>{r.write(t,n=>{n?i(n):e();});});}function _(r,t){let e=Math.min(r.length,t.length);for(let i=0;i<e;i+=1){let n=r[i]-t[i];if(n!==0)return n}return r.length-t.length}var U=class{#i;#e=new Map;#t=new Map;#r=[];#n;constructor(t,e={}){this.#i=new b(t,{sortBudgetBytes:e.identitySortBudgetBytes,mergeFanIn:e.identityMergeFanIn});}openEntry(t){if(t===F){let e=[];return {write:async i=>{e.push(i.slice());},finish:async()=>{this.#n=Buffer.concat(e);}}}return new B(t,this.#e,this.#t,this.#i,e=>this.#r.push(e))}async finish(){if(!this.#n)throw new Error("checkpoint manifest is missing");let t=await H$1(this.#n),e=t.streams.flatMap(i=>i.chunks.map(n=>({...n,shapeName:i.shapeName})));if(e.length!==this.#r.length)throw new Error("checkpoint chunk inventory does not match");for(let[i,n]of e.entries()){let s=this.#r[i];if(s.path!==n.path||s.shapeName!==n.shapeName||s.recordCount!==n.recordCount||s.byteLength!==n.byteLength||s.sha256!==n.sha256)throw new Error("checkpoint chunk integrity does not match")}for(let i of t.streams){let n=this.#e.get(i.shapeName);if(!n||L$1(n)!==i.recordCount)throw new Error("checkpoint stream does not match its manifest")}if(this.#e.size!==t.streams.length)throw new Error("checkpoint contains an undeclared stream");return await this.#i.finish(),{manifest:t,chunkCount:e.length}}},B=class{#i;#e;#t;#r;#n;#o=createHash("sha256");#s=[];#a=0;#c;#h=0;#d=0;constructor(t,e,i,n,s){this.#i=t,this.#e=e,this.#t=i,this.#r=n,this.#n=s;}async write(t){if(this.#o.update(t),this.#d+=t.byteLength,!Number.isSafeInteger(this.#d))throw new Error("checkpoint chunk byte length is unsafe");let e=0;for(;e<t.byteLength;){let i=t.indexOf(10,e);if(i<0)break;let n=this.#u(this.#y(t.subarray(e,i+1)));n&&await n,e=i+1;}e<t.byteLength&&this.#f(t.subarray(e));}async finish(){if(this.#s.length!==0||this.#h===0||!this.#c)throw new Error("checkpoint chunk framing is invalid");if(this.#d>D$1&&this.#h!==1)throw new Error("oversized checkpoint chunk has multiple rows");this.#n({path:this.#i,shapeName:this.#c,recordCount:this.#h,byteLength:this.#d,sha256:this.#o.digest("hex")});let t=this.#t.get(this.#c);if(!t)throw new Error("checkpoint stream chunk state is missing");t.previous={byteLength:this.#d};}#u(t){let e=I$1(t),i=e.kind==="shape"?e.wref:e.shapeName;if(this.#c!==void 0&&this.#c!==i)throw new Error("checkpoint chunk mixes Shape streams");this.#c=i,this.#h===0&&this.#l(i,t.byteLength);let n=this.#e.get(i)??J$1(i);this.#e.set(i,n),K$1(n,e);let s=this.#r.add(e.durableId);if(this.#h+=1,!Number.isSafeInteger(this.#h))throw new Error("checkpoint chunk row count is unsafe");return s}#f(t){if(t.byteLength!==0&&(this.#s.push(t.slice()),this.#a+=t.byteLength,!Number.isSafeInteger(this.#a)))throw new Error("checkpoint row byte length is unsafe")}#y(t){if(this.#s.length===0)return t;let e=Buffer.concat([...this.#s,t],this.#a+t.byteLength);return this.#s=[],this.#a=0,e}#l(t,e){let i=Number(this.#i.match(/part-(\d{10})\.jsonl$/)?.[1]??-1),n=this.#t.get(t)??{nextPart:0};if(i!==n.nextPart)throw new Error("checkpoint stream chunk parts are not contiguous");if(n.previous&&n.previous.byteLength<=D$1&&n.previous.byteLength+e<=D$1)throw new Error("checkpoint stream chunk closed before the byte limit");n.nextPart+=1,this.#t.set(t,n);}};async function Zt(r,t){return qt(r,t)}async function qt(r,t,e={}){let i=await mkdtemp(join(e.scratchParent??tmpdir(),"warmhub-checkpoint-verify-"));await chmod(i,448);let n,s;try{let h=new U(i,e),a=await W(r,y=>h.openEntry(y)),{manifest:d,chunkCount:o}=await h.finish();Wt(t,a,d),n={valid:!0,checkpointId:d.checkpointId,repoSeq:d.repoSeq,recordCount:d.recordCount,chunkCount:o,totalBytes:a.byteLength,rootDigest:d.contentSha256,archiveDigest:a.sha256};}catch(h){s=Gt(h);}try{await rm(i,{recursive:!0,force:!0});}catch{s??=new u("scratch_cleanup_failed");}if(s)throw s;if(!n)throw new u("archive_invalid");return n}function Wt(r,t,e){if(r&&(G$1.parse(r.sha256),r.checkpointId!==e.checkpointId||r.repoSeq!==e.repoSeq||r.byteLength!==t.byteLength||r.sha256!==t.sha256))throw new u("archive_integrity_mismatch")}function Gt(r){if(r instanceof u)return r;if(r instanceof w)return new u("source_failed");if(r instanceof E)return new u("duplicate_durable_id");let t=r instanceof Error?r.message:"";return t.includes("manifest")?new u("manifest_invalid"):t.includes("chunk")||t.includes("row")||t.includes("Shape stream")?new u("chunk_invalid"):new u("archive_invalid")}export{u as RepositoryCheckpointVerificationError,Zt as verifyRepositoryCheckpointArchive};//# sourceMappingURL=checkpoint.js.map
2
2
  //# sourceMappingURL=checkpoint.js.map