Skip to content

Commit 34a250b

Browse files
authored
fix(getResources): move dotenv to default options (#90)
* options.defaults, new xhrFetchOptions * server.getResources, replace dotenv, hardcode
1 parent 5d60f08 commit 34a250b

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

src/__tests__/__snapshots__/options.defaults.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,8 @@ exports[`options defaults should return specific properties: defaults 1`] = `
8282
"toolModules": [],
8383
"urlRegex": /\\^\\(https\\?:\\)\\\\/\\\\//i,
8484
"version": "0.0.0",
85+
"xhrFetch": {
86+
"timeoutMs": 15000,
87+
},
8588
}
8689
`;

src/options.defaults.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { type ToolModule } from './server.toolsUser';
4040
* registered with the server.
4141
* @property urlRegex - Regular expression pattern for URL matching.
4242
* @property version - Version of the package.
43+
* @property xhrFetch - XHR and Fetch options.
4344
*/
4445
interface DefaultOptions<TLogOptions = LoggingOptions> {
4546
contextPath: string;
@@ -73,6 +74,7 @@ interface DefaultOptions<TLogOptions = LoggingOptions> {
7374
toolModules: ToolModule | ToolModule[];
7475
urlRegex: RegExp;
7576
version: string;
77+
xhrFetch: XhrFetchOptions;
7678
}
7779

7880
/**
@@ -153,13 +155,19 @@ interface LoggingSession extends LoggingOptions {
153155
readonly channelName: string;
154156
}
155157

158+
/**
159+
* Base stats options.
160+
*/
156161
type StatsOptions = {
157162
reportIntervalMs: {
158163
health: number;
159164
transport: number;
160165
}
161166
};
162167

168+
/**
169+
* Stats channel names.
170+
*/
163171
type StatsChannels = {
164172
readonly health: string;
165173
readonly session: string;
@@ -179,6 +187,17 @@ interface StatsSession extends StatsOptions {
179187
channels: StatsChannels
180188
}
181189

190+
/**
191+
* XHR and Fetch options.
192+
*
193+
* @interface XhrFetchOptions
194+
*
195+
* @property timeoutMs Timeout for XHR and Fetch requests (ms).
196+
*/
197+
interface XhrFetchOptions {
198+
timeoutMs: number;
199+
}
200+
182201
/**
183202
* Base logging options.
184203
*/
@@ -205,7 +224,7 @@ const HTTP_OPTIONS: HttpOptions = {
205224
*/
206225
const PLUGIN_HOST_OPTIONS: PluginHostOptions = {
207226
loadTimeoutMs: 5000,
208-
invokeTimeoutMs: 10000,
227+
invokeTimeoutMs: 10_000,
209228
gracePeriodMs: 2000
210229
};
211230

@@ -250,7 +269,7 @@ const TOOL_MEMO_OPTIONS = {
250269
};
251270

252271
/**
253-
* Stats options.
272+
* Default stats options.
254273
*/
255274
const STATS_OPTIONS: StatsOptions = {
256275
reportIntervalMs: {
@@ -259,6 +278,13 @@ const STATS_OPTIONS: StatsOptions = {
259278
}
260279
};
261280

281+
/**
282+
* Default XHR and Fetch options.
283+
*/
284+
const XHR_FETCH_OPTIONS: XhrFetchOptions = {
285+
timeoutMs: 15_000
286+
};
287+
262288
/**
263289
* Base logging channel name. Fixed to avoid user override.
264290
*/
@@ -387,7 +413,8 @@ const DEFAULT_OPTIONS: DefaultOptions = {
387413
toolModules: [],
388414
separator: DEFAULT_SEPARATOR,
389415
urlRegex: URL_REGEX,
390-
version: (process.env.NODE_ENV === 'local' && '0.0.0') || packageJson.version
416+
version: (process.env.NODE_ENV === 'local' && '0.0.0') || packageJson.version,
417+
xhrFetch: XHR_FETCH_OPTIONS
391418
};
392419

393420
export {
@@ -412,5 +439,6 @@ export {
412439
type LoggingOptions,
413440
type LoggingSession,
414441
type PluginHostOptions,
415-
type StatsSession
442+
type StatsSession,
443+
type XhrFetchOptions
416444
};

src/server.getResources.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ readLocalFileFunction.memo = memo(readLocalFileFunction, DEFAULT_OPTIONS.resourc
2323
*
2424
* @note Review expanding fetch to handle more file types like JSON.
2525
*
26-
* @param url
26+
* @param url - URL to fetch
27+
* @param options - Options
2728
*/
28-
const fetchUrlFunction = async (url: string) => {
29+
const fetchUrlFunction = async (url: string, options = getOptions()) => {
2930
const controller = new AbortController();
30-
const timeoutMs = Number(process.env.DOC_MCP_FETCH_TIMEOUT_MS || 15_000);
31-
const timeout = setTimeout(() => controller.abort(), timeoutMs);
31+
const timeout = setTimeout(() => controller.abort(), options.xhrFetch.timeoutMs);
3232

3333
// Allow the process to exit
3434
timeout.unref();

0 commit comments

Comments
 (0)