Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Don't override global fetch & fix tests #63

Closed
wants to merge 7 commits into from

Conversation

AntoineAtMistral
Copy link

@AntoineAtMistral AntoineAtMistral commented Apr 26, 2024

Use globalThis.mistralFetch instead of globalThis.fetch to avoid overriding it.
Fix isNode logic.

Thanks @flakey5 for the contribution.

typeof globalThis.fetch === 'undefined') {
const nodeFetch = await import('node-fetch');
fetch = nodeFetch.default;
if (typeof window === 'undefined') {
Copy link
Contributor

@sublimator sublimator Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments:

window is also not defined in service workers, as used by Web Extensions, using manifest version 3

It seems the isNode flag is used to determine when to return the response.body directly from the _request method, vs implementing an async iterator.

In fact, in newer versions of node, with native fetch you need to do much the same, so isNode is a somewhat deceptive name here. It's more node-fetch specific if I'm not mistaken

In any case, this library should work in:

  1. browser in a normal page
  2. browser in a service worker
  3. an older version of node, without native fetch
  4. a newer version of node, with native fetch

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants