@milaboratories/pl-drivers 1.5.4 → 1.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/drivers/download_blob.d.ts.map +1 -1
- package/dist/drivers/download_blob_task.d.ts +9 -0
- package/dist/drivers/download_blob_task.d.ts.map +1 -1
- package/dist/drivers/download_url.d.ts +7 -0
- package/dist/drivers/download_url.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +253 -220
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/drivers/download_blob.test.ts +52 -4
- package/src/drivers/download_blob.ts +7 -4
- package/src/drivers/download_blob_task.ts +31 -12
- package/src/drivers/download_url.ts +24 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"download_blob.d.ts","sourceRoot":"","sources":["../../src/drivers/download_blob.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EACV,aAAa,EACb,uBAAuB,EAExB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EACL,YAAY,EACZ,UAAU,EACV,eAAe,EACf,sBAAsB,EACtB,cAAc,EACd,gBAAgB,EAChB,uBAAuB,EACvB,oBAAoB,EACrB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAGL,WAAW,EACX,YAAY,EAGb,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAkB,QAAQ,EAAE,MAAM,EAAiB,MAAM,4BAA4B,CAAC;AAU7F,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAQ7C,OAAO,EAAW,4BAA4B,EAAE,MAAM,SAAS,CAAC;AAShE,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;;SAIK;IACL,kBAAkB,EAAE,MAAM,CAAC;IAC3B;;;SAGK;IACL,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;6DAC6D;AAC7D,qBAAa,cAAe,YAAW,UAAU;IAmB7C,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAE3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAtBzB,+DAA+D;IAC/D,OAAO,CAAC,YAAY,CAAgD;IAEpE;mCAC+B;IAC/B,OAAO,CAAC,KAAK,CAA+B;IAE5C,wDAAwD;IACxD,OAAO,CAAC,aAAa,CAAgB;IAErC,OAAO,CAAC,YAAY,CAAkD;IAEtE,OAAO,CAAC,aAAa,CAA+C;IACpE,OAAO,CAAC,eAAe,CAA+C;IAEtE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAGd,MAAM,EAAE,QAAQ,EAChB,cAAc,EAAE,cAAc,EAC9B,UAAU,EAAE,UAAU,EACvC,OAAO,EAAE,MAAM,EACE,MAAM,EAAE,MAAM,EAC/B,GAAG,EAAE,iBAAiB;IAQxB,gFAAgF;IACzE,iBAAiB,CACtB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,GAAG,EAAE,aAAa,GACjB,sBAAsB,GAAG,SAAS;IAC9B,iBAAiB,CACtB,GAAG,EAAE,YAAY,GAAG,WAAW,GAC9B,uBAAuB,CAAC,sBAAsB,CAAC;IAkBlD,OAAO,CAAC,sBAAsB;IA0B9B,OAAO,CAAC,kBAAkB;YAcZ,YAAY;IAQ1B,2BAA2B;IACpB,eAAe,CACpB,GAAG,EAAE,4BAA4B,GAAG,WAAW,GAC9C,UAAU,CAAC,uBAAuB,CAAC;IAC/B,eAAe,CACpB,GAAG,EAAE,4BAA4B,GAAG,WAAW,EAC/C,GAAG,EAAE,aAAa,GACjB,uBAAuB;IAqB1B,OAAO,CAAC,oBAAoB;IAkB5B,iCAAiC;IAC1B,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM;IAKpD,4CAA4C;IAC/B,UAAU,CAAC,MAAM,EAAE,eAAe,GAAG,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC;IAcxF;0DACsD;IAC/C,WAAW,CAChB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,KAAK,EAAE,MAAM,GACZ,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1B,WAAW,CAChB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,aAAa,GACjB,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC;IAmBjC,OAAO,CAAC,gBAAgB;IA0BxB;2DACuD;IAChD,cAAc,CACnB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,eAAe,EAAE,MAAM,GACtB,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1B,cAAc,CACnB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,eAAe,EAAE,MAAM,EACvB,GAAG,EAAE,aAAa,GACjB,MAAM,GAAG,SAAS;IAyBrB,OAAO,CAAC,mBAAmB;IA2B3B;uBACmB;IACZ,YAAY,CAAC,GAAG,EAAE,YAAY,GAAG,WAAW,GAAG,UAAU,CAAC,YAAY,CAAC;IACvE,YAAY,CAAC,GAAG,EAAE,YAAY,GAAG,WAAW,EAAE,GAAG,EAAE,aAAa,GAAG,YAAY;IAYtF,OAAO,CAAC,iBAAiB;IAKZ,SAAS,CACpB,MAAM,EAAE,cAAc,EACtB,SAAS,EAAE,MAAM,EACjB,WAAW,CAAC,EAAE,MAAM,EAAE,kCAAkC;IACxD,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,oBAAoB,CAAC;IAiBnB,QAAQ,CACnB,MAAM,EAAE,cAAc,EACtB,SAAS,EAAE,MAAM,EACjB,WAAW,CAAC,EAAE,MAAM,EACpB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,oBAAoB,CAAC;YAiBlB,WAAW;
|
|
1
|
+
{"version":3,"file":"download_blob.d.ts","sourceRoot":"","sources":["../../src/drivers/download_blob.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EACV,aAAa,EACb,uBAAuB,EAExB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EACL,YAAY,EACZ,UAAU,EACV,eAAe,EACf,sBAAsB,EACtB,cAAc,EACd,gBAAgB,EAChB,uBAAuB,EACvB,oBAAoB,EACrB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAGL,WAAW,EACX,YAAY,EAGb,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAkB,QAAQ,EAAE,MAAM,EAAiB,MAAM,4BAA4B,CAAC;AAU7F,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAQ7C,OAAO,EAAW,4BAA4B,EAAE,MAAM,SAAS,CAAC;AAShE,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;;SAIK;IACL,kBAAkB,EAAE,MAAM,CAAC;IAC3B;;;SAGK;IACL,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;6DAC6D;AAC7D,qBAAa,cAAe,YAAW,UAAU;IAmB7C,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAE3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IAtBzB,+DAA+D;IAC/D,OAAO,CAAC,YAAY,CAAgD;IAEpE;mCAC+B;IAC/B,OAAO,CAAC,KAAK,CAA+B;IAE5C,wDAAwD;IACxD,OAAO,CAAC,aAAa,CAAgB;IAErC,OAAO,CAAC,YAAY,CAAkD;IAEtE,OAAO,CAAC,aAAa,CAA+C;IACpE,OAAO,CAAC,eAAe,CAA+C;IAEtE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAGd,MAAM,EAAE,QAAQ,EAChB,cAAc,EAAE,cAAc,EAC9B,UAAU,EAAE,UAAU,EACvC,OAAO,EAAE,MAAM,EACE,MAAM,EAAE,MAAM,EAC/B,GAAG,EAAE,iBAAiB;IAQxB,gFAAgF;IACzE,iBAAiB,CACtB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,GAAG,EAAE,aAAa,GACjB,sBAAsB,GAAG,SAAS;IAC9B,iBAAiB,CACtB,GAAG,EAAE,YAAY,GAAG,WAAW,GAC9B,uBAAuB,CAAC,sBAAsB,CAAC;IAkBlD,OAAO,CAAC,sBAAsB;IA0B9B,OAAO,CAAC,kBAAkB;YAcZ,YAAY;IAQ1B,2BAA2B;IACpB,eAAe,CACpB,GAAG,EAAE,4BAA4B,GAAG,WAAW,GAC9C,UAAU,CAAC,uBAAuB,CAAC;IAC/B,eAAe,CACpB,GAAG,EAAE,4BAA4B,GAAG,WAAW,EAC/C,GAAG,EAAE,aAAa,GACjB,uBAAuB;IAqB1B,OAAO,CAAC,oBAAoB;IAkB5B,iCAAiC;IAC1B,YAAY,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM;IAKpD,4CAA4C;IAC/B,UAAU,CAAC,MAAM,EAAE,eAAe,GAAG,gBAAgB,GAAG,OAAO,CAAC,UAAU,CAAC;IAcxF;0DACsD;IAC/C,WAAW,CAChB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,KAAK,EAAE,MAAM,GACZ,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1B,WAAW,CAChB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,aAAa,GACjB,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC;IAmBjC,OAAO,CAAC,gBAAgB;IA0BxB;2DACuD;IAChD,cAAc,CACnB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,eAAe,EAAE,MAAM,GACtB,UAAU,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1B,cAAc,CACnB,GAAG,EAAE,YAAY,GAAG,WAAW,EAC/B,eAAe,EAAE,MAAM,EACvB,GAAG,EAAE,aAAa,GACjB,MAAM,GAAG,SAAS;IAyBrB,OAAO,CAAC,mBAAmB;IA2B3B;uBACmB;IACZ,YAAY,CAAC,GAAG,EAAE,YAAY,GAAG,WAAW,GAAG,UAAU,CAAC,YAAY,CAAC;IACvE,YAAY,CAAC,GAAG,EAAE,YAAY,GAAG,WAAW,EAAE,GAAG,EAAE,aAAa,GAAG,YAAY;IAYtF,OAAO,CAAC,iBAAiB;IAKZ,SAAS,CACpB,MAAM,EAAE,cAAc,EACtB,SAAS,EAAE,MAAM,EACjB,WAAW,CAAC,EAAE,MAAM,EAAE,kCAAkC;IACxD,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,oBAAoB,CAAC;IAiBnB,QAAQ,CACnB,MAAM,EAAE,cAAc,EACtB,SAAS,EAAE,MAAM,EACjB,WAAW,CAAC,EAAE,MAAM,EACpB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,oBAAoB,CAAC;YAiBlB,WAAW;IA+BzB,OAAO,CAAC,UAAU;YAQJ,mBAAmB;IAKjC,2CAA2C;IACrC,UAAU;IAShB,OAAO,CAAC,WAAW;CAGpB"}
|
|
@@ -18,8 +18,17 @@ export declare class DownloadBlobTask {
|
|
|
18
18
|
/** Represents a size in bytes of the downloaded blob. */
|
|
19
19
|
size: number;
|
|
20
20
|
constructor(logger: MiLogger, clientDownload: ClientDownload, rInfo: ResourceSnapshot, path: string, handle: LocalBlobHandle);
|
|
21
|
+
/** Returns a simple object that describes this task. */
|
|
22
|
+
info(): {
|
|
23
|
+
rInfo: ResourceSnapshot;
|
|
24
|
+
path: string;
|
|
25
|
+
done: boolean;
|
|
26
|
+
size: number;
|
|
27
|
+
error: any;
|
|
28
|
+
};
|
|
21
29
|
attach(w: Watcher, callerId: string): void;
|
|
22
30
|
download(): Promise<void>;
|
|
31
|
+
private ensureDownloaded;
|
|
23
32
|
abort(reason: string): void;
|
|
24
33
|
getBlob(): {
|
|
25
34
|
done: false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"download_blob_task.d.ts","sourceRoot":"","sources":["../../src/drivers/download_blob_task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AAC1F,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EACL,cAAc,EACd,YAAY,EAIZ,QAAQ,EACT,MAAM,4BAA4B,CAAC;AAKpC,OAAO,EAAE,cAAc,EAA0C,MAAM,qBAAqB,CAAC;AAG7F,wBAAwB;AACxB,qBAAa,gBAAgB;IAUzB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,QAAQ,CAAC,KAAK,EAAE,gBAAgB;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAbzB,QAAQ,CAAC,OAAO,iBAAwB;IACxC,QAAQ,CAAC,MAAM,eAAsB;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;IACnD,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,IAAI,CAAS;IACrB,yDAAyD;IACzD,IAAI,SAAK;gBAGU,MAAM,EAAE,QAAQ,EAChB,cAAc,EAAE,cAAc,EACtC,KAAK,EAAE,gBAAgB,EACvB,IAAI,EAAE,MAAM,EACJ,MAAM,EAAE,eAAe;
|
|
1
|
+
{"version":3,"file":"download_blob_task.d.ts","sourceRoot":"","sources":["../../src/drivers/download_blob_task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AAC1F,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EACL,cAAc,EACd,YAAY,EAIZ,QAAQ,EACT,MAAM,4BAA4B,CAAC;AAKpC,OAAO,EAAE,cAAc,EAA0C,MAAM,qBAAqB,CAAC;AAG7F,wBAAwB;AACxB,qBAAa,gBAAgB;IAUzB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,QAAQ,CAAC,KAAK,EAAE,gBAAgB;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM;IAbzB,QAAQ,CAAC,OAAO,iBAAwB;IACxC,QAAQ,CAAC,MAAM,eAAsB;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;IACnD,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,IAAI,CAAS;IACrB,yDAAyD;IACzD,IAAI,SAAK;gBAGU,MAAM,EAAE,QAAQ,EAChB,cAAc,EAAE,cAAc,EACtC,KAAK,EAAE,gBAAgB,EACvB,IAAI,EAAE,MAAM,EACJ,MAAM,EAAE,eAAe;IAG1C,wDAAwD;IACjD,IAAI;;;;;;;IAUJ,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM;IAK7B,QAAQ;YAiBP,gBAAgB;IAmBvB,KAAK,CAAC,MAAM,EAAE,MAAM;IAIpB,OAAO,IACV;QAAE,IAAI,EAAE,KAAK,CAAA;KAAE,GACf;QACE,IAAI,EAAE,IAAI,CAAC;QACX,MAAM,EAAE,YAAY,CAAC,sBAAsB,CAAC,CAAC;KAC9C;IAqBL,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,QAAQ;CAIjB;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,GAAG,WAWzC"}
|
|
@@ -60,6 +60,13 @@ declare class DownloadByUrlTask {
|
|
|
60
60
|
done: boolean;
|
|
61
61
|
size: number;
|
|
62
62
|
constructor(logger: MiLogger, path: string, url: URL);
|
|
63
|
+
info(): {
|
|
64
|
+
url: string;
|
|
65
|
+
path: string;
|
|
66
|
+
done: boolean;
|
|
67
|
+
size: number;
|
|
68
|
+
error: string | undefined;
|
|
69
|
+
};
|
|
63
70
|
attach(w: Watcher, callerId: string): void;
|
|
64
71
|
download(clientDownload: RemoteFileDownloader, withGunzip: boolean): Promise<void>;
|
|
65
72
|
private downloadAndUntar;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"download_url.d.ts","sourceRoot":"","sources":["../../src/drivers/download_url.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAC9F,OAAO,EACL,cAAc,EACd,QAAQ,EAMT,MAAM,4BAA4B,CAAC;AAOpC,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAmB,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"download_url.d.ts","sourceRoot":"","sources":["../../src/drivers/download_url.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAC9F,OAAO,EACL,cAAc,EACd,QAAQ,EAMT,MAAM,4BAA4B,CAAC;AAOpC,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAmB,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAK5E,MAAM,WAAW,qBAAqB;IACpC;;sEAEkE;IAClE,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,UAAU,CAAC,UAAU,GAAG,SAAS,CAAC,CAAC;CACvD;AAED,MAAM,WAAW,UAAU;IACzB,iFAAiF;IACjF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,4DAA4D;IAC5D,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,UAAU,EAAE,OAAO,CAAC;IACpB,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF;qCACqC;AACrC,qBAAa,iBAAkB,YAAW,qBAAqB;IAW3D,OAAO,CAAC,QAAQ,CAAC,MAAM;IAEvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAbvB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAuB;IAEtD,OAAO,CAAC,aAAa,CAA6C;IAClE,OAAO,CAAC,aAAa,CAAgB;IAErC;mCAC+B;IAC/B,OAAO,CAAC,KAAK,CAAgC;gBAG1B,MAAM,EAAE,QAAQ,EACjC,UAAU,EAAE,UAAU,EACL,OAAO,EAAE,MAAM,EACf,IAAI,GAAE,oBAItB;IAOH,2DAA2D;IAC3D,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,aAAa,GAAG,UAAU,GAAG,SAAS;IAE7D,4CAA4C;IAC5C,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,UAAU,CAAC,UAAU,GAAG,SAAS,CAAC;IAuBrD,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM;IAkBnD,wEAAwE;IAClE,WAAW,CAAC,IAAI,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM;IAM3D;mCAC+B;IACzB,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA+B5D,2CAA2C;IACrC,UAAU;IAgBhB,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,UAAU;IAMlB,OAAO,CAAC,WAAW;CAIpB;AAED,wDAAwD;AACxD,cAAM,iBAAiB;IASnB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,QAAQ,CAAC,GAAG,EAAE,GAAG;IAVnB,QAAQ,CAAC,OAAO,iBAAwB;IACxC,QAAQ,CAAC,MAAM,eAAsB;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;IACnD,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,IAAI,UAAS;IACb,IAAI,SAAK;gBAGU,MAAM,EAAE,QAAQ,EACxB,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,GAAG;IAGZ,IAAI;;;;;;;IAUX,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM;IAK7B,QAAQ,CAAC,cAAc,EAAE,oBAAoB,EAAE,UAAU,EAAE,OAAO;YAkB1D,gBAAgB;IA4B9B,OAAO,IAAI,UAAU,GAAG,SAAS;IAQjC,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,QAAQ;IAIhB,KAAK,CAAC,MAAM,EAAE,MAAM;CAGrB"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var Je=Object.defineProperty;var Ze=(i,e,t)=>e in i?Je(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var h=(i,e,t)=>Ze(i,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("@milaboratories/pl-client"),Qe=require("node:fs/promises"),ue=require("undici"),f=require("@protobuf-ts/runtime-rpc"),s=require("@protobuf-ts/runtime"),p=require("@milaboratories/ts-helpers"),pe=require("node:fs"),v=require("node:path"),L=require("node:stream"),Q=require("node:stream/consumers"),g=require("@milaboratories/computable"),m=require("@milaboratories/pl-tree"),Xe=require("denque"),k=require("node:crypto"),ge=require("node:os"),Ye=require("node:readline/promises"),b=require("zod"),fe=require("node:timers/promises"),Ke=require("node:assert"),et=require("node:zlib"),tt=require("tar-fs"),rt=require("@milaboratories/pl-model-common"),nt=require("node:util"),ot=require("node:child_process");function P(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const y=P(Qe),X=P(pe),T=P(v),se=P(ge),it=P(Ye),st=P(et),at=P(tt);class lt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new lt;class ct extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ct;class dt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ht=new dt;class ut extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Response",[{no:1,name:"parts_count",kind:"scalar",T:4,L:0},{no:2,name:"uploaded_parts",kind:"scalar",repeat:1,T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.partsCount=0n,t.uploadedParts=[],e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.partsCount=e.uint64().toBigInt();break;case 2:if(d===s.WireType.LengthDelimited)for(let $=e.int32()+e.pos;e.pos<$;)o.uploadedParts.push(e.uint64().toBigInt());else o.uploadedParts.push(e.uint64().toBigInt());break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){if(e.partsCount!==0n&&t.tag(1,s.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,s.WireType.LengthDelimited).fork();for(let o=0;o<e.uploadedParts.length;o++)t.uint64(e.uploadedParts[o]);t.join()}let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const pt=new ut;class gt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new gt;class ft extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"bytes_processed",kind:"scalar",T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.bytesProcessed=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.bytesProcessed=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).int64(e.bytesProcessed);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const mt=new ft;class wt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const yt=new wt;class Tt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Tt;class bt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"part_number",kind:"scalar",T:4,L:0},{no:3,name:"uploaded_part_size",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.partNumber=0n,t.uploadedPartSize=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.partNumber=e.uint64().toBigInt();break;case 3:o.uploadedPartSize=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,s.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,s.WireType.Varint).uint64(e.uploadedPartSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const kt=new bt;class Lt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const _=new Lt;class Pt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Response",[{no:1,name:"upload_url",kind:"scalar",T:9},{no:2,name:"method",kind:"scalar",T:9},{no:3,name:"headers",kind:"message",repeat:1,T:()=>_},{no:4,name:"chunk_start",kind:"scalar",T:4,L:0},{no:5,name:"chunk_end",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.uploadUrl="",t.method="",t.headers=[],t.chunkStart=0n,t.chunkEnd=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.uploadUrl=e.string();break;case 2:o.method=e.string();break;case 3:o.headers.push(_.internalBinaryRead(e,e.uint32(),r));break;case 4:o.chunkStart=e.uint64().toBigInt();break;case 5:o.chunkEnd=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.uploadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.method);for(let o=0;o<e.headers.length;o++)_.internalBinaryWrite(e.headers[o],t.tag(3,s.WireType.LengthDelimited).fork(),r).join();e.chunkStart!==0n&&t.tag(4,s.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,s.WireType.Varint).uint64(e.chunkEnd);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ut=new Pt;class It extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new It;class Rt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const St=new Rt;class Dt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Nt=new Dt,W=new f.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:ht,O:pt},{name:"GetPartURL",options:{},I:kt,O:Ut},{name:"UpdateProgress",options:{},I:mt,O:yt},{name:"Finalize",options:{},I:St,O:Nt}]);class vt{constructor(e){h(this,"typeName",W.typeName);h(this,"methods",W.methods);h(this,"options",W.options);this._transport=e}init(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}getPartURL(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}updateProgress(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}finalize(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}function me(i){return Object.fromEntries(i.map(({name:e,value:t})=>[e,t]))}class Y extends Error{}class K extends Error{}class we extends Error{}class ee extends Error{}class ye{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=n,this.grpcClient=new vt(this.grpcTransport)}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,o,c){const a=await this.grpcGetPartUrl({id:e,type:t},o,0n,c),d=await Bt(r,a.chunkStart,a.chunkEnd);await Ct(r,n);const{body:l,statusCode:u,headers:$}=await ue.request(a.uploadUrl,{dispatcher:this.httpClient,body:d,headers:me(a.headers),method:a.method.toUpperCase()}),qe=await l.text();$t(u,qe,$,a),await this.grpcUpdateProgress({id:e,type:t},a.chunkEnd-a.chunkStart,c)}async finalize(e,t){return await this.grpcFinalize(e,t)}partsToUpload(e,t){const r=[],n=new Set(t);for(let o=1n;o<=e;o++)n.has(o)||r.push(o);return r}async grpcInit(e,t,r){return await this.grpcClient.init({resourceId:e},w.addRTypeToMetadata(t,r)).response}async grpcGetPartUrl({id:e,type:t},r,n,o){return await this.grpcClient.getPartURL({resourceId:e,partNumber:r,uploadedPartSize:n},w.addRTypeToMetadata(t,o)).response}async grpcUpdateProgress({id:e,type:t},r,n){await this.grpcClient.updateProgress({resourceId:e,bytesProcessed:r},w.addRTypeToMetadata(t,n)).response}async grpcFinalize({id:e,type:t},r){return await this.grpcClient.finalize({resourceId:e},w.addRTypeToMetadata(t,r)).response}}async function Bt(i,e,t){let r;try{r=await y.open(i);const n=Number(t-e),o=Number(e),c=Buffer.alloc(n),a=await Ft(r,c,n,o);return c.subarray(0,a)}catch(n){throw n.code=="ENOENT"?new ee(`there is no file ${i} for uploading`):n}finally{r==null||r.close()}}async function Ft(i,e,t,r){let n=0;for(;n<t;){const{bytesRead:o}=await i.read(e,n,t-n,r+n);if(o===0)throw new K("file ended earlier than expected.");n+=o}return n}async function Ct(i,e){const t=BigInt(Math.floor((await y.stat(i)).mtimeMs/1e3));if(t>e)throw new Y(`file was modified, expected mtime: ${e}, got: ${t}.`)}function $t(i,e,t,r){if(i!=200)throw new we(`response is not ok, status code: ${i}, body: ${e}, headers: ${t}, url: ${r.uploadUrl}`)}class _t extends s.MessageType{constructor(){super("google.protobuf.Duration",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}internalJsonWrite(e,t){let r=s.PbLong.from(e.seconds).toNumber();if(r>315576e6||r<-315576e6)throw new Error("Duration value out of range.");let n=e.seconds.toString();if(r===0&&e.nanos<0&&(n="-"+n),e.nanos!==0){let o=Math.abs(e.nanos).toString();o="0".repeat(9-o.length)+o,o.substring(3)==="000000"?o=o.substring(0,3):o.substring(6)==="000"&&(o=o.substring(0,6)),n+="."+o}return n+"s"}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+s.typeofJsonValue(e)+". Expected string.");let n=e.match(/^(-?)([0-9]+)(?:\.([0-9]+))?s/);if(n===null)throw new Error("Unable to parse Duration from JSON string. Invalid format.");r||(r=this.create());let[,o,c,a]=n,d=s.PbLong.from(o+c);if(d.toNumber()>315576e6||d.toNumber()<-315576e6)throw new Error("Unable to parse Duration from JSON string. Value out of range.");if(r.seconds=d.toBigInt(),typeof a=="string"){let l=o+a+"0".repeat(9-a.length);r.nanos=parseInt(l)}return r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const N=new _t;class Wt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Wt;class Mt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.Report",[{no:1,name:"progress",kind:"scalar",T:2},{no:2,name:"bytes_processed",kind:"scalar",T:4,L:0},{no:3,name:"bytes_total",kind:"scalar",T:4,L:0},{no:4,name:"done",kind:"scalar",T:8},{no:5,name:"name",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.progress=0,t.bytesProcessed=0n,t.bytesTotal=0n,t.done=!1,t.name="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.progress=e.float();break;case 2:o.bytesProcessed=e.uint64().toBigInt();break;case 3:o.bytesTotal=e.uint64().toBigInt();break;case 4:o.done=e.bool();break;case 5:o.name=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.progress!==0&&t.tag(1,s.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,s.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,s.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,s.WireType.LengthDelimited).string(e.name);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const I=new Mt;class Ot extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ot;class Et extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ht=new Et;class zt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&I.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const xt=new zt;class At extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new At;class jt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"update_interval",kind:"message",T:()=>N}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.updateInterval=N.internalBinaryRead(e,e.uint32(),r,o.updateInterval);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.updateInterval&&N.internalBinaryWrite(e.updateInterval,t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Gt=new jt;class Vt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&I.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const qt=new Vt,M=new f.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:Ht,O:xt},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:Gt,O:qt}]);class Jt{constructor(e){h(this,"typeName",M.typeName);h(this,"methods",M.methods);h(this,"options",M.options);this._transport=e}getStatus(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}realtimeStatus(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}}class Te{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.client=r,this.logger=n,this.grpcClient=new Jt(this.grpcTransport)}close(){}async getStatus({id:e,type:t},r){const n=await this.grpcClient.getStatus({resourceId:e},w.addRTypeToMetadata(t,r)),o=p.notEmpty(n.response.report);return{done:o.done,progress:o.progress,bytesProcessed:String(o.bytesProcessed),bytesTotal:String(o.bytesTotal)}}async*realtimeStatus({id:e,type:t},r=100,n){n=w.addRTypeToMetadata(t,n);const o=Math.floor(r/1e3),c=(r-o*1e3)*1e6,a=N.create({seconds:BigInt(o),nanos:c});try{const{responses:d}=this.grpcClient.realtimeStatus({resourceId:e,updateInterval:a},n);yield*d}catch(d){throw this.logger.warn("Failed to get realtime status"+d),d}}}class te extends Error{}class be{constructor(e){this.httpClient=e}async download(e,t,r){const{statusCode:n,body:o,headers:c}=await ue.request(e,{dispatcher:this.httpClient,headers:t,signal:r}),a=L.Readable.toWeb(o);return await Zt(n,a,e),{content:a,size:Number(c["content-length"])}}}async function Zt(i,e,t){if(i!=200){const r=(await Q.text(e)).substring(0,1e3);throw 400<=i&&i<500?new te(`Http error: statusCode: ${i} url: ${t.toString()}, beginning of body: ${r}`):new Error(`Http error: statusCode: ${i} url: ${t.toString()}`)}}function U(i){if(!v.isAbsolute(i))throw new Error(`Path ${i} is not absolute.`);return i}class Qt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Qt;class Xt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Xt;class Yt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Kt=new Yt;class er extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const O=new er;class tr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Response",[{no:1,name:"download_url",kind:"scalar",T:9},{no:2,name:"headers",kind:"message",repeat:1,T:()=>O}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.downloadUrl=e.string();break;case 2:o.headers.push(O.internalBinaryRead(e,e.uint32(),r));break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.downloadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.downloadUrl);for(let o=0;o<e.headers.length;o++)O.internalBinaryWrite(e.headers[o],t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const rr=new tr,E=new f.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:Kt,O:rr}]);class nr{constructor(e){h(this,"typeName",E.typeName);h(this,"methods",E.methods);h(this,"options",E.options);this._transport=e}getDownloadURL(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class ke{constructor(e,t,r,n){h(this,"grpcClient");h(this,"remoteFileDownloader");h(this,"localStorageIdsToRoot");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new nr(this.grpcTransport),this.remoteFileDownloader=new be(t),this.localStorageIdsToRoot=Ue(n)}close(){}async downloadBlob(e,t,r){const{downloadUrl:n,headers:o}=await this.grpcGetDownloadUrl(e,t,r);return this.logger.info(`download blob from url ${n}`),ir(n)?await this.readLocalFile(n):await this.remoteFileDownloader.download(n,me(o),r)}async readLocalFile(e){const{storageId:t,relativePath:r}=Le(e),n=Pe(t,this.localStorageIdsToRoot,r);return{content:L.Readable.toWeb(X.createReadStream(n)),size:(await y.stat(n)).size}}async grpcGetDownloadUrl({id:e,type:t},r,n){const o=r??{};return o.abort=n,await this.grpcClient.getDownloadURL({resourceId:e},w.addRTypeToMetadata(t,o)).response}}function Le(i){const e=new URL(i);if(e.pathname=="")throw new re(`url for local filepath ${i} does not match url scheme`);return{storageId:e.host,relativePath:decodeURIComponent(e.pathname.slice(1))}}function Pe(i,e,t){const r=e.get(i);if(r===void 0)throw new ne(`Unknown storage location: ${i}`);return r===""?t:T.join(r,t)}const or="storage://";function ir(i){return i.startsWith(or)}class re extends Error{}class ne extends Error{}function Ue(i){const e=new Map;for(const t of i)t.localPath!==""&&U(t.localPath),e.set(t.storageId,t.localPath);return e}class sr extends s.MessageType{constructor(){super("google.protobuf.Timestamp",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}now(){const e=this.create(),t=Date.now();return e.seconds=s.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(s.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),r=e.getTime();return t.seconds=s.PbLong.from(Math.floor(r/1e3)).toBigInt(),t.nanos=r%1e3*1e6,t}internalJsonWrite(e,t){let r=s.PbLong.from(e.seconds).toNumber()*1e3;if(r<Date.parse("0001-01-01T00:00:00Z")||r>Date.parse("9999-12-31T23:59:59Z"))throw new Error("Unable to encode Timestamp to JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");if(e.nanos<0)throw new Error("Unable to encode invalid Timestamp to JSON. Nanos must not be negative.");let n="Z";if(e.nanos>0){let o=(e.nanos+1e9).toString().substring(1);o.substring(3)==="000000"?n="."+o.substring(0,3)+"Z":o.substring(6)==="000"?n="."+o.substring(0,6)+"Z":n="."+o+"Z"}return new Date(r).toISOString().replace(".000Z",n)}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Timestamp from JSON "+s.typeofJsonValue(e)+".");let n=e.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|\.([0-9]{3,9})Z|([+-][0-9][0-9]:[0-9][0-9]))$/);if(!n)throw new Error("Unable to parse Timestamp from JSON. Invalid format.");let o=Date.parse(n[1]+"-"+n[2]+"-"+n[3]+"T"+n[4]+":"+n[5]+":"+n[6]+(n[8]?n[8]:"Z"));if(Number.isNaN(o))throw new Error("Unable to parse Timestamp from JSON. Invalid value.");if(o<Date.parse("0001-01-01T00:00:00Z")||o>Date.parse("9999-12-31T23:59:59Z"))throw new globalThis.Error("Unable to parse Timestamp from JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");return r||(r=this.create()),r.seconds=s.PbLong.from(o/1e3).toBigInt(),r.nanos=0,n[7]&&(r.nanos=parseInt("1"+n[7]+"0".repeat(9-n[7].length))-1e9),r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const H=new sr;class ar extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ar;class lr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.ListItem",[{no:1,name:"name",kind:"scalar",T:9},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"is_dir",kind:"scalar",T:8},{no:10,name:"full_name",kind:"scalar",T:9},{no:11,name:"directory",kind:"scalar",T:9},{no:12,name:"last_modified",kind:"message",T:()=>H},{no:13,name:"version",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.size=0n,t.isDir=!1,t.fullName="",t.directory="",t.version="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.isDir=e.bool();break;case 10:o.fullName=e.string();break;case 11:o.directory=e.string();break;case 12:o.lastModified=H.internalBinaryRead(e,e.uint32(),r,o.lastModified);break;case 13:o.version=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,s.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,s.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,s.WireType.LengthDelimited).string(e.directory),e.lastModified&&H.internalBinaryWrite(e.lastModified,t.tag(12,s.WireType.LengthDelimited).fork(),r).join(),e.version!==""&&t.tag(13,s.WireType.LengthDelimited).string(e.version);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const z=new lr;class cr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new cr;class dr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"location",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.location="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.location=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.location);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const hr=new dr;class ur extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:1,T:()=>z},{no:2,name:"delimiter",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.items=[],t.delimiter="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.items.push(z.internalBinaryRead(e,e.uint32(),r));break;case 2:o.delimiter=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){for(let o=0;o<e.items.length;o++)z.internalBinaryWrite(e.items[o],t.tag(1,s.WireType.LengthDelimited).fork(),r).join();e.delimiter!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.delimiter);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const pr=new ur,x=new f.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:hr,O:pr}]);class gr{constructor(e){h(this,"typeName",x.typeName);h(this,"methods",x.methods);h(this,"options",x.options);this._transport=e}list(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Ie{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=new gr(e)}close(){}async list(e,t,r){return await this.grpcClient.list({resourceId:e.id,location:t},w.addRTypeToMetadata(e.type,r)).response}}class fr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new fr;class mr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;case 20:o.readLimit=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const wr=new mr;class yr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Tr=new yr;class br extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const kr=new br;class Lr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Pr=new Lr;class Ur extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.LastLines",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",opt:!0,T:3,L:0},{no:3,name:"line_count",kind:"scalar",opt:!0,T:5},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 3:o.lineCount=e.int32();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,s.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,s.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ir=new Ur;class Rr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.Response",[{no:1,name:"data",kind:"scalar",T:12},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"new_offset",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.data=new Uint8Array(0),t.size=0n,t.newOffset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.data=e.bytes();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.newOffset=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.data.length&&t.tag(1,s.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,s.WireType.Varint).uint64(e.newOffset);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const R=new Rr,A=new f.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:wr,O:R},{name:"ReadBinary",options:{},I:Tr,O:R},{name:"StreamText",serverStreaming:!0,options:{},I:kr,O:R},{name:"ReadText",options:{},I:Pr,O:R},{name:"LastLines",options:{},I:Ir,O:R}]);class Sr{constructor(e){h(this,"typeName",A.typeName);h(this,"methods",A.methods);h(this,"options",A.options);this._transport=e}streamBinary(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}readBinary(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}streamText(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return f.stackIntercept("serverStreaming",this._transport,r,n,e)}readText(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}lastLines(e,t){const r=this.methods[4],n=this._transport.mergeOptions(t);return f.stackIntercept("unary",this._transport,r,n,e)}}class Re{constructor(e,t,r){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new Sr(this.grpcTransport)}close(){}async lastLines({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.lastLines({resourceId:e,lineCount:r,offset:n,search:o},w.addRTypeToMetadata(t,c))).response}async readText({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.readText({resourceId:p.notEmpty(e),readLimit:BigInt(r),offset:n,search:o},w.addRTypeToMetadata(t,c))).response}}function Dr(i,e,t){return e.getDriver({name:"DownloadBlob",init:(r,n,o)=>new ke(n,o,i,t)})}function Nr(i,e){return i.getDriver({name:"StreamLogs",init:(t,r,n)=>new Re(r,n,e)})}function vr(i,e){return i.getDriver({name:"UploadProgress",init:(t,r,n)=>new Te(r,n,i,e)})}function Br(i,e){return i.getDriver({name:"UploadBlob",init:(t,r,n)=>new ye(r,n,i,e)})}function Se(i,e){return i.getDriver({name:"LsFiles",init:(t,r,n)=>new Ie(r,e)})}class Fr{constructor(e,t,r,n,o){h(this,"counter",new p.CallersCounter);h(this,"change",new g.ChangeSource);h(this,"signalCtl",new AbortController);h(this,"error");h(this,"done",!1);h(this,"size",0);this.logger=e,this.clientDownload=t,this.rInfo=r,this.path=n,this.handle=o}attach(e,t){this.counter.inc(t),this.done||this.change.attachWatcher(e)}async download(){try{await p.ensureDirExists(T.dirname(this.path));const{content:e,size:t}=await this.clientDownload.downloadBlob(this.rInfo);await p.fileExists(this.path)?await e.cancel("the file already exists."):await p.createPathAtomically(this.logger,this.path,async r=>{const n=L.Writable.toWeb(pe.createWriteStream(r,{flags:"wx"}));await e.pipeTo(n)}),this.setDone(t),this.change.markChanged()}catch(e){throw De(e)&&(this.setError(e),this.change.markChanged(),await y.rm(this.path)),e}}abort(e){this.signalCtl.abort(new Ne(e))}getBlob(){return this.done?this.error?{done:!0,result:{ok:!1,error:this.error}}:{done:!0,result:{ok:!0,value:{handle:this.handle,size:this.size}}}:{done:!1}}setDone(e){this.done=!0,this.size=e}setError(e){this.done=!0,this.error=e}}function De(i){return i instanceof Ne||i instanceof te||i instanceof ne||i instanceof re||(i==null?void 0:i.code)=="ENOENT"||i.name=="RpcError"&&(i.code=="NOT_FOUND"||i.code=="ABORTED")}class Ne extends Error{}class ve{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 p.mapGet(this.cache,e).counter.dec(t),this.toDelete()}toDelete(){if(this.totalSizeBytes<=this.softSizeBytes)return[];const e=[];let t=0;return p.mapEntries(this.cache).filter(([r,n])=>n.counter.isZero()).forEach(([r,n])=>{if(this.totalSizeBytes-t<=this.softSizeBytes)return;const o=p.mapGet(this.cache,r);t+=o.size,e.push(o)}),e}addCache(e,t){const r=this.cache.get(e.path)==null;if(this.cache.set(e.path,e),e.counter.inc(t),e.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 Be=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function Cr(i,e){return`blob+local://download/${i}#${e.sign(i)}`}function $r(i){return!!i.match(Be)}function j(i,e){const t=i.match(Be);if(t===null)throw new Error(`Local handle is malformed: ${i}, matches: ${t}`);const{path:r,signature:n}=t.groups;return e.verify(r,n,`Signature verification failed for: ${i}`),{path:r,signature:n}}const Fe=m.rsSchema({kv:{"ctl/file/blobInfo":b.z.object({sizeBytes:b.z.coerce.number()})}});function Ce(i){return i.kv["ctl/file/blobInfo"].sizeBytes}const D=b.z.object({localPath:b.z.string(),pathSignature:b.z.string(),sizeBytes:b.z.string(),modificationTime:b.z.string()}),oe=b.z.object({storageId:b.z.string(),path:b.z.string()}),_r=b.z.union([D,oe]),$e=m.rsSchema({data:D,fields:{blob:!1}}),_e=m.rsSchema({fields:{incarnation:!1}}),We=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*))#(?<signature>.*)$/;function Wr(i,e){const t=`${i.type.name}/${i.type.version}/${BigInt(i.id)}`;return`blob+remote://download/${t}#${e.sign(t)}`}function Mr(i){return!!i.match(We)}function Or(i,e){const t=i.match(We);if(t===null)throw new Error(`Remote handle is malformed: ${i}, matches: ${t}`);const{content:r,resourceType:n,resourceVersion:o,resourceId:c,signature:a}=t.groups;return e.verify(r,a,`Signature verification failed for ${i}`),{id:w.bigintToResourceId(BigInt(c)),type:{name:n,version:o}}}function Me(i,e){return i?`log+live://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`:`log+ready://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`}const Oe=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function B(i){return Oe.test(i)}const Ee=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function Er(i){return Ee.test(i)}function F(i){let e;if(B(i))e=i.match(Oe);else if(Er(i))e=i.match(Ee);else throw new Error(`Log handle is malformed: ${i}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${i}`);const{resourceType:t,resourceVersion:r,resourceId:n}=e.groups;return{id:w.bigintToResourceId(BigInt(n)),type:{name:t,version:r}}}class C extends Error{}class He{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}})())}}class Hr{constructor(e,t,r,n,o,c){h(this,"idToDownload",new Map);h(this,"cache");h(this,"downloadQueue");h(this,"idToOnDemand",new Map);h(this,"idToLastLines",new Map);h(this,"idToProgressLog",new Map);h(this,"saveDir");this.logger=e,this.clientDownload=t,this.clientLogs=r,this.signer=o,this.cache=new ve(c.cacheSoftSizeBytes),this.downloadQueue=new p.TaskProcessor(this.logger,c.nConcurrentDownloads),this.saveDir=T.resolve(n)}getDownloadedBlob(e,t){if(t===void 0)return g.Computable.make(c=>this.getDownloadedBlob(e,c));const r=m.treeEntryToResourceInfo(e,t),n=k.randomUUID();t.addOnDestroy(()=>this.releaseBlob(r.id,n));const o=this.getDownloadedBlobNoCtx(t.watcher,r,n);return o==null&&t.markUnstable("download blob is still undefined"),o}getDownloadedBlobNoCtx(e,t,r){S("getDownloadedBlob",t.type);let n=this.idToDownload.get(t.id);if(n===void 0){const c=this.setNewDownloadTask(t);this.downloadQueue.push({fn:()=>this.downloadBlob(c,r),recoverableErrorPredicate:a=>!De(a)}),n=c}n.attach(e,r);const o=n.getBlob();if(o.done){if(o.result.ok)return o.result.value;throw o.result.error}}setNewDownloadTask(e){const t=this.getFilePath(e.id),r=new Fr(this.logger,this.clientDownload,e,t,Cr(t,this.signer));return this.idToDownload.set(e.id,r),r}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 g.Computable.make(c=>this.getOnDemandBlob(e,c));const r=m.isPlTreeEntry(e)?m.makeResourceSnapshot(e,Fe,t):e,n=k.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(r.id,n)),this.getOnDemandBlobNoCtx(r,n)}getOnDemandBlobNoCtx(e,t){S("getOnDemandBlob",e.type);let r=this.idToOnDemand.get(e.id);return r===void 0&&(r=new zr(Ce(e),Wr(e,this.signer)),this.idToOnDemand.set(e.id,r)),r.attach(t),r.getHandle()}getLocalPath(e){const{path:t}=j(e,this.signer);return t}async getContent(e){if($r(e))return await Ar(this.getLocalPath(e));if(Mr(e)){const t=Or(e,this.signer),{content:r}=await this.clientDownload.downloadBlob(t);return await Q.buffer(r)}throw new Error("Malformed remote handle")}getLastLogs(e,t,r){if(r==null)return g.Computable.make(a=>this.getLastLogs(e,t,a));const n=m.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return c==null&&r.markUnstable("either a file was not downloaded or logs was not read"),c}getLastLogsNoCtx(e,t,r,n){S("getLastLogs",t.type);const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const{path:c}=j(o.handle,this.signer);let a=this.idToLastLines.get(t.id);if(a==null){const l=new ae(c,r);this.idToLastLines.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getProgressLog(e,t,r){if(r==null)return g.Computable.make(a=>this.getProgressLog(e,t,a));const n=m.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return c===void 0&&r.markUnstable("either a file was not downloaded or a progress log was not read"),c}getProgressLogNoCtx(e,t,r,n){S("getProgressLog",t.type);const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const{path:c}=j(o.handle,this.signer);let a=this.idToProgressLog.get(t.id);if(a==null){const l=new ae(c,1,r);this.idToProgressLog.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getLogHandle(e,t){if(t==null)return g.Computable.make(n=>this.getLogHandle(e,n));const r=m.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(r)}getLogHandleNoCtx(e){return S("getLogHandle",e.type),Me(!1,e)}async lastLines(e,t,r,n){const o=await this.clientLogs.lastLines(F(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async readText(e,t,r,n){const o=await this.clientLogs.readText(F(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async releaseBlob(e,t){const r=this.idToDownload.get(e);if(r!=null)if(this.cache.existsFile(r.path)){const n=this.cache.removeFile(r.path,t);await Promise.all(n.map(async o=>{await y.rm(o.path),this.cache.removeCache(o),this.removeTask(o,`the task ${o.path} was removedfrom cache along with ${n.map(c=>c.path)}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${r.path} was removed from cache`)}removeTask(e,t){e.abort(t),e.change.markChanged(),this.idToDownload.delete(e.rInfo.id),this.idToLastLines.delete(e.rInfo.id),this.idToProgressLog.delete(e.rInfo.id)}async releaseOnDemandBlob(e,t){var n;(((n=this.idToOnDemand.get(e))==null?void 0:n.release(t))??!1)&&this.idToOnDemand.delete(e)}async releaseAll(){this.downloadQueue.stop(),this.idToDownload.forEach((e,t)=>{this.idToDownload.delete(t),e.change.markChanged()})}getFilePath(e){return T.resolve(this.saveDir,String(BigInt(e)))}}class zr{constructor(e,t){h(this,"counter",new p.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 ae{constructor(e,t,r){h(this,"updater");h(this,"log");h(this,"change",new g.ChangeSource);h(this,"error");this.path=e,this.lines=t,this.patternToSearch=r,this.updater=new He(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 xr(this.path,this.lines,this.patternToSearch);this.log!=e&&this.change.markChanged(),this.log=e}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.log="",this.error=e,this.change.markChanged();return}throw e}}}function xr(i,e,t){const r=X.createReadStream(i),n=new L.Writable;return new Promise((o,c)=>{const a=it.createInterface(r,n),d=new Xe;a.on("line",function(l){t!=null&&!l.includes(t)||(d.push(l),d.length>e&&d.shift())}),a.on("error",c),a.on("close",function(){o(d.toArray().join(se.EOL)+se.EOL)})})}async function Ar(i){return await Q.buffer(L.Readable.toWeb(X.createReadStream(i)))}function S(i,e){if(!e.name.startsWith("Blob/")){let t=`${i}: 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 C(t)}}class jr{constructor(e,t,r,n,o,c){h(this,"change",new g.ChangeSource);h(this,"counter",new p.CallersCounter);h(this,"uploadData");h(this,"progress");h(this,"failed");this.logger=e,this.clientBlob=t,this.clientProgress=r,this.nConcurrentPartsUpload=n,this.res=c;const{uploadData:a,progress:d}=Gr(c,o);this.uploadData=a,this.progress=d}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 Vr(this.progress)}shouldScheduleUpload(){return this.progress.isUpload&&this.progress.isUploadSignMatch}async uploadBlobTask(){Ke(J(this.res),"the upload operation can be done only for BlobUploads");try{if(this.isComputableDone())return;const e=await this.clientBlob.initUpload(this.res);this.logger.info(`started to upload blob ${this.res.id}, parts overall: ${e.overall}, parts remained: ${e.toUpload.length}`);const t=r=>async()=>{this.isComputableDone()||(await this.clientBlob.partUpload(this.res,this.uploadData.localPath,BigInt(this.uploadData.modificationTime),r),this.logger.info(`uploaded chunk ${r}/${e.overall} of resource: ${this.res.id}`))};if(await p.asyncPool(this.nConcurrentPartsUpload,e.toUpload.map(t)),this.isComputableDone())return;await this.clientBlob.finalize(this.res),this.logger.info(`uploading of resource ${this.res.id} finished.`),this.change.markChanged()}catch(e){if(this.setRetriableError(e),le(e)){this.logger.warn(`resource was deleted while uploading a blob: ${e}`),this.change.markChanged(),this.setDone(!0);return}if(this.logger.error(`error while uploading a blob: ${e}`),this.change.markChanged(),ze(e)){this.setTerminalError(e);return}throw e}}async updateStatus(){try{const e=await this.clientProgress.getStatus(this.res),t=this.progress.status;this.progress.status=Zr(e),this.setDone(e.done),(e.done||e.progress!=(t==null?void 0:t.progress))&&this.change.markChanged()}catch(e){if(this.setRetriableError(e),e.name=="RpcError"&&e.code=="DEADLINE_EXCEEDED"){this.logger.warn("deadline exceeded while getting a status of BlobImport");return}if(le(e)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${e}, ${w.stringifyWithResourceId(this.res)}`),this.change.markChanged(),this.setDone(!0);return}this.logger.error(`error while updating a status of BlobImport: ${e}`),this.change.markChanged(),this.setTerminalError(e)}}setRetriableError(e){this.progress.lastError=String(e)}setTerminalError(e){this.progress.lastError=String(e),this.progress.done=!1,this.failed=!0}setDoneIfOutputSet(e){qr(e)&&this.setDone(!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()}}function Gr(i,e){let t,r;return J(i)&&(r=D.parse(i.data),t=Jr(e,r.localPath,r.pathSignature)),{uploadData:r,progress:{done:!1,status:void 0,isUpload:J(i),isUploadSignMatch:t,lastError:void 0}}}function Vr(i){return i.done,i.isUpload,i.isUploadSignMatch,i.lastError,i.status&&(i.status.progress,i.status.bytesProcessed,i.status.bytesTotal),i}function qr(i){return"blob"in i.fields?i.fields.blob!==void 0:i.fields.incarnation!==void 0}function J(i){return i.type.name.startsWith("BlobUpload")}function Jr(i,e,t){try{return i.verify(e,t),!0}catch{return!1}}function Zr(i){return{progress:i.progress??0,bytesProcessed:Number(i.bytesProcessed),bytesTotal:Number(i.bytesTotal)}}function le(i){return i.name=="RpcError"&&(i.code=="NOT_FOUND"||i.code=="ABORTED"||i.code=="ALREADY_EXISTS")}function ze(i){return i instanceof Y||i instanceof K||i instanceof ee}function xe(i,e){const t=m.isPlTreeEntry(i)?e.accessor(i).node():m.isPlTreeEntryAccessor(i)?i.node():i;return t.resourceType.name.startsWith("BlobUpload")?m.makeResourceSnapshot(t,$e):m.makeResourceSnapshot(t,_e)}class Qr{constructor(e,t,r,n,o={nConcurrentPartUploads:10,nConcurrentGetProgresses:10,pollingInterval:1e3,stopPollingDelay:1e3}){h(this,"idToProgress",new Map);h(this,"uploadQueue");h(this,"hooks");h(this,"scheduledOnNextState",[]);h(this,"keepRunning",!1);h(this,"currentLoop");this.logger=e,this.signer=t,this.clientBlob=r,this.clientProgress=n,this.opts=o,this.uploadQueue=new p.TaskProcessor(this.logger,1,{type:"exponentialWithMaxDelayBackoff",initialDelay:20,maxDelay:15e3,backoffMultiplier:1.5,jitter:.5}),this.hooks=new g.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:o.stopPollingDelay},(c,a)=>this.scheduleOnNextState(c,a))}getProgressId(e,t){if(t==null)return g.Computable.make(c=>this.getProgressId(e,c));const r=m.isPlTreeEntry(e)?xe(e,t):e,n=k.randomUUID();return t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(r.id,n)),this.getProgressIdNoCtx(t.watcher,r,n)}getProgressIdNoCtx(e,t,r){Yr("getProgressId",t.type);const n=this.idToProgress.get(t.id);if(n!=null)return n.setDoneIfOutputSet(t),n.getProgress(e,r);const o=new jr(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,o),o.shouldScheduleUpload()&&this.uploadQueue.push({fn:()=>o.uploadBlobTask(),recoverableErrorPredicate:c=>!ze(c)}),o.setDoneIfOutputSet(t),o.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 p.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 fe.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!Xr(t.progress)).map(([e,t])=>t)}}function Xr(i){var e;return i.done&&(((e=i.status)==null?void 0:e.progress)??0)>=1}function Yr(i,e){if(!e.name.startsWith("BlobUpload")&&!e.name.startsWith("BlobIndex"))throw new C(`${i}: wrong resource type: ${e.name}, expected: a resource of either type 'BlobUpload' or 'BlobIndex'.`)}class Kr{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 g.PollingComputableHooks(()=>this.startUpdating(),()=>this.stopUpdating(),{stopDebounce:r.stopPollingDelay},(n,o)=>this.scheduleOnNextState(n,o))}getLastLogs(e,t,r){if(r==null)return g.Computable.make(a=>this.getLastLogs(e,t,a));const n=m.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseLastLogs(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return r.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),c}getLastLogsNoCtx(e,t,r,n){G("getLastLogs",t.type);let o=this.idToLastLines.get(t.id);if(o==null){const a=new ce(this.logger,this.clientLogs,t,r);this.idToLastLines.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error!=null)throw c.error;return c.log}getProgressLog(e,t,r){if(r==null)return g.Computable.make(a=>this.getProgressLog(e,t,a));const n=m.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseProgressLog(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return r.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),c}getProgressLogNoCtx(e,t,r,n){G("getProgressLog",t.type);let o=this.idToProgressLog.get(t.id);if(o==null){const a=new ce(this.logger,this.clientLogs,t,1,r);this.idToProgressLog.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return g.Computable.make(o=>this.getLogHandle(e,o));const r=m.treeEntryToResourceInfo(e,t),n=this.getLogHandleNoCtx(r);return t.markUnstable(`live_log:${w.resourceIdToString(r.id)}`),n}getLogHandleNoCtx(e){return G("getLogHandle",e.type),Me(!0,e)}async lastLines(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(F(e),t,BigInt(r??0),n))}async readText(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(F(e),t,BigInt(r??0),n))}async tryWithNotFound(e,t){if(!B(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 p.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 fe.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 ce{constructor(e,t,r,n,o){h(this,"logs");h(this,"error");h(this,"change",new g.ChangeSource);h(this,"counter",new p.CallersCounter);this.logger=e,this.clientLogs=t,this.rInfo=r,this.lines=n,this.patternToSearch=o}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 t=(await this.clientLogs.lastLines(this.rInfo,this.lines,0n,this.patternToSearch)).data.toString();this.logs!=t&&this.change.markChanged(),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 ${w.stringifyWithResourceId(this.rInfo.id)} failed, reason: ${e}`),e}}}function G(i,e){if(!e.name.startsWith("StreamWorkdir"))throw new C(`${i}: wrong resource type: ${e.name}, expected: a resource of type 'StreamWorkdir'.`)}class en{constructor(e,t,r){this.logger=e,this.logsStreamDriver=t,this.downloadDriver=r}getLastLogs(e,t,r){if(r===void 0)return g.Computable.make(o=>this.getLastLogs(e,t,o));const n=q(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(V(n))return this.downloadDriver.getLastLogs(n,t,r);try{return this.logsStreamDriver.getLastLogs(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${o}`);return}throw o}}getProgressLog(e,t,r){if(r===void 0)return g.Computable.make(o=>this.getProgressLog(e,t,o));const n=q(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(V(n))return this.downloadDriver.getProgressLog(n,t,r);try{return this.logsStreamDriver.getProgressLog(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${o}`);return}throw o}}getLogHandle(e,t){if(t===void 0)return g.Computable.make(n=>this.getLogHandle(e,n));const r=q(t,e);if(r===void 0){t.markUnstable("no stream in stream manager");return}return V(r)?this.downloadDriver.getLogHandle(r,t):this.logsStreamDriver.getLogHandle(r,t)}async lastLines(e,t,r,n){return B(e)?await this.logsStreamDriver.lastLines(e,t,r,n):await this.downloadDriver.lastLines(e,t,r,n)}async readText(e,t,r,n){return B(e)?await this.logsStreamDriver.readText(e,t,r,n):await this.downloadDriver.readText(e,t,r,n)}}function V(i){return!i.type.name.startsWith("StreamWorkdir")}function q(i,e){var t;return(t=i.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}class tn{constructor(e,t,r,n={cacheSoftSizeBytes:50*1024*1024,withGunzip:!0,nConcurrentDownloads:50}){h(this,"downloadHelper");h(this,"urlToDownload",new Map);h(this,"downloadQueue");h(this,"cache");this.logger=e,this.saveDir=r,this.opts=n,this.downloadQueue=new p.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new ve(this.opts.cacheSoftSizeBytes),this.downloadHelper=new be(t)}getPath(e,t){if(t===void 0)return g.Computable.make(o=>this.getPath(e,o));const r=k.randomUUID();t.addOnDestroy(()=>this.releasePath(e,r));const n=this.getPathNoCtx(e,t.watcher,r);return(n==null?void 0:n.path)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${n}`),n}getPathNoCtx(e,t,r){const n=e.toString(),o=this.urlToDownload.get(n);if(o!=null)return o.attach(t,r),o.getPath();const c=this.setNewTask(t,e,r);return this.downloadQueue.push({fn:async()=>this.downloadUrl(c,r),recoverableErrorPredicate:a=>!0}),c.getPath()}async downloadUrl(e,t){var r;await e.download(this.downloadHelper,this.opts.withGunzip),((r=e.getPath())==null?void 0:r.path)!=null&&this.cache.addCache(e,t)}async releasePath(e,t){const r=e.toString(),n=this.urlToDownload.get(r);if(n!=null)if(this.cache.existsFile(n.path)){const o=this.cache.removeFile(n.path,t);await Promise.all(o.map(async c=>{await Z(c.path),this.cache.removeCache(c),this.removeTask(c,`the task ${JSON.stringify(c)} was removedfrom cache along with ${JSON.stringify(o)}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${JSON.stringify(n)} was removed from cache`)}async releaseAll(){this.downloadQueue.stop(),await Promise.all(Array.from(this.urlToDownload.entries()).map(async([e,t])=>{await Z(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${t} was released when the driver was closed`)}))}setNewTask(e,t,r){const n=new rn(this.logger,this.getFilePath(t),t);return n.attach(e,r),this.urlToDownload.set(t.toString(),n),n}removeTask(e,t){e.abort(t),e.change.markChanged(),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=k.createHash("sha256").update(e.toString()).digest("hex");return T.join(this.saveDir,t)}}class rn{constructor(e,t,r){h(this,"counter",new p.CallersCounter);h(this,"change",new g.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}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()}catch(r){if(r instanceof de||r instanceof te){this.setError(r),this.change.markChanged(),await Z(this.path);return}throw r}}async downloadAndUntar(e,t,r){if(await p.ensureDirExists(T.dirname(this.path)),await p.fileExists(this.path))return await Ae(this.path);const n=await e.download(this.url.toString(),{},r);let o=n.content;if(t){const c=L.Transform.toWeb(st.createGunzip());o=o.pipeThrough(c,{signal:r})}return await p.createPathAtomically(this.logger,this.path,async c=>{await y.mkdir(c);const a=L.Writable.toWeb(at.extract(c));await o.pipeTo(a,{signal:r})}),n.size}getPath(){if(this.done)return{path:p.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 de(e))}}class de extends Error{}async function Ae(i){const e=await y.readdir(i,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=T.join(i,r.name);return r.isDirectory()?await Ae(n):(await y.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function Z(i){await y.rm(i,{recursive:!0,force:!0})}function he(i,e){return`index://index/${encodeURIComponent(JSON.stringify({storageId:i,path:e}))}`}function nn(i,e,t,r){const n={localPath:i,pathSignature:e.sign(i),sizeBytes:String(t),modificationTime:String(r)};return`upload://upload/${encodeURIComponent(JSON.stringify(n))}`}function on(i){const e=new URL(i);return D.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function sn(i){const e=new URL(i);return oe.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function an(i){if(hn(i))return pn(i);if(ln(i))return dn(i);p.assertNever(i)}const je=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function ln(i){return je.test(i)}function cn(i,e){return`local://${i}/${encodeURIComponent(e)}`}function dn(i){const e=i.match(je);if(e==null)throw new Error(`Local list handle wasn't parsed: ${i}`);const{name:t,path:r}=e.groups;return{rootPath:decodeURIComponent(r),name:t,isRemote:!1}}const Ge=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function hn(i){return Ge.test(i)}function un(i,e){return`remote://${i}/${BigInt(e)}`}function pn(i){const e=i.match(Ge);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${i}`);const{name:t,resourceId:r}=e.groups;return{id:w.bigintToResourceId(BigInt(r)),type:gn(t),name:t,isRemote:!0}}function gn(i){return{name:`LS/${i}`,version:"1"}}async function Ve(){const i=ge.homedir();if(v.sep=="/")return[{name:"local",root:"/",initialPath:i}];{const t=v.parse(i).root.replaceAll(":\\","");try{return(await nt.promisify(ot.exec)("wmic logicaldisk get name")).stdout.split(`\r
|
|
2
|
-
`).filter(o=>o.includes(":")).map(o=>o.trim().replaceAll(":","")).map(o=>{const c=o==t;return{name:`local_disk_${o}`,root:`${o}:\\`,initialPath:c?i:`${o}:\\`}})}catch{return[{name:`local_disk_${t}`,root:`${t}:\\`,initialPath:i}]}}}class ie{constructor(e,t,r,n,o,c,a){this.logger=e,this.lsClient=t,this.storageIdToResourceId=r,this.signer=n,this.virtualStoragesMap=o,this.localProjectionsMap=c,this.openFileDialogCallback=a}async getLocalFileContent(e,t){const r=await this.tryResolveLocalFileHandle(e);if(t)throw new Error("Range request not yet supported.");return await y.readFile(r)}async getLocalFileSize(e){const t=await this.tryResolveLocalFileHandle(e);return(await y.stat(t)).size}async showOpenMultipleFilesDialog(e){const t=await this.openFileDialogCallback(!0,e);return t===void 0?{}:{files:await Promise.all(t.map(r=>this.getLocalFileHandle(r)))}}async showOpenSingleFileDialog(e){const t=await this.openFileDialogCallback(!1,e);return t===void 0?{}:{file:await this.getLocalFileHandle(t[0])}}async tryResolveLocalFileHandle(e){if(rt.isImportFileHandleIndex(e)){const t=sn(e),r=this.localProjectionsMap.get(t.storageId);if(!r)throw new Error(`Storage ${t.storageId} is not mounted locally.`);return T.join(r.localPath,t.path)}else{const t=on(e);this.signer.verify(t.localPath,t.pathSignature,"Failed to validate local file handle signature.");const r=t.localPath,n=await y.stat(r,{bigint:!0});if(String(n.mtimeMs/1000n)!==t.modificationTime)throw new Error("File has changed since the handle was created.");return r}}async getLocalFileHandle(e){U(e);for(const r of this.localProjectionsMap.values())if(e.startsWith(r.localPath)){const n=r.localPath===""?e:T.relative(r.localPath,e);return he(r.storageId,n)}const t=await y.stat(e,{bigint:!0});return nn(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){const e=[...this.virtualStoragesMap.values()].map(n=>({name:n.name,handle:cn(n.name,n.root),initialFullPath:n.initialPath})),r=Object.entries(this.storageIdToResourceId).map(([n,o])=>({name:n,handle:un(n,o),initialFullPath:"",isInitialPathHome:!1})).filter(n=>n.name!=="root");return[...e,...r]}async listFiles(e,t){const r=an(e);if(r.isRemote)return{entries:(await this.lsClient.list(r,t)).items.map(a=>({type:a.isDir?"dir":"file",name:a.name,fullPath:a.fullName,handle:he(r.name,a.fullName)}))};T.sep==="/"&&t===""&&(t="/"),r.rootPath===""&&U(t);const n=T.isAbsolute(t)?t:T.join(r.rootPath,t),o=[];for await(const c of await y.opendir(n)){if(!c.isFile()&&!c.isDirectory())continue;const a=T.join(n,c.name);o.push({type:c.isFile()?"file":"dir",name:c.name,fullPath:a,handle:await this.getLocalFileHandle(a)})}return{entries:o}}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,o,c){const a=Se(t,e);c||(c=await Ve());for(const u of c)U(u.root);for(const u of n)u.localPath!==""&&U(u.localPath);const d=new Map(c.map(u=>[u.name,u])),l=new Map(n.map(u=>[u.storageId,u]));if(new Set([...d.keys(),...l.keys()]).size!==d.size+l.size)throw new Error("Intersection between local projection storage ids and virtual storages names detected.");return new ie(e,a,await fn(t),r,d,l,o)}}async function fn(i){return i.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),r=await e.getResourceData(t,!0);return mn(r)})}function mn(i){return Object.fromEntries(i.fields.filter(e=>e.type=="Dynamic"&&
|
|
1
|
+
"use strict";var Je=Object.defineProperty;var Ze=(i,e,t)=>e in i?Je(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var h=(i,e,t)=>Ze(i,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("@milaboratories/pl-client"),Qe=require("node:fs/promises"),ue=require("undici"),m=require("@protobuf-ts/runtime-rpc"),s=require("@protobuf-ts/runtime"),p=require("@milaboratories/ts-helpers"),pe=require("node:fs"),v=require("node:path"),L=require("node:stream"),Q=require("node:stream/consumers"),f=require("@milaboratories/computable"),w=require("@milaboratories/pl-tree"),Xe=require("denque"),k=require("node:crypto"),ge=require("node:os"),Ye=require("node:readline/promises"),b=require("zod"),fe=require("node:timers/promises"),Ke=require("node:assert"),et=require("node:zlib"),tt=require("tar-fs"),rt=require("@milaboratories/pl-model-common"),nt=require("node:util"),ot=require("node:child_process");function P(i){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(i){for(const t in i)if(t!=="default"){const r=Object.getOwnPropertyDescriptor(i,t);Object.defineProperty(e,t,r.get?r:{enumerable:!0,get:()=>i[t]})}}return e.default=i,Object.freeze(e)}const y=P(Qe),X=P(pe),T=P(v),se=P(ge),it=P(Ye),st=P(et),at=P(tt);class lt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new lt;class ct extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ct;class dt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const ht=new dt;class ut extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Init.Response",[{no:1,name:"parts_count",kind:"scalar",T:4,L:0},{no:2,name:"uploaded_parts",kind:"scalar",repeat:1,T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.partsCount=0n,t.uploadedParts=[],e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.partsCount=e.uint64().toBigInt();break;case 2:if(d===s.WireType.LengthDelimited)for(let $=e.int32()+e.pos;e.pos<$;)o.uploadedParts.push(e.uint64().toBigInt());else o.uploadedParts.push(e.uint64().toBigInt());break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){if(e.partsCount!==0n&&t.tag(1,s.WireType.Varint).uint64(e.partsCount),e.uploadedParts.length){t.tag(2,s.WireType.LengthDelimited).fork();for(let o=0;o<e.uploadedParts.length;o++)t.uint64(e.uploadedParts[o]);t.join()}let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const pt=new ut;class gt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new gt;class ft extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"bytes_processed",kind:"scalar",T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.bytesProcessed=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.bytesProcessed=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).int64(e.bytesProcessed);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const mt=new ft;class wt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.UpdateProgress.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const yt=new wt;class Tt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Tt;class bt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"part_number",kind:"scalar",T:4,L:0},{no:3,name:"uploaded_part_size",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.partNumber=0n,t.uploadedPartSize=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.partNumber=e.uint64().toBigInt();break;case 3:o.uploadedPartSize=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.partNumber!==0n&&t.tag(2,s.WireType.Varint).uint64(e.partNumber),e.uploadedPartSize!==0n&&t.tag(3,s.WireType.Varint).uint64(e.uploadedPartSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const kt=new bt;class Lt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const _=new Lt;class Pt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.GetPartURL.Response",[{no:1,name:"upload_url",kind:"scalar",T:9},{no:2,name:"method",kind:"scalar",T:9},{no:3,name:"headers",kind:"message",repeat:1,T:()=>_},{no:4,name:"chunk_start",kind:"scalar",T:4,L:0},{no:5,name:"chunk_end",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.uploadUrl="",t.method="",t.headers=[],t.chunkStart=0n,t.chunkEnd=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.uploadUrl=e.string();break;case 2:o.method=e.string();break;case 3:o.headers.push(_.internalBinaryRead(e,e.uint32(),r));break;case 4:o.chunkStart=e.uint64().toBigInt();break;case 5:o.chunkEnd=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.uploadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.uploadUrl),e.method!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.method);for(let o=0;o<e.headers.length;o++)_.internalBinaryWrite(e.headers[o],t.tag(3,s.WireType.LengthDelimited).fork(),r).join();e.chunkStart!==0n&&t.tag(4,s.WireType.Varint).uint64(e.chunkStart),e.chunkEnd!==0n&&t.tag(5,s.WireType.Varint).uint64(e.chunkEnd);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ut=new Pt;class It extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new It;class Rt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const St=new Rt;class Dt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.uploadapi.Finalize.Response",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Nt=new Dt,W=new m.ServiceType("MiLaboratories.Controller.Shared.Upload",[{name:"Init",options:{},I:ht,O:pt},{name:"GetPartURL",options:{},I:kt,O:Ut},{name:"UpdateProgress",options:{},I:mt,O:yt},{name:"Finalize",options:{},I:St,O:Nt}]);class vt{constructor(e){h(this,"typeName",W.typeName);h(this,"methods",W.methods);h(this,"options",W.options);this._transport=e}init(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}getPartURL(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}updateProgress(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}finalize(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}}function me(i){return Object.fromEntries(i.map(({name:e,value:t})=>[e,t]))}class Y extends Error{}class K extends Error{}class we extends Error{}class ee extends Error{}class ye{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=n,this.grpcClient=new vt(this.grpcTransport)}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,o,c){const a=await this.grpcGetPartUrl({id:e,type:t},o,0n,c),d=await Bt(r,a.chunkStart,a.chunkEnd);await Ct(r,n);const{body:l,statusCode:u,headers:$}=await ue.request(a.uploadUrl,{dispatcher:this.httpClient,body:d,headers:me(a.headers),method:a.method.toUpperCase()}),qe=await l.text();$t(u,qe,$,a),await this.grpcUpdateProgress({id:e,type:t},a.chunkEnd-a.chunkStart,c)}async finalize(e,t){return await this.grpcFinalize(e,t)}partsToUpload(e,t){const r=[],n=new Set(t);for(let o=1n;o<=e;o++)n.has(o)||r.push(o);return r}async grpcInit(e,t,r){return await this.grpcClient.init({resourceId:e},g.addRTypeToMetadata(t,r)).response}async grpcGetPartUrl({id:e,type:t},r,n,o){return await this.grpcClient.getPartURL({resourceId:e,partNumber:r,uploadedPartSize:n},g.addRTypeToMetadata(t,o)).response}async grpcUpdateProgress({id:e,type:t},r,n){await this.grpcClient.updateProgress({resourceId:e,bytesProcessed:r},g.addRTypeToMetadata(t,n)).response}async grpcFinalize({id:e,type:t},r){return await this.grpcClient.finalize({resourceId:e},g.addRTypeToMetadata(t,r)).response}}async function Bt(i,e,t){let r;try{r=await y.open(i);const n=Number(t-e),o=Number(e),c=Buffer.alloc(n),a=await Ft(r,c,n,o);return c.subarray(0,a)}catch(n){throw n.code=="ENOENT"?new ee(`there is no file ${i} for uploading`):n}finally{r==null||r.close()}}async function Ft(i,e,t,r){let n=0;for(;n<t;){const{bytesRead:o}=await i.read(e,n,t-n,r+n);if(o===0)throw new K("file ended earlier than expected.");n+=o}return n}async function Ct(i,e){const t=BigInt(Math.floor((await y.stat(i)).mtimeMs/1e3));if(t>e)throw new Y(`file was modified, expected mtime: ${e}, got: ${t}.`)}function $t(i,e,t,r){if(i!=200)throw new we(`response is not ok, status code: ${i}, body: ${e}, headers: ${t}, url: ${r.uploadUrl}`)}class _t extends s.MessageType{constructor(){super("google.protobuf.Duration",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}internalJsonWrite(e,t){let r=s.PbLong.from(e.seconds).toNumber();if(r>315576e6||r<-315576e6)throw new Error("Duration value out of range.");let n=e.seconds.toString();if(r===0&&e.nanos<0&&(n="-"+n),e.nanos!==0){let o=Math.abs(e.nanos).toString();o="0".repeat(9-o.length)+o,o.substring(3)==="000000"?o=o.substring(0,3):o.substring(6)==="000"&&(o=o.substring(0,6)),n+="."+o}return n+"s"}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Duration from JSON "+s.typeofJsonValue(e)+". Expected string.");let n=e.match(/^(-?)([0-9]+)(?:\.([0-9]+))?s/);if(n===null)throw new Error("Unable to parse Duration from JSON string. Invalid format.");r||(r=this.create());let[,o,c,a]=n,d=s.PbLong.from(o+c);if(d.toNumber()>315576e6||d.toNumber()<-315576e6)throw new Error("Unable to parse Duration from JSON string. Value out of range.");if(r.seconds=d.toBigInt(),typeof a=="string"){let l=o+a+"0".repeat(9-a.length);r.nanos=parseInt(l)}return r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const N=new _t;class Wt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Wt;class Mt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.Report",[{no:1,name:"progress",kind:"scalar",T:2},{no:2,name:"bytes_processed",kind:"scalar",T:4,L:0},{no:3,name:"bytes_total",kind:"scalar",T:4,L:0},{no:4,name:"done",kind:"scalar",T:8},{no:5,name:"name",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.progress=0,t.bytesProcessed=0n,t.bytesTotal=0n,t.done=!1,t.name="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.progress=e.float();break;case 2:o.bytesProcessed=e.uint64().toBigInt();break;case 3:o.bytesTotal=e.uint64().toBigInt();break;case 4:o.done=e.bool();break;case 5:o.name=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.progress!==0&&t.tag(1,s.WireType.Bit32).float(e.progress),e.bytesProcessed!==0n&&t.tag(2,s.WireType.Varint).uint64(e.bytesProcessed),e.bytesTotal!==0n&&t.tag(3,s.WireType.Varint).uint64(e.bytesTotal),e.done!==!1&&t.tag(4,s.WireType.Varint).bool(e.done),e.name!==""&&t.tag(5,s.WireType.LengthDelimited).string(e.name);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const I=new Mt;class Ot extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Ot;class Et extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ht=new Et;class zt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.GetStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&I.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const xt=new zt;class At extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new At;class jt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"update_interval",kind:"message",T:()=>N}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.updateInterval=N.internalBinaryRead(e,e.uint32(),r,o.updateInterval);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.updateInterval&&N.internalBinaryWrite(e.updateInterval,t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Gt=new jt;class Vt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.ProgressAPI.RealtimeStatus.Response",[{no:1,name:"report",kind:"message",T:()=>I}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.report=I.internalBinaryRead(e,e.uint32(),r,o.report);break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.report&&I.internalBinaryWrite(e.report,t.tag(1,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const qt=new Vt,M=new m.ServiceType("MiLaboratories.Controller.Shared.Progress",[{name:"GetStatus",options:{"google.api.http":{get:"/resources/{resource_id}/get-progress"}},I:Ht,O:xt},{name:"RealtimeStatus",serverStreaming:!0,options:{},I:Gt,O:qt}]);class Jt{constructor(e){h(this,"typeName",M.typeName);h(this,"methods",M.methods);h(this,"options",M.options);this._transport=e}getStatus(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}realtimeStatus(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,r,n,e)}}class Te{constructor(e,t,r,n){h(this,"grpcClient");this.grpcTransport=e,this.client=r,this.logger=n,this.grpcClient=new Jt(this.grpcTransport)}close(){}async getStatus({id:e,type:t},r){const n=await this.grpcClient.getStatus({resourceId:e},g.addRTypeToMetadata(t,r)),o=p.notEmpty(n.response.report);return{done:o.done,progress:o.progress,bytesProcessed:String(o.bytesProcessed),bytesTotal:String(o.bytesTotal)}}async*realtimeStatus({id:e,type:t},r=100,n){n=g.addRTypeToMetadata(t,n);const o=Math.floor(r/1e3),c=(r-o*1e3)*1e6,a=N.create({seconds:BigInt(o),nanos:c});try{const{responses:d}=this.grpcClient.realtimeStatus({resourceId:e,updateInterval:a},n);yield*d}catch(d){throw this.logger.warn("Failed to get realtime status"+d),d}}}class te extends Error{}class be{constructor(e){this.httpClient=e}async download(e,t,r){const{statusCode:n,body:o,headers:c}=await ue.request(e,{dispatcher:this.httpClient,headers:t,signal:r}),a=L.Readable.toWeb(o);return await Zt(n,a,e),{content:a,size:Number(c["content-length"])}}}async function Zt(i,e,t){if(i!=200){const r=(await Q.text(e)).substring(0,1e3);throw 400<=i&&i<500?new te(`Http error: statusCode: ${i} url: ${t.toString()}, beginning of body: ${r}`):new Error(`Http error: statusCode: ${i} url: ${t.toString()}`)}}function U(i){if(!v.isAbsolute(i))throw new Error(`Path ${i} is not absolute.`);return i}class Qt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Qt;class Xt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new Xt;class Yt extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Kt=new Yt;class er extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.HTTPHeader",[{no:1,name:"Name",kind:"scalar",jsonName:"Name",T:9},{no:2,name:"Value",kind:"scalar",jsonName:"Value",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.value="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.value=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.value!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.value);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const O=new er;class tr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.DownloadAPI.GetDownloadURL.Response",[{no:1,name:"download_url",kind:"scalar",T:9},{no:2,name:"headers",kind:"message",repeat:1,T:()=>O}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.downloadUrl="",t.headers=[],e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.downloadUrl=e.string();break;case 2:o.headers.push(O.internalBinaryRead(e,e.uint32(),r));break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.downloadUrl!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.downloadUrl);for(let o=0;o<e.headers.length;o++)O.internalBinaryWrite(e.headers[o],t.tag(2,s.WireType.LengthDelimited).fork(),r).join();let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const rr=new tr,E=new m.ServiceType("MiLaboratories.Controller.Shared.Download",[{name:"GetDownloadURL",options:{"google.api.http":{get:"/resources/{resource_id}/get-download-url"}},I:Kt,O:rr}]);class nr{constructor(e){h(this,"typeName",E.typeName);h(this,"methods",E.methods);h(this,"options",E.options);this._transport=e}getDownloadURL(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}}class ke{constructor(e,t,r,n){h(this,"grpcClient");h(this,"remoteFileDownloader");h(this,"localStorageIdsToRoot");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new nr(this.grpcTransport),this.remoteFileDownloader=new be(t),this.localStorageIdsToRoot=Ue(n)}close(){}async downloadBlob(e,t,r){const{downloadUrl:n,headers:o}=await this.grpcGetDownloadUrl(e,t,r);return this.logger.info(`download blob from url ${n}`),ir(n)?await this.readLocalFile(n):await this.remoteFileDownloader.download(n,me(o),r)}async readLocalFile(e){const{storageId:t,relativePath:r}=Le(e),n=Pe(t,this.localStorageIdsToRoot,r);return{content:L.Readable.toWeb(X.createReadStream(n)),size:(await y.stat(n)).size}}async grpcGetDownloadUrl({id:e,type:t},r,n){const o=r??{};return o.abort=n,await this.grpcClient.getDownloadURL({resourceId:e},g.addRTypeToMetadata(t,o)).response}}function Le(i){const e=new URL(i);if(e.pathname=="")throw new re(`url for local filepath ${i} does not match url scheme`);return{storageId:e.host,relativePath:decodeURIComponent(e.pathname.slice(1))}}function Pe(i,e,t){const r=e.get(i);if(r===void 0)throw new ne(`Unknown storage location: ${i}`);return r===""?t:T.join(r,t)}const or="storage://";function ir(i){return i.startsWith(or)}class re extends Error{}class ne extends Error{}function Ue(i){const e=new Map;for(const t of i)t.localPath!==""&&U(t.localPath),e.set(t.storageId,t.localPath);return e}class sr extends s.MessageType{constructor(){super("google.protobuf.Timestamp",[{no:1,name:"seconds",kind:"scalar",T:3,L:0},{no:2,name:"nanos",kind:"scalar",T:5}])}now(){const e=this.create(),t=Date.now();return e.seconds=s.PbLong.from(Math.floor(t/1e3)).toBigInt(),e.nanos=t%1e3*1e6,e}toDate(e){return new Date(s.PbLong.from(e.seconds).toNumber()*1e3+Math.ceil(e.nanos/1e6))}fromDate(e){const t=this.create(),r=e.getTime();return t.seconds=s.PbLong.from(Math.floor(r/1e3)).toBigInt(),t.nanos=r%1e3*1e6,t}internalJsonWrite(e,t){let r=s.PbLong.from(e.seconds).toNumber()*1e3;if(r<Date.parse("0001-01-01T00:00:00Z")||r>Date.parse("9999-12-31T23:59:59Z"))throw new Error("Unable to encode Timestamp to JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");if(e.nanos<0)throw new Error("Unable to encode invalid Timestamp to JSON. Nanos must not be negative.");let n="Z";if(e.nanos>0){let o=(e.nanos+1e9).toString().substring(1);o.substring(3)==="000000"?n="."+o.substring(0,3)+"Z":o.substring(6)==="000"?n="."+o.substring(0,6)+"Z":n="."+o+"Z"}return new Date(r).toISOString().replace(".000Z",n)}internalJsonRead(e,t,r){if(typeof e!="string")throw new Error("Unable to parse Timestamp from JSON "+s.typeofJsonValue(e)+".");let n=e.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|\.([0-9]{3,9})Z|([+-][0-9][0-9]:[0-9][0-9]))$/);if(!n)throw new Error("Unable to parse Timestamp from JSON. Invalid format.");let o=Date.parse(n[1]+"-"+n[2]+"-"+n[3]+"T"+n[4]+":"+n[5]+":"+n[6]+(n[8]?n[8]:"Z"));if(Number.isNaN(o))throw new Error("Unable to parse Timestamp from JSON. Invalid value.");if(o<Date.parse("0001-01-01T00:00:00Z")||o>Date.parse("9999-12-31T23:59:59Z"))throw new globalThis.Error("Unable to parse Timestamp from JSON. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.");return r||(r=this.create()),r.seconds=s.PbLong.from(o/1e3).toBigInt(),r.nanos=0,n[7]&&(r.nanos=parseInt("1"+n[7]+"0".repeat(9-n[7].length))-1e9),r}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.seconds=0n,t.nanos=0,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.seconds=e.int64().toBigInt();break;case 2:o.nanos=e.int32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.seconds!==0n&&t.tag(1,s.WireType.Varint).int64(e.seconds),e.nanos!==0&&t.tag(2,s.WireType.Varint).int32(e.nanos);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const H=new sr;class ar extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new ar;class lr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.ListItem",[{no:1,name:"name",kind:"scalar",T:9},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"is_dir",kind:"scalar",T:8},{no:10,name:"full_name",kind:"scalar",T:9},{no:11,name:"directory",kind:"scalar",T:9},{no:12,name:"last_modified",kind:"message",T:()=>H},{no:13,name:"version",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.name="",t.size=0n,t.isDir=!1,t.fullName="",t.directory="",t.version="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.name=e.string();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.isDir=e.bool();break;case 10:o.fullName=e.string();break;case 11:o.directory=e.string();break;case 12:o.lastModified=H.internalBinaryRead(e,e.uint32(),r,o.lastModified);break;case 13:o.version=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.name!==""&&t.tag(1,s.WireType.LengthDelimited).string(e.name),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.isDir!==!1&&t.tag(3,s.WireType.Varint).bool(e.isDir),e.fullName!==""&&t.tag(10,s.WireType.LengthDelimited).string(e.fullName),e.directory!==""&&t.tag(11,s.WireType.LengthDelimited).string(e.directory),e.lastModified&&H.internalBinaryWrite(e.lastModified,t.tag(12,s.WireType.LengthDelimited).fork(),r).join(),e.version!==""&&t.tag(13,s.WireType.LengthDelimited).string(e.version);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const z=new lr;class cr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new cr;class dr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Request",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"location",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.location="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.location=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.location!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.location);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const hr=new dr;class ur extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.LsAPI.List.Response",[{no:1,name:"items",kind:"message",repeat:1,T:()=>z},{no:2,name:"delimiter",kind:"scalar",T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.items=[],t.delimiter="",e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.items.push(z.internalBinaryRead(e,e.uint32(),r));break;case 2:o.delimiter=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){for(let o=0;o<e.items.length;o++)z.internalBinaryWrite(e.items[o],t.tag(1,s.WireType.LengthDelimited).fork(),r).join();e.delimiter!==""&&t.tag(2,s.WireType.LengthDelimited).string(e.delimiter);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const pr=new ur,x=new m.ServiceType("MiLaboratories.Controller.Shared.LS",[{name:"List",options:{},I:hr,O:pr}]);class gr{constructor(e){h(this,"typeName",x.typeName);h(this,"methods",x.methods);h(this,"options",x.options);this._transport=e}list(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}}class Ie{constructor(e,t){h(this,"grpcClient");this.logger=t,this.grpcClient=new gr(e)}close(){}async list(e,t,r){return await this.grpcClient.list({resourceId:e.id,location:t},g.addRTypeToMetadata(e.type,r)).response}}class fr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI",[])}create(e){const t=globalThis.Object.create(this.messagePrototype);return e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){return n??this.create()}internalBinaryWrite(e,t,r){let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}new fr;class mr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;case 20:o.readLimit=e.int64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const wr=new mr;class yr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadBinary",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:11,name:"chunk_size",kind:"scalar",opt:!0,T:13}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 11:o.chunkSize=e.uint32();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.chunkSize!==void 0&&t.tag(11,s.WireType.Varint).uint32(e.chunkSize);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Tr=new yr;class br extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.StreamText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const kr=new br;class Lr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.ReadText",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",T:3,L:0},{no:20,name:"read_limit",kind:"scalar",opt:!0,T:3,L:0},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,t.offset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 20:o.readLimit=e.int64().toBigInt();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==0n&&t.tag(2,s.WireType.Varint).int64(e.offset),e.readLimit!==void 0&&t.tag(20,s.WireType.Varint).int64(e.readLimit),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Pr=new Lr;class Ur extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.LastLines",[{no:1,name:"resource_id",kind:"scalar",T:4,L:0},{no:2,name:"offset",kind:"scalar",opt:!0,T:3,L:0},{no:3,name:"line_count",kind:"scalar",opt:!0,T:5},{no:21,name:"search",kind:"scalar",opt:!0,T:9},{no:22,name:"search_re",kind:"scalar",opt:!0,T:9}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.resourceId=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.resourceId=e.uint64().toBigInt();break;case 2:o.offset=e.int64().toBigInt();break;case 3:o.lineCount=e.int32();break;case 21:o.search=e.string();break;case 22:o.searchRe=e.string();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.resourceId!==0n&&t.tag(1,s.WireType.Varint).uint64(e.resourceId),e.offset!==void 0&&t.tag(2,s.WireType.Varint).int64(e.offset),e.lineCount!==void 0&&t.tag(3,s.WireType.Varint).int32(e.lineCount),e.search!==void 0&&t.tag(21,s.WireType.LengthDelimited).string(e.search),e.searchRe!==void 0&&t.tag(22,s.WireType.LengthDelimited).string(e.searchRe);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const Ir=new Ur;class Rr extends s.MessageType{constructor(){super("MiLaboratories.Controller.Shared.StreamingAPI.Response",[{no:1,name:"data",kind:"scalar",T:12},{no:2,name:"size",kind:"scalar",T:4,L:0},{no:3,name:"new_offset",kind:"scalar",T:4,L:0}])}create(e){const t=globalThis.Object.create(this.messagePrototype);return t.data=new Uint8Array(0),t.size=0n,t.newOffset=0n,e!==void 0&&s.reflectionMergePartial(this,t,e),t}internalBinaryRead(e,t,r,n){let o=n??this.create(),c=e.pos+t;for(;e.pos<c;){let[a,d]=e.tag();switch(a){case 1:o.data=e.bytes();break;case 2:o.size=e.uint64().toBigInt();break;case 3:o.newOffset=e.uint64().toBigInt();break;default:let l=r.readUnknownField;if(l==="throw")throw new globalThis.Error(`Unknown field ${a} (wire type ${d}) for ${this.typeName}`);let u=e.skip(d);l!==!1&&(l===!0?s.UnknownFieldHandler.onRead:l)(this.typeName,o,a,d,u)}}return o}internalBinaryWrite(e,t,r){e.data.length&&t.tag(1,s.WireType.LengthDelimited).bytes(e.data),e.size!==0n&&t.tag(2,s.WireType.Varint).uint64(e.size),e.newOffset!==0n&&t.tag(3,s.WireType.Varint).uint64(e.newOffset);let n=r.writeUnknownFields;return n!==!1&&(n==!0?s.UnknownFieldHandler.onWrite:n)(this.typeName,e,t),t}}const R=new Rr,A=new m.ServiceType("MiLaboratories.Controller.Shared.Streaming",[{name:"StreamBinary",serverStreaming:!0,options:{},I:wr,O:R},{name:"ReadBinary",options:{},I:Tr,O:R},{name:"StreamText",serverStreaming:!0,options:{},I:kr,O:R},{name:"ReadText",options:{},I:Pr,O:R},{name:"LastLines",options:{},I:Ir,O:R}]);class Sr{constructor(e){h(this,"typeName",A.typeName);h(this,"methods",A.methods);h(this,"options",A.options);this._transport=e}streamBinary(e,t){const r=this.methods[0],n=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,r,n,e)}readBinary(e,t){const r=this.methods[1],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}streamText(e,t){const r=this.methods[2],n=this._transport.mergeOptions(t);return m.stackIntercept("serverStreaming",this._transport,r,n,e)}readText(e,t){const r=this.methods[3],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}lastLines(e,t){const r=this.methods[4],n=this._transport.mergeOptions(t);return m.stackIntercept("unary",this._transport,r,n,e)}}class Re{constructor(e,t,r){h(this,"grpcClient");this.grpcTransport=e,this.httpClient=t,this.logger=r,this.grpcClient=new Sr(this.grpcTransport)}close(){}async lastLines({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.lastLines({resourceId:e,lineCount:r,offset:n,search:o},g.addRTypeToMetadata(t,c))).response}async readText({id:e,type:t},r,n=0n,o,c){return(await this.grpcClient.readText({resourceId:p.notEmpty(e),readLimit:BigInt(r),offset:n,search:o},g.addRTypeToMetadata(t,c))).response}}function Dr(i,e,t){return e.getDriver({name:"DownloadBlob",init:(r,n,o)=>new ke(n,o,i,t)})}function Nr(i,e){return i.getDriver({name:"StreamLogs",init:(t,r,n)=>new Re(r,n,e)})}function vr(i,e){return i.getDriver({name:"UploadProgress",init:(t,r,n)=>new Te(r,n,i,e)})}function Br(i,e){return i.getDriver({name:"UploadBlob",init:(t,r,n)=>new ye(r,n,i,e)})}function Se(i,e){return i.getDriver({name:"LsFiles",init:(t,r,n)=>new Ie(r,e)})}class Fr{constructor(e,t,r,n,o){h(this,"counter",new p.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.clientDownload=t,this.rInfo=r,this.path=n,this.handle=o}info(){return{rInfo:this.rInfo,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(){try{const e=await this.ensureDownloaded();this.setDone(e),this.change.markChanged()}catch(e){throw De(e)&&(this.setError(e),this.change.markChanged(),await y.rm(this.path)),e}}async ensureDownloaded(){if(await p.ensureDirExists(T.dirname(this.path)),await p.fileExists(this.path))return this.logger.info(`a blob was already downloaded: ${this.path}`),(await y.stat(this.path)).size;const{content:e,size:t}=await this.clientDownload.downloadBlob(this.rInfo);return await p.createPathAtomically(this.logger,this.path,async r=>{const n=L.Writable.toWeb(pe.createWriteStream(r,{flags:"wx"}));await e.pipeTo(n)}),t}abort(e){this.signalCtl.abort(new Ne(e))}getBlob(){return this.done?this.error?{done:!0,result:{ok:!1,error:this.error}}:{done:!0,result:{ok:!0,value:{handle:this.handle,size:this.size}}}:{done:!1}}setDone(e){this.done=!0,this.size=e}setError(e){this.done=!0,this.error=e}}function De(i){return i instanceof Ne||i instanceof te||i instanceof ne||i instanceof re||(i==null?void 0:i.code)=="ENOENT"||i.name=="RpcError"&&(i.code=="NOT_FOUND"||i.code=="ABORTED")}class Ne extends Error{}class ve{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 p.mapGet(this.cache,e).counter.dec(t),this.toDelete()}toDelete(){if(this.totalSizeBytes<=this.softSizeBytes)return[];const e=[];let t=0;return p.mapEntries(this.cache).filter(([r,n])=>n.counter.isZero()).forEach(([r,n])=>{if(this.totalSizeBytes-t<=this.softSizeBytes)return;const o=p.mapGet(this.cache,r);t+=o.size,e.push(o)}),e}addCache(e,t){const r=this.cache.get(e.path)==null;if(this.cache.set(e.path,e),e.counter.inc(t),e.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 Be=/^blob\+local:\/\/download\/(?<path>.*)#(?<signature>.*)$/;function Cr(i,e){return`blob+local://download/${i}#${e.sign(i)}`}function $r(i){return!!i.match(Be)}function j(i,e){const t=i.match(Be);if(t===null)throw new Error(`Local handle is malformed: ${i}, matches: ${t}`);const{path:r,signature:n}=t.groups;return e.verify(r,n,`Signature verification failed for: ${i}`),{path:r,signature:n}}const Fe=w.rsSchema({kv:{"ctl/file/blobInfo":b.z.object({sizeBytes:b.z.coerce.number()})}});function Ce(i){return i.kv["ctl/file/blobInfo"].sizeBytes}const D=b.z.object({localPath:b.z.string(),pathSignature:b.z.string(),sizeBytes:b.z.string(),modificationTime:b.z.string()}),oe=b.z.object({storageId:b.z.string(),path:b.z.string()}),_r=b.z.union([D,oe]),$e=w.rsSchema({data:D,fields:{blob:!1}}),_e=w.rsSchema({fields:{incarnation:!1}}),We=/^blob\+remote:\/\/download\/(?<content>(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*))#(?<signature>.*)$/;function Wr(i,e){const t=`${i.type.name}/${i.type.version}/${BigInt(i.id)}`;return`blob+remote://download/${t}#${e.sign(t)}`}function Mr(i){return!!i.match(We)}function Or(i,e){const t=i.match(We);if(t===null)throw new Error(`Remote handle is malformed: ${i}, matches: ${t}`);const{content:r,resourceType:n,resourceVersion:o,resourceId:c,signature:a}=t.groups;return e.verify(r,a,`Signature verification failed for ${i}`),{id:g.bigintToResourceId(BigInt(c)),type:{name:n,version:o}}}function Me(i,e){return i?`log+live://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`:`log+ready://log/${e.type.name}/${e.type.version}/${BigInt(e.id)}`}const Oe=/^log\+live:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function B(i){return Oe.test(i)}const Ee=/^log\+ready:\/\/log\/(?<resourceType>.*)\/(?<resourceVersion>.*)\/(?<resourceId>.*)$/;function Er(i){return Ee.test(i)}function F(i){let e;if(B(i))e=i.match(Oe);else if(Er(i))e=i.match(Ee);else throw new Error(`Log handle is malformed: ${i}`);if(e==null)throw new Error(`Log handle wasn't parsed: ${i}`);const{resourceType:t,resourceVersion:r,resourceId:n}=e.groups;return{id:g.bigintToResourceId(BigInt(n)),type:{name:t,version:r}}}class C extends Error{}class He{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}})())}}class Hr{constructor(e,t,r,n,o,c){h(this,"idToDownload",new Map);h(this,"cache");h(this,"downloadQueue");h(this,"idToOnDemand",new Map);h(this,"idToLastLines",new Map);h(this,"idToProgressLog",new Map);h(this,"saveDir");this.logger=e,this.clientDownload=t,this.clientLogs=r,this.signer=o,this.cache=new ve(c.cacheSoftSizeBytes),this.downloadQueue=new p.TaskProcessor(this.logger,c.nConcurrentDownloads),this.saveDir=T.resolve(n)}getDownloadedBlob(e,t){if(t===void 0)return f.Computable.make(c=>this.getDownloadedBlob(e,c));const r=w.treeEntryToResourceInfo(e,t),n=k.randomUUID();t.addOnDestroy(()=>this.releaseBlob(r.id,n));const o=this.getDownloadedBlobNoCtx(t.watcher,r,n);return o==null&&t.markUnstable("download blob is still undefined"),o}getDownloadedBlobNoCtx(e,t,r){S("getDownloadedBlob",t.type);let n=this.idToDownload.get(t.id);if(n===void 0){const c=this.setNewDownloadTask(t);this.downloadQueue.push({fn:()=>this.downloadBlob(c,r),recoverableErrorPredicate:a=>!De(a)}),n=c}n.attach(e,r);const o=n.getBlob();if(o.done){if(o.result.ok)return o.result.value;throw o.result.error}}setNewDownloadTask(e){const t=this.getFilePath(e.id),r=new Fr(this.logger,this.clientDownload,e,t,Cr(t,this.signer));return this.idToDownload.set(e.id,r),r}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(c=>this.getOnDemandBlob(e,c));const r=w.isPlTreeEntry(e)?w.makeResourceSnapshot(e,Fe,t):e,n=k.randomUUID();return t.addOnDestroy(()=>this.releaseOnDemandBlob(r.id,n)),this.getOnDemandBlobNoCtx(r,n)}getOnDemandBlobNoCtx(e,t){S("getOnDemandBlob",e.type);let r=this.idToOnDemand.get(e.id);return r===void 0&&(r=new zr(Ce(e),Wr(e,this.signer)),this.idToOnDemand.set(e.id,r)),r.attach(t),r.getHandle()}getLocalPath(e){const{path:t}=j(e,this.signer);return t}async getContent(e){if($r(e))return await Ar(this.getLocalPath(e));if(Mr(e)){const t=Or(e,this.signer),{content:r}=await this.clientDownload.downloadBlob(t);return await Q.buffer(r)}throw new Error("Malformed remote handle")}getLastLogs(e,t,r){if(r==null)return f.Computable.make(a=>this.getLastLogs(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return c==null&&r.markUnstable("either a file was not downloaded or logs was not read"),c}getLastLogsNoCtx(e,t,r,n){S("getLastLogs",t.type);const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const{path:c}=j(o.handle,this.signer);let a=this.idToLastLines.get(t.id);if(a==null){const l=new ae(c,r);this.idToLastLines.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getProgressLog(e,t,r){if(r==null)return f.Computable.make(a=>this.getProgressLog(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.addOnDestroy(()=>this.releaseBlob(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return c===void 0&&r.markUnstable("either a file was not downloaded or a progress log was not read"),c}getProgressLogNoCtx(e,t,r,n){S("getProgressLog",t.type);const o=this.getDownloadedBlobNoCtx(e,t,n);if(o==null)return;const{path:c}=j(o.handle,this.signer);let a=this.idToProgressLog.get(t.id);if(a==null){const l=new ae(c,1,r);this.idToProgressLog.set(t.id,l),a=l}const d=a.getOrSchedule(e);if(d.error)throw d.error;return d.log}getLogHandle(e,t){if(t==null)return f.Computable.make(n=>this.getLogHandle(e,n));const r=w.treeEntryToResourceInfo(e,t);return this.getLogHandleNoCtx(r)}getLogHandleNoCtx(e){return S("getLogHandle",e.type),Me(!1,e)}async lastLines(e,t,r,n){const o=await this.clientLogs.lastLines(F(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async readText(e,t,r,n){const o=await this.clientLogs.readText(F(e),t,BigInt(r??0),n);return{live:!1,shouldUpdateHandle:!1,data:o.data,size:Number(o.size),newOffset:Number(o.newOffset)}}async releaseBlob(e,t){const r=this.idToDownload.get(e);if(r!=null)if(this.cache.existsFile(r.path)){const n=this.cache.removeFile(r.path,t);await Promise.all(n.map(async o=>{await y.rm(o.path),this.cache.removeCache(o),this.removeTask(o,`the task ${g.stringifyWithResourceId(o.info())} was removedfrom cache along with ${g.stringifyWithResourceId(n.map(c=>c.info()))}`)}))}else r.counter.dec(t)&&this.removeTask(r,`the task ${g.stringifyWithResourceId(r.info())} was removed from cache`)}removeTask(e,t){e.abort(t),e.change.markChanged(),this.idToDownload.delete(e.rInfo.id),this.idToLastLines.delete(e.rInfo.id),this.idToProgressLog.delete(e.rInfo.id)}async releaseOnDemandBlob(e,t){var n;(((n=this.idToOnDemand.get(e))==null?void 0:n.release(t))??!1)&&this.idToOnDemand.delete(e)}async releaseAll(){this.downloadQueue.stop(),this.idToDownload.forEach((e,t)=>{this.idToDownload.delete(t),e.change.markChanged()})}getFilePath(e){return T.resolve(this.saveDir,String(BigInt(e)))}}class zr{constructor(e,t){h(this,"counter",new p.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 ae{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 He(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 xr(this.path,this.lines,this.patternToSearch);this.log!=e&&this.change.markChanged(),this.log=e}catch(e){if(e.name=="RpcError"&&e.code=="NOT_FOUND"){this.log="",this.error=e,this.change.markChanged();return}throw e}}}function xr(i,e,t){const r=X.createReadStream(i),n=new L.Writable;return new Promise((o,c)=>{const a=it.createInterface(r,n),d=new Xe;a.on("line",function(l){t!=null&&!l.includes(t)||(d.push(l),d.length>e&&d.shift())}),a.on("error",c),a.on("close",function(){o(d.toArray().join(se.EOL)+se.EOL)})})}async function Ar(i){return await Q.buffer(L.Readable.toWeb(X.createReadStream(i)))}function S(i,e){if(!e.name.startsWith("Blob/")){let t=`${i}: 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 C(t)}}class jr{constructor(e,t,r,n,o,c){h(this,"change",new f.ChangeSource);h(this,"counter",new p.CallersCounter);h(this,"uploadData");h(this,"progress");h(this,"failed");this.logger=e,this.clientBlob=t,this.clientProgress=r,this.nConcurrentPartsUpload=n,this.res=c;const{uploadData:a,progress:d}=Gr(c,o);this.uploadData=a,this.progress=d}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 Vr(this.progress)}shouldScheduleUpload(){return this.progress.isUpload&&this.progress.isUploadSignMatch}async uploadBlobTask(){Ke(J(this.res),"the upload operation can be done only for BlobUploads");try{if(this.isComputableDone())return;const e=await this.clientBlob.initUpload(this.res);this.logger.info(`started to upload blob ${this.res.id}, parts overall: ${e.overall}, parts remained: ${e.toUpload.length}`);const t=r=>async()=>{this.isComputableDone()||(await this.clientBlob.partUpload(this.res,this.uploadData.localPath,BigInt(this.uploadData.modificationTime),r),this.logger.info(`uploaded chunk ${r}/${e.overall} of resource: ${this.res.id}`))};if(await p.asyncPool(this.nConcurrentPartsUpload,e.toUpload.map(t)),this.isComputableDone())return;await this.clientBlob.finalize(this.res),this.logger.info(`uploading of resource ${this.res.id} finished.`),this.change.markChanged()}catch(e){if(this.setRetriableError(e),le(e)){this.logger.warn(`resource was deleted while uploading a blob: ${e}`),this.change.markChanged(),this.setDone(!0);return}if(this.logger.error(`error while uploading a blob: ${e}`),this.change.markChanged(),ze(e)){this.setTerminalError(e);return}throw e}}async updateStatus(){try{const e=await this.clientProgress.getStatus(this.res),t=this.progress.status;this.progress.status=Zr(e),this.setDone(e.done),(e.done||e.progress!=(t==null?void 0:t.progress))&&this.change.markChanged()}catch(e){if(this.setRetriableError(e),e.name=="RpcError"&&e.code=="DEADLINE_EXCEEDED"){this.logger.warn("deadline exceeded while getting a status of BlobImport");return}if(le(e)){this.logger.warn(`resource was not found while updating a status of BlobImport: ${e}, ${g.stringifyWithResourceId(this.res)}`),this.change.markChanged(),this.setDone(!0);return}this.logger.error(`error while updating a status of BlobImport: ${e}`),this.change.markChanged(),this.setTerminalError(e)}}setRetriableError(e){this.progress.lastError=String(e)}setTerminalError(e){this.progress.lastError=String(e),this.progress.done=!1,this.failed=!0}setDoneIfOutputSet(e){qr(e)&&this.setDone(!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()}}function Gr(i,e){let t,r;return J(i)&&(r=D.parse(i.data),t=Jr(e,r.localPath,r.pathSignature)),{uploadData:r,progress:{done:!1,status:void 0,isUpload:J(i),isUploadSignMatch:t,lastError:void 0}}}function Vr(i){return i.done,i.isUpload,i.isUploadSignMatch,i.lastError,i.status&&(i.status.progress,i.status.bytesProcessed,i.status.bytesTotal),i}function qr(i){return"blob"in i.fields?i.fields.blob!==void 0:i.fields.incarnation!==void 0}function J(i){return i.type.name.startsWith("BlobUpload")}function Jr(i,e,t){try{return i.verify(e,t),!0}catch{return!1}}function Zr(i){return{progress:i.progress??0,bytesProcessed:Number(i.bytesProcessed),bytesTotal:Number(i.bytesTotal)}}function le(i){return i.name=="RpcError"&&(i.code=="NOT_FOUND"||i.code=="ABORTED"||i.code=="ALREADY_EXISTS")}function ze(i){return i instanceof Y||i instanceof K||i instanceof ee}function xe(i,e){const t=w.isPlTreeEntry(i)?e.accessor(i).node():w.isPlTreeEntryAccessor(i)?i.node():i;return t.resourceType.name.startsWith("BlobUpload")?w.makeResourceSnapshot(t,$e):w.makeResourceSnapshot(t,_e)}class Qr{constructor(e,t,r,n,o={nConcurrentPartUploads:10,nConcurrentGetProgresses:10,pollingInterval:1e3,stopPollingDelay:1e3}){h(this,"idToProgress",new Map);h(this,"uploadQueue");h(this,"hooks");h(this,"scheduledOnNextState",[]);h(this,"keepRunning",!1);h(this,"currentLoop");this.logger=e,this.signer=t,this.clientBlob=r,this.clientProgress=n,this.opts=o,this.uploadQueue=new p.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:o.stopPollingDelay},(c,a)=>this.scheduleOnNextState(c,a))}getProgressId(e,t){if(t==null)return f.Computable.make(c=>this.getProgressId(e,c));const r=w.isPlTreeEntry(e)?xe(e,t):e,n=k.randomUUID();return t.attacheHooks(this.hooks),t.addOnDestroy(()=>this.release(r.id,n)),this.getProgressIdNoCtx(t.watcher,r,n)}getProgressIdNoCtx(e,t,r){Yr("getProgressId",t.type);const n=this.idToProgress.get(t.id);if(n!=null)return n.setDoneIfOutputSet(t),n.getProgress(e,r);const o=new jr(this.logger,this.clientBlob,this.clientProgress,this.opts.nConcurrentPartUploads,this.signer,t);return this.idToProgress.set(t.id,o),o.shouldScheduleUpload()&&this.uploadQueue.push({fn:()=>o.uploadBlobTask(),recoverableErrorPredicate:c=>!ze(c)}),o.setDoneIfOutputSet(t),o.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 p.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 fe.scheduler.wait(this.opts.pollingInterval)}this.currentLoop=void 0}getAllNotDoneProgresses(){return Array.from(this.idToProgress.entries()).filter(([e,t])=>!Xr(t.progress)).map(([e,t])=>t)}}function Xr(i){var e;return i.done&&(((e=i.status)==null?void 0:e.progress)??0)>=1}function Yr(i,e){if(!e.name.startsWith("BlobUpload")&&!e.name.startsWith("BlobIndex"))throw new C(`${i}: wrong resource type: ${e.name}, expected: a resource of either type 'BlobUpload' or 'BlobIndex'.`)}class Kr{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,o)=>this.scheduleOnNextState(n,o))}getLastLogs(e,t,r){if(r==null)return f.Computable.make(a=>this.getLastLogs(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseLastLogs(n.id,o));const c=this.getLastLogsNoCtx(r.watcher,n,t,o);return r.markUnstable("The logs are from stream, so we consider them unstable. Final values will be got from blobs."),c}getLastLogsNoCtx(e,t,r,n){G("getLastLogs",t.type);let o=this.idToLastLines.get(t.id);if(o==null){const a=new ce(this.logger,this.clientLogs,t,r);this.idToLastLines.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error!=null)throw c.error;return c.log}getProgressLog(e,t,r){if(r==null)return f.Computable.make(a=>this.getProgressLog(e,t,a));const n=w.treeEntryToResourceInfo(e,r),o=k.randomUUID();r.attacheHooks(this.hooks),r.addOnDestroy(()=>this.releaseProgressLog(n.id,o));const c=this.getProgressLogNoCtx(r.watcher,n,t,o);return r.markUnstable("The progress log is from the stream, so we consider it unstable. Final value will be got from blobs."),c}getProgressLogNoCtx(e,t,r,n){G("getProgressLog",t.type);let o=this.idToProgressLog.get(t.id);if(o==null){const a=new ce(this.logger,this.clientLogs,t,1,r);this.idToProgressLog.set(t.id,a),o=a}o.attach(e,n);const c=o.getLog();if(c.error)throw c.error;return c.log}getLogHandle(e,t){if(t==null)return f.Computable.make(o=>this.getLogHandle(e,o));const r=w.treeEntryToResourceInfo(e,t),n=this.getLogHandleNoCtx(r);return t.markUnstable(`live_log:${g.resourceIdToString(r.id)}`),n}getLogHandleNoCtx(e){return G("getLogHandle",e.type),Me(!0,e)}async lastLines(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.lastLines(F(e),t,BigInt(r??0),n))}async readText(e,t,r,n){return await this.tryWithNotFound(e,()=>this.clientLogs.readText(F(e),t,BigInt(r??0),n))}async tryWithNotFound(e,t){if(!B(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 p.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 fe.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 ce{constructor(e,t,r,n,o){h(this,"logs");h(this,"error");h(this,"change",new f.ChangeSource);h(this,"counter",new p.CallersCounter);this.logger=e,this.clientLogs=t,this.rInfo=r,this.lines=n,this.patternToSearch=o}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 t=(await this.clientLogs.lastLines(this.rInfo,this.lines,0n,this.patternToSearch)).data.toString();this.logs!=t&&this.change.markChanged(),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 ${g.stringifyWithResourceId(this.rInfo.id)} failed, reason: ${e}`),e}}}function G(i,e){if(!e.name.startsWith("StreamWorkdir"))throw new C(`${i}: wrong resource type: ${e.name}, expected: a resource of type 'StreamWorkdir'.`)}class en{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(o=>this.getLastLogs(e,t,o));const n=q(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(V(n))return this.downloadDriver.getLastLogs(n,t,r);try{return this.logsStreamDriver.getLastLogs(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting last logs: ${o}`);return}throw o}}getProgressLog(e,t,r){if(r===void 0)return f.Computable.make(o=>this.getProgressLog(e,t,o));const n=q(r,e);if(n===void 0){r.markUnstable("no stream in stream manager");return}if(V(n))return this.downloadDriver.getProgressLog(n,t,r);try{return this.logsStreamDriver.getProgressLog(n,t,r)}catch(o){if(o.name=="RpcError"&&o.code=="NOT_FOUND"){r.markUnstable(`NOT_FOUND in logs stream driver while getting a progress log: ${o}`);return}throw o}}getLogHandle(e,t){if(t===void 0)return f.Computable.make(n=>this.getLogHandle(e,n));const r=q(t,e);if(r===void 0){t.markUnstable("no stream in stream manager");return}return V(r)?this.downloadDriver.getLogHandle(r,t):this.logsStreamDriver.getLogHandle(r,t)}async lastLines(e,t,r,n){return B(e)?await this.logsStreamDriver.lastLines(e,t,r,n):await this.downloadDriver.lastLines(e,t,r,n)}async readText(e,t,r,n){return B(e)?await this.logsStreamDriver.readText(e,t,r,n):await this.downloadDriver.readText(e,t,r,n)}}function V(i){return!i.type.name.startsWith("StreamWorkdir")}function q(i,e){var t;return(t=i.accessor(e).node().traverse("stream"))==null?void 0:t.resourceInfo}class tn{constructor(e,t,r,n={cacheSoftSizeBytes:50*1024*1024,withGunzip:!0,nConcurrentDownloads:50}){h(this,"downloadHelper");h(this,"urlToDownload",new Map);h(this,"downloadQueue");h(this,"cache");this.logger=e,this.saveDir=r,this.opts=n,this.downloadQueue=new p.TaskProcessor(this.logger,this.opts.nConcurrentDownloads),this.cache=new ve(this.opts.cacheSoftSizeBytes),this.downloadHelper=new be(t)}getPath(e,t){if(t===void 0)return f.Computable.make(o=>this.getPath(e,o));const r=k.randomUUID();t.addOnDestroy(()=>this.releasePath(e,r));const n=this.getPathNoCtx(e,t.watcher,r);return(n==null?void 0:n.path)===void 0&&t.markUnstable(`a path to the downloaded and untared archive might be undefined. The current result: ${n}`),n}getPathNoCtx(e,t,r){const n=e.toString(),o=this.urlToDownload.get(n);if(o!=null)return o.attach(t,r),o.getPath();const c=this.setNewTask(t,e,r);return this.downloadQueue.push({fn:async()=>this.downloadUrl(c,r),recoverableErrorPredicate:a=>!0}),c.getPath()}async downloadUrl(e,t){var r;await e.download(this.downloadHelper,this.opts.withGunzip),((r=e.getPath())==null?void 0:r.path)!=null&&this.cache.addCache(e,t)}async releasePath(e,t){const r=e.toString(),n=this.urlToDownload.get(r);if(n!=null)if(this.cache.existsFile(n.path)){const o=this.cache.removeFile(n.path,t);await Promise.all(o.map(async c=>{await Z(c.path),this.cache.removeCache(c),this.removeTask(c,`the task ${g.stringifyWithResourceId(c.info())} was removedfrom cache along with ${g.stringifyWithResourceId(o.map(a=>a.info()))}`)}))}else n.counter.dec(t)&&this.removeTask(n,`the task ${g.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 Z(t.path),this.cache.removeCache(t),this.removeTask(t,`the task ${g.stringifyWithResourceId(t.info())} was released when the driver was closed`)}))}setNewTask(e,t,r){const n=new rn(this.logger,this.getFilePath(t),t);return n.attach(e,r),this.urlToDownload.set(t.toString(),n),n}removeTask(e,t){e.abort(t),e.change.markChanged(),this.urlToDownload.delete(e.url.toString())}getFilePath(e){const t=k.createHash("sha256").update(e.toString()).digest("hex");return T.join(this.saveDir,t)}}class rn{constructor(e,t,r){h(this,"counter",new p.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}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()}catch(r){if(r instanceof de||r instanceof te){this.setError(r),this.change.markChanged(),await Z(this.path);return}throw r}}async downloadAndUntar(e,t,r){if(await p.ensureDirExists(T.dirname(this.path)),await p.fileExists(this.path))return await Ae(this.path);const n=await e.download(this.url.toString(),{},r);let o=n.content;if(t){const c=L.Transform.toWeb(st.createGunzip());o=o.pipeThrough(c,{signal:r})}return await p.createPathAtomically(this.logger,this.path,async c=>{await y.mkdir(c);const a=L.Writable.toWeb(at.extract(c));await o.pipeTo(a,{signal:r})}),n.size}getPath(){if(this.done)return{path:p.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 de(e))}}class de extends Error{}async function Ae(i){const e=await y.readdir(i,{withFileTypes:!0});return(await Promise.all(e.map(async r=>{const n=T.join(i,r.name);return r.isDirectory()?await Ae(n):(await y.stat(n)).size}))).reduce((r,n)=>r+n,0)}async function Z(i){await y.rm(i,{recursive:!0,force:!0})}function he(i,e){return`index://index/${encodeURIComponent(JSON.stringify({storageId:i,path:e}))}`}function nn(i,e,t,r){const n={localPath:i,pathSignature:e.sign(i),sizeBytes:String(t),modificationTime:String(r)};return`upload://upload/${encodeURIComponent(JSON.stringify(n))}`}function on(i){const e=new URL(i);return D.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function sn(i){const e=new URL(i);return oe.parse(JSON.parse(decodeURIComponent(e.pathname.substring(1))))}function an(i){if(hn(i))return pn(i);if(ln(i))return dn(i);p.assertNever(i)}const je=/^local:\/\/(?<name>.*)\/(?<path>.*)$/;function ln(i){return je.test(i)}function cn(i,e){return`local://${i}/${encodeURIComponent(e)}`}function dn(i){const e=i.match(je);if(e==null)throw new Error(`Local list handle wasn't parsed: ${i}`);const{name:t,path:r}=e.groups;return{rootPath:decodeURIComponent(r),name:t,isRemote:!1}}const Ge=/^remote:\/\/(?<name>.*)\/(?<resourceId>.*)$/;function hn(i){return Ge.test(i)}function un(i,e){return`remote://${i}/${BigInt(e)}`}function pn(i){const e=i.match(Ge);if(e==null)throw new Error(`Remote list handle wasn't parsed: ${i}`);const{name:t,resourceId:r}=e.groups;return{id:g.bigintToResourceId(BigInt(r)),type:gn(t),name:t,isRemote:!0}}function gn(i){return{name:`LS/${i}`,version:"1"}}async function Ve(){const i=ge.homedir();if(v.sep=="/")return[{name:"local",root:"/",initialPath:i}];{const t=v.parse(i).root.replaceAll(":\\","");try{return(await nt.promisify(ot.exec)("wmic logicaldisk get name")).stdout.split(`\r
|
|
2
|
+
`).filter(o=>o.includes(":")).map(o=>o.trim().replaceAll(":","")).map(o=>{const c=o==t;return{name:`local_disk_${o}`,root:`${o}:\\`,initialPath:c?i:`${o}:\\`}})}catch{return[{name:`local_disk_${t}`,root:`${t}:\\`,initialPath:i}]}}}class ie{constructor(e,t,r,n,o,c,a){this.logger=e,this.lsClient=t,this.storageIdToResourceId=r,this.signer=n,this.virtualStoragesMap=o,this.localProjectionsMap=c,this.openFileDialogCallback=a}async getLocalFileContent(e,t){const r=await this.tryResolveLocalFileHandle(e);if(t)throw new Error("Range request not yet supported.");return await y.readFile(r)}async getLocalFileSize(e){const t=await this.tryResolveLocalFileHandle(e);return(await y.stat(t)).size}async showOpenMultipleFilesDialog(e){const t=await this.openFileDialogCallback(!0,e);return t===void 0?{}:{files:await Promise.all(t.map(r=>this.getLocalFileHandle(r)))}}async showOpenSingleFileDialog(e){const t=await this.openFileDialogCallback(!1,e);return t===void 0?{}:{file:await this.getLocalFileHandle(t[0])}}async tryResolveLocalFileHandle(e){if(rt.isImportFileHandleIndex(e)){const t=sn(e),r=this.localProjectionsMap.get(t.storageId);if(!r)throw new Error(`Storage ${t.storageId} is not mounted locally.`);return T.join(r.localPath,t.path)}else{const t=on(e);this.signer.verify(t.localPath,t.pathSignature,"Failed to validate local file handle signature.");const r=t.localPath,n=await y.stat(r,{bigint:!0});if(String(n.mtimeMs/1000n)!==t.modificationTime)throw new Error("File has changed since the handle was created.");return r}}async getLocalFileHandle(e){U(e);for(const r of this.localProjectionsMap.values())if(e.startsWith(r.localPath)){const n=r.localPath===""?e:T.relative(r.localPath,e);return he(r.storageId,n)}const t=await y.stat(e,{bigint:!0});return nn(e,this.signer,t.size,t.mtimeMs/1000n)}async getStorageList(){const e=[...this.virtualStoragesMap.values()].map(n=>({name:n.name,handle:cn(n.name,n.root),initialFullPath:n.initialPath})),r=Object.entries(this.storageIdToResourceId).map(([n,o])=>({name:n,handle:un(n,o),initialFullPath:"",isInitialPathHome:!1})).filter(n=>n.name!=="root");return[...e,...r]}async listFiles(e,t){const r=an(e);if(r.isRemote)return{entries:(await this.lsClient.list(r,t)).items.map(a=>({type:a.isDir?"dir":"file",name:a.name,fullPath:a.fullName,handle:he(r.name,a.fullName)}))};T.sep==="/"&&t===""&&(t="/"),r.rootPath===""&&U(t);const n=T.isAbsolute(t)?t:T.join(r.rootPath,t),o=[];for await(const c of await y.opendir(n)){if(!c.isFile()&&!c.isDirectory())continue;const a=T.join(n,c.name);o.push({type:c.isFile()?"file":"dir",name:c.name,fullPath:a,handle:await this.getLocalFileHandle(a)})}return{entries:o}}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,o,c){const a=Se(t,e);c||(c=await Ve());for(const u of c)U(u.root);for(const u of n)u.localPath!==""&&U(u.localPath);const d=new Map(c.map(u=>[u.name,u])),l=new Map(n.map(u=>[u.storageId,u]));if(new Set([...d.keys(),...l.keys()]).size!==d.size+l.size)throw new Error("Intersection between local projection storage ids and virtual storages names detected.");return new ie(e,a,await fn(t),r,d,l,o)}}async function fn(i){return i.withReadTx("GetAvailableStorageIds",async e=>{const t=await e.getResourceByName("LSProvider"),r=await e.getResourceData(t,!0);return mn(r)})}function mn(i){return Object.fromEntries(i.fields.filter(e=>e.type=="Dynamic"&&g.isNotNullResourceId(e.value)).map(e=>[e.name.substring(8),e.value]))}exports.ClientDownload=ke;exports.ClientLogs=Re;exports.ClientLs=Ie;exports.ClientProgress=Te;exports.ClientUpload=ye;exports.DefaultVirtualLocalStorages=Ve;exports.DownloadDriver=Hr;exports.DownloadUrlDriver=tn;exports.ImportFileHandleData=_r;exports.ImportFileHandleIndexData=oe;exports.ImportFileHandleUploadData=D;exports.IndexResourceSnapshot=_e;exports.LogsDriver=en;exports.LogsStreamDriver=Kr;exports.LsDriver=ie;exports.MTimeError=Y;exports.NetworkError=we;exports.NoFileForUploading=ee;exports.OnDemandBlobResourceSnapshot=Fe;exports.UnexpectedEOF=K;exports.UnknownStorageError=ne;exports.Updater=He;exports.UploadDriver=Qr;exports.UploadResourceSnapshot=$e;exports.WrongLocalFileUrl=re;exports.WrongResourceTypeError=C;exports.createDownloadClient=Dr;exports.createLogsClient=Nr;exports.createLsFilesClient=Se;exports.createUploadBlobClient=Br;exports.createUploadProgressClient=vr;exports.getFullPath=Pe;exports.getSize=Ce;exports.makeBlobImportSnapshot=xe;exports.newLocalStorageIdsToRoot=Ue;exports.parseLocalUrl=Le;exports.validateAbsolute=U;
|
|
3
3
|
//# sourceMappingURL=index.js.map
|