Skip to content

Refactor: use await for loop instead of Stream.pipe. #8726

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
merged 2 commits into from
May 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor: use await for loop instead of Stream.pipe.
  • Loading branch information
isoos committed May 1, 2025
commit 1fb7961766ceff988c313986ecae6085c2a4e71f
27 changes: 13 additions & 14 deletions app/lib/package/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1825,21 +1825,20 @@ Future _saveTarballToFS(Stream<List<int>> data, String filename) async {
await targetFile.delete();
}
try {
final sink = targetFile.openWrite();
int receivedBytes = 0;
final stream = data.transform<List<int>>(
StreamTransformer<List<int>, List<int>>.fromHandlers(
handleData: (chunk, sink) {
receivedBytes += chunk.length;
if (receivedBytes <= UploadSignerService.maxUploadSize) {
sink.add(chunk);
} else {
sink.addError(PackageRejectedException.archiveTooLarge(
UploadSignerService.maxUploadSize));
}
},
),
);
await stream.pipe(targetFile.openWrite());
await for (final chunk in data) {
receivedBytes += chunk.length;
if (receivedBytes <= UploadSignerService.maxUploadSize) {
sink.add(chunk);
} else {
await sink.close();
throw PackageRejectedException.archiveTooLarge(
UploadSignerService.maxUploadSize);
}
}
await sink.flush();
await sink.close();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the close be in a finally block?

} catch (e, st) {
_logger.warning('An error occurred while streaming tarball to FS.', e, st);
rethrow;
Expand Down