Skip to content

Conversation

@lukasjuhas
Copy link

Problem

Node.js 18+ users see this deprecation warning when using the SDK:

(node:12345) [DEP0169] DeprecationWarning: `url.parse()` behavior is not 
standardized and prone to errors that have security implications. 
Use the WHATWG URL API instead.

This warning originates from node-fetch v2.x, which internally uses the deprecated url.parse() API.

Solution

Update safeFetch.ts to check for native fetch availability before attempting to load node-fetch. Since Node.js 18+ includes native fetch, this eliminates the need for node-fetch entirely in modern environments.

Changes

  1. src/utils/safeFetch.ts: Check for native fetch first, only fall back to node-fetch when native fetch is unavailable
  2. src/__tests__/jest.setup.js: Delete global.fetch in tests so existing node-fetch mocks continue to work
  3. src/utils/__tests__/safeFetch.test.ts: New tests to verify the native fetch preference behavior

Behavior by Environment

Environment Fetch Used Deprecation Warning
Node 18+ Native ❌ None
Node 14/16 node-fetch ⚠️ Expected
Edge/Browser Native ❌ None
Tests Mocked node-fetch ❌ None

Test Results

All 326 tests pass (excluding the expected ClientInitializeResponseConsistency failure for non-Statsig employees).

Backward Compatibility

  • ✅ No breaking changes to the public API
  • ✅ Existing tests continue to work with node-fetch mocks
  • ✅ Falls back to node-fetch for older Node.js versions (14, 16)
  • ✅ Works in edge runtimes and browsers that have native fetch

…recation warning

## Problem

Node.js 18+ users see this deprecation warning when using the SDK:

```
(node:12345) [DEP0169] DeprecationWarning: `url.parse()` behavior is not
standardized and prone to errors that have security implications.
Use the WHATWG URL API instead.
```

This warning originates from `node-fetch` v2.x, which internally uses the
deprecated `url.parse()` API.

## Solution

Update `safeFetch.ts` to check for native `fetch` availability BEFORE
attempting to load `node-fetch`. Since Node.js 18+ includes native fetch,
this eliminates the need for `node-fetch` entirely in modern environments.

### Changes:
1. **safeFetch.ts**: Check for native fetch first, only fall back to
   node-fetch when native fetch is unavailable
2. **jest.setup.js**: Delete `global.fetch` in tests so existing
   node-fetch mocks continue to work
3. **safeFetch.test.ts**: New tests to verify the native fetch preference

## Behavior by Environment

| Environment | Fetch Used | Deprecation Warning |
|-------------|-----------|---------------------|
| Node 18+    | Native    | ❌ None             |
| Node 14/16  | node-fetch| ⚠️ Expected         |
| Edge/Browser| Native    | ❌ None             |
| Tests       | Mocked    | ❌ None             |

## Backward Compatibility

- No breaking changes to the public API
- Tests continue to work with node-fetch mocks
- Falls back to node-fetch for older Node.js versions
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.

1 participant