@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.
- package/dist/cache/checkpointWatch.d.ts +36 -0
- package/dist/cache/checkpointWatch.js +83 -0
- package/dist/cache/checkpointWatch.js.map +1 -0
- package/dist/cache/index.d.ts +1 -1
- package/dist/cache/index.js +4 -1
- package/dist/cache/index.js.map +1 -1
- package/dist/cache/listCache.d.ts +54 -2
- package/dist/cache/listCache.js +120 -24
- package/dist/cache/listCache.js.map +1 -1
- package/dist/cache/listCheckpoint.d.ts +5 -8
- package/dist/cache/listCheckpoint.js +17 -28
- package/dist/cache/listCheckpoint.js.map +1 -1
- package/dist/cache/listingInvalidation.js +11 -6
- package/dist/cache/listingInvalidation.js.map +1 -1
- package/dist/cache/procCache.d.ts +11 -0
- package/dist/cache/procCache.js +26 -1
- package/dist/cache/procCache.js.map +1 -1
- package/dist/cache/procCheckpoint.d.ts +11 -6
- package/dist/cache/procCheckpoint.js +280 -86
- package/dist/cache/procCheckpoint.js.map +1 -1
- package/dist/cache/queryIndex.d.ts +11 -0
- package/dist/cache/queryIndex.js +38 -2
- package/dist/cache/queryIndex.js.map +1 -1
- package/dist/cache/queryIndexCheckpoint.d.ts +32 -0
- package/dist/cache/queryIndexCheckpoint.js +180 -30
- package/dist/cache/queryIndexCheckpoint.js.map +1 -1
- package/dist/chrisapi/adapter.d.ts +266 -37
- package/dist/chrisapi/adapter.js +577 -116
- package/dist/chrisapi/adapter.js.map +1 -1
- package/dist/chrisapi/contract.d.ts +20 -20
- package/dist/chrisapi/contract.js +213 -60
- package/dist/chrisapi/contract.js.map +1 -1
- package/dist/chrisapi/cubeRequest.d.ts +198 -0
- package/dist/chrisapi/cubeRequest.js +345 -0
- package/dist/chrisapi/cubeRequest.js.map +1 -0
- package/dist/chrisapi/shape.d.ts +186 -0
- package/dist/chrisapi/shape.js +274 -0
- package/dist/chrisapi/shape.js.map +1 -0
- package/dist/cli.js +2 -1
- package/dist/cli.js.map +1 -1
- package/dist/compute/computeResources.d.ts +3 -0
- package/dist/compute/computeResources.js +5 -3
- package/dist/compute/computeResources.js.map +1 -1
- package/dist/config/config.d.ts +14 -0
- package/dist/config/config.js +6 -3
- package/dist/config/config.js.map +1 -1
- package/dist/connect/chrisConnection.d.ts +116 -6
- package/dist/connect/chrisConnection.js +418 -74
- package/dist/connect/chrisConnection.js.map +1 -1
- package/dist/connect/identityEpoch.d.ts +263 -0
- package/dist/connect/identityEpoch.js +414 -0
- package/dist/connect/identityEpoch.js.map +1 -0
- package/dist/connect/loginFailure.js +39 -22
- package/dist/connect/loginFailure.js.map +1 -1
- package/dist/context/chrisContext.js +36 -30
- package/dist/context/chrisContext.js.map +1 -1
- package/dist/error/errorStack.d.ts +1 -1
- package/dist/error/errorStack.js +1 -2
- package/dist/error/errorStack.js.map +1 -1
- package/dist/feeds/chrisAccess.d.ts +7 -2
- package/dist/feeds/chrisAccess.js +20 -7
- package/dist/feeds/chrisAccess.js.map +1 -1
- package/dist/feeds/chrisFeed.d.ts +8 -10
- package/dist/feeds/chrisFeed.js +27 -57
- package/dist/feeds/chrisFeed.js.map +1 -1
- package/dist/feeds/chrisTags.d.ts +1 -1
- package/dist/feeds/chrisTags.js +66 -24
- package/dist/feeds/chrisTags.js.map +1 -1
- package/dist/filebrowser/chrisFileBrowser.d.ts +0 -1
- package/dist/filebrowser/chrisFileBrowser.js +3 -5
- package/dist/filebrowser/chrisFileBrowser.js.map +1 -1
- package/dist/filebrowser/chrisFiles.d.ts +0 -1
- package/dist/filebrowser/chrisFiles.js +3 -4
- package/dist/filebrowser/chrisFiles.js.map +1 -1
- package/dist/filebrowser/chrisPACS.d.ts +4 -4
- package/dist/filebrowser/chrisPACS.js +10 -33
- package/dist/filebrowser/chrisPACS.js.map +1 -1
- package/dist/filebrowser/chrisPipeline.js +10 -45
- package/dist/filebrowser/chrisPipeline.js.map +1 -1
- package/dist/groups/chrisGroups.d.ts +3 -0
- package/dist/groups/chrisGroups.js +20 -8
- package/dist/groups/chrisGroups.js.map +1 -1
- package/dist/index.d.ts +7 -8
- package/dist/index.js +18 -28
- package/dist/index.js.map +1 -1
- package/dist/io/chrisIO.d.ts +81 -42
- package/dist/io/chrisIO.js +176 -325
- package/dist/io/chrisIO.js.map +1 -1
- package/dist/io/holderClient.d.ts +248 -0
- package/dist/io/holderClient.js +289 -0
- package/dist/io/holderClient.js.map +1 -0
- package/dist/io/holderFolder.d.ts +151 -0
- package/dist/io/holderFolder.js +311 -0
- package/dist/io/holderFolder.js.map +1 -0
- package/dist/io/holderIO.d.ts +258 -0
- package/dist/io/holderIO.js +557 -0
- package/dist/io/holderIO.js.map +1 -0
- package/dist/io/holderRequest.d.ts +299 -0
- package/dist/io/holderRequest.js +447 -0
- package/dist/io/holderRequest.js.map +1 -0
- package/dist/io/holderUpload.d.ts +62 -0
- package/dist/io/holderUpload.js +81 -0
- package/dist/io/holderUpload.js.map +1 -0
- package/dist/io/io.d.ts +12 -0
- package/dist/io/io.js.map +1 -1
- package/dist/io/localUpload.d.ts +59 -0
- package/dist/io/localUpload.js +150 -0
- package/dist/io/localUpload.js.map +1 -0
- package/dist/io/node_io.d.ts +16 -0
- package/dist/io/node_io.js +72 -0
- package/dist/io/node_io.js.map +1 -1
- package/dist/io/streamWatch.js +2 -2
- package/dist/io/streamWatch.js.map +1 -1
- package/dist/io/transferPool.d.ts +150 -0
- package/dist/io/transferPool.js +196 -0
- package/dist/io/transferPool.js.map +1 -0
- package/dist/pacs/chrisPACS.d.ts +3 -9
- package/dist/pacs/chrisPACS.js +102 -61
- package/dist/pacs/chrisPACS.js.map +1 -1
- package/dist/pacs/queryIndexSweep.d.ts +0 -9
- package/dist/pacs/queryIndexSweep.js +45 -41
- package/dist/pacs/queryIndexSweep.js.map +1 -1
- package/dist/path/chrisPath.d.ts +8 -3
- package/dist/path/chrisPath.js +10 -9
- package/dist/path/chrisPath.js.map +1 -1
- package/dist/pipelines/chrisPipeline.d.ts +28 -5
- package/dist/pipelines/chrisPipeline.js +270 -94
- package/dist/pipelines/chrisPipeline.js.map +1 -1
- package/dist/pipelines/pipelineMemo.d.ts +0 -16
- package/dist/pipelines/pipelineMemo.js +0 -27
- package/dist/pipelines/pipelineMemo.js.map +1 -1
- package/dist/plugins/chrisPlugins.d.ts +9 -5
- package/dist/plugins/chrisPlugins.js +97 -58
- package/dist/plugins/chrisPlugins.js.map +1 -1
- package/dist/resources/chrisObjContext.d.ts +7 -11
- package/dist/resources/chrisObjContext.js +7 -42
- package/dist/resources/chrisObjContext.js.map +1 -1
- package/dist/resources/chrisResources.d.ts +186 -74
- package/dist/resources/chrisResources.js +421 -117
- package/dist/resources/chrisResources.js.map +1 -1
- package/dist/resources/resourceShapes.d.ts +109 -0
- package/dist/resources/resourceShapes.js +22 -0
- package/dist/resources/resourceShapes.js.map +1 -0
- package/dist/users/chrisUsers.d.ts +4 -1
- package/dist/users/chrisUsers.js +6 -2
- package/dist/users/chrisUsers.js.map +1 -1
- package/dist/utils/keypair.js +21 -10
- package/dist/utils/keypair.js.map +1 -1
- package/package.json +15 -3
- package/dist/activities/chrisActivity.d.ts +0 -19
- package/dist/activities/chrisActivity.js +0 -29
- package/dist/activities/chrisActivity.js.map +0 -1
- package/dist/jobs/chrisJob.d.ts +0 -43
- package/dist/jobs/chrisJob.js +0 -174
- package/dist/jobs/chrisJob.js.map +0 -1
- package/dist/jobs/index.d.ts +0 -7
- package/dist/jobs/index.js +0 -24
- package/dist/jobs/index.js.map +0 -1
- package/dist/jobs/jobMonitor.d.ts +0 -46
- package/dist/jobs/jobMonitor.js +0 -102
- package/dist/jobs/jobMonitor.js.map +0 -1
package/dist/chrisapi/adapter.js
CHANGED
|
@@ -53,28 +53,36 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
53
53
|
};
|
|
54
54
|
})();
|
|
55
55
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
56
|
-
exports.TOKEN_CHECK_TIMEOUT_MS = exports.FEED_LIST_TIMEOUT_MS = exports.Client = void 0;
|
|
56
|
+
exports.DATA_CARRIER_SHAPE = exports.ShapeError = exports.shape_object = exports.shape_nullable = exports.shape_build = exports.rows_check = exports.TOKEN_CHECK_TIMEOUT_MS = exports.FEED_LIST_TIMEOUT_MS = exports.Client = void 0;
|
|
57
57
|
exports.authToken_get = authToken_get;
|
|
58
58
|
exports.client_create = client_create;
|
|
59
59
|
exports.rootLinks_share = rootLinks_share;
|
|
60
60
|
exports.token_verify = token_verify;
|
|
61
61
|
exports.client_authGet = client_authGet;
|
|
62
62
|
exports.client_adminUrlEnsure = client_adminUrlEnsure;
|
|
63
|
+
exports.apiRoot_read = apiRoot_read;
|
|
64
|
+
exports.apiRoot_parse = apiRoot_parse;
|
|
63
65
|
exports.client_adminRequest = client_adminRequest;
|
|
64
66
|
exports.client_adminPluginUpload = client_adminPluginUpload;
|
|
65
67
|
exports.client_userFileUpload = client_userFileUpload;
|
|
66
68
|
exports.collectionItemFields_decode = collectionItemFields_decode;
|
|
67
69
|
exports.refusalReason_extract = refusalReason_extract;
|
|
68
70
|
exports.resource_isList = resource_isList;
|
|
71
|
+
exports.listResource_wrap = listResource_wrap;
|
|
69
72
|
exports.listData_get = listData_get;
|
|
70
73
|
exports.itemData_get = itemData_get;
|
|
71
74
|
exports.items_get = items_get;
|
|
72
75
|
exports.collectionItems_get = collectionItems_get;
|
|
76
|
+
exports.resource_invoke = resource_invoke;
|
|
73
77
|
exports.resource_call = resource_call;
|
|
74
78
|
exports.method_exists = method_exists;
|
|
75
79
|
exports.pacsRetrieve_deleteByUrl = pacsRetrieve_deleteByUrl;
|
|
80
|
+
exports.files_list = files_list;
|
|
81
|
+
exports.fileById_read = fileById_read;
|
|
76
82
|
exports.filebrowserFile_get = filebrowserFile_get;
|
|
83
|
+
const cubeRequest_js_1 = require("./cubeRequest.js");
|
|
77
84
|
const pace_js_1 = require("./pace.js");
|
|
85
|
+
const shape_js_1 = require("./shape.js");
|
|
78
86
|
const chrisapi_1 = __importStar(require("@fnndsc/chrisapi"));
|
|
79
87
|
exports.Client = chrisapi_1.default;
|
|
80
88
|
/**
|
|
@@ -83,11 +91,46 @@ exports.Client = chrisapi_1.default;
|
|
|
83
91
|
* @param authUrl - Full URL of the CUBE `auth-token/` endpoint.
|
|
84
92
|
* @param user - Username to authenticate as.
|
|
85
93
|
* @param password - Password for the user.
|
|
94
|
+
* @param root - The API root the endpoint belongs to, parsed; the endpoint
|
|
95
|
+
* itself when not given. The password goes only to its origin.
|
|
86
96
|
* @returns The bearer token string.
|
|
87
|
-
* @throws {
|
|
97
|
+
* @throws {CubeStatusError} When CUBE refuses the login (its status on the
|
|
98
|
+
* error; nothing CUBE answered is repeated).
|
|
99
|
+
* @throws {Error} When CUBE answers with a redirect (the password is never
|
|
100
|
+
* sent on: a 307 or 308 would resend it), gives no token, or the network
|
|
101
|
+
* fails (fetch's error, carrying its cause).
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* const token: string = await authToken_get('https://cube/api/v1/auth-token/', 'ada', 'pw');
|
|
106
|
+
* ```
|
|
88
107
|
*/
|
|
89
|
-
async function authToken_get(authUrl, user, password) {
|
|
90
|
-
|
|
108
|
+
async function authToken_get(authUrl, user, password, root = new URL(authUrl)) {
|
|
109
|
+
const answer = await (0, cubeRequest_js_1.cube_send)({
|
|
110
|
+
root,
|
|
111
|
+
address: authUrl,
|
|
112
|
+
method: 'POST',
|
|
113
|
+
headers: { 'Content-Type': 'application/json' },
|
|
114
|
+
body: JSON.stringify({ username: user, password }),
|
|
115
|
+
accept: 'application/json',
|
|
116
|
+
what: 'the login',
|
|
117
|
+
});
|
|
118
|
+
if (!answer.response.ok) {
|
|
119
|
+
// Nothing CUBE answered is repeated: a login's answer may echo what was sent.
|
|
120
|
+
await (0, cubeRequest_js_1.cubeBody_release)(answer.response);
|
|
121
|
+
throw new cubeRequest_js_1.CubeStatusError(`CUBE refused the login (HTTP ${String(answer.response.status)})`, answer.response.status);
|
|
122
|
+
}
|
|
123
|
+
const text = await (0, cubeRequest_js_1.cubeBody_text)(answer, 'the login');
|
|
124
|
+
let token = null;
|
|
125
|
+
try {
|
|
126
|
+
token = Reflect.get(JSON.parse(text), 'token');
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
token = null;
|
|
130
|
+
}
|
|
131
|
+
if (typeof token !== 'string' || token === '')
|
|
132
|
+
throw new Error('CUBE answered the login without a token');
|
|
133
|
+
return token;
|
|
91
134
|
}
|
|
92
135
|
/**
|
|
93
136
|
* Creates a chrisapi Client bound to a CUBE URL and bearer token.
|
|
@@ -147,11 +190,37 @@ function rootLinks_share(client) {
|
|
|
147
190
|
* @param url - Base URL of the CUBE API, ending in `/`.
|
|
148
191
|
* @param token - The token to prove.
|
|
149
192
|
* @param timeout - How long to wait for the answer.
|
|
150
|
-
*
|
|
151
|
-
*
|
|
193
|
+
* The check goes only to the API root's origin and follows no redirect.
|
|
194
|
+
*
|
|
195
|
+
* @throws {Error} Carrying `response` (with CUBE's status) when CUBE
|
|
196
|
+
* refused or redirected; "No server response!" when nothing came back in
|
|
197
|
+
* time; fetch's own error, its cause the network's code, when the
|
|
198
|
+
* connection failed.
|
|
152
199
|
*/
|
|
153
200
|
async function token_verify(url, token, timeout = exports.TOKEN_CHECK_TIMEOUT_MS) {
|
|
154
|
-
|
|
201
|
+
const what = 'the token check';
|
|
202
|
+
// Asked through the one guarded request, not chrisapi's, whose redirects keep the token on a subdomain.
|
|
203
|
+
const deadline = AbortSignal.timeout(timeout);
|
|
204
|
+
let answer;
|
|
205
|
+
try {
|
|
206
|
+
answer = await (0, cubeRequest_js_1.cube_send)({ root: apiRoot_parse(url), address: 'users/', token, headerWaitMs: 0, signal: deadline, what });
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
// Nothing came back: the words chrisapi's check said, which callers read as no answer, not a refusal.
|
|
210
|
+
if (deadline.aborted)
|
|
211
|
+
throw Object.assign(new Error('No server response!'), { cause: error });
|
|
212
|
+
// A network failure (fetch's TypeError) keeps its cause, the code a login's diagnosis reads.
|
|
213
|
+
// By name, not instanceof: fetch's TypeError may come from another realm.
|
|
214
|
+
if (error instanceof Error && error.name === 'TypeError')
|
|
215
|
+
throw error;
|
|
216
|
+
// A redirect is an answer, and not a yes: its status is kept, as a refusal's is.
|
|
217
|
+
const redirect = error instanceof Error ? Reflect.get(error, 'redirect') : undefined;
|
|
218
|
+
throw Object.assign(new Error(error instanceof Error ? error.message : String(error)), { response: typeof redirect === 'number' ? { status: redirect } : {} });
|
|
219
|
+
}
|
|
220
|
+
await (0, cubeRequest_js_1.cubeBody_release)(answer.response);
|
|
221
|
+
const status = answer.response.status;
|
|
222
|
+
if (status < 200 || status >= 300)
|
|
223
|
+
throw Object.assign(new Error(`CUBE refused ${what} (HTTP ${String(status)})`), { response: { status } });
|
|
155
224
|
}
|
|
156
225
|
/**
|
|
157
226
|
* Reads the runtime `auth` slice of a connected client.
|
|
@@ -163,22 +232,198 @@ function client_authGet(client) {
|
|
|
163
232
|
return client.auth;
|
|
164
233
|
}
|
|
165
234
|
/**
|
|
166
|
-
* Resolves the admin URL of a client,
|
|
235
|
+
* Resolves the admin URL of a client: the one it holds, or the `admin` link
|
|
236
|
+
* its API root advertises, read through the one guarded request (held to
|
|
237
|
+
* the root's origin, following no redirect) rather than chrisapi's root
|
|
238
|
+
* read, which follows redirects with the token. The link, held or learnt, is
|
|
239
|
+
* held to the root's origin here as well as when a request is sent there, so
|
|
240
|
+
* a blank or foreign link never passes this preflight.
|
|
167
241
|
*
|
|
168
242
|
* Non-admin users have no admin link; for them this resolves to null.
|
|
169
243
|
*
|
|
170
244
|
* @param client - A connected chrisapi Client.
|
|
171
245
|
* @returns The admin URL string, or null when the user has no admin access.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* const adminUrl: string | null = await client_adminUrlEnsure(client); // 'https://cube/chris-admin/api/v1/' for an admin
|
|
250
|
+
* ```
|
|
172
251
|
*/
|
|
173
252
|
async function client_adminUrlEnsure(client) {
|
|
253
|
+
const root = apiRoot_read(client);
|
|
254
|
+
if (root === null)
|
|
255
|
+
return null;
|
|
256
|
+
const link = await adminLink_learn(client, root);
|
|
257
|
+
return link === null ? null : adminLink_hold(link, root);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* The admin link of a client as CUBE gave it, not yet held: the one it
|
|
261
|
+
* holds, or the one its API root advertises, read through the guarded
|
|
262
|
+
* request. Only a link that passes {@link adminLink_hold} is kept.
|
|
263
|
+
*
|
|
264
|
+
* @param client - A connected chrisapi Client.
|
|
265
|
+
* @param root - The client's API root.
|
|
266
|
+
* @returns The link; null when the root names none or cannot be read.
|
|
267
|
+
*/
|
|
268
|
+
async function adminLink_learn(client, root) {
|
|
174
269
|
const slice = client;
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
await
|
|
270
|
+
// chrisapi starts a client with `adminUrl: ''`: not yet learnt, so the root is read; any other held value is CUBE's word.
|
|
271
|
+
if (typeof slice.adminUrl === 'string' && slice.adminUrl !== '')
|
|
272
|
+
return slice.adminUrl;
|
|
273
|
+
try {
|
|
274
|
+
const answer = await (0, cubeRequest_js_1.cube_sendOk)({
|
|
275
|
+
root,
|
|
276
|
+
address: '?limit=1',
|
|
277
|
+
token: client_authGet(client).token,
|
|
278
|
+
headerWaitMs: exports.FEED_LIST_TIMEOUT_MS,
|
|
279
|
+
what: 'the API root',
|
|
280
|
+
});
|
|
281
|
+
const document = JSON.parse(await (0, cubeRequest_js_1.cubeBody_text)(answer, 'the API root'));
|
|
282
|
+
const href = document.collection?.links?.find((link) => link.rel === 'admin')?.href;
|
|
283
|
+
if (typeof href !== 'string')
|
|
284
|
+
return null;
|
|
285
|
+
if (adminLink_hold(href, root) !== null)
|
|
286
|
+
slice.adminUrl = href;
|
|
287
|
+
return href;
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
// Deliberate absorption: a root that cannot be read and "no admin link"
|
|
291
|
+
// have the same meaning for this probe (no admin access), and callers
|
|
292
|
+
// surface the null with their own authorization message.
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* An admin link, when a credential may go there: nonblank, on the root's
|
|
298
|
+
* origin, naming no user.
|
|
299
|
+
*
|
|
300
|
+
* @param href - The admin link, held or advertised.
|
|
301
|
+
* @param root - The client's API root.
|
|
302
|
+
* @returns The link; null when it is blank or elsewhere.
|
|
303
|
+
*/
|
|
304
|
+
function adminLink_hold(href, root) {
|
|
305
|
+
try {
|
|
306
|
+
(0, cubeRequest_js_1.cubeAddress_hold)(href, root, 'the administrative request');
|
|
307
|
+
return href;
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* The API root a client was made with, as a URL: http or https, with no
|
|
315
|
+
* query or fragment.
|
|
316
|
+
*
|
|
317
|
+
* @param client - The client.
|
|
318
|
+
* @returns The root, ending in a slash; null when the client holds none usable.
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* apiRoot_read(client)?.origin; // 'https://cube'
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
function apiRoot_read(client) {
|
|
326
|
+
const raw = Reflect.get(client, 'url');
|
|
327
|
+
if (typeof raw !== 'string')
|
|
328
|
+
return null;
|
|
329
|
+
try {
|
|
330
|
+
return apiRoot_parse(raw);
|
|
331
|
+
}
|
|
332
|
+
catch {
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* A CUBE API root as given (`https://cube/api/v1`, with or without its
|
|
338
|
+
* trailing slash), as a URL: http or https, ending in a slash, with no
|
|
339
|
+
* query or fragment, so that an address resolved against it stays below it
|
|
340
|
+
* and on its host.
|
|
341
|
+
*
|
|
342
|
+
* @param raw - The API root as given.
|
|
343
|
+
* @returns The root.
|
|
344
|
+
* @throws {Error} When it is not an http or https address; the address itself is never named.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* new URL('auth-token/', apiRoot_parse('https://cube/api/v1')).href; // 'https://cube/api/v1/auth-token/'
|
|
349
|
+
* apiRoot_parse('https://cube/api/v1?x=1#f').href; // 'https://cube/api/v1/'
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
function apiRoot_parse(raw) {
|
|
353
|
+
let root = null;
|
|
354
|
+
try {
|
|
355
|
+
root = raw.trim() === '' ? null : new URL(raw);
|
|
356
|
+
}
|
|
357
|
+
catch {
|
|
358
|
+
root = null;
|
|
359
|
+
}
|
|
360
|
+
if (root === null || (root.protocol !== 'https:' && root.protocol !== 'http:'))
|
|
361
|
+
throw new Error('the CUBE address given is not an http or https address');
|
|
362
|
+
// The query and fragment go first, so the slash closes the path and not them (`…/v1?x` is `…/v1/`, not `…/v1?x/`).
|
|
363
|
+
root.search = '';
|
|
364
|
+
root.hash = '';
|
|
365
|
+
if (!root.pathname.endsWith('/'))
|
|
366
|
+
root.pathname = `${root.pathname}/`;
|
|
367
|
+
return root;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* The address of one administrative request: the administrative endpoint
|
|
371
|
+
* CUBE's root advertised, with the path below it, held to the origin of the
|
|
372
|
+
* API root the client was made with. The admin link is not trusted for more:
|
|
373
|
+
* CUBE's root names it, and the administrator's token goes with every
|
|
374
|
+
* request there.
|
|
375
|
+
*
|
|
376
|
+
* @param client - Connected CUBE client whose token authorizes the request.
|
|
377
|
+
* @param path - Relative path below the endpoint; empty for the endpoint itself.
|
|
378
|
+
* @returns The address.
|
|
379
|
+
* @throws {Error} When the client has no administrative endpoint, no usable
|
|
380
|
+
* API root, or the address is outside the API root's origin.
|
|
381
|
+
*/
|
|
382
|
+
async function adminAddress_build(client, path) {
|
|
383
|
+
const root = apiRoot_read(client);
|
|
384
|
+
if (root === null)
|
|
385
|
+
throw new Error("the client holds no API root of CUBE's own to hold the administrative address to; nothing was sent");
|
|
386
|
+
// Unheld here, so a blank or foreign link is refused below in words that say so.
|
|
387
|
+
const advertised = await adminLink_learn(client, root);
|
|
388
|
+
if (advertised === null)
|
|
389
|
+
throw new Error('Administrator privileges are required.');
|
|
390
|
+
try {
|
|
391
|
+
// The link as advertised (or as held from an earlier read) is held first, then the path below it.
|
|
392
|
+
const base = (0, cubeRequest_js_1.cubeAddress_hold)(advertised, root, 'the administrative request');
|
|
393
|
+
if (path === '')
|
|
394
|
+
return base;
|
|
395
|
+
const below = new URL(base.href);
|
|
396
|
+
if (!below.pathname.endsWith('/'))
|
|
397
|
+
below.pathname = `${below.pathname}/`;
|
|
398
|
+
return (0, cubeRequest_js_1.cubeAddress_hold)(path, below, 'the administrative request');
|
|
399
|
+
}
|
|
400
|
+
catch {
|
|
401
|
+
// The foreign address is never named: it may carry a user, a query or a fragment.
|
|
402
|
+
throw new Error(`CUBE advertised an administrative address outside its own origin (${root.origin}); the administrator's token goes only there, so nothing was sent`);
|
|
180
403
|
}
|
|
181
|
-
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* One administrative request, with the client's token, at an address held
|
|
407
|
+
* to CUBE's own origin, following no redirect, through the one guarded
|
|
408
|
+
* request (CUBE's answer must begin within its bounded wait).
|
|
409
|
+
*
|
|
410
|
+
* Sent directly rather than through chrisapi's Request: that follows
|
|
411
|
+
* redirects, and on a refusal whose body is not Collection+JSON (Django
|
|
412
|
+
* REST's field errors are plain JSON) chrisapi replaces CUBE's reason with
|
|
413
|
+
* "Bad server response!".
|
|
414
|
+
*
|
|
415
|
+
* @param client - Connected CUBE client whose token authorizes the request.
|
|
416
|
+
* @param path - Relative path below the administrative endpoint; empty for the endpoint itself.
|
|
417
|
+
* @param init - The method, the body and any headers beside the token and `Accept`.
|
|
418
|
+
* @returns The answer's status and body text, the answer not a redirect.
|
|
419
|
+
* @throws {Error} When the address is refused, or CUBE answers with a redirect.
|
|
420
|
+
*/
|
|
421
|
+
async function admin_send(client, path, init) {
|
|
422
|
+
const address = await adminAddress_build(client, path);
|
|
423
|
+
const what = 'the administrative request';
|
|
424
|
+
const answer = await (0, cubeRequest_js_1.cube_send)({ ...clientPlace_read(client, what), address, method: init.method, body: init.body, headers: init.headers, what });
|
|
425
|
+
const { response } = answer;
|
|
426
|
+
return { url: address.href, status: response.status, ok: response.ok, text: await (0, cubeRequest_js_1.cubeBody_text)(answer, what) };
|
|
182
427
|
}
|
|
183
428
|
/**
|
|
184
429
|
* Executes a collection+json request against an administrative CUBE child URL.
|
|
@@ -188,42 +433,32 @@ async function client_adminUrlEnsure(client) {
|
|
|
188
433
|
* @param method - Supported HTTP method.
|
|
189
434
|
* @param data - Optional Collection+JSON template fields for a POST request.
|
|
190
435
|
* @returns The decoded HTTP response document.
|
|
191
|
-
* @throws {Error} When the client has no
|
|
436
|
+
* @throws {Error} When the client has no administrative endpoint, the endpoint
|
|
437
|
+
* is outside the API root's origin, CUBE answers with a redirect, or refuses.
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```ts
|
|
441
|
+
* const created: unknown = await client_adminRequest(client, 'users/', 'post', { username: 'ada', email: 'ada@x', password: 'pw' });
|
|
442
|
+
* ```
|
|
192
443
|
*/
|
|
193
444
|
async function client_adminRequest(client, path, method, data) {
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
if (
|
|
200
|
-
|
|
201
|
-
const template = { template: { data: Object.entries(data ?? {}).map(([name, value]) => ({ name, value })) } };
|
|
202
|
-
// Posted directly rather than through chrisapi's Request: on a refusal
|
|
203
|
-
// whose body is not Collection+JSON (Django REST's field errors are plain
|
|
204
|
-
// JSON), chrisapi replaces the message with "Bad server response!" and
|
|
205
|
-
// overwrites the body with it, so CUBE's reason never reached anyone.
|
|
206
|
-
const response = await fetch(url, {
|
|
207
|
-
method: 'POST',
|
|
208
|
-
headers: {
|
|
209
|
-
Authorization: `Token ${client_authGet(client).token}`,
|
|
210
|
-
'Content-Type': 'application/vnd.collection+json',
|
|
211
|
-
Accept: 'application/vnd.collection+json',
|
|
212
|
-
},
|
|
213
|
-
body: JSON.stringify(template),
|
|
214
|
-
});
|
|
215
|
-
const text = await response.text();
|
|
216
|
-
if (!response.ok)
|
|
217
|
-
throw new Error(`CUBE refused POST ${url} (HTTP ${response.status}): ${refusalReason_extract(text)}`);
|
|
445
|
+
const fields = Object.entries(data ?? {}).map(([name, value]) => ({ name, value }));
|
|
446
|
+
const template = JSON.stringify({ template: { data: fields } });
|
|
447
|
+
const answer = method === 'get'
|
|
448
|
+
? await admin_send(client, path, { method: 'GET' })
|
|
449
|
+
: await admin_send(client, path, { method: 'POST', headers: { 'Content-Type': 'application/vnd.collection+json' }, body: template });
|
|
450
|
+
if (!answer.ok)
|
|
451
|
+
throw new Error(`CUBE refused ${method.toUpperCase()} ${answer.url} (HTTP ${answer.status}): ${refusalReason_extract(answer.text)}`);
|
|
218
452
|
try {
|
|
219
|
-
return JSON.parse(text);
|
|
453
|
+
return JSON.parse(answer.text);
|
|
220
454
|
}
|
|
221
455
|
catch {
|
|
222
|
-
throw new Error(`CUBE answered HTTP ${
|
|
456
|
+
throw new Error(`CUBE answered HTTP ${answer.status} with a body that is not JSON`);
|
|
223
457
|
}
|
|
224
458
|
}
|
|
225
459
|
/**
|
|
226
|
-
* Registers one plugin through CUBE's administrative upload
|
|
460
|
+
* Registers one plugin through CUBE's administrative upload, at the
|
|
461
|
+
* advertised endpoint held to the API root's origin, following no redirect.
|
|
227
462
|
*
|
|
228
463
|
* Posted as multipart directly rather than through chrisapi: on a refusal
|
|
229
464
|
* whose body is not Collection+JSON (Django REST's field errors are plain
|
|
@@ -234,12 +469,16 @@ async function client_adminRequest(client, path, method, data) {
|
|
|
234
469
|
* @param computeNames - Compute resources to register the plugin on.
|
|
235
470
|
* @param source - The descriptor text (field `fname`) or a store URL (field `plugin_store_url`).
|
|
236
471
|
* @returns The registered plugin's fields, decoded from the Collection+JSON item.
|
|
237
|
-
* @throws {Error} When the client has no administrative endpoint,
|
|
472
|
+
* @throws {Error} When the client has no administrative endpoint, the endpoint
|
|
473
|
+
* is outside the API root's origin, CUBE answers with a redirect, or CUBE
|
|
474
|
+
* refuses (its reason in the message).
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* ```ts
|
|
478
|
+
* const plugin: Record<string, unknown> = await client_adminPluginUpload(client, ['host'], { descriptor: json });
|
|
479
|
+
* ```
|
|
238
480
|
*/
|
|
239
481
|
async function client_adminPluginUpload(client, computeNames, source) {
|
|
240
|
-
const adminURL = await client_adminUrlEnsure(client);
|
|
241
|
-
if (!adminURL)
|
|
242
|
-
throw new Error('Administrator privileges are required.');
|
|
243
482
|
const form = new FormData();
|
|
244
483
|
form.append('compute_names', computeNames.join(','));
|
|
245
484
|
if ('descriptor' in source) {
|
|
@@ -248,22 +487,43 @@ async function client_adminPluginUpload(client, computeNames, source) {
|
|
|
248
487
|
else {
|
|
249
488
|
form.append('plugin_store_url', source.storeUrl);
|
|
250
489
|
}
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
490
|
+
const answer = await admin_send(client, '', { method: 'POST', body: form });
|
|
491
|
+
if (!answer.ok)
|
|
492
|
+
throw new Error(`CUBE refused the plugin (HTTP ${answer.status}): ${refusalReason_extract(answer.text)}`);
|
|
493
|
+
return collectionItemFields_decode(answer.text);
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* What a failed fetch's cause says when no connection was made: nothing of
|
|
497
|
+
* the request left. Only codes that arise before a connection exists; a lost
|
|
498
|
+
* route (`ENETUNREACH`, `EHOSTUNREACH`) can also end a socket whose request
|
|
499
|
+
* already arrived, so it counts as sent.
|
|
500
|
+
*/
|
|
501
|
+
const UNCONNECTED_CODES = new Set(['ECONNREFUSED', 'ENOTFOUND', 'EAI_AGAIN', 'UND_ERR_CONNECT_TIMEOUT']);
|
|
502
|
+
/**
|
|
503
|
+
* Whether a fetch failed before its request left: the connection was never
|
|
504
|
+
* made (refused, no such host, no route, no connection in time). Its cause
|
|
505
|
+
* says so by its code, or, for several addresses tried, every one's code.
|
|
506
|
+
*
|
|
507
|
+
* @param error - What fetch rejected with.
|
|
508
|
+
* @returns True only when no connection was made; false for anything else, which may have sent the request.
|
|
509
|
+
*/
|
|
510
|
+
function fetchUnsent_is(error) {
|
|
511
|
+
const cause = error !== null && typeof error === 'object' ? Reflect.get(error, 'cause') : undefined;
|
|
512
|
+
if (cause === null || typeof cause !== 'object')
|
|
513
|
+
return false;
|
|
514
|
+
const code = Reflect.get(cause, 'code');
|
|
515
|
+
if (typeof code === 'string')
|
|
516
|
+
return UNCONNECTED_CODES.has(code);
|
|
517
|
+
const tried = Reflect.get(cause, 'errors');
|
|
518
|
+
return Array.isArray(tried) && tried.length > 0 && tried.every((one) => {
|
|
519
|
+
const each = one !== null && typeof one === 'object' ? Reflect.get(one, 'code') : undefined;
|
|
520
|
+
return typeof each === 'string' && UNCONNECTED_CODES.has(each);
|
|
258
521
|
});
|
|
259
|
-
const text = await response.text();
|
|
260
|
-
if (!response.ok)
|
|
261
|
-
throw new Error(`CUBE refused the plugin (HTTP ${response.status}): ${refusalReason_extract(text)}`);
|
|
262
|
-
return collectionItemFields_decode(text);
|
|
263
522
|
}
|
|
264
523
|
/**
|
|
265
524
|
* Posts one file to CUBE's user files, the multipart form chrisapi's
|
|
266
|
-
* `uploadFile` sends, over fetch
|
|
525
|
+
* `uploadFile` sends, over fetch, at the address given and no other: no
|
|
526
|
+
* redirect is followed.
|
|
267
527
|
*
|
|
268
528
|
* fetch reads a form's file only as the connection takes its bytes, so a
|
|
269
529
|
* file whose reads are watched (cumin's `upload_watchStalls`) shows the
|
|
@@ -271,29 +531,39 @@ async function client_adminPluginUpload(client, computeNames, source) {
|
|
|
271
531
|
* it is sent, and holds a fixed 30 s deadline over the whole request.
|
|
272
532
|
*
|
|
273
533
|
* @param client - Connected CUBE client whose token authorizes the upload.
|
|
274
|
-
* @param
|
|
275
|
-
* @param
|
|
276
|
-
*
|
|
277
|
-
* abort's reason is what the returned promise rejects with.
|
|
534
|
+
* @param upload - The address, the path, the bytes, the signal, and the question asked before the post.
|
|
535
|
+
* @param sent - Told once the post has left, when given: CUBE answered, or
|
|
536
|
+
* the request failed after it went out (anything but a failure to connect).
|
|
278
537
|
* @returns The new file's fields (`id`, `fname`, …), decoded from the Collection+JSON item.
|
|
279
|
-
* @throws {Error} When
|
|
538
|
+
* @throws {Error} When no address was built, the connection changed, CUBE
|
|
539
|
+
* answers with a redirect or refuses (its reason in the message), or the signal's reason.
|
|
280
540
|
*/
|
|
281
|
-
async function client_userFileUpload(client,
|
|
282
|
-
if (
|
|
283
|
-
|
|
541
|
+
async function client_userFileUpload(client, upload, sent) {
|
|
542
|
+
if (upload.url === null)
|
|
543
|
+
throw new Error("no address of CUBE's own can be built for the upload; nothing was asked");
|
|
544
|
+
const what = 'the upload';
|
|
545
|
+
const place = clientPlace_read(client, what);
|
|
546
|
+
// Held before anything is asked, so a refused address is never told as sent.
|
|
547
|
+
const address = (0, cubeRequest_js_1.cubeAddress_hold)(upload.url, place.root, what);
|
|
284
548
|
const form = new FormData();
|
|
285
|
-
form.append('upload_path', uploadPath);
|
|
286
|
-
form.append('fname', file);
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
body: form,
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
549
|
+
form.append('upload_path', upload.uploadPath);
|
|
550
|
+
form.append('fname', upload.file);
|
|
551
|
+
if (upload.stillCurrent !== undefined && !await upload.stillCurrent()) {
|
|
552
|
+
throw new Error('the connection to CUBE changed after the place was proved; nothing was asked');
|
|
553
|
+
}
|
|
554
|
+
let answer;
|
|
555
|
+
try {
|
|
556
|
+
// No wait on the answer: CUBE answers once the whole file arrived, and its reads are watched for stalls.
|
|
557
|
+
answer = await (0, cubeRequest_js_1.cube_send)({ ...place, address, method: 'POST', body: form, headerWaitMs: 0, signal: upload.signal, what });
|
|
558
|
+
}
|
|
559
|
+
catch (error) {
|
|
560
|
+
if (!fetchUnsent_is(error))
|
|
561
|
+
sent?.();
|
|
562
|
+
throw error;
|
|
563
|
+
}
|
|
564
|
+
sent?.();
|
|
565
|
+
const { response } = answer;
|
|
566
|
+
const text = await (0, cubeRequest_js_1.cubeBody_text)(answer, what);
|
|
297
567
|
if (!response.ok)
|
|
298
568
|
throw new Error(`CUBE refused the upload (HTTP ${response.status}): ${refusalReason_extract(text)}`);
|
|
299
569
|
return collectionItemFields_decode(text);
|
|
@@ -368,59 +638,126 @@ function resource_isList(resource) {
|
|
|
368
638
|
return resource instanceof chrisapi_1.ListResource;
|
|
369
639
|
}
|
|
370
640
|
/**
|
|
371
|
-
*
|
|
641
|
+
* One page of a collection CUBE answered, held as the SDK holds a page it
|
|
642
|
+
* fetched itself, so a walk over the SDK's pages reads it unchanged.
|
|
643
|
+
*
|
|
644
|
+
* @param url - The address the page was asked at.
|
|
645
|
+
* @param client - The client it was asked with.
|
|
646
|
+
* @param collection - The answer's collection+json `collection`.
|
|
647
|
+
* @returns The page.
|
|
648
|
+
*
|
|
649
|
+
* @example
|
|
650
|
+
* ```ts
|
|
651
|
+
* const page: ListResource = listResource_wrap(url, client, { items: [], links: [] });
|
|
652
|
+
* ```
|
|
653
|
+
*/
|
|
654
|
+
function listResource_wrap(url, client, collection) {
|
|
655
|
+
const page = new chrisapi_1.ListResource(url, client_authGet(client));
|
|
656
|
+
Object.assign(page, { collection });
|
|
657
|
+
return page;
|
|
658
|
+
}
|
|
659
|
+
// What an answer must look like before it is used as its type: every reader below takes a shape, and those
|
|
660
|
+
// who read through this seam get the means to write one from it.
|
|
661
|
+
var shape_js_2 = require("./shape.js");
|
|
662
|
+
Object.defineProperty(exports, "rows_check", { enumerable: true, get: function () { return shape_js_2.rows_check; } });
|
|
663
|
+
Object.defineProperty(exports, "shape_build", { enumerable: true, get: function () { return shape_js_2.shape_build; } });
|
|
664
|
+
Object.defineProperty(exports, "shape_nullable", { enumerable: true, get: function () { return shape_js_2.shape_nullable; } });
|
|
665
|
+
Object.defineProperty(exports, "shape_object", { enumerable: true, get: function () { return shape_js_2.shape_object; } });
|
|
666
|
+
Object.defineProperty(exports, "ShapeError", { enumerable: true, get: function () { return shape_js_2.ShapeError; } });
|
|
667
|
+
/**
|
|
668
|
+
* Anything that may carry a `data` payload: held only to being an object,
|
|
669
|
+
* its `data` read through `listData_get` or `itemData_get` with a shape of
|
|
670
|
+
* its own.
|
|
671
|
+
*/
|
|
672
|
+
exports.DATA_CARRIER_SHAPE = (0, shape_js_1.shape_object)('a resource');
|
|
673
|
+
/**
|
|
674
|
+
* Reads the `data` of a list resource as rows of a shape.
|
|
675
|
+
*
|
|
676
|
+
* Absence and malformation are two answers. No `data` at all (or no list)
|
|
677
|
+
* is an empty list. `data` that is there and is not an array, or a row
|
|
678
|
+
* that is not what the shape says is read, is a `ShapeError`: a listing
|
|
679
|
+
* short by rows it does not mention is never returned.
|
|
372
680
|
*
|
|
373
681
|
* @param list - A chrisapi list resource (or anything carrying `data`).
|
|
374
|
-
* @
|
|
682
|
+
* @param shape - The shape of a row.
|
|
683
|
+
* @returns The rows; empty when there is no `data`.
|
|
684
|
+
* @throws {ShapeError} When `data` is present and is not a list of the shape.
|
|
375
685
|
*/
|
|
376
|
-
function listData_get(list) {
|
|
686
|
+
function listData_get(list, shape) {
|
|
377
687
|
const data = list?.data;
|
|
378
|
-
|
|
688
|
+
if (data === undefined || data === null)
|
|
689
|
+
return [];
|
|
690
|
+
return (0, shape_js_1.rows_check)(data, shape, 'data');
|
|
379
691
|
}
|
|
380
692
|
/**
|
|
381
|
-
*
|
|
693
|
+
* Reads the `data` of an item resource as a shape.
|
|
382
694
|
*
|
|
383
695
|
* @param item - A chrisapi item resource (or anything carrying `data`).
|
|
384
|
-
* @
|
|
696
|
+
* @param shape - The shape of the data.
|
|
697
|
+
* @returns The data; null when there is none.
|
|
698
|
+
* @throws {ShapeError} When `data` is present and is not what the shape says is read.
|
|
385
699
|
*/
|
|
386
|
-
function itemData_get(item) {
|
|
700
|
+
function itemData_get(item, shape) {
|
|
387
701
|
const data = item?.data;
|
|
388
|
-
return data === undefined || data === null ? null : data;
|
|
702
|
+
return data === undefined || data === null ? null : shape.value_check(data, 'data');
|
|
389
703
|
}
|
|
390
704
|
/**
|
|
391
|
-
*
|
|
705
|
+
* Reads the items of a list resource as a shape each.
|
|
392
706
|
*
|
|
393
|
-
* @param list - A chrisapi list resource exposing `getItems()
|
|
394
|
-
* @
|
|
707
|
+
* @param list - A chrisapi list resource exposing `getItems()`; null or undefined for none.
|
|
708
|
+
* @param shape - The shape of an item.
|
|
709
|
+
* @returns The items; empty when there is no list, or it has no items.
|
|
710
|
+
* @throws {ShapeError} When the list has no `getItems` to call, what it returns is not a list, or an item is not what the shape says is read.
|
|
395
711
|
*/
|
|
396
|
-
function items_get(list) {
|
|
397
|
-
|
|
398
|
-
|
|
712
|
+
function items_get(list, shape) {
|
|
713
|
+
if (list === undefined || list === null)
|
|
714
|
+
return [];
|
|
715
|
+
const read = list.getItems;
|
|
716
|
+
if (typeof read !== 'function')
|
|
717
|
+
throw new shape_js_1.ShapeError(`a list of ${shape.what}`, 'getItems', read === undefined ? 'missing' : `a ${typeof read}`, 'a function');
|
|
718
|
+
const items = read.call(list);
|
|
719
|
+
if (items === undefined || items === null)
|
|
720
|
+
return [];
|
|
721
|
+
return (0, shape_js_1.rows_check)(items, shape, 'items');
|
|
399
722
|
}
|
|
400
723
|
/**
|
|
401
|
-
*
|
|
724
|
+
* Reads the collection+json items embedded in a resource as a shape each.
|
|
402
725
|
*
|
|
403
726
|
* @param resource - A chrisapi resource carrying a `collection` document.
|
|
404
|
-
* @
|
|
727
|
+
* @param shape - The shape of an item.
|
|
728
|
+
* @returns The items; empty when there is no collection, or it has no items.
|
|
729
|
+
* @throws {ShapeError} When the collection is present and is not a document, or its items are present and are not a list of the shape.
|
|
405
730
|
*/
|
|
406
|
-
function collectionItems_get(resource) {
|
|
407
|
-
const
|
|
408
|
-
|
|
731
|
+
function collectionItems_get(resource, shape) {
|
|
732
|
+
const collection = resource?.collection;
|
|
733
|
+
if (collection === undefined || collection === null)
|
|
734
|
+
return [];
|
|
735
|
+
// A collection that is there and is not a document is malformed, not empty.
|
|
736
|
+
if (typeof collection !== 'object' || Array.isArray(collection))
|
|
737
|
+
throw new shape_js_1.ShapeError(`a list of ${shape.what}`, 'collection', Array.isArray(collection) ? 'an array' : `a ${typeof collection}`, 'an object');
|
|
738
|
+
const items = collection.items;
|
|
739
|
+
if (items === undefined || items === null)
|
|
740
|
+
return [];
|
|
741
|
+
return (0, shape_js_1.rows_check)(items, shape, 'collection.items');
|
|
409
742
|
}
|
|
410
743
|
/**
|
|
411
|
-
* Invokes a method on a chrisapi object that its typings do not declare
|
|
744
|
+
* Invokes a method on a chrisapi object that its typings do not declare,
|
|
745
|
+
* and hands back what it answered as it is: unknown.
|
|
412
746
|
*
|
|
413
747
|
* Used for client/resource methods that exist at runtime but are missing or
|
|
414
748
|
* mistyped in `@fnndsc/chrisapi` (e.g. `createWorkflow`,
|
|
415
|
-
* `getPipelineSourceFiles`, `_post`, `put` with a `path` body).
|
|
749
|
+
* `getPipelineSourceFiles`, `_post`, `put` with a `path` body). For a call
|
|
750
|
+
* whose answer is not read (a delete, a put), or is read only after checks
|
|
751
|
+
* of the caller's own. A caller that reads the answer as a type uses
|
|
752
|
+
* `resource_call`, which holds it to a shape.
|
|
416
753
|
*
|
|
417
754
|
* @param obj - The chrisapi object to dispatch on.
|
|
418
755
|
* @param methodName - Name of the runtime method to invoke.
|
|
419
756
|
* @param args - Arguments forwarded to the method.
|
|
420
|
-
* @returns The method's resolved value
|
|
757
|
+
* @returns The method's resolved value, unchecked and untyped.
|
|
421
758
|
* @throws {Error} When the named method does not exist on the object.
|
|
422
759
|
*/
|
|
423
|
-
async function
|
|
760
|
+
async function resource_invoke(obj, methodName, ...args) {
|
|
424
761
|
const method = obj[methodName];
|
|
425
762
|
if (typeof method !== 'function') {
|
|
426
763
|
throw new Error(`chrisapi object has no method '${methodName}'`);
|
|
@@ -435,6 +772,23 @@ async function resource_call(obj, methodName, ...args) {
|
|
|
435
772
|
(0, pace_js_1.pace_note)(Math.round(performance.now() - startedAt));
|
|
436
773
|
return result;
|
|
437
774
|
}
|
|
775
|
+
/**
|
|
776
|
+
* Invokes a method on a chrisapi object and holds what it answers to a shape.
|
|
777
|
+
*
|
|
778
|
+
* @param obj - The chrisapi object to dispatch on.
|
|
779
|
+
* @param shape - The shape of the answer.
|
|
780
|
+
* @param methodName - Name of the runtime method to invoke.
|
|
781
|
+
* @param args - Arguments forwarded to the method.
|
|
782
|
+
* @returns The answer, as the shape's type.
|
|
783
|
+
* @throws {Error} When the named method does not exist on the object.
|
|
784
|
+
* @throws {ShapeError} When the answer is not what the shape says is read.
|
|
785
|
+
*
|
|
786
|
+
* @example
|
|
787
|
+
* const logs: DataCarrier = await resource_call(resource, DATA_CARRIER_SHAPE, 'getLogs');
|
|
788
|
+
*/
|
|
789
|
+
async function resource_call(obj, shape, methodName, ...args) {
|
|
790
|
+
return shape.value_check(await resource_invoke(obj, methodName, ...args));
|
|
791
|
+
}
|
|
438
792
|
/**
|
|
439
793
|
* Tests whether a chrisapi object exposes a named runtime method.
|
|
440
794
|
*
|
|
@@ -461,6 +815,115 @@ async function pacsRetrieve_deleteByUrl(retrieveUrl, auth) {
|
|
|
461
815
|
const retrieve = new chrisapi_1.PACSRetrieve(retrieveUrl, auth);
|
|
462
816
|
await retrieve.delete();
|
|
463
817
|
}
|
|
818
|
+
/**
|
|
819
|
+
* The address an item names for its file's bytes, as given: unchecked, and
|
|
820
|
+
* checked only when the bytes are asked for, so the file's fields (its size,
|
|
821
|
+
* its name) are read even when its bytes cannot be.
|
|
822
|
+
*
|
|
823
|
+
* @param item - The item as CUBE listed it.
|
|
824
|
+
* @returns The `file_resource` link, or null when none is given as text.
|
|
825
|
+
*/
|
|
826
|
+
function fileResourceHref_read(item) {
|
|
827
|
+
const href = item?.links?.find((link) => link.rel === 'file_resource')?.href;
|
|
828
|
+
return typeof href === 'string' ? href : null;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* A file's fields, and its bytes read through the one guarded request: the
|
|
832
|
+
* address CUBE named is held to the API root's origin (nothing blank, no
|
|
833
|
+
* user) when the bytes are asked for, no redirect is followed, the wait for
|
|
834
|
+
* CUBE's answer to begin is `timeout` (0 for none), and once the bytes flow
|
|
835
|
+
* only silence ends them.
|
|
836
|
+
*
|
|
837
|
+
* @param data - The file's fields.
|
|
838
|
+
* @param place - The root, the token and the address named.
|
|
839
|
+
* @returns The file.
|
|
840
|
+
*/
|
|
841
|
+
function fileBytes_guard(data, place) {
|
|
842
|
+
const what = "the file's bytes";
|
|
843
|
+
const bytes_ask = (timeout) => (0, cubeRequest_js_1.cube_sendOk)({ root: place.root, address: place.href, token: place.token, accept: 'application/octet-stream', headerWaitMs: timeout, what });
|
|
844
|
+
return {
|
|
845
|
+
data,
|
|
846
|
+
getFileStream: async (timeout = cubeRequest_js_1.HEADER_WAIT_MS) => {
|
|
847
|
+
const answer = await bytes_ask(timeout);
|
|
848
|
+
const length = answer.response.headers.get('content-length');
|
|
849
|
+
// A Node stream, as chrisapi's own downloads give: the reader pipes it and watches it for stalls,
|
|
850
|
+
// and destroying it cancels the body, which abandons the request.
|
|
851
|
+
const stream = (0, cubeRequest_js_1.cubeBody_readable)(answer);
|
|
852
|
+
return { data: stream, headers: length === null ? {} : { 'content-length': length } };
|
|
853
|
+
},
|
|
854
|
+
getFileBlob: async (timeout = cubeRequest_js_1.HEADER_WAIT_MS) => (0, cubeRequest_js_1.cubeBody_bytes)(await bytes_ask(timeout), what),
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* The client's API root and token, which every guarded request needs.
|
|
859
|
+
*
|
|
860
|
+
* @param client - The connected client.
|
|
861
|
+
* @param what - The request, in an operator's words.
|
|
862
|
+
* @returns The root and the token.
|
|
863
|
+
* @throws {Error} When the client holds no usable API root.
|
|
864
|
+
*/
|
|
865
|
+
function clientPlace_read(client, what) {
|
|
866
|
+
const root = apiRoot_read(client);
|
|
867
|
+
if (root === null)
|
|
868
|
+
throw new Error(`the client holds no API root of CUBE's own to ask for ${what}; nothing was sent`);
|
|
869
|
+
return { root, token: client_authGet(client).token };
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* The fields of a file item as CUBE lists it.
|
|
873
|
+
*
|
|
874
|
+
* @param item - The item.
|
|
875
|
+
* @returns Its fields by name.
|
|
876
|
+
*/
|
|
877
|
+
function fileItemData_read(item) {
|
|
878
|
+
return Object.fromEntries((item.data ?? []).map((field) => [field.name, field.value]));
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* The files CUBE lists at an address built from the client's API root (a
|
|
882
|
+
* user file, a PACS file, a pipeline's source file), asked through the one
|
|
883
|
+
* guarded request rather than chrisapi's lookups, which ask the collection
|
|
884
|
+
* CUBE's root advertises, wherever it points, following redirects. Each
|
|
885
|
+
* file's bytes are read at its `file_resource` link only once that is held
|
|
886
|
+
* to the root's origin.
|
|
887
|
+
*
|
|
888
|
+
* @param client - The connected client.
|
|
889
|
+
* @param address - The listing, relative to the API root (`userfiles/41/`).
|
|
890
|
+
* @param what - The listing, in an operator's words.
|
|
891
|
+
* @returns The files listed, in order.
|
|
892
|
+
* @throws {CubeStatusError} When CUBE refuses (the status on the error).
|
|
893
|
+
* @throws {Error} When CUBE answers with a redirect, or the client holds no usable API root.
|
|
894
|
+
*
|
|
895
|
+
* @example
|
|
896
|
+
* ```ts
|
|
897
|
+
* const files: FileBytesSource[] = await files_list(client, 'pacs/files/41/', 'the PACS file');
|
|
898
|
+
* ```
|
|
899
|
+
*/
|
|
900
|
+
async function files_list(client, address, what) {
|
|
901
|
+
const place = clientPlace_read(client, what);
|
|
902
|
+
const listing = await (0, cubeRequest_js_1.cube_sendOk)({ ...place, address, what });
|
|
903
|
+
const document = JSON.parse(await (0, cubeRequest_js_1.cubeBody_text)(listing, what));
|
|
904
|
+
return (document.collection?.items ?? []).map((item) => fileBytes_guard(fileItemData_read(item), { ...place, href: fileResourceHref_read(item) }));
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* One file of a collection of CUBE's, by id, through {@link files_list}:
|
|
908
|
+
* the file's own resource (`userfiles/41/`, `pacs/files/41/`), below the
|
|
909
|
+
* API root.
|
|
910
|
+
*
|
|
911
|
+
* @param client - The connected client.
|
|
912
|
+
* @param collection - The collection's path below the API root (`userfiles`, `pacs/files`).
|
|
913
|
+
* @param fileId - The file's id.
|
|
914
|
+
* @returns The file, or null when CUBE answers with some other file.
|
|
915
|
+
* @throws {CubeStatusError} When CUBE refuses (the status on the error).
|
|
916
|
+
* @throws {Error} When CUBE answers with a redirect, or the client holds no usable API root.
|
|
917
|
+
*
|
|
918
|
+
* @example
|
|
919
|
+
* ```ts
|
|
920
|
+
* const file: FileBytesSource | null = await fileById_read(client, 'userfiles', 41);
|
|
921
|
+
* ```
|
|
922
|
+
*/
|
|
923
|
+
async function fileById_read(client, collection, fileId) {
|
|
924
|
+
const files = await files_list(client, `${collection}/${String(fileId)}/`, `file ${String(fileId)}`);
|
|
925
|
+
return files.find((file) => file.data['id'] === fileId) ?? null;
|
|
926
|
+
}
|
|
464
927
|
/**
|
|
465
928
|
* A file through the file browser (`/filebrowser/files/<id>/`), the
|
|
466
929
|
* resource CUBE serves a feed's files under to everyone the feed is
|
|
@@ -468,25 +931,23 @@ async function pacsRetrieve_deleteByUrl(retrieveUrl, auth) {
|
|
|
468
931
|
* but its owner, so a colleague's shared output could be listed and not
|
|
469
932
|
* downloaded (ekanite, 2026-10-14: file 30916964, 200 here, 403 there).
|
|
470
933
|
*
|
|
934
|
+
* Every request goes through the one guarded request: the listing's
|
|
935
|
+
* address is built from the API root, and the bytes' address the listing
|
|
936
|
+
* names is held to the root's origin when the bytes are asked for.
|
|
937
|
+
*
|
|
471
938
|
* @param client - The connected client.
|
|
472
939
|
* @param fileId - The file's id.
|
|
473
940
|
* @returns The file, or null when the file browser does not serve it.
|
|
474
|
-
* @throws {Error} When CUBE refuses
|
|
941
|
+
* @throws {Error} When CUBE refuses (the status on the error), or answers with a redirect.
|
|
942
|
+
*
|
|
943
|
+
* @example
|
|
944
|
+
* ```ts
|
|
945
|
+
* const file = await filebrowserFile_get(client, 41);
|
|
946
|
+
* const bytes = await file?.getFileBlob();
|
|
947
|
+
* ```
|
|
475
948
|
*/
|
|
476
949
|
async function filebrowserFile_get(client, fileId) {
|
|
477
|
-
const
|
|
478
|
-
|
|
479
|
-
const listing = new chrisapi_1.Request(auth, 'application/vnd.collection+json');
|
|
480
|
-
const document = (await listing.get(`${base}filebrowser/files/${fileId}/`)).data;
|
|
481
|
-
const item = document.collection?.items?.[0];
|
|
482
|
-
const href = item?.links?.find((link) => link.rel === 'file_resource')?.href;
|
|
483
|
-
if (item === undefined || href === undefined)
|
|
484
|
-
return null;
|
|
485
|
-
const data = Object.fromEntries((item.data ?? []).map((field) => [field.name, field.value]));
|
|
486
|
-
return {
|
|
487
|
-
data,
|
|
488
|
-
getFileStream: (timeout = 30000) => new chrisapi_1.Request(auth, 'application/octet-stream', timeout).getStream(href),
|
|
489
|
-
getFileBlob: async (timeout = 30000) => (await new chrisapi_1.Request(auth, 'application/octet-stream', timeout).get(href)).data,
|
|
490
|
-
};
|
|
950
|
+
const files = await files_list(client, `filebrowser/files/${String(fileId)}/`, "the file's listing");
|
|
951
|
+
return files[0] ?? null;
|
|
491
952
|
}
|
|
492
953
|
//# sourceMappingURL=adapter.js.map
|