Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Next

- Added `Body.size` for exposing known body byte lengths without consuming the
body.

## 0.5.0

- BREAKING: `Request.method` and `RequestInit.method` now use `String` values
Expand Down
15 changes: 14 additions & 1 deletion lib/src/fetch/body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ class Body extends Stream<Uint8List> {
Body._({
block.Block? blockHost,
Stream<Uint8List>? streamHost,
int? byteLength,
this.contentType,
}) : assert(blockHost != null || streamHost != null),
assert(byteLength == null || byteLength >= 0),
size = byteLength ?? blockHost?.size,
_blockHost = blockHost,
_streamHost = streamHost;

Expand Down Expand Up @@ -79,6 +82,7 @@ class Body extends Stream<Uint8List> {
final encoded = formData.encodeMultipart();
return Body._(
streamHost: encoded.stream,
byteLength: encoded.contentLength,
contentType: encoded.contentType,
);
case final Stream<List<int>> stream:
Expand All @@ -103,6 +107,11 @@ class Body extends Stream<Uint8List> {
/// The body-derived media type, when extracting the body produced one.
final String? contentType;

/// The byte length when it is known without consuming the body.
///
/// Stream-backed bodies created from arbitrary [Stream] values return `null`.
final int? size;

Stream<Uint8List> get stream async* {
final blockHost = _blockHost;
final streamHost = _streamHost;
Expand Down Expand Up @@ -169,7 +178,11 @@ class Body extends Stream<Uint8List> {
if (streamHost != null) {
final (left, right) = streamTee(streamHost);
_streamHost = left;
return Body._(streamHost: right, contentType: contentType);
return Body._(
streamHost: right,
byteLength: size,
contentType: contentType,
);
}

throw StateError('Body has no host.');
Expand Down
26 changes: 26 additions & 0 deletions test/body_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ void main() {
expect(Body([1, 2, 3]).contentType, isNull);
});

test('exposes known byte size without consuming the body', () {
final params = URLSearchParams({'a': '1', 'b': '2'});
final formData = FormData()..append('a', Multipart.text('1'));
final formBody = Body(formData);

expect(Body().size, 0);
expect(Body('hello').size, 5);
expect(Body(Uint8List.fromList([1, 2, 3])).size, 3);
expect(Body(Uint8List(2).buffer).size, 2);
expect(Body([1, 2, 3, 4]).size, 4);
expect(
Body(block.Block(<Object>['payload'], type: 'text/plain')).size,
7,
);
expect(Body(params).size, 7);
expect(formBody.size, greaterThan(0));
expect(formBody.bodyUsed, isFalse);
});

test('reports null size for arbitrary stream bodies', () {
final body = Body(Stream<List<int>>.value(utf8.encode('stream')));

expect(body.size, isNull);
expect(body.bodyUsed, isFalse);
});

test('block bodies can be converted back to Blob', () async {
final body = Body(block.Block(<Object>['payload'], type: 'text/plain'));
final blob = await body.blob();
Expand Down
2 changes: 2 additions & 0 deletions test/public_api_surface_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void main() {
final file = File(<Object>[blob], 'hello.txt', type: 'text/plain');
final form = FormData()..append('file', Multipart.blob(file));
final multipart = form.encodeMultipart(boundary: 'api');
final body = Body('public');
final blockBody = block.Block(<Object>['block-body'], type: 'text/plain');

final request = Request(
Expand All @@ -41,6 +42,7 @@ void main() {
expect(requestInit.method, 'POST');
expect(requestInit.priority, RequestPriority.high);
expect(responseInit.status, 200);
expect(body.size, 6);
expect(request.headers.has('content-type'), isTrue);
expect(await multipart.bytes(), isNotEmpty);
expect(await response.text(), 'block-body');
Expand Down