-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance code to build the default
Fetcher
(#218)
Fixes #215 This PR is based on the work of @jesseditson in #216 and addresses two issues: - We use dynamic imports to build the default `Fetcher` instance, but some JS runtimes don't support them. - We are being too specific when detecting the presence of a `fetch` function, and different JS runtimes might expose it in different locations. To address these issues, this PR: - Eagerly tries to import all the built-in `Fetcher` implementations while tracking any import errors to provide actionable feedback to end-users as early as possible. - Detects the presence of `fetch` in a looser way to avoid differences across specific JS runtimes.
- Loading branch information
Showing
32 changed files
with
418 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { DNSimple, TimeoutError } from "../main"; | ||
import type { Fetcher } from "./fetcher"; | ||
|
||
const fetchFetcher: Fetcher = async (params: { | ||
method: string; | ||
url: string; | ||
headers: { [name: string]: string }; | ||
timeout: number; | ||
body?: string; | ||
}) => { | ||
const abortController = new AbortController(); | ||
setTimeout(() => abortController.abort(), DNSimple.DEFAULT_TIMEOUT); | ||
try { | ||
const response = await fetch(params.url, { | ||
method: params.method, | ||
headers: params.headers, | ||
body: params.body, | ||
signal: abortController.signal, | ||
}); | ||
return { status: response.status, body: await response.text() }; | ||
} catch (error) { | ||
if (abortController && abortController.signal.aborted) | ||
throw new TimeoutError(); | ||
|
||
throw error; | ||
} | ||
}; | ||
|
||
export default fetchFetcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* A function that makes an HTTP request. It's responsible for throwing {@link TimeoutError} and aborting the request on {@param params.timeout}. | ||
* It should return the response status and full body as a string. It should not throw on any status, even if 4xx or 5xx. | ||
* It can decide to implement retries as appropriate. The default fetcher currently does not implement any retry strategy. | ||
*/ | ||
export type Fetcher = (params: { | ||
method: string; | ||
url: string; | ||
headers: { [name: string]: string }; | ||
body?: string; | ||
timeout: number; | ||
}) => Promise<{ | ||
status: number; | ||
body: string; | ||
}>; | ||
|
||
let fetcherImports: { | ||
fetchFetcher: Fetcher; | ||
httpsFetcher: Fetcher; | ||
}; | ||
let fetcherImportError: Error | undefined; | ||
|
||
try { | ||
fetcherImports = { | ||
fetchFetcher: require("./fetch-fetcher").default, | ||
httpsFetcher: require("./https-fetcher").default, | ||
}; | ||
} catch (error) { | ||
fetcherImportError = error; | ||
} | ||
|
||
export function getRuntimeFetcher(): Fetcher { | ||
if (fetcherImportError) | ||
throw new Error( | ||
`No global \`fetch\` or \`https\` module was found. Please, provide a Fetcher implementation: ${fetcherImportError}` | ||
); | ||
|
||
return typeof fetch === "undefined" | ||
? fetcherImports.httpsFetcher | ||
: fetcherImports.fetchFetcher; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as https from "https"; | ||
import { URL } from "url"; | ||
import type { Fetcher } from "./fetcher"; | ||
import { TimeoutError } from "../main"; | ||
|
||
const httpsFetcher: Fetcher = (params: { | ||
method: string; | ||
url: string; | ||
headers: { [name: string]: string }; | ||
timeout: number; | ||
body?: string; | ||
}): Promise<{ status: number; body: string }> => { | ||
return new Promise((resolve, reject) => { | ||
const urlObj = new URL(params.url); | ||
const options: https.RequestOptions = { | ||
method: params.method, | ||
headers: params.headers, | ||
timeout: params.timeout, | ||
}; | ||
|
||
const req = https.request(urlObj, options, (res) => { | ||
const chunks: Buffer[] = []; | ||
res | ||
.on("data", (chunk) => chunks.push(chunk)) | ||
.on("end", () => { | ||
const body = Buffer.concat(chunks).toString("utf-8"); | ||
resolve({ | ||
status: res.statusCode || 500, // Fallback to 500 if statusCode is undefined | ||
body: body, | ||
}); | ||
}); | ||
}); | ||
|
||
req.on("error", (err: { code?: string }) => { | ||
if (err.code === "ECONNRESET") reject(new TimeoutError()); | ||
else reject(err); | ||
}); | ||
|
||
const timeoutId = setTimeout(() => { | ||
req.destroy(); | ||
reject(new TimeoutError()); | ||
}, params.timeout); | ||
|
||
req.on("close", () => clearTimeout(timeoutId)); | ||
|
||
if (params.body) req.write(params.body); | ||
|
||
req.end(); | ||
}); | ||
}; | ||
|
||
export default httpsFetcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.