mercury-agent 0.8.6 → 0.8.7
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/package.json +1 -1
- package/src/storage/pi-auth.ts +23 -0
package/package.json
CHANGED
package/src/storage/pi-auth.ts
CHANGED
|
@@ -68,6 +68,10 @@ export type PiAuthCredential =
|
|
|
68
68
|
/** An oauth entry exists but could not be turned into a usable key. */
|
|
69
69
|
| { status: "refresh-failed"; error?: Error };
|
|
70
70
|
|
|
71
|
+
// Deduplicate concurrent OAuth refreshes — refresh tokens are single-use, so
|
|
72
|
+
// two parallel calls with the same token race and one always fails.
|
|
73
|
+
const inflightRefresh = new Map<string, Promise<PiAuthCredential>>();
|
|
74
|
+
|
|
71
75
|
export async function getPiAuthCredential(options: {
|
|
72
76
|
provider: string;
|
|
73
77
|
authPath: string;
|
|
@@ -83,6 +87,25 @@ export async function getPiAuthCredential(options: {
|
|
|
83
87
|
return { status: "none" };
|
|
84
88
|
}
|
|
85
89
|
|
|
90
|
+
// Coalesce concurrent refreshes for the same auth file so only one
|
|
91
|
+
// token-endpoint call is made; the rest share its result.
|
|
92
|
+
const key = options.authPath;
|
|
93
|
+
const existing = inflightRefresh.get(key);
|
|
94
|
+
if (existing) return existing;
|
|
95
|
+
|
|
96
|
+
const promise = doGetPiAuthCredential(options);
|
|
97
|
+
inflightRefresh.set(key, promise);
|
|
98
|
+
try {
|
|
99
|
+
return await promise;
|
|
100
|
+
} finally {
|
|
101
|
+
inflightRefresh.delete(key);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function doGetPiAuthCredential(options: {
|
|
106
|
+
provider: string;
|
|
107
|
+
authPath: string;
|
|
108
|
+
}): Promise<PiAuthCredential> {
|
|
86
109
|
const authPath = options.authPath;
|
|
87
110
|
const auth = readAuthFile(authPath);
|
|
88
111
|
|