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

Update FetchHttpClient to make fetch function optional. #1253

Merged
merged 1 commit into from
Sep 24, 2021

Conversation

dcr-stripe
Copy link
Contributor

Notify

r? @richardm-stripe
cc? @tomer-stripe

Summary

Updates the FetchHttpClient to make the fetch function optional, defaulting to the global if not provided.

This also purposefully avoids calling this._fetchFn() directly and instead storing it and calling it via the variable. This ensures we call the passed in function with the global scope instead of accidentally calling it with the FetchHttpClient as this. Calling this._fetchFn() caused issues on Cloudflare Workers as it would invoke fetch in the wrong scope, resulting in code failing with:

A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's request context have already finished.

This is likely because the global fetch stores state to track pending work, and calling it within a different scope resulted in the work not being tracked.

Examples

Default global fetch

new FetchHttpClient();

Explicitly passing in global fetch

new FetchHttpClient(fetch);

Passing in a custom fetch (eg. node-fetch)

const fetch = require('node-fetch');
new FetchHttpClient(fetch);

@dcr-stripe dcr-stripe merged commit a1aa228 into master Sep 24, 2021
@dcr-stripe dcr-stripe deleted the dcr-global-scope branch September 24, 2021 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants