@milaboratories/pl-drivers 1.2.24 → 1.2.26

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.
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var Oe=Object.defineProperty;var Me=(s,e,t)=>e in s?Oe(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var h=(s,e,t)=>Me(s,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Ee=require("node:fs/promises"),p=require("@milaboratories/pl-client"),m=require("@protobuf-ts/runtime-rpc"),i=require("@protobuf-ts/runtime"),ce=require("undici"),f=require("@milaboratories/ts-helpers"),P=require("node:stream"),He=require("node:fs"),Ae=require("node:path"),j=require("node:stream/consumers"),g=require("@milaboratories/computable"),G=require("node:timers/promises"),ze=require("node:readline/promises"),xe=require("denque"),je=require("node:os"),b=require("node:crypto"),T=require("@milaboratories/pl-tree"),k=require("zod"),Ge=require("node:zlib"),Ve=require("tar-fs");function U(s){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const t in s)if(t!=="default"){const n=Object.getOwnPropertyDescriptor(s,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:()=>s[t]})}}return e.default=s,Object.freeze(e)}const y=U(Ee),D=U(He),w=U(Ae),qe=U(ze),te=U(je),Je=U(Ge),Ze=U(Ve);class Qe extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Qe;class Xe extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Xe;class Ye extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Ke=new Ye;class et extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Response",[{no:1,name:"parts_count",kind:"scalar",T:4,L:0},{no:2,name:"uploaded_parts",kind:"scalar",repeat:1,T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.partsCount=0n,t.uploadedParts=[],e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.partsCount=e.uint64().toBigInt();break;case 2:if(d===i.WireType.LengthDelimited)for(let L=e.int32()+e.pos;e.pos<L;)o.uploadedParts.push(e.uint64().toBigInt());else o.uploadedParts.push(e.uint64().toBigInt());break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){if(e.partsCount!==0n&&t.tag(1,i.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,i.WireType.LengthDelimited).fork();for(let o=0;o<e.uploadedParts.length;o++)t.uint64(e.uploadedParts[o]);t.join()}let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const tt=new et;class rt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new rt;class nt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"bytes_processed",kind:"scalar",T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.bytesProcessed=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.bytesProcessed=e.int64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,i.WireType.Varint).int64(e.bytesProcessed);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ot=new nt;class st extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const it=new st;class at extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new at;class lt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"part_number",kind:"scalar",T:4,L:0},{no:3,name:"uploaded_part_size",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.partNumber=0n,t.uploadedPartSize=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.partNumber=e.uint64().toBigInt();break;case 3:o.uploadedPartSize=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,i.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,i.WireType.Varint).uint64(e.uploadedPartSize);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const dt=new lt;class ct extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.name!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.value);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const B=new ct;class ht extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Response",[{no:1,name:"upload_url",kind:"scalar",T:9},{no:2,name:"method",kind:"scalar",T:9},{no:3,name:"headers",kind:"message",repeat:1,T:()=>B},{no:4,name:"chunk_start",kind:"scalar",T:4,L:0},{no:5,name:"chunk_end",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.uploadUrl="",t.method="",t.headers=[],t.chunkStart=0n,t.chunkEnd=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.uploadUrl=e.string();break;case 2:o.method=e.string();break;case 3:o.headers.push(B.internalBinaryRead(e,e.uint32(),n));break;case 4:o.chunkStart=e.uint64().toBigInt();break;case 5:o.chunkEnd=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.uploadUrl!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.method);for(let o=0;o<e.headers.length;o++)B.internalBinaryWrite(e.headers[o],t.tag(3,i.WireType.LengthDelimited).fork(),n).join();e.chunkStart!==0n&&t.tag(4,i.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,i.WireType.Varint).uint64(e.chunkEnd);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ut=new ht;class pt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new pt;class gt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ft=new gt;class mt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const wt=new mt,_=new m.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:Ke,O:tt},{name:"GetPartURL",options:{},I:dt,O:ut},{name:"UpdateProgress",options:{},I:ot,O:it},{name:"Finalize",options:{},I:ft,O:wt}]);class yt{constructor(e){h(this,"typeName",_.typeName);h(this,"methods",_.methods);h(this,"options",_.options);this._transport=e}init(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}getPartURL(e,t){const n=this.methods[1],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}updateProgress(e,t){const n=this.methods[2],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}finalize(e,t){const n=this.methods[3],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class V extends Error{}class q extends Error{}class he extends Error{}class J extends Error{}class ue{constructor(e,t,n,r){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new yt(this.grpcTransport)}close(){}async initUpload({id:e,type:t},n){const r=await this.grpcClient.init({resourceId:e},p.addRTypeToMetadata(t,n));return this.partsToUpload(r.response)}async partUpload({id:e,type:t},n,r,o,c,a){const d=await this.grpcClient.getPartURL({resourceId:e,partNumber:r,uploadedPartSize:0n},p.addRTypeToMetadata(t,a)).response,{chunk:l,mTime:u}=await this.readChunk(n,d.chunkStart,d.chunkEnd);if(u>c)throw new V("file was modified, expected mtime: "+c+", got: "+u+".");const L=await ce.request(d.uploadUrl,this.prepareUploadOpts(d,l)),ee=await L.body.text();if(this.logger.info(`uploaded chunk ${r} from ${o} of resource: ${e}, response: '${ee.toString()}', status code: ${L.statusCode}`),L.statusCode!=200)throw new he(`response is not ok, status code: ${L.statusCode}, body: ${ee}, headers: ${L.headers}, url: ${d.uploadUrl}`);await this.grpcClient.updateProgress({resourceId:e,bytesProcessed:d.chunkEnd-d.chunkStart},p.addRTypeToMetadata(t,a))}async finalizeUpload({id:e,type:t},n){return await this.grpcClient.finalize({resourceId:e},p.addRTypeToMetadata(t,n))}async readChunk(e,t,n){let r;try{r=await y.open(e);const o=Number(n-t),c=Number(t),a=Buffer.alloc(o),d=await this.readBytesFromPosition(r,a,o,c),l=await y.stat(e);return{chunk:a.subarray(0,d),mTime:BigInt(Math.floor(l.mtimeMs/1e3))}}catch(o){throw o.code=="ENOENT"?new J(`there is no file ${e} for uploading`):o}finally{r==null||r.close()}}async readBytesFromPosition(e,t,n,r){let o=0;for(;o<n;){const{bytesRead:c}=await e.read(t,o,n-o,r+o);if(c===0)throw new q("file ended earlier than expected.");o+=c}return o}partsToUpload(e){const t=[],n=new Set(e.uploadedParts);for(let r=1n;r<=e.partsCount;r++)n.has(r)||t.push(r);return t}prepareUploadOpts(e,t){const n=e.headers.map(({name:r,value:o})=>[r,o]);return{dispatcher:this.httpClient,body:t,headers:Object.fromEntries(n),method:e.method.toUpperCase()}}}class Tt extends i.MessageType{constructor(){super("google.protobuf.Duration",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}internalJsonWrite(e,t){let n=i.PbLong.from(e.seconds).toNumber();if(n>315576e6||n<-315576e6)throw new Error("Duration value out of range.");let r=e.seconds.toString();if(n===0&&e.nanos<0&&(r="-"+r),e.nanos!==0){let o=Math.abs(e.nanos).toString();o="0".repeat(9-o.length)+o,o.substring(3)==="000000"?o=o.substring(0,3):o.substring(6)==="000"&&(o=o.substring(0,6)),r+="."+o}return r+"s"}internalJsonRead(e,t,n){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+i.typeofJsonValue(e)+". Expected string.");let r=e.match(/^(-?)([0-9]+)(?:\.([0-9]+))?s/);if(r===null)throw new Error("Unable to parse Duration from JSON string. Invalid format.");n||(n=this.create());let[,o,c,a]=r,d=i.PbLong.from(o+c);if(d.toNumber()>315576e6||d.toNumber()<-315576e6)throw new Error("Unable to parse Duration from JSON string. Value out of range.");if(n.seconds=d.toBigInt(),typeof a=="string"){let l=o+a+"0".repeat(9-a.length);n.nanos=parseInt(l)}return n}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.seconds!==0n&&t.tag(1,i.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,i.WireType.Varint).int32(e.nanos);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const v=new Tt;class bt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new bt;class kt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.Report",[{no:1,name:"progress",kind:"scalar",T:2},{no:2,name:"bytes_processed",kind:"scalar",T:4,L:0},{no:3,name:"bytes_total",kind:"scalar",T:4,L:0},{no:4,name:"done",kind:"scalar",T:8},{no:5,name:"name",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.progress=0,t.bytesProcessed=0n,t.bytesTotal=0n,t.done=!1,t.name="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.progress=e.float();break;case 2:o.bytesProcessed=e.uint64().toBigInt();break;case 3:o.bytesTotal=e.uint64().toBigInt();break;case 4:o.done=e.bool();break;case 5:o.name=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.progress!==0&&t.tag(1,i.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,i.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,i.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,i.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,i.WireType.LengthDelimited).string(e.name);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const I=new kt;class Lt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Lt;class Pt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Ut=new Pt;class It extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),n,o.report);break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.report&&I.internalBinaryWrite(e.report,t.tag(1,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Rt=new It;class St extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new St;class Nt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"update_interval",kind:"message",T:()=>v}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.updateInterval=v.internalBinaryRead(e,e.uint32(),n,o.updateInterval);break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.updateInterval&&v.internalBinaryWrite(e.updateInterval,t.tag(2,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const vt=new Nt;class Dt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),n,o.report);break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.report&&I.internalBinaryWrite(e.report,t.tag(1,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Bt=new Dt,C=new m.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:Ut,O:Rt},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:vt,O:Bt}]);class _t{constructor(e){h(this,"typeName",C.typeName);h(this,"methods",C.methods);h(this,"options",C.options);this._transport=e}getStatus(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}realtimeStatus(e,t){const n=this.methods[1],r=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,n,r,e)}}class pe{constructor(e,t,n,r){h(this,"grpcClient");this.grpcTransport=e,this.client=n,this.logger=r,this.grpcClient=new _t(this.grpcTransport)}close(){}async getStatus({id:e,type:t},n){const r=await this.grpcClient.getStatus({resourceId:e},p.addRTypeToMetadata(t,n)),o=f.notEmpty(r.response.report);return{done:o.done,progress:o.progress,bytesProcessed:String(o.bytesProcessed),bytesTotal:String(o.bytesTotal)}}async*realtimeStatus({id:e,type:t},n=100,r){r=p.addRTypeToMetadata(t,r);const o=Math.floor(n/1e3),c=(n-o*1e3)*1e6,a=v.create({seconds:BigInt(o),nanos:c});try{const{responses:d}=this.grpcClient.realtimeStatus({resourceId:e,updateInterval:a},r);yield*d}catch(d){throw this.logger.warn("Failed to get realtime status"+d),d}}}class Ct extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Ct;class Ft extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Ft;class $t extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Wt=new $t;class Ot extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.name!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.value);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const F=new Ot;class Mt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Response",[{no:1,name:"download_url",kind:"scalar",T:9},{no:2,name:"headers",kind:"message",repeat:1,T:()=>F}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.downloadUrl=e.string();break;case 2:o.headers.push(F.internalBinaryRead(e,e.uint32(),n));break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.downloadUrl!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.downloadUrl);for(let o=0;o<e.headers.length;o++)F.internalBinaryWrite(e.headers[o],t.tag(2,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Et=new Mt,$=new m.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:Wt,O:Et}]);class Ht{constructor(e){h(this,"typeName",$.typeName);h(this,"methods",$.methods);h(this,"options",$.options);this._transport=e}getDownloadURL(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class Z extends Error{}class ge{constructor(e){this.httpClient=e}async downloadRemoteFile(e,t,n){const{statusCode:r,body:o,headers:c}=await ce.request(e,{dispatcher:this.httpClient,headers:t,signal:n}),a=P.Readable.toWeb(o);if(r!=200){const d=await j.text(a),l=d.substring(0,Math.min(d.length,1e3));throw 400<=r&&r<500?new Z(`Http error: statusCode: ${r} url: ${e.toString()}, beginning of body: ${l}`):new Error(`Http error: statusCode: ${r} url: ${e.toString()}`)}return{content:a,size:Number(c["content-length"])}}}const At="storage://",re=/storage:\/\/(?<storageId>.*?)\/(?<localPath>.*)/;class Q extends Error{}class X extends Error{}class fe{constructor(e,t,n,r){h(this,"grpcClient");h(this,"downloadHelper");h(this,"isLocal",e=>e.startsWith(At));this.grpcTransport=e,this.httpClient=t,this.logger=n,this.localStorageIdsToRoot=r,this.grpcClient=new Ht(this.grpcTransport),this.downloadHelper=new ge(t)}close(){}async getUrl({id:e,type:t},n,r){const o=n??{};return o.abort=r,await this.grpcClient.getDownloadURL({resourceId:e},p.addRTypeToMetadata(t,o)).response}async downloadBlob(e,t,n){const{downloadUrl:r,headers:o}=await this.getUrl(e,t,n);return this.logger.info(`download from url ${r}`),this.isLocal(r)?await this.readLocalFile(r):await this.downloadHelper.downloadRemoteFile(r,me(o),n)}async readLocalFile(e){const t=e.match(re);if(t===null||t.length!=3)throw new X(`url for local filepath ${e} does not match regex ${re}, parsed: ${t}`);const[n,r,o]=t;if(this.localStorageIdsToRoot[r]==null)throw new Q(`Unknown storage location: ${r}`);const c=this.localStorageIdsToRoot[r],a=w.join(c,o),l=(await y.stat(a)).size;return{content:P.Readable.toWeb(D.createReadStream(a)),size:l}}}function me(s){return Object.fromEntries(s.map(({name:e,value:t})=>[e,t]))}class zt extends i.MessageType{constructor(){super("google.protobuf.Timestamp",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}now(){const e=this.create(),t=Date.now();return e.seconds=i.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(i.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),n=e.getTime();return t.seconds=i.PbLong.from(Math.floor(n/1e3)).toBigInt(),t.nanos=n%1e3*1e6,t}internalJsonWrite(e,t){let n=i.PbLong.from(e.seconds).toNumber()*1e3;if(n<Date.parse("0001-01-01T00:00:00Z")||n>Date.parse("9999-12-31T23:59:59Z"))throw new Error("Unable to encode Timestamp to JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");if(e.nanos<0)throw new Error("Unable to encode invalid Timestamp to JSON. Nanos must not be negative.");let r="Z";if(e.nanos>0){let o=(e.nanos+1e9).toString().substring(1);o.substring(3)==="000000"?r="."+o.substring(0,3)+"Z":o.substring(6)==="000"?r="."+o.substring(0,6)+"Z":r="."+o+"Z"}return new Date(n).toISOString().replace(".000Z",r)}internalJsonRead(e,t,n){if(typeof e!="string")throw new Error("Unable to parse Timestamp from JSON "+i.typeofJsonValue(e)+".");let r=e.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|\.([0-9]{3,9})Z|([+-][0-9][0-9]:[0-9][0-9]))$/);if(!r)throw new Error("Unable to parse Timestamp from JSON. Invalid format.");let o=Date.parse(r[1]+"-"+r[2]+"-"+r[3]+"T"+r[4]+":"+r[5]+":"+r[6]+(r[8]?r[8]:"Z"));if(Number.isNaN(o))throw new Error("Unable to parse Timestamp from JSON. Invalid value.");if(o<Date.parse("0001-01-01T00:00:00Z")||o>Date.parse("9999-12-31T23:59:59Z"))throw new globalThis.Error("Unable to parse Timestamp from JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");return n||(n=this.create()),n.seconds=i.PbLong.from(o/1e3).toBigInt(),n.nanos=0,r[7]&&(n.nanos=parseInt("1"+r[7]+"0".repeat(9-r[7].length))-1e9),n}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.seconds!==0n&&t.tag(1,i.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,i.WireType.Varint).int32(e.nanos);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const W=new zt;class xt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new xt;class jt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.ListItem",[{no:1,name:"name",kind:"scalar",T:9},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"is_dir",kind:"scalar",T:8},{no:10,name:"full_name",kind:"scalar",T:9},{no:11,name:"directory",kind:"scalar",T:9},{no:12,name:"last_modified",kind:"message",T:()=>W},{no:13,name:"version",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.size=0n,t.isDir=!1,t.fullName="",t.directory="",t.version="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.isDir=e.bool();break;case 10:o.fullName=e.string();break;case 11:o.directory=e.string();break;case 12:o.lastModified=W.internalBinaryRead(e,e.uint32(),n,o.lastModified);break;case 13:o.version=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.name!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,i.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,i.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,i.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,i.WireType.LengthDelimited).string(e.directory),e.lastModified&&W.internalBinaryWrite(e.lastModified,t.tag(12,i.WireType.LengthDelimited).fork(),n).join(),e.version!==""&&t.tag(13,i.WireType.LengthDelimited).string(e.version);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const O=new jt;class Gt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Gt;class Vt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"location",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.location="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.location=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.location);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const qt=new Vt;class Jt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:1,T:()=>O},{no:2,name:"delimiter",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.items=[],t.delimiter="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.items.push(O.internalBinaryRead(e,e.uint32(),n));break;case 2:o.delimiter=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){for(let o=0;o<e.items.length;o++)O.internalBinaryWrite(e.items[o],t.tag(1,i.WireType.LengthDelimited).fork(),n).join();e.delimiter!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.delimiter);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Zt=new Jt,M=new m.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:qt,O:Zt}]);class Qt{constructor(e){h(this,"typeName",M.typeName);h(this,"methods",M.methods);h(this,"options",M.options);this._transport=e}list(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class we{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=new Qt(e)}close(){}async list(e,t,n){return await this.grpcClient.list({resourceId:e.id,location:t},p.addRTypeToMetadata(e.type,n)).response}}class Xt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Xt;class Yt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;case 20:o.readLimit=e.int64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,i.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,i.WireType.Varint).int64(e.readLimit);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Kt=new Yt;class er extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,i.WireType.Varint).uint32(e.chunkSize);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const tr=new er;class rr extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,i.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,i.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,i.WireType.LengthDelimited).string(e.searchRe);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const nr=new rr;class or extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,i.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,i.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,i.WireType.LengthDelimited).string(e.searchRe);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const sr=new or;class ir extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.LastLines",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",opt:!0,T:3,L:0},{no:3,name:"line_count",kind:"scalar",opt:!0,T:5},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 3:o.lineCount=e.int32();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,i.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,i.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,i.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,i.WireType.LengthDelimited).string(e.searchRe);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ar=new ir;class lr extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.Response",[{no:1,name:"data",kind:"scalar",T:12},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"new_offset",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.data=new Uint8Array(0),t.size=0n,t.newOffset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.data=e.bytes();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.newOffset=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.data.length&&t.tag(1,i.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,i.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,i.WireType.Varint).uint64(e.newOffset);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const R=new lr,E=new m.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:Kt,O:R},{name:"ReadBinary",options:{},I:tr,O:R},{name:"StreamText",serverStreaming:!0,options:{},I:nr,O:R},{name:"ReadText",options:{},I:sr,O:R},{name:"LastLines",options:{},I:ar,O:R}]);class dr{constructor(e){h(this,"typeName",E.typeName);h(this,"methods",E.methods);h(this,"options",E.options);this._transport=e}streamBinary(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,n,r,e)}readBinary(e,t){const n=this.methods[1],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}streamText(e,t){const n=this.methods[2],r=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,n,r,e)}readText(e,t){const n=this.methods[3],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}lastLines(e,t){const n=this.methods[4],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class ye{constructor(e,t,n){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=n,this.grpcClient=new dr(this.grpcTransport)}close(){}async lastLines({id:e,type:t},n,r=0n,o,c){return(await this.grpcClient.lastLines({resourceId:e,lineCount:n,offset:r,search:o},p.addRTypeToMetadata(t,c))).response}async readText({id:e,type:t},n,r=0n,o,c){return(await this.grpcClient.readText({resourceId:f.notEmpty(e),readLimit:BigInt(n),offset:r,search:o},p.addRTypeToMetadata(t,c))).response}}const Te=process.env.PL_STORAGE_TO_PATH?Object.fromEntries(process.env.PL_STORAGE_TO_PATH.split(";").map(s=>s.split(":"))):{};function cr(s,e,t){return t===void 0&&(t=Te),e.getDriver({name:"DownloadBlob",init:(r,o,c)=>new fe(o,c,s,t)})}function hr(s,e){return s.getDriver({name:"StreamLogs",init:(t,n,r)=>new ye(n,r,e)})}function ur(s,e){return s.getDriver({name:"UploadProgress",init:(t,n,r)=>new pe(n,r,s,e)})}function pr(s,e){return s.getDriver({name:"UploadBlob",init:(t,n,r)=>new ue(n,r,s,e)})}function gr(s,e){return s.getDriver({name:"LsFiles",init:(t,n,r)=>new we(n,e)})}class fr{constructor(e,t){h(this,"updater");h(this,"schedule",()=>this.updater.schedule());this.onUpdate=e,this.sleepMs=t,this.updater=new Y(async()=>{for(;;){if(await this.onUpdate())return;await G.scheduler.wait(this.sleepMs)}})}}class Y{constructor(e){h(this,"updating");this.onUpdate=e}schedule(){this.updating==null&&(this.updating=(async()=>{try{await this.onUpdate()}catch(e){console.log(`error while updating in Updater: ${e}`)}finally{this.updating=void 0}})())}}async function mr(s,e){return s.withReadTx("LogsDriverGetStream",async t=>{const n=await t.getResourceData(e,!0),r=await p.valErr(t,p.getField(n,"stream"));if(r.error!="")throw new Error(`while getting stream: ${r.error}`);if(!p.isNullResourceId(r.valueId))return await t.getResourceData(r.valueId,!1)})}const be="8C7#F1328%9E089B3D22",wr=/(?<stage>.*):\s*(?<progress>[\d.]+%)\s.*(?<eta>ETA:.*)/g;function ke(s){const t=s.replace(be,"").match(wr);if(t==null||t.length!=4)return;const[n,r,o,c]=t;return{stage:r,progress:o,eta:c}}async function yr(s,e,t){const n=await e.lastLines(s,1,0n,be,t);if(n.data==null||n.data.length==0)return{found:!1};const r=n.data.toString().split(/\r?\n/)[0];if(r==null)return{found:!1};const o=ke(r);return o===void 0?{found:!1}:{found:!0,...o}}class Le{constructor(e){h(this,"cache",new Map);h(this,"totalSizeBytes",0);this.softSizeBytes=e}existsFile(e){return this.cache.get(e)!=null}getFile(e,t){const n=this.cache.get(e);return n!=null&&n.counter.inc(t),n}removeFile(e,t){return f.mapGet(this.cache,e).counter.dec(t),this.toDelete()}toDelete(){if(this.totalSizeBytes<=this.softSizeBytes)return[];const e=[];let t=0;return f.mapEntries(this.cache).filter(([n,r])=>r.counter.isZero()).forEach(([n,r])=>{if(this.totalSizeBytes-t<=this.softSizeBytes)return;const o=f.mapGet(this.cache,n);t+=o.sizeBytes,e.push(o)}),e}addCache(e,t){const n=this.cache.get(e.path)==null;if(this.cache.set(e.path,e),e.counter.inc(t),e.sizeBytes<0)throw new Error(`empty sizeBytes: ${e}`);n&&(this.totalSizeBytes+=e.sizeBytes)}removeCache(e){this.cache.delete(e.path),this.totalSizeBytes-=e.sizeBytes}}class Tr{constructor(e,t){this.logsStreamDriver=e,this.downloadDriver=t}getLastLogs(e,t,n){if(n===void 0)return g.Computable.make(o=>this.getLastLogs(e,t,o));const r=A(n,e);if(r===void 0){n.markUnstable("no stream in stream manager");return}if(H(r))return this.downloadDriver.getLastLogs(r,t,n);try{return this.logsStreamDriver.getLastLogs(r,t,n)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){n.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${o}`);return}throw o}}getProgressLog(e,t,n){if(n===void 0)return g.Computable.make(o=>this.getProgressLog(e,t,o));const r=A(n,e);if(r===void 0){n.markUnstable("no stream in stream manager");return}if(H(r))return this.downloadDriver.getProgressLog(r,t,n);try{return this.logsStreamDriver.getProgressLog(r,t,n)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){n.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${o}`);return}throw o}}getLogHandle(e,t){if(t===void 0)return g.Computable.make(r=>this.getLogHandle(e,r));const n=A(t,e);if(n===void 0){t.markUnstable("no stream in stream manager");return}return H(n)?this.downloadDriver.getLogHandle(n,t):this.logsStreamDriver.getLogHandle(n,t)}async lastLines(e,t,n,r){return N(e)?await this.logsStreamDriver.lastLines(e,t,n,r):await this.downloadDriver.lastLines(e,t,n,r)}async readText(e,t,n,r){return N(e)?await this.logsStreamDriver.readText(e,t,n,r):await this.downloadDriver.readText(e,t,n,r)}}function H(s){return!s.type.name.startsWith("StreamWorkdir")}function A(s,e){var t;return(t=s.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}function S(s){let e;if(N(s))e=s.match(Pe);else if(Ie(s))e=s.match(Ue);else throw new Error(`Log handle is malformed: ${s}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${s}`);const{resourceType:t,resourceVersion:n,resourceId:r}=e.groups;return{id:p.bigintToResourceId(BigInt(r)),type:{name:t,version:n}}}function K(s,e){return s?`log+live://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`:`log+ready://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`}const Pe=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function N(s){return Pe.test(s)}const Ue=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function Ie(s){return Ue.test(s)}const Re=T.rsSchema({kv:{"ctl/file/blobInfo":k.z.object({sizeBytes:k.z.coerce.number()})}});class br{constructor(e,t,n,r,o,c){h(this,"idToDownload",new Map);h(this,"cache");h(this,"downloadQueue");h(this,"idToOnDemand",new Map);h(this,"idToLastLines",new Map);h(this,"idToProgressLog",new Map);h(this,"saveDir");this.logger=e,this.clientDownload=t,this.clientLogs=n,this.signer=o,this.cache=new Le(c.cacheSoftSizeBytes),this.downloadQueue=new f.TaskProcessor(this.logger,c.nConcurrentDownloads),this.saveDir=w.resolve(r)}getDownloadedBlob(e,t){if(t===void 0)return g.Computable.make(c=>this.getDownloadedBlob(e,c));const n=T.treeEntryToResourceInfo(e,t),r=b.randomUUID();t.addOnDestroy(()=>this.releaseBlob(n.id,r));const o=this.getDownloadedBlobNoCtx(t.watcher,n,r);return o==null&&t.markUnstable("download blob is still undefined"),o}getOnDemandBlob(e,t){if(t===void 0)return g.Computable.make(c=>this.getOnDemandBlob(e,c));const n=T.isPlTreeEntry(e)?T.makeResourceSnapshot(e,Re,t):e,r=b.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(n.id,r)),this.getOnDemandBlobNoCtx(t.watcher,n,r)}getLocalPath(e){return z(e,this.signer)}async getContent(e){if(Ur(e))return await Lr(this.getLocalPath(e));if(!Rr(e))throw new Error("Malformed remote handle");const t=Sr(e,this.signer),{content:n}=await this.clientDownload.downloadBlob(t);return await j.buffer(n)}getDownloadedBlobNoCtx(e,t,n){let r=this.idToDownload.get(t.id);if(r===void 0){const c=this.setNewDownloadTask(e,t,n);this.downloadQueue.push({fn:()=>this.downloadBlob(c,n),recoverableErrorPredicate:a=>!0}),r=c}r.attach(e,n);const o=r.getBlob();if(o!==void 0){if(o.ok)return o.value;throw o.error}}setNewDownloadTask(e,t,n){const r=this.getFilePath(t.id),o=new Se(this.clientDownload,t,r,Ir(r,this.signer));return this.idToDownload.set(t.id,o),o}async downloadBlob(e,t){var n;await e.download(),(n=e.getBlob())!=null&&n.ok&&this.cache.addCache(e,t)}getOnDemandBlobNoCtx(e,t,n){let r=this.idToOnDemand.get(t.id);return r===void 0&&(r=new kr(t.kv["ctl/file/blobInfo"].sizeBytes,Nr(t,this.signer)),this.idToOnDemand.set(t.id,r)),r.attach(e,n),r.getHandle()}getLastLogs(e,t,n){if(n==null)return g.Computable.make(a=>this.getLastLogs(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.addOnDestroy(()=>this.releaseBlob(r.id,o));const c=this.getLastLogsNoCtx(n.watcher,r,t,o);return c==null&&n.markUnstable("either a file was not downloaded or logs was not read"),c}getLastLogsNoCtx(e,t,n,r){const o=this.getDownloadedBlobNoCtx(e,t,r);if(o==null)return;const c=z(o.handle,this.signer);let a=this.idToLastLines.get(t.id);if(a==null){const l=new ne(c,n);this.idToLastLines.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getProgressLog(e,t,n){if(n==null)return g.Computable.make(a=>this.getProgressLog(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.addOnDestroy(()=>this.releaseBlob(r.id,o));const c=this.getProgressLogNoCtx(n.watcher,r,t,o);return c===void 0&&n.markUnstable("either a file was not downloaded or a progress log was not read"),c}getProgressLogNoCtx(e,t,n,r){const o=this.getDownloadedBlobNoCtx(e,t,r);if(o==null)return;const c=z(o.handle,this.signer);let a=this.idToProgressLog.get(t.id);if(a==null){const l=new ne(c,1,n);this.idToProgressLog.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getLogHandle(e,t){if(t==null)return g.Computable.make(r=>this.getLogHandle(e,r));const n=T.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(n)}getLogHandleNoCtx(e){return K(!1,e)}async lastLines(e,t,n,r){const o=await this.clientLogs.lastLines(S(e),t,BigInt(n??0),r);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async readText(e,t,n,r){const o=await this.clientLogs.readText(S(e),t,BigInt(n??0),r);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async releaseBlob(e,t){const n=this.idToDownload.get(e);if(n!=null)if(this.cache.existsFile(n.path)){const r=this.cache.removeFile(n.path,t);await Promise.all(r.map(async o=>{await y.rm(o.path),this.cache.removeCache(o),this.removeTask(o,`the task ${o.path} was removedfrom cache along with ${r.map(c=>c.path)}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${n.path} was removed from cache`)}removeTask(e,t){e.abort(t),e.change.markChanged(),this.idToDownload.delete(e.rInfo.id),this.idToLastLines.delete(e.rInfo.id),this.idToProgressLog.delete(e.rInfo.id)}async releaseOnDemandBlob(e,t){var r;(((r=this.idToOnDemand.get(e))==null?void 0:r.release(t))??!1)&&this.idToOnDemand.delete(e)}async releaseAll(){this.downloadQueue.stop(),this.idToDownload.forEach((e,t)=>{this.idToDownload.delete(t),e.change.markChanged()})}getFilePath(e){return w.resolve(w.join(this.saveDir,String(BigInt(e))))}}class kr{constructor(e,t){h(this,"change",new g.ChangeSource);h(this,"counter",new f.CallersCounter);this.size=e,this.handle=t}getHandle(){return{handle:this.handle,size:this.size}}attach(e,t){this.counter.inc(t),this.change.attachWatcher(e)}release(e){return this.counter.dec(e)}}class ne{constructor(e,t,n){h(this,"updater");h(this,"log");h(this,"change",new g.ChangeSource);h(this,"error");this.path=e,this.lines=t,this.patternToSearch=n,this.updater=new Y(async()=>this.update())}getOrSchedule(e){return this.change.attachWatcher(e),this.updater.schedule(),{log:this.log,error:this.error}}async update(){try{const e=await Pr(this.path,this.lines,this.patternToSearch);this.log!=e&&this.change.markChanged(),this.log=e}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.log="",this.error=e,this.change.markChanged();return}throw e}}}async function oe(s){try{return await y.access(s),!0}catch{return!1}}async function Lr(s){return await j.buffer(P.Readable.toWeb(D.createReadStream(s)))}function Pr(s,e,t){const n=D.createReadStream(s),r=new P.Writable;return new Promise((o,c)=>{const a=qe.createInterface(n,r),d=new xe;a.on("line",function(l){t!=null&&!l.includes(t)||(d.push(l),d.length>e&&d.shift())}),a.on("error",c),a.on("close",function(){o(d.toArray().join(te.EOL)+te.EOL)})})}let Se=class{constructor(e,t,n,r){h(this,"counter",new f.CallersCounter);h(this,"change",new g.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"sizeBytes",0);this.clientDownload=e,this.rInfo=t,this.path=n,this.handle=r}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(){try{const{content:e,size:t}=await this.clientDownload.downloadBlob(this.rInfo);if(await oe(w.dirname(this.path))||y.mkdir(w.dirname(this.path),{recursive:!0}),await oe(this.path))await e.cancel("the file already existed");else{const n=P.Writable.toWeb(D.createWriteStream(this.path));await e.pipeTo(n)}this.setDone(t)}catch(e){if(e instanceof se||e instanceof Z||e instanceof Q||e instanceof X||e.code=="ENOENT"){this.setError(e),await y.rm(this.path);return}throw e}}getBlob(){if(this.done)return{ok:!0,value:{handle:this.handle,size:this.sizeBytes}};if(this.error)return{ok:!1,error:this.error}}setDone(e){this.done=!0,this.sizeBytes=e,this.change.markChanged()}abort(e){this.signalCtl.abort(new se(e))}setError(e){this.error=e,this.change.markChanged()}};class se extends Error{}const Ne=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function Ur(s){return!!s.match(Ne)}function z(s,e){const t=s.match(Ne);if(t===null)throw new Error(`Local handle is malformed: ${s}, matches: ${t}`);const{path:n,signature:r}=t.groups;return e.verify(n,r,`Signature verification failed for: ${s}`),n}function Ir(s,e){return`blob+local://download/${s}#${e.sign(s)}`}const ve=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*))#(?<signature>.*)$/;function Rr(s){return!!s.match(ve)}function Sr(s,e){const t=s.match(ve);if(t===null)throw new Error(`Remote handle is malformed: ${s}, matches: ${t}`);const{content:n,resourceType:r,resourceVersion:o,resourceId:c,signature:a}=t.groups;return e.verify(n,a,`Signature verification failed for ${s}`),{id:p.bigintToResourceId(BigInt(c)),type:{name:r,version:o}}}function Nr(s,e){const t=`${s.type.name}/${s.type.version}/${BigInt(s.id)}`;return`blob+remote://download/${t}#${e.sign(t)}`}const vr=k.z.object({localPath:k.z.string(),pathSignature:k.z.string(),modificationTime:k.z.string()}),Dr=k.z.union([vr,k.z.object({})]),De=T.rsSchema({data:Dr,fields:{blob:!1,incarnation:!1}});class Br{constructor(e,t,n,r,o={nConcurrentPartUploads:10,nConcurrentGetProgresses:10,pollingInterval:1e3,stopPollingDelay:1e3}){h(this,"idToProgress",new Map);h(this,"uploadQueue");h(this,"hooks");h(this,"scheduledOnNextState",[]);h(this,"keepRunning",!1);h(this,"currentLoop");this.logger=e,this.signer=t,this.clientBlob=n,this.clientProgress=r,this.opts=o,this.uploadQueue=new f.TaskProcessor(this.logger,1,{type:"exponentialWithMaxDelayBackoff",initialDelay:20,maxDelay:15e3,backoffMultiplier:1.5,jitter:.5}),this.hooks=new g.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:o.stopPollingDelay},(c,a)=>this.scheduleOnNextState(c,a))}getProgressId(e,t){if(t==null)return g.Computable.make(c=>this.getProgressId(e,c));const n=T.isPlTreeEntry(e)?T.makeResourceSnapshot(e,De,t):e,r=b.randomUUID();t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(n.id,r));const o=this.getProgressIdNoCtx(t.watcher,n,r);return ie(o)||t.markUnstable(`upload/index progress was got, but it's not stable: ${o}`),o}getProgressIdNoCtx(e,t,n){const r=t.fields.blob!=null||t.fields.incarnation!=null,o=this.idToProgress.get(t.id);if(o!=null)return o.attach(e,n),o.mustGetProgress(r);const c=new _r(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,c),c.attach(e,n),c.progress.isUpload&&c.progress.isUploadSignMatch&&this.uploadQueue.push({fn:()=>c.uploadBlobTask(),recoverableErrorPredicate:a=>!_e(a)}),c.mustGetProgress(r)}async release(e,t){const n=this.idToProgress.get(e);if(n===void 0)return;n.decCounter(t)&&this.idToProgress.delete(e)}async releaseAll(){this.uploadQueue.stop()}scheduleOnNextState(e,t){this.scheduledOnNextState.push({resolve:e,reject:t})}startUpdating(){this.keepRunning=!0,this.currentLoop===void 0&&(this.currentLoop=this.mainLoop())}stopUpdating(){this.keepRunning=!1}async mainLoop(){for(;this.keepRunning;){const e=this.scheduledOnNextState;this.scheduledOnNextState=[];try{await f.asyncPool(this.opts.nConcurrentGetProgresses,this.getAllNotDoneProgresses().map(t=>async()=>await t.updateStatus())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(n=>n.reject(t))}if(!this.keepRunning)break;await G.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!ie(t.progress)).map(([e,t])=>t)}}class _r{constructor(e,t,n,r,o,c){h(this,"change",new g.ChangeSource);h(this,"counter",new f.CallersCounter);h(this,"progress");h(this,"uploadOpts");h(this,"uploadingTerminallyFailed");this.logger=e,this.clientBlob=t,this.clientProgress=n,this.nConcurrentPartsUpload=r,this.res=c;const a=c.type.name.startsWith("BlobUpload");let d;a&&(this.uploadOpts=Be(c),d=Fr(o,this.uploadOpts.localPath,this.uploadOpts.pathSignature)),this.progress={done:!1,status:void 0,isUpload:a,isUploadSignMatch:d,lastError:void 0}}mustGetProgress(e){if(e)return this.setDone(e),this.progress;if(this.uploadingTerminallyFailed)throw this.logger.error(`Uploading terminally failed: ${this.progress.lastError}`),new Error(this.progress.lastError);return this.progress}attach(e,t){this.change.attachWatcher(e),this.counter.inc(t)}decCounter(e){return this.counter.dec(e)}async uploadBlobTask(){try{await this.uploadBlob()}catch(e){if(this.setLastError(e),ae(e)){this.logger.warn(`resource was deleted while uploading a blob: ${e}`),this.change.markChanged(),this.setDone(!0);return}throw this.logger.error(`error while uploading a blob: ${e}`),this.change.markChanged(),_e(e)&&this.terminateWithError(e),e}}async uploadBlob(){if(this.counter.isZero())return;const e=await this.clientBlob.initUpload(this.res);this.logger.info(`start to upload blob ${this.res.id}, parts count: ${e.length}`);const t=n=>async()=>{this.counter.isZero()||await this.clientBlob.partUpload(this.res,this.uploadOpts.localPath,n,e.length,BigInt(this.uploadOpts.modificationTime))};await f.asyncPool(this.nConcurrentPartsUpload,e.map(t)),!this.counter.isZero()&&(await this.clientBlob.finalizeUpload(this.res),this.logger.info(`uploading of resource ${this.res.id} finished.`),this.change.markChanged())}terminateWithError(e){this.progress.lastError=String(e),this.progress.done=!1,this.uploadingTerminallyFailed=!0}setLastError(e){this.progress.lastError=String(e)}setDone(e){this.progress.done=e,e&&(this.progress.lastError=void 0)}async updateStatus(){try{const e=await this.clientProgress.getStatus(this.res),t=this.progress.status;this.progress.status=Cr(e),this.setDone(e.done),(e.done||e.progress!=(t==null?void 0:t.progress))&&this.change.markChanged()}catch(e){if(this.setLastError(e),e.name=="RpcError"&&e.code=="DEADLINE_EXCEEDED"){this.logger.warn("deadline exceeded while getting a status of BlobImport");return}if(ae(e)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${e}, ${p.stringifyWithResourceId(this.res)}`),this.change.markChanged(),this.setDone(!0);return}this.logger.error(`error while updating a status of BlobImport: ${e}`),this.change.markChanged(),this.terminateWithError(e)}}}function ie(s){return s.done&&s.status!==void 0&&s.status!==null&&s.status.progress>=1}function Be(s){if(s.data==null||!("modificationTime"in s.data))throw new Error("no upload options in BlobUpload resource data: "+p.stringifyWithResourceId(s.data));const e=s.data;if(e.modificationTime===void 0)throw new Error("no modification time in data: "+p.stringifyWithResourceId(s.data));if(e.localPath===void 0)throw new Error("no local path in data: "+p.stringifyWithResourceId(s.data));if(e.pathSignature===void 0)throw new Error("no path signature in data: "+p.stringifyWithResourceId(s.data));return{modificationTime:e.modificationTime,localPath:e.localPath,pathSignature:e.pathSignature}}function Cr(s){return{progress:s.progress??0,bytesProcessed:Number(s.bytesProcessed),bytesTotal:Number(s.bytesTotal)}}function Fr(s,e,t){try{return s.verify(e,t),!0}catch{return!1}}function _e(s){return s instanceof V||s instanceof q||s instanceof J}function ae(s){return s.name=="RpcError"&&(s.code=="NOT_FOUND"||s.code=="ABORTED"||s.code=="ALREADY_EXISTS")}class $r{constructor(e,t={nConcurrentGetLogs:10,pollingInterval:1e3,stopPollingDelay:1e3}){h(this,"idToLastLines",new Map);h(this,"idToProgressLog",new Map);h(this,"hooks");h(this,"scheduledOnNextState",[]);h(this,"keepRunning",!1);h(this,"currentLoop");this.clientLogs=e,this.opts=t,this.hooks=new g.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:t.stopPollingDelay},(n,r)=>this.scheduleOnNextState(n,r))}getLastLogs(e,t,n){if(n==null)return g.Computable.make(a=>this.getLastLogs(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.attacheHooks(this.hooks),n.addOnDestroy(()=>this.releaseLastLogs(r.id,o));const c=this.getLastLogsNoCtx(n.watcher,r,t,o);return n.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),c}getLastLogsNoCtx(e,t,n,r){let o=this.idToLastLines.get(t.id);if(o==null){const a=new le(this.clientLogs,t,n);this.idToLastLines.set(t.id,a),o=a}o.attach(e,r);const c=o.getLog();if(c.error!=null)throw c.error;return c.log}getProgressLog(e,t,n){if(n==null)return g.Computable.make(a=>this.getProgressLog(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.attacheHooks(this.hooks),n.addOnDestroy(()=>this.releaseProgressLog(r.id,o));const c=this.getProgressLogNoCtx(n.watcher,r,t,o);return n.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),c}getProgressLogNoCtx(e,t,n,r){let o=this.idToProgressLog.get(t.id);if(o==null){const a=new le(this.clientLogs,t,1,n);this.idToProgressLog.set(t.id,a),o=a}o.attach(e,r);const c=o.getLog();if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return g.Computable.make(o=>this.getLogHandle(e,o));const n=T.treeEntryToResourceInfo(e,t),r=this.getLogHandleNoCtx(n);return t.markUnstable(),r}getLogHandleNoCtx(e){return K(!0,e)}async lastLines(e,t,n,r){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(S(e),t,BigInt(n??0),r))}async readText(e,t,n,r){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(S(e),t,BigInt(n??0),r))}async tryWithNotFound(e,t){if(!N(e))throw new Error(`Not live log handle was passed to live log driver, handle: ${e}`);try{const n=await t();return{live:!0,shouldUpdateHandle:!1,data:n.data,size:Number(n.size),newOffset:Number(n.newOffset)}}catch(n){if(n.name=="RpcError"&&n.code=="NOT_FOUND")return{shouldUpdateHandle:!0};throw n}}async releaseLastLogs(e,t){var r;((r=this.idToLastLines.get(e))==null?void 0:r.release(t))&&this.idToLastLines.delete(e)}async releaseProgressLog(e,t){var r;((r=this.idToProgressLog.get(e))==null?void 0:r.release(t))&&this.idToProgressLog.delete(e)}async releaseAll(){}scheduleOnNextState(e,t){this.scheduledOnNextState.push({resolve:e,reject:t})}startUpdating(){this.keepRunning=!0,this.currentLoop===void 0&&(this.currentLoop=this.mainLoop())}stopUpdating(){this.keepRunning=!1}async mainLoop(){for(;this.keepRunning;){const e=this.scheduledOnNextState;this.scheduledOnNextState=[];try{await f.asyncPool(this.opts.nConcurrentGetLogs,this.getAllNotDoneLogs().map(t=>async()=>await t.update())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(n=>n.reject(t))}if(!this.keepRunning)break;await G.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneLogs(){return Array.from(this.idToLastLines.entries()).concat(Array.from(this.idToProgressLog.entries())).filter(([e,t])=>!t.getLog().done).map(([e,t])=>t)}}class le{constructor(e,t,n,r){h(this,"logs");h(this,"error");h(this,"done",!1);h(this,"change",new g.ChangeSource);h(this,"counter",new f.CallersCounter);this.clientLogs=e,this.rInfo=t,this.lines=n,this.patternToSearch=r}getLog(){return{log:this.logs,error:this.error,done:this.done}}attach(e,t){this.change.attachWatcher(e),this.counter.inc(t)}release(e){return this.counter.dec(e)}async update(){try{const t=(await this.clientLogs.lastLines(this.rInfo,this.lines,0n,this.patternToSearch)).data.toString();this.logs!=t&&this.change.markChanged(),this.logs=t;return}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.logs="",this.error=e,this.done=!0,this.change.markChanged();return}throw e}}}class Wr{constructor(e,t,n,r={cacheSoftSizeBytes:50*1024*1024,withGunzip:!0,nConcurrentDownloads:50}){h(this,"downloadHelper");h(this,"urlToDownload",new Map);h(this,"downloadQueue");h(this,"cache");this.logger=e,this.saveDir=n,this.opts=r,this.downloadQueue=new f.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new Le(this.opts.cacheSoftSizeBytes),this.downloadHelper=new ge(t)}getPath(e,t){if(t===void 0)return g.Computable.make(o=>this.getPath(e,o));const n=b.randomUUID();t.addOnDestroy(()=>this.releasePath(e,n));const r=this.getPathNoCtx(e,t.watcher,n);return(r==null?void 0:r.path)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${r}`),r}getPathNoCtx(e,t,n){const r=e.toString(),o=this.urlToDownload.get(r);if(o!=null)return o.attach(t,n),o.getPath();const c=this.setNewTask(t,e,n);return this.downloadQueue.push({fn:async()=>this.downloadUrl(c,n),recoverableErrorPredicate:a=>!0}),c.getPath()}async downloadUrl(e,t){var n;await e.download(this.downloadHelper,this.opts.withGunzip),((n=e.getPath())==null?void 0:n.path)!=null&&this.cache.addCache(e,t)}async releasePath(e,t){const n=e.toString(),r=this.urlToDownload.get(n);if(r!=null)if(this.cache.existsFile(r.path)){const o=this.cache.removeFile(r.path,t);await Promise.all(o.map(async c=>{await x(c.path),this.cache.removeCache(c),this.removeTask(c,`the task ${JSON.stringify(c)} was removedfrom cache along with ${JSON.stringify(o)}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${JSON.stringify(r)} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.urlToDownload.entries()).map(async([e,t])=>{await x(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${t} was released when the driver was closed`)}))}setNewTask(e,t,n){const r=new Or(this.getFilePath(t),t);return r.attach(e,n),this.urlToDownload.set(t.toString(),r),r}removeTask(e,t){e.abort(t),e.change.markChanged(),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=b.createHash("sha256").update(e.toString()).digest("hex");return w.join(this.saveDir,t)}}class Or{constructor(e,t){h(this,"counter",new f.CallersCounter);h(this,"change",new g.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"sizeBytes",0);this.path=e,this.url=t}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(e,t){try{const n=await this.downloadAndUntar(e,t,this.signalCtl.signal);this.setDone(n)}catch(n){if(n instanceof de||n instanceof Z){this.setError(n),await x(this.path);return}throw n}}async downloadAndUntar(e,t,n){if(await Mr(this.path))return await Ce(this.path);const r=await e.downloadRemoteFile(this.url.toString(),{},n);let o=r.content;if(t){const a=P.Transform.toWeb(Je.createGunzip());o=o.pipeThrough(a,{signal:n})}const c=P.Writable.toWeb(Ze.extract(this.path));return await o.pipeTo(c,{signal:n}),r.size}getPath(){if(this.done)return{path:f.notEmpty(this.path)};if(this.error)return{error:this.error}}setDone(e){this.done=!0,this.sizeBytes=e,this.change.markChanged()}abort(e){this.signalCtl.abort(new de(e))}setError(e){this.error=String(e),this.change.markChanged()}}class de extends Error{}async function Mr(s){try{return await y.access(s),!0}catch{return!1}}async function Ce(s){const e=await y.readdir(s,{withFileTypes:!0});return(await Promise.all(e.map(async n=>{const r=w.join(s,n.name);return n.isDirectory()?await Ce(r):(await y.stat(r)).size}))).reduce((n,r)=>n+r,0)}async function x(s){await y.rm(s,{recursive:!0,force:!0})}function Er(s){var t;return{parent:s.list.items.length>0?(t=s.list.items[0])==null?void 0:t.directory:void 0,entries:s.list.items.map(n=>Hr(n,s))}}function Hr(s,e){return s.isDir?{type:"dir",name:s.name,fullPath:s.fullName}:{type:"file",name:s.name,fullPath:s.fullName,handle:Ar({item:s,...e})}}function Ar(s){return s.remote?zr(s):Fe(s.item.fullName,s.signer,s.item.size,f.notEmpty(s.item.lastModified).seconds)}function zr(s){return`index://index/${encodeURIComponent(JSON.stringify({storageId:s.storageName,path:s.item.fullName}))}`}function Fe(s,e,t,n){const r={localPath:s,pathSignature:e.sign(s),sizeBytes:String(t),modificationTime:String(n)};return`upload://upload/${encodeURIComponent(JSON.stringify(r))}`}function xr(s,e){if(!(e.dirent.isFile()||e.dirent.isDirectory())){s.warn(`tried to get non-dir and non-file ${e.dirent.name}, skip it`);return}return{directory:e.directory,isDir:e.dirent.isDirectory(),name:e.dirent.name,fullName:e.fullName,lastModified:{seconds:BigInt(Math.floor(e.stat.mtimeMs/1e3)),nanos:0},size:BigInt(e.stat.size)}}function jr(s,e){const t=Object.entries(s).map(Vr),n=Object.entries(e).map(Qr);return t.concat(n)}function Gr(s){if(Xr(s))return Kr(s);if(qr(s))return Zr(s);f.assertNever(s)}function Vr([s,e]){return{name:s,handle:Jr(s,e),initialFullPath:e}}const $e=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function qr(s){return $e.test(s)}function Jr(s,e){return`local://${s}/${encodeURIComponent(e)}`}function Zr(s){const e=s.match($e);if(e==null)throw new Error(`Local list handle wasn't parsed: ${s}`);const{name:t,path:n}=e.groups;return{path:decodeURIComponent(n),name:t,remote:!1}}function Qr([s,e]){return{name:s,handle:Yr(s,e),initialFullPath:""}}const We=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function Xr(s){return We.test(s)}function Yr(s,e){return`remote://${s}/${BigInt(e)}`}function Kr(s){const e=s.match(We);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${s}`);const{name:t,resourceId:n}=e.groups;return{id:p.bigintToResourceId(BigInt(n)),type:en(t),name:t,remote:!0}}function en(s){return{name:`LS/${s}`,version:"1"}}class tn{constructor(e,t,n,r,o){h(this,"storageIdToResourceId");this.logger=e,this.clientLs=t,this.client=n,this.signer=r,this.localStorageToPath=o}async getLocalFileHandle(e){const t=await y.stat(e,{bigint:!0});return Fe(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){return jr(this.localStorageToPath,await this.getAvailableStorageIds())}async listFiles(e,t){const n=Gr(e);let r;return n.remote?r=await this.clientLs.list(n,t):r=await this.getLocalFiles(this.logger,n.path,t),Er({storageName:n.name,list:r,signer:this.signer,remote:n.remote})}async getAvailableStorageIds(){return this.storageIdToResourceId==null&&(this.storageIdToResourceId=await rn(this.client)),this.storageIdToResourceId}async getLocalFiles(e,t,n){const r=w.resolve(t),o=w.isAbsolute(n)?n:w.resolve(w.join(r,n)),c=await y.opendir(o),a=[];for await(const l of c){const u=w.join(o,l.name);a.push({directory:o,fullName:u,dirent:l,stat:await y.stat(u)})}return{delimiter:w.sep,items:a.map(l=>xr(e,l)).filter(l=>l!=null).map(l=>l)}}}async function rn(s){return s.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),n=await e.getResourceData(t,!0);return nn(n)})}function nn(s){return Object.fromEntries(s.fields.filter(e=>e.type=="Dynamic"&&p.isNotNullResourceId(e.value)).map(e=>[e.name.substring(8),e.value]))}exports.ClientDownload=fe;exports.ClientLogs=ye;exports.ClientLs=we;exports.ClientProgress=pe;exports.ClientUpload=ue;exports.Download=Se;exports.DownloadDriver=br;exports.DownloadUrlDriver=Wr;exports.LogsDriver=Tr;exports.LogsStreamDriver=$r;exports.LongUpdater=fr;exports.LsDriver=tn;exports.MTimeError=V;exports.NetworkError=he;exports.NoFileForUploading=J;exports.OnDemandBlobResourceSnapshot=Re;exports.PL_STORAGE_TO_PATH=Te;exports.UnexpectedEOF=q;exports.UnknownStorageError=Q;exports.Updater=Y;exports.UploadDriver=Br;exports.UploadResourceSnapshot=De;exports.WrongLocalFileUrl=X;exports.createDownloadClient=cr;exports.createLogsClient=hr;exports.createLsFilesClient=gr;exports.createUploadBlobClient=pr;exports.createUploadProgressClient=ur;exports.dataToHandle=K;exports.getStream=mr;exports.handleToData=S;exports.headersFromProto=me;exports.importToUploadOpts=Be;exports.isLiveLogHandle=N;exports.isReadyLogHandle=Ie;exports.lineToProgress=ke;exports.mixcrProgressFromLogs=yr;
1
+ "use strict";var Oe=Object.defineProperty;var Me=(s,e,t)=>e in s?Oe(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var h=(s,e,t)=>Me(s,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Ee=require("node:fs/promises"),p=require("@milaboratories/pl-client"),m=require("@protobuf-ts/runtime-rpc"),i=require("@protobuf-ts/runtime"),ce=require("undici"),f=require("@milaboratories/ts-helpers"),P=require("node:stream"),He=require("node:fs"),Ae=require("node:path"),j=require("node:stream/consumers"),g=require("@milaboratories/computable"),G=require("node:timers/promises"),ze=require("node:readline/promises"),xe=require("denque"),je=require("node:os"),b=require("node:crypto"),T=require("@milaboratories/pl-tree"),k=require("zod"),Ge=require("node:zlib"),Ve=require("tar-fs");function U(s){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const t in s)if(t!=="default"){const n=Object.getOwnPropertyDescriptor(s,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:()=>s[t]})}}return e.default=s,Object.freeze(e)}const y=U(Ee),D=U(He),w=U(Ae),qe=U(ze),te=U(je),Je=U(Ge),Ze=U(Ve);class Qe extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Qe;class Xe extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Xe;class Ye extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Ke=new Ye;class et extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Response",[{no:1,name:"parts_count",kind:"scalar",T:4,L:0},{no:2,name:"uploaded_parts",kind:"scalar",repeat:1,T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.partsCount=0n,t.uploadedParts=[],e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.partsCount=e.uint64().toBigInt();break;case 2:if(d===i.WireType.LengthDelimited)for(let L=e.int32()+e.pos;e.pos<L;)o.uploadedParts.push(e.uint64().toBigInt());else o.uploadedParts.push(e.uint64().toBigInt());break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){if(e.partsCount!==0n&&t.tag(1,i.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,i.WireType.LengthDelimited).fork();for(let o=0;o<e.uploadedParts.length;o++)t.uint64(e.uploadedParts[o]);t.join()}let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const tt=new et;class rt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new rt;class nt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"bytes_processed",kind:"scalar",T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.bytesProcessed=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.bytesProcessed=e.int64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,i.WireType.Varint).int64(e.bytesProcessed);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ot=new nt;class st extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const it=new st;class at extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new at;class lt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"part_number",kind:"scalar",T:4,L:0},{no:3,name:"uploaded_part_size",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.partNumber=0n,t.uploadedPartSize=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.partNumber=e.uint64().toBigInt();break;case 3:o.uploadedPartSize=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,i.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,i.WireType.Varint).uint64(e.uploadedPartSize);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const dt=new lt;class ct extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.name!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.value);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const B=new ct;class ht extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Response",[{no:1,name:"upload_url",kind:"scalar",T:9},{no:2,name:"method",kind:"scalar",T:9},{no:3,name:"headers",kind:"message",repeat:1,T:()=>B},{no:4,name:"chunk_start",kind:"scalar",T:4,L:0},{no:5,name:"chunk_end",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.uploadUrl="",t.method="",t.headers=[],t.chunkStart=0n,t.chunkEnd=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.uploadUrl=e.string();break;case 2:o.method=e.string();break;case 3:o.headers.push(B.internalBinaryRead(e,e.uint32(),n));break;case 4:o.chunkStart=e.uint64().toBigInt();break;case 5:o.chunkEnd=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.uploadUrl!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.method);for(let o=0;o<e.headers.length;o++)B.internalBinaryWrite(e.headers[o],t.tag(3,i.WireType.LengthDelimited).fork(),n).join();e.chunkStart!==0n&&t.tag(4,i.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,i.WireType.Varint).uint64(e.chunkEnd);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ut=new ht;class pt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new pt;class gt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ft=new gt;class mt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const wt=new mt,_=new m.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:Ke,O:tt},{name:"GetPartURL",options:{},I:dt,O:ut},{name:"UpdateProgress",options:{},I:ot,O:it},{name:"Finalize",options:{},I:ft,O:wt}]);class yt{constructor(e){h(this,"typeName",_.typeName);h(this,"methods",_.methods);h(this,"options",_.options);this._transport=e}init(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}getPartURL(e,t){const n=this.methods[1],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}updateProgress(e,t){const n=this.methods[2],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}finalize(e,t){const n=this.methods[3],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class V extends Error{}class q extends Error{}class he extends Error{}class J extends Error{}class ue{constructor(e,t,n,r){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new yt(this.grpcTransport)}close(){}async initUpload({id:e,type:t},n){const r=await this.grpcClient.init({resourceId:e},p.addRTypeToMetadata(t,n));return this.partsToUpload(r.response)}async partUpload({id:e,type:t},n,r,o,c,a){const d=await this.grpcClient.getPartURL({resourceId:e,partNumber:r,uploadedPartSize:0n},p.addRTypeToMetadata(t,a)).response,{chunk:l,mTime:u}=await this.readChunk(n,d.chunkStart,d.chunkEnd);if(u>c)throw new V("file was modified, expected mtime: "+c+", got: "+u+".");const L=await ce.request(d.uploadUrl,this.prepareUploadOpts(d,l)),ee=await L.body.text();if(this.logger.info(`uploaded chunk ${r} from ${o} of resource: ${e}, response: '${ee.toString()}', status code: ${L.statusCode}`),L.statusCode!=200)throw new he(`response is not ok, status code: ${L.statusCode}, body: ${ee}, headers: ${L.headers}, url: ${d.uploadUrl}`);await this.grpcClient.updateProgress({resourceId:e,bytesProcessed:d.chunkEnd-d.chunkStart},p.addRTypeToMetadata(t,a))}async finalizeUpload({id:e,type:t},n){return await this.grpcClient.finalize({resourceId:e},p.addRTypeToMetadata(t,n))}async readChunk(e,t,n){let r;try{r=await y.open(e);const o=Number(n-t),c=Number(t),a=Buffer.alloc(o),d=await this.readBytesFromPosition(r,a,o,c),l=await y.stat(e);return{chunk:a.subarray(0,d),mTime:BigInt(Math.floor(l.mtimeMs/1e3))}}catch(o){throw o.code=="ENOENT"?new J(`there is no file ${e} for uploading`):o}finally{r==null||r.close()}}async readBytesFromPosition(e,t,n,r){let o=0;for(;o<n;){const{bytesRead:c}=await e.read(t,o,n-o,r+o);if(c===0)throw new q("file ended earlier than expected.");o+=c}return o}partsToUpload(e){const t=[],n=new Set(e.uploadedParts);for(let r=1n;r<=e.partsCount;r++)n.has(r)||t.push(r);return t}prepareUploadOpts(e,t){const n=e.headers.map(({name:r,value:o})=>[r,o]);return{dispatcher:this.httpClient,body:t,headers:Object.fromEntries(n),method:e.method.toUpperCase()}}}class Tt extends i.MessageType{constructor(){super("google.protobuf.Duration",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}internalJsonWrite(e,t){let n=i.PbLong.from(e.seconds).toNumber();if(n>315576e6||n<-315576e6)throw new Error("Duration value out of range.");let r=e.seconds.toString();if(n===0&&e.nanos<0&&(r="-"+r),e.nanos!==0){let o=Math.abs(e.nanos).toString();o="0".repeat(9-o.length)+o,o.substring(3)==="000000"?o=o.substring(0,3):o.substring(6)==="000"&&(o=o.substring(0,6)),r+="."+o}return r+"s"}internalJsonRead(e,t,n){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+i.typeofJsonValue(e)+". Expected string.");let r=e.match(/^(-?)([0-9]+)(?:\.([0-9]+))?s/);if(r===null)throw new Error("Unable to parse Duration from JSON string. Invalid format.");n||(n=this.create());let[,o,c,a]=r,d=i.PbLong.from(o+c);if(d.toNumber()>315576e6||d.toNumber()<-315576e6)throw new Error("Unable to parse Duration from JSON string. Value out of range.");if(n.seconds=d.toBigInt(),typeof a=="string"){let l=o+a+"0".repeat(9-a.length);n.nanos=parseInt(l)}return n}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.seconds!==0n&&t.tag(1,i.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,i.WireType.Varint).int32(e.nanos);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const v=new Tt;class bt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new bt;class kt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.Report",[{no:1,name:"progress",kind:"scalar",T:2},{no:2,name:"bytes_processed",kind:"scalar",T:4,L:0},{no:3,name:"bytes_total",kind:"scalar",T:4,L:0},{no:4,name:"done",kind:"scalar",T:8},{no:5,name:"name",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.progress=0,t.bytesProcessed=0n,t.bytesTotal=0n,t.done=!1,t.name="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.progress=e.float();break;case 2:o.bytesProcessed=e.uint64().toBigInt();break;case 3:o.bytesTotal=e.uint64().toBigInt();break;case 4:o.done=e.bool();break;case 5:o.name=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.progress!==0&&t.tag(1,i.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,i.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,i.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,i.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,i.WireType.LengthDelimited).string(e.name);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const I=new kt;class Lt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Lt;class Pt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Ut=new Pt;class It extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),n,o.report);break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.report&&I.internalBinaryWrite(e.report,t.tag(1,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Rt=new It;class St extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new St;class Nt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"update_interval",kind:"message",T:()=>v}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.updateInterval=v.internalBinaryRead(e,e.uint32(),n,o.updateInterval);break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.updateInterval&&v.internalBinaryWrite(e.updateInterval,t.tag(2,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const vt=new Nt;class Dt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),n,o.report);break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.report&&I.internalBinaryWrite(e.report,t.tag(1,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Bt=new Dt,C=new m.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:Ut,O:Rt},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:vt,O:Bt}]);class _t{constructor(e){h(this,"typeName",C.typeName);h(this,"methods",C.methods);h(this,"options",C.options);this._transport=e}getStatus(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}realtimeStatus(e,t){const n=this.methods[1],r=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,n,r,e)}}class pe{constructor(e,t,n,r){h(this,"grpcClient");this.grpcTransport=e,this.client=n,this.logger=r,this.grpcClient=new _t(this.grpcTransport)}close(){}async getStatus({id:e,type:t},n){const r=await this.grpcClient.getStatus({resourceId:e},p.addRTypeToMetadata(t,n)),o=f.notEmpty(r.response.report);return{done:o.done,progress:o.progress,bytesProcessed:String(o.bytesProcessed),bytesTotal:String(o.bytesTotal)}}async*realtimeStatus({id:e,type:t},n=100,r){r=p.addRTypeToMetadata(t,r);const o=Math.floor(n/1e3),c=(n-o*1e3)*1e6,a=v.create({seconds:BigInt(o),nanos:c});try{const{responses:d}=this.grpcClient.realtimeStatus({resourceId:e,updateInterval:a},r);yield*d}catch(d){throw this.logger.warn("Failed to get realtime status"+d),d}}}class Ct extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Ct;class Ft extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Ft;class $t extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Wt=new $t;class Ot extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.name!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.value);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const F=new Ot;class Mt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Response",[{no:1,name:"download_url",kind:"scalar",T:9},{no:2,name:"headers",kind:"message",repeat:1,T:()=>F}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.downloadUrl=e.string();break;case 2:o.headers.push(F.internalBinaryRead(e,e.uint32(),n));break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.downloadUrl!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.downloadUrl);for(let o=0;o<e.headers.length;o++)F.internalBinaryWrite(e.headers[o],t.tag(2,i.WireType.LengthDelimited).fork(),n).join();let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Et=new Mt,$=new m.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:Wt,O:Et}]);class Ht{constructor(e){h(this,"typeName",$.typeName);h(this,"methods",$.methods);h(this,"options",$.options);this._transport=e}getDownloadURL(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class Z extends Error{}class ge{constructor(e){this.httpClient=e}async downloadRemoteFile(e,t,n){const{statusCode:r,body:o,headers:c}=await ce.request(e,{dispatcher:this.httpClient,headers:t,signal:n}),a=P.Readable.toWeb(o);if(r!=200){const d=await j.text(a),l=d.substring(0,Math.min(d.length,1e3));throw 400<=r&&r<500?new Z(`Http error: statusCode: ${r} url: ${e.toString()}, beginning of body: ${l}`):new Error(`Http error: statusCode: ${r} url: ${e.toString()}`)}return{content:a,size:Number(c["content-length"])}}}const At="storage://",re=/storage:\/\/(?<storageId>.*?)\/(?<localPath>.*)/;class Q extends Error{}class X extends Error{}class fe{constructor(e,t,n,r){h(this,"grpcClient");h(this,"downloadHelper");h(this,"isLocal",e=>e.startsWith(At));this.grpcTransport=e,this.httpClient=t,this.logger=n,this.localStorageIdsToRoot=r,this.grpcClient=new Ht(this.grpcTransport),this.downloadHelper=new ge(t)}close(){}async getUrl({id:e,type:t},n,r){const o=n??{};return o.abort=r,await this.grpcClient.getDownloadURL({resourceId:e},p.addRTypeToMetadata(t,o)).response}async downloadBlob(e,t,n){const{downloadUrl:r,headers:o}=await this.getUrl(e,t,n);return this.logger.info(`download from url ${r}`),this.isLocal(r)?await this.readLocalFile(r):await this.downloadHelper.downloadRemoteFile(r,me(o),n)}async readLocalFile(e){const t=e.match(re);if(t===null||t.length!=3)throw new X(`url for local filepath ${e} does not match regex ${re}, parsed: ${t}`);const[n,r,o]=t;if(this.localStorageIdsToRoot[r]==null)throw new Q(`Unknown storage location: ${r}`);const c=this.localStorageIdsToRoot[r],a=w.join(c,o),l=(await y.stat(a)).size;return{content:P.Readable.toWeb(D.createReadStream(a)),size:l}}}function me(s){return Object.fromEntries(s.map(({name:e,value:t})=>[e,t]))}class zt extends i.MessageType{constructor(){super("google.protobuf.Timestamp",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}now(){const e=this.create(),t=Date.now();return e.seconds=i.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(i.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),n=e.getTime();return t.seconds=i.PbLong.from(Math.floor(n/1e3)).toBigInt(),t.nanos=n%1e3*1e6,t}internalJsonWrite(e,t){let n=i.PbLong.from(e.seconds).toNumber()*1e3;if(n<Date.parse("0001-01-01T00:00:00Z")||n>Date.parse("9999-12-31T23:59:59Z"))throw new Error("Unable to encode Timestamp to JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");if(e.nanos<0)throw new Error("Unable to encode invalid Timestamp to JSON. Nanos must not be negative.");let r="Z";if(e.nanos>0){let o=(e.nanos+1e9).toString().substring(1);o.substring(3)==="000000"?r="."+o.substring(0,3)+"Z":o.substring(6)==="000"?r="."+o.substring(0,6)+"Z":r="."+o+"Z"}return new Date(n).toISOString().replace(".000Z",r)}internalJsonRead(e,t,n){if(typeof e!="string")throw new Error("Unable to parse Timestamp from JSON "+i.typeofJsonValue(e)+".");let r=e.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|\.([0-9]{3,9})Z|([+-][0-9][0-9]:[0-9][0-9]))$/);if(!r)throw new Error("Unable to parse Timestamp from JSON. Invalid format.");let o=Date.parse(r[1]+"-"+r[2]+"-"+r[3]+"T"+r[4]+":"+r[5]+":"+r[6]+(r[8]?r[8]:"Z"));if(Number.isNaN(o))throw new Error("Unable to parse Timestamp from JSON. Invalid value.");if(o<Date.parse("0001-01-01T00:00:00Z")||o>Date.parse("9999-12-31T23:59:59Z"))throw new globalThis.Error("Unable to parse Timestamp from JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");return n||(n=this.create()),n.seconds=i.PbLong.from(o/1e3).toBigInt(),n.nanos=0,r[7]&&(n.nanos=parseInt("1"+r[7]+"0".repeat(9-r[7].length))-1e9),n}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.seconds!==0n&&t.tag(1,i.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,i.WireType.Varint).int32(e.nanos);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const W=new zt;class xt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new xt;class jt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.ListItem",[{no:1,name:"name",kind:"scalar",T:9},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"is_dir",kind:"scalar",T:8},{no:10,name:"full_name",kind:"scalar",T:9},{no:11,name:"directory",kind:"scalar",T:9},{no:12,name:"last_modified",kind:"message",T:()=>W},{no:13,name:"version",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.size=0n,t.isDir=!1,t.fullName="",t.directory="",t.version="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.isDir=e.bool();break;case 10:o.fullName=e.string();break;case 11:o.directory=e.string();break;case 12:o.lastModified=W.internalBinaryRead(e,e.uint32(),n,o.lastModified);break;case 13:o.version=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.name!==""&&t.tag(1,i.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,i.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,i.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,i.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,i.WireType.LengthDelimited).string(e.directory),e.lastModified&&W.internalBinaryWrite(e.lastModified,t.tag(12,i.WireType.LengthDelimited).fork(),n).join(),e.version!==""&&t.tag(13,i.WireType.LengthDelimited).string(e.version);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const O=new jt;class Gt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Gt;class Vt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"location",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.location="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.location=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.location);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const qt=new Vt;class Jt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:1,T:()=>O},{no:2,name:"delimiter",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.items=[],t.delimiter="",e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.items.push(O.internalBinaryRead(e,e.uint32(),n));break;case 2:o.delimiter=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){for(let o=0;o<e.items.length;o++)O.internalBinaryWrite(e.items[o],t.tag(1,i.WireType.LengthDelimited).fork(),n).join();e.delimiter!==""&&t.tag(2,i.WireType.LengthDelimited).string(e.delimiter);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Zt=new Jt,M=new m.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:qt,O:Zt}]);class Qt{constructor(e){h(this,"typeName",M.typeName);h(this,"methods",M.methods);h(this,"options",M.options);this._transport=e}list(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class we{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=new Qt(e)}close(){}async list(e,t,n){return await this.grpcClient.list({resourceId:e.id,location:t},p.addRTypeToMetadata(e.type,n)).response}}class Xt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){return r??this.create()}internalBinaryWrite(e,t,n){let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}new Xt;class Yt extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;case 20:o.readLimit=e.int64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,i.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,i.WireType.Varint).int64(e.readLimit);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const Kt=new Yt;class er extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,i.WireType.Varint).uint32(e.chunkSize);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const tr=new er;class rr extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,i.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,i.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,i.WireType.LengthDelimited).string(e.searchRe);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const nr=new rr;class or extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,i.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,i.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,i.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,i.WireType.LengthDelimited).string(e.searchRe);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const sr=new or;class ir extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.LastLines",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",opt:!0,T:3,L:0},{no:3,name:"line_count",kind:"scalar",opt:!0,T:5},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 3:o.lineCount=e.int32();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.resourceId!==0n&&t.tag(1,i.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,i.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,i.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,i.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,i.WireType.LengthDelimited).string(e.searchRe);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const ar=new ir;class lr extends i.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.Response",[{no:1,name:"data",kind:"scalar",T:12},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"new_offset",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.data=new Uint8Array(0),t.size=0n,t.newOffset=0n,e!==void 0&&i.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,n,r){let o=r??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.data=e.bytes();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.newOffset=e.uint64().toBigInt();break;default:let l=n.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?i.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,n){e.data.length&&t.tag(1,i.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,i.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,i.WireType.Varint).uint64(e.newOffset);let r=n.writeUnknownFields;return r!==!1&&(r==!0?i.UnknownFieldHandler.onWrite:r)(this.typeName,e,t),t}}const R=new lr,E=new m.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:Kt,O:R},{name:"ReadBinary",options:{},I:tr,O:R},{name:"StreamText",serverStreaming:!0,options:{},I:nr,O:R},{name:"ReadText",options:{},I:sr,O:R},{name:"LastLines",options:{},I:ar,O:R}]);class dr{constructor(e){h(this,"typeName",E.typeName);h(this,"methods",E.methods);h(this,"options",E.options);this._transport=e}streamBinary(e,t){const n=this.methods[0],r=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,n,r,e)}readBinary(e,t){const n=this.methods[1],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}streamText(e,t){const n=this.methods[2],r=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,n,r,e)}readText(e,t){const n=this.methods[3],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}lastLines(e,t){const n=this.methods[4],r=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,n,r,e)}}class ye{constructor(e,t,n){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=n,this.grpcClient=new dr(this.grpcTransport)}close(){}async lastLines({id:e,type:t},n,r=0n,o,c){return(await this.grpcClient.lastLines({resourceId:e,lineCount:n,offset:r,search:o},p.addRTypeToMetadata(t,c))).response}async readText({id:e,type:t},n,r=0n,o,c){return(await this.grpcClient.readText({resourceId:f.notEmpty(e),readLimit:BigInt(n),offset:r,search:o},p.addRTypeToMetadata(t,c))).response}}const Te=process.env.PL_STORAGE_TO_PATH?Object.fromEntries(process.env.PL_STORAGE_TO_PATH.split(";").map(s=>s.split(":"))):{};function cr(s,e,t){return t===void 0&&(t=Te),e.getDriver({name:"DownloadBlob",init:(r,o,c)=>new fe(o,c,s,t)})}function hr(s,e){return s.getDriver({name:"StreamLogs",init:(t,n,r)=>new ye(n,r,e)})}function ur(s,e){return s.getDriver({name:"UploadProgress",init:(t,n,r)=>new pe(n,r,s,e)})}function pr(s,e){return s.getDriver({name:"UploadBlob",init:(t,n,r)=>new ue(n,r,s,e)})}function gr(s,e){return s.getDriver({name:"LsFiles",init:(t,n,r)=>new we(n,e)})}class fr{constructor(e,t){h(this,"updater");h(this,"schedule",()=>this.updater.schedule());this.onUpdate=e,this.sleepMs=t,this.updater=new Y(async()=>{for(;;){if(await this.onUpdate())return;await G.scheduler.wait(this.sleepMs)}})}}class Y{constructor(e){h(this,"updating");this.onUpdate=e}schedule(){this.updating==null&&(this.updating=(async()=>{try{await this.onUpdate()}catch(e){console.log(`error while updating in Updater: ${e}`)}finally{this.updating=void 0}})())}}async function mr(s,e){return s.withReadTx("LogsDriverGetStream",async t=>{const n=await t.getResourceData(e,!0),r=await p.valErr(t,p.getField(n,"stream"));if(r.error!="")throw new Error(`while getting stream: ${r.error}`);if(!p.isNullResourceId(r.valueId))return await t.getResourceData(r.valueId,!1)})}const be="8C7#F1328%9E089B3D22",wr=/(?<stage>.*):\s*(?<progress>[\d.]+%)\s.*(?<eta>ETA:.*)/g;function ke(s){const t=s.replace(be,"").match(wr);if(t==null||t.length!=4)return;const[n,r,o,c]=t;return{stage:r,progress:o,eta:c}}async function yr(s,e,t){const n=await e.lastLines(s,1,0n,be,t);if(n.data==null||n.data.length==0)return{found:!1};const r=n.data.toString().split(/\r?\n/)[0];if(r==null)return{found:!1};const o=ke(r);return o===void 0?{found:!1}:{found:!0,...o}}class Le{constructor(e){h(this,"cache",new Map);h(this,"totalSizeBytes",0);this.softSizeBytes=e}existsFile(e){return this.cache.get(e)!=null}getFile(e,t){const n=this.cache.get(e);return n!=null&&n.counter.inc(t),n}removeFile(e,t){return f.mapGet(this.cache,e).counter.dec(t),this.toDelete()}toDelete(){if(this.totalSizeBytes<=this.softSizeBytes)return[];const e=[];let t=0;return f.mapEntries(this.cache).filter(([n,r])=>r.counter.isZero()).forEach(([n,r])=>{if(this.totalSizeBytes-t<=this.softSizeBytes)return;const o=f.mapGet(this.cache,n);t+=o.sizeBytes,e.push(o)}),e}addCache(e,t){const n=this.cache.get(e.path)==null;if(this.cache.set(e.path,e),e.counter.inc(t),e.sizeBytes<0)throw new Error(`empty sizeBytes: ${e}`);n&&(this.totalSizeBytes+=e.sizeBytes)}removeCache(e){this.cache.delete(e.path),this.totalSizeBytes-=e.sizeBytes}}class Tr{constructor(e,t){this.logsStreamDriver=e,this.downloadDriver=t}getLastLogs(e,t,n){if(n===void 0)return g.Computable.make(o=>this.getLastLogs(e,t,o));const r=A(n,e);if(r===void 0){n.markUnstable("no stream in stream manager");return}if(H(r))return this.downloadDriver.getLastLogs(r,t,n);try{return this.logsStreamDriver.getLastLogs(r,t,n)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){n.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${o}`);return}throw o}}getProgressLog(e,t,n){if(n===void 0)return g.Computable.make(o=>this.getProgressLog(e,t,o));const r=A(n,e);if(r===void 0){n.markUnstable("no stream in stream manager");return}if(H(r))return this.downloadDriver.getProgressLog(r,t,n);try{return this.logsStreamDriver.getProgressLog(r,t,n)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){n.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${o}`);return}throw o}}getLogHandle(e,t){if(t===void 0)return g.Computable.make(r=>this.getLogHandle(e,r));const n=A(t,e);if(n===void 0){t.markUnstable("no stream in stream manager");return}return H(n)?this.downloadDriver.getLogHandle(n,t):this.logsStreamDriver.getLogHandle(n,t)}async lastLines(e,t,n,r){return N(e)?await this.logsStreamDriver.lastLines(e,t,n,r):await this.downloadDriver.lastLines(e,t,n,r)}async readText(e,t,n,r){return N(e)?await this.logsStreamDriver.readText(e,t,n,r):await this.downloadDriver.readText(e,t,n,r)}}function H(s){return!s.type.name.startsWith("StreamWorkdir")}function A(s,e){var t;return(t=s.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}function S(s){let e;if(N(s))e=s.match(Pe);else if(Ie(s))e=s.match(Ue);else throw new Error(`Log handle is malformed: ${s}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${s}`);const{resourceType:t,resourceVersion:n,resourceId:r}=e.groups;return{id:p.bigintToResourceId(BigInt(r)),type:{name:t,version:n}}}function K(s,e){return s?`log+live://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`:`log+ready://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`}const Pe=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function N(s){return Pe.test(s)}const Ue=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function Ie(s){return Ue.test(s)}const Re=T.rsSchema({kv:{"ctl/file/blobInfo":k.z.object({sizeBytes:k.z.coerce.number()})}});class br{constructor(e,t,n,r,o,c){h(this,"idToDownload",new Map);h(this,"cache");h(this,"downloadQueue");h(this,"idToOnDemand",new Map);h(this,"idToLastLines",new Map);h(this,"idToProgressLog",new Map);h(this,"saveDir");this.logger=e,this.clientDownload=t,this.clientLogs=n,this.signer=o,this.cache=new Le(c.cacheSoftSizeBytes),this.downloadQueue=new f.TaskProcessor(this.logger,c.nConcurrentDownloads),this.saveDir=w.resolve(r)}getDownloadedBlob(e,t){if(t===void 0)return g.Computable.make(c=>this.getDownloadedBlob(e,c));const n=T.treeEntryToResourceInfo(e,t),r=b.randomUUID();t.addOnDestroy(()=>this.releaseBlob(n.id,r));const o=this.getDownloadedBlobNoCtx(t.watcher,n,r);return o==null&&t.markUnstable("download blob is still undefined"),o}getOnDemandBlob(e,t){if(t===void 0)return g.Computable.make(c=>this.getOnDemandBlob(e,c));const n=T.isPlTreeEntry(e)?T.makeResourceSnapshot(e,Re,t):e,r=b.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(n.id,r)),this.getOnDemandBlobNoCtx(t.watcher,n,r)}getLocalPath(e){return z(e,this.signer)}async getContent(e){if(Ur(e))return await Lr(this.getLocalPath(e));if(!Rr(e))throw new Error("Malformed remote handle");const t=Sr(e,this.signer),{content:n}=await this.clientDownload.downloadBlob(t);return await j.buffer(n)}getDownloadedBlobNoCtx(e,t,n){let r=this.idToDownload.get(t.id);if(r===void 0){const c=this.setNewDownloadTask(e,t,n);this.downloadQueue.push({fn:()=>this.downloadBlob(c,n),recoverableErrorPredicate:a=>!0}),r=c}r.attach(e,n);const o=r.getBlob();if(o!==void 0){if(o.ok)return o.value;throw o.error}}setNewDownloadTask(e,t,n){const r=this.getFilePath(t.id),o=new Se(this.clientDownload,t,r,Ir(r,this.signer));return this.idToDownload.set(t.id,o),o}async downloadBlob(e,t){var n;await e.download(),(n=e.getBlob())!=null&&n.ok&&this.cache.addCache(e,t)}getOnDemandBlobNoCtx(e,t,n){let r=this.idToOnDemand.get(t.id);return r===void 0&&(r=new kr(t.kv["ctl/file/blobInfo"].sizeBytes,Nr(t,this.signer)),this.idToOnDemand.set(t.id,r)),r.attach(e,n),r.getHandle()}getLastLogs(e,t,n){if(n==null)return g.Computable.make(a=>this.getLastLogs(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.addOnDestroy(()=>this.releaseBlob(r.id,o));const c=this.getLastLogsNoCtx(n.watcher,r,t,o);return c==null&&n.markUnstable("either a file was not downloaded or logs was not read"),c}getLastLogsNoCtx(e,t,n,r){const o=this.getDownloadedBlobNoCtx(e,t,r);if(o==null)return;const c=z(o.handle,this.signer);let a=this.idToLastLines.get(t.id);if(a==null){const l=new ne(c,n);this.idToLastLines.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getProgressLog(e,t,n){if(n==null)return g.Computable.make(a=>this.getProgressLog(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.addOnDestroy(()=>this.releaseBlob(r.id,o));const c=this.getProgressLogNoCtx(n.watcher,r,t,o);return c===void 0&&n.markUnstable("either a file was not downloaded or a progress log was not read"),c}getProgressLogNoCtx(e,t,n,r){const o=this.getDownloadedBlobNoCtx(e,t,r);if(o==null)return;const c=z(o.handle,this.signer);let a=this.idToProgressLog.get(t.id);if(a==null){const l=new ne(c,1,n);this.idToProgressLog.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getLogHandle(e,t){if(t==null)return g.Computable.make(r=>this.getLogHandle(e,r));const n=T.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(n)}getLogHandleNoCtx(e){return K(!1,e)}async lastLines(e,t,n,r){const o=await this.clientLogs.lastLines(S(e),t,BigInt(n??0),r);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async readText(e,t,n,r){const o=await this.clientLogs.readText(S(e),t,BigInt(n??0),r);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async releaseBlob(e,t){const n=this.idToDownload.get(e);if(n!=null)if(this.cache.existsFile(n.path)){const r=this.cache.removeFile(n.path,t);await Promise.all(r.map(async o=>{await y.rm(o.path),this.cache.removeCache(o),this.removeTask(o,`the task ${o.path} was removedfrom cache along with ${r.map(c=>c.path)}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${n.path} was removed from cache`)}removeTask(e,t){e.abort(t),e.change.markChanged(),this.idToDownload.delete(e.rInfo.id),this.idToLastLines.delete(e.rInfo.id),this.idToProgressLog.delete(e.rInfo.id)}async releaseOnDemandBlob(e,t){var r;(((r=this.idToOnDemand.get(e))==null?void 0:r.release(t))??!1)&&this.idToOnDemand.delete(e)}async releaseAll(){this.downloadQueue.stop(),this.idToDownload.forEach((e,t)=>{this.idToDownload.delete(t),e.change.markChanged()})}getFilePath(e){return w.resolve(w.join(this.saveDir,String(BigInt(e))))}}class kr{constructor(e,t){h(this,"change",new g.ChangeSource);h(this,"counter",new f.CallersCounter);this.size=e,this.handle=t}getHandle(){return{handle:this.handle,size:this.size}}attach(e,t){this.counter.inc(t),this.change.attachWatcher(e)}release(e){return this.counter.dec(e)}}class ne{constructor(e,t,n){h(this,"updater");h(this,"log");h(this,"change",new g.ChangeSource);h(this,"error");this.path=e,this.lines=t,this.patternToSearch=n,this.updater=new Y(async()=>this.update())}getOrSchedule(e){return this.change.attachWatcher(e),this.updater.schedule(),{log:this.log,error:this.error}}async update(){try{const e=await Pr(this.path,this.lines,this.patternToSearch);this.log!=e&&this.change.markChanged(),this.log=e}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.log="",this.error=e,this.change.markChanged();return}throw e}}}async function oe(s){try{return await y.access(s),!0}catch{return!1}}async function Lr(s){return await j.buffer(P.Readable.toWeb(D.createReadStream(s)))}function Pr(s,e,t){const n=D.createReadStream(s),r=new P.Writable;return new Promise((o,c)=>{const a=qe.createInterface(n,r),d=new xe;a.on("line",function(l){t!=null&&!l.includes(t)||(d.push(l),d.length>e&&d.shift())}),a.on("error",c),a.on("close",function(){o(d.toArray().join(te.EOL)+te.EOL)})})}let Se=class{constructor(e,t,n,r){h(this,"counter",new f.CallersCounter);h(this,"change",new g.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"sizeBytes",0);this.clientDownload=e,this.rInfo=t,this.path=n,this.handle=r}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(){try{const{content:e,size:t}=await this.clientDownload.downloadBlob(this.rInfo);if(await oe(w.dirname(this.path))||await y.mkdir(w.dirname(this.path),{recursive:!0}),await oe(this.path))await e.cancel("the file already existed");else{const n=P.Writable.toWeb(D.createWriteStream(this.path));await e.pipeTo(n)}this.setDone(t)}catch(e){if(e instanceof se||e instanceof Z||e instanceof Q||e instanceof X||e.code=="ENOENT"){this.setError(e),await y.rm(this.path);return}throw e}}getBlob(){if(this.done)return{ok:!0,value:{handle:this.handle,size:this.sizeBytes}};if(this.error)return{ok:!1,error:this.error}}setDone(e){this.done=!0,this.sizeBytes=e,this.change.markChanged()}abort(e){this.signalCtl.abort(new se(e))}setError(e){this.error=e,this.change.markChanged()}};class se extends Error{}const Ne=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function Ur(s){return!!s.match(Ne)}function z(s,e){const t=s.match(Ne);if(t===null)throw new Error(`Local handle is malformed: ${s}, matches: ${t}`);const{path:n,signature:r}=t.groups;return e.verify(n,r,`Signature verification failed for: ${s}`),n}function Ir(s,e){return`blob+local://download/${s}#${e.sign(s)}`}const ve=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*))#(?<signature>.*)$/;function Rr(s){return!!s.match(ve)}function Sr(s,e){const t=s.match(ve);if(t===null)throw new Error(`Remote handle is malformed: ${s}, matches: ${t}`);const{content:n,resourceType:r,resourceVersion:o,resourceId:c,signature:a}=t.groups;return e.verify(n,a,`Signature verification failed for ${s}`),{id:p.bigintToResourceId(BigInt(c)),type:{name:r,version:o}}}function Nr(s,e){const t=`${s.type.name}/${s.type.version}/${BigInt(s.id)}`;return`blob+remote://download/${t}#${e.sign(t)}`}const vr=k.z.object({localPath:k.z.string(),pathSignature:k.z.string(),modificationTime:k.z.string()}),Dr=k.z.union([vr,k.z.object({})]),De=T.rsSchema({data:Dr,fields:{blob:!1,incarnation:!1}});class Br{constructor(e,t,n,r,o={nConcurrentPartUploads:10,nConcurrentGetProgresses:10,pollingInterval:1e3,stopPollingDelay:1e3}){h(this,"idToProgress",new Map);h(this,"uploadQueue");h(this,"hooks");h(this,"scheduledOnNextState",[]);h(this,"keepRunning",!1);h(this,"currentLoop");this.logger=e,this.signer=t,this.clientBlob=n,this.clientProgress=r,this.opts=o,this.uploadQueue=new f.TaskProcessor(this.logger,1,{type:"exponentialWithMaxDelayBackoff",initialDelay:20,maxDelay:15e3,backoffMultiplier:1.5,jitter:.5}),this.hooks=new g.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:o.stopPollingDelay},(c,a)=>this.scheduleOnNextState(c,a))}getProgressId(e,t){if(t==null)return g.Computable.make(c=>this.getProgressId(e,c));const n=T.isPlTreeEntry(e)?T.makeResourceSnapshot(e,De,t):e,r=b.randomUUID();t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(n.id,r));const o=this.getProgressIdNoCtx(t.watcher,n,r);return ie(o)||t.markUnstable(`upload/index progress was got, but it's not stable: ${o}`),o}getProgressIdNoCtx(e,t,n){const r=t.fields.blob!=null||t.fields.incarnation!=null,o=this.idToProgress.get(t.id);if(o!=null)return o.attach(e,n),o.mustGetProgress(r);const c=new _r(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,c),c.attach(e,n),c.progress.isUpload&&c.progress.isUploadSignMatch&&this.uploadQueue.push({fn:()=>c.uploadBlobTask(),recoverableErrorPredicate:a=>!_e(a)}),c.mustGetProgress(r)}async release(e,t){const n=this.idToProgress.get(e);if(n===void 0)return;n.decCounter(t)&&this.idToProgress.delete(e)}async releaseAll(){this.uploadQueue.stop()}scheduleOnNextState(e,t){this.scheduledOnNextState.push({resolve:e,reject:t})}startUpdating(){this.keepRunning=!0,this.currentLoop===void 0&&(this.currentLoop=this.mainLoop())}stopUpdating(){this.keepRunning=!1}async mainLoop(){for(;this.keepRunning;){const e=this.scheduledOnNextState;this.scheduledOnNextState=[];try{await f.asyncPool(this.opts.nConcurrentGetProgresses,this.getAllNotDoneProgresses().map(t=>async()=>await t.updateStatus())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(n=>n.reject(t))}if(!this.keepRunning)break;await G.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!ie(t.progress)).map(([e,t])=>t)}}class _r{constructor(e,t,n,r,o,c){h(this,"change",new g.ChangeSource);h(this,"counter",new f.CallersCounter);h(this,"progress");h(this,"uploadOpts");h(this,"uploadingTerminallyFailed");this.logger=e,this.clientBlob=t,this.clientProgress=n,this.nConcurrentPartsUpload=r,this.res=c;const a=c.type.name.startsWith("BlobUpload");let d;a&&(this.uploadOpts=Be(c),d=Fr(o,this.uploadOpts.localPath,this.uploadOpts.pathSignature)),this.progress={done:!1,status:void 0,isUpload:a,isUploadSignMatch:d,lastError:void 0}}mustGetProgress(e){if(e)return this.setDone(e),this.progress;if(this.uploadingTerminallyFailed)throw this.logger.error(`Uploading terminally failed: ${this.progress.lastError}`),new Error(this.progress.lastError);return this.progress}attach(e,t){this.change.attachWatcher(e),this.counter.inc(t)}decCounter(e){return this.counter.dec(e)}async uploadBlobTask(){try{await this.uploadBlob()}catch(e){if(this.setLastError(e),ae(e)){this.logger.warn(`resource was deleted while uploading a blob: ${e}`),this.change.markChanged(),this.setDone(!0);return}throw this.logger.error(`error while uploading a blob: ${e}`),this.change.markChanged(),_e(e)&&this.terminateWithError(e),e}}async uploadBlob(){if(this.counter.isZero())return;const e=await this.clientBlob.initUpload(this.res);this.logger.info(`start to upload blob ${this.res.id}, parts count: ${e.length}`);const t=n=>async()=>{this.counter.isZero()||await this.clientBlob.partUpload(this.res,this.uploadOpts.localPath,n,e.length,BigInt(this.uploadOpts.modificationTime))};await f.asyncPool(this.nConcurrentPartsUpload,e.map(t)),!this.counter.isZero()&&(await this.clientBlob.finalizeUpload(this.res),this.logger.info(`uploading of resource ${this.res.id} finished.`),this.change.markChanged())}terminateWithError(e){this.progress.lastError=String(e),this.progress.done=!1,this.uploadingTerminallyFailed=!0}setLastError(e){this.progress.lastError=String(e)}setDone(e){this.progress.done=e,e&&(this.progress.lastError=void 0)}async updateStatus(){try{const e=await this.clientProgress.getStatus(this.res),t=this.progress.status;this.progress.status=Cr(e),this.setDone(e.done),(e.done||e.progress!=(t==null?void 0:t.progress))&&this.change.markChanged()}catch(e){if(this.setLastError(e),e.name=="RpcError"&&e.code=="DEADLINE_EXCEEDED"){this.logger.warn("deadline exceeded while getting a status of BlobImport");return}if(ae(e)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${e}, ${p.stringifyWithResourceId(this.res)}`),this.change.markChanged(),this.setDone(!0);return}this.logger.error(`error while updating a status of BlobImport: ${e}`),this.change.markChanged(),this.terminateWithError(e)}}}function ie(s){return s.done&&s.status!==void 0&&s.status!==null&&s.status.progress>=1}function Be(s){if(s.data==null||!("modificationTime"in s.data))throw new Error("no upload options in BlobUpload resource data: "+p.stringifyWithResourceId(s.data));const e=s.data;if(e.modificationTime===void 0)throw new Error("no modification time in data: "+p.stringifyWithResourceId(s.data));if(e.localPath===void 0)throw new Error("no local path in data: "+p.stringifyWithResourceId(s.data));if(e.pathSignature===void 0)throw new Error("no path signature in data: "+p.stringifyWithResourceId(s.data));return{modificationTime:e.modificationTime,localPath:e.localPath,pathSignature:e.pathSignature}}function Cr(s){return{progress:s.progress??0,bytesProcessed:Number(s.bytesProcessed),bytesTotal:Number(s.bytesTotal)}}function Fr(s,e,t){try{return s.verify(e,t),!0}catch{return!1}}function _e(s){return s instanceof V||s instanceof q||s instanceof J}function ae(s){return s.name=="RpcError"&&(s.code=="NOT_FOUND"||s.code=="ABORTED"||s.code=="ALREADY_EXISTS")}class $r{constructor(e,t={nConcurrentGetLogs:10,pollingInterval:1e3,stopPollingDelay:1e3}){h(this,"idToLastLines",new Map);h(this,"idToProgressLog",new Map);h(this,"hooks");h(this,"scheduledOnNextState",[]);h(this,"keepRunning",!1);h(this,"currentLoop");this.clientLogs=e,this.opts=t,this.hooks=new g.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:t.stopPollingDelay},(n,r)=>this.scheduleOnNextState(n,r))}getLastLogs(e,t,n){if(n==null)return g.Computable.make(a=>this.getLastLogs(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.attacheHooks(this.hooks),n.addOnDestroy(()=>this.releaseLastLogs(r.id,o));const c=this.getLastLogsNoCtx(n.watcher,r,t,o);return n.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),c}getLastLogsNoCtx(e,t,n,r){let o=this.idToLastLines.get(t.id);if(o==null){const a=new le(this.clientLogs,t,n);this.idToLastLines.set(t.id,a),o=a}o.attach(e,r);const c=o.getLog();if(c.error!=null)throw c.error;return c.log}getProgressLog(e,t,n){if(n==null)return g.Computable.make(a=>this.getProgressLog(e,t,a));const r=T.treeEntryToResourceInfo(e,n),o=b.randomUUID();n.attacheHooks(this.hooks),n.addOnDestroy(()=>this.releaseProgressLog(r.id,o));const c=this.getProgressLogNoCtx(n.watcher,r,t,o);return n.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),c}getProgressLogNoCtx(e,t,n,r){let o=this.idToProgressLog.get(t.id);if(o==null){const a=new le(this.clientLogs,t,1,n);this.idToProgressLog.set(t.id,a),o=a}o.attach(e,r);const c=o.getLog();if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return g.Computable.make(o=>this.getLogHandle(e,o));const n=T.treeEntryToResourceInfo(e,t),r=this.getLogHandleNoCtx(n);return t.markUnstable(),r}getLogHandleNoCtx(e){return K(!0,e)}async lastLines(e,t,n,r){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(S(e),t,BigInt(n??0),r))}async readText(e,t,n,r){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(S(e),t,BigInt(n??0),r))}async tryWithNotFound(e,t){if(!N(e))throw new Error(`Not live log handle was passed to live log driver, handle: ${e}`);try{const n=await t();return{live:!0,shouldUpdateHandle:!1,data:n.data,size:Number(n.size),newOffset:Number(n.newOffset)}}catch(n){if(n.name=="RpcError"&&n.code=="NOT_FOUND")return{shouldUpdateHandle:!0};throw n}}async releaseLastLogs(e,t){var r;((r=this.idToLastLines.get(e))==null?void 0:r.release(t))&&this.idToLastLines.delete(e)}async releaseProgressLog(e,t){var r;((r=this.idToProgressLog.get(e))==null?void 0:r.release(t))&&this.idToProgressLog.delete(e)}async releaseAll(){}scheduleOnNextState(e,t){this.scheduledOnNextState.push({resolve:e,reject:t})}startUpdating(){this.keepRunning=!0,this.currentLoop===void 0&&(this.currentLoop=this.mainLoop())}stopUpdating(){this.keepRunning=!1}async mainLoop(){for(;this.keepRunning;){const e=this.scheduledOnNextState;this.scheduledOnNextState=[];try{await f.asyncPool(this.opts.nConcurrentGetLogs,this.getAllNotDoneLogs().map(t=>async()=>await t.update())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(n=>n.reject(t))}if(!this.keepRunning)break;await G.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneLogs(){return Array.from(this.idToLastLines.entries()).concat(Array.from(this.idToProgressLog.entries())).filter(([e,t])=>!t.getLog().done).map(([e,t])=>t)}}class le{constructor(e,t,n,r){h(this,"logs");h(this,"error");h(this,"done",!1);h(this,"change",new g.ChangeSource);h(this,"counter",new f.CallersCounter);this.clientLogs=e,this.rInfo=t,this.lines=n,this.patternToSearch=r}getLog(){return{log:this.logs,error:this.error,done:this.done}}attach(e,t){this.change.attachWatcher(e),this.counter.inc(t)}release(e){return this.counter.dec(e)}async update(){try{const t=(await this.clientLogs.lastLines(this.rInfo,this.lines,0n,this.patternToSearch)).data.toString();this.logs!=t&&this.change.markChanged(),this.logs=t;return}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.logs="",this.error=e,this.done=!0,this.change.markChanged();return}throw e}}}class Wr{constructor(e,t,n,r={cacheSoftSizeBytes:50*1024*1024,withGunzip:!0,nConcurrentDownloads:50}){h(this,"downloadHelper");h(this,"urlToDownload",new Map);h(this,"downloadQueue");h(this,"cache");this.logger=e,this.saveDir=n,this.opts=r,this.downloadQueue=new f.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new Le(this.opts.cacheSoftSizeBytes),this.downloadHelper=new ge(t)}getPath(e,t){if(t===void 0)return g.Computable.make(o=>this.getPath(e,o));const n=b.randomUUID();t.addOnDestroy(()=>this.releasePath(e,n));const r=this.getPathNoCtx(e,t.watcher,n);return(r==null?void 0:r.path)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${r}`),r}getPathNoCtx(e,t,n){const r=e.toString(),o=this.urlToDownload.get(r);if(o!=null)return o.attach(t,n),o.getPath();const c=this.setNewTask(t,e,n);return this.downloadQueue.push({fn:async()=>this.downloadUrl(c,n),recoverableErrorPredicate:a=>!0}),c.getPath()}async downloadUrl(e,t){var n;await e.download(this.downloadHelper,this.opts.withGunzip),((n=e.getPath())==null?void 0:n.path)!=null&&this.cache.addCache(e,t)}async releasePath(e,t){const n=e.toString(),r=this.urlToDownload.get(n);if(r!=null)if(this.cache.existsFile(r.path)){const o=this.cache.removeFile(r.path,t);await Promise.all(o.map(async c=>{await x(c.path),this.cache.removeCache(c),this.removeTask(c,`the task ${JSON.stringify(c)} was removedfrom cache along with ${JSON.stringify(o)}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${JSON.stringify(r)} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.urlToDownload.entries()).map(async([e,t])=>{await x(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${t} was released when the driver was closed`)}))}setNewTask(e,t,n){const r=new Or(this.getFilePath(t),t);return r.attach(e,n),this.urlToDownload.set(t.toString(),r),r}removeTask(e,t){e.abort(t),e.change.markChanged(),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=b.createHash("sha256").update(e.toString()).digest("hex");return w.join(this.saveDir,t)}}class Or{constructor(e,t){h(this,"counter",new f.CallersCounter);h(this,"change",new g.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"sizeBytes",0);this.path=e,this.url=t}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(e,t){try{const n=await this.downloadAndUntar(e,t,this.signalCtl.signal);this.setDone(n)}catch(n){if(n instanceof de||n instanceof Z){this.setError(n),await x(this.path);return}throw n}}async downloadAndUntar(e,t,n){if(await Mr(this.path))return await Ce(this.path);const r=await e.downloadRemoteFile(this.url.toString(),{},n);let o=r.content;if(t){const a=P.Transform.toWeb(Je.createGunzip());o=o.pipeThrough(a,{signal:n})}const c=P.Writable.toWeb(Ze.extract(this.path));return await o.pipeTo(c,{signal:n}),r.size}getPath(){if(this.done)return{path:f.notEmpty(this.path)};if(this.error)return{error:this.error}}setDone(e){this.done=!0,this.sizeBytes=e,this.change.markChanged()}abort(e){this.signalCtl.abort(new de(e))}setError(e){this.error=String(e),this.change.markChanged()}}class de extends Error{}async function Mr(s){try{return await y.access(s),!0}catch{return!1}}async function Ce(s){const e=await y.readdir(s,{withFileTypes:!0});return(await Promise.all(e.map(async n=>{const r=w.join(s,n.name);return n.isDirectory()?await Ce(r):(await y.stat(r)).size}))).reduce((n,r)=>n+r,0)}async function x(s){await y.rm(s,{recursive:!0,force:!0})}function Er(s){var t;return{parent:s.list.items.length>0?(t=s.list.items[0])==null?void 0:t.directory:void 0,entries:s.list.items.map(n=>Hr(n,s))}}function Hr(s,e){return s.isDir?{type:"dir",name:s.name,fullPath:s.fullName}:{type:"file",name:s.name,fullPath:s.fullName,handle:Ar({item:s,...e})}}function Ar(s){return s.remote?zr(s):Fe(s.item.fullName,s.signer,s.item.size,f.notEmpty(s.item.lastModified).seconds)}function zr(s){return`index://index/${encodeURIComponent(JSON.stringify({storageId:s.storageName,path:s.item.fullName}))}`}function Fe(s,e,t,n){const r={localPath:s,pathSignature:e.sign(s),sizeBytes:String(t),modificationTime:String(n)};return`upload://upload/${encodeURIComponent(JSON.stringify(r))}`}function xr(s,e){if(!(e.dirent.isFile()||e.dirent.isDirectory())){s.warn(`tried to get non-dir and non-file ${e.dirent.name}, skip it`);return}return{directory:e.directory,isDir:e.dirent.isDirectory(),name:e.dirent.name,fullName:e.fullName,lastModified:{seconds:BigInt(Math.floor(e.stat.mtimeMs/1e3)),nanos:0},size:BigInt(e.stat.size)}}function jr(s,e){const t=Object.entries(s).map(Vr),n=Object.entries(e).map(Qr);return t.concat(n)}function Gr(s){if(Xr(s))return Kr(s);if(qr(s))return Zr(s);f.assertNever(s)}function Vr([s,e]){return{name:s,handle:Jr(s,e),initialFullPath:e}}const $e=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function qr(s){return $e.test(s)}function Jr(s,e){return`local://${s}/${encodeURIComponent(e)}`}function Zr(s){const e=s.match($e);if(e==null)throw new Error(`Local list handle wasn't parsed: ${s}`);const{name:t,path:n}=e.groups;return{path:decodeURIComponent(n),name:t,remote:!1}}function Qr([s,e]){return{name:s,handle:Yr(s,e),initialFullPath:""}}const We=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function Xr(s){return We.test(s)}function Yr(s,e){return`remote://${s}/${BigInt(e)}`}function Kr(s){const e=s.match(We);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${s}`);const{name:t,resourceId:n}=e.groups;return{id:p.bigintToResourceId(BigInt(n)),type:en(t),name:t,remote:!0}}function en(s){return{name:`LS/${s}`,version:"1"}}class tn{constructor(e,t,n,r,o){h(this,"storageIdToResourceId");this.logger=e,this.clientLs=t,this.client=n,this.signer=r,this.localStorageToPath=o}async getLocalFileHandle(e){const t=await y.stat(e,{bigint:!0});return Fe(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){return jr(this.localStorageToPath,await this.getAvailableStorageIds())}async listFiles(e,t){const n=Gr(e);let r;return n.remote?r=await this.clientLs.list(n,t):r=await this.getLocalFiles(this.logger,n.path,t),Er({storageName:n.name,list:r,signer:this.signer,remote:n.remote})}async getAvailableStorageIds(){return this.storageIdToResourceId==null&&(this.storageIdToResourceId=await rn(this.client)),this.storageIdToResourceId}async getLocalFiles(e,t,n){const r=w.resolve(t),o=w.isAbsolute(n)?n:w.resolve(w.join(r,n)),c=await y.opendir(o),a=[];for await(const l of c){const u=w.join(o,l.name);a.push({directory:o,fullName:u,dirent:l,stat:await y.stat(u)})}return{delimiter:w.sep,items:a.map(l=>xr(e,l)).filter(l=>l!=null).map(l=>l)}}}async function rn(s){return s.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),n=await e.getResourceData(t,!0);return nn(n)})}function nn(s){return Object.fromEntries(s.fields.filter(e=>e.type=="Dynamic"&&p.isNotNullResourceId(e.value)).map(e=>[e.name.substring(8),e.value]))}exports.ClientDownload=fe;exports.ClientLogs=ye;exports.ClientLs=we;exports.ClientProgress=pe;exports.ClientUpload=ue;exports.Download=Se;exports.DownloadDriver=br;exports.DownloadUrlDriver=Wr;exports.LogsDriver=Tr;exports.LogsStreamDriver=$r;exports.LongUpdater=fr;exports.LsDriver=tn;exports.MTimeError=V;exports.NetworkError=he;exports.NoFileForUploading=J;exports.OnDemandBlobResourceSnapshot=Re;exports.PL_STORAGE_TO_PATH=Te;exports.UnexpectedEOF=q;exports.UnknownStorageError=Q;exports.Updater=Y;exports.UploadDriver=Br;exports.UploadResourceSnapshot=De;exports.WrongLocalFileUrl=X;exports.createDownloadClient=cr;exports.createLogsClient=hr;exports.createLsFilesClient=gr;exports.createUploadBlobClient=pr;exports.createUploadProgressClient=ur;exports.dataToHandle=K;exports.getStream=mr;exports.handleToData=S;exports.headersFromProto=me;exports.importToUploadOpts=Be;exports.isLiveLogHandle=N;exports.isReadyLogHandle=Ie;exports.lineToProgress=ke;exports.mixcrProgressFromLogs=yr;
2
2
  //# sourceMappingURL=index.js.map