@fnndsc/cumin 3.30.5 → 3.31.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 (161) hide show
  1. package/dist/cache/checkpointWatch.d.ts +36 -0
  2. package/dist/cache/checkpointWatch.js +83 -0
  3. package/dist/cache/checkpointWatch.js.map +1 -0
  4. package/dist/cache/index.d.ts +1 -1
  5. package/dist/cache/index.js +4 -1
  6. package/dist/cache/index.js.map +1 -1
  7. package/dist/cache/listCache.d.ts +54 -2
  8. package/dist/cache/listCache.js +120 -24
  9. package/dist/cache/listCache.js.map +1 -1
  10. package/dist/cache/listCheckpoint.d.ts +5 -8
  11. package/dist/cache/listCheckpoint.js +17 -28
  12. package/dist/cache/listCheckpoint.js.map +1 -1
  13. package/dist/cache/listingInvalidation.js +11 -6
  14. package/dist/cache/listingInvalidation.js.map +1 -1
  15. package/dist/cache/procCache.d.ts +11 -0
  16. package/dist/cache/procCache.js +26 -1
  17. package/dist/cache/procCache.js.map +1 -1
  18. package/dist/cache/procCheckpoint.d.ts +11 -6
  19. package/dist/cache/procCheckpoint.js +280 -86
  20. package/dist/cache/procCheckpoint.js.map +1 -1
  21. package/dist/cache/queryIndex.d.ts +11 -0
  22. package/dist/cache/queryIndex.js +38 -2
  23. package/dist/cache/queryIndex.js.map +1 -1
  24. package/dist/cache/queryIndexCheckpoint.d.ts +32 -0
  25. package/dist/cache/queryIndexCheckpoint.js +180 -30
  26. package/dist/cache/queryIndexCheckpoint.js.map +1 -1
  27. package/dist/chrisapi/adapter.d.ts +266 -37
  28. package/dist/chrisapi/adapter.js +577 -116
  29. package/dist/chrisapi/adapter.js.map +1 -1
  30. package/dist/chrisapi/contract.d.ts +20 -20
  31. package/dist/chrisapi/contract.js +213 -60
  32. package/dist/chrisapi/contract.js.map +1 -1
  33. package/dist/chrisapi/cubeRequest.d.ts +198 -0
  34. package/dist/chrisapi/cubeRequest.js +345 -0
  35. package/dist/chrisapi/cubeRequest.js.map +1 -0
  36. package/dist/chrisapi/shape.d.ts +186 -0
  37. package/dist/chrisapi/shape.js +274 -0
  38. package/dist/chrisapi/shape.js.map +1 -0
  39. package/dist/cli.js +2 -1
  40. package/dist/cli.js.map +1 -1
  41. package/dist/compute/computeResources.d.ts +3 -0
  42. package/dist/compute/computeResources.js +5 -3
  43. package/dist/compute/computeResources.js.map +1 -1
  44. package/dist/config/config.d.ts +14 -0
  45. package/dist/config/config.js +6 -3
  46. package/dist/config/config.js.map +1 -1
  47. package/dist/connect/chrisConnection.d.ts +116 -6
  48. package/dist/connect/chrisConnection.js +418 -74
  49. package/dist/connect/chrisConnection.js.map +1 -1
  50. package/dist/connect/identityEpoch.d.ts +263 -0
  51. package/dist/connect/identityEpoch.js +414 -0
  52. package/dist/connect/identityEpoch.js.map +1 -0
  53. package/dist/connect/loginFailure.js +39 -22
  54. package/dist/connect/loginFailure.js.map +1 -1
  55. package/dist/context/chrisContext.js +36 -30
  56. package/dist/context/chrisContext.js.map +1 -1
  57. package/dist/error/errorStack.d.ts +1 -1
  58. package/dist/error/errorStack.js +1 -2
  59. package/dist/error/errorStack.js.map +1 -1
  60. package/dist/feeds/chrisAccess.d.ts +7 -2
  61. package/dist/feeds/chrisAccess.js +20 -7
  62. package/dist/feeds/chrisAccess.js.map +1 -1
  63. package/dist/feeds/chrisFeed.d.ts +8 -10
  64. package/dist/feeds/chrisFeed.js +27 -57
  65. package/dist/feeds/chrisFeed.js.map +1 -1
  66. package/dist/feeds/chrisTags.d.ts +1 -1
  67. package/dist/feeds/chrisTags.js +66 -24
  68. package/dist/feeds/chrisTags.js.map +1 -1
  69. package/dist/filebrowser/chrisFileBrowser.d.ts +0 -1
  70. package/dist/filebrowser/chrisFileBrowser.js +3 -5
  71. package/dist/filebrowser/chrisFileBrowser.js.map +1 -1
  72. package/dist/filebrowser/chrisFiles.d.ts +0 -1
  73. package/dist/filebrowser/chrisFiles.js +3 -4
  74. package/dist/filebrowser/chrisFiles.js.map +1 -1
  75. package/dist/filebrowser/chrisPACS.d.ts +4 -4
  76. package/dist/filebrowser/chrisPACS.js +10 -33
  77. package/dist/filebrowser/chrisPACS.js.map +1 -1
  78. package/dist/filebrowser/chrisPipeline.js +10 -45
  79. package/dist/filebrowser/chrisPipeline.js.map +1 -1
  80. package/dist/groups/chrisGroups.d.ts +3 -0
  81. package/dist/groups/chrisGroups.js +20 -8
  82. package/dist/groups/chrisGroups.js.map +1 -1
  83. package/dist/index.d.ts +7 -8
  84. package/dist/index.js +18 -28
  85. package/dist/index.js.map +1 -1
  86. package/dist/io/chrisIO.d.ts +81 -42
  87. package/dist/io/chrisIO.js +176 -325
  88. package/dist/io/chrisIO.js.map +1 -1
  89. package/dist/io/holderClient.d.ts +248 -0
  90. package/dist/io/holderClient.js +289 -0
  91. package/dist/io/holderClient.js.map +1 -0
  92. package/dist/io/holderFolder.d.ts +151 -0
  93. package/dist/io/holderFolder.js +311 -0
  94. package/dist/io/holderFolder.js.map +1 -0
  95. package/dist/io/holderIO.d.ts +258 -0
  96. package/dist/io/holderIO.js +557 -0
  97. package/dist/io/holderIO.js.map +1 -0
  98. package/dist/io/holderRequest.d.ts +299 -0
  99. package/dist/io/holderRequest.js +447 -0
  100. package/dist/io/holderRequest.js.map +1 -0
  101. package/dist/io/holderUpload.d.ts +62 -0
  102. package/dist/io/holderUpload.js +81 -0
  103. package/dist/io/holderUpload.js.map +1 -0
  104. package/dist/io/io.d.ts +12 -0
  105. package/dist/io/io.js.map +1 -1
  106. package/dist/io/localUpload.d.ts +59 -0
  107. package/dist/io/localUpload.js +150 -0
  108. package/dist/io/localUpload.js.map +1 -0
  109. package/dist/io/node_io.d.ts +16 -0
  110. package/dist/io/node_io.js +72 -0
  111. package/dist/io/node_io.js.map +1 -1
  112. package/dist/io/streamWatch.js +2 -2
  113. package/dist/io/streamWatch.js.map +1 -1
  114. package/dist/io/transferPool.d.ts +150 -0
  115. package/dist/io/transferPool.js +196 -0
  116. package/dist/io/transferPool.js.map +1 -0
  117. package/dist/pacs/chrisPACS.d.ts +3 -9
  118. package/dist/pacs/chrisPACS.js +102 -61
  119. package/dist/pacs/chrisPACS.js.map +1 -1
  120. package/dist/pacs/queryIndexSweep.d.ts +0 -9
  121. package/dist/pacs/queryIndexSweep.js +45 -41
  122. package/dist/pacs/queryIndexSweep.js.map +1 -1
  123. package/dist/path/chrisPath.d.ts +8 -3
  124. package/dist/path/chrisPath.js +10 -9
  125. package/dist/path/chrisPath.js.map +1 -1
  126. package/dist/pipelines/chrisPipeline.d.ts +28 -5
  127. package/dist/pipelines/chrisPipeline.js +270 -94
  128. package/dist/pipelines/chrisPipeline.js.map +1 -1
  129. package/dist/pipelines/pipelineMemo.d.ts +0 -16
  130. package/dist/pipelines/pipelineMemo.js +0 -27
  131. package/dist/pipelines/pipelineMemo.js.map +1 -1
  132. package/dist/plugins/chrisPlugins.d.ts +9 -5
  133. package/dist/plugins/chrisPlugins.js +97 -58
  134. package/dist/plugins/chrisPlugins.js.map +1 -1
  135. package/dist/resources/chrisObjContext.d.ts +7 -11
  136. package/dist/resources/chrisObjContext.js +7 -42
  137. package/dist/resources/chrisObjContext.js.map +1 -1
  138. package/dist/resources/chrisResources.d.ts +186 -74
  139. package/dist/resources/chrisResources.js +421 -117
  140. package/dist/resources/chrisResources.js.map +1 -1
  141. package/dist/resources/resourceShapes.d.ts +109 -0
  142. package/dist/resources/resourceShapes.js +22 -0
  143. package/dist/resources/resourceShapes.js.map +1 -0
  144. package/dist/users/chrisUsers.d.ts +4 -1
  145. package/dist/users/chrisUsers.js +6 -2
  146. package/dist/users/chrisUsers.js.map +1 -1
  147. package/dist/utils/keypair.js +21 -10
  148. package/dist/utils/keypair.js.map +1 -1
  149. package/package.json +15 -3
  150. package/dist/activities/chrisActivity.d.ts +0 -19
  151. package/dist/activities/chrisActivity.js +0 -29
  152. package/dist/activities/chrisActivity.js.map +0 -1
  153. package/dist/jobs/chrisJob.d.ts +0 -43
  154. package/dist/jobs/chrisJob.js +0 -174
  155. package/dist/jobs/chrisJob.js.map +0 -1
  156. package/dist/jobs/index.d.ts +0 -7
  157. package/dist/jobs/index.js +0 -24
  158. package/dist/jobs/index.js.map +0 -1
  159. package/dist/jobs/jobMonitor.d.ts +0 -46
  160. package/dist/jobs/jobMonitor.js +0 -102
  161. package/dist/jobs/jobMonitor.js.map +0 -1
