Skip to content
This repository was archived by the owner on Jan 3, 2026. It is now read-only.

feat!: fetch (POC)#618

Closed
danielbankhead wants to merge 44 commits intomainfrom
native-fetch
Closed

feat!: fetch (POC)#618
danielbankhead wants to merge 44 commits intomainfrom
native-fetch

Conversation

@danielbankhead
Copy link
Contributor

@danielbankhead danielbankhead commented Apr 19, 2024

A proof-of-concept for migrating to native fetch. However, there are a few limitations today.

No native proxy support

It is possible to enable proxy support, however customers would have to install unidici downstream. Including it in this library would make it far too large. Here are some numbers:

package version size (minified)
gaxios 6.5.0 38.3 kB
undici 6.5.0 402.6 kB
@google-cloud/storage 7.10.0 484.2 kB

Adding undici would double the size of a downstream dependency like @google-cloud/storage.

However, there is discussion on adding the ProxyAgent within Node:

Workaround

Potential workaround/fallback for customers:

import {instance, request} from 'gaxios';
import {fetch, ProxyAgent} from 'undici';

instance.defaults.fetchImplementation = fetch;

await request({
  url: '', 
  dispatcher: new ProxyAgent();
})

Environment support

Additionally, it wasn't until recently that undici added env support for HTTP_PROXY, HTTPS_PROXY, & NO_PROXY and it will take time for it to propagate to Node.js releases:

Performance

The native fetch implementation is 26% - 28% slower than node-fetch according to undici's benchmarks:

Returning Streams

Native fetch returns a ReadableStream ('Web Streams') instead of stream.Readable. While Web Streams have more features, like tee and cancel, this may require rewriting downstream for many applications that currently expect a Node stream. However, here's a simple way to resolve:

import {Readable} from 'node:stream';

Readable.fromWeb(res.body);

In some case we may want to return Web Streams instead - especially if not consumer-facing and to support non-Node.js runtimes/environments.

Additional Notes

https://nodejs.org/en/about/previous-releases

Fixes: googleapis/google-cloud-node-core#165
🦕

@product-auto-label product-auto-label bot added the size: l Pull request size is large. label Apr 19, 2024
@generated-files-bot
Copy link

Warning: This pull request is touching the following templated files:

@ddelgrosso1
Copy link
Contributor

Really cool to see and thank you for digging into the numbers, both size wise and speed wise. Seems like there is a lot of work that still needs to happen on the node side for this to be competitive with node-fetch.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat!: Migrate to fetch

3 participants