Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.

Commit 85b4f54

Browse files
authored
add fetch retries configuration (#378)
1 parent e4c7d8a commit 85b4f54

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/common/fetch-common.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export type FetchFn = (
1111
...args: any[]
1212
) => Promise<Response | globalThis.Response>;
1313

14+
let retryCount = 3;
15+
16+
export function setRetryCount(count: number) {
17+
retryCount = count;
18+
}
19+
1420
/**
1521
* Wraps a fetch request with retry logic on exceptions, which is useful for
1622
* spotty connections that may fail intermittently.
@@ -22,7 +28,7 @@ export function wrapWithRetry(fetch: FetchFn): FetchFn {
2228
try {
2329
return await fetch(url, ...args);
2430
} catch (e) {
25-
if (retries++ > 3) throw e;
31+
if (retries++ >= retryCount) throw e;
2632
}
2733
}
2834
};

src/generator.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { getDefaultProviderStrings, type Provider } from "./providers/index.js";
4444
import * as nodemodules from "./providers/nodemodules.js";
4545
import { Resolver } from "./trace/resolver.js";
4646
import { getMaybeWrapperUrl } from "./common/wrapper.js";
47+
import { setRetryCount } from "./common/fetch-common.js";
4748

4849
// Utility exports for users:
4950
export { analyzeHtml };
@@ -308,6 +309,12 @@ export interface GeneratorOptions {
308309
* Whether to include "integrity" field in the import map
309310
*/
310311
integrity?: boolean;
312+
313+
/**
314+
* The number of fetch retries to attempt for request failures.
315+
* Defaults to 3.
316+
*/
317+
fetchRetries?: number;
311318
}
312319

313320
export interface ModuleAnalysis {
@@ -400,6 +407,7 @@ export class Generator {
400407
commonJS = false,
401408
typeScript = false,
402409
integrity = false,
410+
fetchRetries,
403411
}: GeneratorOptions = {}) {
404412
// Initialise the debug logger:
405413
const { log, logStream } = createLogger();
@@ -512,6 +520,10 @@ export class Generator {
512520
this.map = new ImportMap({ mapUrl: this.mapUrl, rootUrl: this.rootUrl });
513521
if (!integrity) this.map.integrity = {};
514522
if (inputMap) this.addMappings(inputMap);
523+
524+
// Set the fetch retry count
525+
if (typeof fetchRetries === 'number')
526+
setRetryCount(fetchRetries);
515527
}
516528

517529
/**
@@ -1411,3 +1423,4 @@ function detectDefaultProvider(
14111423

14121424
return defaultProvider || winner || "jspm.io";
14131425
}
1426+

src/providers/deno.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { SemverRange } from "sver";
88
// @ts-ignore
99
import { fetch } from "#fetch";
1010
import { Install } from "../generator.js";
11-
import { IImportMap, ImportMap } from "@jspm/import-map";
1211

1312
const cdnUrl = "https://deno.land/x/";
1413
const stdlibUrl = "https://deno.land/std";

src/providers/nodemodules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExactModule, LatestPackageTarget } from "../install/package.js";
1+
import { LatestPackageTarget } from "../install/package.js";
22
import { ExactPackage } from "../install/package.js";
33
import { Resolver } from "../trace/resolver.js";
44
import { Provider } from "./index.js";

0 commit comments

Comments
 (0)