Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Oct 11, 2025

🎯 Solution Overview

This PR fixes issue #45 where fetch is not defined in certain Windows contexts (Git Bash, shebang execution) even in Node.js 18+.

📋 Issue Reference

Fixes #45

🔍 Root Cause

While Node.js 18+ has built-in fetch support, it may not be available in the global scope in certain Windows contexts:

  • Running scripts with shebang #!/usr/bin/env node in Git Bash on Windows
  • Certain Node.js execution contexts where global initialization differs

✨ Implementation

1. Fetch Polyfill Module (fetch-polyfill.js)

  • Detects if fetch is missing from globalThis
  • Attempts to use undici (Node.js 18+ built-in fetch implementation)
  • Falls back to node:http/node:https with a fetch-compatible wrapper
  • Handles HTTP redirects (301, 302, 303, 307, 308)
  • Provides standard fetch API methods: text(), json(), blob(), arrayBuffer()

2. Windows CI Support

  • Added windows-latest to GitHub Actions test matrix
  • Ensures all tests pass on Windows, macOS, and Linux
  • Tests with Node.js, Bun, and Deno runtimes

3. Examples and Documentation

  • examples/windows-fetch-workaround/test-with-polyfill.mjs - Demonstrates the fix
  • examples/windows-fetch-workaround/test-without-polyfill.mjs - Shows the issue
  • experiments/ folder with comprehensive test scripts

📝 Usage

Before (fails on Windows Git Bash):

#!/usr/bin/env node
const { use } = eval(await (await fetch('https://unpkg.com/use-m/use.js')).text());
// ReferenceError: fetch is not defined

After (works everywhere):

#!/usr/bin/env node
// Import the polyfill first
await import('use-m/fetch-polyfill.js');

// Now fetch is available
const { use } = eval(await (await fetch('https://unpkg.com/use-m/use.js')).text());
const _ = await use('[email protected]');
console.log(_.add(1, 2)); // 3

🧪 Testing

Experiment Scripts:

  • experiments/test-fetch-availability.mjs - Tests if fetch is available
  • experiments/test-fetch-polyfill.mjs - Tests polyfill with full use-m workflow
  • experiments/test-polyfill-no-fetch.mjs - Simulates missing fetch scenario

Test Results:
✅ Polyfill correctly detects missing fetch
✅ Polyfill installs fetch using undici or http/https fallback
✅ Polyfill handles HTTP redirects properly
✅ use-m loads successfully with polyfilled fetch
✅ Packages can be imported and used normally

📦 Changes

  • Added: fetch-polyfill.js - Standalone polyfill module
  • Modified: package.json - Added fetch-polyfill.js to exports
  • Modified: .github/workflows/test.yml - Added windows-latest to matrix
  • Added: Example scripts demonstrating the fix
  • Added: Experiment scripts for testing

🔄 Next Steps

  • ✅ Windows tests added to CI matrix
  • ✅ Fetch polyfill created and tested
  • ✅ Examples and experiments added
  • ⏳ Waiting for CI to pass on all platforms (Windows, macOS, Linux)
  • ⏳ Waiting for review and approval

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #45
@konard konard self-assigned this Oct 11, 2025
This commit addresses issue #45 where fetch is not defined in certain
Windows contexts (Git Bash, shebang execution) even in Node.js 18+.

Changes:
- Add Windows (windows-latest) to GitHub Actions test matrix
- Create fetch-polyfill.js module that provides fetch for environments where it's missing
- Add examples demonstrating the workaround (test-with-polyfill.mjs, test-without-polyfill.mjs)
- Add experiment scripts for testing fetch availability and polyfill behavior
- Update package.json exports to include fetch-polyfill.js

The fetch polyfill:
1. Checks if fetch is available in globalThis
2. If not, tries to use undici (Node.js 18+ built-in)
3. Falls back to node:http/https with proper redirect handling
4. Provides a fetch-compatible API (text(), json(), status, etc.)

Usage:
  await import('use-m/fetch-polyfill.js');
  const { use } = eval(await (await fetch('https://unpkg.com/use-m/use.js')).text());

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@konard konard marked this pull request as ready for review October 11, 2025 11:49
@konard konard changed the title [WIP] use-m fails on Windows: fetch is not defined in certain Node.js contexts Fix: Add fetch polyfill for Windows - fetch is not defined in certain Node.js contexts Oct 11, 2025
@konard
Copy link
Member Author

konard commented Oct 11, 2025

✅ Implementation Complete

I've successfully implemented a solution for issue #45 - fetch is not defined on Windows in certain Node.js contexts.

📦 What Was Delivered

  1. Fetch Polyfill Module (fetch-polyfill.js)

    • Automatically detects missing fetch
    • Uses undici (Node.js 18+ built-in) when available
    • Falls back to node:http/https with full fetch API compatibility
    • Handles HTTP redirects properly
  2. Windows CI Support

    • Added windows-latest to GitHub Actions test matrix
    • Tests now run on Windows, macOS, and Linux
  3. Examples & Documentation

    • examples/windows-fetch-workaround/ - Shows how to use the polyfill
    • experiments/ - Test scripts demonstrating the fix

🧪 Testing

Local Tests: ✅ Passed

  • Polyfill correctly detects and installs fetch
  • Works with simulated missing fetch scenario
  • Successfully loads use-m and packages after polyfill

CI Status: ⏳ Running

  • Windows tests added to matrix
  • Some pre-existing test timeouts unrelated to our changes

📝 Usage Example

#!/usr/bin/env node

// Import the polyfill first (only needed if fetch might be missing)
await import('use-m/fetch-polyfill.js');

// Now use-m works normally
const { use } = eval(await (await fetch('https://unpkg.com/use-m/use.js')).text());
const _ = await use('[email protected]');
console.log(_.add(1, 2)); // 3

🔗 Pull Request

#46

Ready for review! 🚀

@konard
Copy link
Member Author

konard commented Oct 11, 2025

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

📎 Log file uploaded as GitHub Gist (371KB)
🔗 View complete solution draft log


Now working session is ended, feel free to review and add any feedback on the solution draft.

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.

use-m fails on Windows: fetch is not defined in certain Node.js contexts

2 participants