@open-webapp/drive-sync 0.4.0 → 0.4.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/http.js +14 -1
- package/package.json +1 -1
package/dist/http.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { acquireToken } from './token.js';
|
|
2
2
|
import { getConnection, refreshSilently } from './connection.js';
|
|
3
|
-
import { clearToken } from './storage.js';
|
|
3
|
+
import { clearToken, getToken } from './storage.js';
|
|
4
4
|
import { DriveSyncError, NeedsReauthError, ScopeInsufficientError, NotFoundError, RateLimitedError, TransientError, WrongAccountError, } from './errors.js';
|
|
5
5
|
const MAX_ATTEMPTS = 3;
|
|
6
6
|
const BASE_DELAY_MS = 500;
|
|
7
|
+
/** Mirrors connection.ts's own buffer: a cached token this close to expiry is treated as unusable. */
|
|
8
|
+
const TOKEN_REUSE_BUFFER_MS = 5 * 60 * 1000;
|
|
7
9
|
function isRetryableStatus(status) {
|
|
8
10
|
return status === 429 || (status >= 500 && status <= 599);
|
|
9
11
|
}
|
|
@@ -40,6 +42,17 @@ async function sleep(ms) {
|
|
|
40
42
|
export async function driveFetch(opts) {
|
|
41
43
|
const { appId, projectId, clientId, requiredScopes, logger } = opts;
|
|
42
44
|
const interactive = !!opts.interactive;
|
|
45
|
+
// Reuse a still-valid cached token as-is on the non-interactive path,
|
|
46
|
+
// exactly like connection.ts's getAccessToken() already does — without
|
|
47
|
+
// this check every single Drive call (not just an actual token expiry)
|
|
48
|
+
// forced its own live, non-interactive GIS round-trip via acquireToken
|
|
49
|
+
// below, even when the cached token had plenty of life left.
|
|
50
|
+
if (!interactive) {
|
|
51
|
+
const cached = await getToken(appId, projectId);
|
|
52
|
+
if (cached && cached.expiresAt > Date.now() + TOKEN_REUSE_BUFFER_MS) {
|
|
53
|
+
return performFetch(opts, cached.accessToken, /* isRetryAfter401 */ false);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
43
56
|
// Resolve a hint email from the current connection (if any) so a
|
|
44
57
|
// non-interactive silent refresh can target the right account. If there is
|
|
45
58
|
// no connection at all and interactive is false, acquireToken's silent
|