@fnndsc/salsa 4.5.7 → 4.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/README.md +0 -2
  2. package/dist/compute/index.d.ts +0 -9
  3. package/dist/compute/index.js +0 -16
  4. package/dist/compute/index.js.map +1 -1
  5. package/dist/dag/feedData.js +166 -73
  6. package/dist/dag/feedData.js.map +1 -1
  7. package/dist/dag/feedLoad.d.ts +1 -1
  8. package/dist/dag/feedLoad.js +6 -4
  9. package/dist/dag/feedLoad.js.map +1 -1
  10. package/dist/dicom/pixels.js +95 -36
  11. package/dist/dicom/pixels.js.map +1 -1
  12. package/dist/dicom/series.d.ts +4 -1
  13. package/dist/dicom/series.js +80 -31
  14. package/dist/dicom/series.js.map +1 -1
  15. package/dist/dicom/tags.d.ts +28 -0
  16. package/dist/dicom/tags.js +30 -0
  17. package/dist/dicom/tags.js.map +1 -1
  18. package/dist/files/copy.d.ts +197 -0
  19. package/dist/files/copy.js +636 -0
  20. package/dist/files/copy.js.map +1 -0
  21. package/dist/files/copyFolder.d.ts +103 -0
  22. package/dist/files/copyFolder.js +199 -0
  23. package/dist/files/copyFolder.js.map +1 -0
  24. package/dist/files/create.d.ts +178 -0
  25. package/dist/files/create.js +444 -0
  26. package/dist/files/create.js.map +1 -0
  27. package/dist/files/download.d.ts +188 -0
  28. package/dist/files/download.js +541 -0
  29. package/dist/files/download.js.map +1 -0
  30. package/dist/files/index.d.ts +69 -141
  31. package/dist/files/index.js +184 -535
  32. package/dist/files/index.js.map +1 -1
  33. package/dist/files/listing.d.ts +132 -0
  34. package/dist/files/listing.js +179 -0
  35. package/dist/files/listing.js.map +1 -0
  36. package/dist/files/move.d.ts +125 -0
  37. package/dist/files/move.js +334 -0
  38. package/dist/files/move.js.map +1 -0
  39. package/dist/files/pathMapper.d.ts +24 -1
  40. package/dist/files/pathMapper.js +108 -47
  41. package/dist/files/pathMapper.js.map +1 -1
  42. package/dist/files/proof.d.ts +315 -0
  43. package/dist/files/proof.js +574 -0
  44. package/dist/files/proof.js.map +1 -0
  45. package/dist/files/removal.d.ts +223 -0
  46. package/dist/files/removal.js +318 -0
  47. package/dist/files/removal.js.map +1 -0
  48. package/dist/files/replace.d.ts +175 -0
  49. package/dist/files/replace.js +582 -0
  50. package/dist/files/replace.js.map +1 -0
  51. package/dist/files/upload.d.ts +205 -0
  52. package/dist/files/upload.js +478 -0
  53. package/dist/files/upload.js.map +1 -0
  54. package/dist/index.d.ts +14 -2
  55. package/dist/index.js +45 -32
  56. package/dist/index.js.map +1 -1
  57. package/dist/jobs/index.d.ts +1 -1
  58. package/dist/jobs/index.js +1 -2
  59. package/dist/jobs/index.js.map +1 -1
  60. package/dist/jobs/jobOps.d.ts +0 -13
  61. package/dist/jobs/jobOps.js +0 -37
  62. package/dist/jobs/jobOps.js.map +1 -1
  63. package/dist/pacs/anonymise.d.ts +17 -36
  64. package/dist/pacs/anonymise.js +29 -67
  65. package/dist/pacs/anonymise.js.map +1 -1
  66. package/dist/pipelines/index.d.ts +1 -13
  67. package/dist/pipelines/index.js +0 -38
  68. package/dist/pipelines/index.js.map +1 -1
  69. package/dist/pipelines/invocation.js +321 -167
  70. package/dist/pipelines/invocation.js.map +1 -1
  71. package/dist/pipelines/manifest.js.map +1 -1
  72. package/dist/pipelines/packages.d.ts +11 -8
  73. package/dist/pipelines/packages.js +253 -77
  74. package/dist/pipelines/packages.js.map +1 -1
  75. package/dist/plugins/adminToken.d.ts +67 -0
  76. package/dist/plugins/adminToken.js +77 -0
  77. package/dist/plugins/adminToken.js.map +1 -0
  78. package/dist/plugins/index.d.ts +0 -7
  79. package/dist/plugins/index.js +0 -16
  80. package/dist/plugins/index.js.map +1 -1
  81. package/dist/plugins/peer_search.d.ts +0 -15
  82. package/dist/plugins/peer_search.js +0 -25
  83. package/dist/plugins/peer_search.js.map +1 -1
  84. package/dist/plugins/plugin_add.d.ts +14 -2
  85. package/dist/plugins/plugin_add.js +42 -13
  86. package/dist/plugins/plugin_add.js.map +1 -1
  87. package/dist/plugins/plugin_docker.d.ts +25 -0
  88. package/dist/plugins/plugin_docker.js +33 -0
  89. package/dist/plugins/plugin_docker.js.map +1 -1
  90. package/dist/plugins/plugin_register.d.ts +0 -10
  91. package/dist/plugins/plugin_register.js +3 -54
  92. package/dist/plugins/plugin_register.js.map +1 -1
  93. package/dist/plugins/plugin_sync.js +45 -22
  94. package/dist/plugins/plugin_sync.js.map +1 -1
  95. package/dist/plugins/plugin_validate.js +47 -13
  96. package/dist/plugins/plugin_validate.js.map +1 -1
  97. package/dist/plugins/store_import.d.ts +0 -9
  98. package/dist/plugins/store_import.js +3 -29
  99. package/dist/plugins/store_import.js.map +1 -1
  100. package/dist/retrieve/task.d.ts +137 -0
  101. package/dist/retrieve/task.js +41 -0
  102. package/dist/retrieve/task.js.map +1 -0
  103. package/dist/retrieve/watch.d.ts +2 -118
  104. package/dist/retrieve/watch.js +452 -321
  105. package/dist/retrieve/watch.js.map +1 -1
  106. package/dist/vfs/providers/etc.d.ts +6 -4
  107. package/dist/vfs/providers/etc.js +12 -4
  108. package/dist/vfs/providers/etc.js.map +1 -1
  109. package/dist/vfs/providers/native.d.ts +116 -39
  110. package/dist/vfs/providers/native.js +291 -347
  111. package/dist/vfs/providers/native.js.map +1 -1
  112. package/dist/vfs/providers/pacs.d.ts +29 -3
  113. package/dist/vfs/providers/pacs.js +303 -73
  114. package/dist/vfs/providers/pacs.js.map +1 -1
  115. package/dist/vfs/providers/pacs_content.js +22 -6
  116. package/dist/vfs/providers/pacs_content.js.map +1 -1
  117. package/dist/vfs/providers/proc/backfill.d.ts +46 -0
  118. package/dist/vfs/providers/proc/backfill.js +147 -0
  119. package/dist/vfs/providers/proc/backfill.js.map +1 -0
  120. package/dist/vfs/providers/proc/book.d.ts +176 -0
  121. package/dist/vfs/providers/proc/book.js +73 -0
  122. package/dist/vfs/providers/proc/book.js.map +1 -0
  123. package/dist/vfs/providers/proc/catchup.d.ts +69 -0
  124. package/dist/vfs/providers/proc/catchup.js +125 -0
  125. package/dist/vfs/providers/proc/catchup.js.map +1 -0
  126. package/dist/vfs/providers/proc/feedIndex.d.ts +146 -0
  127. package/dist/vfs/providers/proc/feedIndex.js +354 -0
  128. package/dist/vfs/providers/proc/feedIndex.js.map +1 -0
  129. package/dist/vfs/providers/proc/feedTopology.d.ts +85 -0
  130. package/dist/vfs/providers/proc/feedTopology.js +192 -0
  131. package/dist/vfs/providers/proc/feedTopology.js.map +1 -0
  132. package/dist/vfs/providers/proc/locate.d.ts +60 -0
  133. package/dist/vfs/providers/proc/locate.js +103 -0
  134. package/dist/vfs/providers/proc/locate.js.map +1 -0
  135. package/dist/vfs/providers/proc/path.d.ts +50 -0
  136. package/dist/vfs/providers/proc/path.js +106 -0
  137. package/dist/vfs/providers/proc/path.js.map +1 -0
  138. package/dist/vfs/providers/proc/roster.d.ts +95 -0
  139. package/dist/vfs/providers/proc/roster.js +425 -0
  140. package/dist/vfs/providers/proc/roster.js.map +1 -0
  141. package/dist/vfs/providers/proc/rows.d.ts +120 -0
  142. package/dist/vfs/providers/proc/rows.js +181 -0
  143. package/dist/vfs/providers/proc/rows.js.map +1 -0
  144. package/dist/vfs/providers/proc/subtree.d.ts +73 -0
  145. package/dist/vfs/providers/proc/subtree.js +125 -0
  146. package/dist/vfs/providers/proc/subtree.js.map +1 -0
  147. package/dist/vfs/providers/proc/sweep.d.ts +89 -0
  148. package/dist/vfs/providers/proc/sweep.js +297 -0
  149. package/dist/vfs/providers/proc/sweep.js.map +1 -0
  150. package/dist/vfs/providers/proc.d.ts +47 -346
  151. package/dist/vfs/providers/proc.js +349 -1806
  152. package/dist/vfs/providers/proc.js.map +1 -1
  153. package/dist/vfs/providers/share.js +4 -1
  154. package/dist/vfs/providers/share.js.map +1 -1
  155. package/package.json +3 -3
  156. package/dist/pacs/resultDecode.d.ts +0 -13
  157. package/dist/pacs/resultDecode.js +0 -19
  158. package/dist/pacs/resultDecode.js.map +0 -1
