Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetch retries configuration #378

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/common/fetch-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export type FetchFn = (
...args: any[]
) => Promise<Response | globalThis.Response>;

let retryCount = 3;

export function setRetryCount(count: number) {
retryCount = count;
}

/**
* Wraps a fetch request with retry logic on exceptions, which is useful for
* spotty connections that may fail intermittently.
Expand All @@ -22,7 +28,7 @@ export function wrapWithRetry(fetch: FetchFn): FetchFn {
try {
return await fetch(url, ...args);
} catch (e) {
if (retries++ > 3) throw e;
if (retries++ >= retryCount) throw e;
}
}
};
Expand Down
13 changes: 13 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { getDefaultProviderStrings, type Provider } from "./providers/index.js";
import * as nodemodules from "./providers/nodemodules.js";
import { Resolver } from "./trace/resolver.js";
import { getMaybeWrapperUrl } from "./common/wrapper.js";
import { setRetryCount } from "./common/fetch-common.js";

// Utility exports for users:
export { analyzeHtml };
Expand Down Expand Up @@ -308,6 +309,12 @@ export interface GeneratorOptions {
* Whether to include "integrity" field in the import map
*/
integrity?: boolean;

/**
* The number of fetch retries to attempt for request failures.
* Defaults to 3.
*/
fetchRetries?: number;
}

export interface ModuleAnalysis {
Expand Down Expand Up @@ -400,6 +407,7 @@ export class Generator {
commonJS = false,
typeScript = false,
integrity = false,
fetchRetries,
}: GeneratorOptions = {}) {
// Initialise the debug logger:
const { log, logStream } = createLogger();
Expand Down Expand Up @@ -512,6 +520,10 @@ export class Generator {
this.map = new ImportMap({ mapUrl: this.mapUrl, rootUrl: this.rootUrl });
if (!integrity) this.map.integrity = {};
if (inputMap) this.addMappings(inputMap);

// Set the fetch retry count
if (typeof fetchRetries === 'number')
setRetryCount(fetchRetries);
}

/**
Expand Down Expand Up @@ -1411,3 +1423,4 @@ function detectDefaultProvider(

return defaultProvider || winner || "jspm.io";
}

1 change: 0 additions & 1 deletion src/providers/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SemverRange } from "sver";
// @ts-ignore
import { fetch } from "#fetch";
import { Install } from "../generator.js";
import { IImportMap, ImportMap } from "@jspm/import-map";

const cdnUrl = "https://deno.land/x/";
const stdlibUrl = "https://deno.land/std";
Expand Down
2 changes: 1 addition & 1 deletion src/providers/nodemodules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExactModule, LatestPackageTarget } from "../install/package.js";
import { LatestPackageTarget } from "../install/package.js";
import { ExactPackage } from "../install/package.js";
import { Resolver } from "../trace/resolver.js";
import { Provider } from "./index.js";
Expand Down
Loading