-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #960 from prismicio/mm/http-keep-alive
fix(http-connect): try wrapping fetch so that it's passed and agent t…
- Loading branch information
Showing
10 changed files
with
55 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// This temporary wrapper around `node-fetch` fixes an issue where quick | ||
// consecutive network requests cause failed requests. | ||
// | ||
// See https://github.com/node-fetch/node-fetch/issues/1735 for more details. | ||
// | ||
// TODO: Remove this wrapper and replace all imports with `node-fetch` if https://github.com/node-fetch/node-fetch/pull/1736 is merged. | ||
|
||
import * as http from "node:http"; | ||
import * as https from "node:https"; | ||
import baseFetch from "node-fetch"; | ||
|
||
export * from "node-fetch"; | ||
|
||
/** | ||
* The default HTTP Agent with `keepAlive: true` used in `fetch()` requests. | ||
*/ | ||
const DEFAULT_HTTP_AGENT = new http.Agent({ keepAlive: true }); | ||
|
||
/** | ||
* The default HTTPS Agent with `keepAlive: true` used in `fetch()` requests. | ||
*/ | ||
const DEFAULT_HTTPS_AGENT = new https.Agent({ keepAlive: true }); | ||
|
||
/** | ||
* Patched `fetch()` from `node-fetch` that fixes a bug where quick consecutive | ||
* network requests cause failed requests. | ||
* | ||
* Use this `fetch()` in place of `node-fetch`'s `fetch()`. | ||
* | ||
* @remarks | ||
* `fetch()` is patched by setting an HTTP/HTTPS Agent with `keepAlive: true`. | ||
* If you need to assign an Agent, be sure to retain the `keepAlive: true` | ||
* option. | ||
*/ | ||
const fetch: typeof baseFetch = (url, init) => { | ||
return baseFetch(url, { | ||
agent: (parsedURL) => { | ||
return parsedURL.protocol === "http:" | ||
? DEFAULT_HTTP_AGENT | ||
: DEFAULT_HTTPS_AGENT; | ||
}, | ||
...init, | ||
}); | ||
}; | ||
|
||
export default fetch; |
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
2 changes: 1 addition & 1 deletion
2
packages/manager/src/managers/customTypes/CustomTypesManager.ts
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
2 changes: 1 addition & 1 deletion
2
packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts
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
2 changes: 1 addition & 1 deletion
2
packages/manager/src/managers/screenshots/ScreenshotsManager.ts
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