@@ -0,0 +1,205 @@
1
+ /**
2
+ * @file An upload of many files into CUBE, each stored on proof of what holds its name.
3
+ *
4
+ * CUBE's upload does not refuse a held name: posted onto one, the bytes
5
+ * land under another name beside it, or the old content stays. So nothing
6
+ * here posts onto a name it has not proved. An upload is bound to one
7
+ * client, the session's when it began. Every folder it lands in is proved
8
+ * whole once, fresh on that client (a folder not there yet is proved
9
+ * absent, and every name in it is free); each file is then stored by the
10
+ * proof of its folder:
11
+ *
12
+ * - a free name is written create-only (`file_createProved`): one that
13
+ * arrives between the proof and the write is not written over, and no
14
+ * twin is left beside it;
15
+ * - a name one file holds, inside a folder the operator agreed to merge
16
+ * into ("Merge/overwrite?"), or anywhere in a forced upload, is replaced
17
+ * through the proved swap (`file_replace`), the old content kept until
18
+ * the new is at its name;
19
+ * - any other held name (a file outside such a folder, a folder, a link,
20
+ * several holders) refuses that file, says so, and writes nothing.
21
+ *
22
+ * A file that failed is tried again only when nothing was asked of CUBE
23
+ * that could change anything (no upload whose POST left, move, delete or
24
+ * folder made): a failure after such a request, a returned one or a
25
+ * thrown one alike, may have left the store half changed (a replacement
26
+ * at its name, the original set aside), so it is final
27
+ * (`TransferFinalError`), and its words, which name where each copy is,
28
+ * are the ones said. An upload that failed before its POST left changed
29
+ * nothing, and is tried again.
30
+ *
31
+ * Before anything is sent, each folder the upload lands at is settled:
32
+ * proved free, or held by a folder the operator agrees to merge into. A
33
+ * place that cannot be checked, one a file or a link holds, a "no", and a
34
+ * question that cannot be asked each stop the upload unsent.
35
+ *
36
+ * Key components:
37
+ * - UploadConfirm: how a merge into a folder already there is asked.
38
+ * - UploadPlan: the upload's client, the folders merged into, and each destination folder's proof.
39
+ * - uploadPlan_make: bind the client and settle every folder the upload lands at.
40
+ * - uploadPlanCreateOnly_make: a plan that asks nothing and replaces nothing (`path upload`).
41
+ * - files_uploadPath: a local file or folder uploaded create-only (`chefs upload`).
42
+ *
43
+ * Typical usage:
44
+ * ```ts
45
+ * const plan: UploadPlan = await uploadPlan_make(['/home/ada/study'], confirm);
46
+ * await plan.file_store('/home/ada/study/a.txt', new Blob(['a']));
47
+ * ```
48
+ *
49
+ * Dependencies: the proofs of `./proof.js`, the proved writes of
50
+ * `./replace.js`, and cumin's `chrisIO` (the session's client, bound as a
51
+ * `HolderClient`) and error stack. Paths are absolute CUBE paths.
52
+ *
53
+ * @module
54
+ */
55
+ import { type HolderClient } from '@fnndsc/cumin';
56
+ /**
57
+ * How a merge into a folder already there is asked: returns on a yes,
58
+ * throws on a no or when the question cannot be asked.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const confirm: UploadConfirm = async (message: string): Promise<void> => { if (!await ask(message)) throw new Error('declined'); };
63
+ * ```
64
+ */
65
+ export type UploadConfirm = (message: string) => Promise<void>;
66
+ /**
67
+ * An upload under way: its one client, the folders the operator agreed to
68
+ * merge into, whether it is forced, and each destination folder's proof,
69
+ * made once.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * const plan: UploadPlan = await uploadPlan_make([], null);
74
+ * await plan.file_store('/home/ada/a.txt', new Blob(['a']));
75
+ * ```
76
+ */
77
+ export declare class UploadPlan {
78
+ /** The client every proof and write goes to. */
79
+ readonly acts: HolderClient;
80
+ /** The folders a merge was agreed for: a file one holds below them is replaced. */
81
+ readonly merged: readonly string[];
82
+ /** Whether the upload is forced: a file one file holds is replaced wherever it lands. */
83
+ readonly forced: boolean;
84
+ /** Each destination folder's proof, asked once. */
85
+ private readonly folders;
86
+ /**
87
+ * @param acts - The upload's client.
88
+ * @param merged - The folders a merge was agreed for.
89
+ * @param forced - Whether the upload is forced.
90
+ */
91
+ constructor(acts: HolderClient, merged: readonly string[], forced: boolean);
92
+ /**
93
+ * A destination folder's proof, asked once; a folder that could not be
94
+ * proved is asked again by the next file (a retry).
95
+ *
96
+ * @param folder - The folder.
97
+ * @returns Its proof.
98
+ */
99
+ private folder_get;
100
+ /**
101
+ * Whether a file one file holds is to be replaced: the upload is forced,
102
+ * or the file lands below a folder a merge was agreed for.
103
+ *
104
+ * @param target - The file's path.
105
+ * @returns True when it is.
106
+ */
107
+ private replace_isAgreed;
108
+ /**
109
+ * Stores one file on the proof of its folder (see the module's rules).
110
+ *
111
+ * @param acts - The client the writes go to: the upload's, perhaps watched.
112
+ * @param target - The file's path.
113
+ * @param content - Its content.
114
+ * @returns True when the path holds the content; false otherwise (stacked).
115
+ */
116
+ private file_settle;
117
+ /**
118
+ * Stores one file of the upload, on the proof of its folder. A store that
119
+ * threw is a store that did not: its words join the reasons it left.
120
+ *
121
+ * @param target - The file's CUBE path; one written without its leading
122
+ * slash is read from the root.
123
+ * @param content - Its content.
124
+ * @returns Nothing, once the path holds the content.
125
+ * @throws {Error} When it does not and nothing that could change CUBE was
126
+ * asked, with every reason the store left: the transfer pool tries it again.
127
+ * @throws {TransferFinalError} When it does not after such a request: the
128
+ * pool does not try it again, and the reasons name where each copy is.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * await plan.file_store('/home/ada/study/a.txt', new Blob(['a']));
133
+ * ```
134
+ */
135
+ file_store(target: string, content: Blob): Promise<void>;
136
+ /**
137
+ * Stores one file of the upload, on the proof of its folder, for a caller
138
+ * that does not retry: its reasons are left on the error stack.
139
+ *
140
+ * @param target - The file's absolute CUBE path.
141
+ * @param content - Its content.
142
+ * @returns True when the path holds the content; false otherwise (stacked).
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * const stored: boolean = await plan.file_tryStore('/home/ada/a.txt', new Blob(['a']));
147
+ * ```
148
+ */
149
+ file_tryStore(target: string, content: Blob): Promise<boolean>;
150
+ }
151
+ /**
152
+ * Begins an upload: the session's client bound, and every folder it lands
153
+ * at settled before anything is sent, so a "no" at the second of two
154
+ * folders leaves the first unsent.
155
+ *
156
+ * @param targets - The CUBE path each folder source lands at; each is read
157
+ * from the root, as every file stored through the plan is, so the folder
158
+ * asked about is the one merged into (`/dest/./study` is `/dest/study`).
159
+ * @param confirm - How a merge into a folder already there is asked; null
160
+ * when the upload is forced (a merge is then taken as agreed).
161
+ * @returns The plan every file is stored through.
162
+ * @throws {Error} When there is no connection, or a target cannot be
163
+ * settled (nothing has been sent).
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * const plan: UploadPlan = await uploadPlan_make(['/home/ada/study'], async (message) => confirmOrThrow(message));
168
+ * ```
169
+ */
170
+ export declare function uploadPlan_make(targets: readonly string[], confirm: UploadConfirm | null): Promise<UploadPlan>;
171
+ /**
172
+ * Begins an upload that asks nothing and replaces nothing: every file is
173
+ * stored create-only, and a held name refuses that file (a word with no
174
+ * question and no `--force`, such as `path upload`).
175
+ *
176
+ * @returns The plan every file is stored through.
177
+ * @throws {Error} When there is no connection (nothing has been sent).
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * const plan: UploadPlan = await uploadPlanCreateOnly_make();
182
+ * await plan.file_store('/home/ada/a.txt', new Blob(['a']));
183
+ * ```
184
+ */
185
+ export declare function uploadPlanCreateOnly_make(): Promise<UploadPlan>;
186
+ /**
187
+ * Uploads a local file or folder to ChRIS, create-only (`chefs upload`): a
188
+ * folder lands under the destination by its own name, as `cp -r` puts it,
189
+ * and a file at the destination path itself. Each file is stored through a
190
+ * plan that asks nothing and replaces nothing, so a held name refuses that
191
+ * file and says so. The local path is read as written, as the shell would
192
+ * read it; a folder lands under the last name of its path resolved
193
+ * (`study/old/..` as `study`, never above the destination), and one written with `.` last
194
+ * (`.`, `study/.`) puts its contents in the destination itself.
195
+ *
196
+ * @param localPath - The local filesystem path; a relative one is read from the working directory.
197
+ * @param remotePath - The ChRIS destination path.
198
+ * @returns True when every file was stored; false otherwise, with the reasons on the error stack.
199
+ *
200
+ * @example
201
+ * ```ts
202
+ * const uploaded: boolean = await files_uploadPath('/tmp/study', '/home/ada');
203
+ * ```
204
+ */
205
+ export declare function files_uploadPath(localPath: string, remotePath: string): Promise<boolean>;
@@ -0,0 +1,478 @@
1
+ "use strict";
2
+ /**
3
+ * @file An upload of many files into CUBE, each stored on proof of what holds its name.
4
+ *
5
+ * CUBE's upload does not refuse a held name: posted onto one, the bytes
6
+ * land under another name beside it, or the old content stays. So nothing
7
+ * here posts onto a name it has not proved. An upload is bound to one
8
+ * client, the session's when it began. Every folder it lands in is proved
9
+ * whole once, fresh on that client (a folder not there yet is proved
10
+ * absent, and every name in it is free); each file is then stored by the
11
+ * proof of its folder:
12
+ *
13
+ * - a free name is written create-only (`file_createProved`): one that
14
+ * arrives between the proof and the write is not written over, and no
15
+ * twin is left beside it;
16
+ * - a name one file holds, inside a folder the operator agreed to merge
17
+ * into ("Merge/overwrite?"), or anywhere in a forced upload, is replaced
18
+ * through the proved swap (`file_replace`), the old content kept until
19
+ * the new is at its name;
20
+ * - any other held name (a file outside such a folder, a folder, a link,
21
+ * several holders) refuses that file, says so, and writes nothing.
22
+ *
23
+ * A file that failed is tried again only when nothing was asked of CUBE
24
+ * that could change anything (no upload whose POST left, move, delete or
25
+ * folder made): a failure after such a request, a returned one or a
26
+ * thrown one alike, may have left the store half changed (a replacement
27
+ * at its name, the original set aside), so it is final
28
+ * (`TransferFinalError`), and its words, which name where each copy is,
29
+ * are the ones said. An upload that failed before its POST left changed
30
+ * nothing, and is tried again.
31
+ *
32
+ * Before anything is sent, each folder the upload lands at is settled:
33
+ * proved free, or held by a folder the operator agrees to merge into. A
34
+ * place that cannot be checked, one a file or a link holds, a "no", and a
35
+ * question that cannot be asked each stop the upload unsent.
36
+ *
37
+ * Key components:
38
+ * - UploadConfirm: how a merge into a folder already there is asked.
39
+ * - UploadPlan: the upload's client, the folders merged into, and each destination folder's proof.
40
+ * - uploadPlan_make: bind the client and settle every folder the upload lands at.
41
+ * - uploadPlanCreateOnly_make: a plan that asks nothing and replaces nothing (`path upload`).
42
+ * - files_uploadPath: a local file or folder uploaded create-only (`chefs upload`).
43
+ *
44
+ * Typical usage:
45
+ * ```ts
46
+ * const plan: UploadPlan = await uploadPlan_make(['/home/ada/study'], confirm);
47
+ * await plan.file_store('/home/ada/study/a.txt', new Blob(['a']));
48
+ * ```
49
+ *
50
+ * Dependencies: the proofs of `./proof.js`, the proved writes of
51
+ * `./replace.js`, and cumin's `chrisIO` (the session's client, bound as a
52
+ * `HolderClient`) and error stack. Paths are absolute CUBE paths.
53
+ *
54
+ * @module
55
+ */
56
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
57
+ if (k2 === undefined) k2 = k;
58
+ var desc = Object.getOwnPropertyDescriptor(m, k);
59
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
60
+ desc = { enumerable: true, get: function() { return m[k]; } };
61
+ }
62
+ Object.defineProperty(o, k2, desc);
63
+ }) : (function(o, m, k, k2) {
64
+ if (k2 === undefined) k2 = k;
65
+ o[k2] = m[k];
66
+ }));
67
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
68
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
69
+ }) : function(o, v) {
70
+ o["default"] = v;
71
+ });
72
+ var __importStar = (this && this.__importStar) || (function () {
73
+ var ownKeys = function(o) {
74
+ ownKeys = Object.getOwnPropertyNames || function (o) {
75
+ var ar = [];
76
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
77
+ return ar;
78
+ };
79
+ return ownKeys(o);
80
+ };
81
+ return function (mod) {
82
+ if (mod && mod.__esModule) return mod;
83
+ var result = {};
84
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
85
+ __setModuleDefault(result, mod);
86
+ return result;
87
+ };
88
+ })();
89
+ Object.defineProperty(exports, "__esModule", { value: true });
90
+ exports.UploadPlan = void 0;
91
+ exports.uploadPlan_make = uploadPlan_make;
92
+ exports.uploadPlanCreateOnly_make = uploadPlanCreateOnly_make;
93
+ exports.files_uploadPath = files_uploadPath;
94
+ const path = __importStar(require("path"));
95
+ const cumin_1 = require("@fnndsc/cumin");
96
+ const proof_js_1 = require("./proof.js");
97
+ const replace_js_1 = require("./replace.js");
98
+ /**
99
+ * What the error stack was left since a mark, taken off it, as one line.
100
+ *
101
+ * @param since - The stack's mark.
102
+ * @param fallback - What is said when nothing was left.
103
+ * @returns The messages, joined.
104
+ */
105
+ function reasons_take(since, fallback) {
106
+ const reasons = cumin_1.errorStack.checkpoint_drain(since)
107
+ .map((note) => note.message.replace(/^\[[^\]]*\]\s*\|\s*/, '').trim())
108
+ .filter((line) => line.length > 0)
109
+ .join('; ');
110
+ return reasons.length > 0 ? reasons : fallback;
111
+ }
112
+ /**
113
+ * The holders of a held name, in words.
114
+ *
115
+ * @param holders - Its holders.
116
+ * @returns `a file`, `a folder and a file`, and the like.
117
+ */
118
+ function heldBy_say(holders) {
119
+ const sole = holders.length === 1 ? holders[0] : undefined;
120
+ return sole === undefined ? (0, proof_js_1.holders_describe)(holders) : `a ${sole.kind === 'dir' ? 'folder' : sole.kind}`;
121
+ }
122
+ /**
123
+ * The requests of a bound client that may change something in CUBE, each
124
+ * counted as asked once it is called. An upload is not among them: it is
125
+ * counted once its POST left ({@link uploadWatched_make}).
126
+ */
127
+ const CHANGING_ACTS = new Set([
128
+ 'file_moveById', 'file_deleteById', 'folder_create', 'folder_deleteById', 'folder_moveById', 'link_deleteById',
129
+ ]);
130
+ /**
131
+ * A bound client's upload, watched: it counts as sent once cumin says its
132
+ * POST left, or once CUBE took it, or when it threw (what it did is then
133
+ * not known). An upload that failed before its POST left (no connection,
134
+ * the connection changed, nothing could be reached) is not sent.
135
+ *
136
+ * @param acts - The upload's client.
137
+ * @param sentNote - Notes that the upload was sent.
138
+ * @returns The upload, as the client's own is called.
139
+ */
140
+ function uploadWatched_make(acts, sentNote) {
141
+ return async (content, uploadDir, filename) => {
142
+ let receipt;
143
+ try {
144
+ receipt = await acts.file_uploadReceipted(content, uploadDir, filename, sentNote);
145
+ }
146
+ catch (error) {
147
+ sentNote();
148
+ throw error;
149
+ }
150
+ if (receipt.ok)
151
+ sentNote();
152
+ return receipt;
153
+ };
154
+ }
155
+ /**
156
+ * The upload's client, watched: every request that may change something is
157
+ * noted before it is asked (an upload, once its POST left), so a failure
158
+ * can say whether anything could have changed.
159
+ *
160
+ * @param acts - The upload's client.
161
+ * @returns The watched client, and the question.
162
+ */
163
+ function actsWatched_make(acts) {
164
+ let sent = false;
165
+ const upload_watched = uploadWatched_make(acts, () => { sent = true; });
166
+ const handler = {};
167
+ // A Proxy calls its handler's trap by the name `get`.
168
+ handler.get = (target, name) => {
169
+ if (name === 'file_uploadReceipted')
170
+ return upload_watched;
171
+ const value = Reflect.get(target, name, target);
172
+ if (typeof value !== 'function')
173
+ return value;
174
+ return (...args) => {
175
+ if (CHANGING_ACTS.has(name))
176
+ sent = true;
177
+ return Reflect.apply(value, target, args);
178
+ };
179
+ };
180
+ const watched = new Proxy(acts, handler);
181
+ return {
182
+ acts: watched,
183
+ changed_ask() {
184
+ return sent;
185
+ },
186
+ };
187
+ }
188
+ /**
189
+ * Proves a destination folder, fresh on the upload's client: not there (every
190
+ * name free), or one folder read whole; anything else proves nothing.
191
+ *
192
+ * @param acts - The upload's client.
193
+ * @param folder - The folder.
194
+ * @returns Its names, or why it could not be proved.
195
+ */
196
+ async function destinationFolder_prove(acts, folder) {
197
+ const since = cumin_1.errorStack.checkpoint_mark();
198
+ const there = await (0, proof_js_1.pathHolders_answer)(folder, acts);
199
+ if (there.kind === 'absent') {
200
+ cumin_1.errorStack.checkpoint_drain(since);
201
+ return { names: new Map() };
202
+ }
203
+ if (there.kind === 'held' && there.holder.kind === 'dir') {
204
+ const held = await (0, proof_js_1.folderHolders_prove)(acts, folder);
205
+ if (held.ok) {
206
+ cumin_1.errorStack.checkpoint_drain(since);
207
+ return { names: held.value };
208
+ }
209
+ }
210
+ else if (there.kind !== 'error') {
211
+ cumin_1.errorStack.stack_push('error', `${heldBy_say(there.kind === 'held' ? [there.holder] : there.holders)} holds ${folder}, where a folder is needed`);
212
+ }
213
+ return { refusal: `Not uploaded: ${reasons_take(since, `${folder} could not be proved`)}` };
214
+ }
215
+ /**
216
+ * An upload under way: its one client, the folders the operator agreed to
217
+ * merge into, whether it is forced, and each destination folder's proof,
218
+ * made once.
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * const plan: UploadPlan = await uploadPlan_make([], null);
223
+ * await plan.file_store('/home/ada/a.txt', new Blob(['a']));
224
+ * ```
225
+ */
226
+ class UploadPlan {
227
+ /** The client every proof and write goes to. */
228
+ acts;
229
+ /** The folders a merge was agreed for: a file one holds below them is replaced. */
230
+ merged;
231
+ /** Whether the upload is forced: a file one file holds is replaced wherever it lands. */
232
+ forced;
233
+ /** Each destination folder's proof, asked once. */
234
+ folders = new Map();
235
+ /**
236
+ * @param acts - The upload's client.
237
+ * @param merged - The folders a merge was agreed for.
238
+ * @param forced - Whether the upload is forced.
239
+ */
240
+ constructor(acts, merged, forced) {
241
+ this.acts = acts;
242
+ this.merged = merged;
243
+ this.forced = forced;
244
+ }
245
+ /**
246
+ * A destination folder's proof, asked once; a folder that could not be
247
+ * proved is asked again by the next file (a retry).
248
+ *
249
+ * @param folder - The folder.
250
+ * @returns Its proof.
251
+ */
252
+ async folder_get(folder) {
253
+ const asked = this.folders.get(folder) ?? destinationFolder_prove(this.acts, folder);
254
+ this.folders.set(folder, asked);
255
+ const proved = await asked;
256
+ if ('refusal' in proved)
257
+ this.folders.delete(folder);
258
+ return proved;
259
+ }
260
+ /**
261
+ * Whether a file one file holds is to be replaced: the upload is forced,
262
+ * or the file lands below a folder a merge was agreed for.
263
+ *
264
+ * @param target - The file's path.
265
+ * @returns True when it is.
266
+ */
267
+ replace_isAgreed(target) {
268
+ return this.forced || this.merged.some((folder) => target.startsWith(folder.endsWith('/') ? folder : `${folder}/`));
269
+ }
270
+ /**
271
+ * Stores one file on the proof of its folder (see the module's rules).
272
+ *
273
+ * @param acts - The client the writes go to: the upload's, perhaps watched.
274
+ * @param target - The file's path.
275
+ * @param content - Its content.
276
+ * @returns True when the path holds the content; false otherwise (stacked).
277
+ */
278
+ async file_settle(acts, target, content) {
279
+ const proved = await this.folder_get(path.posix.dirname(target));
280
+ if ('refusal' in proved) {
281
+ cumin_1.errorStack.stack_push('error', proved.refusal);
282
+ return false;
283
+ }
284
+ const held = proved.names.get(path.posix.basename(target)) ?? [];
285
+ if (held.length === 0)
286
+ return (0, replace_js_1.file_createProved)(acts, target, content);
287
+ const sole = held.length === 1 ? held[0] : undefined;
288
+ if (sole?.kind === 'file' && this.replace_isAgreed(target))
289
+ return (0, replace_js_1.file_replace)(target, content, sole.id, acts);
290
+ cumin_1.errorStack.stack_push('error', `Not uploaded: ${heldBy_say(held)} already ${held.length === 1 ? 'holds' : 'hold'} ${target}; nothing was written onto it`);
291
+ return false;
292
+ }
293
+ /**
294
+ * Stores one file of the upload, on the proof of its folder. A store that
295
+ * threw is a store that did not: its words join the reasons it left.
296
+ *
297
+ * @param target - The file's CUBE path; one written without its leading
298
+ * slash is read from the root.
299
+ * @param content - Its content.
300
+ * @returns Nothing, once the path holds the content.
301
+ * @throws {Error} When it does not and nothing that could change CUBE was
302
+ * asked, with every reason the store left: the transfer pool tries it again.
303
+ * @throws {TransferFinalError} When it does not after such a request: the
304
+ * pool does not try it again, and the reasons name where each copy is.
305
+ *
306
+ * @example
307
+ * ```ts
308
+ * await plan.file_store('/home/ada/study/a.txt', new Blob(['a']));
309
+ * ```
310
+ */
311
+ async file_store(target, content) {
312
+ const since = cumin_1.errorStack.checkpoint_mark();
313
+ const watched = actsWatched_make(this.acts);
314
+ try {
315
+ if (await this.file_settle(watched.acts, path.posix.resolve('/', target), content))
316
+ return;
317
+ }
318
+ catch (error) {
319
+ cumin_1.errorStack.stack_push('error', error instanceof Error ? error.message : String(error));
320
+ }
321
+ const reasons = reasons_take(since, 'CUBE did not take it');
322
+ throw watched.changed_ask() ? new cumin_1.TransferFinalError(reasons) : new Error(reasons);
323
+ }
324
+ /**
325
+ * Stores one file of the upload, on the proof of its folder, for a caller
326
+ * that does not retry: its reasons are left on the error stack.
327
+ *
328
+ * @param target - The file's absolute CUBE path.
329
+ * @param content - Its content.
330
+ * @returns True when the path holds the content; false otherwise (stacked).
331
+ *
332
+ * @example
333
+ * ```ts
334
+ * const stored: boolean = await plan.file_tryStore('/home/ada/a.txt', new Blob(['a']));
335
+ * ```
336
+ */
337
+ async file_tryStore(target, content) {
338
+ return this.file_settle(this.acts, path.posix.resolve('/', target), content);
339
+ }
340
+ }
341
+ exports.UploadPlan = UploadPlan;
342
+ /**
343
+ * Settles one folder an upload lands at: free (proved absent), or the one
344
+ * folder there, which the operator agrees to merge into (asked, unless the
345
+ * upload is forced). Its holders are told apart as every proof tells them:
346
+ * a row listed twice is one holder.
347
+ *
348
+ * @param acts - The upload's client.
349
+ * @param target - The folder's path.
350
+ * @param confirm - How a merge is asked; null when the upload is forced.
351
+ * @returns True when a merge into a folder there was agreed; false when nothing is there.
352
+ * @throws {Error} When the place could not be checked, a file or a link
353
+ * holds it, several things share it (two folders, a folder and a file), or the
354
+ * operator declined (nothing has been sent).
355
+ */
356
+ async function uploadTarget_settle(acts, target, confirm) {
357
+ const since = cumin_1.errorStack.checkpoint_mark();
358
+ const proof = await (0, proof_js_1.pathHolders_answer)(target, acts);
359
+ if (proof.kind === 'error') {
360
+ const reasons = reasons_take(since, '');
361
+ throw new Error(`Cannot tell whether '${target}' is already in ChRIS${reasons.length > 0 ? ` (${reasons})` : ''}. Nothing was uploaded.`);
362
+ }
363
+ if (proof.kind === 'absent')
364
+ return false;
365
+ const holders = proof.kind === 'held' ? [proof.holder] : proof.holders;
366
+ const other = holders.find((holder) => holder.kind !== 'dir');
367
+ if (other !== undefined) {
368
+ throw new Error(`A ${other.kind} already holds '${target}' in ChRIS, and a folder cannot be uploaded onto it. Nothing was uploaded.`);
369
+ }
370
+ if (proof.kind === 'ambiguous') {
371
+ throw new Error(`Cannot upload into '${target}': ${(0, proof_js_1.holders_describe)(proof.holders)} share it in ChRIS, and no one of them is the folder to merge into. Nothing was uploaded.`);
372
+ }
373
+ if (confirm !== null)
374
+ await confirm(`Target '${target}' already exists in ChRIS. Merge/overwrite? (y/N)`);
375
+ return true;
376
+ }
377
+ /**
378
+ * Begins an upload: the session's client bound, and every folder it lands
379
+ * at settled before anything is sent, so a "no" at the second of two
380
+ * folders leaves the first unsent.
381
+ *
382
+ * @param targets - The CUBE path each folder source lands at; each is read
383
+ * from the root, as every file stored through the plan is, so the folder
384
+ * asked about is the one merged into (`/dest/./study` is `/dest/study`).
385
+ * @param confirm - How a merge into a folder already there is asked; null
386
+ * when the upload is forced (a merge is then taken as agreed).
387
+ * @returns The plan every file is stored through.
388
+ * @throws {Error} When there is no connection, or a target cannot be
389
+ * settled (nothing has been sent).
390
+ *
391
+ * @example
392
+ * ```ts
393
+ * const plan: UploadPlan = await uploadPlan_make(['/home/ada/study'], async (message) => confirmOrThrow(message));
394
+ * ```
395
+ */
396
+ async function uploadPlan_make(targets, confirm) {
397
+ const acts = await uploadClient_bind();
398
+ const merged = [];
399
+ for (const written of targets) {
400
+ const target = path.posix.resolve('/', written);
401
+ if (await uploadTarget_settle(acts, target, confirm))
402
+ merged.push(target);
403
+ }
404
+ return new UploadPlan(acts, merged, confirm === null);
405
+ }
406
+ /**
407
+ * The session's client, bound for an upload.
408
+ *
409
+ * @returns The binding.
410
+ * @throws {Error} When there is no connection (nothing has been sent).
411
+ */
412
+ async function uploadClient_bind() {
413
+ const since = cumin_1.errorStack.checkpoint_mark();
414
+ const bound = await cumin_1.chrisIO.holderClient_bind();
415
+ if (!bound.ok)
416
+ throw new Error(`There is no connection to CUBE (${reasons_take(since, 'no client')}). Nothing was uploaded.`);
417
+ return bound.value;
418
+ }
419
+ /**
420
+ * Begins an upload that asks nothing and replaces nothing: every file is
421
+ * stored create-only, and a held name refuses that file (a word with no
422
+ * question and no `--force`, such as `path upload`).
423
+ *
424
+ * @returns The plan every file is stored through.
425
+ * @throws {Error} When there is no connection (nothing has been sent).
426
+ *
427
+ * @example
428
+ * ```ts
429
+ * const plan: UploadPlan = await uploadPlanCreateOnly_make();
430
+ * await plan.file_store('/home/ada/a.txt', new Blob(['a']));
431
+ * ```
432
+ */
433
+ async function uploadPlanCreateOnly_make() {
434
+ return new UploadPlan(await uploadClient_bind(), [], false);
435
+ }
436
+ /**
437
+ * Uploads a local file or folder to ChRIS, create-only (`chefs upload`): a
438
+ * folder lands under the destination by its own name, as `cp -r` puts it,
439
+ * and a file at the destination path itself. Each file is stored through a
440
+ * plan that asks nothing and replaces nothing, so a held name refuses that
441
+ * file and says so. The local path is read as written, as the shell would
442
+ * read it; a folder lands under the last name of its path resolved
443
+ * (`study/old/..` as `study`, never above the destination), and one written with `.` last
444
+ * (`.`, `study/.`) puts its contents in the destination itself.
445
+ *
446
+ * @param localPath - The local filesystem path; a relative one is read from the working directory.
447
+ * @param remotePath - The ChRIS destination path.
448
+ * @returns True when every file was stored; false otherwise, with the reasons on the error stack.
449
+ *
450
+ * @example
451
+ * ```ts
452
+ * const uploaded: boolean = await files_uploadPath('/tmp/study', '/home/ada');
453
+ * ```
454
+ */
455
+ async function files_uploadPath(localPath, remotePath) {
456
+ let plan;
457
+ try {
458
+ plan = await uploadPlanCreateOnly_make();
459
+ }
460
+ catch (error) {
461
+ cumin_1.errorStack.stack_push('error', error instanceof Error ? error.message : String(error));
462
+ return false;
463
+ }
464
+ return cumin_1.chrisIO.uploadLocalPath(localPath, remotePath, (content, target) => plan.file_tryStore(target, content), localFolderName_find(localPath));
465
+ }
466
+ /**
467
+ * The name a local folder lands under in an upload: none (its contents in
468
+ * the destination itself) when `.` is its last part, else the last name of
469
+ * its path resolved as written (`study/old/..` is `study`). Only the name is
470
+ * resolved; the folder is read by its path as written.
471
+ *
472
+ * @param localPath - The local path, as written.
473
+ * @returns The folder's name, or null for its contents.
474
+ */
475
+ function localFolderName_find(localPath) {
476
+ return path.basename(localPath) === '.' ? null : path.basename(path.resolve(localPath));
477
+ }
478
+ //# sourceMappingURL=upload.js.map