@milaboratories/pl-drivers 1.3.2 → 1.3.3
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/README.md +5 -3
- package/dist/clients/download.d.ts.map +1 -1
- package/dist/clients/progress.d.ts.map +1 -1
- package/dist/clients/upload.d.ts.map +1 -1
- package/dist/drivers/download_and_logs_blob.d.ts.map +1 -1
- package/dist/drivers/download_url.d.ts.map +1 -1
- package/dist/drivers/helpers/helpers.d.ts.map +1 -1
- package/dist/drivers/helpers/test_helpers.d.ts.map +1 -1
- package/dist/drivers/logs.d.ts.map +1 -1
- package/dist/drivers/logs_stream.d.ts.map +1 -1
- package/dist/drivers/ls.d.ts +1 -11
- package/dist/drivers/ls.d.ts.map +1 -1
- package/dist/drivers/types.d.ts +11 -0
- package/dist/drivers/types.d.ts.map +1 -1
- package/dist/drivers/virtual_storages.d.ts +10 -0
- package/dist/drivers/virtual_storages.d.ts.map +1 -0
- package/dist/helpers/download.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +446 -546
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/clients/download.test.ts +9 -5
- package/src/clients/download.ts +5 -8
- package/src/clients/progress.ts +1 -4
- package/src/clients/upload.test.ts +2 -5
- package/src/clients/upload.ts +10 -44
- package/src/drivers/download_and_logs_blob.ts +32 -109
- package/src/drivers/download_blob.test.ts +37 -55
- package/src/drivers/download_url.test.ts +1 -1
- package/src/drivers/download_url.ts +6 -25
- package/src/drivers/helpers/helpers.ts +3 -12
- package/src/drivers/helpers/test_helpers.ts +1 -3
- package/src/drivers/logs.test.ts +27 -65
- package/src/drivers/logs.ts +17 -66
- package/src/drivers/logs_stream.ts +12 -48
- package/src/drivers/ls.test.ts +7 -6
- package/src/drivers/ls.ts +33 -42
- package/src/drivers/types.ts +15 -0
- package/src/drivers/virtual_storages.ts +44 -0
- package/src/helpers/download.ts +2 -4
- package/src/index.ts +1 -0
package/dist/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
"use strict";var Ae=Object.defineProperty;var je=(i,e,t)=>e in i?Ae(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var h=(i,e,t)=>je(i,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Ge=require("node:fs/promises"),m=require("@milaboratories/pl-client"),f=require("@protobuf-ts/runtime-rpc"),s=require("@protobuf-ts/runtime"),ge=require("undici"),g=require("@milaboratories/ts-helpers"),P=require("node:stream"),Ve=require("node:fs"),fe=require("node:path"),q=require("node:stream/consumers"),p=require("@milaboratories/computable"),J=require("node:timers/promises"),qe=require("node:readline/promises"),Je=require("denque"),Ze=require("node:os"),k=require("node:crypto"),w=require("@milaboratories/pl-tree"),b=require("zod"),Qe=require("node:zlib"),Xe=require("tar-fs"),Ye=require("@milaboratories/pl-model-common");function U(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const y=U(Ge),F=U(Ve),T=U(fe),Ke=U(qe),G=U(Ze),et=U(Qe),tt=U(Xe);class nt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new nt;class rt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new rt;class ot extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const st=new ot;class it extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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===s.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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){if(e.partsCount!==0n&&t.tag(1,s.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,s.WireType.LengthDelimited).fork();for(let o=0;o<e.uploadedParts.length;o++)t.uint64(e.uploadedParts[o]);t.join()}let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const at=new it;class lt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new lt;class dt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).int64(e.bytesProcessed);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ct=new dt;class ht extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ut=new ht;class pt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new pt;class gt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,s.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,s.WireType.Varint).uint64(e.uploadedPartSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ft=new gt;class mt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const C=new mt;class wt extends s.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:()=>C},{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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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(C.internalBinaryRead(e,e.uint32(),r));break;case 4:o.chunkStart=e.uint64().toBigInt();break;case 5:o.chunkEnd=e.uint64().toBigInt();break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.uploadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.method);for(let o=0;o<e.headers.length;o++)C.internalBinaryWrite(e.headers[o],t.tag(3,s.WireType.LengthDelimited).fork(),r).join();e.chunkStart!==0n&&t.tag(4,s.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,s.WireType.Varint).uint64(e.chunkEnd);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const yt=new wt;class Tt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Tt;class bt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const kt=new bt;class Lt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Pt=new Lt,_=new f.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:st,O:at},{name:"GetPartURL",options:{},I:ft,O:yt},{name:"UpdateProgress",options:{},I:ct,O:ut},{name:"Finalize",options:{},I:kt,O:Pt}]);class Ut{constructor(e){h(this,"typeName",_.typeName);h(this,"methods",_.methods);h(this,"options",_.options);this._transport=e}init(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}getPartURL(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}updateProgress(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}finalize(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Z extends Error{}class Q extends Error{}class me extends Error{}class X extends Error{}class we{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=n,this.grpcClient=new Ut(this.grpcTransport)}close(){}async initUpload({id:e,type:t},r){const n=await this.grpcClient.init({resourceId:e},m.addRTypeToMetadata(t,r));return this.partsToUpload(n.response)}async partUpload({id:e,type:t},r,n,o,c,a){const d=await this.grpcClient.getPartURL({resourceId:e,partNumber:n,uploadedPartSize:0n},m.addRTypeToMetadata(t,a)).response,{chunk:l,mTime:u}=await this.readChunk(r,d.chunkStart,d.chunkEnd);if(u>c)throw new Z("file was modified, expected mtime: "+c+", got: "+u+".");const L=await ge.request(d.uploadUrl,this.prepareUploadOpts(d,l)),se=await L.body.text();if(this.logger.info(`uploaded chunk ${n} from ${o} of resource: ${e}, response: '${se.toString()}', status code: ${L.statusCode}`),L.statusCode!=200)throw new me(`response is not ok, status code: ${L.statusCode}, body: ${se}, headers: ${L.headers}, url: ${d.uploadUrl}`);await this.grpcClient.updateProgress({resourceId:e,bytesProcessed:d.chunkEnd-d.chunkStart},m.addRTypeToMetadata(t,a))}async finalizeUpload({id:e,type:t},r){return await this.grpcClient.finalize({resourceId:e},m.addRTypeToMetadata(t,r))}async readChunk(e,t,r){let n;try{n=await y.open(e);const o=Number(r-t),c=Number(t),a=Buffer.alloc(o),d=await this.readBytesFromPosition(n,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 X(`there is no file ${e} for uploading`):o}finally{n==null||n.close()}}async readBytesFromPosition(e,t,r,n){let o=0;for(;o<r;){const{bytesRead:c}=await e.read(t,o,r-o,n+o);if(c===0)throw new Q("file ended earlier than expected.");o+=c}return o}partsToUpload(e){const t=[],r=new Set(e.uploadedParts);for(let n=1n;n<=e.partsCount;n++)r.has(n)||t.push(n);return t}prepareUploadOpts(e,t){const r=e.headers.map(({name:n,value:o})=>[n,o]);return{dispatcher:this.httpClient,body:t,headers:Object.fromEntries(r),method:e.method.toUpperCase()}}}class It extends s.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 r=s.PbLong.from(e.seconds).toNumber();if(r>315576e6||r<-315576e6)throw new Error("Duration value out of range.");let n=e.seconds.toString();if(r===0&&e.nanos<0&&(n="-"+n),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)),n+="."+o}return n+"s"}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+s.typeofJsonValue(e)+". Expected string.");let n=e.match(/^(-?)([0-9]+)(?:\.([0-9]+))?s/);if(n===null)throw new Error("Unable to parse Duration from JSON string. Invalid format.");r||(r=this.create());let[,o,c,a]=n,d=s.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(r.seconds=d.toBigInt(),typeof a=="string"){let l=o+a+"0".repeat(9-a.length);r.nanos=parseInt(l)}return r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const B=new It;class Rt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Rt;class St extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.progress!==0&&t.tag(1,s.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,s.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,s.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,s.WireType.LengthDelimited).string(e.name);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const R=new St;class Dt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Dt;class Nt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const vt=new Nt;class Bt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>R}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=R.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&R.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ft=new Bt;class Ct extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ct;class _t extends s.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:()=>B}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=B.internalBinaryRead(e,e.uint32(),r,o.updateInterval);break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.updateInterval&&B.internalBinaryWrite(e.updateInterval,t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const $t=new _t;class Wt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>R}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=R.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&R.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Mt=new Wt,$=new f.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:vt,O:Ft},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:$t,O:Mt}]);class Ot{constructor(e){h(this,"typeName",$.typeName);h(this,"methods",$.methods);h(this,"options",$.options);this._transport=e}getStatus(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}realtimeStatus(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}}class ye{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.client=r,this.logger=n,this.grpcClient=new Ot(this.grpcTransport)}close(){}async getStatus({id:e,type:t},r){const n=await this.grpcClient.getStatus({resourceId:e},m.addRTypeToMetadata(t,r)),o=g.notEmpty(n.response.report);return{done:o.done,progress:o.progress,bytesProcessed:String(o.bytesProcessed),bytesTotal:String(o.bytesTotal)}}async*realtimeStatus({id:e,type:t},r=100,n){n=m.addRTypeToMetadata(t,n);const o=Math.floor(r/1e3),c=(r-o*1e3)*1e6,a=B.create({seconds:BigInt(o),nanos:c});try{const{responses:d}=this.grpcClient.realtimeStatus({resourceId:e,updateInterval:a},n);yield*d}catch(d){throw this.logger.warn("Failed to get realtime status"+d),d}}}class Ht extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ht;class Et extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Et;class zt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const xt=new zt;class At extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const W=new At;class jt extends s.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:()=>W}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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(W.internalBinaryRead(e,e.uint32(),r));break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.downloadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.downloadUrl);for(let o=0;o<e.headers.length;o++)W.internalBinaryWrite(e.headers[o],t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Gt=new jt,M=new f.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:xt,O:Gt}]);class Vt{constructor(e){h(this,"typeName",M.typeName);h(this,"methods",M.methods);h(this,"options",M.options);this._transport=e}getDownloadURL(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Y extends Error{}class Te{constructor(e){this.httpClient=e}async downloadRemoteFile(e,t,r){const{statusCode:n,body:o,headers:c}=await ge.request(e,{dispatcher:this.httpClient,headers:t,signal:r}),a=P.Readable.toWeb(o);if(n!=200){const d=await q.text(a),l=d.substring(0,Math.min(d.length,1e3));throw 400<=n&&n<500?new Y(`Http error: statusCode: ${n} url: ${e.toString()}, beginning of body: ${l}`):new Error(`Http error: statusCode: ${n} url: ${e.toString()}`)}return{content:a,size:Number(c["content-length"])}}}function I(i){if(!fe.isAbsolute(i))throw new Error(`Path ${i} is not absolute.`);return i}const qt="storage://";class K extends Error{}class ee extends Error{}class be{constructor(e,t,r,n){h(this,"grpcClient");h(this,"downloadHelper");h(this,"localStorageIdsToRoot");h(this,"isLocal",e=>e.startsWith(qt));this.grpcTransport=e,this.httpClient=t,this.logger=r;for(const o of n)o.localPath!==""&&I(o.localPath);this.grpcClient=new Vt(this.grpcTransport),this.downloadHelper=new Te(t),this.localStorageIdsToRoot=new Map(n.map(o=>[o.storageId,o.localPath]))}close(){}async getUrl({id:e,type:t},r,n){const o=r??{};return o.abort=n,await this.grpcClient.getDownloadURL({resourceId:e},m.addRTypeToMetadata(t,o)).response}async downloadBlob(e,t,r){const{downloadUrl:n,headers:o}=await this.getUrl(e,t,r);return this.logger.info(`download from url ${n}`),this.isLocal(n)?await this.readLocalFile(n):await this.downloadHelper.downloadRemoteFile(n,Le(o),r)}async readLocalFile(e){const t=ke(e,this.localStorageIdsToRoot),n=(await y.stat(t)).size;return{content:P.Readable.toWeb(F.createReadStream(t)),size:n}}}function ke(i,e){const t=new URL(i);if(t.pathname=="")throw new ee(`url for local filepath ${i} does not match url scheme`);const r=t.host,n=e.get(r);if(n===void 0)throw new K(`Unknown storage location: ${r}`);const o=decodeURIComponent(t.pathname.slice(1));return n===""?o:T.join(n,o)}function Le(i){return Object.fromEntries(i.map(({name:e,value:t})=>[e,t]))}class Jt extends s.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=s.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(s.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),r=e.getTime();return t.seconds=s.PbLong.from(Math.floor(r/1e3)).toBigInt(),t.nanos=r%1e3*1e6,t}internalJsonWrite(e,t){let r=s.PbLong.from(e.seconds).toNumber()*1e3;if(r<Date.parse("0001-01-01T00:00:00Z")||r>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 n="Z";if(e.nanos>0){let o=(e.nanos+1e9).toString().substring(1);o.substring(3)==="000000"?n="."+o.substring(0,3)+"Z":o.substring(6)==="000"?n="."+o.substring(0,6)+"Z":n="."+o+"Z"}return new Date(r).toISOString().replace(".000Z",n)}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Timestamp from JSON "+s.typeofJsonValue(e)+".");let n=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(!n)throw new Error("Unable to parse Timestamp from JSON. Invalid format.");let o=Date.parse(n[1]+"-"+n[2]+"-"+n[3]+"T"+n[4]+":"+n[5]+":"+n[6]+(n[8]?n[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 r||(r=this.create()),r.seconds=s.PbLong.from(o/1e3).toBigInt(),r.nanos=0,n[7]&&(r.nanos=parseInt("1"+n[7]+"0".repeat(9-n[7].length))-1e9),r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const O=new Jt;class Zt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Zt;class Qt extends s.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:()=>O},{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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=O.internalBinaryRead(e,e.uint32(),r,o.lastModified);break;case 13:o.version=e.string();break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,s.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,s.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,s.WireType.LengthDelimited).string(e.directory),e.lastModified&&O.internalBinaryWrite(e.lastModified,t.tag(12,s.WireType.LengthDelimited).fork(),r).join(),e.version!==""&&t.tag(13,s.WireType.LengthDelimited).string(e.version);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const H=new Qt;class Xt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Xt;class Yt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.location);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Kt=new Yt;class en extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:1,T:()=>H},{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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.items.push(H.internalBinaryRead(e,e.uint32(),r));break;case 2:o.delimiter=e.string();break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){for(let o=0;o<e.items.length;o++)H.internalBinaryWrite(e.items[o],t.tag(1,s.WireType.LengthDelimited).fork(),r).join();e.delimiter!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.delimiter);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const tn=new en,E=new f.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:Kt,O:tn}]);class nn{constructor(e){h(this,"typeName",E.typeName);h(this,"methods",E.methods);h(this,"options",E.options);this._transport=e}list(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Pe{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=new nn(e)}close(){}async list(e,t,r){return await this.grpcClient.list({resourceId:e.id,location:t},m.addRTypeToMetadata(e.type,r)).response}}class rn extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new rn;class on extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const sn=new on;class an extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ln=new an;class dn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const cn=new dn;class hn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const un=new hn;class pn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,s.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,s.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const gn=new pn;class fn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.data.length&&t.tag(1,s.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,s.WireType.Varint).uint64(e.newOffset);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const S=new fn,z=new f.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:sn,O:S},{name:"ReadBinary",options:{},I:ln,O:S},{name:"StreamText",serverStreaming:!0,options:{},I:cn,O:S},{name:"ReadText",options:{},I:un,O:S},{name:"LastLines",options:{},I:gn,O:S}]);class mn{constructor(e){h(this,"typeName",z.typeName);h(this,"methods",z.methods);h(this,"options",z.options);this._transport=e}streamBinary(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}readBinary(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}streamText(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}readText(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}lastLines(e,t){const r=this.methods[4],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Ue{constructor(e,t,r){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new mn(this.grpcTransport)}close(){}async lastLines({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.lastLines({resourceId:e,lineCount:r,offset:n,search:o},m.addRTypeToMetadata(t,c))).response}async readText({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.readText({resourceId:g.notEmpty(e),readLimit:BigInt(r),offset:n,search:o},m.addRTypeToMetadata(t,c))).response}}function wn(i,e,t){return e.getDriver({name:"DownloadBlob",init:(r,n,o)=>new be(n,o,i,t)})}function yn(i,e){return i.getDriver({name:"StreamLogs",init:(t,r,n)=>new Ue(r,n,e)})}function Tn(i,e){return i.getDriver({name:"UploadProgress",init:(t,r,n)=>new ye(r,n,i,e)})}function bn(i,e){return i.getDriver({name:"UploadBlob",init:(t,r,n)=>new we(r,n,i,e)})}function Ie(i,e){return i.getDriver({name:"LsFiles",init:(t,r,n)=>new Pe(r,e)})}class kn{constructor(e,t){h(this,"updater");h(this,"schedule",()=>this.updater.schedule());this.onUpdate=e,this.sleepMs=t,this.updater=new te(async()=>{for(;;){if(await this.onUpdate())return;await J.scheduler.wait(this.sleepMs)}})}}class te{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 Ln(i,e){return i.withReadTx("LogsDriverGetStream",async t=>{const r=await t.getResourceData(e,!0),n=await m.valErr(t,m.getField(r,"stream"));if(n.error!="")throw new Error(`while getting stream: ${n.error}`);if(!m.isNullResourceId(n.valueId))return await t.getResourceData(n.valueId,!1)})}const Re="8C7#F1328%9E089B3D22",Pn=/(?<stage>.*):\s*(?<progress>[\d.]+%)\s.*(?<eta>ETA:.*)/g;function Se(i){const t=i.replace(Re,"").match(Pn);if(t==null||t.length!=4)return;const[r,n,o,c]=t;return{stage:n,progress:o,eta:c}}async function Un(i,e,t){const r=await e.lastLines(i,1,0n,Re,t);if(r.data==null||r.data.length==0)return{found:!1};const n=r.data.toString().split(/\r?\n/)[0];if(n==null)return{found:!1};const o=Se(n);return o===void 0?{found:!1}:{found:!0,...o}}class De{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 r=this.cache.get(e);return r!=null&&r.counter.inc(t),r}removeFile(e,t){return g.mapGet(this.cache,e).counter.dec(t),this.toDelete()}toDelete(){if(this.totalSizeBytes<=this.softSizeBytes)return[];const e=[];let t=0;return g.mapEntries(this.cache).filter(([r,n])=>n.counter.isZero()).forEach(([r,n])=>{if(this.totalSizeBytes-t<=this.softSizeBytes)return;const o=g.mapGet(this.cache,r);t+=o.sizeBytes,e.push(o)}),e}addCache(e,t){const r=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}`);r&&(this.totalSizeBytes+=e.sizeBytes)}removeCache(e){this.cache.delete(e.path),this.totalSizeBytes-=e.sizeBytes}}class In{constructor(e,t){this.logsStreamDriver=e,this.downloadDriver=t}getLastLogs(e,t,r){if(r===void 0)return p.Computable.make(o=>this.getLastLogs(e,t,o));const n=A(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(x(n))return this.downloadDriver.getLastLogs(n,t,r);try{return this.logsStreamDriver.getLastLogs(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${o}`);return}throw o}}getProgressLog(e,t,r){if(r===void 0)return p.Computable.make(o=>this.getProgressLog(e,t,o));const n=A(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(x(n))return this.downloadDriver.getProgressLog(n,t,r);try{return this.logsStreamDriver.getProgressLog(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${o}`);return}throw o}}getLogHandle(e,t){if(t===void 0)return p.Computable.make(n=>this.getLogHandle(e,n));const r=A(t,e);if(r===void 0){t.markUnstable("no stream in stream manager");return}return x(r)?this.downloadDriver.getLogHandle(r,t):this.logsStreamDriver.getLogHandle(r,t)}async lastLines(e,t,r,n){return N(e)?await this.logsStreamDriver.lastLines(e,t,r,n):await this.downloadDriver.lastLines(e,t,r,n)}async readText(e,t,r,n){return N(e)?await this.logsStreamDriver.readText(e,t,r,n):await this.downloadDriver.readText(e,t,r,n)}}function x(i){return!i.type.name.startsWith("StreamWorkdir")}function A(i,e){var t;return(t=i.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}function D(i){let e;if(N(i))e=i.match(Ne);else if(Be(i))e=i.match(ve);else throw new Error(`Log handle is malformed: ${i}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${i}`);const{resourceType:t,resourceVersion:r,resourceId:n}=e.groups;return{id:m.bigintToResourceId(BigInt(n)),type:{name:t,version:r}}}function ne(i,e){return i?`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 Ne=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function N(i){return Ne.test(i)}const ve=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function Be(i){return ve.test(i)}const Fe=w.rsSchema({kv:{"ctl/file/blobInfo":b.z.object({sizeBytes:b.z.coerce.number()})}});class Rn{constructor(e,t,r,n,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=r,this.signer=o,this.cache=new De(c.cacheSoftSizeBytes),this.downloadQueue=new g.TaskProcessor(this.logger,c.nConcurrentDownloads),this.saveDir=T.resolve(n)}getDownloadedBlob(e,t){if(t===void 0)return p.Computable.make(c=>this.getDownloadedBlob(e,c));const r=w.treeEntryToResourceInfo(e,t),n=k.randomUUID();t.addOnDestroy(()=>this.releaseBlob(r.id,n));const o=this.getDownloadedBlobNoCtx(t.watcher,r,n);return o==null&&t.markUnstable("download blob is still undefined"),o}getOnDemandBlob(e,t){if(t===void 0)return p.Computable.make(c=>this.getOnDemandBlob(e,c));const r=w.isPlTreeEntry(e)?w.makeResourceSnapshot(e,Fe,t):e,n=k.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(r.id,n)),this.getOnDemandBlobNoCtx(t.watcher,r,n)}getLocalPath(e){return j(e,this.signer)}async getContent(e){if(vn(e))return await Dn(this.getLocalPath(e));if(!Fn(e))throw new Error("Malformed remote handle");const t=Cn(e,this.signer),{content:r}=await this.clientDownload.downloadBlob(t);return await q.buffer(r)}getDownloadedBlobNoCtx(e,t,r){let n=this.idToDownload.get(t.id);if(n===void 0){const c=this.setNewDownloadTask(e,t,r);this.downloadQueue.push({fn:()=>this.downloadBlob(c,r),recoverableErrorPredicate:a=>!0}),n=c}n.attach(e,r);const o=n.getBlob();if(o!==void 0){if(o.ok)return o.value;throw o.error}}setNewDownloadTask(e,t,r){const n=this.getFilePath(t.id),o=new Ce(this.clientDownload,t,n,Bn(n,this.signer));return this.idToDownload.set(t.id,o),o}async downloadBlob(e,t){var r;await e.download(),(r=e.getBlob())!=null&&r.ok&&this.cache.addCache(e,t)}getOnDemandBlobNoCtx(e,t,r){let n=this.idToOnDemand.get(t.id);return n===void 0&&(n=new Sn(t.kv["ctl/file/blobInfo"].sizeBytes,_n(t,this.signer)),this.idToOnDemand.set(t.id,n)),n.attach(e,r),n.getHandle()}getLastLogs(e,t,r){if(r==null)return p.Computable.make(a=>this.getLastLogs(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return c==null&&r.markUnstable("either a file was not downloaded or logs was not read"),c}getLastLogsNoCtx(e,t,r,n){const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const c=j(o.handle,this.signer);let a=this.idToLastLines.get(t.id);if(a==null){const l=new ie(c,r);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,r){if(r==null)return p.Computable.make(a=>this.getProgressLog(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return c===void 0&&r.markUnstable("either a file was not downloaded or a progress log was not read"),c}getProgressLogNoCtx(e,t,r,n){const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const c=j(o.handle,this.signer);let a=this.idToProgressLog.get(t.id);if(a==null){const l=new ie(c,1,r);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 p.Computable.make(n=>this.getLogHandle(e,n));const r=w.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(r)}getLogHandleNoCtx(e){return ne(!1,e)}async lastLines(e,t,r,n){const o=await this.clientLogs.lastLines(D(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async readText(e,t,r,n){const o=await this.clientLogs.readText(D(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async releaseBlob(e,t){const r=this.idToDownload.get(e);if(r!=null)if(this.cache.existsFile(r.path)){const n=this.cache.removeFile(r.path,t);await Promise.all(n.map(async o=>{await y.rm(o.path),this.cache.removeCache(o),this.removeTask(o,`the task ${o.path} was removedfrom cache along with ${n.map(c=>c.path)}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${r.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 n;(((n=this.idToOnDemand.get(e))==null?void 0:n.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 T.resolve(T.join(this.saveDir,String(BigInt(e))))}}class Sn{constructor(e,t){h(this,"change",new p.ChangeSource);h(this,"counter",new g.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 ie{constructor(e,t,r){h(this,"updater");h(this,"log");h(this,"change",new p.ChangeSource);h(this,"error");this.path=e,this.lines=t,this.patternToSearch=r,this.updater=new te(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 Nn(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 ae(i){try{return await y.access(i),!0}catch{return!1}}async function Dn(i){return await q.buffer(P.Readable.toWeb(F.createReadStream(i)))}function Nn(i,e,t){const r=F.createReadStream(i),n=new P.Writable;return new Promise((o,c)=>{const a=Ke.createInterface(r,n),d=new Je;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(G.EOL)+G.EOL)})})}let Ce=class{constructor(e,t,r,n){h(this,"counter",new g.CallersCounter);h(this,"change",new p.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=r,this.handle=n}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 ae(T.dirname(this.path))||await y.mkdir(T.dirname(this.path),{recursive:!0}),await ae(this.path))await e.cancel("the file already existed");else{const r=P.Writable.toWeb(F.createWriteStream(this.path));await e.pipeTo(r)}this.setDone(t)}catch(e){if(e instanceof le||e instanceof Y||e instanceof K||e instanceof ee||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 le(e))}setError(e){this.error=e,this.change.markChanged()}};class le extends Error{}const _e=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function vn(i){return!!i.match(_e)}function j(i,e){const t=i.match(_e);if(t===null)throw new Error(`Local handle is malformed: ${i}, matches: ${t}`);const{path:r,signature:n}=t.groups;return e.verify(r,n,`Signature verification failed for: ${i}`),r}function Bn(i,e){return`blob+local://download/${i}#${e.sign(i)}`}const $e=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*))#(?<signature>.*)$/;function Fn(i){return!!i.match($e)}function Cn(i,e){const t=i.match($e);if(t===null)throw new Error(`Remote handle is malformed: ${i}, matches: ${t}`);const{content:r,resourceType:n,resourceVersion:o,resourceId:c,signature:a}=t.groups;return e.verify(r,a,`Signature verification failed for ${i}`),{id:m.bigintToResourceId(BigInt(c)),type:{name:n,version:o}}}function _n(i,e){const t=`${i.type.name}/${i.type.version}/${BigInt(i.id)}`;return`blob+remote://download/${t}#${e.sign(t)}`}const v=b.z.object({localPath:b.z.string(),pathSignature:b.z.string(),sizeBytes:b.z.string(),modificationTime:b.z.string()}),re=b.z.object({storageId:b.z.string(),path:b.z.string()}),$n=b.z.union([v,re]),We=w.rsSchema({data:v,fields:{blob:!1}}),Me=w.rsSchema({fields:{incarnation:!1}});function Oe(i,e){const t=w.isPlTreeEntry(i)?e.accessor(i).node():w.isPlTreeEntryAccessor(i)?i.node():i;return t.resourceType.name.startsWith("BlobUpload")?w.makeResourceSnapshot(t,We):w.makeResourceSnapshot(t,Me)}class Wn{constructor(e,t,r,n,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=r,this.clientProgress=n,this.opts=o,this.uploadQueue=new g.TaskProcessor(this.logger,1,{type:"exponentialWithMaxDelayBackoff",initialDelay:20,maxDelay:15e3,backoffMultiplier:1.5,jitter:.5}),this.hooks=new p.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:o.stopPollingDelay},(c,a)=>this.scheduleOnNextState(c,a))}getProgressId(e,t){if(t==null)return p.Computable.make(c=>this.getProgressId(e,c));const r=w.isPlTreeEntry(e)?Oe(e,t):e,n=k.randomUUID();t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(r.id,n));const o=this.getProgressIdNoCtx(t.watcher,r,n);return de(o)||t.markUnstable(`upload/index progress was got, but it's not stable: ${o}`),o}getProgressIdNoCtx(e,t,r){const n="blob"in t.fields?t.fields.blob!==void 0:t.fields.incarnation!==void 0,o=this.idToProgress.get(t.id);if(o!=null)return o.attach(e,r),o.mustGetProgress(n);const c=new Mn(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,c),c.attach(e,r),c.progress.isUpload&&c.progress.isUploadSignMatch&&this.uploadQueue.push({fn:()=>c.uploadBlobTask(),recoverableErrorPredicate:a=>!He(a)}),c.mustGetProgress(n)}async release(e,t){const r=this.idToProgress.get(e);if(r===void 0)return;r.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 g.asyncPool(this.opts.nConcurrentGetProgresses,this.getAllNotDoneProgresses().map(t=>async()=>await t.updateStatus())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(r=>r.reject(t))}if(!this.keepRunning)break;await J.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!de(t.progress)).map(([e,t])=>t)}}class Mn{constructor(e,t,r,n,o,c){h(this,"change",new p.ChangeSource);h(this,"counter",new g.CallersCounter);h(this,"progress");h(this,"uploadData");h(this,"uploadingTerminallyFailed");this.logger=e,this.clientBlob=t,this.clientProgress=r,this.nConcurrentPartsUpload=n,this.res=c;const a=c.type.name.startsWith("BlobUpload");let d;a&&(this.uploadData=v.parse(c.data),d=Hn(o,this.uploadData.localPath,this.uploadData.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),ce(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(),He(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=r=>async()=>{this.counter.isZero()||await this.clientBlob.partUpload(this.res,this.uploadData.localPath,r,e.length,BigInt(this.uploadData.modificationTime))};await g.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=On(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(ce(e)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${e}, ${m.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 de(i){return i.done&&i.status!==void 0&&i.status!==null&&i.status.progress>=1}function On(i){return{progress:i.progress??0,bytesProcessed:Number(i.bytesProcessed),bytesTotal:Number(i.bytesTotal)}}function Hn(i,e,t){try{return i.verify(e,t),!0}catch{return!1}}function He(i){return i instanceof Z||i instanceof Q||i instanceof X}function ce(i){return i.name=="RpcError"&&(i.code=="NOT_FOUND"||i.code=="ABORTED"||i.code=="ALREADY_EXISTS")}class En{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 p.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:t.stopPollingDelay},(r,n)=>this.scheduleOnNextState(r,n))}getLastLogs(e,t,r){if(r==null)return p.Computable.make(a=>this.getLastLogs(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseLastLogs(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return r.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),c}getLastLogsNoCtx(e,t,r,n){let o=this.idToLastLines.get(t.id);if(o==null){const a=new he(this.clientLogs,t,r);this.idToLastLines.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error!=null)throw c.error;return c.log}getProgressLog(e,t,r){if(r==null)return p.Computable.make(a=>this.getProgressLog(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseProgressLog(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return r.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),c}getProgressLogNoCtx(e,t,r,n){let o=this.idToProgressLog.get(t.id);if(o==null){const a=new he(this.clientLogs,t,1,r);this.idToProgressLog.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return p.Computable.make(o=>this.getLogHandle(e,o));const r=w.treeEntryToResourceInfo(e,t),n=this.getLogHandleNoCtx(r);return t.markUnstable(),n}getLogHandleNoCtx(e){return ne(!0,e)}async lastLines(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(D(e),t,BigInt(r??0),n))}async readText(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(D(e),t,BigInt(r??0),n))}async tryWithNotFound(e,t){if(!N(e))throw new Error(`Not live log handle was passed to live log driver, handle: ${e}`);try{const r=await t();return{live:!0,shouldUpdateHandle:!1,data:r.data,size:Number(r.size),newOffset:Number(r.newOffset)}}catch(r){if(r.name=="RpcError"&&r.code=="NOT_FOUND")return{shouldUpdateHandle:!0};throw r}}async releaseLastLogs(e,t){var n;((n=this.idToLastLines.get(e))==null?void 0:n.release(t))&&this.idToLastLines.delete(e)}async releaseProgressLog(e,t){var n;((n=this.idToProgressLog.get(e))==null?void 0:n.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 g.asyncPool(this.opts.nConcurrentGetLogs,this.getAllNotDoneLogs().map(t=>async()=>await t.update())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(r=>r.reject(t))}if(!this.keepRunning)break;await J.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 he{constructor(e,t,r,n){h(this,"logs");h(this,"error");h(this,"done",!1);h(this,"change",new p.ChangeSource);h(this,"counter",new g.CallersCounter);this.clientLogs=e,this.rInfo=t,this.lines=r,this.patternToSearch=n}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 zn{constructor(e,t,r,n={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=r,this.opts=n,this.downloadQueue=new g.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new De(this.opts.cacheSoftSizeBytes),this.downloadHelper=new Te(t)}getPath(e,t){if(t===void 0)return p.Computable.make(o=>this.getPath(e,o));const r=k.randomUUID();t.addOnDestroy(()=>this.releasePath(e,r));const n=this.getPathNoCtx(e,t.watcher,r);return(n==null?void 0:n.path)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${n}`),n}getPathNoCtx(e,t,r){const n=e.toString(),o=this.urlToDownload.get(n);if(o!=null)return o.attach(t,r),o.getPath();const c=this.setNewTask(t,e,r);return this.downloadQueue.push({fn:async()=>this.downloadUrl(c,r),recoverableErrorPredicate:a=>!0}),c.getPath()}async downloadUrl(e,t){var r;await e.download(this.downloadHelper,this.opts.withGunzip),((r=e.getPath())==null?void 0:r.path)!=null&&this.cache.addCache(e,t)}async releasePath(e,t){const r=e.toString(),n=this.urlToDownload.get(r);if(n!=null)if(this.cache.existsFile(n.path)){const o=this.cache.removeFile(n.path,t);await Promise.all(o.map(async c=>{await V(c.path),this.cache.removeCache(c),this.removeTask(c,`the task ${JSON.stringify(c)} was removedfrom cache along with ${JSON.stringify(o)}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${JSON.stringify(n)} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.urlToDownload.entries()).map(async([e,t])=>{await V(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${t} was released when the driver was closed`)}))}setNewTask(e,t,r){const n=new xn(this.getFilePath(t),t);return n.attach(e,r),this.urlToDownload.set(t.toString(),n),n}removeTask(e,t){e.abort(t),e.change.markChanged(),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=k.createHash("sha256").update(e.toString()).digest("hex");return T.join(this.saveDir,t)}}class xn{constructor(e,t){h(this,"counter",new g.CallersCounter);h(this,"change",new p.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 r=await this.downloadAndUntar(e,t,this.signalCtl.signal);this.setDone(r)}catch(r){if(r instanceof ue||r instanceof Y){this.setError(r),await V(this.path);return}throw r}}async downloadAndUntar(e,t,r){if(await g.fileExists(this.path))return await Ee(this.path);const n=await e.downloadRemoteFile(this.url.toString(),{},r);let o=n.content;if(t){const a=P.Transform.toWeb(et.createGunzip());o=o.pipeThrough(a,{signal:r})}const c=P.Writable.toWeb(tt.extract(this.path));return await o.pipeTo(c,{signal:r}),n.size}getPath(){if(this.done)return{path:g.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 ue(e))}setError(e){this.error=String(e),this.change.markChanged()}}class ue extends Error{}async function Ee(i){const e=await y.readdir(i,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=T.join(i,r.name);return r.isDirectory()?await Ee(n):(await y.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function V(i){await y.rm(i,{recursive:!0,force:!0})}function pe(i,e){return`index://index/${encodeURIComponent(JSON.stringify({storageId:i,path:e}))}`}function An(i,e,t,r){const n={localPath:i,pathSignature:e.sign(i),sizeBytes:String(t),modificationTime:String(r)};return`upload://upload/${encodeURIComponent(JSON.stringify(n))}`}function jn(i){const e=new URL(i);return v.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function Gn(i){const e=new URL(i);return re.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function Vn(i){if(Qn(i))return Yn(i);if(qn(i))return Zn(i);g.assertNever(i)}const ze=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function qn(i){return ze.test(i)}function Jn(i,e){return`local://${i}/${encodeURIComponent(e)}`}function Zn(i){const e=i.match(ze);if(e==null)throw new Error(`Local list handle wasn't parsed: ${i}`);const{name:t,path:r}=e.groups;return{rootPath:decodeURIComponent(r),name:t,isRemote:!1}}const xe=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function Qn(i){return xe.test(i)}function Xn(i,e){return`remote://${i}/${BigInt(e)}`}function Yn(i){const e=i.match(xe);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${i}`);const{name:t,resourceId:r}=e.groups;return{id:m.bigintToResourceId(BigInt(r)),type:Kn(t),name:t,isRemote:!0}}function Kn(i){return{name:`LS/${i}`,version:"1"}}function er(){const i=G.homedir();return T.sep==="/"?[{name:"local",root:"/",initialPath:i}]:[{name:"local",root:T.parse(i).root,initialPath:i}]}class oe{constructor(e,t,r,n,o,c,a){this.logger=e,this.lsClient=t,this.storageIdToResourceId=r,this.signer=n,this.virtualStoragesMap=o,this.localProjectionsMap=c,this.openFileDialogCallback=a}async getLocalFileContent(e,t){const r=await this.tryResolveLocalFileHandle(e);if(t)throw new Error("Range request not yet supported.");return await y.readFile(r)}async getLocalFileSize(e){const t=await this.tryResolveLocalFileHandle(e);return(await y.stat(t)).size}async showOpenMultipleFilesDialog(e){const t=await this.openFileDialogCallback(!0,e);return t===void 0?{}:{files:await Promise.all(t.map(r=>this.getLocalFileHandle(r)))}}async showOpenSingleFileDialog(e){const t=await this.openFileDialogCallback(!1,e);return t===void 0?{}:{file:await this.getLocalFileHandle(t[0])}}async tryResolveLocalFileHandle(e){if(Ye.isImportFileHandleIndex(e)){const t=Gn(e),r=this.localProjectionsMap.get(t.storageId);if(!r)throw new Error(`Storage ${t.storageId} is not mounted locally.`);return T.join(r.localPath,t.path)}else{const t=jn(e);this.signer.verify(t.localPath,t.pathSignature,"Failed to validate local file handle signature.");const r=t.localPath,n=await y.stat(r,{bigint:!0});if(String(n.mtimeMs/1000n)!==t.modificationTime)throw new Error("File has changed since the handle was created.");return r}}async getLocalFileHandle(e){I(e);for(const r of this.localProjectionsMap.values())if(e.startsWith(r.localPath)){const n=r.localPath===""?e:T.relative(r.localPath,e);return pe(r.storageId,n)}const t=await y.stat(e,{bigint:!0});return An(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){return[...[...this.virtualStoragesMap.values()].map(e=>({name:e.name,handle:Jn(e.name,e.root),initialFullPath:e.initialPath})),...Object.entries(this.storageIdToResourceId).map(([e,t])=>({name:e,handle:Xn(e,t),initialFullPath:""}))]}async listFiles(e,t){const r=Vn(e);if(r.isRemote)return{entries:(await this.lsClient.list(r,t)).items.map(o=>({type:o.isDir?"dir":"file",name:o.name,fullPath:o.fullName,handle:pe(r.name,o.fullName)}))};{T.sep==="/"&&t===""&&(t="/");const n=r.rootPath===""?I(t):T.join(r.rootPath,t),o=[];for await(const c of await y.opendir(n)){if(!c.isFile()&&!c.isDirectory())continue;const a=T.join(n,c.name);o.push({type:c.isFile()?"file":"dir",name:c.name,fullPath:a,handle:await this.getLocalFileHandle(a)})}return{entries:o}}}static async init(e,t,r,n,o,c){const a=Ie(t,e);for(const u of n)I(u.root);for(const u of o)u.localPath!==""&&I(u.localPath);const d=new Map(n.map(u=>[u.name,u])),l=new Map(o.map(u=>[u.storageId,u]));if(new Set([...d.keys(),...l.keys()]).size!==d.size+l.size)throw new Error("Intersection between local projection storage ids and virtual storages names detected.");return new oe(e,a,await tr(t),r,d,l,c)}}async function tr(i){return i.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),r=await e.getResourceData(t,!0);return nr(r)})}function nr(i){return Object.fromEntries(i.fields.filter(e=>e.type=="Dynamic"&&m.isNotNullResourceId(e.value)).map(e=>[e.name.substring(8),e.value]))}exports.ClientDownload=be;exports.ClientLogs=Ue;exports.ClientLs=Pe;exports.ClientProgress=ye;exports.ClientUpload=we;exports.DefaultVirtualLocalStorages=er;exports.Download=Ce;exports.DownloadDriver=Rn;exports.DownloadUrlDriver=zn;exports.ImportFileHandleData=$n;exports.ImportFileHandleIndexData=re;exports.ImportFileHandleUploadData=v;exports.IndexResourceSnapshot=Me;exports.LogsDriver=In;exports.LogsStreamDriver=En;exports.LongUpdater=kn;exports.LsDriver=oe;exports.MTimeError=Z;exports.NetworkError=me;exports.NoFileForUploading=X;exports.OnDemandBlobResourceSnapshot=Fe;exports.UnexpectedEOF=Q;exports.UnknownStorageError=K;exports.Updater=te;exports.UploadDriver=Wn;exports.UploadResourceSnapshot=We;exports.WrongLocalFileUrl=ee;exports.createDownloadClient=wn;exports.createLogsClient=yn;exports.createLsFilesClient=Ie;exports.createUploadBlobClient=bn;exports.createUploadProgressClient=Tn;exports.dataToHandle=ne;exports.getStream=Ln;exports.handleToData=D;exports.headersFromProto=Le;exports.isLiveLogHandle=N;exports.isReadyLogHandle=Be;exports.lineToProgress=Se;exports.makeBlobImportSnapshot=Oe;exports.mixcrProgressFromLogs=Un;exports.parseLocalFileUrl=ke;exports.validateAbsolute=I;
|
|
1
|
+
"use strict";var Ve=Object.defineProperty;var qe=(i,e,t)=>e in i?Ve(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var h=(i,e,t)=>qe(i,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const Je=require("node:fs/promises"),m=require("@milaboratories/pl-client"),f=require("@protobuf-ts/runtime-rpc"),s=require("@protobuf-ts/runtime"),fe=require("undici"),g=require("@milaboratories/ts-helpers"),P=require("node:stream"),Ze=require("node:fs"),F=require("node:path"),q=require("node:stream/consumers"),p=require("@milaboratories/computable"),J=require("node:timers/promises"),Qe=require("node:readline/promises"),Xe=require("denque"),me=require("node:os"),k=require("node:crypto"),w=require("@milaboratories/pl-tree"),b=require("zod"),Ye=require("node:zlib"),Ke=require("tar-fs"),et=require("@milaboratories/pl-model-common"),tt=require("node:util"),nt=require("node:child_process");function U(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const y=U(Je),C=U(Ze),T=U(F),rt=U(Qe),ie=U(me),ot=U(Ye),st=U(Ke);class it extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new it;class at extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new at;class lt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const dt=new lt;class ct extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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===s.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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){if(e.partsCount!==0n&&t.tag(1,s.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,s.WireType.LengthDelimited).fork();for(let o=0;o<e.uploadedParts.length;o++)t.uint64(e.uploadedParts[o]);t.join()}let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ht=new ct;class ut extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ut;class pt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).int64(e.bytesProcessed);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const gt=new pt;class ft extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const mt=new ft;class wt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new wt;class yt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,s.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,s.WireType.Varint).uint64(e.uploadedPartSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Tt=new yt;class bt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const _=new bt;class kt extends s.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:()=>_},{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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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(_.internalBinaryRead(e,e.uint32(),r));break;case 4:o.chunkStart=e.uint64().toBigInt();break;case 5:o.chunkEnd=e.uint64().toBigInt();break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.uploadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.method);for(let o=0;o<e.headers.length;o++)_.internalBinaryWrite(e.headers[o],t.tag(3,s.WireType.LengthDelimited).fork(),r).join();e.chunkStart!==0n&&t.tag(4,s.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,s.WireType.Varint).uint64(e.chunkEnd);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Lt=new kt;class Pt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Pt;class Ut extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const It=new Ut;class Rt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const St=new Rt,$=new f.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:dt,O:ht},{name:"GetPartURL",options:{},I:Tt,O:Lt},{name:"UpdateProgress",options:{},I:gt,O:mt},{name:"Finalize",options:{},I:It,O:St}]);class Dt{constructor(e){h(this,"typeName",$.typeName);h(this,"methods",$.methods);h(this,"options",$.options);this._transport=e}init(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}getPartURL(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}updateProgress(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}finalize(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Z extends Error{}class Q extends Error{}class we extends Error{}class X extends Error{}class ye{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=n,this.grpcClient=new Dt(this.grpcTransport)}close(){}async initUpload({id:e,type:t},r){const n=await this.grpcClient.init({resourceId:e},m.addRTypeToMetadata(t,r));return this.partsToUpload(n.response)}async partUpload({id:e,type:t},r,n,o,c,a){const d=await this.grpcClient.getPartURL({resourceId:e,partNumber:n,uploadedPartSize:0n},m.addRTypeToMetadata(t,a)).response,{chunk:l,mTime:u}=await this.readChunk(r,d.chunkStart,d.chunkEnd);if(u>c)throw new Z("file was modified, expected mtime: "+c+", got: "+u+".");const L=await fe.request(d.uploadUrl,this.prepareUploadOpts(d,l)),se=await L.body.text();if(this.logger.info(`uploaded chunk ${n} from ${o} of resource: ${e}, response: '${se.toString()}', status code: ${L.statusCode}`),L.statusCode!=200)throw new we(`response is not ok, status code: ${L.statusCode}, body: ${se}, headers: ${L.headers}, url: ${d.uploadUrl}`);await this.grpcClient.updateProgress({resourceId:e,bytesProcessed:d.chunkEnd-d.chunkStart},m.addRTypeToMetadata(t,a))}async finalizeUpload({id:e,type:t},r){return await this.grpcClient.finalize({resourceId:e},m.addRTypeToMetadata(t,r))}async readChunk(e,t,r){let n;try{n=await y.open(e);const o=Number(r-t),c=Number(t),a=Buffer.alloc(o),d=await this.readBytesFromPosition(n,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 X(`there is no file ${e} for uploading`):o}finally{n==null||n.close()}}async readBytesFromPosition(e,t,r,n){let o=0;for(;o<r;){const{bytesRead:c}=await e.read(t,o,r-o,n+o);if(c===0)throw new Q("file ended earlier than expected.");o+=c}return o}partsToUpload(e){const t=[],r=new Set(e.uploadedParts);for(let n=1n;n<=e.partsCount;n++)r.has(n)||t.push(n);return t}prepareUploadOpts(e,t){const r=e.headers.map(({name:n,value:o})=>[n,o]);return{dispatcher:this.httpClient,body:t,headers:Object.fromEntries(r),method:e.method.toUpperCase()}}}class Nt extends s.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 r=s.PbLong.from(e.seconds).toNumber();if(r>315576e6||r<-315576e6)throw new Error("Duration value out of range.");let n=e.seconds.toString();if(r===0&&e.nanos<0&&(n="-"+n),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)),n+="."+o}return n+"s"}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+s.typeofJsonValue(e)+". Expected string.");let n=e.match(/^(-?)([0-9]+)(?:\.([0-9]+))?s/);if(n===null)throw new Error("Unable to parse Duration from JSON string. Invalid format.");r||(r=this.create());let[,o,c,a]=n,d=s.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(r.seconds=d.toBigInt(),typeof a=="string"){let l=o+a+"0".repeat(9-a.length);r.nanos=parseInt(l)}return r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const B=new Nt;class vt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new vt;class Bt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.progress!==0&&t.tag(1,s.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,s.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,s.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,s.WireType.LengthDelimited).string(e.name);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const R=new Bt;class Ft extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ft;class Ct extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const _t=new Ct;class $t extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>R}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=R.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&R.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Wt=new $t;class Mt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Mt;class Ot extends s.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:()=>B}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=B.internalBinaryRead(e,e.uint32(),r,o.updateInterval);break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.updateInterval&&B.internalBinaryWrite(e.updateInterval,t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ht=new Ot;class Et extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>R}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=R.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&R.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const zt=new Et,W=new f.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:_t,O:Wt},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:Ht,O:zt}]);class At{constructor(e){h(this,"typeName",W.typeName);h(this,"methods",W.methods);h(this,"options",W.options);this._transport=e}getStatus(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}realtimeStatus(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}}class Te{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.client=r,this.logger=n,this.grpcClient=new At(this.grpcTransport)}close(){}async getStatus({id:e,type:t},r){const n=await this.grpcClient.getStatus({resourceId:e},m.addRTypeToMetadata(t,r)),o=g.notEmpty(n.response.report);return{done:o.done,progress:o.progress,bytesProcessed:String(o.bytesProcessed),bytesTotal:String(o.bytesTotal)}}async*realtimeStatus({id:e,type:t},r=100,n){n=m.addRTypeToMetadata(t,n);const o=Math.floor(r/1e3),c=(r-o*1e3)*1e6,a=B.create({seconds:BigInt(o),nanos:c});try{const{responses:d}=this.grpcClient.realtimeStatus({resourceId:e,updateInterval:a},n);yield*d}catch(d){throw this.logger.warn("Failed to get realtime status"+d),d}}}class xt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new xt;class jt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new jt;class Gt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Vt=new Gt;class qt extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const M=new qt;class Jt extends s.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:()=>M}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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(M.internalBinaryRead(e,e.uint32(),r));break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.downloadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.downloadUrl);for(let o=0;o<e.headers.length;o++)M.internalBinaryWrite(e.headers[o],t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Zt=new Jt,O=new f.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:Vt,O:Zt}]);class Qt{constructor(e){h(this,"typeName",O.typeName);h(this,"methods",O.methods);h(this,"options",O.options);this._transport=e}getDownloadURL(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Y extends Error{}class be{constructor(e){this.httpClient=e}async downloadRemoteFile(e,t,r){const{statusCode:n,body:o,headers:c}=await fe.request(e,{dispatcher:this.httpClient,headers:t,signal:r}),a=P.Readable.toWeb(o);if(n!=200){const d=await q.text(a),l=d.substring(0,Math.min(d.length,1e3));throw 400<=n&&n<500?new Y(`Http error: statusCode: ${n} url: ${e.toString()}, beginning of body: ${l}`):new Error(`Http error: statusCode: ${n} url: ${e.toString()}`)}return{content:a,size:Number(c["content-length"])}}}function I(i){if(!F.isAbsolute(i))throw new Error(`Path ${i} is not absolute.`);return i}const Xt="storage://";class K extends Error{}class ee extends Error{}class ke{constructor(e,t,r,n){h(this,"grpcClient");h(this,"downloadHelper");h(this,"localStorageIdsToRoot");h(this,"isLocal",e=>e.startsWith(Xt));this.grpcTransport=e,this.httpClient=t,this.logger=r;for(const o of n)o.localPath!==""&&I(o.localPath);this.grpcClient=new Qt(this.grpcTransport),this.downloadHelper=new be(t),this.localStorageIdsToRoot=new Map(n.map(o=>[o.storageId,o.localPath]))}close(){}async getUrl({id:e,type:t},r,n){const o=r??{};return o.abort=n,await this.grpcClient.getDownloadURL({resourceId:e},m.addRTypeToMetadata(t,o)).response}async downloadBlob(e,t,r){const{downloadUrl:n,headers:o}=await this.getUrl(e,t,r);return this.logger.info(`download from url ${n}`),this.isLocal(n)?await this.readLocalFile(n):await this.downloadHelper.downloadRemoteFile(n,Pe(o),r)}async readLocalFile(e){const t=Le(e,this.localStorageIdsToRoot),n=(await y.stat(t)).size;return{content:P.Readable.toWeb(C.createReadStream(t)),size:n}}}function Le(i,e){const t=new URL(i);if(t.pathname=="")throw new ee(`url for local filepath ${i} does not match url scheme`);const r=t.host,n=e.get(r);if(n===void 0)throw new K(`Unknown storage location: ${r}`);const o=decodeURIComponent(t.pathname.slice(1));return n===""?o:T.join(n,o)}function Pe(i){return Object.fromEntries(i.map(({name:e,value:t})=>[e,t]))}class Yt extends s.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=s.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(s.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),r=e.getTime();return t.seconds=s.PbLong.from(Math.floor(r/1e3)).toBigInt(),t.nanos=r%1e3*1e6,t}internalJsonWrite(e,t){let r=s.PbLong.from(e.seconds).toNumber()*1e3;if(r<Date.parse("0001-01-01T00:00:00Z")||r>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 n="Z";if(e.nanos>0){let o=(e.nanos+1e9).toString().substring(1);o.substring(3)==="000000"?n="."+o.substring(0,3)+"Z":o.substring(6)==="000"?n="."+o.substring(0,6)+"Z":n="."+o+"Z"}return new Date(r).toISOString().replace(".000Z",n)}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Timestamp from JSON "+s.typeofJsonValue(e)+".");let n=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(!n)throw new Error("Unable to parse Timestamp from JSON. Invalid format.");let o=Date.parse(n[1]+"-"+n[2]+"-"+n[3]+"T"+n[4]+":"+n[5]+":"+n[6]+(n[8]?n[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 r||(r=this.create()),r.seconds=s.PbLong.from(o/1e3).toBigInt(),r.nanos=0,n[7]&&(r.nanos=parseInt("1"+n[7]+"0".repeat(9-n[7].length))-1e9),r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const H=new Yt;class Kt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Kt;class en extends s.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:()=>H},{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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=H.internalBinaryRead(e,e.uint32(),r,o.lastModified);break;case 13:o.version=e.string();break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,s.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,s.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,s.WireType.LengthDelimited).string(e.directory),e.lastModified&&H.internalBinaryWrite(e.lastModified,t.tag(12,s.WireType.LengthDelimited).fork(),r).join(),e.version!==""&&t.tag(13,s.WireType.LengthDelimited).string(e.version);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const E=new en;class tn extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new tn;class nn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.location);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const rn=new nn;class on extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:1,T:()=>E},{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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.items.push(E.internalBinaryRead(e,e.uint32(),r));break;case 2:o.delimiter=e.string();break;default:let l=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){for(let o=0;o<e.items.length;o++)E.internalBinaryWrite(e.items[o],t.tag(1,s.WireType.LengthDelimited).fork(),r).join();e.delimiter!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.delimiter);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const sn=new on,z=new f.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:rn,O:sn}]);class an{constructor(e){h(this,"typeName",z.typeName);h(this,"methods",z.methods);h(this,"options",z.options);this._transport=e}list(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Ue{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=new an(e)}close(){}async list(e,t,r){return await this.grpcClient.list({resourceId:e.id,location:t},m.addRTypeToMetadata(e.type,r)).response}}class ln extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ln;class dn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const cn=new dn;class hn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const un=new hn;class pn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const gn=new pn;class fn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const mn=new fn;class wn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,s.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,s.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const yn=new wn;class Tn extends s.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&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??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=r.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?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.data.length&&t.tag(1,s.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,s.WireType.Varint).uint64(e.newOffset);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const S=new Tn,A=new f.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:cn,O:S},{name:"ReadBinary",options:{},I:un,O:S},{name:"StreamText",serverStreaming:!0,options:{},I:gn,O:S},{name:"ReadText",options:{},I:mn,O:S},{name:"LastLines",options:{},I:yn,O:S}]);class bn{constructor(e){h(this,"typeName",A.typeName);h(this,"methods",A.methods);h(this,"options",A.options);this._transport=e}streamBinary(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}readBinary(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}streamText(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}readText(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}lastLines(e,t){const r=this.methods[4],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Ie{constructor(e,t,r){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new bn(this.grpcTransport)}close(){}async lastLines({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.lastLines({resourceId:e,lineCount:r,offset:n,search:o},m.addRTypeToMetadata(t,c))).response}async readText({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.readText({resourceId:g.notEmpty(e),readLimit:BigInt(r),offset:n,search:o},m.addRTypeToMetadata(t,c))).response}}function kn(i,e,t){return e.getDriver({name:"DownloadBlob",init:(r,n,o)=>new ke(n,o,i,t)})}function Ln(i,e){return i.getDriver({name:"StreamLogs",init:(t,r,n)=>new Ie(r,n,e)})}function Pn(i,e){return i.getDriver({name:"UploadProgress",init:(t,r,n)=>new Te(r,n,i,e)})}function Un(i,e){return i.getDriver({name:"UploadBlob",init:(t,r,n)=>new ye(r,n,i,e)})}function Re(i,e){return i.getDriver({name:"LsFiles",init:(t,r,n)=>new Ue(r,e)})}class In{constructor(e,t){h(this,"updater");h(this,"schedule",()=>this.updater.schedule());this.onUpdate=e,this.sleepMs=t,this.updater=new te(async()=>{for(;;){if(await this.onUpdate())return;await J.scheduler.wait(this.sleepMs)}})}}class te{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 Rn(i,e){return i.withReadTx("LogsDriverGetStream",async t=>{const r=await t.getResourceData(e,!0),n=await m.valErr(t,m.getField(r,"stream"));if(n.error!="")throw new Error(`while getting stream: ${n.error}`);if(!m.isNullResourceId(n.valueId))return await t.getResourceData(n.valueId,!1)})}const Se="8C7#F1328%9E089B3D22",Sn=/(?<stage>.*):\s*(?<progress>[\d.]+%)\s.*(?<eta>ETA:.*)/g;function De(i){const t=i.replace(Se,"").match(Sn);if(t==null||t.length!=4)return;const[r,n,o,c]=t;return{stage:n,progress:o,eta:c}}async function Dn(i,e,t){const r=await e.lastLines(i,1,0n,Se,t);if(r.data==null||r.data.length==0)return{found:!1};const n=r.data.toString().split(/\r?\n/)[0];if(n==null)return{found:!1};const o=De(n);return o===void 0?{found:!1}:{found:!0,...o}}class Ne{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 r=this.cache.get(e);return r!=null&&r.counter.inc(t),r}removeFile(e,t){return g.mapGet(this.cache,e).counter.dec(t),this.toDelete()}toDelete(){if(this.totalSizeBytes<=this.softSizeBytes)return[];const e=[];let t=0;return g.mapEntries(this.cache).filter(([r,n])=>n.counter.isZero()).forEach(([r,n])=>{if(this.totalSizeBytes-t<=this.softSizeBytes)return;const o=g.mapGet(this.cache,r);t+=o.sizeBytes,e.push(o)}),e}addCache(e,t){const r=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}`);r&&(this.totalSizeBytes+=e.sizeBytes)}removeCache(e){this.cache.delete(e.path),this.totalSizeBytes-=e.sizeBytes}}class Nn{constructor(e,t){this.logsStreamDriver=e,this.downloadDriver=t}getLastLogs(e,t,r){if(r===void 0)return p.Computable.make(o=>this.getLastLogs(e,t,o));const n=j(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(x(n))return this.downloadDriver.getLastLogs(n,t,r);try{return this.logsStreamDriver.getLastLogs(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${o}`);return}throw o}}getProgressLog(e,t,r){if(r===void 0)return p.Computable.make(o=>this.getProgressLog(e,t,o));const n=j(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(x(n))return this.downloadDriver.getProgressLog(n,t,r);try{return this.logsStreamDriver.getProgressLog(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${o}`);return}throw o}}getLogHandle(e,t){if(t===void 0)return p.Computable.make(n=>this.getLogHandle(e,n));const r=j(t,e);if(r===void 0){t.markUnstable("no stream in stream manager");return}return x(r)?this.downloadDriver.getLogHandle(r,t):this.logsStreamDriver.getLogHandle(r,t)}async lastLines(e,t,r,n){return N(e)?await this.logsStreamDriver.lastLines(e,t,r,n):await this.downloadDriver.lastLines(e,t,r,n)}async readText(e,t,r,n){return N(e)?await this.logsStreamDriver.readText(e,t,r,n):await this.downloadDriver.readText(e,t,r,n)}}function x(i){return!i.type.name.startsWith("StreamWorkdir")}function j(i,e){var t;return(t=i.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}function D(i){let e;if(N(i))e=i.match(ve);else if(Fe(i))e=i.match(Be);else throw new Error(`Log handle is malformed: ${i}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${i}`);const{resourceType:t,resourceVersion:r,resourceId:n}=e.groups;return{id:m.bigintToResourceId(BigInt(n)),type:{name:t,version:r}}}function ne(i,e){return i?`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 ve=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function N(i){return ve.test(i)}const Be=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function Fe(i){return Be.test(i)}const Ce=w.rsSchema({kv:{"ctl/file/blobInfo":b.z.object({sizeBytes:b.z.coerce.number()})}});class vn{constructor(e,t,r,n,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=r,this.signer=o,this.cache=new Ne(c.cacheSoftSizeBytes),this.downloadQueue=new g.TaskProcessor(this.logger,c.nConcurrentDownloads),this.saveDir=T.resolve(n)}getDownloadedBlob(e,t){if(t===void 0)return p.Computable.make(c=>this.getDownloadedBlob(e,c));const r=w.treeEntryToResourceInfo(e,t),n=k.randomUUID();t.addOnDestroy(()=>this.releaseBlob(r.id,n));const o=this.getDownloadedBlobNoCtx(t.watcher,r,n);return o==null&&t.markUnstable("download blob is still undefined"),o}getOnDemandBlob(e,t){if(t===void 0)return p.Computable.make(c=>this.getOnDemandBlob(e,c));const r=w.isPlTreeEntry(e)?w.makeResourceSnapshot(e,Ce,t):e,n=k.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(r.id,n)),this.getOnDemandBlobNoCtx(t.watcher,r,n)}getLocalPath(e){return G(e,this.signer)}async getContent(e){if(_n(e))return await Fn(this.getLocalPath(e));if(!Wn(e))throw new Error("Malformed remote handle");const t=Mn(e,this.signer),{content:r}=await this.clientDownload.downloadBlob(t);return await q.buffer(r)}getDownloadedBlobNoCtx(e,t,r){let n=this.idToDownload.get(t.id);if(n===void 0){const c=this.setNewDownloadTask(e,t,r);this.downloadQueue.push({fn:()=>this.downloadBlob(c,r),recoverableErrorPredicate:a=>!0}),n=c}n.attach(e,r);const o=n.getBlob();if(o!==void 0){if(o.ok)return o.value;throw o.error}}setNewDownloadTask(e,t,r){const n=this.getFilePath(t.id),o=new _e(this.clientDownload,t,n,$n(n,this.signer));return this.idToDownload.set(t.id,o),o}async downloadBlob(e,t){var r;await e.download(),(r=e.getBlob())!=null&&r.ok&&this.cache.addCache(e,t)}getOnDemandBlobNoCtx(e,t,r){let n=this.idToOnDemand.get(t.id);return n===void 0&&(n=new Bn(t.kv["ctl/file/blobInfo"].sizeBytes,On(t,this.signer)),this.idToOnDemand.set(t.id,n)),n.attach(e,r),n.getHandle()}getLastLogs(e,t,r){if(r==null)return p.Computable.make(a=>this.getLastLogs(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return c==null&&r.markUnstable("either a file was not downloaded or logs was not read"),c}getLastLogsNoCtx(e,t,r,n){const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const c=G(o.handle,this.signer);let a=this.idToLastLines.get(t.id);if(a==null){const l=new ae(c,r);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,r){if(r==null)return p.Computable.make(a=>this.getProgressLog(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return c===void 0&&r.markUnstable("either a file was not downloaded or a progress log was not read"),c}getProgressLogNoCtx(e,t,r,n){const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const c=G(o.handle,this.signer);let a=this.idToProgressLog.get(t.id);if(a==null){const l=new ae(c,1,r);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 p.Computable.make(n=>this.getLogHandle(e,n));const r=w.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(r)}getLogHandleNoCtx(e){return ne(!1,e)}async lastLines(e,t,r,n){const o=await this.clientLogs.lastLines(D(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async readText(e,t,r,n){const o=await this.clientLogs.readText(D(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async releaseBlob(e,t){const r=this.idToDownload.get(e);if(r!=null)if(this.cache.existsFile(r.path)){const n=this.cache.removeFile(r.path,t);await Promise.all(n.map(async o=>{await y.rm(o.path),this.cache.removeCache(o),this.removeTask(o,`the task ${o.path} was removedfrom cache along with ${n.map(c=>c.path)}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${r.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 n;(((n=this.idToOnDemand.get(e))==null?void 0:n.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 T.resolve(T.join(this.saveDir,String(BigInt(e))))}}class Bn{constructor(e,t){h(this,"change",new p.ChangeSource);h(this,"counter",new g.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 ae{constructor(e,t,r){h(this,"updater");h(this,"log");h(this,"change",new p.ChangeSource);h(this,"error");this.path=e,this.lines=t,this.patternToSearch=r,this.updater=new te(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 Cn(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 le(i){try{return await y.access(i),!0}catch{return!1}}async function Fn(i){return await q.buffer(P.Readable.toWeb(C.createReadStream(i)))}function Cn(i,e,t){const r=C.createReadStream(i),n=new P.Writable;return new Promise((o,c)=>{const a=rt.createInterface(r,n),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(ie.EOL)+ie.EOL)})})}let _e=class{constructor(e,t,r,n){h(this,"counter",new g.CallersCounter);h(this,"change",new p.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=r,this.handle=n}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 le(T.dirname(this.path))||await y.mkdir(T.dirname(this.path),{recursive:!0}),await le(this.path))await e.cancel("the file already existed");else{const r=P.Writable.toWeb(C.createWriteStream(this.path));await e.pipeTo(r)}this.setDone(t)}catch(e){if(e instanceof de||e instanceof Y||e instanceof K||e instanceof ee||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 de(e))}setError(e){this.error=e,this.change.markChanged()}};class de extends Error{}const $e=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function _n(i){return!!i.match($e)}function G(i,e){const t=i.match($e);if(t===null)throw new Error(`Local handle is malformed: ${i}, matches: ${t}`);const{path:r,signature:n}=t.groups;return e.verify(r,n,`Signature verification failed for: ${i}`),r}function $n(i,e){return`blob+local://download/${i}#${e.sign(i)}`}const We=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*))#(?<signature>.*)$/;function Wn(i){return!!i.match(We)}function Mn(i,e){const t=i.match(We);if(t===null)throw new Error(`Remote handle is malformed: ${i}, matches: ${t}`);const{content:r,resourceType:n,resourceVersion:o,resourceId:c,signature:a}=t.groups;return e.verify(r,a,`Signature verification failed for ${i}`),{id:m.bigintToResourceId(BigInt(c)),type:{name:n,version:o}}}function On(i,e){const t=`${i.type.name}/${i.type.version}/${BigInt(i.id)}`;return`blob+remote://download/${t}#${e.sign(t)}`}const v=b.z.object({localPath:b.z.string(),pathSignature:b.z.string(),sizeBytes:b.z.string(),modificationTime:b.z.string()}),re=b.z.object({storageId:b.z.string(),path:b.z.string()}),Hn=b.z.union([v,re]),Me=w.rsSchema({data:v,fields:{blob:!1}}),Oe=w.rsSchema({fields:{incarnation:!1}});function He(i,e){const t=w.isPlTreeEntry(i)?e.accessor(i).node():w.isPlTreeEntryAccessor(i)?i.node():i;return t.resourceType.name.startsWith("BlobUpload")?w.makeResourceSnapshot(t,Me):w.makeResourceSnapshot(t,Oe)}class En{constructor(e,t,r,n,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=r,this.clientProgress=n,this.opts=o,this.uploadQueue=new g.TaskProcessor(this.logger,1,{type:"exponentialWithMaxDelayBackoff",initialDelay:20,maxDelay:15e3,backoffMultiplier:1.5,jitter:.5}),this.hooks=new p.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:o.stopPollingDelay},(c,a)=>this.scheduleOnNextState(c,a))}getProgressId(e,t){if(t==null)return p.Computable.make(c=>this.getProgressId(e,c));const r=w.isPlTreeEntry(e)?He(e,t):e,n=k.randomUUID();t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(r.id,n));const o=this.getProgressIdNoCtx(t.watcher,r,n);return ce(o)||t.markUnstable(`upload/index progress was got, but it's not stable: ${o}`),o}getProgressIdNoCtx(e,t,r){const n="blob"in t.fields?t.fields.blob!==void 0:t.fields.incarnation!==void 0,o=this.idToProgress.get(t.id);if(o!=null)return o.attach(e,r),o.mustGetProgress(n);const c=new zn(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,c),c.attach(e,r),c.progress.isUpload&&c.progress.isUploadSignMatch&&this.uploadQueue.push({fn:()=>c.uploadBlobTask(),recoverableErrorPredicate:a=>!Ee(a)}),c.mustGetProgress(n)}async release(e,t){const r=this.idToProgress.get(e);if(r===void 0)return;r.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 g.asyncPool(this.opts.nConcurrentGetProgresses,this.getAllNotDoneProgresses().map(t=>async()=>await t.updateStatus())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(r=>r.reject(t))}if(!this.keepRunning)break;await J.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!ce(t.progress)).map(([e,t])=>t)}}class zn{constructor(e,t,r,n,o,c){h(this,"change",new p.ChangeSource);h(this,"counter",new g.CallersCounter);h(this,"progress");h(this,"uploadData");h(this,"uploadingTerminallyFailed");this.logger=e,this.clientBlob=t,this.clientProgress=r,this.nConcurrentPartsUpload=n,this.res=c;const a=c.type.name.startsWith("BlobUpload");let d;a&&(this.uploadData=v.parse(c.data),d=xn(o,this.uploadData.localPath,this.uploadData.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),he(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(),Ee(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=r=>async()=>{this.counter.isZero()||await this.clientBlob.partUpload(this.res,this.uploadData.localPath,r,e.length,BigInt(this.uploadData.modificationTime))};await g.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=An(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(he(e)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${e}, ${m.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 ce(i){return i.done&&i.status!==void 0&&i.status!==null&&i.status.progress>=1}function An(i){return{progress:i.progress??0,bytesProcessed:Number(i.bytesProcessed),bytesTotal:Number(i.bytesTotal)}}function xn(i,e,t){try{return i.verify(e,t),!0}catch{return!1}}function Ee(i){return i instanceof Z||i instanceof Q||i instanceof X}function he(i){return i.name=="RpcError"&&(i.code=="NOT_FOUND"||i.code=="ABORTED"||i.code=="ALREADY_EXISTS")}class jn{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 p.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:t.stopPollingDelay},(r,n)=>this.scheduleOnNextState(r,n))}getLastLogs(e,t,r){if(r==null)return p.Computable.make(a=>this.getLastLogs(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseLastLogs(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return r.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),c}getLastLogsNoCtx(e,t,r,n){let o=this.idToLastLines.get(t.id);if(o==null){const a=new ue(this.clientLogs,t,r);this.idToLastLines.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error!=null)throw c.error;return c.log}getProgressLog(e,t,r){if(r==null)return p.Computable.make(a=>this.getProgressLog(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseProgressLog(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return r.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),c}getProgressLogNoCtx(e,t,r,n){let o=this.idToProgressLog.get(t.id);if(o==null){const a=new ue(this.clientLogs,t,1,r);this.idToProgressLog.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return p.Computable.make(o=>this.getLogHandle(e,o));const r=w.treeEntryToResourceInfo(e,t),n=this.getLogHandleNoCtx(r);return t.markUnstable(),n}getLogHandleNoCtx(e){return ne(!0,e)}async lastLines(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(D(e),t,BigInt(r??0),n))}async readText(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(D(e),t,BigInt(r??0),n))}async tryWithNotFound(e,t){if(!N(e))throw new Error(`Not live log handle was passed to live log driver, handle: ${e}`);try{const r=await t();return{live:!0,shouldUpdateHandle:!1,data:r.data,size:Number(r.size),newOffset:Number(r.newOffset)}}catch(r){if(r.name=="RpcError"&&r.code=="NOT_FOUND")return{shouldUpdateHandle:!0};throw r}}async releaseLastLogs(e,t){var n;((n=this.idToLastLines.get(e))==null?void 0:n.release(t))&&this.idToLastLines.delete(e)}async releaseProgressLog(e,t){var n;((n=this.idToProgressLog.get(e))==null?void 0:n.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 g.asyncPool(this.opts.nConcurrentGetLogs,this.getAllNotDoneLogs().map(t=>async()=>await t.update())),e.forEach(t=>t.resolve())}catch(t){console.error(t),e.forEach(r=>r.reject(t))}if(!this.keepRunning)break;await J.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 ue{constructor(e,t,r,n){h(this,"logs");h(this,"error");h(this,"done",!1);h(this,"change",new p.ChangeSource);h(this,"counter",new g.CallersCounter);this.clientLogs=e,this.rInfo=t,this.lines=r,this.patternToSearch=n}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 Gn{constructor(e,t,r,n={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=r,this.opts=n,this.downloadQueue=new g.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new Ne(this.opts.cacheSoftSizeBytes),this.downloadHelper=new be(t)}getPath(e,t){if(t===void 0)return p.Computable.make(o=>this.getPath(e,o));const r=k.randomUUID();t.addOnDestroy(()=>this.releasePath(e,r));const n=this.getPathNoCtx(e,t.watcher,r);return(n==null?void 0:n.path)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${n}`),n}getPathNoCtx(e,t,r){const n=e.toString(),o=this.urlToDownload.get(n);if(o!=null)return o.attach(t,r),o.getPath();const c=this.setNewTask(t,e,r);return this.downloadQueue.push({fn:async()=>this.downloadUrl(c,r),recoverableErrorPredicate:a=>!0}),c.getPath()}async downloadUrl(e,t){var r;await e.download(this.downloadHelper,this.opts.withGunzip),((r=e.getPath())==null?void 0:r.path)!=null&&this.cache.addCache(e,t)}async releasePath(e,t){const r=e.toString(),n=this.urlToDownload.get(r);if(n!=null)if(this.cache.existsFile(n.path)){const o=this.cache.removeFile(n.path,t);await Promise.all(o.map(async c=>{await V(c.path),this.cache.removeCache(c),this.removeTask(c,`the task ${JSON.stringify(c)} was removedfrom cache along with ${JSON.stringify(o)}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${JSON.stringify(n)} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.urlToDownload.entries()).map(async([e,t])=>{await V(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${t} was released when the driver was closed`)}))}setNewTask(e,t,r){const n=new Vn(this.getFilePath(t),t);return n.attach(e,r),this.urlToDownload.set(t.toString(),n),n}removeTask(e,t){e.abort(t),e.change.markChanged(),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=k.createHash("sha256").update(e.toString()).digest("hex");return T.join(this.saveDir,t)}}class Vn{constructor(e,t){h(this,"counter",new g.CallersCounter);h(this,"change",new p.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 r=await this.downloadAndUntar(e,t,this.signalCtl.signal);this.setDone(r)}catch(r){if(r instanceof pe||r instanceof Y){this.setError(r),await V(this.path);return}throw r}}async downloadAndUntar(e,t,r){if(await g.fileExists(this.path))return await ze(this.path);const n=await e.downloadRemoteFile(this.url.toString(),{},r);let o=n.content;if(t){const a=P.Transform.toWeb(ot.createGunzip());o=o.pipeThrough(a,{signal:r})}const c=P.Writable.toWeb(st.extract(this.path));return await o.pipeTo(c,{signal:r}),n.size}getPath(){if(this.done)return{path:g.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 pe(e))}setError(e){this.error=String(e),this.change.markChanged()}}class pe extends Error{}async function ze(i){const e=await y.readdir(i,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=T.join(i,r.name);return r.isDirectory()?await ze(n):(await y.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function V(i){await y.rm(i,{recursive:!0,force:!0})}function ge(i,e){return`index://index/${encodeURIComponent(JSON.stringify({storageId:i,path:e}))}`}function qn(i,e,t,r){const n={localPath:i,pathSignature:e.sign(i),sizeBytes:String(t),modificationTime:String(r)};return`upload://upload/${encodeURIComponent(JSON.stringify(n))}`}function Jn(i){const e=new URL(i);return v.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function Zn(i){const e=new URL(i);return re.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function Qn(i){if(er(i))return nr(i);if(Xn(i))return Kn(i);g.assertNever(i)}const Ae=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function Xn(i){return Ae.test(i)}function Yn(i,e){return`local://${i}/${encodeURIComponent(e)}`}function Kn(i){const e=i.match(Ae);if(e==null)throw new Error(`Local list handle wasn't parsed: ${i}`);const{name:t,path:r}=e.groups;return{rootPath:decodeURIComponent(r),name:t,isRemote:!1}}const xe=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function er(i){return xe.test(i)}function tr(i,e){return`remote://${i}/${BigInt(e)}`}function nr(i){const e=i.match(xe);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${i}`);const{name:t,resourceId:r}=e.groups;return{id:m.bigintToResourceId(BigInt(r)),type:rr(t),name:t,isRemote:!0}}function rr(i){return{name:`LS/${i}`,version:"1"}}class oe{constructor(e,t,r,n,o,c,a){this.logger=e,this.lsClient=t,this.storageIdToResourceId=r,this.signer=n,this.virtualStoragesMap=o,this.localProjectionsMap=c,this.openFileDialogCallback=a}async getLocalFileContent(e,t){const r=await this.tryResolveLocalFileHandle(e);if(t)throw new Error("Range request not yet supported.");return await y.readFile(r)}async getLocalFileSize(e){const t=await this.tryResolveLocalFileHandle(e);return(await y.stat(t)).size}async showOpenMultipleFilesDialog(e){const t=await this.openFileDialogCallback(!0,e);return t===void 0?{}:{files:await Promise.all(t.map(r=>this.getLocalFileHandle(r)))}}async showOpenSingleFileDialog(e){const t=await this.openFileDialogCallback(!1,e);return t===void 0?{}:{file:await this.getLocalFileHandle(t[0])}}async tryResolveLocalFileHandle(e){if(et.isImportFileHandleIndex(e)){const t=Zn(e),r=this.localProjectionsMap.get(t.storageId);if(!r)throw new Error(`Storage ${t.storageId} is not mounted locally.`);return T.join(r.localPath,t.path)}else{const t=Jn(e);this.signer.verify(t.localPath,t.pathSignature,"Failed to validate local file handle signature.");const r=t.localPath,n=await y.stat(r,{bigint:!0});if(String(n.mtimeMs/1000n)!==t.modificationTime)throw new Error("File has changed since the handle was created.");return r}}async getLocalFileHandle(e){I(e);for(const r of this.localProjectionsMap.values())if(e.startsWith(r.localPath)){const n=r.localPath===""?e:T.relative(r.localPath,e);return ge(r.storageId,n)}const t=await y.stat(e,{bigint:!0});return qn(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){const e=[...this.virtualStoragesMap.values()].map(n=>({name:n.name,handle:Yn(n.name,n.root),initialFullPath:n.initialPath,isInitialPathHome:n.isInitialPathHome})),r=Object.entries(this.storageIdToResourceId).map(([n,o])=>({name:n,handle:tr(n,o),initialFullPath:"",isInitialPathHome:!1})).filter(n=>n.name!=="root");return[...e,...r]}async listFiles(e,t){const r=Qn(e);if(r.isRemote)return{entries:(await this.lsClient.list(r,t)).items.map(o=>({type:o.isDir?"dir":"file",name:o.name,fullPath:o.fullName,handle:ge(r.name,o.fullName)}))};{T.sep==="/"&&t===""&&(t="/"),r.rootPath===""&&I(t);const n=T.isAbsolute(t)?t:T.join(r.rootPath,t),o=[];for await(const c of await y.opendir(n)){if(!c.isFile()&&!c.isDirectory())continue;const a=T.join(n,c.name);o.push({type:c.isFile()?"file":"dir",name:c.name,fullPath:a,handle:await this.getLocalFileHandle(a)})}return{entries:o}}}static async init(e,t,r,n,o,c){const a=Re(t,e);for(const u of n)I(u.root);for(const u of o)u.localPath!==""&&I(u.localPath);const d=new Map(n.map(u=>[u.name,u])),l=new Map(o.map(u=>[u.storageId,u]));if(new Set([...d.keys(),...l.keys()]).size!==d.size+l.size)throw new Error("Intersection between local projection storage ids and virtual storages names detected.");return new oe(e,a,await or(t),r,d,l,c)}}async function or(i){return i.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),r=await e.getResourceData(t,!0);return sr(r)})}function sr(i){return Object.fromEntries(i.fields.filter(e=>e.type=="Dynamic"&&m.isNotNullResourceId(e.value)).map(e=>[e.name.substring(8),e.value]))}async function ir(){const i=me.homedir();if(F.sep=="/")return[{name:"local",root:"/",initialPath:i,isInitialPathHome:!0}];const t=F.parse(i).root.replaceAll(":\\",""),r=await tt.promisify(nt.exec)("wmic logicaldisk get name"),n=je(r.stdout);return Ge(n,t,i)}function je(i){return i.split(`\r
|
|
2
|
+
`).filter(e=>e.includes(":")).map(e=>e.trim().replaceAll(":",""))}function Ge(i,e,t){return i.map(r=>{const n=r==e;return{name:`local_disk_${r}`,root:`${r}:\\`,initialPath:n?t:`${r}:\\`,isInitialPathHome:n}})}exports.ClientDownload=ke;exports.ClientLogs=Ie;exports.ClientLs=Ue;exports.ClientProgress=Te;exports.ClientUpload=ye;exports.DefaultVirtualLocalStorages=ir;exports.Download=_e;exports.DownloadDriver=vn;exports.DownloadUrlDriver=Gn;exports.ImportFileHandleData=Hn;exports.ImportFileHandleIndexData=re;exports.ImportFileHandleUploadData=v;exports.IndexResourceSnapshot=Oe;exports.LogsDriver=Nn;exports.LogsStreamDriver=jn;exports.LongUpdater=In;exports.LsDriver=oe;exports.MTimeError=Z;exports.NetworkError=we;exports.NoFileForUploading=X;exports.OnDemandBlobResourceSnapshot=Ce;exports.UnexpectedEOF=Q;exports.UnknownStorageError=K;exports.Updater=te;exports.UploadDriver=En;exports.UploadResourceSnapshot=Me;exports.WrongLocalFileUrl=ee;exports.createDownloadClient=kn;exports.createLogsClient=Ln;exports.createLsFilesClient=Re;exports.createUploadBlobClient=Un;exports.createUploadProgressClient=Pn;exports.dataToHandle=ne;exports.getStream=Rn;exports.handleToData=D;exports.headersFromProto=Pe;exports.isLiveLogHandle=N;exports.isReadyLogHandle=Fe;exports.lineToProgress=De;exports.makeBlobImportSnapshot=He;exports.mixcrProgressFromLogs=Dn;exports.parseLocalFileUrl=Le;exports.validateAbsolute=I;exports.windowsDrives=je;exports.windowsStorages=Ge;
|
|
2
3
|
//# sourceMappingURL=index.js.map
|