-
Notifications
You must be signed in to change notification settings - Fork 1
fix(proxy): rewrite URLs and forward status for streamed responses #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sergak01
merged 2 commits into
mi-examples:develop
from
eastagiletracker:agile-board/stream-response-rewrite
Aug 19, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||
| import { createServer, request, type Server } from 'node:http'; | ||
| import initProxy from '../../../src/lib/proxy-pass.middleware.js'; | ||
| import type { MiAPI } from '../../../src/lib/pp.middleware.js'; | ||
|
|
||
| /** | ||
| * End-to-end check that a proxied streaming response gets the same treatment as every other | ||
| * proxied response: the upstream host is rewritten to the host the browser asked for, and the | ||
| * upstream status code reaches the client. Both were lost for `text/event-stream` responses, | ||
| * which were piped through untouched with a hard-coded 200. | ||
| */ | ||
| describe('proxy-pass streaming responses', () => { | ||
| let upstream: Server; | ||
| let local: Server; | ||
| let upstreamHost: string; | ||
| let localPort: number; | ||
|
|
||
| const miAPI = { | ||
| personalAccessToken: undefined, | ||
| v7Features: false, | ||
| internalPageName: undefined, | ||
| } as unknown as MiAPI; | ||
|
|
||
| const get = (path: string) => | ||
| new Promise<{ status: number; body: string }>((resolve, reject) => { | ||
| const req = request({ host: '127.0.0.1', port: localPort, path, method: 'GET' }, (res) => { | ||
| const chunks: Buffer[] = []; | ||
|
|
||
| res.on('data', (chunk) => chunks.push(chunk)); | ||
| res.on('end', () => resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString('utf8') })); | ||
| }); | ||
|
|
||
| req.on('error', reject); | ||
| req.end(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| upstream = createServer((req, res) => { | ||
| if (req.url?.startsWith('/stream')) { | ||
| res.writeHead(503, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' }); | ||
| res.write(`data: {"next":"http://${upstreamHost}/data/page/next"}\n\n`); | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(200, { 'content-type': 'text/html' }); | ||
| res.end(`<html><body><a href="http://${upstreamHost}/data/page/next">go</a></body></html>`); | ||
| }); | ||
|
|
||
| await new Promise<void>((resolve) => upstream.listen(0, '127.0.0.1', () => resolve())); | ||
|
|
||
| const upstreamAddress = upstream.address(); | ||
|
|
||
| upstreamHost = `127.0.0.1:${typeof upstreamAddress === 'object' && upstreamAddress ? upstreamAddress.port : 0}`; | ||
|
|
||
| const proxy = initProxy({ baseURL: `http://${upstreamHost}`, devServer: {} as never, miAPI }); | ||
|
|
||
| local = createServer((req, res) => { | ||
| proxy(req as never, res as never, () => { | ||
| res.statusCode = 404; | ||
| res.end('not proxied'); | ||
| }); | ||
| }); | ||
|
|
||
| await new Promise<void>((resolve) => local.listen(0, '127.0.0.1', () => resolve())); | ||
|
|
||
| const localAddress = local.address(); | ||
|
|
||
| localPort = typeof localAddress === 'object' && localAddress ? localAddress.port : 0; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await new Promise<void>((resolve) => local.close(() => resolve())); | ||
| await new Promise<void>((resolve) => upstream.close(() => resolve())); | ||
| }); | ||
|
|
||
| it('rewrites the upstream host in a streamed body', async () => { | ||
| const streamed = await get('/stream'); | ||
|
|
||
| expect(streamed.body).toContain(`http://127.0.0.1:${localPort}/data/page/next`); | ||
| expect(streamed.body).not.toContain(upstreamHost); | ||
| }); | ||
|
|
||
| it('forwards the upstream status code of a streamed response', async () => { | ||
| const streamed = await get('/stream'); | ||
|
|
||
| expect(streamed.status).toBe(503); | ||
| }); | ||
|
|
||
| it('still rewrites the upstream host in a non-streamed body', async () => { | ||
| const html = await get('/page'); | ||
|
|
||
| expect(html.status).toBe(200); | ||
| expect(html.body).toContain(`http://127.0.0.1:${localPort}/data/page/next`); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Honor client-response backpressure.
Line 104 ignores the
falsereturn fromres.write(). Thedatalistener keepsproxyResin flowing mode, so a slow client can cause an unboundedServerResponsewrite queue.MAX_PENDING_STREAM_CHUNKonly bounds the trailing partial line. PauseproxyReswhenres.write()returnsfalse, and resume it onres.once('drain').🤖 Prompt for AI Agents