Skip to content

Commit 060795f

Browse files
ishaquehassanmdebbarbkonyi
authored
[flutter_tools] Stream web dev server proxy responses instead of buffering them (flutter#194934)
When a request matches a `web_dev_config.yaml` proxy rule, `_applyProxyRules` returns the `Response` that `shelf_proxy` builds around the backend stream. That response has no context, and `shelf_io` only touches `HttpResponse.bufferOutput` when `shelf.io.buffer_output` is in the context, so proxied responses go through dart:io's default 8 KB output buffer. A single SSE event or NDJSON line just sits in that buffer until it fills up or the backend closes the response. In the issue's repro the first line shows up after 33 ms when hitting the backend directly, and doesn't arrive through the proxy within the 3 second timeout. The fix sets `shelf.io.buffer_output: false` on the proxied response via `Response.change`. Status, headers and the streamed body carry over unchanged, only the context gets the extra key, so `shelf_io` writes each chunk out as soon as it comes in from the backend. I didn't scope it to `text/event-stream` or NDJSON: dart:io already writes chunks over 8 KB straight through even with buffering on, so for regular responses the only difference is that small chunks no longer get coalesced. Both handlers in `web_asset_server.dart` (release/Wasm and the DWDS debug one) go through `proxyMiddleware`, so both get this. The new test in `proxy_test.dart` has the backend send one line and only finish the response after that line has made it through the proxy. Without the fix the first line never reaches the client and the test times out. With it, all 40 tests in `proxy_test.dart` pass, and so does the rest of `test/general.shard/web/`. I also ran a local check (not part of this PR) with an SSE backend behind `WebAssetServer.start`: the first event came through after 24 ms in release and 141 ms in Wasm debug. Without the change it took about 8 s, which is when that backend closed the response. dart-lang/shelf#503 asks for a `bufferOutput` option on `proxyHandler` for the same SSE case. Even with that option the tool would still have to opt in here, and `shelf.io.buffer_output` is the key `shelf_io` already documents for this, so there's no need to wait for a `shelf_proxy` release. If that option lands later, this call site can switch over to it. Fixes flutter#194354 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Co-authored-by: Mouad Debbar <[email protected]> Co-authored-by: Ben Konyi <[email protected]>
1 parent 0ab6e05 commit 060795f

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎packages/flutter_tools/lib/src/isolated/proxy_middleware.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ shelf.Request proxyRequest(shelf.Request originalRequest, Uri finalTargetUrl) {
2929
/// If a rule's pattern matches the request's path, the request is
3030
/// rewritten and forwarded to the target URI defined in the matching [ProxyRule].
3131
/// Otherwise, the request is passed to the next handler in the Shelf stack.
32+
///
33+
/// Proxied responses are written without output buffering, so streamed bodies
34+
/// such as server-sent events reach the client as soon as the backend sends them.
3235
Future<shelf.Response> _applyProxyRules(
3336
shelf.Request request,
3437
List<ProxyRule> effectiveProxy,
@@ -47,7 +50,7 @@ Future<shelf.Response> _applyProxyRules(
4750
final shelf.Response proxyResponse = await handler(proxyBackendRequest);
4851
logger.printStatus('$_kLogEntryPrefix Matched "$requestPath". Requesting "$finalTargetUrl"');
4952
logger.printTrace('$_kLogEntryPrefix Matched with proxy rule: $rule');
50-
return proxyResponse;
53+
return proxyResponse.change(context: <String, Object>{'shelf.io.buffer_output': false});
5154
} on Exception catch (e) {
5255
logger.printError('$_kLogEntryPrefix Error for $finalTargetUrl: $e. Allowing fall-through.');
5356
return innerHandler(request);

‎packages/flutter_tools/test/general.shard/web/proxy_test.dart‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:io' show HttpServer;
6+
import 'dart:convert';
7+
import 'dart:io' show HttpClient, HttpClientResponse, HttpServer;
78

89
import 'package:flutter_tools/src/base/logger.dart';
910
import 'package:flutter_tools/src/isolated/proxy_middleware.dart';
@@ -445,5 +446,50 @@ void main() {
445446
}
446447
},
447448
);
449+
450+
test('should stream the backend response instead of buffering it', () async {
451+
final body = StreamController<List<int>>();
452+
final client = HttpClient();
453+
HttpServer? backendServer;
454+
HttpServer? proxyServer;
455+
try {
456+
backendServer = await shelf_io.serve(
457+
(Request request) =>
458+
Response.ok(body.stream, context: <String, Object>{'shelf.io.buffer_output': false}),
459+
'localhost',
460+
0,
461+
);
462+
final rules = <ProxyRule>[
463+
PrefixProxyRule(prefix: '/api/', target: 'http://localhost:${backendServer.port}/'),
464+
];
465+
proxyServer = await shelf_io.serve(
466+
proxyMiddleware(rules, logger)((Request request) => Response.notFound(null)),
467+
'localhost',
468+
0,
469+
);
470+
471+
body.add(utf8.encode('first\n'));
472+
final HttpClientResponse response = await (await client.getUrl(
473+
Uri.parse('http://localhost:${proxyServer.port}/api/events'),
474+
)).close();
475+
final Stream<String> lines = utf8.decoder.bind(response).transform(const LineSplitter());
476+
final received = <String>[];
477+
await for (final line in lines) {
478+
received.add(line);
479+
// The backend only ends the response once the first line has made it
480+
// through the proxy, so a buffering proxy never delivers it.
481+
if (received.length == 1) {
482+
body.add(utf8.encode('second\n'));
483+
unawaited(body.close());
484+
}
485+
}
486+
487+
expect(received, <String>['first', 'second']);
488+
} finally {
489+
client.close(force: true);
490+
await proxyServer?.close(force: true);
491+
await backendServer?.close(force: true);
492+
}
493+
});
448494
});
449495
}

0 commit comments

Comments
 (0)