streamed-chatgpt-api icon indicating copy to clipboard operation
streamed-chatgpt-api copied to clipboard

Fix response.body.getReader compatibility with node-fetch

Open Copilot opened this issue 9 months ago • 0 comments

Issue

When using this library with node-fetch instead of native fetch, it would fail with an error: response.body.getReader is not a function. This is because node-fetch doesn't implement the Web Streams API in the same way as native fetch.

Solution

This PR adds support for both implementations:

  1. It checks if response.body.getReader() is available (native fetch)
  2. If available, it uses the existing implementation with getReader()
  3. If not available, it falls back to a Node.js stream implementation using event listeners

The implementation maintains identical timeout and error handling behaviors between both approaches.

Changes

  • Added a check for response.body.getReader availability
  • Added a new processNodeStream function for node-fetch compatible streams
  • Updated documentation to clarify compatibility with both fetch implementations
  • Added a simple compatibility test

This change enables the library to work with both Node.js 18+ (with native fetch) and older Node.js versions using node-fetch as a fallback without any code changes required by the user.

Fixes #6.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar May 23 '25 19:05 Copilot