mcp-compress-router 4.0.2 → 4.0.3
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/build/services/auth-status.js +31 -13
- package/build/utils/atomic-write.js +47 -1
- package/build/utils/file-lock.js +76 -15
- package/package.json +1 -1
|
@@ -92,7 +92,10 @@ function hasAuthorizationHeader(headers) {
|
|
|
92
92
|
* the entry instead of being overwritten by a stale snapshot. Each
|
|
93
93
|
* entry's server-URL binding is backfilled from the probed server when it
|
|
94
94
|
* has none yet; an existing binding is preserved so a URL change never
|
|
95
|
-
* re-binds old credentials to the new server.
|
|
95
|
+
* re-binds old credentials to the new server. A cache write that fails
|
|
96
|
+
* (for example, the file is momentarily locked on Windows) is logged and
|
|
97
|
+
* skipped: this cache only feeds the `list` command, so it must never
|
|
98
|
+
* abort startup.
|
|
96
99
|
*
|
|
97
100
|
* @param configPath - Absolute path to the mcp.json file.
|
|
98
101
|
* @param servers - Typed downstream server configs.
|
|
@@ -109,18 +112,33 @@ export async function persistAuthRequirements(configPath, servers, logger) {
|
|
|
109
112
|
const checkedAt = new Date().toISOString();
|
|
110
113
|
// Each entry is updated from its current on-disk state under the
|
|
111
114
|
// credentials lock, preserving anything written in the meantime.
|
|
115
|
+
const cached = {};
|
|
112
116
|
for (const { name, requirement, serverUrl } of results) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
117
|
+
try {
|
|
118
|
+
await mutateCredentials(configPath, name, (current) => {
|
|
119
|
+
const boundUrl = resolveBindingUrl(current, serverUrl);
|
|
120
|
+
return {
|
|
121
|
+
...current,
|
|
122
|
+
...(boundUrl !== undefined ? { serverUrl: boundUrl } : {}),
|
|
123
|
+
authRequirement: requirement,
|
|
124
|
+
checkedAt,
|
|
125
|
+
};
|
|
126
|
+
}, logger);
|
|
127
|
+
cached[name] = requirement;
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
// This cache only feeds the `list` command's auth column, so a
|
|
131
|
+
// failed write MUST NOT abort startup and take every downstream
|
|
132
|
+
// server down with it. There is one store behind every entry, so
|
|
133
|
+
// the remaining writes would fail the same way: stop after the
|
|
134
|
+
// first one. A damaged store still surfaces when the tokens are
|
|
135
|
+
// read for an actual request.
|
|
136
|
+
logger.warn(`Failed to cache auth requirement for "${name}"`, {
|
|
137
|
+
server: name,
|
|
138
|
+
error: err instanceof Error ? err.message : String(err),
|
|
139
|
+
});
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
122
142
|
}
|
|
123
|
-
logger.debug('Cached auth requirements', {
|
|
124
|
-
servers: Object.fromEntries(results.map((r) => [r.name, r.requirement])),
|
|
125
|
-
});
|
|
143
|
+
logger.debug('Cached auth requirements', { servers: cached });
|
|
126
144
|
}
|
|
@@ -1,4 +1,46 @@
|
|
|
1
1
|
import * as fs from 'node:fs/promises';
|
|
2
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
3
|
+
/**
|
|
4
|
+
* Error codes a rename returns when the target is briefly held open by
|
|
5
|
+
* another process (a scanner, a backup agent, or a process that has just
|
|
6
|
+
* released the name). On Windows this is how a momentarily locked file
|
|
7
|
+
* looks: EPERM or EACCES rather than a wait, plus EBUSY for a scanner.
|
|
8
|
+
*/
|
|
9
|
+
const TRANSIENT_RENAME_ERROR_CODES = new Set(['EPERM', 'EACCES', 'EBUSY']);
|
|
10
|
+
/** Total time (ms) to keep retrying a transiently refused rename. */
|
|
11
|
+
const RENAME_RETRY_BUDGET_MS = 2_000;
|
|
12
|
+
/** Delay (ms) between rename attempts. */
|
|
13
|
+
const RENAME_RETRY_DELAY_MS = 25;
|
|
14
|
+
/**
|
|
15
|
+
* Renames the temporary file over the target, retrying transient
|
|
16
|
+
* failures.
|
|
17
|
+
*
|
|
18
|
+
* A momentary external lock must not surface as a failed write: these
|
|
19
|
+
* files are runtime state, and a refusal here used to abort the whole
|
|
20
|
+
* router startup. A real permission problem still fails once the retry
|
|
21
|
+
* budget is spent.
|
|
22
|
+
*
|
|
23
|
+
* @param tempPath - The fully written temporary sibling file.
|
|
24
|
+
* @param filePath - The destination file.
|
|
25
|
+
* @throws The last rename error when it is not transient, or when the
|
|
26
|
+
* retry budget is spent.
|
|
27
|
+
*/
|
|
28
|
+
async function renameWithRetry(tempPath, filePath) {
|
|
29
|
+
const deadline = Date.now() + RENAME_RETRY_BUDGET_MS;
|
|
30
|
+
for (;;) {
|
|
31
|
+
try {
|
|
32
|
+
await fs.rename(tempPath, filePath);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
const code = err instanceof Error ? err.code : undefined;
|
|
37
|
+
if (!code || !TRANSIENT_RENAME_ERROR_CODES.has(code) || Date.now() >= deadline) {
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
await sleep(RENAME_RETRY_DELAY_MS);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
2
44
|
/**
|
|
3
45
|
* Writes a file atomically: the content is first written to a unique
|
|
4
46
|
* temporary sibling file, then renamed over the target. Renaming within
|
|
@@ -7,6 +49,10 @@ import * as fs from 'node:fs/promises';
|
|
|
7
49
|
* file — never a torn or empty one. The temporary file is removed when
|
|
8
50
|
* the write or the rename fails.
|
|
9
51
|
*
|
|
52
|
+
* A rename the OS temporarily refuses (on Windows the target may be held
|
|
53
|
+
* open for a moment) is retried for up to two seconds before the write
|
|
54
|
+
* fails.
|
|
55
|
+
*
|
|
10
56
|
* The optional `mode` is applied to the temporary file (the rename
|
|
11
57
|
* preserves it), so callers can keep a sensitive file's permissions —
|
|
12
58
|
* e.g. `credentials.json` must stay owner-only across rewrites.
|
|
@@ -22,7 +68,7 @@ export async function atomicWriteFile(filePath, content, mode) {
|
|
|
22
68
|
const tempPath = `${filePath}.tmp-${process.pid}-${Math.random().toString(36).slice(2)}`;
|
|
23
69
|
try {
|
|
24
70
|
await fs.writeFile(tempPath, content, mode !== undefined ? { mode } : undefined);
|
|
25
|
-
await
|
|
71
|
+
await renameWithRetry(tempPath, filePath);
|
|
26
72
|
}
|
|
27
73
|
catch (err) {
|
|
28
74
|
await fs.unlink(tempPath).catch(() => { });
|
package/build/utils/file-lock.js
CHANGED
|
@@ -1,14 +1,65 @@
|
|
|
1
1
|
import * as fs from 'node:fs/promises';
|
|
2
2
|
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
3
4
|
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
4
5
|
const DEFAULT_STALE_MS = 10_000;
|
|
5
6
|
const DEFAULT_RETRY_DELAY_MS = 25;
|
|
7
|
+
/**
|
|
8
|
+
* Error codes from creating the exclusive lock file that mean "another
|
|
9
|
+
* holder owns the lock, or is releasing it right now" rather than
|
|
10
|
+
* "locking is impossible".
|
|
11
|
+
*
|
|
12
|
+
* POSIX reports the first case as EEXIST. Windows reports a name that is
|
|
13
|
+
* still being deleted (the previous holder released the lock between the
|
|
14
|
+
* failed create and this attempt) as EPERM or EACCES, and a concurrent
|
|
15
|
+
* file scanner can produce EBUSY; retrying all of them keeps a transient
|
|
16
|
+
* condition from becoming a fatal startup error.
|
|
17
|
+
*/
|
|
18
|
+
const CONTENTION_ERROR_CODES = new Set(['EEXIST', 'EPERM', 'EACCES', 'EBUSY']);
|
|
19
|
+
/**
|
|
20
|
+
* How long (ms) a create refused with an ambiguous code is retried.
|
|
21
|
+
*
|
|
22
|
+
* EPERM, EACCES, and EBUSY may mean "the name is still being deleted"
|
|
23
|
+
* (which clears within microseconds) or "this directory cannot be
|
|
24
|
+
* written at all". Retrying them for the full lock timeout would turn a
|
|
25
|
+
* permanent permission problem into a long stall once per stored
|
|
26
|
+
* credential entry, so they get a short window and then fail with their
|
|
27
|
+
* own error, which names the real reason.
|
|
28
|
+
*/
|
|
29
|
+
const AMBIGUOUS_ERROR_RETRY_MS = 1_000;
|
|
6
30
|
/**
|
|
7
31
|
* Type guard for Node.js system errors that carry a `code` property.
|
|
8
32
|
*/
|
|
9
33
|
function isNodeError(err) {
|
|
10
34
|
return err instanceof Error && typeof err.code === 'string';
|
|
11
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Records a create refusal that may be transient and throws it once it
|
|
38
|
+
* outlives {@link AMBIGUOUS_ERROR_RETRY_MS}.
|
|
39
|
+
*
|
|
40
|
+
* EEXIST means a live holder, so it resets the window. EPERM, EACCES,
|
|
41
|
+
* and EBUSY mean either a name that is still being deleted or a
|
|
42
|
+
* directory that cannot be written; a run of them longer than the
|
|
43
|
+
* settle window is the latter.
|
|
44
|
+
*
|
|
45
|
+
* @param code - The errno code of the refusal.
|
|
46
|
+
* @param since - Start of the current ambiguous run, 0 when none.
|
|
47
|
+
* @param err - The original error, rethrown when the window is spent.
|
|
48
|
+
* @returns The updated start of the ambiguous run.
|
|
49
|
+
* @throws The original error when the ambiguous run is too long.
|
|
50
|
+
*/
|
|
51
|
+
function trackAmbiguousRefusal(code, since, err) {
|
|
52
|
+
if (code === 'EEXIST') {
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
if (since === 0) {
|
|
56
|
+
return Date.now();
|
|
57
|
+
}
|
|
58
|
+
if (Date.now() - since >= AMBIGUOUS_ERROR_RETRY_MS) {
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
return since;
|
|
62
|
+
}
|
|
12
63
|
/**
|
|
13
64
|
* Runs `task` while holding an exclusive lock at `lockPath`.
|
|
14
65
|
*
|
|
@@ -67,10 +118,26 @@ export async function withFileLock(lockPath, task, options) {
|
|
|
67
118
|
*/
|
|
68
119
|
async function acquireLock(lockPath, timeoutMs, staleMs, retryDelayMs) {
|
|
69
120
|
const deadline = Date.now() + timeoutMs;
|
|
121
|
+
// Start of the current run of ambiguous refusals (see
|
|
122
|
+
// AMBIGUOUS_ERROR_RETRY_MS). A real EEXIST contention resets it: the
|
|
123
|
+
// holder was there, so a following EPERM is most likely its delete.
|
|
124
|
+
let ambiguousSince = 0;
|
|
70
125
|
for (;;) {
|
|
71
126
|
const token = randomUUID();
|
|
127
|
+
let handle;
|
|
72
128
|
try {
|
|
73
|
-
|
|
129
|
+
handle = await fs.open(lockPath, 'wx', 0o600);
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
// Only a refused create means contention. A failed write or close
|
|
133
|
+
// happens on a file this attempt already created and must not be
|
|
134
|
+
// retried against our own lock.
|
|
135
|
+
if (!isNodeError(err) || !CONTENTION_ERROR_CODES.has(err.code)) {
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
ambiguousSince = trackAmbiguousRefusal(err.code, ambiguousSince, err);
|
|
139
|
+
}
|
|
140
|
+
if (handle) {
|
|
74
141
|
try {
|
|
75
142
|
await handle.writeFile(`${process.pid} ${Date.now()} ${token}\n`);
|
|
76
143
|
}
|
|
@@ -79,11 +146,6 @@ async function acquireLock(lockPath, timeoutMs, staleMs, retryDelayMs) {
|
|
|
79
146
|
}
|
|
80
147
|
return token;
|
|
81
148
|
}
|
|
82
|
-
catch (err) {
|
|
83
|
-
if (!isNodeError(err) || err.code !== 'EEXIST') {
|
|
84
|
-
throw err;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
149
|
if (Date.now() >= deadline) {
|
|
88
150
|
throw new Error(`Timed out after ${timeoutMs}ms waiting for lock: ${lockPath}`);
|
|
89
151
|
}
|
|
@@ -98,7 +160,9 @@ async function acquireLock(lockPath, timeoutMs, staleMs, retryDelayMs) {
|
|
|
98
160
|
}
|
|
99
161
|
catch (err) {
|
|
100
162
|
if (isNodeError(err) && err.code === 'ENOENT') {
|
|
101
|
-
// Another waiter reclaimed the stale lock first
|
|
163
|
+
// Another waiter reclaimed the stale lock first: wait before
|
|
164
|
+
// the next attempt, so an unwritable directory cannot spin.
|
|
165
|
+
await sleep(retryDelayMs + Math.floor(Math.random() * retryDelayMs));
|
|
102
166
|
continue;
|
|
103
167
|
}
|
|
104
168
|
throw err;
|
|
@@ -127,6 +191,11 @@ async function isStale(lockPath, staleMs) {
|
|
|
127
191
|
if (isNodeError(err) && err.code === 'ENOENT') {
|
|
128
192
|
return true;
|
|
129
193
|
}
|
|
194
|
+
if (isNodeError(err) && CONTENTION_ERROR_CODES.has(err.code)) {
|
|
195
|
+
// The lock is mid-deletion and cannot be inspected. Treat it as
|
|
196
|
+
// live so the caller waits and retries instead of failing.
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
130
199
|
throw err;
|
|
131
200
|
}
|
|
132
201
|
}
|
|
@@ -157,11 +226,3 @@ async function releaseLock(lockPath, token) {
|
|
|
157
226
|
// The lock file is already gone; nothing to release.
|
|
158
227
|
}
|
|
159
228
|
}
|
|
160
|
-
/**
|
|
161
|
-
* Waits for the given number of milliseconds.
|
|
162
|
-
*
|
|
163
|
-
* @param ms - Delay in milliseconds.
|
|
164
|
-
*/
|
|
165
|
-
function sleep(ms) {
|
|
166
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
167
|
-
}
|