@venizia/ardor-kernel 0.4.8-9 → 0.5.0-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/README.md +4 -4
- package/dist/base/applications/abstract.d.ts +41 -14
- package/dist/base/applications/abstract.js +142 -34
- package/dist/base/applications/app-info.d.ts +8 -0
- package/dist/base/applications/app-info.js +38 -0
- package/dist/base/providers/index.d.ts +1 -0
- package/dist/base/providers/index.js +1 -0
- package/dist/base/providers/role-permission.d.ts +59 -0
- package/dist/base/providers/role-permission.js +118 -0
- package/dist/base/services/auth.d.ts +54 -6
- package/dist/base/services/auth.js +167 -17
- package/dist/base/services/network-request.d.ts +62 -14
- package/dist/base/services/network-request.js +243 -50
- package/dist/common/keys.d.ts +60 -20
- package/dist/common/keys.js +61 -19
- package/dist/common/types.d.ts +88 -38
- package/dist/helpers/auth-token-store.d.ts +6 -2
- package/dist/helpers/auth-token-store.js +6 -3
- package/dist/helpers/networks/auth-recovery.d.ts +10 -3
- package/dist/helpers/networks/auth-recovery.js +17 -6
- package/dist/helpers/networks/index.d.ts +1 -1
- package/dist/helpers/networks/index.js +1 -1
- package/dist/helpers/networks/{response-reader.d.ts → network-response-reader.d.ts} +4 -2
- package/dist/helpers/networks/{response-reader.js → network-response-reader.js} +28 -13
- package/dist/utilities/runtime-config.d.ts +40 -14
- package/dist/utilities/runtime-config.js +45 -8
- package/package.json +13 -13
|
@@ -1,18 +1,33 @@
|
|
|
1
1
|
import { getError } from '@venizia/ignis-inversion';
|
|
2
|
-
import {
|
|
2
|
+
import { HTTP } from '@venizia/ignis-helpers/common';
|
|
3
|
+
import { NativeFetchNetworkRequest } from '@venizia/ignis-helpers/core';
|
|
3
4
|
import { UuidV4Generator } from '@venizia/ignis-helpers/uuid';
|
|
4
5
|
import { HttpExtraRequest } from '@venizia/ignis-kernel/repository';
|
|
5
6
|
import { App, HeaderConstants, LocalStorageKeys, RequestChannel, RequestCountData, RequestMethods, RequestTypes, } from '../../common/index.js';
|
|
6
|
-
import { AuthRecovery, AuthTokenStore, NetworkHeaders, RequestBodyEncoder,
|
|
7
|
+
import { AuthRecovery, AuthTokenStore, NetworkHeaders, RequestBodyEncoder, NetworkResponseReader, } from '../../helpers/index.js';
|
|
7
8
|
import { BaseService } from './base.js';
|
|
9
|
+
/** The `normalized.code` of the 401 that answers a request whose session is gone: the user who sent it signed out or another user signed in while it was in flight. */
|
|
10
|
+
export const SESSION_CHANGED_CODE = 'ardor.network.session_changed';
|
|
11
|
+
/** The `normalized.code` of the 504 a request rejects with when it outlives the configured `timeout`. */
|
|
12
|
+
export const NETWORK_TIMEOUT_CODE = 'ardor.network.timeout';
|
|
8
13
|
/**
|
|
14
|
+
* Sends the requests of the data provider with its own sender. The repositories of
|
|
15
|
+
* `@venizia/ardor/repository` send through IGNIS's `HttpDataSource`, from the optional peer
|
|
16
|
+
* `@venizia/ignis-connectors`, which this service cannot import without making that peer required:
|
|
17
|
+
* until IGNIS's transport core is in a required peer, two clients exist, and `getDataSourceAuth`
|
|
18
|
+
* gives the second the session of the first.
|
|
19
|
+
*
|
|
9
20
|
* The options hold data (the base URL, headers, the no-auth lists, the refresh path); behaviour is
|
|
10
21
|
* in the protected hooks, each taking one `opts` object, which a subclass overrides.
|
|
11
22
|
*/
|
|
12
23
|
export class DefaultNetworkRequestService extends BaseService {
|
|
13
24
|
constructor(opts) {
|
|
14
25
|
super({ scope: DefaultNetworkRequestService.name });
|
|
15
|
-
|
|
26
|
+
/** The session each data source request was sent under, keyed by the context object IGNIS hands to both hooks. */
|
|
27
|
+
this.sentSessions = new WeakMap();
|
|
28
|
+
const { name, baseUrl = '', headers = {}, noAuthPaths, noAuthPathRegex, authRecovery, authService, timeout = { use: false }, } = opts;
|
|
29
|
+
DefaultNetworkRequestService.assertTimeout({ timeout });
|
|
30
|
+
this.timeout = timeout;
|
|
16
31
|
this.headers = NetworkHeaders.toRecord({ headers });
|
|
17
32
|
this.noAuthPaths = noAuthPaths;
|
|
18
33
|
this.noAuthPathRegexes = this.compileNoAuthPathRegex({ input: noAuthPathRegex });
|
|
@@ -26,12 +41,38 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
26
41
|
onFailure: () => this.onAuthFailure(),
|
|
27
42
|
isNoAuthPath: (paths) => this.isNoAuthPath({ resource: paths?.[0], paths }),
|
|
28
43
|
});
|
|
29
|
-
this.networkRequest = new
|
|
44
|
+
this.networkRequest = new NativeFetchNetworkRequest({
|
|
30
45
|
name,
|
|
31
46
|
// A filter travels as one JSON parameter, and a null is left out: what an IGNIS server reads.
|
|
32
|
-
networkOptions: { baseUrl,
|
|
47
|
+
networkOptions: { baseUrl, queryString: { nullish: 'skip', nested: 'json' } },
|
|
33
48
|
});
|
|
34
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* The headers of one call: its own alone, or the ones `setHeaders` holds now. The transport keeps no
|
|
52
|
+
* default of ours, so a removed header is gone; a call that names no `content-type` sends none (`null`)
|
|
53
|
+
* when its body is one `fetch` types itself (`FormData`, `Blob`, bytes, a stream), so a multipart boundary is written.
|
|
54
|
+
*/
|
|
55
|
+
toSendHeaders(opts) {
|
|
56
|
+
const sent = Object.fromEntries(new Headers(opts.headers ?? this.headers));
|
|
57
|
+
if (opts.headers && !(HeaderConstants.CONTENT_TYPE in sent)) {
|
|
58
|
+
sent[HeaderConstants.CONTENT_TYPE] = null;
|
|
59
|
+
}
|
|
60
|
+
if (DefaultNetworkRequestService.isRawBody({ body: opts.body }) &&
|
|
61
|
+
!sent[HeaderConstants.CONTENT_TYPE]) {
|
|
62
|
+
sent[HeaderConstants.CONTENT_TYPE] = null;
|
|
63
|
+
}
|
|
64
|
+
return sent;
|
|
65
|
+
}
|
|
66
|
+
/** A body the platform `fetch` gives a content type of its own, or none: it is sent as it is. */
|
|
67
|
+
static isRawBody(opts) {
|
|
68
|
+
const { body } = opts;
|
|
69
|
+
return (body instanceof FormData ||
|
|
70
|
+
body instanceof Blob ||
|
|
71
|
+
body instanceof URLSearchParams ||
|
|
72
|
+
body instanceof ArrayBuffer ||
|
|
73
|
+
ArrayBuffer.isView(body) ||
|
|
74
|
+
body instanceof ReadableStream);
|
|
75
|
+
}
|
|
35
76
|
/**
|
|
36
77
|
* The token under the unscoped session key (`AuthTokenStore.read`), `undefined` outside a browser, for an
|
|
37
78
|
* application with no `id`. With an application `id` the session is under other keys: use
|
|
@@ -54,6 +95,19 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
54
95
|
const { token: key } = LocalStorageKeys.scoped({ id: opts.id });
|
|
55
96
|
return () => AuthTokenStore.read({ key });
|
|
56
97
|
}
|
|
98
|
+
/** Throws unless a timeout that is on has a positive integer `milliseconds`. */
|
|
99
|
+
static assertTimeout(opts) {
|
|
100
|
+
const { timeout } = opts;
|
|
101
|
+
if (!timeout?.use) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const { milliseconds } = timeout;
|
|
105
|
+
if (!Number.isSafeInteger(milliseconds) || milliseconds <= 0) {
|
|
106
|
+
throw getError({
|
|
107
|
+
message: `[DefaultNetworkRequestService] timeout.milliseconds must be a positive integer, got ${milliseconds}`,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
57
111
|
// --- Hooks -----------------------------------------------------------------------------------
|
|
58
112
|
/**
|
|
59
113
|
* The token this service sends. The one way to supply a token: override it to read another
|
|
@@ -217,7 +271,79 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
217
271
|
* Also what `DefaultAuthProvider.refreshToken` calls, so a refresh is made in one place.
|
|
218
272
|
*/
|
|
219
273
|
refresh() {
|
|
220
|
-
|
|
274
|
+
const { userId, token } = this.readSession();
|
|
275
|
+
return this.recovery.refresh({ key: `${userId}|${token}` });
|
|
276
|
+
}
|
|
277
|
+
/** The session signed in now: who, and with which token. */
|
|
278
|
+
readSession() {
|
|
279
|
+
const userId = this.authService?.getUser().userId;
|
|
280
|
+
return {
|
|
281
|
+
userId: userId === undefined ? undefined : `${userId}`,
|
|
282
|
+
token: this.resolveAuthToken()?.value,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/** The 401 that answers a request whose session is gone: `DefaultAuthProvider.checkError` leaves the signed-in session alone for it. */
|
|
286
|
+
sessionChangedError() {
|
|
287
|
+
return getError({
|
|
288
|
+
statusCode: 401,
|
|
289
|
+
messageCode: SESSION_CHANGED_CODE,
|
|
290
|
+
message: '[doRequest] The session that sent this request ended while it was in flight',
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* How the session changed since `sent`: `undefined` when it did not, `true` when the same user holds
|
|
295
|
+
* another token now (the request can be sent again with it), `false` when the user who sent is gone. When
|
|
296
|
+
* nobody can tell who sent, another token is a switch: it is never read as the same user's.
|
|
297
|
+
*/
|
|
298
|
+
readSessionChange(opts) {
|
|
299
|
+
const { sent } = opts;
|
|
300
|
+
const now = this.readSession();
|
|
301
|
+
if (now.userId === sent.userId && now.token === sent.token) {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
return sent.userId !== undefined && now.userId === sent.userId && !!now.token;
|
|
305
|
+
}
|
|
306
|
+
/** `task` settles as it does, or the wait rejects with the reason of `signal` once it aborts; the task itself is not cancelled. */
|
|
307
|
+
static async untilAborted(opts) {
|
|
308
|
+
const { task, signal } = opts;
|
|
309
|
+
if (!signal) {
|
|
310
|
+
return task;
|
|
311
|
+
}
|
|
312
|
+
let onAbort;
|
|
313
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
314
|
+
onAbort = () => reject(signal.reason);
|
|
315
|
+
if (signal.aborted) {
|
|
316
|
+
onAbort();
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
320
|
+
});
|
|
321
|
+
try {
|
|
322
|
+
const settled = await Promise.race([task, aborted]);
|
|
323
|
+
return settled;
|
|
324
|
+
}
|
|
325
|
+
finally {
|
|
326
|
+
if (onAbort) {
|
|
327
|
+
signal.removeEventListener('abort', onAbort);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Whether the 401 of a request that `sent` is a session change for the caller: the user who sent is gone and
|
|
333
|
+
* either the request was sent again since (`isRetried`) or another session is signed in now. A refresh that
|
|
334
|
+
* failed and ended the session itself leaves nobody to protect, and its 401 stands.
|
|
335
|
+
*/
|
|
336
|
+
isSessionTaken(opts) {
|
|
337
|
+
// With no user to compare, the token that follows a refresh this service ran is that refresh's.
|
|
338
|
+
if (opts.sent.userId === undefined && opts.isRetried) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
// A user switch in progress (a token stored for another user than the identity) is someone signing in: it
|
|
342
|
+
// reads no token, but it is not a sign-out.
|
|
343
|
+
return (this.readSessionChange({ sent: opts.sent }) === false &&
|
|
344
|
+
(opts.isRetried ||
|
|
345
|
+
this.readSession().token !== undefined ||
|
|
346
|
+
this.authService?.isSwitching() === true));
|
|
221
347
|
}
|
|
222
348
|
// --- Requests --------------------------------------------------------------------------------
|
|
223
349
|
/**
|
|
@@ -227,20 +353,52 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
227
353
|
*/
|
|
228
354
|
getDataSourceAuth(opts) {
|
|
229
355
|
return {
|
|
230
|
-
authTokenResolver: (
|
|
231
|
-
|
|
356
|
+
authTokenResolver: (context) => {
|
|
357
|
+
const { paths } = context;
|
|
358
|
+
const sent = this.sentSessions.get(context);
|
|
359
|
+
// The retry of a request whose user has gone must not carry the token of whoever signed in since.
|
|
360
|
+
if (sent && this.readSessionChange({ sent }) === false) {
|
|
361
|
+
return undefined;
|
|
362
|
+
}
|
|
363
|
+
const token = this.isNoAuthPath({ resource: paths[0], paths })
|
|
232
364
|
? undefined
|
|
233
365
|
: this.resolveAuthToken();
|
|
366
|
+
// Both hooks receive this very object, and a retry sends again under the session of that moment.
|
|
367
|
+
this.sentSessions.set(context, { userId: this.readSession().userId, token: token?.value });
|
|
368
|
+
return token;
|
|
234
369
|
},
|
|
235
370
|
headersResolver: () => {
|
|
236
371
|
return NetworkHeaders.merge({
|
|
237
372
|
sources: [this.getSessionHeaders(), opts ? this.getTracingHeaders(opts) : {}],
|
|
238
373
|
});
|
|
239
374
|
},
|
|
240
|
-
onUnauthorized: (
|
|
241
|
-
|
|
375
|
+
onUnauthorized: (context, info) => {
|
|
376
|
+
if (!this.recovery.canRecover({ paths: context.paths })) {
|
|
377
|
+
return Promise.resolve(false);
|
|
378
|
+
}
|
|
379
|
+
const sent = this.sentSessions.get(context);
|
|
380
|
+
const change = sent ? this.readSessionChange({ sent }) : undefined;
|
|
381
|
+
if (change === false) {
|
|
382
|
+
return Promise.reject(this.sessionChangedError());
|
|
383
|
+
}
|
|
384
|
+
// The retry's own 401 stands, as it does for `doRequest`: one refresh, one retry.
|
|
385
|
+
if (info.attempt >= 2) {
|
|
386
|
+
return Promise.resolve(false);
|
|
387
|
+
}
|
|
388
|
+
if (change === true) {
|
|
389
|
+
return Promise.resolve(true);
|
|
390
|
+
}
|
|
391
|
+
return this.refresh().then((isRefreshed) => {
|
|
392
|
+
// The retry sends again under the session of this refresh.
|
|
393
|
+
if (sent && sent.userId === undefined && isRefreshed) {
|
|
394
|
+
this.sentSessions.set(context, this.readSession());
|
|
395
|
+
}
|
|
396
|
+
return sent && this.isSessionTaken({ sent, isRetried: isRefreshed })
|
|
397
|
+
? Promise.reject(this.sessionChangedError())
|
|
398
|
+
: isRefreshed;
|
|
399
|
+
});
|
|
242
400
|
},
|
|
243
|
-
errorRootKey:
|
|
401
|
+
errorRootKey: NetworkResponseReader.ERROR_ROOT_KEY,
|
|
244
402
|
};
|
|
245
403
|
}
|
|
246
404
|
/**
|
|
@@ -315,30 +473,6 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
315
473
|
[HeaderConstants.AUTHORIZATION]: authHeader.token,
|
|
316
474
|
};
|
|
317
475
|
}
|
|
318
|
-
/**
|
|
319
|
-
* Whether the request stays where `baseUrl` points: no entry of `paths` carries a tab, a line break,
|
|
320
|
-
* a backslash or a dot segment (also written `%2E`), and the URL built from
|
|
321
|
-
* them, resolved the way a browser does, has the origin of the base and a path under it.
|
|
322
|
-
*/
|
|
323
|
-
static isUnderBase(opts) {
|
|
324
|
-
const { baseUrl, paths, url } = opts;
|
|
325
|
-
// The joined path, so a dot segment is found whichever entry holds it.
|
|
326
|
-
if (/[\\\t\n\r]|(^|\/)(\.|%2e){1,2}(\/|$)/i.test(paths.join('/'))) {
|
|
327
|
-
return false;
|
|
328
|
-
}
|
|
329
|
-
try {
|
|
330
|
-
const origin = 'http://base.invalid';
|
|
331
|
-
const base = new URL(baseUrl, origin);
|
|
332
|
-
const target = new URL(url, origin);
|
|
333
|
-
const prefix = base.pathname.replace(/\/+$/, '');
|
|
334
|
-
return (target.origin === base.origin &&
|
|
335
|
-
(target.pathname === prefix || target.pathname.startsWith(`${prefix}/`)));
|
|
336
|
-
// eslint-disable-next-line venizia/no-silent-catch -- a URL that does not parse is the answer: the request is refused
|
|
337
|
-
}
|
|
338
|
-
catch {
|
|
339
|
-
return false;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
476
|
/** The body as it is sent and the content type to send it under. Override to encode another way. */
|
|
343
477
|
encodeBody(opts) {
|
|
344
478
|
return RequestBodyEncoder.encode(opts);
|
|
@@ -368,43 +502,90 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
368
502
|
};
|
|
369
503
|
return props;
|
|
370
504
|
}
|
|
371
|
-
/** Reads a response by the request type. See `
|
|
505
|
+
/** Reads a response by the request type. See `NetworkResponseReader.convert`. */
|
|
372
506
|
convertResponse(opts) {
|
|
373
|
-
return
|
|
507
|
+
return NetworkResponseReader.convert(opts);
|
|
374
508
|
}
|
|
375
509
|
/**
|
|
376
510
|
* Sends the request and reads the response by `type`. Throws when `baseUrl` (the option, else the
|
|
377
511
|
* service's) is empty. A 401 on a path that can recover triggers `refresh` and, if it refreshed,
|
|
378
512
|
* one retry with the new token. Any other non-2xx response, or a retry that still fails, throws
|
|
379
|
-
* the error read from the response.
|
|
513
|
+
* the error read from the response. An aborted `signal` rejects with its reason, and a request
|
|
514
|
+
* that outlives the service's `timeout` rejects with a 504 coded `NETWORK_TIMEOUT_CODE`.
|
|
380
515
|
*/
|
|
381
516
|
async doRequest(opts) {
|
|
382
|
-
const { baseUrl = this.baseUrl,
|
|
517
|
+
const { baseUrl = this.baseUrl, paths, signal } = opts;
|
|
383
518
|
if (!baseUrl) {
|
|
384
519
|
throw getError({
|
|
385
520
|
message: '[doRequest] Invalid baseUrl to send request!',
|
|
386
521
|
});
|
|
387
522
|
}
|
|
388
523
|
const url = this.networkRequest.getRequestUrl({ baseUrl, paths });
|
|
389
|
-
|
|
390
|
-
|
|
524
|
+
const timeoutSignal = this.timeout.use
|
|
525
|
+
? AbortSignal.timeout(this.timeout.milliseconds)
|
|
526
|
+
: undefined;
|
|
527
|
+
const sendSignal = timeoutSignal && signal
|
|
528
|
+
? AbortSignal.any([signal, timeoutSignal])
|
|
529
|
+
: (timeoutSignal ?? signal);
|
|
530
|
+
try {
|
|
531
|
+
const response = await this.sendAndRead({ ...opts, url, signal: sendSignal });
|
|
532
|
+
return response;
|
|
391
533
|
}
|
|
534
|
+
catch (error) {
|
|
535
|
+
// The combined signal keeps the reason of whichever source aborted first.
|
|
536
|
+
if (this.timeout.use &&
|
|
537
|
+
timeoutSignal?.aborted &&
|
|
538
|
+
sendSignal?.reason === timeoutSignal.reason) {
|
|
539
|
+
throw getError({
|
|
540
|
+
statusCode: HTTP.ResultCodes.RS_5.GatewayTimeout,
|
|
541
|
+
messageCode: NETWORK_TIMEOUT_CODE,
|
|
542
|
+
message: `[doRequest] The request to ${paths.join('/')} did not answer within ${this.timeout.milliseconds} ms`,
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
throw error;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/** `doRequest` once the URL is checked: the send, the refresh and retry after a 401, then the read. */
|
|
549
|
+
async sendAndRead(opts) {
|
|
550
|
+
const { url, type, method, paths, body, headers, query, requestCountData, signal } = opts;
|
|
392
551
|
const sendOnce = (sendHeaders) => {
|
|
393
552
|
return this.networkRequest.getNetworkService().send({
|
|
394
553
|
url,
|
|
395
554
|
method,
|
|
396
555
|
params: query,
|
|
397
556
|
body: method === RequestMethods.GET ? undefined : body,
|
|
398
|
-
headers:
|
|
557
|
+
headers: this.toSendHeaders({
|
|
558
|
+
headers: sendHeaders,
|
|
559
|
+
body: method === RequestMethods.GET ? undefined : body,
|
|
560
|
+
}),
|
|
561
|
+
signal,
|
|
562
|
+
acceptStatus: () => true,
|
|
399
563
|
}, this.logger);
|
|
400
564
|
};
|
|
401
|
-
|
|
402
|
-
|
|
565
|
+
const sent = this.readSession();
|
|
566
|
+
let isRetried = false;
|
|
403
567
|
let fetched = await sendOnce(headers);
|
|
404
|
-
if (fetched.status === 401 &&
|
|
405
|
-
|
|
406
|
-
this.
|
|
407
|
-
|
|
568
|
+
if (fetched.status === 401 && this.recovery.canRecover({ paths })) {
|
|
569
|
+
// A 401 that arrives after the session changed is not the new session's to refresh or to end.
|
|
570
|
+
const change = this.readSessionChange({ sent });
|
|
571
|
+
if (change === false) {
|
|
572
|
+
throw this.sessionChangedError();
|
|
573
|
+
}
|
|
574
|
+
let isRefreshed = change === true;
|
|
575
|
+
if (change === undefined) {
|
|
576
|
+
// The wait ends with this request's abort; the refresh itself keeps running for the others.
|
|
577
|
+
const refreshed = await DefaultNetworkRequestService.untilAborted({
|
|
578
|
+
task: this.refresh(),
|
|
579
|
+
signal,
|
|
580
|
+
});
|
|
581
|
+
isRefreshed = refreshed;
|
|
582
|
+
}
|
|
583
|
+
// The session may have moved on while the refresh was in flight, or inside a `refreshToken` override.
|
|
584
|
+
if (this.isSessionTaken({ sent, isRetried: isRefreshed })) {
|
|
585
|
+
throw this.sessionChangedError();
|
|
586
|
+
}
|
|
587
|
+
// The shared refresh is never aborted for one request: this request stops when it returns.
|
|
588
|
+
signal?.throwIfAborted();
|
|
408
589
|
if (isRefreshed) {
|
|
409
590
|
const authHeader = this.getRequestAuthorizationHeader();
|
|
410
591
|
const retryHeaders = NetworkHeaders.merge({
|
|
@@ -418,14 +599,26 @@ export class DefaultNetworkRequestService extends BaseService {
|
|
|
418
599
|
});
|
|
419
600
|
const result = await sendOnce(retryHeaders);
|
|
420
601
|
fetched = result;
|
|
602
|
+
isRetried = true;
|
|
421
603
|
}
|
|
422
604
|
}
|
|
423
605
|
const status = fetched.status;
|
|
606
|
+
// The retry may have been answered 401 after the user who sent it had gone too.
|
|
607
|
+
if (status === 401 &&
|
|
608
|
+
this.recovery.canRecover({ paths }) &&
|
|
609
|
+
this.isSessionTaken({ sent, isRetried })) {
|
|
610
|
+
throw this.sessionChangedError();
|
|
611
|
+
}
|
|
424
612
|
if (status < 200 || status >= 300) {
|
|
425
|
-
const result2 = await
|
|
613
|
+
const result2 = await NetworkResponseReader.toError({ response: fetched });
|
|
426
614
|
throw result2;
|
|
427
615
|
}
|
|
428
|
-
|
|
616
|
+
const response = await NetworkResponseReader.read({
|
|
617
|
+
response: fetched,
|
|
618
|
+
type,
|
|
619
|
+
requestCountData,
|
|
620
|
+
});
|
|
621
|
+
return response;
|
|
429
622
|
}
|
|
430
623
|
}
|
|
431
624
|
//# sourceMappingURL=network-request.js.map
|
package/dist/common/keys.d.ts
CHANGED
|
@@ -1,44 +1,83 @@
|
|
|
1
|
-
|
|
1
|
+
import { type TBindingKey } from '@venizia/ignis-inversion';
|
|
2
|
+
import type { AbstractArdorApplication } from '../base/applications/abstract.js';
|
|
3
|
+
import type { DefaultAuthService } from '../base/services/auth.js';
|
|
4
|
+
import type { IApplicationInfo, IFeatureBase, IPermissionProvider, IRestDataProviderOptions, IRolePermissionProviderOptions, TAuthProviderOptions } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* The types of the `CoreBindings` values that live in `@venizia/ardor-admin`, which merges them in;
|
|
7
|
+
* the kernel alone reads `unknown`.
|
|
8
|
+
*/
|
|
9
|
+
export interface ICoreBindingTypes {
|
|
10
|
+
}
|
|
11
|
+
/** The type merged into `ICoreBindingTypes` under `TName`, or `unknown` when nothing merged it. */
|
|
12
|
+
export type TCoreBindingType<TName extends string> = TName extends keyof ICoreBindingTypes ? ICoreBindingTypes[TName] : unknown;
|
|
13
|
+
/**
|
|
14
|
+
* The container keys the kernel binds and reads, each made by `BindingKeys.build<T>` with the type of
|
|
15
|
+
* its value.
|
|
16
|
+
*/
|
|
2
17
|
export declare class CoreBindings {
|
|
3
18
|
/** The application itself, bound by `preConfigure`. */
|
|
4
|
-
static readonly APPLICATION_INSTANCE
|
|
19
|
+
static readonly APPLICATION_INSTANCE: TBindingKey<AbstractArdorApplication>;
|
|
5
20
|
/** The `IApplicationInfo`, bound by `preConfigure`. */
|
|
6
|
-
static readonly APPLICATION_INFO
|
|
21
|
+
static readonly APPLICATION_INFO: TBindingKey<IApplicationInfo>;
|
|
7
22
|
/** The auth provider. */
|
|
8
|
-
static readonly DEFAULT_AUTH_PROVIDER
|
|
23
|
+
static readonly DEFAULT_AUTH_PROVIDER: TBindingKey<TCoreBindingType<'authProvider'>>;
|
|
9
24
|
/** The i18n provider. */
|
|
10
|
-
static readonly DEFAULT_I18N_PROVIDER
|
|
25
|
+
static readonly DEFAULT_I18N_PROVIDER: TBindingKey<TCoreBindingType<'i18nProvider'>>;
|
|
11
26
|
/** The REST data provider. */
|
|
12
|
-
static readonly DEFAULT_REST_DATA_PROVIDER
|
|
13
|
-
/** The `
|
|
14
|
-
static readonly AUTH_PROVIDER_OPTIONS
|
|
27
|
+
static readonly DEFAULT_REST_DATA_PROVIDER: TBindingKey<TCoreBindingType<'restDataProvider'>>;
|
|
28
|
+
/** The `TAuthProviderOptions`: `{ use: true }` turns sign-in on; unbound means `{ use: false }`. */
|
|
29
|
+
static readonly AUTH_PROVIDER_OPTIONS: TBindingKey<TAuthProviderOptions>;
|
|
15
30
|
/** The `DefaultAuthService`. */
|
|
16
|
-
static readonly DEFAULT_AUTH_SERVICE
|
|
31
|
+
static readonly DEFAULT_AUTH_SERVICE: TBindingKey<DefaultAuthService>;
|
|
17
32
|
/** The `IRestDataProviderOptions`. */
|
|
18
|
-
static readonly REST_DATA_PROVIDER_OPTIONS
|
|
19
|
-
/** The options of the i18n provider. */
|
|
20
|
-
static readonly I18N_PROVIDER_OPTIONS
|
|
33
|
+
static readonly REST_DATA_PROVIDER_OPTIONS: TBindingKey<IRestDataProviderOptions>;
|
|
34
|
+
/** The options of the i18n provider; unbound means English only. */
|
|
35
|
+
static readonly I18N_PROVIDER_OPTIONS: TBindingKey<TCoreBindingType<'i18nProviderOptions'>>;
|
|
21
36
|
/** The `features()` an application mounts, bound by `preConfigure()`. */
|
|
22
|
-
static readonly FEATURES
|
|
37
|
+
static readonly FEATURES: TBindingKey<IFeatureBase[]>;
|
|
23
38
|
/** An `IPermissionProvider`, bound by the application; unbound means every check passes. */
|
|
24
|
-
static readonly PERMISSION_PROVIDER
|
|
39
|
+
static readonly PERMISSION_PROVIDER: TBindingKey<IPermissionProvider>;
|
|
40
|
+
/** The `IRolePermissionProviderOptions` that `RolePermissionProvider` reads. */
|
|
41
|
+
static readonly PERMISSION_PROVIDER_OPTIONS: TBindingKey<IRolePermissionProviderOptions>;
|
|
25
42
|
}
|
|
26
43
|
/** The session keys and the store prefix of one application, from `LocalStorageKeys.scoped`. */
|
|
27
44
|
export interface IScopedKeys {
|
|
28
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* The prefix of this application's session keys: `DefaultAuthService.cleanUp` removes them. Without
|
|
47
|
+
* an application `id` it is also the start of every scoped session, and `cleanUp` skips those.
|
|
48
|
+
*/
|
|
29
49
|
authPrefix: string;
|
|
30
|
-
/**
|
|
50
|
+
/**
|
|
51
|
+
* The data of this application's signed-in user; sign-out removes every key that starts with it.
|
|
52
|
+
* Without an application `id` it is `@app/store/`, the start of every scoped store prefix, so an
|
|
53
|
+
* application without an `id` removes the data of the applications with one: every application on
|
|
54
|
+
* an origin sets an `id`.
|
|
55
|
+
*/
|
|
31
56
|
storePrefix: string;
|
|
57
|
+
/** The preferences of this application's user: they outlive a sign-out. */
|
|
58
|
+
prefsPrefix: string;
|
|
59
|
+
/** The stored locale of the i18n provider. */
|
|
60
|
+
locale: string;
|
|
32
61
|
token: string;
|
|
33
62
|
identity: string;
|
|
34
63
|
permission: string;
|
|
35
64
|
}
|
|
36
65
|
/** The `localStorage` keys the kernel owns. */
|
|
37
66
|
export declare class LocalStorageKeys {
|
|
38
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* The session keys of an application with no `id`: `DefaultAuthService.cleanUp` removes them, and
|
|
69
|
+
* skips the keys of applications with an `id`, which are `@app/auth/<id>/...`.
|
|
70
|
+
*/
|
|
39
71
|
static readonly PREFIX_AUTH = "@app/auth/";
|
|
40
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* The data of the signed-in user: sign-out removes every key that starts with the store prefix of
|
|
74
|
+
* the application. This one is the start of every scoped store prefix (`@app/store/<id>/`), so
|
|
75
|
+
* applications that share an origin must all set an `id`. Preferences that outlive a sign-out go
|
|
76
|
+
* under `@app/prefs/`.
|
|
77
|
+
*/
|
|
41
78
|
static readonly PREFIX_STORE = "@app/store/";
|
|
79
|
+
/** Preferences that outlive a sign-out, such as the chosen language: nothing sweeps them. */
|
|
80
|
+
static readonly PREFIX_PREFS = "@app/prefs/";
|
|
42
81
|
/** The stored `IAuthTokenRecord`, as JSON. */
|
|
43
82
|
static readonly KEY_AUTH_TOKEN = "@app/auth/token";
|
|
44
83
|
/** The stored identity (`userId`, `username`, `referenceId`, `provider`), as JSON. */
|
|
@@ -50,9 +89,10 @@ export declare class LocalStorageKeys {
|
|
|
50
89
|
/**
|
|
51
90
|
* The keys of one application. With no `id` they are the constants above, so an application that is
|
|
52
91
|
* alone on its origin changes nothing. With an `id` (`IApplicationInfo.id`) the session sits under
|
|
53
|
-
* `@app/auth/<id>/` and the
|
|
92
|
+
* `@app/auth/<id>/`, the store under `@app/store/<id>/` and the preferences under `@app/prefs/<id>/`, so two applications on one origin share
|
|
54
93
|
* no session and no sign-out sweeps the other's data. The `id` is letters, digits, `.`, `_` and `-`,
|
|
55
|
-
* and is not made of dots only.
|
|
94
|
+
* and is not made of dots only. Applications on one origin must ALL set an `id`: the store prefix
|
|
95
|
+
* without one is a prefix of every scoped store prefix, so its sign-out removes their data too.
|
|
56
96
|
*/
|
|
57
97
|
static scoped(opts?: {
|
|
58
98
|
id?: string;
|
package/dist/common/keys.js
CHANGED
|
@@ -1,35 +1,74 @@
|
|
|
1
|
-
import { getError } from '@venizia/ignis-inversion';
|
|
2
|
-
/**
|
|
1
|
+
import { BindingKeys, getError } from '@venizia/ignis-inversion';
|
|
2
|
+
/**
|
|
3
|
+
* The container keys the kernel binds and reads, each made by `BindingKeys.build<T>` with the type of
|
|
4
|
+
* its value.
|
|
5
|
+
*/
|
|
3
6
|
export class CoreBindings {
|
|
4
7
|
/** The application itself, bound by `preConfigure`. */
|
|
5
|
-
static { this.APPLICATION_INSTANCE =
|
|
8
|
+
static { this.APPLICATION_INSTANCE = BindingKeys.build({
|
|
9
|
+
key: '@app/application/instance',
|
|
10
|
+
}); }
|
|
6
11
|
/** The `IApplicationInfo`, bound by `preConfigure`. */
|
|
7
|
-
static { this.APPLICATION_INFO =
|
|
12
|
+
static { this.APPLICATION_INFO = BindingKeys.build({
|
|
13
|
+
key: '@app/application/info',
|
|
14
|
+
}); }
|
|
8
15
|
/** The auth provider. */
|
|
9
|
-
static { this.DEFAULT_AUTH_PROVIDER =
|
|
16
|
+
static { this.DEFAULT_AUTH_PROVIDER = BindingKeys.build({
|
|
17
|
+
key: '@app/application/auth/default',
|
|
18
|
+
}); }
|
|
10
19
|
/** The i18n provider. */
|
|
11
|
-
static { this.DEFAULT_I18N_PROVIDER =
|
|
20
|
+
static { this.DEFAULT_I18N_PROVIDER = BindingKeys.build({
|
|
21
|
+
key: '@app/application/i18n/default',
|
|
22
|
+
}); }
|
|
12
23
|
/** The REST data provider. */
|
|
13
|
-
static { this.DEFAULT_REST_DATA_PROVIDER =
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
static { this.DEFAULT_REST_DATA_PROVIDER = BindingKeys.build({
|
|
25
|
+
key: '@app/application/data/rest/default',
|
|
26
|
+
}); }
|
|
27
|
+
/** The `TAuthProviderOptions`: `{ use: true }` turns sign-in on; unbound means `{ use: false }`. */
|
|
28
|
+
static { this.AUTH_PROVIDER_OPTIONS = BindingKeys.build({
|
|
29
|
+
key: '@app/application/options/auth',
|
|
30
|
+
}); }
|
|
16
31
|
/** The `DefaultAuthService`. */
|
|
17
|
-
static { this.DEFAULT_AUTH_SERVICE =
|
|
32
|
+
static { this.DEFAULT_AUTH_SERVICE = BindingKeys.build({
|
|
33
|
+
key: '@app/application/service/auth/default',
|
|
34
|
+
}); }
|
|
18
35
|
/** The `IRestDataProviderOptions`. */
|
|
19
|
-
static { this.REST_DATA_PROVIDER_OPTIONS =
|
|
20
|
-
|
|
21
|
-
|
|
36
|
+
static { this.REST_DATA_PROVIDER_OPTIONS = BindingKeys.build({
|
|
37
|
+
key: '@app/application/options/rest/data',
|
|
38
|
+
}); }
|
|
39
|
+
/** The options of the i18n provider; unbound means English only. */
|
|
40
|
+
static { this.I18N_PROVIDER_OPTIONS = BindingKeys.build({
|
|
41
|
+
key: '@app/application/options/i18n',
|
|
42
|
+
}); }
|
|
22
43
|
/** The `features()` an application mounts, bound by `preConfigure()`. */
|
|
23
|
-
static { this.FEATURES =
|
|
44
|
+
static { this.FEATURES = BindingKeys.build({
|
|
45
|
+
key: '@app/application/features',
|
|
46
|
+
}); }
|
|
24
47
|
/** An `IPermissionProvider`, bound by the application; unbound means every check passes. */
|
|
25
|
-
static { this.PERMISSION_PROVIDER =
|
|
48
|
+
static { this.PERMISSION_PROVIDER = BindingKeys.build({
|
|
49
|
+
key: '@app/application/permission/provider',
|
|
50
|
+
}); }
|
|
51
|
+
/** The `IRolePermissionProviderOptions` that `RolePermissionProvider` reads. */
|
|
52
|
+
static { this.PERMISSION_PROVIDER_OPTIONS = BindingKeys.build({
|
|
53
|
+
key: '@app/application/options/permission',
|
|
54
|
+
}); }
|
|
26
55
|
}
|
|
27
56
|
/** The `localStorage` keys the kernel owns. */
|
|
28
57
|
export class LocalStorageKeys {
|
|
29
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* The session keys of an application with no `id`: `DefaultAuthService.cleanUp` removes them, and
|
|
60
|
+
* skips the keys of applications with an `id`, which are `@app/auth/<id>/...`.
|
|
61
|
+
*/
|
|
30
62
|
static { this.PREFIX_AUTH = '@app/auth/'; }
|
|
31
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* The data of the signed-in user: sign-out removes every key that starts with the store prefix of
|
|
65
|
+
* the application. This one is the start of every scoped store prefix (`@app/store/<id>/`), so
|
|
66
|
+
* applications that share an origin must all set an `id`. Preferences that outlive a sign-out go
|
|
67
|
+
* under `@app/prefs/`.
|
|
68
|
+
*/
|
|
32
69
|
static { this.PREFIX_STORE = '@app/store/'; }
|
|
70
|
+
/** Preferences that outlive a sign-out, such as the chosen language: nothing sweeps them. */
|
|
71
|
+
static { this.PREFIX_PREFS = '@app/prefs/'; }
|
|
33
72
|
/** The stored `IAuthTokenRecord`, as JSON. */
|
|
34
73
|
static { this.KEY_AUTH_TOKEN = '@app/auth/token'; }
|
|
35
74
|
/** The stored identity (`userId`, `username`, `referenceId`, `provider`), as JSON. */
|
|
@@ -45,9 +84,10 @@ export class LocalStorageKeys {
|
|
|
45
84
|
/**
|
|
46
85
|
* The keys of one application. With no `id` they are the constants above, so an application that is
|
|
47
86
|
* alone on its origin changes nothing. With an `id` (`IApplicationInfo.id`) the session sits under
|
|
48
|
-
* `@app/auth/<id>/` and the
|
|
87
|
+
* `@app/auth/<id>/`, the store under `@app/store/<id>/` and the preferences under `@app/prefs/<id>/`, so two applications on one origin share
|
|
49
88
|
* no session and no sign-out sweeps the other's data. The `id` is letters, digits, `.`, `_` and `-`,
|
|
50
|
-
* and is not made of dots only.
|
|
89
|
+
* and is not made of dots only. Applications on one origin must ALL set an `id`: the store prefix
|
|
90
|
+
* without one is a prefix of every scoped store prefix, so its sign-out removes their data too.
|
|
51
91
|
*/
|
|
52
92
|
static scoped(opts = {}) {
|
|
53
93
|
const { id } = opts;
|
|
@@ -60,6 +100,8 @@ export class LocalStorageKeys {
|
|
|
60
100
|
return {
|
|
61
101
|
authPrefix: `${this.PREFIX_AUTH}${scope}`,
|
|
62
102
|
storePrefix: `${this.PREFIX_STORE}${scope}`,
|
|
103
|
+
prefsPrefix: `${this.PREFIX_PREFS}${scope}`,
|
|
104
|
+
locale: `${this.PREFIX_PREFS}${scope}locale`,
|
|
63
105
|
token: `${this.PREFIX_AUTH}${scope}token`,
|
|
64
106
|
identity: `${this.PREFIX_AUTH}${scope}identity`,
|
|
65
107
|
permission: `${this.PREFIX_AUTH}${scope}permission`,
|