@milaboratories/pl-drivers 1.8.2 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../../src/clients/upload.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAsB,yBAAyB,EAAE,QAAQ,EAA4B,MAAM,2BAA2B,CAAC;AAEnI,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAE3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAOzC,qBAAa,UAAW,SAAQ,KAAK;IACnC,IAAI,SAAgB;CACrB;AAED,qBAAa,aAAc,SAAQ,KAAK;IACtC,IAAI,SAAmB;CACxB;AAED,qBAAa,YAAa,SAAQ,KAAK;IACrC,IAAI,SAAkB;CACvB;AAED,0CAA0C;AAC1C,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,IAAI,SAAwB;CAC7B;AAED;;oDAEoD;AACpD,qBAAa,YAAY;aAKL,UAAU,EAAE,UAAU;aAEtB,MAAM,EAAE,QAAQ;IANlC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmC;gBAG5D,yBAAyB,EAAE,yBAAyB,EACpC,UAAU,EAAE,UAAU,EACtC,CAAC,EAAE,QAAQ,EACK,MAAM,EAAE,QAAQ;IAKlC,KAAK;IAEQ,UAAU,CACrB,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,YAAY,EAC1B,OAAO,CAAC,EAAE,UAAU,GACnB,OAAO,CAAC;QACP,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,EAAE,CAAC;KACpB,CAAC;IAQS,UAAU,CACrB,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,YAAY,EAC1B,IAAI,EAAE,MAAM,EACZ,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,UAAU;IA2CT,QAAQ,CAAC,IAAI,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,UAAU;IAI9D;2BACuB;IACvB,OAAO,CAAC,aAAa;YAWP,QAAQ;YAKR,cAAc;YAYd,kBAAkB;YAclB,YAAY;CAI3B"}
1
+ {"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../../src/clients/upload.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAsB,yBAAyB,EAAE,QAAQ,EAA4B,MAAM,2BAA2B,CAAC;AAEnI,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAE3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAOzC,qBAAa,UAAW,SAAQ,KAAK;IACnC,IAAI,SAAgB;CACrB;AAED,qBAAa,aAAc,SAAQ,KAAK;IACtC,IAAI,SAAmB;CACxB;AAED,qBAAa,YAAa,SAAQ,KAAK;IACrC,IAAI,SAAkB;CACvB;AAED,0CAA0C;AAC1C,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,IAAI,SAAwB;CAC7B;AAED;;oDAEoD;AACpD,qBAAa,YAAY;aAKL,UAAU,EAAE,UAAU;aAEtB,MAAM,EAAE,QAAQ;IANlC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmC;gBAG5D,yBAAyB,EAAE,yBAAyB,EACpC,UAAU,EAAE,UAAU,EACtC,CAAC,EAAE,QAAQ,EACK,MAAM,EAAE,QAAQ;IAKlC,KAAK;IAEQ,UAAU,CACrB,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,YAAY,EAC1B,OAAO,CAAC,EAAE,UAAU,GACnB,OAAO,CAAC;QACP,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,EAAE,CAAC;KACpB,CAAC;IAQS,UAAU,CACrB,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,YAAY,EAC1B,IAAI,EAAE,MAAM,EACZ,iBAAiB,EAAE,MAAM,EACzB,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,UAAU;IAoET,QAAQ,CAAC,IAAI,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,UAAU;IAI9D;2BACuB;IACvB,OAAO,CAAC,aAAa;YAWP,QAAQ;YAKR,cAAc;YAYd,kBAAkB;YAclB,YAAY;CAI3B"}
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";var yt=Object.defineProperty;var Tt=(o,e,t)=>e in o?yt(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var h=(o,e,t)=>Tt(o,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@milaboratories/pl-client"),bt=require("node:fs/promises"),Ce=require("undici"),y=require("@protobuf-ts/runtime-rpc"),a=require("@protobuf-ts/runtime"),g=require("@milaboratories/ts-helpers"),S=require("node:fs"),k=require("node:path"),R=require("node:stream"),ue=require("node:stream/consumers"),f=require("@milaboratories/computable"),v=require("@milaboratories/pl-model-common"),m=require("@milaboratories/pl-tree"),kt=require("denque"),P=require("node:crypto"),Fe=require("node:os"),Ut=require("node:readline/promises"),U=require("zod"),Lt=require("@milaboratories/helpers"),Pt=require("node:zlib"),Rt=require("tar-fs"),It=require("decompress"),Be=require("node:timers/promises"),St=require("node:assert"),Dt=require("node:util"),Nt=require("node:child_process");function D(o){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const t in o)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(o,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>o[t]})}}return e.default=o,Object.freeze(e)}const w=D(bt),pe=D(S),b=D(k),Re=D(Fe),$t=D(Ut),Ee=D(Pt),le=D(Rt);class vt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new vt;class Ct extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ct;class Ft extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Bt=new Ft;class Et extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.partsCount=e.uint64().toBigInt();break;case 2:if(c===a.WireType.LengthDelimited)for(let L=e.int32()+e.pos;e.pos<L;)i.uploadedParts.push(e.uint64().toBigInt());else i.uploadedParts.push(e.uint64().toBigInt());break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){if(e.partsCount!==0n&&t.tag(1,a.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,a.WireType.LengthDelimited).fork();for(let i=0;i<e.uploadedParts.length;i++)t.uint64(e.uploadedParts[i]);t.join()}let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Wt=new Et;class _t extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new _t;class Mt extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.bytesProcessed=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,a.WireType.Varint).int64(e.bytesProcessed);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ot=new Mt;class Ht extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const zt=new Ht;class xt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new xt;class At extends a.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},{no:4,name:"is_internal_use",kind:"scalar",T:8}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.partNumber=0n,t.uploadedPartSize=0n,t.isInternalUse=!1,e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.partNumber=e.uint64().toBigInt();break;case 3:i.uploadedPartSize=e.uint64().toBigInt();break;case 4:i.isInternalUse=e.bool();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,a.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,a.WireType.Varint).uint64(e.uploadedPartSize),e.isInternalUse!==!1&&t.tag(4,a.WireType.Varint).bool(e.isInternalUse);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const jt=new At;class Gt extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.name=e.string();break;case 2:i.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Z=new Gt;class Vt extends a.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:2,T:()=>Z},{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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.uploadUrl=e.string();break;case 2:i.method=e.string();break;case 3:i.headers.push(Z.internalBinaryRead(e,e.uint32(),r));break;case 4:i.chunkStart=e.uint64().toBigInt();break;case 5:i.chunkEnd=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.uploadUrl!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.method);for(let i=0;i<e.headers.length;i++)Z.internalBinaryWrite(e.headers[i],t.tag(3,a.WireType.LengthDelimited).fork(),r).join();e.chunkStart!==0n&&t.tag(4,a.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,a.WireType.Varint).uint64(e.chunkEnd);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const qt=new Vt;class Jt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Jt;class Zt extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Qt=new Zt;class Kt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Xt=new Kt,Q=new y.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:Bt,O:Wt},{name:"GetPartURL",options:{},I:jt,O:qt},{name:"UpdateProgress",options:{},I:Ot,O:zt},{name:"Finalize",options:{},I:Qt,O:Xt}]);class Yt{constructor(e){h(this,"typeName",Q.typeName);h(this,"methods",Q.methods);h(this,"options",Q.options);this._transport=e}init(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}getPartURL(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}updateProgress(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}finalize(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}}class ge extends Error{constructor(){super(...arguments);h(this,"name","MTimeError")}}class fe extends Error{constructor(){super(...arguments);h(this,"name","UnexpectedEOF")}}class we extends Error{constructor(){super(...arguments);h(this,"name","NetworkError")}}class me extends Error{constructor(){super(...arguments);h(this,"name","NoFileForUploading")}}class We{constructor(e,t,r,n){h(this,"grpcClient");this.httpClient=t,this.logger=n,this.grpcClient=e.createGrpcClientProvider(i=>new Yt(i))}close(){}async initUpload({id:e,type:t},r){const n=await this.grpcInit(e,t,r);return{overall:n.partsCount,toUpload:this.partsToUpload(n.partsCount,n.uploadedParts)}}async partUpload({id:e,type:t},r,n,i,d){const s=await this.grpcGetPartUrl({id:e,type:t},i,0n,d),c=await er(r,s.chunkStart,s.chunkEnd);await rr(r,n);try{const{body:l,statusCode:u,headers:L}=await Ce.request(s.uploadUrl,{dispatcher:this.httpClient,body:c,headersTimeout:6e4,bodyTimeout:6e4,headers:Object.fromEntries(s.headers.map(({name:q,value:J})=>[q,J])),method:s.method.toUpperCase()}),I=await l.text();nr(u,I,L,s)}catch(l){throw l instanceof we?l:new Error(`partUpload: error ${JSON.stringify(l)} happened while trying to do part upload to the url ${s.uploadUrl}, headers: ${JSON.stringify(s.headers)}`)}await this.grpcUpdateProgress({id:e,type:t},BigInt(s.chunkEnd-s.chunkStart),d)}async finalize(e,t){return await this.grpcFinalize(e,t)}partsToUpload(e,t){const r=[],n=new Set(t);for(let i=1n;i<=e;i++)n.has(i)||r.push(i);return r}async grpcInit(e,t,r){return await this.grpcClient.get().init({resourceId:e},p.addRTypeToMetadata(t,r)).response}async grpcGetPartUrl({id:e,type:t},r,n,i){return await this.grpcClient.get().getPartURL({resourceId:e,partNumber:r,uploadedPartSize:n,isInternalUse:!1},p.addRTypeToMetadata(t,i)).response}async grpcUpdateProgress({id:e,type:t},r,n){await this.grpcClient.get().updateProgress({resourceId:e,bytesProcessed:r},p.addRTypeToMetadata(t,n)).response}async grpcFinalize({id:e,type:t},r){return await this.grpcClient.get().finalize({resourceId:e},p.addRTypeToMetadata(t,r)).response}}async function er(o,e,t){let r;try{r=await w.open(o);const n=Number(t-e),i=Number(e),d=Buffer.alloc(n),s=await tr(r,d,n,i);return d.subarray(0,s)}catch(n){throw n&&typeof n=="object"&&"code"in n&&n.code=="ENOENT"?new me(`there is no file ${o} for uploading`):n}finally{await(r==null?void 0:r.close())}}async function tr(o,e,t,r){let n=0;for(;n<t;){const{bytesRead:i}=await o.read(e,n,t-n,r+n);if(i===0)throw new fe("file ended earlier than expected.");n+=i}return n}async function rr(o,e){const t=BigInt(Math.floor((await w.stat(o)).mtimeMs/1e3));if(t>e)throw new ge(`file was modified, expected mtime: ${e}, got: ${t}.`)}function nr(o,e,t,r){if(o!=200)throw new we(`response is not ok, status code: ${o}, body: ${e}, headers: ${JSON.stringify(t)}, url: ${r.uploadUrl}`)}class ir extends a.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=a.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 i=Math.abs(e.nanos).toString();i="0".repeat(9-i.length)+i,i.substring(3)==="000000"?i=i.substring(0,3):i.substring(6)==="000"&&(i=i.substring(0,6)),n+="."+i}return n+"s"}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+a.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[,i,d,s]=n,c=a.PbLong.from(i+d);if(c.toNumber()>315576e6||c.toNumber()<-315576e6)throw new Error("Unable to parse Duration from JSON string. Value out of range.");if(r.seconds=c.toBigInt(),typeof s=="string"){let l=i+s+"0".repeat(9-s.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.seconds=e.int64().toBigInt();break;case 2:i.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,a.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,a.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const O=new ir;class or extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new or;class sr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.progress=e.float();break;case 2:i.bytesProcessed=e.uint64().toBigInt();break;case 3:i.bytesTotal=e.uint64().toBigInt();break;case 4:i.done=e.bool();break;case 5:i.name=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.progress!==0&&t.tag(1,a.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,a.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,a.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,a.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,a.WireType.LengthDelimited).string(e.name);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const $=new sr;class ar extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ar;class lr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const cr=new lr;class dr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>$}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.report=$.internalBinaryRead(e,e.uint32(),r,i.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.report&&$.internalBinaryWrite(e.report,t.tag(1,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const hr=new dr;class ur extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ur;class pr extends a.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:()=>O}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.updateInterval=O.internalBinaryRead(e,e.uint32(),r,i.updateInterval);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.updateInterval&&O.internalBinaryWrite(e.updateInterval,t.tag(2,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const gr=new pr;class fr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>$}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.report=$.internalBinaryRead(e,e.uint32(),r,i.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.report&&$.internalBinaryWrite(e.report,t.tag(1,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const wr=new fr,K=new y.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:cr,O:hr},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:gr,O:wr}]);class mr{constructor(e){h(this,"typeName",K.typeName);h(this,"methods",K.methods);h(this,"options",K.options);this._transport=e}getStatus(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}realtimeStatus(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return y.stackIntercept("serverStreaming",this._transport,r,n,e)}}class _e{constructor(e,t,r,n){h(this,"grpcClient");this.client=r,this.logger=n,this.grpcClient=e.createGrpcClientProvider(i=>new mr(i))}close(){}async getStatus({id:e,type:t},r){const n=await this.grpcClient.get().getStatus({resourceId:e},p.addRTypeToMetadata(t,r)),i=g.notEmpty(n.response.report);return{done:i.done,progress:i.progress,bytesProcessed:String(i.bytesProcessed),bytesTotal:String(i.bytesTotal)}}async*realtimeStatus({id:e,type:t},r=100,n){n=p.addRTypeToMetadata(t,n);const i=Math.floor(r/1e3),d=(r-i*1e3)*1e6,s=O.create({seconds:BigInt(i),nanos:d});try{const{responses:c}=this.grpcClient.get().realtimeStatus({resourceId:e,updateInterval:s},n);yield*c}catch(c){throw this.logger.warn("Failed to get realtime status"+String(c)),c}}}class A extends Error{constructor(){super(...arguments);h(this,"name","NetworkError400")}}class Me{constructor(e){this.httpClient=e}async withContent(e,t,r,n){const i={...t};r.range&&(i.Range=`bytes=${r.range.from}-${r.range.to-1}`);const{statusCode:d,body:s,headers:c}=await Ce.request(e,{dispatcher:this.httpClient,headers:i,signal:r.signal}),l=R.Readable.toWeb(s);let u=!1;try{await yr(d,l,e);const L=Number(c["content-length"]),I=await n(l,L);return u=!0,I}catch(L){if(!u&&!l.locked)try{await l.cancel()}catch{}throw L}}}async function yr(o,e,t){if(o!=200&&o!=206){const r=(await ue.text(e)).substring(0,1e3);throw 400<=o&&o<500?new A(`Http error: statusCode: ${o} url: ${t.toString()}, beginning of body: ${r}`):new Error(`Http error: statusCode: ${o} url: ${t.toString()}`)}}function N(o){if(!k.isAbsolute(o))throw new Error(`Path ${o} is not absolute.`);return o}class Tr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Tr;class br extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new br;class kr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"is_internal_use",kind:"scalar",T:8}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.isInternalUse=!1,e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.isInternalUse=e.bool();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.isInternalUse!==!1&&t.tag(2,a.WireType.Varint).bool(e.isInternalUse);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ur=new kr;class Lr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.name=e.string();break;case 2:i.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const X=new Lr;class Pr extends a.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:2,T:()=>X}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.downloadUrl=e.string();break;case 2:i.headers.push(X.internalBinaryRead(e,e.uint32(),r));break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.downloadUrl!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.downloadUrl);for(let i=0;i<e.headers.length;i++)X.internalBinaryWrite(e.headers[i],t.tag(2,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Rr=new Pr,Y=new y.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:Ur,O:Rr}]);class Ir{constructor(e){h(this,"typeName",Y.typeName);h(this,"methods",Y.methods);h(this,"options",Y.options);this._transport=e}getDownloadURL(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}}class Oe{constructor(e,t,r,n){h(this,"grpcClient");h(this,"remoteFileDownloader");h(this,"localStorageIdsToRoot");h(this,"localFileReadLimiter",new g.ConcurrencyLimitingExecutor(32));this.httpClient=t,this.logger=r,this.grpcClient=e.createGrpcClientProvider(i=>new Ir(i)),this.remoteFileDownloader=new Me(t),this.localStorageIdsToRoot=xe(n)}close(){}async withBlobContent(e,t,r,n){const{downloadUrl:i,headers:d}=await this.grpcGetDownloadUrl(e,t,r.signal),s=Object.fromEntries(d.map(({name:c,value:l})=>[c,l]));return this.logger.info(`download blob ${p.stringifyWithResourceId(e)} from url ${i}, ops: ${JSON.stringify(r)}`),Dr(i)?await this.withLocalFileContent(i,r,n):await this.remoteFileDownloader.withContent(i,s,r,n)}async withLocalFileContent(e,t,r){const{storageId:n,relativePath:i}=He(e),d=ze(n,this.localStorageIdsToRoot,i);return await this.localFileReadLimiter.run(async()=>{var u,L;const s={start:(u=t.range)==null?void 0:u.from,end:((L=t.range)==null?void 0:L.to)!==void 0?t.range.to-1:void 0};let c,l=!1;try{const I=await w.stat(d);c=pe.createReadStream(d,s);const q=R.Readable.toWeb(c),J=await r(q,I.size);return l=!0,J}catch(I){throw!l&&c&&!c.destroyed&&c.destroy(),I}})}async grpcGetDownloadUrl({id:e,type:t},r,n){const i=r??{};return i.abort=n,await this.grpcClient.get().getDownloadURL({resourceId:e,isInternalUse:!1},p.addRTypeToMetadata(t,i)).response}}function He(o){const e=new URL(o);if(e.pathname=="")throw new j(`url for local filepath ${o} does not match url scheme`);return{storageId:e.host,relativePath:decodeURIComponent(e.pathname.slice(1))}}function ze(o,e,t){const r=e.get(o);if(r===void 0)throw new G(`Unknown storage location: ${o}`);return r===""?t:b.join(r,t)}const Sr="storage://";function Dr(o){return o.startsWith(Sr)}class j extends Error{constructor(){super(...arguments);h(this,"name","WrongLocalFileUrl")}}class G extends Error{constructor(){super(...arguments);h(this,"name","UnknownStorageError")}}function xe(o){const e=new Map;for(const t of o)t.localPath!==""&&N(t.localPath),e.set(t.storageId,t.localPath);return e}class Nr extends a.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=a.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(a.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),r=e.getTime();return t.seconds=a.PbLong.from(Math.floor(r/1e3)).toBigInt(),t.nanos=(r%1e3+(r<0&&r%1e3!==0?1e3:0))*1e6,t}internalJsonWrite(e,t){let r=a.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 i=(e.nanos+1e9).toString().substring(1);i.substring(3)==="000000"?n="."+i.substring(0,3)+"Z":i.substring(6)==="000"?n="."+i.substring(0,6)+"Z":n="."+i+"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 "+a.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 i=Date.parse(n[1]+"-"+n[2]+"-"+n[3]+"T"+n[4]+":"+n[5]+":"+n[6]+(n[8]?n[8]:"Z"));if(Number.isNaN(i))throw new Error("Unable to parse Timestamp from JSON. Invalid value.");if(i<Date.parse("0001-01-01T00:00:00Z")||i>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=a.PbLong.from(i/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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.seconds=e.int64().toBigInt();break;case 2:i.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,a.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,a.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ee=new Nr;class $r extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new $r;class vr extends a.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:()=>ee},{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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.name=e.string();break;case 2:i.size=e.uint64().toBigInt();break;case 3:i.isDir=e.bool();break;case 10:i.fullName=e.string();break;case 11:i.directory=e.string();break;case 12:i.lastModified=ee.internalBinaryRead(e,e.uint32(),r,i.lastModified);break;case 13:i.version=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,a.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,a.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,a.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,a.WireType.LengthDelimited).string(e.directory),e.lastModified&&ee.internalBinaryWrite(e.lastModified,t.tag(12,a.WireType.LengthDelimited).fork(),r).join(),e.version!==""&&t.tag(13,a.WireType.LengthDelimited).string(e.version);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const te=new vr;class Cr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Cr;class Fr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.location=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.location);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Br=new Fr;class Er extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:2,T:()=>te},{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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.items.push(te.internalBinaryRead(e,e.uint32(),r));break;case 2:i.delimiter=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){for(let i=0;i<e.items.length;i++)te.internalBinaryWrite(e.items[i],t.tag(1,a.WireType.LengthDelimited).fork(),r).join();e.delimiter!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.delimiter);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Wr=new Er,re=new y.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:Br,O:Wr}]);class _r{constructor(e){h(this,"typeName",re.typeName);h(this,"methods",re.methods);h(this,"options",re.options);this._transport=e}list(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}}class Ae{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=e.createGrpcClientProvider(r=>new _r(r))}close(){}async list(e,t,r){return await this.grpcClient.get().list({resourceId:e.id,location:t},p.addRTypeToMetadata(e.type,r)).response}}class Mr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Mr;class Or extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 11:i.chunkSize=e.uint32();break;case 20:i.readLimit=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,a.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,a.WireType.Varint).int64(e.readLimit);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Hr=new Or;class zr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 11:i.chunkSize=e.uint32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,a.WireType.Varint).uint32(e.chunkSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const xr=new zr;class Ar extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 20:i.readLimit=e.int64().toBigInt();break;case 21:i.search=e.string();break;case 22:i.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,a.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,a.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,a.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const jr=new Ar;class Gr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 20:i.readLimit=e.int64().toBigInt();break;case 21:i.search=e.string();break;case 22:i.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,a.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,a.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,a.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Vr=new Gr;class qr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 3:i.lineCount=e.int32();break;case 21:i.search=e.string();break;case 22:i.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,a.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,a.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,a.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,a.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Jr=new qr;class Zr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.data=e.bytes();break;case 2:i.size=e.uint64().toBigInt();break;case 3:i.newOffset=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.data.length&&t.tag(1,a.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,a.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,a.WireType.Varint).uint64(e.newOffset);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const C=new Zr,ne=new y.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:Hr,O:C},{name:"ReadBinary",options:{},I:xr,O:C},{name:"StreamText",serverStreaming:!0,options:{},I:jr,O:C},{name:"ReadText",options:{},I:Vr,O:C},{name:"LastLines",options:{},I:Jr,O:C}]);class Qr{constructor(e){h(this,"typeName",ne.typeName);h(this,"methods",ne.methods);h(this,"options",ne.options);this._transport=e}streamBinary(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return y.stackIntercept("serverStreaming",this._transport,r,n,e)}readBinary(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}streamText(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return y.stackIntercept("serverStreaming",this._transport,r,n,e)}readText(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}lastLines(e,t){const r=this.methods[4],n=this._transport.mergeOptions(t);return y.stackIntercept("unary",this._transport,r,n,e)}}class je{constructor(e,t,r){h(this,"grpcClient");this.httpClient=t,this.logger=r,this.grpcClient=e.createGrpcClientProvider(n=>new Qr(n))}close(){}async lastLines({id:e,type:t},r,n=0n,i,d){return(await this.grpcClient.get().lastLines({resourceId:e,lineCount:r,offset:n,search:i},p.addRTypeToMetadata(t,d))).response}async readText({id:e,type:t},r,n=0n,i,d){return(await this.grpcClient.get().readText({resourceId:g.notEmpty(e),readLimit:BigInt(r),offset:n,search:i},p.addRTypeToMetadata(t,d))).response}}function Kr(o,e,t){return e.getDriver({name:"DownloadBlob",init:(r,n,i)=>new Oe(n,i,o,t)})}function Xr(o,e){return o.getDriver({name:"StreamLogs",init:(t,r,n)=>new je(r,n,e)})}function Yr(o,e){return o.getDriver({name:"UploadProgress",init:(t,r,n)=>new _e(r,n,o,e)})}function en(o,e){return o.getDriver({name:"UploadBlob",init:(t,r,n)=>new We(r,n,o,e)})}function Ge(o,e){return o.getDriver({name:"LsFiles",init:(t,r,n)=>new Ae(r,e)})}const tn=new g.ConcurrencyLimitingExecutor(32);async function Ie(o,e){return await tn.run(async()=>{const t={};e&&(t.start=e.from,t.end=e.to-1);let r;try{return r=pe.createReadStream(o,t),await ue.buffer(r)}catch(n){throw r&&!r.destroyed&&r.destroy(),n}})}const Ve=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function rn(o,e){return`blob+local://download/${o}#${e.sign(o)}`}function nn(o){return!!o.match(Ve)}function ie(o,e){const t=o.match(Ve);if(t===null)throw new Error(`Local handle is malformed: ${o}, matches: ${t}`);const{path:r,signature:n}=t.groups;return e.verify(r,n,`Signature verification failed for: ${o}`),{path:r,signature:n}}const qe=m.rsSchema({kv:{"ctl/file/blobInfo":U.z.object({sizeBytes:U.z.coerce.number()})}});function ye(o,e){const t=o.kv["ctl/file/blobInfo"].sizeBytes;if(e){const r=e.to-e.from;if(r>t)throw new Error(`getSize: range (${JSON.stringify(e)}, newSize: ${r}) is greater than size (${t})`);return r}return t}const B=U.z.object({localPath:U.z.string(),pathSignature:U.z.string(),sizeBytes:U.z.string(),modificationTime:U.z.string()}),Te=U.z.object({storageId:U.z.string(),path:U.z.string()}),on=U.z.union([B,Te]),Je=m.rsSchema({data:B,fields:{blob:!1}}),Ze=m.rsSchema({fields:{incarnation:!1}}),Qe=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.+)\/(?<resourceVersion>.+?)\/(?<resourceId>\d+?)\/(?<size>\d+?))#(?<signature>.*)$/;function sn(o,e){let t=`${o.type.name}/${o.type.version}/${BigInt(o.id)}/${ye(o)}`;return`blob+remote://download/${t}#${e.sign(t)}`}function an(o){return!!o.match(Qe)}function ln(o,e){const t=o.match(Qe);if(t===null)throw new Error(`Remote handle is malformed: ${o}, matches: ${t}`);const{content:r,resourceType:n,resourceVersion:i,resourceId:d,size:s,signature:c}=t.groups;return e.verify(r,c,`Signature verification failed for ${o}`),{info:{id:p.bigintToResourceId(BigInt(d)),type:{name:n,version:i}},size:Number(s)}}class V extends Error{constructor(){super(...arguments);h(this,"name","WrongResourceTypeError")}}class Ke{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}})())}}function Xe(o,e){return o?`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 Ye=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function H(o){return Ye.test(o)}const et=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function cn(o){return et.test(o)}function z(o){let e;if(H(o))e=o.match(Ye);else if(cn(o))e=o.match(et);else throw new Error(`Log handle is malformed: ${o}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${o}`);const{resourceType:t,resourceVersion:r,resourceId:n}=e.groups;return{id:p.bigintToResourceId(BigInt(n)),type:{name:t,version:r}}}function T(o){return`${BigInt(o)}`}function Se(o){return b.basename(o)}class dn{constructor(e,t,r,n,i){h(this,"change",new f.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"counter",new g.CallersCounter);h(this,"error");h(this,"done",!1);h(this,"size",0);h(this,"state",{});this.logger=e,this.clientDownload=t,this.rInfo=r,this.handle=n,this.path=i}info(){return{rInfo:this.rInfo,fPath:this.path,done:this.done,error:this.error,state:this.state}}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(){try{const e=await this.ensureDownloaded();this.setDone(e),this.change.markChanged(`blob download for ${p.resourceIdToString(this.rInfo.id)} finished`)}catch(e){throw this.logger.error(`download blob ${p.stringifyWithResourceId(this.rInfo)} failed: ${e}, state: ${JSON.stringify(this.state)}`),be(e)&&(this.setError(e),this.change.markChanged(`blob download for ${p.resourceIdToString(this.rInfo.id)} failed`),await w.rm(this.path,{force:!0})),e}}async ensureDownloaded(){this.signalCtl.signal.throwIfAborted(),this.state={},this.state.filePath=this.path,await g.ensureDirExists(b.dirname(this.state.filePath)),this.signalCtl.signal.throwIfAborted(),this.state.dirExists=!0;const e=await g.fileExists(this.state.filePath);if(this.signalCtl.signal.throwIfAborted(),e){this.state.fileExists=!0,this.logger.info(`a blob was already downloaded: ${this.state.filePath}`);const r=await w.stat(this.state.filePath);return this.signalCtl.signal.throwIfAborted(),this.state.fileSize=r.size,this.state.fileSize}return await this.clientDownload.withBlobContent(this.rInfo,{},{signal:this.signalCtl.signal},async(r,n)=>(this.state.fileSize=n,this.state.downloaded=!0,await g.createPathAtomically(this.logger,this.state.filePath,async i=>{const d=R.Writable.toWeb(S.createWriteStream(i,{flags:"wx"}));await r.pipeTo(d,{signal:this.signalCtl.signal}),this.state.tempWritten=!0}),this.state.done=!0,n))}abort(e){this.signalCtl.abort(new tt(e))}getBlob(){return this.done?{done:this.done,result:hn(this.handle,this.size,this.error)}:{done:!1}}setDone(e){this.done=!0,this.size=e}setError(e){this.done=!0,this.error=e}}function be(o){return o instanceof tt||o instanceof A||o instanceof G||o instanceof j||(o==null?void 0:o.code)=="ENOENT"||o.name=="RpcError"&&(o.code=="NOT_FOUND"||o.code=="ABORTED")}class tt extends Error{constructor(){super(...arguments);h(this,"name","DownloadAborted")}}function hn(o,e,t){return t?{ok:!1,error:t}:o?{ok:!0,value:{handle:o,size:e}}:{ok:!1,error:new Error("No file or handle provided")}}class ke{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 i=g.mapGet(this.cache,r);t+=i.size,e.push(i)}),e}addCache(e,t){const r=this.cache.get(e.path)==null;if(this.cache.set(e.path,e),e.counter.inc(t),e.size<0)throw new Error(`empty sizeBytes: ${e}`);r&&(this.totalSizeBytes+=e.size)}removeCache(e){this.cache.delete(e.path),this.totalSizeBytes-=e.size}}const un=U.z.object({ranges:U.z.array(v.RangeBytes)}),pn=".ranges.json";function gn(o){return o+pn}async function fn(o,e){let t={ranges:[]};try{const r=await w.readFile(e,"utf8");t=un.parse(JSON.parse(r))}catch(r){if(r instanceof SyntaxError||r instanceof U.z.ZodError){const n=`readRangesFile: the file ${e} was corrupted: ${r}`;throw o.error(n),new nt(n)}if(!(r instanceof Error&&"code"in r&&r.code==="ENOENT"))throw r}return rt(t),t}async function wn(o,e,t){await g.createPathAtomically(o,e,async r=>{await w.writeFile(r,JSON.stringify(t,null,2),{flag:"wx"})})}function rt(o){o.ranges.sort((e,t)=>e.from-t.from);for(let e=0;e<o.ranges.length-1;e++)o.ranges[e].to>=o.ranges[e+1].from&&(mn(o,e),e--)}function mn(o,e){const t=Math.min(o.ranges[e].from,o.ranges[e+1].from),r=Math.max(o.ranges[e].to,o.ranges[e+1].to);o.ranges.splice(e,2,{from:t,to:r})}function E(o){return o.ranges.reduce((e,t)=>e+t.to-t.from,0)}function yn(o,e){for(const t of o.ranges)if(t.from<=e.from&&e.to<=t.to)return!0;return!1}function Tn(o,e){return o.ranges.push(e),rt(o),o}async function bn(o,e,t){try{await(await w.open(e,"w")).close(),await kn(e,t)}catch(r){o.error(`Error creating file ${e} on platform ${t}: ${r}`)}}async function kn(o,e){e==="win32"&&await g.spawnAsync("fsutil",["sparse","setflag",`"${o}"`],{stdio:"pipe"})}async function Un(o,e,t,r,n){await g.fileExists(t)||await bn(o,t,e);const i=await w.open(t,"r+");await i.write(r,0,r.length,n),await i.close()}class nt extends Error{constructor(){super(...arguments);h(this,"name","CorruptedRangesError")}}class Ln{constructor(e,t){this.logger=e,this.cacheDir=t}fPath(e){return k.join(this.cacheDir,gn(e))}async get(e){return await fn(this.logger,this.fPath(e))}async set(e,t){return await wn(this.logger,this.fPath(e),t)}async delete(e){await S.promises.rm(this.fPath(e))}}class Pn{constructor(e,t){h(this,"suffix",".sparse.bin");this.logger=e,this.cacheDir=t}async all(){return await g.ensureDirExists(this.cacheDir),(await S.promises.readdir(this.cacheDir)).filter(t=>t.endsWith(this.suffix))}async exists(e){return await g.fileExists(this.path(e))}path(e){return k.join(this.cacheDir,e+this.suffix)}async write(e,t,r){await g.ensureDirExists(this.cacheDir),await Un(this.logger,process.platform,this.path(e),t,r)}async delete(e){await S.promises.rm(this.path(e))}}class Rn{constructor(e,t,r,n){h(this,"lock",new Lt.functions.AwaitLock);h(this,"keyToLastAccessTime",new Map);h(this,"size",0);this.logger=e,this.maxSize=t,this.ranges=r,this.storage=n}async reset(){await oe(this.lock,async()=>{await this.resetUnsafe()})}async get(e,t){return await oe(this.lock,async()=>await this.getUnsafe(e,t))}async set(e,t,r){await oe(this.lock,async()=>{await this.setUnsafe(e,t,r)})}async resetUnsafe(){this.size=0,this.keyToLastAccessTime=new Map;const e=new Date;for(const t of await this.storage.all()){const r=await this.ranges.get(t);this.size+=E(r),this.keyToLastAccessTime.set(t,e)}}async getUnsafe(e,t){if(await this.storage.exists(e)){this.keyToLastAccessTime.set(e,new Date);const r=await this.getRanges(e);return yn(r,t)?this.storage.path(e):void 0}}async setUnsafe(e,t,r){await this.setWithoutEviction(e,t,r),await this.ensureEvicted()}async setWithoutEviction(e,t,r){if(t.to-t.from!==r.length)throw new Error(`SparseCache.set: trying to set ${e} with wrong range length: range: ${JSON.stringify(t)}, data: ${r.length}`);this.keyToLastAccessTime.set(e,new Date);const n=await this.getRanges(e);this.size-=E(n),await this.storage.write(e,r,t.from);const i=Tn(n,t);this.size+=E(i),await this.ranges.set(e,i)}async ensureEvicted(){const e=g.mapEntries(this.keyToLastAccessTime);for(e.sort(([t,r],[n,i])=>i.getTime()-r.getTime());this.size>this.maxSize;){const t=e.pop();if(!t)break;const[r,n]=t,i=await this.getRanges(r);this.size-=E(i),this.rmKey(r)}}async getRanges(e){try{return await this.ranges.get(e)}catch(t){if(t instanceof nt)return await this.rmKey(e),await this.resetUnsafe(),await this.ranges.get(e);throw t}}async rmKey(e){await this.storage.delete(e),await this.ranges.delete(e),this.keyToLastAccessTime.delete(e)}}async function oe(o,e){try{return await o.acquireAsync(),await e()}finally{o.release()}}class Ue{constructor(e,t,r,n,i,d,s){h(this,"keyToDownload",new Map);h(this,"cache");h(this,"rangesCache");h(this,"downloadQueue");h(this,"keyToOnDemand",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.rangesCacheDir=i,this.signer=d,this.ops=s,this.cache=new ke(this.ops.cacheSoftSizeBytes);const c=new Ln(this.logger,this.rangesCacheDir),l=new Pn(this.logger,this.rangesCacheDir);this.rangesCache=new Rn(this.logger,this.ops.rangesCacheMaxSizeBytes,c,l),this.downloadQueue=new g.TaskProcessor(this.logger,s.nConcurrentDownloads),this.saveDir=b.resolve(n)}static async init(e,t,r,n,i,d,s){const c=new Ue(e,t,r,n,i,d,s);return await c.rangesCache.reset(),c}getDownloadedBlob(e,t){if(t===void 0)return f.Computable.make(d=>this.getDownloadedBlob(e,d));const r=m.treeEntryToResourceInfo(e,t),n=P.randomUUID();t.addOnDestroy(()=>this.releaseBlob(r,n));const i=this.getDownloadedBlobNoCtx(t.watcher,r,n);return i==null&&t.markUnstable("download blob is still undefined"),i}getDownloadedBlobNoCtx(e,t,r){F("getDownloadedBlob",t.type);const n=this.getOrSetNewTask(t,r);n.attach(e,r);const i=n.getBlob();if(i.done){if(i.result.ok)return i.result.value;throw i.result.error}}getOrSetNewTask(e,t){const r=T(e.id),n=this.keyToDownload.get(r);if(n)return n;const i=b.resolve(this.saveDir,r),d=new dn(this.logger,this.clientDownload,e,rn(i,this.signer),i);return this.keyToDownload.set(r,d),this.downloadQueue.push({fn:()=>this.downloadBlob(d,t),recoverableErrorPredicate:s=>!be(s)}),d}async downloadBlob(e,t){await e.download();const r=e.getBlob();r.done&&r.result.ok&&this.cache.addCache(e,t)}getOnDemandBlob(e,t){if(t===void 0)return f.Computable.make(d=>this.getOnDemandBlob(e,d));const r=m.isPlTreeEntry(e)?m.makeResourceSnapshot(e,qe,t):e,n=P.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(r.id,n)),this.getOnDemandBlobNoCtx(r,n)}getOnDemandBlobNoCtx(e,t){F("getOnDemandBlob",e.type);let r=this.keyToOnDemand.get(T(e.id));return r===void 0&&(r=new In(ye(e),sn(e,this.signer)),this.keyToOnDemand.set(T(e.id),r)),r.attach(t),r.getHandle()}getLocalPath(e){const{path:t}=ie(e,this.signer);return t}async getContent(e,t){if(t&&v.validateRangeBytes(t,"getContent"),nn(e))return await Ie(this.getLocalPath(e),t);if(an(e)){const r=ln(e,this.signer),n=T(r.info.id),i=await this.rangesCache.get(n,t??{from:0,to:r.size});if(i)return await Ie(i,t);const d=await this.clientDownload.withBlobContent({id:r.info.id,type:r.info.type},void 0,{range:t},async s=>await ue.buffer(s));return await this.rangesCache.set(n,t??{from:0,to:r.size},d),d}throw new Error("Malformed remote handle")}getComputableContent(e,t){return t&&v.validateRangeBytes(t,"getComputableContent"),f.Computable.make(r=>this.getDownloadedBlob(e,r),{postprocessValue:r=>r?this.getContent(r.handle,t):void 0}).withStableType()}getLastLogs(e,t,r){if(r==null)return f.Computable.make(s=>this.getLastLogs(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=P.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n,i));const d=this.getLastLogsNoCtx(r.watcher,n,t,i);return d==null&&r.markUnstable("either a file was not downloaded or logs was not read"),d}getLastLogsNoCtx(e,t,r,n){F("getLastLogs",t.type);const i=this.getDownloadedBlobNoCtx(e,t,n);if(i==null)return;const{path:d}=ie(i.handle,this.signer);let s=this.idToLastLines.get(T(t.id));if(s==null){const l=new De(d,r);this.idToLastLines.set(T(t.id),l),s=l}const c=s.getOrSchedule(e);if(c.error)throw c.error;return c.log}getProgressLog(e,t,r){if(r==null)return f.Computable.make(s=>this.getProgressLog(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=P.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n,i));const d=this.getProgressLogNoCtx(r.watcher,n,t,i);return d===void 0&&r.markUnstable("either a file was not downloaded or a progress log was not read"),d}getProgressLogNoCtx(e,t,r,n){F("getProgressLog",t.type);const i=this.getDownloadedBlobNoCtx(e,t,n);if(i==null)return;const{path:d}=ie(i.handle,this.signer);let s=this.idToProgressLog.get(T(t.id));if(s==null){const l=new De(d,1,r);this.idToProgressLog.set(T(t.id),l),s=l}const c=s.getOrSchedule(e);if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return f.Computable.make(n=>this.getLogHandle(e,n));const r=m.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(r)}getLogHandleNoCtx(e){return F("getLogHandle",e.type),Xe(!1,e)}async lastLines(e,t,r,n){const i=await this.clientLogs.lastLines(z(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:i.data,size:Number(i.size),newOffset:Number(i.newOffset)}}async readText(e,t,r,n){const i=await this.clientLogs.readText(z(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:i.data,size:Number(i.size),newOffset:Number(i.newOffset)}}async releaseBlob(e,t){const r=this.keyToDownload.get(T(e.id));if(r!=null)if(this.cache.existsFile(T(e.id))){const n=this.cache.removeFile(T(e.id),t);await Promise.all(n.map(async i=>{await w.rm(i.path),this.cache.removeCache(i),this.removeTask(g.mapGet(this.keyToDownload,Se(i.path)),`the task ${p.stringifyWithResourceId(i)} was removedfrom cache along with ${p.stringifyWithResourceId(n.map(d=>d.path))}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${p.stringifyWithResourceId(r.info())} was removed from cache`)}removeTask(e,t){e.abort(t),e.change.markChanged(`download task for ${e.path} removed: ${t}`),this.keyToDownload.delete(Se(e.path)),this.idToLastLines.delete(T(e.rInfo.id)),this.idToProgressLog.delete(T(e.rInfo.id))}async releaseOnDemandBlob(e,t){var n;(((n=this.keyToOnDemand.get(T(e)))==null?void 0:n.release(t))??!1)&&this.keyToOnDemand.delete(T(e))}async releaseAll(){this.downloadQueue.stop(),this.keyToDownload.forEach((e,t)=>{this.keyToDownload.delete(t),e.change.markChanged(`task ${p.resourceIdToString(e.rInfo.id)} released`)})}}class In{constructor(e,t){h(this,"counter",new g.CallersCounter);this.size=e,this.handle=t}getHandle(){return{handle:this.handle,size:this.size}}attach(e){this.counter.inc(e)}release(e){return this.counter.dec(e)}}class De{constructor(e,t,r){h(this,"updater");h(this,"log");h(this,"change",new f.ChangeSource);h(this,"error");this.path=e,this.lines=t,this.patternToSearch=r,this.updater=new Ke(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 Sn(this.path,this.lines,this.patternToSearch);this.log!=e&&this.change.markChanged(`logs for ${this.path} updated`),this.log=e}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.log="",this.error=e,this.change.markChanged(`log update for ${this.path} failed, resource not found`);return}throw e}}}async function Sn(o,e,t){let r,n;try{r=pe.createReadStream(o),n=$t.createInterface({input:r,crlfDelay:1/0});const i=new kt;for await(const d of n)t!=null&&!d.includes(t)||(i.push(d),i.length>e&&i.shift());return i.toArray().join(Re.EOL)+Re.EOL}finally{try{n&&n.close()}catch(i){console.error("Error closing readline interface:",i)}try{r&&!r.destroyed&&r.destroy()}catch(i){console.error("Error destroying read stream:",i)}}}function F(o,e){if(!e.name.startsWith("Blob/")){let t=`${o}: wrong resource type: ${e.name}, expected: a resource of type that starts with 'Blob/'.`;throw e.name=="Blob"&&(t+=" If it's called from workflow, should a file be exported with 'file.exportFile' function?"),new V(t)}}const it=m.rsSchema({});function ot(o,e){const t=m.isPlTreeEntry(o)?e.accessor(o).node():m.isPlTreeEntryAccessor(o)?o.node():o;return m.makeResourceSnapshot(t,it)}function Dn(o,e,t){const r=k.relative(e,t);return`plblob+folder://${o.sign(r)}.${r}.blob`}function Nn(o,e,t){const r=k.relative(e,t);return`block-ui://${o.sign(r)}.${r}.uidir`}function $n(o,e,t){return st(o,e,t)}function vn(o,e,t){return st(o,e,t)}function st(o,e,t){const r=new URL(e),[n,i,d]=r.host.split(".");o.verify(i,n,`signature verification failed for url: ${e}, subfolder: ${i}`);const s=decodeURIComponent(r.pathname.slice(1));let c=Cn(k.join(t,`${i}`),s);return(r.pathname==""||r.pathname=="/")&&(c=k.join(c,"index.html")),k.resolve(c)}function Cn(o,e){const t=k.resolve(o,e),r=k.resolve(o);if(!t.startsWith(r))throw new Error("Path validation failed.");return t}class Fn{constructor(e,t,r,n,i,d,s){h(this,"counter",new g.CallersCounter);h(this,"change",new f.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"size",0);h(this,"url");h(this,"state");this.logger=e,this.signer=t,this.saveDir=r,this.path=n,this.rInfo=i,this.format=d,this.clientDownload=s}info(){return{rInfo:this.rInfo,format:this.format,path:this.path,done:this.done,size:this.size,error:this.error,taskHistory:this.state}}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(){try{const e=await this.downloadAndDecompress(this.signalCtl.signal);this.setDone(e),this.change.markChanged(`download and decompress for ${p.resourceIdToString(this.rInfo.id)} finished`),this.logger.info(`blob to URL task is done: ${p.stringifyWithResourceId(this.info())}`)}catch(e){if(this.logger.warn(`a error was produced: ${e} for blob to URL task: ${p.stringifyWithResourceId(this.info())}`),Bn(e)){this.setError(e),this.change.markChanged(`download and decompress for ${p.resourceIdToString(this.rInfo.id)} failed`),await ce(this.path);return}throw e}}async downloadAndDecompress(e){return this.state={},this.state.parentDir=k.dirname(this.path),await g.ensureDirExists(this.state.parentDir),this.state.fileExisted=await g.fileExists(this.path),this.state.fileExisted?await at(this.path):await this.clientDownload.withBlobContent(this.rInfo,{},{signal:e},async(r,n)=>(this.state.downloaded=!0,await g.createPathAtomically(this.logger,this.path,async i=>{switch(this.state.tempPath=i,this.state.archiveFormat=this.format,this.format){case"tar":await w.mkdir(i);const d=R.Writable.toWeb(le.extract(i));await r.pipeTo(d,{signal:e});return;case"tgz":await w.mkdir(i);const s=R.Transform.toWeb(Ee.createGunzip()),c=R.Writable.toWeb(le.extract(i));await r.pipeThrough(s,{signal:e}).pipeTo(c,{signal:e});return;case"zip":this.state.zipPath=this.path+".zip";const l=R.Writable.toWeb(S.createWriteStream(this.state.zipPath));await r.pipeTo(l,{signal:e}),this.state.zipPathCreated=!0,await It(this.state.zipPath,i,{filter:u=>!u.path.endsWith("/")}),this.state.zipDecompressed=!0,await S.promises.rm(this.state.zipPath),this.state.zipPathDeleted=!0;return;default:a.assertNever(this.format)}}),this.state.pathCreated=!0,n))}getURL(){if(this.done)return{url:g.notEmpty(this.url)};if(this.error)return{error:this.error}}setDone(e){this.done=!0,this.size=e,this.url=Dn(this.signer,this.saveDir,this.path)}setError(e){this.error=String(e)}abort(e){this.signalCtl.abort(new lt(e))}}async function at(o){const e=await w.readdir(o,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=k.join(o,r.name);return r.isDirectory()?await at(n):(await w.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function ce(o){await w.rm(o,{recursive:!0,force:!0})}let lt=class extends Error{constructor(){super(...arguments);h(this,"name","URLAborted")}};function Bn(o){return o instanceof lt||o instanceof A||o instanceof G||o instanceof j||(o==null?void 0:o.code)=="ENOENT"||o.name=="RpcError"&&(o.code=="NOT_FOUND"||o.code=="ABORTED")||String(o).includes("incorrect header check")}function W(o,e){return`id:${String(BigInt(o))}-${e}`}class En{constructor(e,t,r,n,i={cacheSoftSizeBytes:50*1024*1024,nConcurrentDownloads:50}){h(this,"idToDownload",new Map);h(this,"downloadQueue");h(this,"cache");this.logger=e,this.signer=t,this.clientDownload=r,this.saveDir=n,this.opts=i,this.downloadQueue=new g.TaskProcessor(this.logger,this.opts.nConcurrentDownloads,{type:"exponentialWithMaxDelayBackoff",initialDelay:1e4,maxDelay:3e4,backoffMultiplier:1.5,jitter:.5}),this.cache=new ke(this.opts.cacheSoftSizeBytes)}info(){return{saveDir:this.saveDir,opts:this.opts,idToDownloadSize:this.idToDownload.size,idToDownloadKeys:this.idToDownload.keys(),idToDownload:Array.from(this.idToDownload.entries()).map(([e,t])=>[e,t.info()])}}getPathForCustomProtocol(e){if(v.isFolderURL(e))return $n(this.signer,e,this.saveDir);throw new Error(`getPathForCustomProtocol: ${e} is invalid`)}extractArchiveAndGetURL(e,t,r){if(r===void 0)return f.Computable.make(s=>this.extractArchiveAndGetURL(e,t,s));const n=m.isPlTreeEntry(e)?ot(e,r):e,i=P.randomUUID();r.addOnDestroy(()=>this.releasePath(n.id,t,i));const d=this.extractArchiveAndGetURLNoCtx(n,t,r.watcher,i);if((d==null?void 0:d.url)===void 0&&r.markUnstable(`a path to the downloaded archive might be undefined. The current result: ${d}`),(d==null?void 0:d.error)!==void 0)throw d==null?void 0:d.error;return d==null?void 0:d.url}extractArchiveAndGetURLNoCtx(e,t,r,n){const i=this.idToDownload.get(W(e.id,t));if(i!=null)return i.attach(r,n),i.getURL();const d=this.setNewTask(r,e,t,n);return this.downloadQueue.push({fn:async()=>this.downloadUrl(d,n),recoverableErrorPredicate:s=>!be(s)}),d.getURL()}async downloadUrl(e,t){var r;await e.download(),((r=e.getURL())==null?void 0:r.url)!=null&&this.cache.addCache(e,t)}async releasePath(e,t,r){const n=this.idToDownload.get(W(e,t));if(n!=null)if(this.cache.existsFile(n.path)){const i=this.cache.removeFile(n.path,r);await Promise.all(i.map(async d=>{await ce(d.path),this.cache.removeCache(d),this.removeTask(d,`the task ${p.stringifyWithResourceId(d.info())} was removedfrom cache along with ${p.stringifyWithResourceId(i.map(s=>s.info()))}`)}))}else n.counter.dec(r)&&this.removeTask(n,`the task ${p.stringifyWithResourceId(n.info())} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.idToDownload.entries()).map(async([e,t])=>{await ce(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${p.stringifyWithResourceId(t.info())} was released when the driver was closed`)}))}setNewTask(e,t,r,n){const i=new Fn(this.logger,this.signer,this.saveDir,this.getFilePath(t.id,r),t,r,this.clientDownload);return i.attach(e,n),this.idToDownload.set(W(t.id,r),i),i}removeTask(e,t){e.abort(t),e.change.markChanged(`task for ${p.resourceIdToString(e.rInfo.id)} removed: ${t}`),this.idToDownload.delete(W(e.rInfo.id,e.format))}getFilePath(e,t){return b.join(this.saveDir,`${String(BigInt(e))}_${t}`)}}class ct{constructor(e,t,r,n,i,d){h(this,"change",new f.ChangeSource);h(this,"counter",new g.CallersCounter);h(this,"nMaxUploads");h(this,"nPartsWithThisUploadSpeed",0);h(this,"nPartsToIncreaseUpload",10);h(this,"uploadData");h(this,"progress");h(this,"failed");h(this,"alreadyExisted",!1);this.logger=e,this.clientBlob=t,this.clientProgress=r,this.maxNConcurrentPartsUpload=n,this.res=d,this.nMaxUploads=this.maxNConcurrentPartsUpload;const{uploadData:s,progress:c}=Wn(d,i);this.uploadData=s,this.progress=c}getProgress(e,t){if(this.incCounter(e,t),this.failed)throw this.logger.error(`Uploading terminally failed: ${this.progress.lastError}`),new Error(this.progress.lastError);return _n(this.progress)}shouldScheduleUpload(){return ht(this.progress)}async uploadBlobTask(){try{await dt(this.logger,this.clientBlob,this.res,this.uploadData,this.isComputableDone.bind(this),{nPartsWithThisUploadSpeed:this.nPartsWithThisUploadSpeed,nPartsToIncreaseUpload:this.nPartsToIncreaseUpload,currentSpeed:this.nMaxUploads,maxSpeed:this.maxNConcurrentPartsUpload}),this.change.markChanged(`blob upload for ${p.resourceIdToString(this.res.id)} finished`)}catch(e){if(this.setRetriableError(e),de(e)){this.logger.warn(`resource was deleted while uploading a blob: ${e}`),this.change.markChanged(`blob upload for ${p.resourceIdToString(this.res.id)} aborted, resource was deleted`),this.setDone(!0);return}if(this.logger.error(`error while uploading a blob: ${e}`),this.change.markChanged(`blob upload for ${p.resourceIdToString(this.res.id)} failed`),Le(e)){this.setTerminalError(e);return}throw zn(e)&&(this.nMaxUploads=An(this.logger,this.nMaxUploads,1)),e}}async updateStatus(){var e;try{const t=await this.clientProgress.getStatus(this.res,{timeout:1e4}),r=this.progress.status,n=Hn(this.alreadyExisted,On(t));this.progress.status=n,this.setDone(t.done),(t.done||this.progress.status.progress!=(r==null?void 0:r.progress))&&this.change.markChanged(`upload status for ${p.resourceIdToString(this.res.id)} changed`)}catch(t){if(this.setRetriableError(t),t.name=="RpcError"&&t.code=="DEADLINE_EXCEEDED"||(e=t==null?void 0:t.message)!=null&&e.includes("DEADLINE_EXCEEDED")){this.logger.warn("deadline exceeded while getting a status of BlobImport");return}if(de(t)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${t}, ${p.stringifyWithResourceId(this.res)}`),this.change.markChanged(`upload status for ${p.resourceIdToString(this.res.id)} changed, resource not found`),this.setDone(!0);return}this.logger.error(`retryable error while updating a status of BlobImport: ${t}`)}}setRetriableError(e){this.progress.lastError=String(e)}setTerminalError(e){this.progress.lastError=String(e),this.progress.done=!1,this.failed=!0}setDoneIfOutputSet(e){Mn(e)&&(this.setDone(!0),this.alreadyExisted=!0)}setDone(e){this.progress.done=e,e&&(this.progress.lastError=void 0)}incCounter(e,t){this.change.attachWatcher(e),this.counter.inc(t)}decCounter(e){return this.counter.dec(e)}isComputableDone(){return this.counter.isZero()}}async function dt(o,e,t,r,n,i){St(x(t),"the upload operation can be done only for BlobUploads");const d=1e4;if(n())return;const s=await e.initUpload(t,{timeout:d});o.info(`started to upload blob ${t.id}, parts overall: ${s.overall}, parts remained: ${s.toUpload.length}, number of concurrent uploads: ${i.currentSpeed}`);const c=l=>async u=>{n()||(await e.partUpload(t,r.localPath,BigInt(r.modificationTime),l,{timeout:d}),o.info(`uploaded chunk ${l}/${s.overall} of resource: ${t.id}`),i.nPartsWithThisUploadSpeed++,i.nPartsWithThisUploadSpeed>=i.nPartsToIncreaseUpload&&(i.nPartsWithThisUploadSpeed=0,i.currentSpeed=xn(o,i.currentSpeed,i.maxSpeed),u.setConcurrency(i.currentSpeed)))};await g.asyncPool(i.currentSpeed,s.toUpload.map(c)),!n()&&(await e.finalize(t,{timeout:d}),o.info(`uploading of resource ${t.id} finished.`))}function Wn(o,e){let t,r;return x(o)&&(r=B.parse(o.data),t=ut(e,r.localPath,r.pathSignature)),{uploadData:r,progress:{done:!1,status:void 0,isUpload:x(o),isUploadSignMatch:t,lastError:void 0}}}function ht(o){return o.isUpload&&(o.isUploadSignMatch??!1)}function _n(o){return o.done,o.isUpload,o.isUploadSignMatch,o.lastError,o.status&&(o.status.progress,o.status.bytesProcessed,o.status.bytesTotal),o}function Mn(o){return"blob"in o.fields?o.fields.blob!==void 0:o.fields.incarnation!==void 0}function x(o){return o.type.name.startsWith("BlobUpload")}function ut(o,e,t){try{return o.verify(e,t),!0}catch{return!1}}function On(o){return{progress:o.progress??0,bytesProcessed:Number(o.bytesProcessed),bytesTotal:Number(o.bytesTotal)}}function Hn(o,e){return o&&e.bytesTotal!=0&&e.bytesProcessed==0?{progress:1,bytesProcessed:Number(e.bytesTotal),bytesTotal:Number(e.bytesTotal)}:e}function de(o){return o.name=="RpcError"&&(o.code=="NOT_FOUND"||o.code=="ABORTED"||o.code=="ALREADY_EXISTS")}function Le(o){return o instanceof ge||o instanceof fe||o instanceof me}function zn(o){return o==null?void 0:o.message.includes("UND_ERR_HEADERS_TIMEOUT")}function xn(o,e,t){const r=Math.min(e+2,t);return r!=e&&o.info(`uploadTask.increaseConcurrency: increased from ${e} to ${r}`),r}function An(o,e,t){const r=Math.max(Math.round(e/2),t);return r!=e&&o.info(`uploadTask.decreaseConcurrency: decreased from ${e} to ${r}`),r}function pt(o,e){const t=m.isPlTreeEntry(o)?e.accessor(o).node():m.isPlTreeEntryAccessor(o)?o.node():o;return t.resourceType.name.startsWith("BlobUpload")?m.makeResourceSnapshot(t,Je):m.makeResourceSnapshot(t,Ze)}class jn{constructor(e,t,r,n,i={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=i,this.uploadQueue=new g.TaskProcessor(this.logger,1,{type:"exponentialWithMaxDelayBackoff",initialDelay:20,maxDelay:15e3,backoffMultiplier:1.5,jitter:.5}),this.hooks=new f.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:i.stopPollingDelay},(d,s)=>this.scheduleOnNextState(d,s))}getProgressId(e,t){if(t==null)return f.Computable.make(d=>this.getProgressId(e,d));const r=m.isPlTreeEntry(e)?pt(e,t):e,n=P.randomUUID();return t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(r.id,n)),this.getProgressIdNoCtx(t.watcher,r,n)}getProgressIdNoCtx(e,t,r){Vn("getProgressId",t.type);const n=this.idToProgress.get(t.id);if(n!=null)return n.setDoneIfOutputSet(t),n.getProgress(e,r);const i=new ct(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,i),i.shouldScheduleUpload()&&this.uploadQueue.push({fn:()=>i.uploadBlobTask(),recoverableErrorPredicate:d=>!Le(d)}),i.setDoneIfOutputSet(t),i.getProgress(e,r)}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 Be.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!Gn(t.progress)).map(([e,t])=>t)}}function Gn(o){var e;return o.done&&(((e=o.status)==null?void 0:e.progress)??0)>=1}function Vn(o,e){if(!e.name.startsWith("BlobUpload")&&!e.name.startsWith("BlobIndex"))throw new V(`${o}: wrong resource type: ${e.name}, expected: a resource of either type 'BlobUpload' or 'BlobIndex'.`)}class qn{constructor(e,t,r={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.logger=e,this.clientLogs=t,this.opts=r,this.hooks=new f.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:r.stopPollingDelay},(n,i)=>this.scheduleOnNextState(n,i))}getLastLogs(e,t,r){if(r==null)return f.Computable.make(s=>this.getLastLogs(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=P.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseLastLogs(n.id,i));const d=this.getLastLogsNoCtx(r.watcher,n,t,i);return r.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),d}getLastLogsNoCtx(e,t,r,n){se("getLastLogs",t.type);let i=this.idToLastLines.get(t.id);if(i==null){const s=new Ne(this.logger,this.clientLogs,t,r);this.idToLastLines.set(t.id,s),i=s}i.attach(e,n);const d=i.getLog();if(d.error!=null)throw d.error;return d.log}getProgressLog(e,t,r){if(r==null)return f.Computable.make(s=>this.getProgressLog(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=P.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseProgressLog(n.id,i));const d=this.getProgressLogNoCtx(r.watcher,n,t,i);return r.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),d}getProgressLogNoCtx(e,t,r,n){se("getProgressLog",t.type);let i=this.idToProgressLog.get(t.id);if(i==null){const s=new Ne(this.logger,this.clientLogs,t,1,r);this.idToProgressLog.set(t.id,s),i=s}i.attach(e,n);const d=i.getLog();if(d.error)throw d.error;return d.log}getLogHandle(e,t){if(t==null)return f.Computable.make(i=>this.getLogHandle(e,i));const r=m.treeEntryToResourceInfo(e,t),n=this.getLogHandleNoCtx(r);return t.markUnstable(`live_log:${p.resourceIdToString(r.id)}`),n}getLogHandleNoCtx(e){return se("getLogHandle",e.type),Xe(!0,e)}async lastLines(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(z(e),t,BigInt(r??0),n))}async readText(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(z(e),t,BigInt(r??0),n))}async tryWithNotFound(e,t){if(!H(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{const t=this.getAllLogs();await g.asyncPool(this.opts.nConcurrentGetLogs,t.map(r=>async()=>await r.update())),e.forEach(r=>r.resolve())}catch(t){console.error(t),e.forEach(r=>r.reject(t))}if(!this.keepRunning)break;await Be.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllLogs(){return Array.from(this.idToLastLines.entries()).concat(Array.from(this.idToProgressLog.entries())).map(([e,t])=>t)}}class Ne{constructor(e,t,r,n,i){h(this,"logs");h(this,"error");h(this,"change",new f.ChangeSource);h(this,"counter",new g.CallersCounter);this.logger=e,this.clientLogs=t,this.rInfo=r,this.lines=n,this.patternToSearch=i}getLog(){return{log:this.logs,error:this.error}}attach(e,t){this.change.attachWatcher(e),this.counter.inc(t)}release(e){return this.counter.dec(e)}async update(){try{const e=await this.clientLogs.lastLines(this.rInfo,this.lines,0n,this.patternToSearch),t=new TextDecoder().decode(e.data);this.logs!=t&&this.change.markChanged(`logs for ${p.resourceIdToString(this.rInfo.id)} updated`),this.logs=t,this.error=void 0;return}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.logs="",this.error=e,this.change.markChanged();return}throw this.logger.error(`Stream log lines for ${p.stringifyWithResourceId(this.rInfo.id)} failed, reason: ${e}`),e}}}function se(o,e){if(!e.name.startsWith("StreamWorkdir"))throw new V(`${o}: wrong resource type: ${e.name}, expected: a resource of type 'StreamWorkdir'.`)}class Jn{constructor(e,t,r){this.logger=e,this.logsStreamDriver=t,this.downloadDriver=r}getLastLogs(e,t,r){if(r===void 0)return f.Computable.make(i=>this.getLastLogs(e,t,i));const n=M(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(_(n))return this.downloadDriver.getLastLogs(n,t,r);try{return this.logsStreamDriver.getLastLogs(n,t,r)}catch(i){if(i.name=="RpcError"&&i.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${i}`);return}throw i}}getProgressLog(e,t,r){if(r===void 0)return f.Computable.make(i=>this.getProgressLog(e,t,i));const n=M(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(_(n))return this.downloadDriver.getProgressLog(n,t,r);try{return this.logsStreamDriver.getProgressLog(n,t,r)}catch(i){if(i.name=="RpcError"&&i.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${i}`);return}throw i}}getProgressLogWithInfo(e,t,r){if(r===void 0)return f.Computable.make(i=>this.getProgressLogWithInfo(e,t,i));const n=M(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(_(n))return{progressLine:this.downloadDriver.getProgressLog(n,t,r),live:!1};try{return{progressLine:this.logsStreamDriver.getProgressLog(n,t,r),live:!0}}catch(i){if(i.name=="RpcError"&&i.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log with info: ${i}`);return}throw i}}getLogHandle(e,t){if(t===void 0)return f.Computable.make(n=>this.getLogHandle(e,n));const r=M(t,e);if(r===void 0){t.markUnstable("no stream in stream manager");return}return _(r)?this.downloadDriver.getLogHandle(r,t):this.logsStreamDriver.getLogHandle(r,t)}async lastLines(e,t,r,n){return H(e)?await this.logsStreamDriver.lastLines(e,t,r,n):await this.downloadDriver.lastLines(e,t,r,n)}async readText(e,t,r,n){return H(e)?await this.logsStreamDriver.readText(e,t,r,n):await this.downloadDriver.readText(e,t,r,n)}}function _(o){return!o.type.name.startsWith("StreamWorkdir")}function M(o,e){var t;return(t=o.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}class Zn{constructor(e,t,r,n,i){h(this,"counter",new g.CallersCounter);h(this,"change",new f.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"size",0);this.logger=e,this.path=t,this.url=r,this.signer=n,this.saveDir=i}info(){return{url:this.url.toString(),path:this.path,done:this.done,size:this.size,error:this.error}}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),this.change.markChanged(`download of ${this.url} finished`)}catch(r){if(r instanceof $e||r instanceof A){this.setError(r),this.change.markChanged(`download of ${this.url} failed`),await he(this.path);return}throw r}}async downloadAndUntar(e,t,r){return await g.ensureDirExists(b.dirname(this.path)),await g.fileExists(this.path)?await gt(this.path):await e.withContent(this.url.toString(),{},{signal:r},async(i,d)=>{let s=i;if(t){const c=R.Transform.toWeb(Ee.createGunzip());s=i.pipeThrough(c,{signal:r})}return await g.createPathAtomically(this.logger,this.path,async c=>{await w.mkdir(c);const l=R.Writable.toWeb(le.extract(c));await s.pipeTo(l,{signal:r})}),d})}getUrl(){if(this.done)return{url:Nn(this.signer,this.saveDir,g.notEmpty(this.path))};if(this.error)return{error:this.error}}setDone(e){this.done=!0,this.size=e}setError(e){this.error=String(e)}abort(e){this.signalCtl.abort(new $e(e))}}class $e extends Error{constructor(){super(...arguments);h(this,"name","URLAborted")}}async function gt(o){const e=await w.readdir(o,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=b.join(o,r.name);return r.isDirectory()?await gt(n):(await w.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function he(o){await w.rm(o,{recursive:!0,force:!0})}class Qn{constructor(e,t,r,n,i={cacheSoftSizeBytes:1*1024*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.signer=n,this.opts=i,this.downloadQueue=new g.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new ke(this.opts.cacheSoftSizeBytes),this.downloadHelper=new Me(t)}getUrl(e,t){if(t===void 0)return f.Computable.make(i=>this.getUrl(e,i));const r=P.randomUUID();t.addOnDestroy(()=>this.releasePath(e,r));const n=this.getUrlNoCtx(e,t.watcher,r);return(n==null?void 0:n.url)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${n}`),n}getUrlNoCtx(e,t,r){const n=e.toString(),i=this.urlToDownload.get(n);if(i!==void 0)return i.attach(t,r),i.getUrl();const d=this.setNewTask(t,e,r);return this.downloadQueue.push({fn:async()=>this.downloadUrl(d,r),recoverableErrorPredicate:s=>!0}),d.getUrl()}getPathForBlockUI(e){if(!v.isBlockUIURL(e))throw new Error(`getPathForBlockUI: ${e} is invalid`);return vn(this.signer,e,this.saveDir)}async downloadUrl(e,t){var r;await e.download(this.downloadHelper,this.opts.withGunzip),((r=e.getUrl())==null?void 0:r.url)!==void 0&&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 i=this.cache.removeFile(n.path,t);await Promise.all(i.map(async d=>{await he(d.path),this.cache.removeCache(d),this.removeTask(d,`the task ${p.stringifyWithResourceId(d.info())} was removedfrom cache along with ${p.stringifyWithResourceId(i.map(s=>s.info()))}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${p.stringifyWithResourceId(n.info())} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.urlToDownload.entries()).map(async([e,t])=>{await he(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${p.stringifyWithResourceId(t.info())} was released when the driver was closed`)}))}setNewTask(e,t,r){const n=new Zn(this.logger,this.getFilePath(t),t,this.signer,this.saveDir);return n.attach(e,r),this.urlToDownload.set(t.toString(),n),n}removeTask(e,t){e.abort(t),e.change.markChanged(`task for url ${e.url} removed: ${t}`),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=P.createHash("sha256").update(e.toString()).digest("hex");return b.join(this.saveDir,t)}}function ae(o,e){return`index://index/${encodeURIComponent(JSON.stringify({storageId:o,path:e}))}`}function Kn(o,e,t,r){const n={localPath:o,pathSignature:e.sign(o),sizeBytes:String(t),modificationTime:String(r)};return`upload://upload/${encodeURIComponent(JSON.stringify(n))}`}function Xn(o){const e=new URL(o);return B.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function Yn(o){const e=new URL(o);return Te.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function ve(o){if(ni(o))return oi(o);if(ei(o))return ri(o);g.assertNever(o)}const ft=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function ei(o){return ft.test(o)}function ti(o,e){return`local://${o}/${encodeURIComponent(e)}`}function ri(o){const e=o.match(ft);if(e==null)throw new Error(`Local list handle wasn't parsed: ${o}`);const{name:t,path:r}=e.groups;return{rootPath:decodeURIComponent(r),name:t,isRemote:!1}}const wt=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function ni(o){return wt.test(o)}function ii(o,e){return`remote://${o}/${BigInt(e)}`}function oi(o){const e=o.match(wt);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${o}`);const{name:t,resourceId:r}=e.groups;return{id:p.bigintToResourceId(BigInt(r)),type:si(t),name:t,isRemote:!0}}function si(o){return{name:`LS/${o}`,version:"1"}}async function mt(){const o=Fe.homedir();if(k.sep=="/")return[{name:"local",root:"/",initialPath:o}];{const t=k.parse(o).root.replaceAll(":\\","");try{return(await Dt.promisify(Nt.exec)("wmic logicaldisk get name")).stdout.split(`\r
2
- `).filter(i=>i.includes(":")).map(i=>i.trim().replaceAll(":","")).map(i=>{const d=i==t;return{name:`local_disk_${i}`,root:`${i}:\\`,initialPath:d?o:`${i}:\\`}})}catch{return[{name:`local_disk_${t}`,root:`${t}:\\`,initialPath:o}]}}}class Pe{constructor(e,t,r,n,i,d,s){this.logger=e,this.lsClient=t,this.storageIdToResourceId=r,this.signer=n,this.virtualStoragesMap=i,this.localProjectionsMap=d,this.openFileDialogCallback=s}async getLocalFileContent(e,t){const r=await this.tryResolveLocalFileHandle(e);if(t)throw new Error("Range request not yet supported.");return await w.readFile(r)}async getLocalFileSize(e){const t=await this.tryResolveLocalFileHandle(e);return(await w.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(v.isImportFileHandleIndex(e)){const t=Yn(e),r=this.localProjectionsMap.get(t.storageId);if(!r)throw new Error(`Storage ${t.storageId} is not mounted locally.`);return b.join(r.localPath,t.path)}else{const t=Xn(e);this.signer.verify(t.localPath,t.pathSignature,"Failed to validate local file handle signature.");const r=t.localPath,n=await w.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){N(e);for(const r of this.localProjectionsMap.values())if(e.startsWith(r.localPath)){const n=r.localPath===""?e:b.relative(r.localPath,e);return ae(r.storageId,n)}const t=await w.stat(e,{bigint:!0});return Kn(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){const e=[...this.virtualStoragesMap.values()].map(n=>({name:n.name,handle:ti(n.name,n.root),initialFullPath:n.initialPath})),r=Object.entries(this.storageIdToResourceId).map(([n,i])=>({name:n,handle:ii(n,i),initialFullPath:"",isInitialPathHome:!1})).filter(n=>n.name!=="root");return[...e,...r]}async listFiles(e,t){const r=ve(e);if(r.isRemote)return{entries:(await this.lsClient.list(r,t)).items.map(s=>({type:s.isDir?"dir":"file",name:s.name,fullPath:s.fullName,handle:ae(r.name,s.fullName)}))};b.sep==="/"&&t===""&&(t="/"),r.rootPath===""&&N(t);const n=b.isAbsolute(t)?t:b.join(r.rootPath,t),i=[];for await(const d of await w.opendir(n)){if(!d.isFile()&&!d.isDirectory())continue;const s=b.join(n,d.name);i.push({type:d.isFile()?"file":"dir",name:d.name,fullPath:s,handle:await this.getLocalFileHandle(s)})}return{entries:i}}async listRemoteFilesWithAdditionalInfo(e,t){const r=ve(e);if(!r.isRemote)throw new Error(`Storage ${r.name} is not remote`);return{entries:(await this.lsClient.list(r,t)).items.map(i=>({type:i.isDir?"dir":"file",name:i.name,fullPath:i.fullName,handle:ae(r.name,i.fullName),size:Number(i.size)}))}}async fileToImportHandle(e){throw new Error("Not implemented. This method must be implemented and intercepted in desktop preload script.")}static async init(e,t,r,n,i,d){const s=Ge(t,e);d||(d=await mt());for(const u of d)N(u.root);for(const u of n)u.localPath!==""&&N(u.localPath);const c=new Map(d.map(u=>[u.name,u])),l=new Map(n.map(u=>[u.storageId,u]));if(new Set([...c.keys(),...l.keys()]).size!==c.size+l.size)throw new Error("Intersection between local projection storage ids and virtual storages names detected.");return new Pe(e,s,await ai(t),r,c,l,i)}}async function ai(o){return o.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),r=await e.getResourceData(t,!0);return li(r)})}function li(o){return Object.fromEntries(o.fields.filter(e=>e.type=="Dynamic"&&p.isNotNullResourceId(e.value)).map(e=>[e.name.substring(8),e.value]))}exports.ClientDownload=Oe;exports.ClientLogs=je;exports.ClientLs=Ae;exports.ClientProgress=_e;exports.ClientUpload=We;exports.DefaultVirtualLocalStorages=mt;exports.DownloadBlobToURLDriver=En;exports.DownloadDriver=Ue;exports.DownloadUrlDriver=Qn;exports.DownloadableBlobSnapshot=it;exports.ImportFileHandleData=on;exports.ImportFileHandleIndexData=Te;exports.ImportFileHandleUploadData=B;exports.IndexResourceSnapshot=Ze;exports.LogsDriver=Jn;exports.LogsStreamDriver=qn;exports.LsDriver=Pe;exports.MTimeError=ge;exports.NetworkError=we;exports.NoFileForUploading=me;exports.OnDemandBlobResourceSnapshot=qe;exports.UnexpectedEOF=fe;exports.UnknownStorageError=G;exports.Updater=Ke;exports.UploadDriver=jn;exports.UploadResourceSnapshot=Je;exports.UploadTask=ct;exports.WrongLocalFileUrl=j;exports.WrongResourceTypeError=V;exports.createDownloadClient=Kr;exports.createLogsClient=Xr;exports.createLsFilesClient=Ge;exports.createUploadBlobClient=en;exports.createUploadProgressClient=Yr;exports.getFullPath=ze;exports.getSize=ye;exports.isMyUpload=ht;exports.isResourceWasDeletedError=de;exports.isSignMatch=ut;exports.isUpload=x;exports.makeBlobImportSnapshot=pt;exports.makeDownloadableBlobSnapshot=ot;exports.newLocalStorageIdsToRoot=xe;exports.nonRecoverableError=Le;exports.parseLocalUrl=He;exports.uploadBlob=dt;exports.validateAbsolute=N;
1
+ "use strict";var Tt=Object.defineProperty;var bt=(o,e,t)=>e in o?Tt(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var h=(o,e,t)=>bt(o,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@milaboratories/pl-client"),kt=require("node:fs/promises"),Ce=require("undici"),T=require("@protobuf-ts/runtime-rpc"),a=require("@protobuf-ts/runtime"),g=require("@milaboratories/ts-helpers"),S=require("node:fs"),U=require("node:path"),I=require("node:stream"),ue=require("node:stream/consumers"),f=require("@milaboratories/computable"),v=require("@milaboratories/pl-model-common"),m=require("@milaboratories/pl-tree"),Ut=require("denque"),R=require("node:crypto"),Fe=require("node:os"),Lt=require("node:readline/promises"),L=require("zod"),Pt=require("@milaboratories/helpers"),Rt=require("node:zlib"),It=require("tar-fs"),St=require("decompress"),Be=require("node:timers/promises"),Dt=require("node:assert"),Nt=require("node:util"),$t=require("node:child_process");function D(o){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const t in o)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(o,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>o[t]})}}return e.default=o,Object.freeze(e)}const w=D(kt),pe=D(S),k=D(U),Re=D(Fe),vt=D(Lt),Ee=D(Rt),le=D(It);class Ct extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ct;class Ft extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ft;class Bt extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Et=new Bt;class Wt extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.partsCount=e.uint64().toBigInt();break;case 2:if(c===a.WireType.LengthDelimited)for(let P=e.int32()+e.pos;e.pos<P;)i.uploadedParts.push(e.uint64().toBigInt());else i.uploadedParts.push(e.uint64().toBigInt());break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){if(e.partsCount!==0n&&t.tag(1,a.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,a.WireType.LengthDelimited).fork();for(let i=0;i<e.uploadedParts.length;i++)t.uint64(e.uploadedParts[i]);t.join()}let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const _t=new Wt;class Mt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Mt;class Ot extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.bytesProcessed=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,a.WireType.Varint).int64(e.bytesProcessed);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ht=new Ot;class zt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const xt=new zt;class At extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new At;class jt extends a.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},{no:4,name:"is_internal_use",kind:"scalar",T:8}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.partNumber=0n,t.uploadedPartSize=0n,t.isInternalUse=!1,e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.partNumber=e.uint64().toBigInt();break;case 3:i.uploadedPartSize=e.uint64().toBigInt();break;case 4:i.isInternalUse=e.bool();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,a.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,a.WireType.Varint).uint64(e.uploadedPartSize),e.isInternalUse!==!1&&t.tag(4,a.WireType.Varint).bool(e.isInternalUse);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Gt=new jt;class Vt extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.name=e.string();break;case 2:i.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Z=new Vt;class qt extends a.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:2,T:()=>Z},{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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.uploadUrl=e.string();break;case 2:i.method=e.string();break;case 3:i.headers.push(Z.internalBinaryRead(e,e.uint32(),r));break;case 4:i.chunkStart=e.uint64().toBigInt();break;case 5:i.chunkEnd=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.uploadUrl!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.method);for(let i=0;i<e.headers.length;i++)Z.internalBinaryWrite(e.headers[i],t.tag(3,a.WireType.LengthDelimited).fork(),r).join();e.chunkStart!==0n&&t.tag(4,a.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,a.WireType.Varint).uint64(e.chunkEnd);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Jt=new qt;class Zt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Zt;class Qt extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Kt=new Qt;class Xt extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Yt=new Xt,Q=new T.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:Et,O:_t},{name:"GetPartURL",options:{},I:Gt,O:Jt},{name:"UpdateProgress",options:{},I:Ht,O:xt},{name:"Finalize",options:{},I:Kt,O:Yt}]);class er{constructor(e){h(this,"typeName",Q.typeName);h(this,"methods",Q.methods);h(this,"options",Q.options);this._transport=e}init(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}getPartURL(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}updateProgress(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}finalize(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}}class ge extends Error{constructor(){super(...arguments);h(this,"name","MTimeError")}}class fe extends Error{constructor(){super(...arguments);h(this,"name","UnexpectedEOF")}}class we extends Error{constructor(){super(...arguments);h(this,"name","NetworkError")}}class me extends Error{constructor(){super(...arguments);h(this,"name","NoFileForUploading")}}class We{constructor(e,t,r,n){h(this,"grpcClient");this.httpClient=t,this.logger=n,this.grpcClient=e.createGrpcClientProvider(i=>new er(i))}close(){}async initUpload({id:e,type:t},r){const n=await this.grpcInit(e,t,r);return{overall:n.partsCount,toUpload:this.partsToUpload(n.partsCount,n.uploadedParts)}}async partUpload({id:e,type:t},r,n,i,d){const s=await this.grpcGetPartUrl({id:e,type:t},i,0n,d),c=await tr(r,s.chunkStart,s.chunkEnd);await nr(r,n);const l=Number(s.chunkEnd-s.chunkStart);if(c.length!==l)throw new Error(`Chunk size mismatch: expected ${l} bytes, but read ${c.length} bytes from file`);const u=Object.fromEntries(s.headers.map(({name:y,value:C})=>[y,C])),P=Object.keys(u).find(y=>y.toLowerCase()==="content-length");if(P){const y=Number(u[P]);if(y!==l)throw new Error(`Content-Length mismatch: expected ${l}, but got ${y} in headers`)}try{const{body:y,statusCode:C,headers:J}=await Ce.request(s.uploadUrl,{dispatcher:this.httpClient,body:c,headersTimeout:6e4,bodyTimeout:6e4,reset:!0,headers:u,method:s.method.toUpperCase()}),yt=await y.text();ir(C,yt,J,s)}catch(y){throw y instanceof we?y:new Error(`partUpload: error ${JSON.stringify(y)} happened while trying to do part upload to the url ${s.uploadUrl}, headers: ${JSON.stringify(s.headers)}`)}await this.grpcUpdateProgress({id:e,type:t},BigInt(s.chunkEnd-s.chunkStart),d)}async finalize(e,t){return await this.grpcFinalize(e,t)}partsToUpload(e,t){const r=[],n=new Set(t);for(let i=1n;i<=e;i++)n.has(i)||r.push(i);return r}async grpcInit(e,t,r){return await this.grpcClient.get().init({resourceId:e},p.addRTypeToMetadata(t,r)).response}async grpcGetPartUrl({id:e,type:t},r,n,i){return await this.grpcClient.get().getPartURL({resourceId:e,partNumber:r,uploadedPartSize:n,isInternalUse:!1},p.addRTypeToMetadata(t,i)).response}async grpcUpdateProgress({id:e,type:t},r,n){await this.grpcClient.get().updateProgress({resourceId:e,bytesProcessed:r},p.addRTypeToMetadata(t,n)).response}async grpcFinalize({id:e,type:t},r){return await this.grpcClient.get().finalize({resourceId:e},p.addRTypeToMetadata(t,r)).response}}async function tr(o,e,t){let r;try{r=await w.open(o);const n=Number(t-e),i=Number(e),d=Buffer.alloc(n),s=await rr(r,d,n,i);return d.subarray(0,s)}catch(n){throw n&&typeof n=="object"&&"code"in n&&n.code=="ENOENT"?new me(`there is no file ${o} for uploading`):n}finally{await(r==null?void 0:r.close())}}async function rr(o,e,t,r){let n=0;for(;n<t;){const{bytesRead:i}=await o.read(e,n,t-n,r+n);if(i===0)throw new fe("file ended earlier than expected.");n+=i}return n}async function nr(o,e){const t=BigInt(Math.floor((await w.stat(o)).mtimeMs/1e3));if(t>e)throw new ge(`file was modified, expected mtime: ${e}, got: ${t}.`)}function ir(o,e,t,r){if(o!=200)throw new we(`response is not ok, status code: ${o}, body: ${e}, headers: ${JSON.stringify(t)}, url: ${r.uploadUrl}`)}class or extends a.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=a.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 i=Math.abs(e.nanos).toString();i="0".repeat(9-i.length)+i,i.substring(3)==="000000"?i=i.substring(0,3):i.substring(6)==="000"&&(i=i.substring(0,6)),n+="."+i}return n+"s"}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+a.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[,i,d,s]=n,c=a.PbLong.from(i+d);if(c.toNumber()>315576e6||c.toNumber()<-315576e6)throw new Error("Unable to parse Duration from JSON string. Value out of range.");if(r.seconds=c.toBigInt(),typeof s=="string"){let l=i+s+"0".repeat(9-s.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.seconds=e.int64().toBigInt();break;case 2:i.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,a.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,a.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const H=new or;class sr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new sr;class ar extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.progress=e.float();break;case 2:i.bytesProcessed=e.uint64().toBigInt();break;case 3:i.bytesTotal=e.uint64().toBigInt();break;case 4:i.done=e.bool();break;case 5:i.name=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.progress!==0&&t.tag(1,a.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,a.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,a.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,a.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,a.WireType.LengthDelimited).string(e.name);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const $=new ar;class lr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new lr;class cr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const dr=new cr;class hr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>$}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.report=$.internalBinaryRead(e,e.uint32(),r,i.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.report&&$.internalBinaryWrite(e.report,t.tag(1,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ur=new hr;class pr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new pr;class gr extends a.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:()=>H}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.updateInterval=H.internalBinaryRead(e,e.uint32(),r,i.updateInterval);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.updateInterval&&H.internalBinaryWrite(e.updateInterval,t.tag(2,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const fr=new gr;class wr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>$}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.report=$.internalBinaryRead(e,e.uint32(),r,i.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.report&&$.internalBinaryWrite(e.report,t.tag(1,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const mr=new wr,K=new T.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:dr,O:ur},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:fr,O:mr}]);class yr{constructor(e){h(this,"typeName",K.typeName);h(this,"methods",K.methods);h(this,"options",K.options);this._transport=e}getStatus(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}realtimeStatus(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return T.stackIntercept("serverStreaming",this._transport,r,n,e)}}class _e{constructor(e,t,r,n){h(this,"grpcClient");this.client=r,this.logger=n,this.grpcClient=e.createGrpcClientProvider(i=>new yr(i))}close(){}async getStatus({id:e,type:t},r){const n=await this.grpcClient.get().getStatus({resourceId:e},p.addRTypeToMetadata(t,r)),i=g.notEmpty(n.response.report);return{done:i.done,progress:i.progress,bytesProcessed:String(i.bytesProcessed),bytesTotal:String(i.bytesTotal)}}async*realtimeStatus({id:e,type:t},r=100,n){n=p.addRTypeToMetadata(t,n);const i=Math.floor(r/1e3),d=(r-i*1e3)*1e6,s=H.create({seconds:BigInt(i),nanos:d});try{const{responses:c}=this.grpcClient.get().realtimeStatus({resourceId:e,updateInterval:s},n);yield*c}catch(c){throw this.logger.warn("Failed to get realtime status"+String(c)),c}}}class j extends Error{constructor(){super(...arguments);h(this,"name","NetworkError400")}}class Me{constructor(e){this.httpClient=e}async withContent(e,t,r,n){const i={...t};r.range&&(i.Range=`bytes=${r.range.from}-${r.range.to-1}`);const{statusCode:d,body:s,headers:c}=await Ce.request(e,{dispatcher:this.httpClient,headers:i,signal:r.signal}),l=I.Readable.toWeb(s);let u=!1;try{await Tr(d,l,e);const P=Number(c["content-length"]),y=await n(l,P);return u=!0,y}catch(P){if(!u&&!l.locked)try{await l.cancel()}catch{}throw P}}}async function Tr(o,e,t){if(o!=200&&o!=206){const r=(await ue.text(e)).substring(0,1e3);throw 400<=o&&o<500?new j(`Http error: statusCode: ${o} url: ${t.toString()}, beginning of body: ${r}`):new Error(`Http error: statusCode: ${o} url: ${t.toString()}`)}}function N(o){if(!U.isAbsolute(o))throw new Error(`Path ${o} is not absolute.`);return o}class br extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new br;class kr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new kr;class Ur extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"is_internal_use",kind:"scalar",T:8}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.isInternalUse=!1,e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.isInternalUse=e.bool();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.isInternalUse!==!1&&t.tag(2,a.WireType.Varint).bool(e.isInternalUse);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Lr=new Ur;class Pr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.name=e.string();break;case 2:i.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const X=new Pr;class Rr extends a.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:2,T:()=>X}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.downloadUrl=e.string();break;case 2:i.headers.push(X.internalBinaryRead(e,e.uint32(),r));break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.downloadUrl!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.downloadUrl);for(let i=0;i<e.headers.length;i++)X.internalBinaryWrite(e.headers[i],t.tag(2,a.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ir=new Rr,Y=new T.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:Lr,O:Ir}]);class Sr{constructor(e){h(this,"typeName",Y.typeName);h(this,"methods",Y.methods);h(this,"options",Y.options);this._transport=e}getDownloadURL(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}}class Oe{constructor(e,t,r,n){h(this,"grpcClient");h(this,"remoteFileDownloader");h(this,"localStorageIdsToRoot");h(this,"localFileReadLimiter",new g.ConcurrencyLimitingExecutor(32));this.httpClient=t,this.logger=r,this.grpcClient=e.createGrpcClientProvider(i=>new Sr(i)),this.remoteFileDownloader=new Me(t),this.localStorageIdsToRoot=xe(n)}close(){}async withBlobContent(e,t,r,n){const{downloadUrl:i,headers:d}=await this.grpcGetDownloadUrl(e,t,r.signal),s=Object.fromEntries(d.map(({name:c,value:l})=>[c,l]));return this.logger.info(`download blob ${p.stringifyWithResourceId(e)} from url ${i}, ops: ${JSON.stringify(r)}`),Nr(i)?await this.withLocalFileContent(i,r,n):await this.remoteFileDownloader.withContent(i,s,r,n)}async withLocalFileContent(e,t,r){const{storageId:n,relativePath:i}=He(e),d=ze(n,this.localStorageIdsToRoot,i);return await this.localFileReadLimiter.run(async()=>{var u,P;const s={start:(u=t.range)==null?void 0:u.from,end:((P=t.range)==null?void 0:P.to)!==void 0?t.range.to-1:void 0};let c,l=!1;try{const y=await w.stat(d);c=pe.createReadStream(d,s);const C=I.Readable.toWeb(c),J=await r(C,y.size);return l=!0,J}catch(y){throw!l&&c&&!c.destroyed&&c.destroy(),y}})}async grpcGetDownloadUrl({id:e,type:t},r,n){const i=r??{};return i.abort=n,await this.grpcClient.get().getDownloadURL({resourceId:e,isInternalUse:!1},p.addRTypeToMetadata(t,i)).response}}function He(o){const e=new URL(o);if(e.pathname=="")throw new G(`url for local filepath ${o} does not match url scheme`);return{storageId:e.host,relativePath:decodeURIComponent(e.pathname.slice(1))}}function ze(o,e,t){const r=e.get(o);if(r===void 0)throw new V(`Unknown storage location: ${o}`);return r===""?t:k.join(r,t)}const Dr="storage://";function Nr(o){return o.startsWith(Dr)}class G extends Error{constructor(){super(...arguments);h(this,"name","WrongLocalFileUrl")}}class V extends Error{constructor(){super(...arguments);h(this,"name","UnknownStorageError")}}function xe(o){const e=new Map;for(const t of o)t.localPath!==""&&N(t.localPath),e.set(t.storageId,t.localPath);return e}class $r extends a.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=a.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(a.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),r=e.getTime();return t.seconds=a.PbLong.from(Math.floor(r/1e3)).toBigInt(),t.nanos=(r%1e3+(r<0&&r%1e3!==0?1e3:0))*1e6,t}internalJsonWrite(e,t){let r=a.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 i=(e.nanos+1e9).toString().substring(1);i.substring(3)==="000000"?n="."+i.substring(0,3)+"Z":i.substring(6)==="000"?n="."+i.substring(0,6)+"Z":n="."+i+"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 "+a.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 i=Date.parse(n[1]+"-"+n[2]+"-"+n[3]+"T"+n[4]+":"+n[5]+":"+n[6]+(n[8]?n[8]:"Z"));if(Number.isNaN(i))throw new Error("Unable to parse Timestamp from JSON. Invalid value.");if(i<Date.parse("0001-01-01T00:00:00Z")||i>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=a.PbLong.from(i/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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.seconds=e.int64().toBigInt();break;case 2:i.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,a.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,a.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ee=new $r;class vr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new vr;class Cr extends a.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:()=>ee},{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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.name=e.string();break;case 2:i.size=e.uint64().toBigInt();break;case 3:i.isDir=e.bool();break;case 10:i.fullName=e.string();break;case 11:i.directory=e.string();break;case 12:i.lastModified=ee.internalBinaryRead(e,e.uint32(),r,i.lastModified);break;case 13:i.version=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,a.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,a.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,a.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,a.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,a.WireType.LengthDelimited).string(e.directory),e.lastModified&&ee.internalBinaryWrite(e.lastModified,t.tag(12,a.WireType.LengthDelimited).fork(),r).join(),e.version!==""&&t.tag(13,a.WireType.LengthDelimited).string(e.version);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const te=new Cr;class Fr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Fr;class Br extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.location=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.location);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Er=new Br;class Wr extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:2,T:()=>te},{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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.items.push(te.internalBinaryRead(e,e.uint32(),r));break;case 2:i.delimiter=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){for(let i=0;i<e.items.length;i++)te.internalBinaryWrite(e.items[i],t.tag(1,a.WireType.LengthDelimited).fork(),r).join();e.delimiter!==""&&t.tag(2,a.WireType.LengthDelimited).string(e.delimiter);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const _r=new Wr,re=new T.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:Er,O:_r}]);class Mr{constructor(e){h(this,"typeName",re.typeName);h(this,"methods",re.methods);h(this,"options",re.options);this._transport=e}list(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}}class Ae{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=e.createGrpcClientProvider(r=>new Mr(r))}close(){}async list(e,t,r){return await this.grpcClient.get().list({resourceId:e.id,location:t},p.addRTypeToMetadata(e.type,r)).response}}class Or extends a.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Or;class Hr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 11:i.chunkSize=e.uint32();break;case 20:i.readLimit=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,a.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,a.WireType.Varint).int64(e.readLimit);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const zr=new Hr;class xr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 11:i.chunkSize=e.uint32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,a.WireType.Varint).uint32(e.chunkSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ar=new xr;class jr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 20:i.readLimit=e.int64().toBigInt();break;case 21:i.search=e.string();break;case 22:i.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,a.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,a.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,a.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Gr=new jr;class Vr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 20:i.readLimit=e.int64().toBigInt();break;case 21:i.search=e.string();break;case 22:i.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,a.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,a.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,a.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,a.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const qr=new Vr;class Jr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.resourceId=e.uint64().toBigInt();break;case 2:i.offset=e.int64().toBigInt();break;case 3:i.lineCount=e.int32();break;case 21:i.search=e.string();break;case 22:i.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,a.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,a.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,a.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,a.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,a.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Zr=new Jr;class Qr extends a.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&&a.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let i=n??this.create(),d=e.pos+t;for(;e.pos<d;){let[s,c]=e.tag();switch(s){case 1:i.data=e.bytes();break;case 2:i.size=e.uint64().toBigInt();break;case 3:i.newOffset=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${s} (wire type ${c}) for ${this.typeName}`);let u=e.skip(c);l!==!1&&(l===!0?a.UnknownFieldHandler.onRead:l)(this.typeName,i,s,c,u)}}return i}internalBinaryWrite(e,t,r){e.data.length&&t.tag(1,a.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,a.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,a.WireType.Varint).uint64(e.newOffset);let n=r.writeUnknownFields;return n!==!1&&(n==!0?a.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const F=new Qr,ne=new T.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:zr,O:F},{name:"ReadBinary",options:{},I:Ar,O:F},{name:"StreamText",serverStreaming:!0,options:{},I:Gr,O:F},{name:"ReadText",options:{},I:qr,O:F},{name:"LastLines",options:{},I:Zr,O:F}]);class Kr{constructor(e){h(this,"typeName",ne.typeName);h(this,"methods",ne.methods);h(this,"options",ne.options);this._transport=e}streamBinary(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return T.stackIntercept("serverStreaming",this._transport,r,n,e)}readBinary(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}streamText(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return T.stackIntercept("serverStreaming",this._transport,r,n,e)}readText(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}lastLines(e,t){const r=this.methods[4],n=this._transport.mergeOptions(t);return T.stackIntercept("unary",this._transport,r,n,e)}}class je{constructor(e,t,r){h(this,"grpcClient");this.httpClient=t,this.logger=r,this.grpcClient=e.createGrpcClientProvider(n=>new Kr(n))}close(){}async lastLines({id:e,type:t},r,n=0n,i,d){return(await this.grpcClient.get().lastLines({resourceId:e,lineCount:r,offset:n,search:i},p.addRTypeToMetadata(t,d))).response}async readText({id:e,type:t},r,n=0n,i,d){return(await this.grpcClient.get().readText({resourceId:g.notEmpty(e),readLimit:BigInt(r),offset:n,search:i},p.addRTypeToMetadata(t,d))).response}}function Xr(o,e,t){return e.getDriver({name:"DownloadBlob",init:(r,n,i)=>new Oe(n,i,o,t)})}function Yr(o,e){return o.getDriver({name:"StreamLogs",init:(t,r,n)=>new je(r,n,e)})}function en(o,e){return o.getDriver({name:"UploadProgress",init:(t,r,n)=>new _e(r,n,o,e)})}function tn(o,e){return o.getDriver({name:"UploadBlob",init:(t,r,n)=>new We(r,n,o,e)})}function Ge(o,e){return o.getDriver({name:"LsFiles",init:(t,r,n)=>new Ae(r,e)})}const rn=new g.ConcurrencyLimitingExecutor(32);async function Ie(o,e){return await rn.run(async()=>{const t={};e&&(t.start=e.from,t.end=e.to-1);let r;try{return r=pe.createReadStream(o,t),await ue.buffer(r)}catch(n){throw r&&!r.destroyed&&r.destroy(),n}})}const Ve=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function nn(o,e){return`blob+local://download/${o}#${e.sign(o)}`}function on(o){return!!o.match(Ve)}function ie(o,e){const t=o.match(Ve);if(t===null)throw new Error(`Local handle is malformed: ${o}, matches: ${t}`);const{path:r,signature:n}=t.groups;return e.verify(r,n,`Signature verification failed for: ${o}`),{path:r,signature:n}}const qe=m.rsSchema({kv:{"ctl/file/blobInfo":L.z.object({sizeBytes:L.z.coerce.number()})}});function ye(o,e){const t=o.kv["ctl/file/blobInfo"].sizeBytes;if(e){const r=e.to-e.from;if(r>t)throw new Error(`getSize: range (${JSON.stringify(e)}, newSize: ${r}) is greater than size (${t})`);return r}return t}const E=L.z.object({localPath:L.z.string(),pathSignature:L.z.string(),sizeBytes:L.z.string(),modificationTime:L.z.string()}),Te=L.z.object({storageId:L.z.string(),path:L.z.string()}),sn=L.z.union([E,Te]),Je=m.rsSchema({data:E,fields:{blob:!1}}),Ze=m.rsSchema({fields:{incarnation:!1}}),Qe=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.+)\/(?<resourceVersion>.+?)\/(?<resourceId>\d+?)\/(?<size>\d+?))#(?<signature>.*)$/;function an(o,e){let t=`${o.type.name}/${o.type.version}/${BigInt(o.id)}/${ye(o)}`;return`blob+remote://download/${t}#${e.sign(t)}`}function ln(o){return!!o.match(Qe)}function cn(o,e){const t=o.match(Qe);if(t===null)throw new Error(`Remote handle is malformed: ${o}, matches: ${t}`);const{content:r,resourceType:n,resourceVersion:i,resourceId:d,size:s,signature:c}=t.groups;return e.verify(r,c,`Signature verification failed for ${o}`),{info:{id:p.bigintToResourceId(BigInt(d)),type:{name:n,version:i}},size:Number(s)}}class q extends Error{constructor(){super(...arguments);h(this,"name","WrongResourceTypeError")}}class Ke{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}})())}}function Xe(o,e){return o?`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 Ye=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function z(o){return Ye.test(o)}const et=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function dn(o){return et.test(o)}function x(o){let e;if(z(o))e=o.match(Ye);else if(dn(o))e=o.match(et);else throw new Error(`Log handle is malformed: ${o}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${o}`);const{resourceType:t,resourceVersion:r,resourceId:n}=e.groups;return{id:p.bigintToResourceId(BigInt(n)),type:{name:t,version:r}}}function b(o){return`${BigInt(o)}`}function Se(o){return k.basename(o)}class hn{constructor(e,t,r,n,i){h(this,"change",new f.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"counter",new g.CallersCounter);h(this,"error");h(this,"done",!1);h(this,"size",0);h(this,"state",{});this.logger=e,this.clientDownload=t,this.rInfo=r,this.handle=n,this.path=i}info(){return{rInfo:this.rInfo,fPath:this.path,done:this.done,error:this.error,state:this.state}}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(){try{const e=await this.ensureDownloaded();this.setDone(e),this.change.markChanged(`blob download for ${p.resourceIdToString(this.rInfo.id)} finished`)}catch(e){throw this.logger.error(`download blob ${p.stringifyWithResourceId(this.rInfo)} failed: ${e}, state: ${JSON.stringify(this.state)}`),be(e)&&(this.setError(e),this.change.markChanged(`blob download for ${p.resourceIdToString(this.rInfo.id)} failed`),await w.rm(this.path,{force:!0})),e}}async ensureDownloaded(){this.signalCtl.signal.throwIfAborted(),this.state={},this.state.filePath=this.path,await g.ensureDirExists(k.dirname(this.state.filePath)),this.signalCtl.signal.throwIfAborted(),this.state.dirExists=!0;const e=await g.fileExists(this.state.filePath);if(this.signalCtl.signal.throwIfAborted(),e){this.state.fileExists=!0,this.logger.info(`a blob was already downloaded: ${this.state.filePath}`);const r=await w.stat(this.state.filePath);return this.signalCtl.signal.throwIfAborted(),this.state.fileSize=r.size,this.state.fileSize}return await this.clientDownload.withBlobContent(this.rInfo,{},{signal:this.signalCtl.signal},async(r,n)=>(this.state.fileSize=n,this.state.downloaded=!0,await g.createPathAtomically(this.logger,this.state.filePath,async i=>{const d=I.Writable.toWeb(S.createWriteStream(i,{flags:"wx"}));await r.pipeTo(d,{signal:this.signalCtl.signal}),this.state.tempWritten=!0}),this.state.done=!0,n))}abort(e){this.signalCtl.abort(new tt(e))}getBlob(){return this.done?{done:this.done,result:un(this.handle,this.size,this.error)}:{done:!1}}setDone(e){this.done=!0,this.size=e}setError(e){this.done=!0,this.error=e}}function be(o){return o instanceof tt||o instanceof j||o instanceof V||o instanceof G||(o==null?void 0:o.code)=="ENOENT"||o.name=="RpcError"&&(o.code=="NOT_FOUND"||o.code=="ABORTED")}class tt extends Error{constructor(){super(...arguments);h(this,"name","DownloadAborted")}}function un(o,e,t){return t?{ok:!1,error:t}:o?{ok:!0,value:{handle:o,size:e}}:{ok:!1,error:new Error("No file or handle provided")}}class ke{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 i=g.mapGet(this.cache,r);t+=i.size,e.push(i)}),e}addCache(e,t){const r=this.cache.get(e.path)==null;if(this.cache.set(e.path,e),e.counter.inc(t),e.size<0)throw new Error(`empty sizeBytes: ${e}`);r&&(this.totalSizeBytes+=e.size)}removeCache(e){this.cache.delete(e.path),this.totalSizeBytes-=e.size}}const pn=L.z.object({ranges:L.z.array(v.RangeBytes)}),gn=".ranges.json";function fn(o){return o+gn}async function wn(o,e){let t={ranges:[]};try{const r=await w.readFile(e,"utf8");t=pn.parse(JSON.parse(r))}catch(r){if(r instanceof SyntaxError||r instanceof L.z.ZodError){const n=`readRangesFile: the file ${e} was corrupted: ${r}`;throw o.error(n),new nt(n)}if(!(r instanceof Error&&"code"in r&&r.code==="ENOENT"))throw r}return rt(t),t}async function mn(o,e,t){await g.createPathAtomically(o,e,async r=>{await w.writeFile(r,JSON.stringify(t,null,2),{flag:"wx"})})}function rt(o){o.ranges.sort((e,t)=>e.from-t.from);for(let e=0;e<o.ranges.length-1;e++)o.ranges[e].to>=o.ranges[e+1].from&&(yn(o,e),e--)}function yn(o,e){const t=Math.min(o.ranges[e].from,o.ranges[e+1].from),r=Math.max(o.ranges[e].to,o.ranges[e+1].to);o.ranges.splice(e,2,{from:t,to:r})}function W(o){return o.ranges.reduce((e,t)=>e+t.to-t.from,0)}function Tn(o,e){for(const t of o.ranges)if(t.from<=e.from&&e.to<=t.to)return!0;return!1}function bn(o,e){return o.ranges.push(e),rt(o),o}async function kn(o,e,t){try{await(await w.open(e,"w")).close(),await Un(e,t)}catch(r){o.error(`Error creating file ${e} on platform ${t}: ${r}`)}}async function Un(o,e){e==="win32"&&await g.spawnAsync("fsutil",["sparse","setflag",`"${o}"`],{stdio:"pipe"})}async function Ln(o,e,t,r,n){await g.fileExists(t)||await kn(o,t,e);const i=await w.open(t,"r+");await i.write(r,0,r.length,n),await i.close()}class nt extends Error{constructor(){super(...arguments);h(this,"name","CorruptedRangesError")}}class Pn{constructor(e,t){this.logger=e,this.cacheDir=t}fPath(e){return U.join(this.cacheDir,fn(e))}async get(e){return await wn(this.logger,this.fPath(e))}async set(e,t){return await mn(this.logger,this.fPath(e),t)}async delete(e){await S.promises.rm(this.fPath(e))}}class Rn{constructor(e,t){h(this,"suffix",".sparse.bin");this.logger=e,this.cacheDir=t}async all(){return await g.ensureDirExists(this.cacheDir),(await S.promises.readdir(this.cacheDir)).filter(t=>t.endsWith(this.suffix))}async exists(e){return await g.fileExists(this.path(e))}path(e){return U.join(this.cacheDir,e+this.suffix)}async write(e,t,r){await g.ensureDirExists(this.cacheDir),await Ln(this.logger,process.platform,this.path(e),t,r)}async delete(e){await S.promises.rm(this.path(e))}}class In{constructor(e,t,r,n){h(this,"lock",new Pt.functions.AwaitLock);h(this,"keyToLastAccessTime",new Map);h(this,"size",0);this.logger=e,this.maxSize=t,this.ranges=r,this.storage=n}async reset(){await oe(this.lock,async()=>{await this.resetUnsafe()})}async get(e,t){return await oe(this.lock,async()=>await this.getUnsafe(e,t))}async set(e,t,r){await oe(this.lock,async()=>{await this.setUnsafe(e,t,r)})}async resetUnsafe(){this.size=0,this.keyToLastAccessTime=new Map;const e=new Date;for(const t of await this.storage.all()){const r=await this.ranges.get(t);this.size+=W(r),this.keyToLastAccessTime.set(t,e)}}async getUnsafe(e,t){if(await this.storage.exists(e)){this.keyToLastAccessTime.set(e,new Date);const r=await this.getRanges(e);return Tn(r,t)?this.storage.path(e):void 0}}async setUnsafe(e,t,r){await this.setWithoutEviction(e,t,r),await this.ensureEvicted()}async setWithoutEviction(e,t,r){if(t.to-t.from!==r.length)throw new Error(`SparseCache.set: trying to set ${e} with wrong range length: range: ${JSON.stringify(t)}, data: ${r.length}`);this.keyToLastAccessTime.set(e,new Date);const n=await this.getRanges(e);this.size-=W(n),await this.storage.write(e,r,t.from);const i=bn(n,t);this.size+=W(i),await this.ranges.set(e,i)}async ensureEvicted(){const e=g.mapEntries(this.keyToLastAccessTime);for(e.sort(([t,r],[n,i])=>i.getTime()-r.getTime());this.size>this.maxSize;){const t=e.pop();if(!t)break;const[r,n]=t,i=await this.getRanges(r);this.size-=W(i),this.rmKey(r)}}async getRanges(e){try{return await this.ranges.get(e)}catch(t){if(t instanceof nt)return await this.rmKey(e),await this.resetUnsafe(),await this.ranges.get(e);throw t}}async rmKey(e){await this.storage.delete(e),await this.ranges.delete(e),this.keyToLastAccessTime.delete(e)}}async function oe(o,e){try{return await o.acquireAsync(),await e()}finally{o.release()}}class Ue{constructor(e,t,r,n,i,d,s){h(this,"keyToDownload",new Map);h(this,"cache");h(this,"rangesCache");h(this,"downloadQueue");h(this,"keyToOnDemand",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.rangesCacheDir=i,this.signer=d,this.ops=s,this.cache=new ke(this.ops.cacheSoftSizeBytes);const c=new Pn(this.logger,this.rangesCacheDir),l=new Rn(this.logger,this.rangesCacheDir);this.rangesCache=new In(this.logger,this.ops.rangesCacheMaxSizeBytes,c,l),this.downloadQueue=new g.TaskProcessor(this.logger,s.nConcurrentDownloads),this.saveDir=k.resolve(n)}static async init(e,t,r,n,i,d,s){const c=new Ue(e,t,r,n,i,d,s);return await c.rangesCache.reset(),c}getDownloadedBlob(e,t){if(t===void 0)return f.Computable.make(d=>this.getDownloadedBlob(e,d));const r=m.treeEntryToResourceInfo(e,t),n=R.randomUUID();t.addOnDestroy(()=>this.releaseBlob(r,n));const i=this.getDownloadedBlobNoCtx(t.watcher,r,n);return i==null&&t.markUnstable("download blob is still undefined"),i}getDownloadedBlobNoCtx(e,t,r){B("getDownloadedBlob",t.type);const n=this.getOrSetNewTask(t,r);n.attach(e,r);const i=n.getBlob();if(i.done){if(i.result.ok)return i.result.value;throw i.result.error}}getOrSetNewTask(e,t){const r=b(e.id),n=this.keyToDownload.get(r);if(n)return n;const i=k.resolve(this.saveDir,r),d=new hn(this.logger,this.clientDownload,e,nn(i,this.signer),i);return this.keyToDownload.set(r,d),this.downloadQueue.push({fn:()=>this.downloadBlob(d,t),recoverableErrorPredicate:s=>!be(s)}),d}async downloadBlob(e,t){await e.download();const r=e.getBlob();r.done&&r.result.ok&&this.cache.addCache(e,t)}getOnDemandBlob(e,t){if(t===void 0)return f.Computable.make(d=>this.getOnDemandBlob(e,d));const r=m.isPlTreeEntry(e)?m.makeResourceSnapshot(e,qe,t):e,n=R.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(r.id,n)),this.getOnDemandBlobNoCtx(r,n)}getOnDemandBlobNoCtx(e,t){B("getOnDemandBlob",e.type);let r=this.keyToOnDemand.get(b(e.id));return r===void 0&&(r=new Sn(ye(e),an(e,this.signer)),this.keyToOnDemand.set(b(e.id),r)),r.attach(t),r.getHandle()}getLocalPath(e){const{path:t}=ie(e,this.signer);return t}async getContent(e,t){if(t&&v.validateRangeBytes(t,"getContent"),on(e))return await Ie(this.getLocalPath(e),t);if(ln(e)){const r=cn(e,this.signer),n=b(r.info.id),i=await this.rangesCache.get(n,t??{from:0,to:r.size});if(i)return await Ie(i,t);const d=await this.clientDownload.withBlobContent({id:r.info.id,type:r.info.type},void 0,{range:t},async s=>await ue.buffer(s));return await this.rangesCache.set(n,t??{from:0,to:r.size},d),d}throw new Error("Malformed remote handle")}getComputableContent(e,t){return t&&v.validateRangeBytes(t,"getComputableContent"),f.Computable.make(r=>this.getDownloadedBlob(e,r),{postprocessValue:r=>r?this.getContent(r.handle,t):void 0}).withStableType()}getLastLogs(e,t,r){if(r==null)return f.Computable.make(s=>this.getLastLogs(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=R.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n,i));const d=this.getLastLogsNoCtx(r.watcher,n,t,i);return d==null&&r.markUnstable("either a file was not downloaded or logs was not read"),d}getLastLogsNoCtx(e,t,r,n){B("getLastLogs",t.type);const i=this.getDownloadedBlobNoCtx(e,t,n);if(i==null)return;const{path:d}=ie(i.handle,this.signer);let s=this.idToLastLines.get(b(t.id));if(s==null){const l=new De(d,r);this.idToLastLines.set(b(t.id),l),s=l}const c=s.getOrSchedule(e);if(c.error)throw c.error;return c.log}getProgressLog(e,t,r){if(r==null)return f.Computable.make(s=>this.getProgressLog(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=R.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n,i));const d=this.getProgressLogNoCtx(r.watcher,n,t,i);return d===void 0&&r.markUnstable("either a file was not downloaded or a progress log was not read"),d}getProgressLogNoCtx(e,t,r,n){B("getProgressLog",t.type);const i=this.getDownloadedBlobNoCtx(e,t,n);if(i==null)return;const{path:d}=ie(i.handle,this.signer);let s=this.idToProgressLog.get(b(t.id));if(s==null){const l=new De(d,1,r);this.idToProgressLog.set(b(t.id),l),s=l}const c=s.getOrSchedule(e);if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return f.Computable.make(n=>this.getLogHandle(e,n));const r=m.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(r)}getLogHandleNoCtx(e){return B("getLogHandle",e.type),Xe(!1,e)}async lastLines(e,t,r,n){const i=await this.clientLogs.lastLines(x(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:i.data,size:Number(i.size),newOffset:Number(i.newOffset)}}async readText(e,t,r,n){const i=await this.clientLogs.readText(x(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:i.data,size:Number(i.size),newOffset:Number(i.newOffset)}}async releaseBlob(e,t){const r=this.keyToDownload.get(b(e.id));if(r!=null)if(this.cache.existsFile(b(e.id))){const n=this.cache.removeFile(b(e.id),t);await Promise.all(n.map(async i=>{await w.rm(i.path),this.cache.removeCache(i),this.removeTask(g.mapGet(this.keyToDownload,Se(i.path)),`the task ${p.stringifyWithResourceId(i)} was removedfrom cache along with ${p.stringifyWithResourceId(n.map(d=>d.path))}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${p.stringifyWithResourceId(r.info())} was removed from cache`)}removeTask(e,t){e.abort(t),e.change.markChanged(`download task for ${e.path} removed: ${t}`),this.keyToDownload.delete(Se(e.path)),this.idToLastLines.delete(b(e.rInfo.id)),this.idToProgressLog.delete(b(e.rInfo.id))}async releaseOnDemandBlob(e,t){var n;(((n=this.keyToOnDemand.get(b(e)))==null?void 0:n.release(t))??!1)&&this.keyToOnDemand.delete(b(e))}async releaseAll(){this.downloadQueue.stop(),this.keyToDownload.forEach((e,t)=>{this.keyToDownload.delete(t),e.change.markChanged(`task ${p.resourceIdToString(e.rInfo.id)} released`)})}}class Sn{constructor(e,t){h(this,"counter",new g.CallersCounter);this.size=e,this.handle=t}getHandle(){return{handle:this.handle,size:this.size}}attach(e){this.counter.inc(e)}release(e){return this.counter.dec(e)}}class De{constructor(e,t,r){h(this,"updater");h(this,"log");h(this,"change",new f.ChangeSource);h(this,"error");this.path=e,this.lines=t,this.patternToSearch=r,this.updater=new Ke(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 Dn(this.path,this.lines,this.patternToSearch);this.log!=e&&this.change.markChanged(`logs for ${this.path} updated`),this.log=e}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.log="",this.error=e,this.change.markChanged(`log update for ${this.path} failed, resource not found`);return}throw e}}}async function Dn(o,e,t){let r,n;try{r=pe.createReadStream(o),n=vt.createInterface({input:r,crlfDelay:1/0});const i=new Ut;for await(const d of n)t!=null&&!d.includes(t)||(i.push(d),i.length>e&&i.shift());return i.toArray().join(Re.EOL)+Re.EOL}finally{try{n&&n.close()}catch(i){console.error("Error closing readline interface:",i)}try{r&&!r.destroyed&&r.destroy()}catch(i){console.error("Error destroying read stream:",i)}}}function B(o,e){if(!e.name.startsWith("Blob/")){let t=`${o}: wrong resource type: ${e.name}, expected: a resource of type that starts with 'Blob/'.`;throw e.name=="Blob"&&(t+=" If it's called from workflow, should a file be exported with 'file.exportFile' function?"),new q(t)}}const it=m.rsSchema({});function ot(o,e){const t=m.isPlTreeEntry(o)?e.accessor(o).node():m.isPlTreeEntryAccessor(o)?o.node():o;return m.makeResourceSnapshot(t,it)}function Nn(o,e,t){const r=U.relative(e,t);return`plblob+folder://${o.sign(r)}.${r}.blob`}function $n(o,e,t){const r=U.relative(e,t);return`block-ui://${o.sign(r)}.${r}.uidir`}function vn(o,e,t){return st(o,e,t)}function Cn(o,e,t){return st(o,e,t)}function st(o,e,t){const r=new URL(e),[n,i,d]=r.host.split(".");o.verify(i,n,`signature verification failed for url: ${e}, subfolder: ${i}`);const s=decodeURIComponent(r.pathname.slice(1));let c=Fn(U.join(t,`${i}`),s);return(r.pathname==""||r.pathname=="/")&&(c=U.join(c,"index.html")),U.resolve(c)}function Fn(o,e){const t=U.resolve(o,e),r=U.resolve(o);if(!t.startsWith(r))throw new Error("Path validation failed.");return t}class Bn{constructor(e,t,r,n,i,d,s){h(this,"counter",new g.CallersCounter);h(this,"change",new f.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"size",0);h(this,"url");h(this,"state");this.logger=e,this.signer=t,this.saveDir=r,this.path=n,this.rInfo=i,this.format=d,this.clientDownload=s}info(){return{rInfo:this.rInfo,format:this.format,path:this.path,done:this.done,size:this.size,error:this.error,taskHistory:this.state}}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(){try{const e=await this.downloadAndDecompress(this.signalCtl.signal);this.setDone(e),this.change.markChanged(`download and decompress for ${p.resourceIdToString(this.rInfo.id)} finished`),this.logger.info(`blob to URL task is done: ${p.stringifyWithResourceId(this.info())}`)}catch(e){if(this.logger.warn(`a error was produced: ${e} for blob to URL task: ${p.stringifyWithResourceId(this.info())}`),En(e)){this.setError(e),this.change.markChanged(`download and decompress for ${p.resourceIdToString(this.rInfo.id)} failed`),await ce(this.path);return}throw e}}async downloadAndDecompress(e){return this.state={},this.state.parentDir=U.dirname(this.path),await g.ensureDirExists(this.state.parentDir),this.state.fileExisted=await g.fileExists(this.path),this.state.fileExisted?await at(this.path):await this.clientDownload.withBlobContent(this.rInfo,{},{signal:e},async(r,n)=>(this.state.downloaded=!0,await g.createPathAtomically(this.logger,this.path,async i=>{switch(this.state.tempPath=i,this.state.archiveFormat=this.format,this.format){case"tar":await w.mkdir(i);const d=I.Writable.toWeb(le.extract(i));await r.pipeTo(d,{signal:e});return;case"tgz":await w.mkdir(i);const s=I.Transform.toWeb(Ee.createGunzip()),c=I.Writable.toWeb(le.extract(i));await r.pipeThrough(s,{signal:e}).pipeTo(c,{signal:e});return;case"zip":this.state.zipPath=this.path+".zip";const l=I.Writable.toWeb(S.createWriteStream(this.state.zipPath));await r.pipeTo(l,{signal:e}),this.state.zipPathCreated=!0,await St(this.state.zipPath,i,{filter:u=>!u.path.endsWith("/")}),this.state.zipDecompressed=!0,await S.promises.rm(this.state.zipPath),this.state.zipPathDeleted=!0;return;default:a.assertNever(this.format)}}),this.state.pathCreated=!0,n))}getURL(){if(this.done)return{url:g.notEmpty(this.url)};if(this.error)return{error:this.error}}setDone(e){this.done=!0,this.size=e,this.url=Nn(this.signer,this.saveDir,this.path)}setError(e){this.error=String(e)}abort(e){this.signalCtl.abort(new lt(e))}}async function at(o){const e=await w.readdir(o,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=U.join(o,r.name);return r.isDirectory()?await at(n):(await w.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function ce(o){await w.rm(o,{recursive:!0,force:!0})}let lt=class extends Error{constructor(){super(...arguments);h(this,"name","URLAborted")}};function En(o){return o instanceof lt||o instanceof j||o instanceof V||o instanceof G||(o==null?void 0:o.code)=="ENOENT"||o.name=="RpcError"&&(o.code=="NOT_FOUND"||o.code=="ABORTED")||String(o).includes("incorrect header check")}function _(o,e){return`id:${String(BigInt(o))}-${e}`}class Wn{constructor(e,t,r,n,i={cacheSoftSizeBytes:50*1024*1024,nConcurrentDownloads:50}){h(this,"idToDownload",new Map);h(this,"downloadQueue");h(this,"cache");this.logger=e,this.signer=t,this.clientDownload=r,this.saveDir=n,this.opts=i,this.downloadQueue=new g.TaskProcessor(this.logger,this.opts.nConcurrentDownloads,{type:"exponentialWithMaxDelayBackoff",initialDelay:1e4,maxDelay:3e4,backoffMultiplier:1.5,jitter:.5}),this.cache=new ke(this.opts.cacheSoftSizeBytes)}info(){return{saveDir:this.saveDir,opts:this.opts,idToDownloadSize:this.idToDownload.size,idToDownloadKeys:this.idToDownload.keys(),idToDownload:Array.from(this.idToDownload.entries()).map(([e,t])=>[e,t.info()])}}getPathForCustomProtocol(e){if(v.isFolderURL(e))return vn(this.signer,e,this.saveDir);throw new Error(`getPathForCustomProtocol: ${e} is invalid`)}extractArchiveAndGetURL(e,t,r){if(r===void 0)return f.Computable.make(s=>this.extractArchiveAndGetURL(e,t,s));const n=m.isPlTreeEntry(e)?ot(e,r):e,i=R.randomUUID();r.addOnDestroy(()=>this.releasePath(n.id,t,i));const d=this.extractArchiveAndGetURLNoCtx(n,t,r.watcher,i);if((d==null?void 0:d.url)===void 0&&r.markUnstable(`a path to the downloaded archive might be undefined. The current result: ${d}`),(d==null?void 0:d.error)!==void 0)throw d==null?void 0:d.error;return d==null?void 0:d.url}extractArchiveAndGetURLNoCtx(e,t,r,n){const i=this.idToDownload.get(_(e.id,t));if(i!=null)return i.attach(r,n),i.getURL();const d=this.setNewTask(r,e,t,n);return this.downloadQueue.push({fn:async()=>this.downloadUrl(d,n),recoverableErrorPredicate:s=>!be(s)}),d.getURL()}async downloadUrl(e,t){var r;await e.download(),((r=e.getURL())==null?void 0:r.url)!=null&&this.cache.addCache(e,t)}async releasePath(e,t,r){const n=this.idToDownload.get(_(e,t));if(n!=null)if(this.cache.existsFile(n.path)){const i=this.cache.removeFile(n.path,r);await Promise.all(i.map(async d=>{await ce(d.path),this.cache.removeCache(d),this.removeTask(d,`the task ${p.stringifyWithResourceId(d.info())} was removedfrom cache along with ${p.stringifyWithResourceId(i.map(s=>s.info()))}`)}))}else n.counter.dec(r)&&this.removeTask(n,`the task ${p.stringifyWithResourceId(n.info())} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.idToDownload.entries()).map(async([e,t])=>{await ce(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${p.stringifyWithResourceId(t.info())} was released when the driver was closed`)}))}setNewTask(e,t,r,n){const i=new Bn(this.logger,this.signer,this.saveDir,this.getFilePath(t.id,r),t,r,this.clientDownload);return i.attach(e,n),this.idToDownload.set(_(t.id,r),i),i}removeTask(e,t){e.abort(t),e.change.markChanged(`task for ${p.resourceIdToString(e.rInfo.id)} removed: ${t}`),this.idToDownload.delete(_(e.rInfo.id,e.format))}getFilePath(e,t){return k.join(this.saveDir,`${String(BigInt(e))}_${t}`)}}class ct{constructor(e,t,r,n,i,d){h(this,"change",new f.ChangeSource);h(this,"counter",new g.CallersCounter);h(this,"nMaxUploads");h(this,"nPartsWithThisUploadSpeed",0);h(this,"nPartsToIncreaseUpload",10);h(this,"uploadData");h(this,"progress");h(this,"failed");h(this,"alreadyExisted",!1);this.logger=e,this.clientBlob=t,this.clientProgress=r,this.maxNConcurrentPartsUpload=n,this.res=d,this.nMaxUploads=this.maxNConcurrentPartsUpload;const{uploadData:s,progress:c}=_n(d,i);this.uploadData=s,this.progress=c}getProgress(e,t){if(this.incCounter(e,t),this.failed)throw this.logger.error(`Uploading terminally failed: ${this.progress.lastError}`),new Error(this.progress.lastError);return Mn(this.progress)}shouldScheduleUpload(){return ht(this.progress)}async uploadBlobTask(){try{await dt(this.logger,this.clientBlob,this.res,this.uploadData,this.isComputableDone.bind(this),{nPartsWithThisUploadSpeed:this.nPartsWithThisUploadSpeed,nPartsToIncreaseUpload:this.nPartsToIncreaseUpload,currentSpeed:this.nMaxUploads,maxSpeed:this.maxNConcurrentPartsUpload}),this.change.markChanged(`blob upload for ${p.resourceIdToString(this.res.id)} finished`)}catch(e){if(this.setRetriableError(e),de(e)){this.logger.warn(`resource was deleted while uploading a blob: ${e}`),this.change.markChanged(`blob upload for ${p.resourceIdToString(this.res.id)} aborted, resource was deleted`),this.setDone(!0);return}if(this.logger.error(`error while uploading a blob: ${e}`),this.change.markChanged(`blob upload for ${p.resourceIdToString(this.res.id)} failed`),Le(e)){this.setTerminalError(e);return}throw xn(e)&&(this.nMaxUploads=jn(this.logger,this.nMaxUploads,1)),e}}async updateStatus(){var e;try{const t=await this.clientProgress.getStatus(this.res,{timeout:1e4}),r=this.progress.status,n=zn(this.alreadyExisted,Hn(t));this.progress.status=n,this.setDone(t.done),(t.done||this.progress.status.progress!=(r==null?void 0:r.progress))&&this.change.markChanged(`upload status for ${p.resourceIdToString(this.res.id)} changed`)}catch(t){if(this.setRetriableError(t),t.name=="RpcError"&&t.code=="DEADLINE_EXCEEDED"||(e=t==null?void 0:t.message)!=null&&e.includes("DEADLINE_EXCEEDED")){this.logger.warn("deadline exceeded while getting a status of BlobImport");return}if(de(t)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${t}, ${p.stringifyWithResourceId(this.res)}`),this.change.markChanged(`upload status for ${p.resourceIdToString(this.res.id)} changed, resource not found`),this.setDone(!0);return}this.logger.error(`retryable error while updating a status of BlobImport: ${t}`)}}setRetriableError(e){this.progress.lastError=String(e)}setTerminalError(e){this.progress.lastError=String(e),this.progress.done=!1,this.failed=!0}setDoneIfOutputSet(e){On(e)&&(this.setDone(!0),this.alreadyExisted=!0)}setDone(e){this.progress.done=e,e&&(this.progress.lastError=void 0)}incCounter(e,t){this.change.attachWatcher(e),this.counter.inc(t)}decCounter(e){return this.counter.dec(e)}isComputableDone(){return this.counter.isZero()}}async function dt(o,e,t,r,n,i){Dt(A(t),"the upload operation can be done only for BlobUploads");const d=1e4;if(n())return;const s=await e.initUpload(t,{timeout:d});o.info(`started to upload blob ${t.id}, parts overall: ${s.overall}, parts remained: ${s.toUpload.length}, number of concurrent uploads: ${i.currentSpeed}`);const c=l=>async u=>{n()||(await e.partUpload(t,r.localPath,BigInt(r.modificationTime),l,{timeout:d}),o.info(`uploaded chunk ${l}/${s.overall} of resource: ${t.id}`),i.nPartsWithThisUploadSpeed++,i.nPartsWithThisUploadSpeed>=i.nPartsToIncreaseUpload&&(i.nPartsWithThisUploadSpeed=0,i.currentSpeed=An(o,i.currentSpeed,i.maxSpeed),u.setConcurrency(i.currentSpeed)))};await g.asyncPool(i.currentSpeed,s.toUpload.map(c)),!n()&&(await e.finalize(t,{timeout:d}),o.info(`uploading of resource ${t.id} finished.`))}function _n(o,e){let t,r;return A(o)&&(r=E.parse(o.data),t=ut(e,r.localPath,r.pathSignature)),{uploadData:r,progress:{done:!1,status:void 0,isUpload:A(o),isUploadSignMatch:t,lastError:void 0}}}function ht(o){return o.isUpload&&(o.isUploadSignMatch??!1)}function Mn(o){return o.done,o.isUpload,o.isUploadSignMatch,o.lastError,o.status&&(o.status.progress,o.status.bytesProcessed,o.status.bytesTotal),o}function On(o){return"blob"in o.fields?o.fields.blob!==void 0:o.fields.incarnation!==void 0}function A(o){return o.type.name.startsWith("BlobUpload")}function ut(o,e,t){try{return o.verify(e,t),!0}catch{return!1}}function Hn(o){return{progress:o.progress??0,bytesProcessed:Number(o.bytesProcessed),bytesTotal:Number(o.bytesTotal)}}function zn(o,e){return o&&e.bytesTotal!=0&&e.bytesProcessed==0?{progress:1,bytesProcessed:Number(e.bytesTotal),bytesTotal:Number(e.bytesTotal)}:e}function de(o){return o.name=="RpcError"&&(o.code=="NOT_FOUND"||o.code=="ABORTED"||o.code=="ALREADY_EXISTS")}function Le(o){return o instanceof ge||o instanceof fe||o instanceof me}function xn(o){return o==null?void 0:o.message.includes("UND_ERR_HEADERS_TIMEOUT")}function An(o,e,t){const r=Math.min(e+2,t);return r!=e&&o.info(`uploadTask.increaseConcurrency: increased from ${e} to ${r}`),r}function jn(o,e,t){const r=Math.max(Math.round(e/2),t);return r!=e&&o.info(`uploadTask.decreaseConcurrency: decreased from ${e} to ${r}`),r}function pt(o,e){const t=m.isPlTreeEntry(o)?e.accessor(o).node():m.isPlTreeEntryAccessor(o)?o.node():o;return t.resourceType.name.startsWith("BlobUpload")?m.makeResourceSnapshot(t,Je):m.makeResourceSnapshot(t,Ze)}class Gn{constructor(e,t,r,n,i={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=i,this.uploadQueue=new g.TaskProcessor(this.logger,1,{type:"exponentialWithMaxDelayBackoff",initialDelay:20,maxDelay:15e3,backoffMultiplier:1.5,jitter:.5}),this.hooks=new f.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:i.stopPollingDelay},(d,s)=>this.scheduleOnNextState(d,s))}getProgressId(e,t){if(t==null)return f.Computable.make(d=>this.getProgressId(e,d));const r=m.isPlTreeEntry(e)?pt(e,t):e,n=R.randomUUID();return t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(r.id,n)),this.getProgressIdNoCtx(t.watcher,r,n)}getProgressIdNoCtx(e,t,r){qn("getProgressId",t.type);const n=this.idToProgress.get(t.id);if(n!=null)return n.setDoneIfOutputSet(t),n.getProgress(e,r);const i=new ct(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,i),i.shouldScheduleUpload()&&this.uploadQueue.push({fn:()=>i.uploadBlobTask(),recoverableErrorPredicate:d=>!Le(d)}),i.setDoneIfOutputSet(t),i.getProgress(e,r)}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 Be.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!Vn(t.progress)).map(([e,t])=>t)}}function Vn(o){var e;return o.done&&(((e=o.status)==null?void 0:e.progress)??0)>=1}function qn(o,e){if(!e.name.startsWith("BlobUpload")&&!e.name.startsWith("BlobIndex"))throw new q(`${o}: wrong resource type: ${e.name}, expected: a resource of either type 'BlobUpload' or 'BlobIndex'.`)}class Jn{constructor(e,t,r={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.logger=e,this.clientLogs=t,this.opts=r,this.hooks=new f.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:r.stopPollingDelay},(n,i)=>this.scheduleOnNextState(n,i))}getLastLogs(e,t,r){if(r==null)return f.Computable.make(s=>this.getLastLogs(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=R.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseLastLogs(n.id,i));const d=this.getLastLogsNoCtx(r.watcher,n,t,i);return r.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),d}getLastLogsNoCtx(e,t,r,n){se("getLastLogs",t.type);let i=this.idToLastLines.get(t.id);if(i==null){const s=new Ne(this.logger,this.clientLogs,t,r);this.idToLastLines.set(t.id,s),i=s}i.attach(e,n);const d=i.getLog();if(d.error!=null)throw d.error;return d.log}getProgressLog(e,t,r){if(r==null)return f.Computable.make(s=>this.getProgressLog(e,t,s));const n=m.treeEntryToResourceInfo(e,r),i=R.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseProgressLog(n.id,i));const d=this.getProgressLogNoCtx(r.watcher,n,t,i);return r.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),d}getProgressLogNoCtx(e,t,r,n){se("getProgressLog",t.type);let i=this.idToProgressLog.get(t.id);if(i==null){const s=new Ne(this.logger,this.clientLogs,t,1,r);this.idToProgressLog.set(t.id,s),i=s}i.attach(e,n);const d=i.getLog();if(d.error)throw d.error;return d.log}getLogHandle(e,t){if(t==null)return f.Computable.make(i=>this.getLogHandle(e,i));const r=m.treeEntryToResourceInfo(e,t),n=this.getLogHandleNoCtx(r);return t.markUnstable(`live_log:${p.resourceIdToString(r.id)}`),n}getLogHandleNoCtx(e){return se("getLogHandle",e.type),Xe(!0,e)}async lastLines(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(x(e),t,BigInt(r??0),n))}async readText(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(x(e),t,BigInt(r??0),n))}async tryWithNotFound(e,t){if(!z(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{const t=this.getAllLogs();await g.asyncPool(this.opts.nConcurrentGetLogs,t.map(r=>async()=>await r.update())),e.forEach(r=>r.resolve())}catch(t){console.error(t),e.forEach(r=>r.reject(t))}if(!this.keepRunning)break;await Be.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllLogs(){return Array.from(this.idToLastLines.entries()).concat(Array.from(this.idToProgressLog.entries())).map(([e,t])=>t)}}class Ne{constructor(e,t,r,n,i){h(this,"logs");h(this,"error");h(this,"change",new f.ChangeSource);h(this,"counter",new g.CallersCounter);this.logger=e,this.clientLogs=t,this.rInfo=r,this.lines=n,this.patternToSearch=i}getLog(){return{log:this.logs,error:this.error}}attach(e,t){this.change.attachWatcher(e),this.counter.inc(t)}release(e){return this.counter.dec(e)}async update(){try{const e=await this.clientLogs.lastLines(this.rInfo,this.lines,0n,this.patternToSearch),t=new TextDecoder().decode(e.data);this.logs!=t&&this.change.markChanged(`logs for ${p.resourceIdToString(this.rInfo.id)} updated`),this.logs=t,this.error=void 0;return}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.logs="",this.error=e,this.change.markChanged();return}throw this.logger.error(`Stream log lines for ${p.stringifyWithResourceId(this.rInfo.id)} failed, reason: ${e}`),e}}}function se(o,e){if(!e.name.startsWith("StreamWorkdir"))throw new q(`${o}: wrong resource type: ${e.name}, expected: a resource of type 'StreamWorkdir'.`)}class Zn{constructor(e,t,r){this.logger=e,this.logsStreamDriver=t,this.downloadDriver=r}getLastLogs(e,t,r){if(r===void 0)return f.Computable.make(i=>this.getLastLogs(e,t,i));const n=O(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(M(n))return this.downloadDriver.getLastLogs(n,t,r);try{return this.logsStreamDriver.getLastLogs(n,t,r)}catch(i){if(i.name=="RpcError"&&i.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${i}`);return}throw i}}getProgressLog(e,t,r){if(r===void 0)return f.Computable.make(i=>this.getProgressLog(e,t,i));const n=O(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(M(n))return this.downloadDriver.getProgressLog(n,t,r);try{return this.logsStreamDriver.getProgressLog(n,t,r)}catch(i){if(i.name=="RpcError"&&i.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${i}`);return}throw i}}getProgressLogWithInfo(e,t,r){if(r===void 0)return f.Computable.make(i=>this.getProgressLogWithInfo(e,t,i));const n=O(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(M(n))return{progressLine:this.downloadDriver.getProgressLog(n,t,r),live:!1};try{return{progressLine:this.logsStreamDriver.getProgressLog(n,t,r),live:!0}}catch(i){if(i.name=="RpcError"&&i.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log with info: ${i}`);return}throw i}}getLogHandle(e,t){if(t===void 0)return f.Computable.make(n=>this.getLogHandle(e,n));const r=O(t,e);if(r===void 0){t.markUnstable("no stream in stream manager");return}return M(r)?this.downloadDriver.getLogHandle(r,t):this.logsStreamDriver.getLogHandle(r,t)}async lastLines(e,t,r,n){return z(e)?await this.logsStreamDriver.lastLines(e,t,r,n):await this.downloadDriver.lastLines(e,t,r,n)}async readText(e,t,r,n){return z(e)?await this.logsStreamDriver.readText(e,t,r,n):await this.downloadDriver.readText(e,t,r,n)}}function M(o){return!o.type.name.startsWith("StreamWorkdir")}function O(o,e){var t;return(t=o.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}class Qn{constructor(e,t,r,n,i){h(this,"counter",new g.CallersCounter);h(this,"change",new f.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"size",0);this.logger=e,this.path=t,this.url=r,this.signer=n,this.saveDir=i}info(){return{url:this.url.toString(),path:this.path,done:this.done,size:this.size,error:this.error}}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),this.change.markChanged(`download of ${this.url} finished`)}catch(r){if(r instanceof $e||r instanceof j){this.setError(r),this.change.markChanged(`download of ${this.url} failed`),await he(this.path);return}throw r}}async downloadAndUntar(e,t,r){return await g.ensureDirExists(k.dirname(this.path)),await g.fileExists(this.path)?await gt(this.path):await e.withContent(this.url.toString(),{},{signal:r},async(i,d)=>{let s=i;if(t){const c=I.Transform.toWeb(Ee.createGunzip());s=i.pipeThrough(c,{signal:r})}return await g.createPathAtomically(this.logger,this.path,async c=>{await w.mkdir(c);const l=I.Writable.toWeb(le.extract(c));await s.pipeTo(l,{signal:r})}),d})}getUrl(){if(this.done)return{url:$n(this.signer,this.saveDir,g.notEmpty(this.path))};if(this.error)return{error:this.error}}setDone(e){this.done=!0,this.size=e}setError(e){this.error=String(e)}abort(e){this.signalCtl.abort(new $e(e))}}class $e extends Error{constructor(){super(...arguments);h(this,"name","URLAborted")}}async function gt(o){const e=await w.readdir(o,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=k.join(o,r.name);return r.isDirectory()?await gt(n):(await w.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function he(o){await w.rm(o,{recursive:!0,force:!0})}class Kn{constructor(e,t,r,n,i={cacheSoftSizeBytes:1*1024*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.signer=n,this.opts=i,this.downloadQueue=new g.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new ke(this.opts.cacheSoftSizeBytes),this.downloadHelper=new Me(t)}getUrl(e,t){if(t===void 0)return f.Computable.make(i=>this.getUrl(e,i));const r=R.randomUUID();t.addOnDestroy(()=>this.releasePath(e,r));const n=this.getUrlNoCtx(e,t.watcher,r);return(n==null?void 0:n.url)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${n}`),n}getUrlNoCtx(e,t,r){const n=e.toString(),i=this.urlToDownload.get(n);if(i!==void 0)return i.attach(t,r),i.getUrl();const d=this.setNewTask(t,e,r);return this.downloadQueue.push({fn:async()=>this.downloadUrl(d,r),recoverableErrorPredicate:s=>!0}),d.getUrl()}getPathForBlockUI(e){if(!v.isBlockUIURL(e))throw new Error(`getPathForBlockUI: ${e} is invalid`);return Cn(this.signer,e,this.saveDir)}async downloadUrl(e,t){var r;await e.download(this.downloadHelper,this.opts.withGunzip),((r=e.getUrl())==null?void 0:r.url)!==void 0&&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 i=this.cache.removeFile(n.path,t);await Promise.all(i.map(async d=>{await he(d.path),this.cache.removeCache(d),this.removeTask(d,`the task ${p.stringifyWithResourceId(d.info())} was removedfrom cache along with ${p.stringifyWithResourceId(i.map(s=>s.info()))}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${p.stringifyWithResourceId(n.info())} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.urlToDownload.entries()).map(async([e,t])=>{await he(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${p.stringifyWithResourceId(t.info())} was released when the driver was closed`)}))}setNewTask(e,t,r){const n=new Qn(this.logger,this.getFilePath(t),t,this.signer,this.saveDir);return n.attach(e,r),this.urlToDownload.set(t.toString(),n),n}removeTask(e,t){e.abort(t),e.change.markChanged(`task for url ${e.url} removed: ${t}`),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=R.createHash("sha256").update(e.toString()).digest("hex");return k.join(this.saveDir,t)}}function ae(o,e){return`index://index/${encodeURIComponent(JSON.stringify({storageId:o,path:e}))}`}function Xn(o,e,t,r){const n={localPath:o,pathSignature:e.sign(o),sizeBytes:String(t),modificationTime:String(r)};return`upload://upload/${encodeURIComponent(JSON.stringify(n))}`}function Yn(o){const e=new URL(o);return E.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function ei(o){const e=new URL(o);return Te.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function ve(o){if(ii(o))return si(o);if(ti(o))return ni(o);g.assertNever(o)}const ft=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function ti(o){return ft.test(o)}function ri(o,e){return`local://${o}/${encodeURIComponent(e)}`}function ni(o){const e=o.match(ft);if(e==null)throw new Error(`Local list handle wasn't parsed: ${o}`);const{name:t,path:r}=e.groups;return{rootPath:decodeURIComponent(r),name:t,isRemote:!1}}const wt=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function ii(o){return wt.test(o)}function oi(o,e){return`remote://${o}/${BigInt(e)}`}function si(o){const e=o.match(wt);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${o}`);const{name:t,resourceId:r}=e.groups;return{id:p.bigintToResourceId(BigInt(r)),type:ai(t),name:t,isRemote:!0}}function ai(o){return{name:`LS/${o}`,version:"1"}}async function mt(){const o=Fe.homedir();if(U.sep=="/")return[{name:"local",root:"/",initialPath:o}];{const t=U.parse(o).root.replaceAll(":\\","");try{return(await Nt.promisify($t.exec)("wmic logicaldisk get name")).stdout.split(`\r
2
+ `).filter(i=>i.includes(":")).map(i=>i.trim().replaceAll(":","")).map(i=>{const d=i==t;return{name:`local_disk_${i}`,root:`${i}:\\`,initialPath:d?o:`${i}:\\`}})}catch{return[{name:`local_disk_${t}`,root:`${t}:\\`,initialPath:o}]}}}class Pe{constructor(e,t,r,n,i,d,s){this.logger=e,this.lsClient=t,this.storageIdToResourceId=r,this.signer=n,this.virtualStoragesMap=i,this.localProjectionsMap=d,this.openFileDialogCallback=s}async getLocalFileContent(e,t){const r=await this.tryResolveLocalFileHandle(e);if(t)throw new Error("Range request not yet supported.");return await w.readFile(r)}async getLocalFileSize(e){const t=await this.tryResolveLocalFileHandle(e);return(await w.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(v.isImportFileHandleIndex(e)){const t=ei(e),r=this.localProjectionsMap.get(t.storageId);if(!r)throw new Error(`Storage ${t.storageId} is not mounted locally.`);return k.join(r.localPath,t.path)}else{const t=Yn(e);this.signer.verify(t.localPath,t.pathSignature,"Failed to validate local file handle signature.");const r=t.localPath,n=await w.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){N(e);for(const r of this.localProjectionsMap.values())if(e.startsWith(r.localPath)){const n=r.localPath===""?e:k.relative(r.localPath,e);return ae(r.storageId,n)}const t=await w.stat(e,{bigint:!0});return Xn(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){const e=[...this.virtualStoragesMap.values()].map(n=>({name:n.name,handle:ri(n.name,n.root),initialFullPath:n.initialPath})),r=Object.entries(this.storageIdToResourceId).map(([n,i])=>({name:n,handle:oi(n,i),initialFullPath:"",isInitialPathHome:!1})).filter(n=>n.name!=="root");return[...e,...r]}async listFiles(e,t){const r=ve(e);if(r.isRemote)return{entries:(await this.lsClient.list(r,t)).items.map(s=>({type:s.isDir?"dir":"file",name:s.name,fullPath:s.fullName,handle:ae(r.name,s.fullName)}))};k.sep==="/"&&t===""&&(t="/"),r.rootPath===""&&N(t);const n=k.isAbsolute(t)?t:k.join(r.rootPath,t),i=[];for await(const d of await w.opendir(n)){if(!d.isFile()&&!d.isDirectory())continue;const s=k.join(n,d.name);i.push({type:d.isFile()?"file":"dir",name:d.name,fullPath:s,handle:await this.getLocalFileHandle(s)})}return{entries:i}}async listRemoteFilesWithAdditionalInfo(e,t){const r=ve(e);if(!r.isRemote)throw new Error(`Storage ${r.name} is not remote`);return{entries:(await this.lsClient.list(r,t)).items.map(i=>({type:i.isDir?"dir":"file",name:i.name,fullPath:i.fullName,handle:ae(r.name,i.fullName),size:Number(i.size)}))}}async fileToImportHandle(e){throw new Error("Not implemented. This method must be implemented and intercepted in desktop preload script.")}static async init(e,t,r,n,i,d){const s=Ge(t,e);d||(d=await mt());for(const u of d)N(u.root);for(const u of n)u.localPath!==""&&N(u.localPath);const c=new Map(d.map(u=>[u.name,u])),l=new Map(n.map(u=>[u.storageId,u]));if(new Set([...c.keys(),...l.keys()]).size!==c.size+l.size)throw new Error("Intersection between local projection storage ids and virtual storages names detected.");return new Pe(e,s,await li(t),r,c,l,i)}}async function li(o){return o.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),r=await e.getResourceData(t,!0);return ci(r)})}function ci(o){return Object.fromEntries(o.fields.filter(e=>e.type=="Dynamic"&&p.isNotNullResourceId(e.value)).map(e=>[e.name.substring(8),e.value]))}exports.ClientDownload=Oe;exports.ClientLogs=je;exports.ClientLs=Ae;exports.ClientProgress=_e;exports.ClientUpload=We;exports.DefaultVirtualLocalStorages=mt;exports.DownloadBlobToURLDriver=Wn;exports.DownloadDriver=Ue;exports.DownloadUrlDriver=Kn;exports.DownloadableBlobSnapshot=it;exports.ImportFileHandleData=sn;exports.ImportFileHandleIndexData=Te;exports.ImportFileHandleUploadData=E;exports.IndexResourceSnapshot=Ze;exports.LogsDriver=Zn;exports.LogsStreamDriver=Jn;exports.LsDriver=Pe;exports.MTimeError=ge;exports.NetworkError=we;exports.NoFileForUploading=me;exports.OnDemandBlobResourceSnapshot=qe;exports.UnexpectedEOF=fe;exports.UnknownStorageError=V;exports.Updater=Ke;exports.UploadDriver=Gn;exports.UploadResourceSnapshot=Je;exports.UploadTask=ct;exports.WrongLocalFileUrl=G;exports.WrongResourceTypeError=q;exports.createDownloadClient=Xr;exports.createLogsClient=Yr;exports.createLsFilesClient=Ge;exports.createUploadBlobClient=tn;exports.createUploadProgressClient=en;exports.getFullPath=ze;exports.getSize=ye;exports.isMyUpload=ht;exports.isResourceWasDeletedError=de;exports.isSignMatch=ut;exports.isUpload=A;exports.makeBlobImportSnapshot=pt;exports.makeDownloadableBlobSnapshot=ot;exports.newLocalStorageIdsToRoot=xe;exports.nonRecoverableError=Le;exports.parseLocalUrl=He;exports.uploadBlob=dt;exports.validateAbsolute=N;
3
3
  //# sourceMappingURL=index.js.map