Skip to content

fix: resolve request hang with large API responses#437

Merged
angeloashmore merged 6 commits intomasterfrom
aa/fix-getbytype-hang-435
Feb 10, 2026
Merged

fix: resolve request hang with large API responses#437
angeloashmore merged 6 commits intomasterfrom
aa/fix-getbytype-hang-435

Conversation

@angeloashmore
Copy link
Member

@angeloashmore angeloashmore commented Jan 30, 2026

Resolves: #435

Description

Fixes requests hanging indefinitely in Node.js 22+ when using methods like getByType(), get(), etc.

Root cause: The request() function deduplicates concurrent GET requests to the same URL, sharing a single Response across callers. It then called response.clone() so each caller could read the body independently. In Node.js 22+, cloned responses share the same underlying stream — if the original response body is never consumed, backpressure blocks the clone from being read, causing .json() to hang forever.

Solution: Instead of cloning, buffer the response body as a blob immediately when the response is received, then derive text/JSON from the buffered blob on demand. This eliminates response.clone() entirely and works for both text (JSON API responses) and binary data (image uploads via WriteClient).

Checklist

  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

How to QA

  1. Run existing tests: npm test
  2. Test with Node.js 22+:
    const client = prismic.createClient("your-repo")
    const results = await client.getByType("your-type", { pageSize: 100 })
    // Should complete without hanging

Replace `response.clone()` with a memoized response wrapper that caches
promises for `text()`, `json()`, and `blob()` reads. This allows multiple
callers sharing a deduplicated request to safely read from the same response
without stream/body handling issues.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Clone the response before reading text/blob to avoid "body already read"
errors when both methods are called on the same memoized response.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Buffers the response body as a blob immediately instead of using
response.clone(). This fixes hanging requests in Node.js 22+ where
backpressure blocks the cloned stream when the original is not consumed.

See: node-fetch/node-fetch#139

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

angeloashmore and others added 3 commits February 6, 2026 17:14
Replace verbose wrapper with lazy caching variables with a compact
implementation that eagerly buffers the blob and derives text/json
from it on each call.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mockResolvedValue reuses one Response object across calls, but real
fetch always returns a fresh Response. Use mockImplementation so each
call creates a new Response, avoiding "Body has already been read".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

@TUNER88 TUNER88 left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@angeloashmore angeloashmore changed the title fix: resolve request hang with pageSize and fetchLinks fix: resolve request hang with large API responses Feb 10, 2026
@angeloashmore angeloashmore merged commit e5828e2 into master Feb 10, 2026
12 checks passed
@angeloashmore angeloashmore deleted the aa/fix-getbytype-hang-435 branch February 10, 2026 19:03
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.

getByType() hangs/times out with pageSize and fetchLinks parameters v7.19.2+

3 participants

Comments