@aws-amplify/core 4.7.9-unstable.2 → 4.7.9
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/CHANGELOG.md +11 -0
- package/dist/aws-amplify-core.js +673 -45
- package/dist/aws-amplify-core.js.map +1 -1
- package/dist/aws-amplify-core.min.js +4 -4
- package/dist/aws-amplify-core.min.js.map +1 -1
- package/lib/Credentials.js +1 -1
- package/lib/Credentials.js.map +1 -1
- package/lib/Util/BackgroundProcessManager.js +444 -0
- package/lib/Util/BackgroundProcessManager.js.map +1 -0
- package/lib/Util/Retry.js +76 -34
- package/lib/Util/Retry.js.map +1 -1
- package/lib/Util/index.js +1 -0
- package/lib/Util/index.js.map +1 -1
- package/lib-esm/Credentials.d.ts +1 -1
- package/lib-esm/Credentials.js +1 -1
- package/lib-esm/Credentials.js.map +1 -1
- package/lib-esm/Util/BackgroundProcessManager.d.ts +204 -0
- package/lib-esm/Util/BackgroundProcessManager.js +442 -0
- package/lib-esm/Util/BackgroundProcessManager.js.map +1 -0
- package/lib-esm/Util/Retry.d.ts +2 -2
- package/lib-esm/Util/Retry.js +76 -34
- package/lib-esm/Util/Retry.js.map +1 -1
- package/lib-esm/Util/index.d.ts +1 -0
- package/lib-esm/Util/index.js +1 -0
- package/lib-esm/Util/index.js.map +1 -1
- package/package.json +2 -2
- package/src/Credentials.ts +13 -14
- package/src/Util/BackgroundProcessManager.ts +449 -0
- package/src/Util/Retry.ts +66 -31
- package/src/Util/index.ts +1 -0
- package/lib/Amplify.d.ts +0 -31
- package/lib/ClientDevice/android.d.ts +0 -5
- package/lib/ClientDevice/browser.d.ts +0 -25
- package/lib/ClientDevice/index.d.ts +0 -27
- package/lib/ClientDevice/ios.d.ts +0 -7
- package/lib/ClientDevice/reactnative.d.ts +0 -14
- package/lib/Credentials.d.ts +0 -51
- package/lib/Errors.d.ts +0 -2
- package/lib/Hub.d.ts +0 -31
- package/lib/I18n/I18n.d.ts +0 -59
- package/lib/I18n/index.d.ts +0 -53
- package/lib/I18n/types.d.ts +0 -3
- package/lib/JS.d.ts +0 -56
- package/lib/Logger/ConsoleLogger.d.ts +0 -80
- package/lib/Logger/index.d.ts +0 -1
- package/lib/Logger/logger-interface.d.ts +0 -12
- package/lib/OAuthHelper/FacebookOAuth.d.ts +0 -10
- package/lib/OAuthHelper/GoogleOAuth.d.ts +0 -10
- package/lib/OAuthHelper/index.d.ts +0 -4
- package/lib/Parser.d.ts +0 -12
- package/lib/Platform/index.d.ts +0 -11
- package/lib/Platform/version.d.ts +0 -1
- package/lib/Providers/AWSCloudWatchProvider.d.ts +0 -34
- package/lib/Providers/index.d.ts +0 -1
- package/lib/RNComponents/index.d.ts +0 -5
- package/lib/RNComponents/reactnative.d.ts +0 -3
- package/lib/ServiceWorker/ServiceWorker.d.ts +0 -72
- package/lib/ServiceWorker/index.d.ts +0 -13
- package/lib/Signer.d.ts +0 -45
- package/lib/StorageHelper/index.d.ts +0 -45
- package/lib/StorageHelper/reactnative.d.ts +0 -17
- package/lib/UniversalStorage/index.d.ts +0 -23
- package/lib/Util/Constants.d.ts +0 -8
- package/lib/Util/DateUtils.d.ts +0 -7
- package/lib/Util/Mutex.d.ts +0 -45
- package/lib/Util/Reachability.d.ts +0 -13
- package/lib/Util/Reachability.native.d.ts +0 -11
- package/lib/Util/Retry.d.ts +0 -20
- package/lib/Util/StringUtils.d.ts +0 -2
- package/lib/Util/index.d.ts +0 -6
- package/lib/constants.d.ts +0 -6
- package/lib/index.d.ts +0 -30
- package/lib/types/index.d.ts +0 -1
- package/lib/types/types.d.ts +0 -43
package/src/Credentials.ts
CHANGED
|
@@ -187,7 +187,7 @@ export class CredentialsClass {
|
|
|
187
187
|
const MAX_DELAY_MS = 10 * 1000;
|
|
188
188
|
// refreshHandler will retry network errors, otherwise it will
|
|
189
189
|
// return NonRetryableError to break out of jitteredExponentialRetry
|
|
190
|
-
return jitteredExponentialRetry(refreshHandler, [], MAX_DELAY_MS)
|
|
190
|
+
return jitteredExponentialRetry<any>(refreshHandler, [], MAX_DELAY_MS)
|
|
191
191
|
.then(data => {
|
|
192
192
|
logger.debug('refresh federated token sucessfully', data);
|
|
193
193
|
return this._setCredentialsFromFederation({
|
|
@@ -256,7 +256,7 @@ export class CredentialsClass {
|
|
|
256
256
|
);
|
|
257
257
|
}
|
|
258
258
|
|
|
259
|
-
const identityId = this._identityId = await this._getGuestIdentityId();
|
|
259
|
+
const identityId = (this._identityId = await this._getGuestIdentityId());
|
|
260
260
|
|
|
261
261
|
const cognitoClient = new CognitoIdentityClient({
|
|
262
262
|
region,
|
|
@@ -446,19 +446,14 @@ export class CredentialsClass {
|
|
|
446
446
|
}
|
|
447
447
|
|
|
448
448
|
const {
|
|
449
|
-
Credentials: {
|
|
450
|
-
AccessKeyId,
|
|
451
|
-
Expiration,
|
|
452
|
-
SecretKey,
|
|
453
|
-
SessionToken,
|
|
454
|
-
},
|
|
449
|
+
Credentials: { AccessKeyId, Expiration, SecretKey, SessionToken },
|
|
455
450
|
// single source of truth for the primary identity associated with the logins
|
|
456
451
|
// only if a guest identity is used for a first-time user, that guest identity will become its primary identity
|
|
457
452
|
IdentityId: primaryIdentityId,
|
|
458
453
|
} = await cognitoClient.send(
|
|
459
454
|
new GetCredentialsForIdentityCommand({
|
|
460
|
-
|
|
461
|
-
|
|
455
|
+
IdentityId: guestIdentityId || generatedOrRetrievedIdentityId,
|
|
456
|
+
Logins: logins,
|
|
462
457
|
})
|
|
463
458
|
);
|
|
464
459
|
|
|
@@ -466,9 +461,13 @@ export class CredentialsClass {
|
|
|
466
461
|
if (guestIdentityId) {
|
|
467
462
|
// if guestIdentity is found and used by GetCredentialsForIdentity
|
|
468
463
|
// it will be linked to the logins provided, and disqualified as an unauth identity
|
|
469
|
-
logger.debug(
|
|
464
|
+
logger.debug(
|
|
465
|
+
`The guest identity ${guestIdentityId} has been successfully linked to the logins`
|
|
466
|
+
);
|
|
470
467
|
if (guestIdentityId === primaryIdentityId) {
|
|
471
|
-
logger.debug(
|
|
468
|
+
logger.debug(
|
|
469
|
+
`The guest identity ${guestIdentityId} has become the primary identity`
|
|
470
|
+
);
|
|
472
471
|
}
|
|
473
472
|
// remove it from local storage to avoid being used as a guest Identity by _setCredentialsForGuest
|
|
474
473
|
await this._removeGuestIdentityId();
|
|
@@ -481,7 +480,7 @@ export class CredentialsClass {
|
|
|
481
480
|
sessionToken: SessionToken,
|
|
482
481
|
expiration: Expiration,
|
|
483
482
|
identityId: primaryIdentityId,
|
|
484
|
-
|
|
483
|
+
};
|
|
485
484
|
};
|
|
486
485
|
|
|
487
486
|
const credentials = credentialsProvider().catch(async err => {
|
|
@@ -587,7 +586,7 @@ export class CredentialsClass {
|
|
|
587
586
|
await this._storageSync;
|
|
588
587
|
this._storage.setItem(
|
|
589
588
|
this._getCognitoIdentityIdStorageKey(identityPoolId),
|
|
590
|
-
identityId
|
|
589
|
+
identityId
|
|
591
590
|
);
|
|
592
591
|
} catch (e) {
|
|
593
592
|
logger.debug('Failed to cache guest identityId', e);
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private For internal Amplify use.
|
|
3
|
+
*
|
|
4
|
+
* Creates a new scope for promises, observables, and other types of work or
|
|
5
|
+
* processes that may be running in the background. This manager provides
|
|
6
|
+
* an singular entrypoint to request termination and await completion.
|
|
7
|
+
*
|
|
8
|
+
* As work completes on its own prior to close, the manager removes them
|
|
9
|
+
* from the registry to avoid holding references to completed jobs.
|
|
10
|
+
*/
|
|
11
|
+
export class BackgroundProcessManager {
|
|
12
|
+
/**
|
|
13
|
+
* A string indicating whether the manager is accepting new work ("Open"),
|
|
14
|
+
* waiting for work to complete ("Closing"), or fully done with all
|
|
15
|
+
* submitted work and *not* accepting new jobs ("Closed").
|
|
16
|
+
*/
|
|
17
|
+
private _state = BackgroundProcessManagerState.Open;
|
|
18
|
+
|
|
19
|
+
private _closingPromise: Promise<PromiseSettledResult<any>[]> | undefined;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The list of outstanding jobs we'll need to wait for upon `close()`
|
|
23
|
+
*/
|
|
24
|
+
private jobs = new Set<JobEntry>();
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Creates a new manager for promises, observables, and other types
|
|
28
|
+
* of work that may be running in the background. This manager provides
|
|
29
|
+
* a centralized mechanism to request termination and await completion.
|
|
30
|
+
*/
|
|
31
|
+
constructor() {}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Executes an async `job` function, passing the return value through to
|
|
35
|
+
* the caller, registering it as a running job in the manager. When the
|
|
36
|
+
* manager *closes*, it will `await` the job.
|
|
37
|
+
*
|
|
38
|
+
* @param job The function to execute.
|
|
39
|
+
* @param description Optional description to help identify pending jobs.
|
|
40
|
+
* @returns The return value from the given function.
|
|
41
|
+
*/
|
|
42
|
+
add<T>(job: () => Promise<T>, description?: string): Promise<T>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Executes an async `job` function, passing the return value through to
|
|
46
|
+
* the caller, registering it as a running job in the manager. When the
|
|
47
|
+
* manager *closes*, it will request termination by resolving the
|
|
48
|
+
* provided `onTerminate` promise. It will then `await` the job, so it is
|
|
49
|
+
* important that the job still `resolve()` or `reject()` when responding
|
|
50
|
+
* to a termination request.
|
|
51
|
+
*
|
|
52
|
+
* @param job The function to execute.
|
|
53
|
+
* @param description Optional description to help identify pending jobs.
|
|
54
|
+
* @returns The return value from the given function.
|
|
55
|
+
*/
|
|
56
|
+
add<T>(
|
|
57
|
+
job: (onTerminate: Promise<void>) => Promise<T>,
|
|
58
|
+
description?: string
|
|
59
|
+
): Promise<T>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Create a no-op job, registers it with the manager, and returns hooks
|
|
63
|
+
* to the caller to signal the job's completion and respond to termination
|
|
64
|
+
* requests.
|
|
65
|
+
*
|
|
66
|
+
* When the manager closes, the no-op job will be `await`-ed, so its
|
|
67
|
+
* important to always `resolve()` or `reject()` when done responding to an
|
|
68
|
+
* `onTerminate` signal.
|
|
69
|
+
* @param description Optional description to help identify pending jobs.
|
|
70
|
+
* @returns Job promise hooks + onTerminate signaling promise
|
|
71
|
+
*/
|
|
72
|
+
add(description?: string): {
|
|
73
|
+
resolve: (value?: unknown) => void;
|
|
74
|
+
reject: (reason?: any) => void;
|
|
75
|
+
onTerminate: Promise<void>;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Adds another job manager to await on at the time of closing. the inner
|
|
80
|
+
* manager's termination is signaled when this manager's `close()` is
|
|
81
|
+
* called for.
|
|
82
|
+
*
|
|
83
|
+
* @param job The inner job manager to await.
|
|
84
|
+
* @param description Optional description to help identify pending jobs.
|
|
85
|
+
*/
|
|
86
|
+
add(job: BackgroundProcessManager, description?: string);
|
|
87
|
+
|
|
88
|
+
add(jobOrDescription?, optionalDescription?) {
|
|
89
|
+
let job;
|
|
90
|
+
let description: string;
|
|
91
|
+
|
|
92
|
+
if (typeof jobOrDescription === 'string') {
|
|
93
|
+
job = undefined;
|
|
94
|
+
description = jobOrDescription;
|
|
95
|
+
} else {
|
|
96
|
+
job = jobOrDescription;
|
|
97
|
+
description = optionalDescription;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const error = this.closedFailure(description);
|
|
101
|
+
if (error) return error;
|
|
102
|
+
|
|
103
|
+
if (job === undefined) {
|
|
104
|
+
return this.addHook(description);
|
|
105
|
+
} else if (typeof job === 'function') {
|
|
106
|
+
return this.addFunction(job, description);
|
|
107
|
+
} else if (job instanceof BackgroundProcessManager) {
|
|
108
|
+
return this.addManager(job, description);
|
|
109
|
+
} else {
|
|
110
|
+
throw new Error(
|
|
111
|
+
'If `job` is provided, it must be an Observable, Function, or BackgroundProcessManager.'
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Adds a **cleaner** function that doesn't immediately get executed.
|
|
118
|
+
* Instead, the caller gets a **terminate** function back. The *cleaner* is
|
|
119
|
+
* invoked only once the mananger *closes* or the returned **terminate**
|
|
120
|
+
* function is called.
|
|
121
|
+
*
|
|
122
|
+
* @param clean The cleanup function.
|
|
123
|
+
* @param description Optional description to help identify pending jobs.
|
|
124
|
+
* @returns A terminate function.
|
|
125
|
+
*/
|
|
126
|
+
addCleaner<T>(
|
|
127
|
+
clean: () => Promise<T>,
|
|
128
|
+
description?: string
|
|
129
|
+
): () => Promise<void> {
|
|
130
|
+
const { resolve, onTerminate } = this.addHook(description);
|
|
131
|
+
|
|
132
|
+
const proxy = async () => {
|
|
133
|
+
await clean();
|
|
134
|
+
resolve();
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
onTerminate.then(proxy);
|
|
138
|
+
|
|
139
|
+
return proxy;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private addFunction<T>(
|
|
143
|
+
job: () => Promise<T>,
|
|
144
|
+
description?: string
|
|
145
|
+
): Promise<T>;
|
|
146
|
+
private addFunction<T>(
|
|
147
|
+
job: (onTerminate: Promise<void>) => Promise<T>,
|
|
148
|
+
description?: string
|
|
149
|
+
): Promise<T>;
|
|
150
|
+
private addFunction(job, description) {
|
|
151
|
+
// the function we call when we want to try to terminate this job.
|
|
152
|
+
let terminate;
|
|
153
|
+
|
|
154
|
+
// the promise the job can opt into listening to for termination.
|
|
155
|
+
const onTerminate = new Promise(resolve => {
|
|
156
|
+
terminate = resolve;
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// finally! start the job.
|
|
160
|
+
const jobResult = job(onTerminate);
|
|
161
|
+
|
|
162
|
+
// depending on what the job gives back, register the result
|
|
163
|
+
// so we can monitor for completion.
|
|
164
|
+
if (typeof jobResult?.then === 'function') {
|
|
165
|
+
this.registerPromise(jobResult, terminate, description);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// At the end of the day, or you know, method call, it doesn't matter
|
|
169
|
+
// what the return value is at all; we just pass it through to the
|
|
170
|
+
// caller.
|
|
171
|
+
return jobResult;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private addManager(manager: BackgroundProcessManager, description?: string) {
|
|
175
|
+
this.addCleaner(async () => await manager.close(), description);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Creates and registers a fabricated job for processes that need to operate
|
|
180
|
+
* with callbacks/hooks. The returned `resolve` and `reject`
|
|
181
|
+
* functions can be used to signal the job is done successfully or not.
|
|
182
|
+
* The returned `onTerminate` is a promise that will resolve when the
|
|
183
|
+
* manager is requesting the termination of the job.
|
|
184
|
+
*
|
|
185
|
+
* @param description Optional description to help identify pending jobs.
|
|
186
|
+
* @returns `{ resolve, reject, onTerminate }`
|
|
187
|
+
*/
|
|
188
|
+
private addHook(description?: string) {
|
|
189
|
+
// the resolve/reject functions we'll provide to the caller to signal
|
|
190
|
+
// the state of the job.
|
|
191
|
+
let resolve!: (value?: unknown) => void;
|
|
192
|
+
let reject!: (reason?: any) => void;
|
|
193
|
+
|
|
194
|
+
// the underlying promise we'll use to manage it, pretty much like
|
|
195
|
+
// any other promise.
|
|
196
|
+
const promise = new Promise((res, rej) => {
|
|
197
|
+
resolve = res;
|
|
198
|
+
reject = rej;
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// the function we call when we want to try to terminate this job.
|
|
202
|
+
let terminate;
|
|
203
|
+
|
|
204
|
+
// the promise the job can opt into listening to for termination.
|
|
205
|
+
const onTerminate = new Promise(resolveTerminate => {
|
|
206
|
+
terminate = resolveTerminate;
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
this.registerPromise(promise, terminate, description);
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
resolve,
|
|
213
|
+
reject,
|
|
214
|
+
onTerminate,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Adds a Promise based job to the list of jobs for monitoring and listens
|
|
220
|
+
* for either a success or failure, upon which the job is considered "done"
|
|
221
|
+
* and removed from the registry.
|
|
222
|
+
*
|
|
223
|
+
* @param promise A promise that is on its way to being returned to a
|
|
224
|
+
* caller, which needs to be tracked as a background job.
|
|
225
|
+
* @param terminate The termination function to register, which can be
|
|
226
|
+
* invoked to request the job stop.
|
|
227
|
+
* @param description Optional description to help identify pending jobs.
|
|
228
|
+
*/
|
|
229
|
+
private registerPromise<T extends Promise<any>>(
|
|
230
|
+
promise: T,
|
|
231
|
+
terminate: () => void,
|
|
232
|
+
description?: string
|
|
233
|
+
) {
|
|
234
|
+
const jobEntry = { promise, terminate, description };
|
|
235
|
+
this.jobs.add(jobEntry);
|
|
236
|
+
|
|
237
|
+
// in all of my testing, it is safe to multi-subscribe to a promise.
|
|
238
|
+
// so, rather than create another layer of promising, we're just going
|
|
239
|
+
// to hook into the promise we already have, and when it's done
|
|
240
|
+
// (successfully or not), we no longer need to wait for it upon close.
|
|
241
|
+
|
|
242
|
+
//
|
|
243
|
+
// sorry this is a bit hand-wavy:
|
|
244
|
+
//
|
|
245
|
+
// i believe we use `.then` and `.catch` instead of `.finally` because
|
|
246
|
+
// `.finally` is invoked in a different order in the sequence, and this
|
|
247
|
+
// breaks assumptions throughout and causes failures.
|
|
248
|
+
promise
|
|
249
|
+
.then(() => {
|
|
250
|
+
this.jobs.delete(jobEntry);
|
|
251
|
+
})
|
|
252
|
+
.catch(() => {
|
|
253
|
+
this.jobs.delete(jobEntry);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The number of jobs being waited on.
|
|
259
|
+
*
|
|
260
|
+
* We don't use this for anything. It's just informational for the caller,
|
|
261
|
+
* and can be used in logging and testing.
|
|
262
|
+
*
|
|
263
|
+
* @returns the number of jobs.
|
|
264
|
+
*/
|
|
265
|
+
get length() {
|
|
266
|
+
return this.jobs.size;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The execution state of the manager. One of:
|
|
271
|
+
*
|
|
272
|
+
* 1. "Open" -> Accepting new jobs
|
|
273
|
+
* 1. "Closing" -> Not accepting new work. Waiting for jobs to complete.
|
|
274
|
+
* 1. "Closed" -> Not accepting new work. All submitted jobs are complete.
|
|
275
|
+
*/
|
|
276
|
+
get state() {
|
|
277
|
+
return this._state;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* The registered `description` of all still-pending jobs.
|
|
282
|
+
*
|
|
283
|
+
* @returns descriptions as an array.
|
|
284
|
+
*/
|
|
285
|
+
get pending() {
|
|
286
|
+
return Array.from(this.jobs).map(job => job.description);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Whether the manager is accepting new jobs.
|
|
291
|
+
*/
|
|
292
|
+
get isOpen() {
|
|
293
|
+
return this._state === BackgroundProcessManagerState.Open;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Whether the manager is rejecting new work, but still waiting for
|
|
298
|
+
* submitted work to complete.
|
|
299
|
+
*/
|
|
300
|
+
get isClosing() {
|
|
301
|
+
return this._state === BackgroundProcessManagerState.Closing;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Whether the manager is rejecting work and done waiting for submitted
|
|
306
|
+
* work to complete.
|
|
307
|
+
*/
|
|
308
|
+
get isClosed() {
|
|
309
|
+
return this._state === BackgroundProcessManagerState.Closed;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private closedFailure(description: string) {
|
|
313
|
+
if (!this.isOpen) {
|
|
314
|
+
return Promise.reject(
|
|
315
|
+
new BackgroundManagerNotOpenError(
|
|
316
|
+
[
|
|
317
|
+
`The manager is ${this.state}.`,
|
|
318
|
+
`You tried to add "${description}".`,
|
|
319
|
+
`Pending jobs: [\n${this.pending
|
|
320
|
+
.map(t => ' ' + t)
|
|
321
|
+
.join(',\n')}\n]`,
|
|
322
|
+
].join('\n')
|
|
323
|
+
)
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Signals jobs to stop (for those that accept interruptions) and waits
|
|
330
|
+
* for confirmation that jobs have stopped.
|
|
331
|
+
*
|
|
332
|
+
* This immediately puts the manager into a closing state and just begins
|
|
333
|
+
* to reject new work. After all work in the manager is complete, the
|
|
334
|
+
* manager goes into a `Completed` state and `close()` returns.
|
|
335
|
+
*
|
|
336
|
+
* This call is idempotent.
|
|
337
|
+
*
|
|
338
|
+
* If the manager is already closing or closed, `finalCleaup` is not executed.
|
|
339
|
+
*
|
|
340
|
+
* @param onClosed
|
|
341
|
+
* @returns The settled results of each still-running job's promise. If the
|
|
342
|
+
* manager is already closed, this will contain the results as of when the
|
|
343
|
+
* manager's `close()` was called in an `Open` state.
|
|
344
|
+
*/
|
|
345
|
+
async close() {
|
|
346
|
+
if (this.isOpen) {
|
|
347
|
+
this._state = BackgroundProcessManagerState.Closing;
|
|
348
|
+
for (const job of Array.from(this.jobs)) {
|
|
349
|
+
try {
|
|
350
|
+
job.terminate();
|
|
351
|
+
} catch (error) {
|
|
352
|
+
// Due to potential races with a job's natural completion, it's
|
|
353
|
+
// reasonable to expect the termination call to fail. Hence,
|
|
354
|
+
// not logging as an error.
|
|
355
|
+
console.warn(
|
|
356
|
+
`Failed to send termination signal to job. Error: ${error.message}`,
|
|
357
|
+
job
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Use `allSettled()` because we want to wait for all to finish. We do
|
|
363
|
+
// not want to stop waiting if there is a failure.
|
|
364
|
+
this._closingPromise = Promise.allSettled(
|
|
365
|
+
Array.from(this.jobs).map(j => j.promise)
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
await this._closingPromise;
|
|
369
|
+
this._state = BackgroundProcessManagerState.Closed;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return this._closingPromise;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Signals the manager to start accepting work (again) and returns once
|
|
377
|
+
* the manager is ready to do so.
|
|
378
|
+
*
|
|
379
|
+
* If the state is already `Open`, this call is a no-op.
|
|
380
|
+
*
|
|
381
|
+
* If the state is `Closed`, this call simply updates state and returns.
|
|
382
|
+
*
|
|
383
|
+
* If the state is `Closing`, this call waits for completion before it
|
|
384
|
+
* updates the state and returns.
|
|
385
|
+
*/
|
|
386
|
+
async open() {
|
|
387
|
+
if (this.isClosing) {
|
|
388
|
+
await this.close();
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
this._state = BackgroundProcessManagerState.Open;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
*
|
|
397
|
+
*/
|
|
398
|
+
export class BackgroundManagerNotOpenError extends Error {
|
|
399
|
+
constructor(message: string) {
|
|
400
|
+
super(`BackgroundManagerNotOpenError: ${message}`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* All possible states a `BackgroundProcessManager` instance can be in.
|
|
406
|
+
*/
|
|
407
|
+
export enum BackgroundProcessManagerState {
|
|
408
|
+
/**
|
|
409
|
+
* Accepting new jobs.
|
|
410
|
+
*/
|
|
411
|
+
Open = 'Open',
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Not accepting new jobs. Waiting for submitted jobs to complete.
|
|
415
|
+
*/
|
|
416
|
+
Closing = 'Closing',
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Not accepting new jobs. All submitted jobs are complete.
|
|
420
|
+
*/
|
|
421
|
+
Closed = 'Closed',
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Completely internal to `BackgroundProcessManager`, and describes the structure of
|
|
426
|
+
* an entry in the jobs registry.
|
|
427
|
+
*/
|
|
428
|
+
type JobEntry = {
|
|
429
|
+
/**
|
|
430
|
+
* The underlying promise provided by the job function to wait for.
|
|
431
|
+
*/
|
|
432
|
+
promise: Promise<any>;
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Request the termination of the job.
|
|
436
|
+
*/
|
|
437
|
+
terminate: () => void;
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* An object provided by the caller that can be used to identify the description
|
|
441
|
+
* of the job, which can otherwise be unclear from the `promise` and
|
|
442
|
+
* `terminate` function. The `description` can be a string. (May be extended
|
|
443
|
+
* later to also support object refs.)
|
|
444
|
+
*
|
|
445
|
+
* Useful for troubleshooting why a manager is waiting for long periods of time
|
|
446
|
+
* on `close()`.
|
|
447
|
+
*/
|
|
448
|
+
description?: string;
|
|
449
|
+
};
|
package/src/Util/Retry.ts
CHANGED
|
@@ -18,41 +18,74 @@ const isNonRetryableError = (obj: any): obj is NonRetryableError => {
|
|
|
18
18
|
* @private
|
|
19
19
|
* Internal use of Amplify only
|
|
20
20
|
*/
|
|
21
|
-
export async function retry(
|
|
22
|
-
functionToRetry:
|
|
21
|
+
export async function retry<T>(
|
|
22
|
+
functionToRetry: (...args: any[]) => T,
|
|
23
23
|
args: any[],
|
|
24
24
|
delayFn: DelayFunction,
|
|
25
|
-
|
|
26
|
-
) {
|
|
25
|
+
onTerminate?: Promise<void>
|
|
26
|
+
): Promise<T> {
|
|
27
27
|
if (typeof functionToRetry !== 'function') {
|
|
28
28
|
throw Error('functionToRetry must be a function');
|
|
29
29
|
}
|
|
30
|
-
logger.debug(
|
|
31
|
-
`${
|
|
32
|
-
functionToRetry.name
|
|
33
|
-
} attempt #${attempt} with this vars: ${JSON.stringify(args)}`
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
return await functionToRetry(...args);
|
|
38
|
-
} catch (err) {
|
|
39
|
-
logger.debug(`error on ${functionToRetry.name}`, err);
|
|
40
|
-
|
|
41
|
-
if (isNonRetryableError(err)) {
|
|
42
|
-
logger.debug(`${functionToRetry.name} non retryable error`, err);
|
|
43
|
-
throw err;
|
|
44
|
-
}
|
|
45
30
|
|
|
46
|
-
|
|
47
|
-
|
|
31
|
+
return new Promise(async (resolve, reject) => {
|
|
32
|
+
let attempt = 0;
|
|
33
|
+
let terminated = false;
|
|
34
|
+
let timeout: any;
|
|
35
|
+
let wakeUp: any = () => {}; // will be replaced with a resolver()
|
|
36
|
+
|
|
37
|
+
// used after the loop if terminated while waiting for a timer.
|
|
38
|
+
let lastError: Error;
|
|
39
|
+
|
|
40
|
+
onTerminate &&
|
|
41
|
+
onTerminate.then(() => {
|
|
42
|
+
// signal not to try anymore.
|
|
43
|
+
terminated = true;
|
|
44
|
+
|
|
45
|
+
// stop sleeping if we're sleeping.
|
|
46
|
+
clearTimeout(timeout);
|
|
47
|
+
wakeUp();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
while (!terminated) {
|
|
51
|
+
attempt++;
|
|
52
|
+
|
|
53
|
+
logger.debug(
|
|
54
|
+
`${
|
|
55
|
+
functionToRetry.name
|
|
56
|
+
} attempt #${attempt} with this vars: ${JSON.stringify(args)}`
|
|
57
|
+
);
|
|
48
58
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
try {
|
|
60
|
+
return resolve(await functionToRetry(...args));
|
|
61
|
+
} catch (err) {
|
|
62
|
+
lastError = err;
|
|
63
|
+
logger.debug(`error on ${functionToRetry.name}`, err);
|
|
64
|
+
|
|
65
|
+
if (isNonRetryableError(err)) {
|
|
66
|
+
logger.debug(`${functionToRetry.name} non retryable error`, err);
|
|
67
|
+
return reject(err);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const retryIn = delayFn(attempt, args, err);
|
|
71
|
+
logger.debug(`${functionToRetry.name} retrying in ${retryIn} ms`);
|
|
72
|
+
|
|
73
|
+
// we check `terminated` again here because it could have flipped
|
|
74
|
+
// in the time it took `functionToRetry` to return.
|
|
75
|
+
if (retryIn === false || terminated) {
|
|
76
|
+
return reject(err);
|
|
77
|
+
} else {
|
|
78
|
+
await new Promise(r => {
|
|
79
|
+
wakeUp = r; // export wakeUp for onTerminate handling
|
|
80
|
+
timeout = setTimeout(wakeUp, retryIn);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
54
84
|
}
|
|
55
|
-
|
|
85
|
+
|
|
86
|
+
// reached if terminated while waiting for a timer.
|
|
87
|
+
reject(lastError);
|
|
88
|
+
});
|
|
56
89
|
}
|
|
57
90
|
|
|
58
91
|
const MAX_DELAY_MS = 5 * 60 * 1000;
|
|
@@ -77,8 +110,10 @@ export function jitteredBackoff(
|
|
|
77
110
|
* @private
|
|
78
111
|
* Internal use of Amplify only
|
|
79
112
|
*/
|
|
80
|
-
export const jitteredExponentialRetry = (
|
|
81
|
-
functionToRetry:
|
|
113
|
+
export const jitteredExponentialRetry = <T>(
|
|
114
|
+
functionToRetry: (...args: any[]) => T,
|
|
82
115
|
args: any[],
|
|
83
|
-
maxDelayMs: number = MAX_DELAY_MS
|
|
84
|
-
|
|
116
|
+
maxDelayMs: number = MAX_DELAY_MS,
|
|
117
|
+
onTerminate?: Promise<void>
|
|
118
|
+
): Promise<T> =>
|
|
119
|
+
retry(functionToRetry, args, jitteredBackoff(maxDelayMs), onTerminate);
|
package/src/Util/index.ts
CHANGED
package/lib/Amplify.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { ConsoleLogger as LoggerClass } from './Logger';
|
|
2
|
-
export declare class AmplifyClass {
|
|
3
|
-
private _components;
|
|
4
|
-
private _config;
|
|
5
|
-
private _modules;
|
|
6
|
-
Auth: any;
|
|
7
|
-
Analytics: any;
|
|
8
|
-
API: any;
|
|
9
|
-
Credentials: any;
|
|
10
|
-
Storage: any;
|
|
11
|
-
I18n: any;
|
|
12
|
-
Cache: any;
|
|
13
|
-
PubSub: any;
|
|
14
|
-
Interactions: any;
|
|
15
|
-
Pushnotification: any;
|
|
16
|
-
UI: any;
|
|
17
|
-
XR: any;
|
|
18
|
-
Predictions: any;
|
|
19
|
-
DataStore: any;
|
|
20
|
-
Geo: any;
|
|
21
|
-
Logger: typeof LoggerClass;
|
|
22
|
-
ServiceWorker: any;
|
|
23
|
-
register(comp: any): void;
|
|
24
|
-
configure(config?: any): {};
|
|
25
|
-
addPluggable(pluggable: any): void;
|
|
26
|
-
}
|
|
27
|
-
export declare const Amplify: AmplifyClass;
|
|
28
|
-
/**
|
|
29
|
-
* @deprecated use named import
|
|
30
|
-
*/
|
|
31
|
-
export default Amplify;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export declare function clientInfo(): {
|
|
2
|
-
platform?: undefined;
|
|
3
|
-
make?: undefined;
|
|
4
|
-
model?: undefined;
|
|
5
|
-
version?: undefined;
|
|
6
|
-
appVersion?: undefined;
|
|
7
|
-
language?: undefined;
|
|
8
|
-
timezone?: undefined;
|
|
9
|
-
} | {
|
|
10
|
-
platform: string;
|
|
11
|
-
make: string;
|
|
12
|
-
model: string;
|
|
13
|
-
version: string;
|
|
14
|
-
appVersion: string;
|
|
15
|
-
language: string;
|
|
16
|
-
timezone: string;
|
|
17
|
-
};
|
|
18
|
-
export declare function dimension(): {
|
|
19
|
-
width: number;
|
|
20
|
-
height: number;
|
|
21
|
-
};
|
|
22
|
-
export declare function browserType(userAgent: string): {
|
|
23
|
-
type: string;
|
|
24
|
-
version: string;
|
|
25
|
-
};
|