@genesislcap/foundation-comms 15.83.0 → 15.84.1
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/dts/connect/connect.d.ts +22 -10
- package/dist/dts/connect/connect.d.ts.map +1 -1
- package/dist/dts/connect/connect.errors.d.ts +120 -0
- package/dist/dts/connect/connect.errors.d.ts.map +1 -0
- package/dist/dts/connect/connect.types.d.ts +27 -0
- package/dist/dts/connect/connect.types.d.ts.map +1 -1
- package/dist/dts/connect/errorCodes.d.ts +6 -0
- package/dist/dts/connect/errorCodes.d.ts.map +1 -0
- package/dist/dts/connect/http.connect.d.ts +14 -5
- package/dist/dts/connect/http.connect.d.ts.map +1 -1
- package/dist/dts/connect/http.d.ts +1 -0
- package/dist/dts/connect/http.d.ts.map +1 -1
- package/dist/dts/connect/httpTransport.d.ts +12 -6
- package/dist/dts/connect/httpTransport.d.ts.map +1 -1
- package/dist/dts/connect/index.d.ts +1 -0
- package/dist/dts/connect/index.d.ts.map +1 -1
- package/dist/dts/connect/message.d.ts +18 -1
- package/dist/dts/connect/message.d.ts.map +1 -1
- package/dist/dts/connect/networkFailure.d.ts +7 -0
- package/dist/dts/connect/networkFailure.d.ts.map +1 -0
- package/dist/dts/connect/reconnectStrategy.d.ts +2 -1
- package/dist/dts/connect/reconnectStrategy.d.ts.map +1 -1
- package/dist/dts/connect/requestLifecycle.d.ts +23 -0
- package/dist/dts/connect/requestLifecycle.d.ts.map +1 -0
- package/dist/dts/connect/socket.d.ts +14 -3
- package/dist/dts/connect/socket.d.ts.map +1 -1
- package/dist/dts/connect/socket.types.d.ts +5 -0
- package/dist/dts/connect/socket.types.d.ts.map +1 -1
- package/dist/dts/datasource/datasource.d.ts +6 -0
- package/dist/dts/datasource/datasource.d.ts.map +1 -1
- package/dist/dts/utils/error-code.d.ts +2 -1
- package/dist/dts/utils/error-code.d.ts.map +1 -1
- package/dist/esm/connect/connect.errors.js +192 -0
- package/dist/esm/connect/connect.js +56 -28
- package/dist/esm/connect/errorCodes.js +20 -0
- package/dist/esm/connect/http.connect.js +166 -74
- package/dist/esm/connect/http.js +22 -9
- package/dist/esm/connect/httpTransport.js +35 -12
- package/dist/esm/connect/index.js +1 -0
- package/dist/esm/connect/message.js +6 -1
- package/dist/esm/connect/networkFailure.js +12 -0
- package/dist/esm/connect/reconnectStrategy.js +4 -3
- package/dist/esm/connect/requestLifecycle.js +96 -0
- package/dist/esm/connect/socket.js +74 -37
- package/dist/esm/datasource/datasource.js +75 -22
- package/dist/esm/utils/error-code.js +6 -2
- package/dist/foundation-comms.api.json +1573 -35
- package/dist/foundation-comms.d.ts +244 -24
- package/package.json +12 -12
|
@@ -4,14 +4,16 @@ import { JSONSerializer } from '@genesislcap/foundation-utils';
|
|
|
4
4
|
import { observable, volatile } from '@microsoft/fast-element';
|
|
5
5
|
import { DI } from '@microsoft/fast-foundation';
|
|
6
6
|
import { StatusCodes } from 'http-status-codes';
|
|
7
|
-
import { BehaviorSubject, concat, filter, finalize, from, interval, retry, scan, switchMap, takeWhile, timer, } from 'rxjs';
|
|
7
|
+
import { BehaviorSubject, concat, EMPTY, exhaustMap, filter, finalize, from, interval, map, of, retry, scan, switchMap, takeWhile, timer, } from 'rxjs';
|
|
8
8
|
import { MetaCache } from '../metadata';
|
|
9
9
|
import { Session } from '../session';
|
|
10
10
|
import { logger } from '../utils';
|
|
11
|
+
import { isRetryable } from './connect.errors';
|
|
11
12
|
import { Http } from './http';
|
|
12
13
|
import { HttpConnectConfig } from './http.connect.types';
|
|
13
14
|
import { HttpTransport } from './httpTransport';
|
|
14
15
|
import { buildRequestCacheKey, EventMessageType, MessageBuilder, MessageType, } from './message';
|
|
16
|
+
import { abortedBeforeSend, InFlightRequests, rejectEventNackIfAsked, settleWithin, } from './requestLifecycle';
|
|
15
17
|
import { resourceNameOf, ScdbStreamDecoder } from './scdbStreamDecoder';
|
|
16
18
|
import { SessionRefreshResult, tryRefreshSession } from './sessionRefresh';
|
|
17
19
|
import { updateState } from './updateState';
|
|
@@ -50,7 +52,8 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
50
52
|
* @internal
|
|
51
53
|
*/
|
|
52
54
|
this.streams = new Map();
|
|
53
|
-
this.inFlightRequests = new
|
|
55
|
+
this.inFlightRequests = new InFlightRequests();
|
|
56
|
+
this.inFlightMetadata = new InFlightRequests();
|
|
54
57
|
/** Guards the one-time warning when a columnar encoding is stripped from a dataserver HTTP call. */
|
|
55
58
|
this.dataserverEncodingStrippedWarned = false;
|
|
56
59
|
/**
|
|
@@ -58,6 +61,8 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
58
61
|
* @internal
|
|
59
62
|
*/
|
|
60
63
|
this.ongoingRefresh = null;
|
|
64
|
+
// The router keys HTTP subscriptions by session, so each refresh invalidates every DATA_LOGON.
|
|
65
|
+
this.sessionGeneration = 0;
|
|
61
66
|
this.scdbDecoder = new ScdbStreamDecoder(this.metaCache);
|
|
62
67
|
this.scdbDecoder.setMetadataRefresher((resourceName) => this.getMetadata(resourceName, false));
|
|
63
68
|
}
|
|
@@ -77,19 +82,27 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
77
82
|
// Start a new refresh and clear the lock when done
|
|
78
83
|
this.ongoingRefresh = (() => __awaiter(this, void 0, void 0, function* () {
|
|
79
84
|
const host = this.host;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
yield new Promise((resolve) => {
|
|
85
|
-
setTimeout(resolve, DefaultHttpConnect_1.REAUTH_WAIT_MS);
|
|
86
|
-
});
|
|
87
|
-
// Check if the other tab succeeded
|
|
85
|
+
// Streams are kept: each one logs on again once the session is back.
|
|
86
|
+
this.isConnected = false;
|
|
87
|
+
let result;
|
|
88
|
+
try {
|
|
88
89
|
result = yield tryRefreshSession();
|
|
90
|
+
// If another tab is handling the refresh, wait and check once more
|
|
91
|
+
if (result === SessionRefreshResult.HANDLED_BY_OTHER_TAB) {
|
|
92
|
+
yield new Promise((resolve) => {
|
|
93
|
+
setTimeout(resolve, DefaultHttpConnect_1.REAUTH_WAIT_MS);
|
|
94
|
+
});
|
|
95
|
+
// Check if the other tab succeeded
|
|
96
|
+
result = yield tryRefreshSession();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
logger.debug('Could not refresh the session: ', error);
|
|
89
101
|
}
|
|
90
102
|
// Reconnect if session is valid (either we refreshed it or another tab did)
|
|
91
103
|
if (result === SessionRefreshResult.SUCCESS ||
|
|
92
104
|
result === SessionRefreshResult.ALREADY_VALID) {
|
|
105
|
+
this.sessionGeneration += 1;
|
|
93
106
|
this.connect(host);
|
|
94
107
|
return true;
|
|
95
108
|
}
|
|
@@ -120,16 +133,47 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
120
133
|
}
|
|
121
134
|
});
|
|
122
135
|
}
|
|
123
|
-
send(message, needsHandling) {
|
|
136
|
+
send(message, needsHandling, options) {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
return this.withinDeadline(options, (signal) => __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
yield this.waitForOngoingRefresh('Cannot send message: session refresh failed');
|
|
140
|
+
const post = () => this.transport.post(this.host, message, { signal });
|
|
141
|
+
return AUTH_MESSAGES.has(message === null || message === void 0 ? void 0 : message.MESSAGE_TYPE)
|
|
142
|
+
? post()
|
|
143
|
+
: this.retryAfterSessionRefresh(post);
|
|
144
|
+
}));
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
// One deadline covers the wait for a session refresh, the send and the resend after a 401.
|
|
148
|
+
withinDeadline(options, run) {
|
|
149
|
+
const aborted = abortedBeforeSend(options === null || options === void 0 ? void 0 : options.signal);
|
|
150
|
+
if (aborted) {
|
|
151
|
+
return aborted;
|
|
152
|
+
}
|
|
153
|
+
const controller = new AbortController();
|
|
154
|
+
return settleWithin(run(controller.signal), options, () => controller.abort());
|
|
155
|
+
}
|
|
156
|
+
waitForOngoingRefresh(failureMessage) {
|
|
124
157
|
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
// Wait for any ongoing session refresh to complete before sending
|
|
126
158
|
if (this.ongoingRefresh) {
|
|
127
159
|
const refreshSucceeded = yield this.ongoingRefresh;
|
|
128
160
|
if (!refreshSucceeded) {
|
|
129
|
-
throw new Error(
|
|
161
|
+
throw new Error(failureMessage);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
retryAfterSessionRefresh(send) {
|
|
167
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
168
|
+
try {
|
|
169
|
+
return yield send();
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (!isUnauthorized(error) || !(yield this.handleSessionRefresh())) {
|
|
173
|
+
throw error;
|
|
130
174
|
}
|
|
175
|
+
return send();
|
|
131
176
|
}
|
|
132
|
-
return this.transport.post(this.host, message);
|
|
133
177
|
});
|
|
134
178
|
}
|
|
135
179
|
httpMode() {
|
|
@@ -185,46 +229,84 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
185
229
|
RESOURCE_NAME: resourceName,
|
|
186
230
|
});
|
|
187
231
|
const interval$ = this.streams.get(SOURCE_REF).POLLING_FREQUENCY;
|
|
232
|
+
let loggedOnIn = this.sessionGeneration;
|
|
233
|
+
let subscriptionLost = false;
|
|
234
|
+
let justLoggedOn = true;
|
|
235
|
+
const logOnAgain = () => __awaiter(this, void 0, void 0, function* () {
|
|
236
|
+
const generation = this.sessionGeneration;
|
|
237
|
+
const reply = yield this.send(streamMessage).catch((error) => {
|
|
238
|
+
// Still subscribed: the subscription's session survived the refresh.
|
|
239
|
+
if ((error === null || error === void 0 ? void 0 : error.status) !== StatusCodes.CONFLICT)
|
|
240
|
+
throw error;
|
|
241
|
+
return undefined;
|
|
242
|
+
});
|
|
243
|
+
loggedOnIn = generation;
|
|
244
|
+
subscriptionLost = false;
|
|
245
|
+
justLoggedOn = true;
|
|
246
|
+
return reply;
|
|
247
|
+
});
|
|
248
|
+
// Decided after any refresh in flight, since the refresh may change the session.
|
|
249
|
+
const nextReply = () => __awaiter(this, void 0, void 0, function* () {
|
|
250
|
+
yield this.waitForOngoingRefresh('Cannot get message: session refresh failed');
|
|
251
|
+
if (subscriptionLost || loggedOnIn !== this.sessionGeneration) {
|
|
252
|
+
return logOnAgain();
|
|
253
|
+
}
|
|
254
|
+
const reply = yield this.getMessage(streamMessage);
|
|
255
|
+
justLoggedOn = false;
|
|
256
|
+
return reply;
|
|
257
|
+
});
|
|
258
|
+
// A subscription that vanished between polls, e.g. after another tab refreshed the session,
|
|
259
|
+
// is logged on again; one missing straight after a DATA_LOGON is gone for good.
|
|
260
|
+
const onSubscriptionLost = () => {
|
|
261
|
+
if (justLoggedOn)
|
|
262
|
+
return false;
|
|
263
|
+
subscriptionLost = true;
|
|
264
|
+
return true;
|
|
265
|
+
};
|
|
188
266
|
const stream = concat(
|
|
189
267
|
// initial DATA_LOGON needs to be a POST
|
|
190
268
|
from(this.send(streamMessage)),
|
|
191
|
-
// subsequent
|
|
192
|
-
interval
|
|
193
|
-
.pipe(filter(() => document.visibilityState === 'visible'))
|
|
194
|
-
.pipe(switchMap((ms) => interval(ms)))
|
|
195
|
-
.pipe(switchMap(() => from(this.getMessage(streamMessage))))).pipe(takeWhile(() => this.streams.has(SOURCE_REF)), // ensure it stops emitting once removed
|
|
196
|
-
retry({
|
|
269
|
+
// subsequent GETs with the same SOURCE_REF
|
|
270
|
+
interval$.pipe(switchMap((ms) => interval(ms)), takeWhile(() => this.streams.has(SOURCE_REF)), filter(() => document.visibilityState === 'visible'), exhaustMap(() => from(nextReply())), filter((reply) => reply !== undefined), retry({
|
|
197
271
|
count: DefaultHttpConnect_1.MAX_RETRIES,
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
// Only retry if the error is a connection error or auth error
|
|
203
|
-
const isRetryableError = error.status === 0 ||
|
|
204
|
-
(error.status &&
|
|
205
|
-
error.status >= StatusCodes.BAD_REQUEST &&
|
|
206
|
-
error.status < StatusCodes.INTERNAL_SERVER_ERROR);
|
|
207
|
-
if (!isRetryableError) {
|
|
208
|
-
throw err;
|
|
209
|
-
}
|
|
210
|
-
// Remove SOURCE_REF from the map if the error is a 404, meaning the DS has already expired
|
|
211
|
-
if (error.status === StatusCodes.NOT_FOUND) {
|
|
212
|
-
this.streams.delete(SOURCE_REF);
|
|
213
|
-
}
|
|
214
|
-
// Handle 401 by trying to refresh session, then delay for other errors
|
|
215
|
-
if (error.status === StatusCodes.UNAUTHORIZED) {
|
|
216
|
-
return from(this.handleSessionRefresh());
|
|
217
|
-
}
|
|
218
|
-
// For other errors, just delay
|
|
219
|
-
return timer(DefaultHttpConnect_1.RETRY_DELAY_MS);
|
|
220
|
-
},
|
|
221
|
-
}), finalize(() => {
|
|
272
|
+
resetOnSuccess: true,
|
|
273
|
+
delay: (error, retryCount) => this.pollRetryDelay(error, retryCount, SOURCE_REF, onSubscriptionLost),
|
|
274
|
+
}))).pipe(takeWhile(() => this.streams.has(SOURCE_REF)), // ensure it stops emitting once removed
|
|
275
|
+
finalize(() => {
|
|
222
276
|
logger.debug(`Terminating stream for ${resourceName} - ${SOURCE_REF}`);
|
|
223
|
-
|
|
277
|
+
// Not registered once the server has dropped it (404) or the connection was closed.
|
|
278
|
+
if (this.streams.has(SOURCE_REF)) {
|
|
279
|
+
this.dataLogoff(SOURCE_REF).catch((error) => logger.debug(`DATA_LOGOFF failed for ${SOURCE_REF}`, error));
|
|
280
|
+
}
|
|
224
281
|
}));
|
|
225
282
|
this.streams.get(SOURCE_REF).STREAM = stream;
|
|
226
283
|
return stream;
|
|
227
284
|
}
|
|
285
|
+
// Completing without emitting ends the stream; throwing errors it.
|
|
286
|
+
pollRetryDelay(error, retryCount, sourceRef, onSubscriptionLost) {
|
|
287
|
+
const status = error === null || error === void 0 ? void 0 : error.status;
|
|
288
|
+
logger.error(`Retry ${retryCount}/${DefaultHttpConnect_1.MAX_RETRIES}: status ${status !== null && status !== void 0 ? status : 'unknown'}`, error);
|
|
289
|
+
// the router no longer has the subscription
|
|
290
|
+
if (status === StatusCodes.NOT_FOUND) {
|
|
291
|
+
if (onSubscriptionLost()) {
|
|
292
|
+
return of(true);
|
|
293
|
+
}
|
|
294
|
+
this.streams.delete(sourceRef);
|
|
295
|
+
return EMPTY;
|
|
296
|
+
}
|
|
297
|
+
if (isUnauthorized(error)) {
|
|
298
|
+
return from(this.handleSessionRefresh()).pipe(map((refreshed) => {
|
|
299
|
+
if (!refreshed) {
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
return refreshed;
|
|
303
|
+
}));
|
|
304
|
+
}
|
|
305
|
+
if (!isRetryable(error)) {
|
|
306
|
+
throw error;
|
|
307
|
+
}
|
|
308
|
+
return timer(DefaultHttpConnect_1.RETRY_DELAY_MS);
|
|
309
|
+
}
|
|
228
310
|
streamState(resourceName, onMessage = () => { }, onError = () => { }, params, initialState = []) {
|
|
229
311
|
return this.stream(resourceName, onMessage, onError, params).pipe(scan(updateState, initialState));
|
|
230
312
|
}
|
|
@@ -234,12 +316,7 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
234
316
|
}
|
|
235
317
|
getMessage(message) {
|
|
236
318
|
return __awaiter(this, void 0, void 0, function* () {
|
|
237
|
-
|
|
238
|
-
const refreshSucceeded = yield this.ongoingRefresh;
|
|
239
|
-
if (!refreshSucceeded) {
|
|
240
|
-
throw new Error('Cannot get message: session refresh failed');
|
|
241
|
-
}
|
|
242
|
-
}
|
|
319
|
+
yield this.waitForOngoingRefresh('Cannot get message: session refresh failed');
|
|
243
320
|
return this.transport.get(this.host, message);
|
|
244
321
|
});
|
|
245
322
|
}
|
|
@@ -269,17 +346,27 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
269
346
|
});
|
|
270
347
|
}
|
|
271
348
|
getMetadata(resourceName_1) {
|
|
272
|
-
return __awaiter(this, arguments, void 0, function* (resourceName, useCache = true) {
|
|
349
|
+
return __awaiter(this, arguments, void 0, function* (resourceName, useCache = true, options) {
|
|
273
350
|
if (useCache) {
|
|
274
351
|
if (this.metaCache.hasMetadataFor(resourceName)) {
|
|
275
352
|
logger.debug(`HTTP MODE - Cached metadata retrieved for ${resourceName}`);
|
|
276
353
|
return this.metaCache.getMetadataFor(resourceName);
|
|
277
354
|
}
|
|
278
355
|
}
|
|
356
|
+
if (this.inFlightMetadata.has(resourceName)) {
|
|
357
|
+
logger.debug(`HTTP MODE - In-flight metadata request collapsed for ${resourceName}`);
|
|
358
|
+
}
|
|
359
|
+
return this.inFlightMetadata.join(resourceName, (signal) => this.fetchMetadata(resourceName, signal), options);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
fetchMetadata(resourceName, signal) {
|
|
363
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
364
|
+
var _a;
|
|
279
365
|
const metaRequestMessage = this.messageBuilder.createMetaRequestMessage(resourceName);
|
|
280
366
|
logger.debug(`HTTP MODE - ${metaRequestMessage.MESSAGE_TYPE} (metadata) message created -> `, metaRequestMessage);
|
|
281
367
|
// what was top level payload using websockets is now under DETAILS for some reason
|
|
282
|
-
const
|
|
368
|
+
const response = yield this.send(metaRequestMessage, true, { signal });
|
|
369
|
+
const details = (_a = response === null || response === void 0 ? void 0 : response.DETAILS) !== null && _a !== void 0 ? _a : {};
|
|
283
370
|
if (Object.keys(details).length) {
|
|
284
371
|
this.metaCache.setMetadataFor(resourceName, details);
|
|
285
372
|
}
|
|
@@ -294,37 +381,33 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
294
381
|
});
|
|
295
382
|
}
|
|
296
383
|
request(resourceName_1, params_1) {
|
|
297
|
-
return __awaiter(this, arguments, void 0, function* (resourceName, params, useCache = false) {
|
|
384
|
+
return __awaiter(this, arguments, void 0, function* (resourceName, params, useCache = false, options) {
|
|
298
385
|
const cacheKey = buildRequestCacheKey(resourceName, params);
|
|
299
386
|
if (useCache && this.metaCache.hasRequestFor(cacheKey)) {
|
|
300
387
|
logger.debug(`HTTP MODE - Cached request response retrieved for ${cacheKey}`);
|
|
301
388
|
return this.metaCache.getRequestFor(cacheKey);
|
|
302
389
|
}
|
|
303
|
-
|
|
304
|
-
if (inFlight) {
|
|
390
|
+
if (this.inFlightRequests.has(cacheKey)) {
|
|
305
391
|
logger.debug(`HTTP MODE - In-flight request collapsed for ${cacheKey}`);
|
|
306
|
-
return inFlight;
|
|
307
392
|
}
|
|
393
|
+
const response = yield this.inFlightRequests.join(cacheKey, (signal) => this.sendRequest(resourceName, params, signal), options);
|
|
394
|
+
if (useCache && response) {
|
|
395
|
+
this.metaCache.setRequestFor(cacheKey, response);
|
|
396
|
+
}
|
|
397
|
+
return response;
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
sendRequest(resourceName, params, signal) {
|
|
401
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
308
402
|
const requestMessage = this.messageBuilder.createRequestMessage(resourceName, params);
|
|
309
403
|
const scdbResourceName = resourceNameOf(requestMessage);
|
|
310
404
|
this.scdbDecoder.prepareRequest(requestMessage);
|
|
311
405
|
logger.debug(`HTTP MODE! - ${requestMessage.MESSAGE_TYPE} (request server) message created -> `, requestMessage);
|
|
406
|
+
yield this.waitForOngoingRefresh('Cannot send request: session refresh failed');
|
|
312
407
|
// REQUEST_SERVER messages use the GET method in HTTP mode; the transport encodes the
|
|
313
408
|
// REQUEST/DETAILS params as a query string since GET requests cannot carry a body.
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
.then((response) => {
|
|
317
|
-
this.scdbDecoder.onMessage(response, scdbResourceName);
|
|
318
|
-
return response;
|
|
319
|
-
})
|
|
320
|
-
.finally(() => {
|
|
321
|
-
this.inFlightRequests.delete(cacheKey);
|
|
322
|
-
});
|
|
323
|
-
this.inFlightRequests.set(cacheKey, promise);
|
|
324
|
-
const response = yield promise;
|
|
325
|
-
if (useCache && response) {
|
|
326
|
-
this.metaCache.setRequestFor(cacheKey, response);
|
|
327
|
-
}
|
|
409
|
+
const response = yield this.retryAfterSessionRefresh(() => this.transport.requestServer(this.host, requestMessage, { signal }));
|
|
410
|
+
this.scdbDecoder.onMessage(response, scdbResourceName);
|
|
328
411
|
return response;
|
|
329
412
|
});
|
|
330
413
|
}
|
|
@@ -360,13 +443,13 @@ let DefaultHttpConnect = DefaultHttpConnect_1 = class DefaultHttpConnect {
|
|
|
360
443
|
return resources;
|
|
361
444
|
});
|
|
362
445
|
}
|
|
363
|
-
commitEvent(eventName, params) {
|
|
446
|
+
commitEvent(eventName, params, options) {
|
|
364
447
|
return __awaiter(this, void 0, void 0, function* () {
|
|
365
448
|
const commitMessage = this.messageBuilder.createCommitMessage(eventName, params);
|
|
366
449
|
logger.debug(`HTTP MODE! - ${commitMessage.MESSAGE_TYPE} (commit) message created -> `, commitMessage);
|
|
367
|
-
return this.send(commitMessage).then((reply) => {
|
|
450
|
+
return this.send(commitMessage, true, options).then((reply) => {
|
|
368
451
|
this.maybeTurbo(eventName, reply);
|
|
369
|
-
return reply;
|
|
452
|
+
return rejectEventNackIfAsked(reply, commitMessage, options);
|
|
370
453
|
});
|
|
371
454
|
});
|
|
372
455
|
}
|
|
@@ -470,6 +553,15 @@ DefaultHttpConnect = DefaultHttpConnect_1 = __decorate([
|
|
|
470
553
|
__param(6, HttpTransport)
|
|
471
554
|
], DefaultHttpConnect);
|
|
472
555
|
export { DefaultHttpConnect };
|
|
556
|
+
const isUnauthorized = (error) => (error === null || error === void 0 ? void 0 : error.status) === StatusCodes.UNAUTHORIZED;
|
|
557
|
+
// A 401 on these is the answer itself, not an expired session to refresh.
|
|
558
|
+
const AUTH_MESSAGES = new Set([
|
|
559
|
+
EventMessageType.EVENT_LOGIN_AUTH,
|
|
560
|
+
EventMessageType.EVENT_LOGOUT,
|
|
561
|
+
EventMessageType.EVENT_CHANGE_USER_PASSWORD,
|
|
562
|
+
EventMessageType.EVENT_SELF_SERVICE_PASSWORD_RESET,
|
|
563
|
+
EventMessageType.EVENT_PASSWORD_RESET_ACTION,
|
|
564
|
+
]);
|
|
473
565
|
/**
|
|
474
566
|
* The DI token for the Connect interface (HTTP-only).
|
|
475
567
|
* @internal
|
package/dist/esm/connect/http.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import { __awaiter, __decorate, __param } from "tslib";
|
|
2
2
|
import { JSONSerializer } from '@genesislcap/foundation-utils';
|
|
3
3
|
import { DI } from '@microsoft/fast-foundation';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
import { ConnectHttpError } from './connect.errors';
|
|
5
|
+
import { markNetworkFailure } from './networkFailure';
|
|
6
|
+
const readBody = (response) => __awaiter(void 0, void 0, void 0, function* () {
|
|
7
|
+
const text = yield response.text().catch(() => undefined);
|
|
8
|
+
if (!text) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(text);
|
|
13
|
+
}
|
|
14
|
+
catch (_a) {
|
|
15
|
+
return text;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
const createHttpError = (response) => __awaiter(void 0, void 0, void 0, function* () { return new ConnectHttpError(response.status, response.statusText, yield readBody(response)); });
|
|
10
19
|
/** HTTP status for a successful conditional request whose resource is unchanged. */
|
|
11
20
|
const HTTP_STATUS_NOT_MODIFIED = 304;
|
|
12
21
|
/**
|
|
@@ -27,13 +36,17 @@ let DefaultHttp = class DefaultHttp {
|
|
|
27
36
|
}
|
|
28
37
|
get(url, requestInit) {
|
|
29
38
|
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
const response = yield fetch(url, Object.assign(Object.assign({ credentials: 'include', headers: { 'Content-type': 'application/json; charset=UTF-8' } }, requestInit), { method: 'GET' }))
|
|
39
|
+
const response = yield fetch(url, Object.assign(Object.assign({ credentials: 'include', headers: { 'Content-type': 'application/json; charset=UTF-8' } }, requestInit), { method: 'GET' })).catch((error) => {
|
|
40
|
+
throw markNetworkFailure(error);
|
|
41
|
+
});
|
|
31
42
|
return this.handleResponse(response, requestInit);
|
|
32
43
|
});
|
|
33
44
|
}
|
|
34
45
|
post(url, requestInit) {
|
|
35
46
|
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
const response = yield fetch(url, Object.assign(Object.assign({ credentials: 'include', headers: { 'Content-type': 'application/json; charset=UTF-8' } }, requestInit), { method: 'POST' }))
|
|
47
|
+
const response = yield fetch(url, Object.assign(Object.assign({ credentials: 'include', headers: { 'Content-type': 'application/json; charset=UTF-8' } }, requestInit), { method: 'POST' })).catch((error) => {
|
|
48
|
+
throw markNetworkFailure(error);
|
|
49
|
+
});
|
|
37
50
|
return this.handleResponse(response, requestInit);
|
|
38
51
|
});
|
|
39
52
|
}
|
|
@@ -52,7 +65,7 @@ let DefaultHttp = class DefaultHttp {
|
|
|
52
65
|
return undefined;
|
|
53
66
|
}
|
|
54
67
|
if (!response.ok) {
|
|
55
|
-
throw createHttpError(response);
|
|
68
|
+
throw yield createHttpError(response);
|
|
56
69
|
}
|
|
57
70
|
const serializer = requestInit.serializer || this.serializer;
|
|
58
71
|
return serializer.deserialize(response);
|
|
@@ -2,8 +2,12 @@ var DefaultHttpTransport_1;
|
|
|
2
2
|
import { __decorate, __param } from "tslib";
|
|
3
3
|
import { DI } from '@microsoft/fast-foundation';
|
|
4
4
|
import { logger } from '../utils';
|
|
5
|
+
import { ConnectHttpError, ConnectNackError, ConnectTransportError } from './connect.errors';
|
|
6
|
+
import { normaliseErrorCodes } from './errorCodes';
|
|
5
7
|
import { Http } from './http';
|
|
6
8
|
import { EventMessageType, MessageBuilder } from './message';
|
|
9
|
+
import { isNetworkFailure } from './networkFailure';
|
|
10
|
+
import { abortedBeforeSend, isRejectedNack, settleWithin } from './requestLifecycle';
|
|
7
11
|
/**
|
|
8
12
|
* The default implementation of the `HttpTransport` interface.
|
|
9
13
|
* @internal
|
|
@@ -30,18 +34,13 @@ let DefaultHttpTransport = DefaultHttpTransport_1 = class DefaultHttpTransport {
|
|
|
30
34
|
}
|
|
31
35
|
return normalised;
|
|
32
36
|
}
|
|
33
|
-
post(host, message) {
|
|
34
|
-
return this.http.post(DefaultHttpTransport_1.getUrl(host, message), {
|
|
35
|
-
body: DefaultHttpTransport_1.getBody(message),
|
|
36
|
-
headers: this.messageBuilder.createHTTPHeadersFromMessage(message),
|
|
37
|
-
});
|
|
37
|
+
post(host, message, options) {
|
|
38
|
+
return this.call(message, options, (init) => this.http.post(DefaultHttpTransport_1.getUrl(host, message), Object.assign(Object.assign({}, init), { body: DefaultHttpTransport_1.getBody(message), headers: this.messageBuilder.createHTTPHeadersFromMessage(message) })));
|
|
38
39
|
}
|
|
39
|
-
get(host, message) {
|
|
40
|
-
return this.http.get(DefaultHttpTransport_1.getUrl(host, message), {
|
|
41
|
-
headers: this.messageBuilder.createHTTPHeadersFromMessage(message),
|
|
42
|
-
});
|
|
40
|
+
get(host, message, options) {
|
|
41
|
+
return this.call(message, options, (init) => this.http.get(DefaultHttpTransport_1.getUrl(host, message), Object.assign(Object.assign({}, init), { headers: this.messageBuilder.createHTTPHeadersFromMessage(message) })));
|
|
43
42
|
}
|
|
44
|
-
requestServer(host, requestMessage) {
|
|
43
|
+
requestServer(host, requestMessage, options) {
|
|
45
44
|
// REQUEST_SERVER messages use the GET method in HTTP mode.
|
|
46
45
|
// GET requests cannot have a body, so we convert REQUEST and DETAILS params to a query string.
|
|
47
46
|
let url = DefaultHttpTransport_1.getUrl(host, requestMessage);
|
|
@@ -70,9 +69,33 @@ let DefaultHttpTransport = DefaultHttpTransport_1 = class DefaultHttpTransport {
|
|
|
70
69
|
url = `${url}?${queryParts.join('&')}`;
|
|
71
70
|
}
|
|
72
71
|
logger.debug(`HTTP transport - REQUEST_SERVER GET URL: ${url}`);
|
|
73
|
-
return this.http.get(url, {
|
|
74
|
-
|
|
72
|
+
return this.call(requestMessage, options, (init) => this.http.get(url, Object.assign(Object.assign({}, init), { headers: this.messageBuilder.createHTTPHeadersFromMessage(requestMessage) })));
|
|
73
|
+
}
|
|
74
|
+
// The router sends NACKs as 200 unless strictHttpStatusCode is on; reject them as the socket does.
|
|
75
|
+
call(message, options, request) {
|
|
76
|
+
const aborted = abortedBeforeSend(options === null || options === void 0 ? void 0 : options.signal);
|
|
77
|
+
if (aborted) {
|
|
78
|
+
return aborted;
|
|
79
|
+
}
|
|
80
|
+
const controller = (options === null || options === void 0 ? void 0 : options.signal) || (options === null || options === void 0 ? void 0 : options.timeout) ? new AbortController() : undefined;
|
|
81
|
+
const response = request(controller ? { signal: controller.signal } : {}).then((reply) => {
|
|
82
|
+
normaliseErrorCodes(reply);
|
|
83
|
+
if (isRejectedNack(reply)) {
|
|
84
|
+
throw new ConnectNackError(reply, message);
|
|
85
|
+
}
|
|
86
|
+
return reply;
|
|
87
|
+
}, (error) => {
|
|
88
|
+
throw DefaultHttpTransport_1.asConnectError(error);
|
|
75
89
|
});
|
|
90
|
+
return settleWithin(response, options, () => controller === null || controller === void 0 ? void 0 : controller.abort());
|
|
91
|
+
}
|
|
92
|
+
static asConnectError(error) {
|
|
93
|
+
if (error instanceof ConnectHttpError) {
|
|
94
|
+
normaliseErrorCodes(error.body);
|
|
95
|
+
}
|
|
96
|
+
return isNetworkFailure(error)
|
|
97
|
+
? new ConnectTransportError(error.message, { cause: error })
|
|
98
|
+
: error;
|
|
76
99
|
}
|
|
77
100
|
static getBody(message) {
|
|
78
101
|
return JSON.stringify(message);
|
|
@@ -23,6 +23,7 @@ export var MessageType;
|
|
|
23
23
|
MessageType["LOGOUT_ACK"] = "LOGOUT_ACK";
|
|
24
24
|
MessageType["LOGOUT_NACK"] = "LOGOUT_NACK";
|
|
25
25
|
MessageType["QUERY_UPDATE"] = "QUERY_UPDATE";
|
|
26
|
+
MessageType["INVALID_INDEX_NACK"] = "INVALID_INDEX_NACK";
|
|
26
27
|
})(MessageType || (MessageType = {}));
|
|
27
28
|
/**
|
|
28
29
|
* Event Message types
|
|
@@ -98,7 +99,11 @@ export const messageOrThrow = (message) => {
|
|
|
98
99
|
*/
|
|
99
100
|
export const messageHasErrorCode = (message, code) => {
|
|
100
101
|
return Array.isArray(message === null || message === void 0 ? void 0 : message.ERROR)
|
|
101
|
-
? message === null || message === void 0 ? void 0 : message.ERROR.some((error) => {
|
|
102
|
+
? message === null || message === void 0 ? void 0 : message.ERROR.some((error) => {
|
|
103
|
+
var _a, _b;
|
|
104
|
+
return String((_a = error.CODE) !== null && _a !== void 0 ? _a : '').includes(`${code}`) ||
|
|
105
|
+
String((_b = error.STATUS_CODE) !== null && _b !== void 0 ? _b : '').includes(`${code}`);
|
|
106
|
+
})
|
|
102
107
|
: false;
|
|
103
108
|
};
|
|
104
109
|
/**
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const networkFailures = new WeakSet();
|
|
2
|
+
/**
|
|
3
|
+
* Marks the `TypeError` `fetch` rejects with when a request never got a response, so it can be
|
|
4
|
+
* told apart from other `TypeError`s such as bugs in a custom `Http` or serializer.
|
|
5
|
+
*/
|
|
6
|
+
export const markNetworkFailure = (error) => {
|
|
7
|
+
if (error instanceof TypeError) {
|
|
8
|
+
networkFailures.add(error);
|
|
9
|
+
}
|
|
10
|
+
return error;
|
|
11
|
+
};
|
|
12
|
+
export const isNetworkFailure = (error) => error instanceof TypeError && networkFailures.has(error);
|
|
@@ -46,13 +46,14 @@ export const retryInterval = (reconnectOptions, scheduler) => {
|
|
|
46
46
|
* @public
|
|
47
47
|
* @param reconnectOptions - {@link SocketReconnectOptions} options
|
|
48
48
|
* @param attempt - number of attempts
|
|
49
|
-
* @returns number of milliseconds
|
|
49
|
+
* @returns a random number of milliseconds between half and all of 2 ^ attempts * baseInterval,
|
|
50
|
+
* capped at 60 seconds, so clients dropped together do not reconnect together
|
|
50
51
|
*/
|
|
51
52
|
export const exponentialScheduler = (reconnectOptions, attempts) => {
|
|
52
53
|
const { reconnectInterval = DEFAULT_RECONNECT_INTERVAL } = reconnectOptions;
|
|
53
54
|
const baseInterval = reconnectInterval > MAX_RECONNECT_INTERVAL ? MAX_RECONNECT_INTERVAL : reconnectInterval;
|
|
54
|
-
const
|
|
55
|
-
return
|
|
55
|
+
const interval = Math.min(baseInterval * Math.pow(2, attempts), MAX_RECONNECT_INTERVAL);
|
|
56
|
+
return interval / 2 + (Math.random() * interval) / 2;
|
|
56
57
|
};
|
|
57
58
|
/**
|
|
58
59
|
* Linear Interval scheduler for reconnect
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ConnectAbortError, ConnectNackError, ConnectTimeoutError, } from './connect.errors';
|
|
2
|
+
import { MessageType } from './message';
|
|
3
|
+
const EVENT_NACK = 'EVENT_NACK';
|
|
4
|
+
// setTimeout fires almost at once for delays it cannot hold.
|
|
5
|
+
const MAX_TIMEOUT = 2147483647;
|
|
6
|
+
const REJECTED_NACKS = new Set([
|
|
7
|
+
MessageType.MSG_NACK,
|
|
8
|
+
MessageType.LOGON_NACK,
|
|
9
|
+
MessageType.INVALID_INDEX_NACK,
|
|
10
|
+
]);
|
|
11
|
+
export const isRejectedNack = (message) => REJECTED_NACKS.has(message === null || message === void 0 ? void 0 : message.MESSAGE_TYPE);
|
|
12
|
+
/**
|
|
13
|
+
* Settles like `promise` unless `timeout` elapses or `signal` aborts first, then rejects and calls
|
|
14
|
+
* `onCancel` so the caller can release what the promise was waiting on.
|
|
15
|
+
*/
|
|
16
|
+
export function settleWithin(promise, { timeout, signal } = {}, onCancel) {
|
|
17
|
+
const hasTimeout = timeout > 0 && Number.isFinite(timeout);
|
|
18
|
+
if (!hasTimeout && !signal) {
|
|
19
|
+
return promise;
|
|
20
|
+
}
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
let timer;
|
|
23
|
+
const onAbort = () => cancel(new ConnectAbortError(signal.reason));
|
|
24
|
+
const cleanUp = () => {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
signal === null || signal === void 0 ? void 0 : signal.removeEventListener('abort', onAbort);
|
|
27
|
+
};
|
|
28
|
+
const cancel = (error) => {
|
|
29
|
+
cleanUp();
|
|
30
|
+
onCancel === null || onCancel === void 0 ? void 0 : onCancel(error);
|
|
31
|
+
reject(error);
|
|
32
|
+
};
|
|
33
|
+
promise.then((value) => {
|
|
34
|
+
cleanUp();
|
|
35
|
+
resolve(value);
|
|
36
|
+
}, (error) => {
|
|
37
|
+
cleanUp();
|
|
38
|
+
reject(error);
|
|
39
|
+
});
|
|
40
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
41
|
+
onAbort();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', onAbort, { once: true });
|
|
45
|
+
if (hasTimeout) {
|
|
46
|
+
timer = setTimeout(() => cancel(new ConnectTimeoutError(timeout)), Math.min(timeout, MAX_TIMEOUT));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export const abortedBeforeSend = (signal) => (signal === null || signal === void 0 ? void 0 : signal.aborted) ? Promise.reject(new ConnectAbortError(signal.reason)) : undefined;
|
|
51
|
+
export const rejectEventNackIfAsked = (response, sentMessage, options) => {
|
|
52
|
+
if ((options === null || options === void 0 ? void 0 : options.rejectEventNack) && (response === null || response === void 0 ? void 0 : response.MESSAGE_TYPE) === EVENT_NACK) {
|
|
53
|
+
throw new ConnectNackError(response, sentMessage);
|
|
54
|
+
}
|
|
55
|
+
return response;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Shares one in-flight request per key. Each caller's timeout and signal stop only its own wait;
|
|
59
|
+
* the request itself is aborted once every caller has stopped waiting.
|
|
60
|
+
*/
|
|
61
|
+
export class InFlightRequests {
|
|
62
|
+
constructor() {
|
|
63
|
+
this.entries = new Map();
|
|
64
|
+
}
|
|
65
|
+
has(key) {
|
|
66
|
+
return this.entries.has(key);
|
|
67
|
+
}
|
|
68
|
+
join(key, start, options) {
|
|
69
|
+
var _a;
|
|
70
|
+
const aborted = abortedBeforeSend(options === null || options === void 0 ? void 0 : options.signal);
|
|
71
|
+
if (aborted) {
|
|
72
|
+
return aborted;
|
|
73
|
+
}
|
|
74
|
+
const entry = (_a = this.entries.get(key)) !== null && _a !== void 0 ? _a : this.start(key, start);
|
|
75
|
+
entry.waiting += 1;
|
|
76
|
+
return settleWithin(entry.promise, options, () => {
|
|
77
|
+
entry.waiting -= 1;
|
|
78
|
+
if (entry.waiting === 0) {
|
|
79
|
+
this.release(key, entry);
|
|
80
|
+
entry.controller.abort();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
start(key, start) {
|
|
85
|
+
const controller = new AbortController();
|
|
86
|
+
const entry = { controller, waiting: 0, promise: undefined };
|
|
87
|
+
entry.promise = start(controller.signal).finally(() => this.release(key, entry));
|
|
88
|
+
this.entries.set(key, entry);
|
|
89
|
+
return entry;
|
|
90
|
+
}
|
|
91
|
+
release(key, entry) {
|
|
92
|
+
if (this.entries.get(key) === entry) {
|
|
93
|
+
this.entries.delete(key);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|