fix(PP-3449): stop request inspector from swallowing proxied PUT/POST bodies - #190
Merged
Merged
Conversation
… bodies The Request Inspector (added in e53fa90) broke request bodies for proxied PUT/POST requests, causing the backend to answer 408 Request Timeout: - plugin.ts mounted the inspector Express app (with global express.json/ urlencoded body parsers) for all paths before the proxy, so JSON bodies were consumed before http-proxy could pipe them to the backend. Route only /@api/* and /@pp-dev/inspector into it, matching cli.ts. - request-capture.middleware.ts attached a req 'data' listener, which switches the stream into flowing mode; buffered body chunks were emitted on process.nextTick, before the async proxy middleware attached its pipe, and were lost. Patch req.emit instead: chunks are observed only when a downstream consumer actually reads the stream, leaving its state intact. Add regression tests covering stream neutrality, late (next-tick) consumers receiving the full body, and capture still recording consumed bodies.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔧 Fix: Request Inspector swallowing proxied PUT/POST bodies (408 Request Timeout)
Summary
Since the Request Inspector landed (
e53fa90), PUT/POST requests with a body that go through the dev-server proxy never delivered their body to the MI backend. Apache waited for a body that never arrived and answered408 Request Timeout. GET requests were unaffected, which made the failure look backend-side.This PR fixes two independent defects introduced by the inspector wiring:
Key changes
src/plugin.ts🔧 — the inspector's internal Express app (which globally appliesexpress.json()/express.urlencoded()) was mounted for all paths before the proxy. The body parsers consumed the request stream, sohttp-proxypiped an already-exhausted stream to the backend. The app is now only routed requests for/@api/*and/@pp-dev/inspector— the same gatecli.ts(Next.js mode) already had.src/lib/request-capture.middleware.ts🔧 — capturing the request body viareq.on('data')switched the stream into flowing mode; buffered chunks were emitted onprocess.nextTick, before the async proxy middleware attached its pipe, and were lost (this affected both Vite and Next.js modes). Capture now patchesreq.emitinstead, observing chunks only when a downstream consumer actually reads the stream — stream state stays untouched.tests/unit/lib/request-capture.middleware.spec.ts🧪 — new regression suite: the middleware must not flip the stream into flowing mode, a late (next-tick) consumer must receive the full body, and the inspector must still capture bodies that are consumed downstream.Testing
http-proxy-middleware→ Apache-like backend): pre-fix code reproduces the408 Request Timeoutpage; fixed code returns 200 with all body bytes receivedstg7x.metricinsights.comviatests/test-commonjs:PUTwith a JSON body answered by the MI app in 0.77 s (405 for a nonexistent path, as expected — no timeout), and the Inspector captured the request with its full 51-byte body (source: proxy)Compatibility
No API or config changes; no breaking changes. Inspector UI/REST endpoints behave as before.
Included commits:
610a993fix(PP-3449): stop request inspector from swallowing proxied PUT/POST bodiesMerge Request:
origin/pp-3449-inspector-put-body-408→origin/develop