@@ -12,11 +12,11 @@ exports.ChRISResource = void 0;
12
12
  exports.resourceFields_get = resourceFields_get;
13
13
  const adapter_js_1 = require("../chrisapi/adapter.js");
14
14
  const chrisConnection_js_1 = require("../connect/chrisConnection.js");
15
- const config_js_1 = require("../config/config.js");
16
15
  const errorStack_js_1 = require("../error/errorStack.js");
17
- /**
18
- * A dictionary with string keys and primitive values.
19
- */
16
+ /** A field of a collection item: its name is read as text. */
17
+ const ITEM_FIELD_SHAPE = (0, adapter_js_1.shape_build)('a field of a collection item')(['name', 'string']);
18
+ /** A collection item: its fields are walked as a list, each by name, and its address is split as text. */
19
+ const ITEM_SHAPE = (0, adapter_js_1.shape_build)('a collection item')(['data', { rows: ITEM_FIELD_SHAPE }], ['href', 'string']);
20
20
  /**
21
21
  * How many records a page asks for.
22
22
  *
@@ -25,20 +25,86 @@ const errorStack_js_1 = require("../error/errorStack.js");
25
25
  * thing that walks and the thing that stops.
26
26
  */
27
27
  const PAGE_SIZE = 100;
28
+ /**
29
+ * Keeps what was kept of one page: its rows after those before, and its
30
+ * fields when no page before named any.
31
+ *
32
+ * @param gathered - What the walk has kept; added to in place.
33
+ * @param result - What was kept of the page, or null when nothing was.
34
+ */
35
+ function walkRows_take(gathered, result) {
36
+ if (result !== null && result.tableData) {
37
+ if (gathered.selectedFields.length === 0)
38
+ gathered.selectedFields = result.selectedFields;
39
+ gathered.tableData = gathered.tableData.concat(result.tableData);
40
+ }
41
+ }
42
+ /**
43
+ * What a failed page makes of a walk: a walk that never gathered a row is
44
+ * a failed listing, the failure thrown as it came; one that did stops, and
45
+ * the failure is why.
46
+ *
47
+ * @param gathered - How many rows the walk has kept.
48
+ * @param error - What the page failed with.
49
+ * @returns Why the walk stopped short.
50
+ * @throws {unknown} The failure itself, as it came, when nothing was gathered.
51
+ */
52
+ function walkFailure_bear(gathered, error) {
53
+ if (gathered === 0)
54
+ throw error;
55
+ return error instanceof Error ? error.message : String(error);
56
+ }
57
+ /**
58
+ * What a stalled page (no rows, and more said to follow) makes of a walk:
59
+ * a failure when nothing was gathered, else the reason it stopped short.
60
+ *
61
+ * @param gathered - How many rows the walk has kept.
62
+ * @param stall - The stall, said.
63
+ * @returns The stall, as why the walk stopped short.
64
+ * @throws {Error} The stall, when nothing was gathered.
65
+ */
66
+ function walkStall_bear(gathered, stall) {
67
+ if (gathered === 0)
68
+ throw new Error(stall);
69
+ return stall;
70
+ }
71
+ /**
72
+ * Whether a page that sent rows was the walk's last: the server says no
73
+ * more follows, or says nothing and the page came shorter than asked.
74
+ *
75
+ * @param more - Whether the server says more follows; null when it says nothing.
76
+ * @param received - The rows the page sent.
77
+ * @param limit - The rows asked for.
78
+ * @returns True when the walk ends with this page.
79
+ */
80
+ function walkPage_isLast(more, received, limit) {
81
+ // The server's own word when it gives one; when it gives none, a page
82
+ // shorter than was asked for is the last, as it always was.
83
+ if (more === false)
84
+ return true;
85
+ if (more === null && received < limit)
86
+ return true;
87
+ return false;
88
+ }
28
89
  /**
29
90
  * Base class for managing ChRIS resources.
30
91
  */
31
92
  class ChRISResource {
32
93
  constructor() {
33
- this._client = null;
34
94
  this._resourceName = "";
35
95
  this._resourceCollection = null;
36
96
  this._resourceArrayItems = null;
37
97
  this._resourceArray = null;
98
+ /** Whether the last read was a query naming a search or a bound: then its answer (or none) is what is read. */
99
+ this._queryAsked = false;
100
+ /** How many reads have been asked of this resource: only the newest one's answer is kept. */
101
+ this._readSeq = 0;
38
102
  this._resourceItem = null;
39
103
  this.resourceMethod = null;
40
104
  this._lazyMethodName = null;
41
105
  this._lazyObjProvider = null;
106
+ /** What the last `resources_getAll` walk saw. */
107
+ this._walkSeen = { firstWhole: false, received: 0, reported: -1 };
42
108
  // Client initialization is deferred
43
109
  }
44
110
  /**
@@ -46,10 +112,9 @@ class ChRISResource {
46
112
  * @returns A Promise resolving to the Client instance or null.
47
113
  */
48
114
  async client_get() {
49
- if (!this._client) {
50
- this._client = await chrisConnection_js_1.chrisConnection.client_get();
51
- }
52
- return this._client;
115
+ // Asked of the connection each time, never kept: the connection knows who is connected now, and whose
116
+ // client a `sudo` scope is running under.
117
+ return chrisConnection_js_1.chrisConnection.client_get();
53
118
  }
54
119
  get resourceName() {
55
120
  return this._resourceName;
@@ -109,7 +174,7 @@ class ChRISResource {
109
174
  if (!resources || !resources.collection) {
110
175
  return null;
111
176
  }
112
- return (0, adapter_js_1.collectionItems_get)(resources).map((item) => ({
177
+ return (0, adapter_js_1.collectionItems_get)(resources, ITEM_SHAPE).map((item) => ({
113
178
  data: item.data,
114
179
  href: item.href,
115
180
  links: item.links,
@@ -153,30 +218,84 @@ class ChRISResource {
153
218
  }
154
219
  }
155
220
  }
221
+ /**
222
+ * What the page last fetched says about a page after it.
223
+ *
224
+ * The SDK derives this from the collection's `next` link and names it
225
+ * `hasNextPage`. It was read here as `hasNext`, which the SDK does not
226
+ * have, so the answer was always undefined and a walk could only guess
227
+ * from the length of a page.
228
+ *
229
+ * @returns True when a `next` link is there, false when the page carries
230
+ * links and none is `next`, and null when it carries no links at all:
231
+ * such a page has said nothing either way, and the SDK's getter would
232
+ * fail on it.
233
+ */
234
+ nextPage_read() {
235
+ const page = this._resourceCollection;
236
+ if (!(0, adapter_js_1.resource_isList)(page))
237
+ return null;
238
+ if (!Array.isArray(page.collection?.links))
239
+ return null;
240
+ return page.hasNextPage;
241
+ }
242
+ /** Whether the server says another page follows the one last fetched; false when it does not say. */
156
243
  get hasNextPage() {
157
- if ((0, adapter_js_1.resource_isList)(this._resourceCollection)) {
158
- return this._resourceCollection.hasNext;
159
- }
160
- return false;
244
+ return this.nextPage_read() === true;
245
+ }
246
+ /**
247
+ * How many rows the server sent on the page last fetched.
248
+ *
249
+ * @returns The count, or null when the page is not a list that says.
250
+ */
251
+ rawCount_read() {
252
+ const page = this._resourceCollection;
253
+ if (!(0, adapter_js_1.resource_isList)(page))
254
+ return null;
255
+ const items = page.collection?.items;
256
+ return Array.isArray(items) ? items.length : null;
161
257
  }
162
258
  /**
163
259
  * Lists *all* resources by automatically handling pagination.
260
+ *
261
+ * The walk is steered by what the server sent. Its search is the
262
+ * server's on every page: a query the server refuses fails the walk
263
+ * (see {@link resources_getList}). Each step moves on by the number of
264
+ * rows received, since a server may send fewer than were asked for and
265
+ * still have more.
266
+ *
164
267
  * @param options - Filtering options. Limit and offset will be managed internally.
165
- * @returns A Promise resolving to FilteredResourceData containing all matching resources, or null.
268
+ * @returns Every matching resource, or null when there is none. When the
269
+ * walk could not finish, the rows it has and `incomplete`.
270
+ * @throws {Error} When the first page cannot be fetched, or the walk
271
+ * stalls with nothing gathered: neither is an empty collection.
166
272
  */
167
273
  async resources_getAll(options) {
168
274
  const limit = PAGE_SIZE;
169
275
  let offset = 0;
170
- let allTableData = [];
171
- let selectedFields = [];
172
- // If options has a limit, we should respect it? No, getAll implies fetching everything.
173
- // But we should respect filters.
276
+ const gathered = { tableData: [], selectedFields: [] };
174
277
  let stopped = null;
278
+ this._walkSeen = { firstWhole: false, received: 0, reported: -1 };
175
279
  while (true) {
176
280
  const pageOptions = { ...options, limit, offset };
177
281
  let result;
282
+ // A fetch that yields nothing leaves the last page's collection in
283
+ // place; cleared, what is read below is this page's or nobody's.
284
+ this._resourceCollection = null;
178
285
  try {
179
- result = this.resources_filterByFields(await this.resourceFields_get(await this.resources_getList(pageOptions)));
286
+ const { asked, superseded } = await this.page_ask(pageOptions);
287
+ this.pageSuperseded_refuse(superseded, offset);
288
+ // Nothing came, or something that is not a list. For the first
289
+ // page that is how a collection with nothing in it answers. For a
290
+ // later page it is a page that went missing: the one before it
291
+ // was not the last, and its rows are not the whole answer.
292
+ const listed = (0, adapter_js_1.resource_isList)(this.collection_read());
293
+ if (!listed && offset === 0)
294
+ break;
295
+ if (!listed)
296
+ throw new Error(`${this._resourceName}: the page at offset ${offset} did not come as a list`);
297
+ this.walkPageItems_check(offset);
298
+ result = this.resources_filterByFields(await this.resourceFields_get(asked));
180
299
  }
181
300
  catch (error) {
182
301
  // A walk that never started is a FAILED listing and stays a failure:
@@ -184,49 +303,239 @@ class ChRISResource {
184
303
  // server refused keeps its rows and says where it stopped — CUBE is
185
304
  // known to fail some queries beyond a certain offset, and a short
186
305
  // answer that reads as a complete one is the defect this fixes.
187
- if (allTableData.length === 0)
188
- throw error;
189
- stopped = error instanceof Error ? error.message : String(error);
190
- break;
191
- }
192
- if (!result || !result.tableData) {
306
+ stopped = walkFailure_bear(gathered.tableData.length, error);
193
307
  break;
194
308
  }
195
- if (selectedFields.length === 0) {
196
- selectedFields = result.selectedFields;
197
- }
198
- allTableData = allTableData.concat(result.tableData);
199
- // Robust exit conditions:
200
- // 1. If we got 0 items, we are definitely done.
201
- if (result.tableData.length === 0) {
202
- break;
203
- }
204
- // 2. If we got fewer items than the limit, this must be the last page.
205
- if (result.tableData.length < limit) {
309
+ walkRows_take(gathered, result);
310
+ // What the server sent, whatever was kept of it. A page that is not a
311
+ // list says nothing of the kind, and the kept rows stand in for it.
312
+ const received = this.pageReceived_note(result);
313
+ const more = this.nextPage_read();
314
+ if (received === 0) {
315
+ if (more !== true)
316
+ break;
317
+ // No rows and "more follows": following it would ask for the same
318
+ // offset for ever, and stopping here quietly would pass a short
319
+ // listing off as a whole one.
320
+ const stall = `${this._resourceName}: the server sent no rows at offset ${offset} and said that more follow`;
321
+ errorStack_js_1.errorStack.stack_push("warning", stall);
322
+ stopped = walkStall_bear(gathered.tableData.length, stall);
206
323
  break;
207
324
  }
208
- // 3. If API explicitly says no next page (and it's not undefined).
209
- if (this.hasNextPage === false) {
325
+ if (walkPage_isLast(more, received, limit))
210
326
  break;
211
- }
212
- // If we got a full page and hasNextPage is not explicitly false, continue
213
- offset += limit;
327
+ offset += received;
214
328
  }
215
- if (allTableData.length === 0) {
216
- return null;
329
+ return this.walkAnswer_make(gathered.tableData, gathered.selectedFields, stopped);
330
+ }
331
+ /**
332
+ * Refuses a page a newer read of this resource replaced: its collection
333
+ * is the newer read's, not this walk's.
334
+ *
335
+ * @param superseded - Whether a newer read replaced the page.
336
+ * @param offset - The page's offset.
337
+ * @throws {Error} When the page was replaced.
338
+ */
339
+ pageSuperseded_refuse(superseded, offset) {
340
+ if (superseded)
341
+ throw new Error(`${this._resourceName}: a newer read of this resource replaced the page at offset ${String(offset)}`);
342
+ }
343
+ /**
344
+ * Checks that a page of a `resources_getAll` walk, already known to be a
345
+ * list, carries an array of items, and notes it of the first page.
346
+ *
347
+ * @param offset - The page's offset.
348
+ * @throws {Error} When a later page carries no array of items.
349
+ */
350
+ walkPageItems_check(offset) {
351
+ // The SDK answers a body with no collection in it as a list all the
352
+ // same, one that holds nothing. A later page like that has not said
353
+ // "no more rows": it has said nothing.
354
+ if (offset > 0 && this.rawCount_read() === null) {
355
+ throw new Error(`${this._resourceName}: the page at offset ${offset} did not come as a list of items`);
217
356
  }
357
+ if (offset === 0)
358
+ this._walkSeen.firstWhole = this.rawCount_read() !== null;
359
+ }
360
+ /**
361
+ * The page last fetched by a `resources_getWhole` walk, held to what such
362
+ * a walk needs: a list, carrying an array of items and an array of links.
363
+ *
364
+ * @param offset - The page's offset.
365
+ * @returns The page and its items.
366
+ * @throws {Error} When the page is not a list, or carries no items or no links.
367
+ */
368
+ wholePage_read(offset) {
369
+ const page = this.collection_read();
370
+ if (!(0, adapter_js_1.resource_isList)(page)) {
371
+ throw new Error(`${this._resourceName}: the answer at offset ${offset} is not a list`);
372
+ }
373
+ const collection = page.collection;
374
+ const items = collection?.items;
375
+ if (!Array.isArray(items)) {
376
+ throw new Error(`${this._resourceName}: the list at offset ${offset} carries no items`);
377
+ }
378
+ // Whether more follows is read from the page's links. A page without
379
+ // them has not said that nothing follows.
380
+ if (!Array.isArray(collection?.links)) {
381
+ throw new Error(`${this._resourceName}: the list at offset ${offset} carries no links, so it does not say whether more follows`);
382
+ }
383
+ return { page, items };
384
+ }
385
+ /**
386
+ * What a walk answers: its rows, and `incomplete` when it stopped short or
387
+ * the server counted more rows than it sent, though every page came.
388
+ *
389
+ * @param tableData - The rows kept.
390
+ * @param selectedFields - The fields they carry.
391
+ * @param stopped - Why the walk stopped short, or null when it did not.
392
+ * @returns The rows, or null when none were kept.
393
+ */
394
+ walkAnswer_make(tableData, selectedFields, stopped) {
395
+ if (tableData.length === 0)
396
+ return null;
397
+ const seen = this._walkSeen;
398
+ const short = stopped
399
+ ?? (seen.reported > seen.received ? `${this._resourceName}: the server counts ${String(seen.reported)} and sent ${String(seen.received)}` : null);
218
400
  return {
219
- tableData: allTableData,
401
+ tableData,
220
402
  selectedFields,
221
- totalCount: allTableData.length,
222
- ...(stopped === null ? {} : {
223
- incomplete: {
224
- gathered: allTableData.length,
225
- reason: `stopped after ${allTableData.length}: ${stopped}`,
226
- },
227
- }),
403
+ totalCount: tableData.length,
404
+ ...(short === null ? {} : { incomplete: { gathered: tableData.length, reason: `stopped after ${tableData.length}: ${short}` } }),
228
405
  };
229
406
  }
407
+ /**
408
+ * How many rows the server sent on the page last fetched, whatever was
409
+ * kept of them, noted with the count that page reports for the whole
410
+ * collection.
411
+ *
412
+ * @param result - What was kept of the page: it stands in for a page that is not a list.
413
+ * @returns The rows sent.
414
+ */
415
+ pageReceived_note(result) {
416
+ const received = this.rawCount_read() ?? result?.tableData?.length ?? 0;
417
+ const page = this.collection_read();
418
+ this._walkSeen.received += received;
419
+ if ((0, adapter_js_1.resource_isList)(page))
420
+ this._walkSeen.reported = Math.max(this._walkSeen.reported, this.pageCount_read(page));
421
+ return received;
422
+ }
423
+ /**
424
+ * Whether the last {@link resources_getAll} walk's first page was fetched
425
+ * and came as a list carrying an array of items, and the walk received as
426
+ * many rows as any page's count reports. Its null answers "nothing here"
427
+ * only then: a resource never bound to a fetch, a first answer that is no
428
+ * list, or a server that counts rows it did not send gives null too, and
429
+ * says nothing.
430
+ *
431
+ * @returns True when the first page came whole and no count contradicts the rows.
432
+ *
433
+ * @example
434
+ * ```ts
435
+ * const empty: boolean = (await resource.resources_getAll()) === null && resource.firstPage_isWhole();
436
+ * ```
437
+ */
438
+ firstPage_isWhole() {
439
+ return this._walkSeen.firstWhole && this._walkSeen.reported <= this._walkSeen.received;
440
+ }
441
+ /**
442
+ * The collection the last fetch left, read after the fetch.
443
+ *
444
+ * A method, so the compiler does not carry what was assigned before the
445
+ * fetch across the `await` that replaced it.
446
+ *
447
+ * @returns The collection, or null when the fetch left none.
448
+ */
449
+ collection_read() {
450
+ return this._resourceCollection;
451
+ }
452
+ /**
453
+ * Walks the whole collection and answers only when it has all of it.
454
+ *
455
+ * {@link resources_getAll} is forgiving, which suits a listing meant to be
456
+ * shown: a page it cannot read ends the walk with the rows it has, a page
457
+ * shorter than the bound is taken as the last, and an answer that is not
458
+ * a list reads as "nothing here". An act that destroys on the strength of
459
+ * a listing cannot use an answer like that. Here every page must be a
460
+ * list that carries an array of items, the walk follows the server's own
461
+ * word on whether more follows and never infers the end from a short
462
+ * page, and anything else is an error.
463
+ *
464
+ * The end of the walk is the server's `next` link alone. A count the
465
+ * server sends is a second witness: a walk that ends having received
466
+ * fewer rows than the largest count any page reported is `incomplete`.
467
+ * A page with no count (the SDK's -1) says nothing about the total, and
468
+ * so never stands in for the `next` link.
469
+ *
470
+ * @param options - Filtering options. Limit and offset are managed here.
471
+ * @param walk - `hrefs`: keep each row's own address, as `href`, so a
472
+ * caller can act on the row without finding it again.
473
+ * @returns Every row of the collection. `tableData` is empty, not absent,
474
+ * when the collection holds nothing; `incomplete` is set when a count
475
+ * the server reported exceeds the rows received.
476
+ * @throws {Error} When a page could not be fetched, is not a list, carries
477
+ * no array of items or of links, or says that more follows and gives no
478
+ * rows.
479
+ */
480
+ async resources_getWhole(options, walk = {}) {
481
+ const tableData = [];
482
+ let selectedFields = [];
483
+ let offset = 0;
484
+ let reported = -1;
485
+ let received = 0;
486
+ for (;;) {
487
+ // A fetch that yields nothing leaves the last page's collection in
488
+ // place; cleared, such a fetch is seen for what it is.
489
+ this._resourceCollection = null;
490
+ const { asked, superseded } = await this.page_ask({ ...options, limit: PAGE_SIZE, offset });
491
+ this.pageSuperseded_refuse(superseded, offset);
492
+ const { page, items } = this.wholePage_read(offset);
493
+ const rows = await this.pageRows_read(asked, walk);
494
+ if (rows !== null && rows.tableData) {
495
+ tableData.push(...rows.tableData);
496
+ if (selectedFields.length === 0)
497
+ selectedFields = rows.selectedFields;
498
+ }
499
+ received += items.length;
500
+ reported = Math.max(reported, this.pageCount_read(page));
501
+ if (!this.hasNextPage)
502
+ break;
503
+ if (items.length === 0) {
504
+ throw new Error(`${this._resourceName}: more is said to follow offset ${offset}, and the page is empty`);
505
+ }
506
+ offset += items.length;
507
+ }
508
+ if (reported > received) {
509
+ const reason = `${this._resourceName}: the server counts ${String(reported)} and sent ${String(received)}`;
510
+ return { tableData, selectedFields, totalCount: reported, incomplete: { gathered: received, reason } };
511
+ }
512
+ return { tableData, selectedFields, totalCount: tableData.length };
513
+ }
514
+ /**
515
+ * The rows of the page last fetched, each with its own address when asked.
516
+ *
517
+ * @param asked - The options the page was fetched with.
518
+ * @param walk - Whether to keep each row's address.
519
+ * @returns The rows, or null when the page gave none.
520
+ */
521
+ async pageRows_read(asked, walk) {
522
+ const fielded = await this.resourceFields_get(asked);
523
+ const rows = this.resources_filterByFields(fielded);
524
+ if (rows === null || walk.hrefs !== true)
525
+ return rows;
526
+ const items = fielded?.items ?? [];
527
+ return { ...rows, tableData: rows.tableData.map((row, index) => ({ ...row, href: items[index]?.href })) };
528
+ }
529
+ /**
530
+ * The total a page reports for its whole collection.
531
+ *
532
+ * @param page - The page.
533
+ * @returns The count, or -1 when the page sends none (the SDK's own word for that).
534
+ */
535
+ pageCount_read(page) {
536
+ const counted = page.totalCount;
537
+ return typeof counted === 'number' && Number.isSafeInteger(counted) ? counted : -1;
538
+ }
230
539
  /**
231
540
  * Lists and filters resources based on options.
232
541
  * @param options - Filtering and listing options.
@@ -240,7 +549,10 @@ class ChRISResource {
240
549
  if (options?.limit === undefined) {
241
550
  return await this.resources_getAll(options);
242
551
  }
243
- const results = this.resources_filterByFields(await this.resourceFields_get(await this.resources_getList(options)));
552
+ const { asked, superseded } = await this.page_ask(options);
553
+ if (superseded)
554
+ return null;
555
+ const results = this.resources_filterByFields(await this.resourceFields_get(asked));
244
556
  if (results && (0, adapter_js_1.resource_isList)(this._resourceCollection)) {
245
557
  const total = this._resourceCollection.totalCount;
246
558
  if (total >= 0)
@@ -306,7 +618,10 @@ class ChRISResource {
306
618
  return selectedFields;
307
619
  }
308
620
  async resourceFields_get(options, fields) {
309
- if (!this._resourceCollection) {
621
+ // Only a resource nothing has been asked of yet loads its collection here. One whose query came
622
+ // back with nothing, or was refused, has no rows: asking again without that query would answer
623
+ // a broader question than the one asked.
624
+ if (!this._resourceCollection && !this._queryAsked) {
310
625
  await this.resources_getList();
311
626
  }
312
627
  if (!this._resourceArrayItems) {
@@ -340,51 +655,56 @@ class ChRISResource {
340
655
  if (!this.resourceMethod) {
341
656
  return null;
342
657
  }
658
+ // The collection just fetched, not the one a call before this left.
343
659
  const resources = await this.resourceMethod(params);
344
- if (!(0, adapter_js_1.resource_isList)(this._resourceCollection)) {
660
+ if (!(0, adapter_js_1.resource_isList)(resources)) {
345
661
  return null;
346
662
  }
347
- this._resourceArray = this._resourceCollection?.getItems();
663
+ this._resourceArray = resources.getItems();
348
664
  if (this._resourceArray) {
349
665
  // console.log(this._resourceArray);
350
666
  return this._resourceArray;
351
667
  }
352
668
  return null;
353
669
  }
354
- options_simplify(options) {
355
- const params = {
356
- limit: options?.limit ?? 20,
357
- offset: options?.offset ?? 0,
358
- };
359
- return params;
360
- }
361
- filtering_applyAdditional(originalParams) {
362
- if (!this._resourceArrayItems || this._resourceArrayItems.length === 0) {
363
- return null;
364
- }
365
- const filteredItems = this._resourceArrayItems.filter((item) => {
366
- return Object.entries(originalParams).every(([paramKey, paramValue]) => {
367
- // Skip non-search related parameters
368
- if (["limit", "offset"].includes(paramKey)) {
369
- return true;
370
- }
371
- const dataItem = item.data.find((d) => d.name === paramKey);
372
- if (!dataItem) {
373
- return false; // If the field is not found, it doesn't match the filter
374
- }
375
- const itemValue = String(dataItem.value).toLowerCase();
376
- const searchValue = String(paramValue).toLowerCase();
377
- // Perform exact match for 'id', partial match for other fields
378
- if (paramKey === "id") {
379
- return itemValue === searchValue;
380
- }
381
- else {
382
- return itemValue.includes(searchValue);
383
- }
384
- });
385
- });
386
- return filteredItems.length > 0 ? filteredItems : null;
670
+ /**
671
+ * Asks for one page as {@link resources_getList} does, and says whether it
672
+ * is still the newest read of this resource once it has answered: what the
673
+ * resource holds is then the newer read's, never this one's.
674
+ *
675
+ * @param options - The page and search.
676
+ * @returns The parameters asked with (null as `resources_getList` says),
677
+ * and whether a newer read replaced this one.
678
+ */
679
+ async page_ask(options) {
680
+ const answer = this.resources_getList(options);
681
+ // resources_getList numbers its read before it first waits, so this is its number.
682
+ const mine = this._readSeq;
683
+ const asked = await answer;
684
+ return { asked, superseded: mine !== this._readSeq };
387
685
  }
686
+ /**
687
+ * Asks CUBE for one page of the collection, as the caller's parameters
688
+ * name it, and keeps what came as the resource's collection and rows.
689
+ *
690
+ * The search is CUBE's to answer: a query it refuses is refused here
691
+ * (thrown), never asked again without its search and redone on one page
692
+ * by a guess at what the fields mean. Nothing of an earlier question
693
+ * stays readable meanwhile: the collection and rows are cleared before
694
+ * anything is asked, so a refusal leaves none, never the last query's.
695
+ *
696
+ * @param options - The page and search, as CUBE's query parameters; no
697
+ * bound named asks for a page of {@link PAGE_SIZE}.
698
+ * @param resourceMethod - The fetch to bind, when one is given.
699
+ * @returns The parameters asked with; null when the answer was not a list,
700
+ * or when a newer read of this resource was asked before it answered.
701
+ * @throws {Error} What CUBE's answer was, when it refused or did not answer.
702
+ *
703
+ * @example
704
+ * ```ts
705
+ * await resource.resources_getList({ name: 'pl-dircopy', limit: 10 });
706
+ * ```
707
+ */
388
708
  async resources_getList(options, resourceMethod) {
389
709
  // A caller that named no bound is asking for the collection, not for the
390
710
  // first twenty of it. The default that used to sit here answered a
@@ -398,33 +718,24 @@ class ChRISResource {
398
718
  };
399
719
  // Remove the "fields" otherwise some list resource ops break
400
720
  const { fields, ...pureparams } = params;
401
- let simplifiedParams = pureparams;
402
721
  if (resourceMethod) {
403
722
  this.resourceMethod = resourceMethod;
404
723
  }
724
+ this._resourceCollection = null;
725
+ this._resourceArray = null;
726
+ this._resourceArrayItems = null;
727
+ // Each read is numbered as it is asked; an older read answering after a newer one was asked keeps nothing.
728
+ const mine = ++this._readSeq;
729
+ // Only a query that names a search or a bound guards the rows: an implicit read of the whole
730
+ // collection that failed may simply be read again.
731
+ this._queryAsked = Object.keys(options ?? {}).some((key) => key !== 'fields');
405
732
  await this.lazyBinding_resolve();
406
733
  if (!this.resourceMethod)
407
734
  return params;
408
- let resources = null;
409
- try {
410
- resources = await this.resourceMethod(pureparams);
411
- }
412
- catch (error) {
413
- if (config_js_1.connectionConfig && config_js_1.connectionConfig.debug) {
414
- console.error(`Error fetching ${this._resourceName}:`, error);
415
- }
416
- // Attempt simplified params as fallback
417
- try {
418
- simplifiedParams = this.options_simplify(pureparams);
419
- resources = await this.resourceMethod(simplifiedParams);
420
- }
421
- catch (retryError) {
422
- if (config_js_1.connectionConfig && config_js_1.connectionConfig.debug) {
423
- console.error(`Retry failed for ${this._resourceName}:`, retryError);
424
- }
425
- throw retryError; // Re-throw the error if retry fails
426
- }
427
- }
735
+ const resources = await this.resourceMethod(pureparams);
736
+ // A newer read was asked meanwhile: this one's caller gets nothing, never the newer one's rows.
737
+ if (mine !== this._readSeq)
738
+ return null;
428
739
  if (resources == undefined || resources == null) {
429
740
  console.log(this._resourceName + " resource list returned 'undefined' or 'null'");
430
741
  return params;
@@ -436,13 +747,6 @@ class ChRISResource {
436
747
  this._resourceArray = this._resourceCollection?.getItems();
437
748
  this._resourceArrayItems =
438
749
  this.resourceItems_buildFromCollection(resources);
439
- if (Object.keys(pureparams).length > Object.keys(simplifiedParams).length) {
440
- const filteredItems = this.filtering_applyAdditional(pureparams);
441
- if (filteredItems === null) {
442
- console.log("Warning: No items match the search criteria.");
443
- }
444
- this._resourceArrayItems = filteredItems;
445
- }
446
750
  return params;
447
751
  }
448
752
  }