Skip to content

Commit

Permalink
update: dart format xhr_transport.dart and update dart sdk to v3.4.0 …
Browse files Browse the repository at this point in the history
…in workflows
  • Loading branch information
minoic committed Aug 20, 2024
1 parent 05756b7 commit c424253
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
sdk: [3.2.0, dev]
sdk: [3.4.0, dev]
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
sdk: [3.2.0, dev]
sdk: [3.4.0, dev]
platform: [vm, chrome]
exclude:
# We only run Chrome tests on Linux. No need to run them
Expand Down
44 changes: 30 additions & 14 deletions lib/src/client/transport/xhr_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ class XhrTransportStream implements GrpcTransportStream {
@override
StreamSink<List<int>> get outgoingMessages => _outgoingMessages.sink;

XhrTransportStream(this._request, {required ErrorHandler onError, required onDone})
XhrTransportStream(this._request,
{required ErrorHandler onError, required onDone})
: _onError = onError,
_onDone = onDone {
_outgoingMessages.stream
.map(frame)
.listen((data) => _request.send(Int8List.fromList(data).toJS), cancelOnError: true, onError: _onError);
_outgoingMessages.stream.map(frame).listen(
(data) => _request.send(Int8List.fromList(data).toJS),
cancelOnError: true,
onError: _onError);

_request.onReadyStateChange.listen((_) {
if (_incomingProcessor.isClosed) {
Expand All @@ -72,7 +74,8 @@ class XhrTransportStream implements GrpcTransportStream {
if (_incomingProcessor.isClosed) {
return;
}
_onError(GrpcError.unavailable('XhrConnection connection-error'), StackTrace.current);
_onError(GrpcError.unavailable('XhrConnection connection-error'),
StackTrace.current);
terminate();
});

Expand All @@ -81,20 +84,24 @@ class XhrTransportStream implements GrpcTransportStream {
return;
}
final responseText = _request.responseText;
final bytes = Uint8List.fromList(responseText.substring(_requestBytesRead).codeUnits).buffer;
final bytes = Uint8List.fromList(
responseText.substring(_requestBytesRead).codeUnits)
.buffer;
_requestBytesRead = responseText.length;
_incomingProcessor.add(bytes);
});

_incomingProcessor.stream
.transform(GrpcWebDecoder())
.transform(grpcDecompressor())
.listen(_incomingMessages.add, onError: _onError, onDone: _incomingMessages.close);
.listen(_incomingMessages.add,
onError: _onError, onDone: _incomingMessages.close);
}

bool _validateResponseState() {
try {
validateHttpStatusAndContentType(_request.status, _parseHeaders(_request.getAllResponseHeaders()),
validateHttpStatusAndContentType(
_request.status, _parseHeaders(_request.getAllResponseHeaders()),
rawResponse: _request.responseText);
return true;
} catch (e, st) {
Expand All @@ -108,15 +115,20 @@ class XhrTransportStream implements GrpcTransportStream {
if (!_validateResponseState()) {
return;
}
_incomingMessages.add(GrpcMetadata(_parseHeaders(_request.getAllResponseHeaders())));
_incomingMessages
.add(GrpcMetadata(_parseHeaders(_request.getAllResponseHeaders())));
}

void _onRequestDone() {
if (!_headersReceived && !_validateResponseState()) {
return;
}
if (_request.status != 200) {
_onError(GrpcError.unavailable('Request failed with status: ${_request.status}', null, _request.responseText),
_onError(
GrpcError.unavailable(
'Request failed with status: ${_request.status}',
null,
_request.responseText),
StackTrace.current);
return;
}
Expand Down Expand Up @@ -162,7 +174,8 @@ class XhrClientConnection implements ClientConnection {
@override
String get scheme => uri.scheme;

void _initializeRequest(XMLHttpRequest request, Map<String, String> metadata) {
void _initializeRequest(
XMLHttpRequest request, Map<String, String> metadata) {
metadata.forEach((key, value) {
request.setRequestHeader(key, value);
});
Expand All @@ -175,7 +188,8 @@ class XhrClientConnection implements ClientConnection {
XMLHttpRequest createHttpRequest() => XMLHttpRequest();

@override
GrpcTransportStream makeRequest(String path, Duration? timeout, Map<String, String> metadata, ErrorHandler onError,
GrpcTransportStream makeRequest(String path, Duration? timeout,
Map<String, String> metadata, ErrorHandler onError,
{CallOptions? callOptions}) {
// gRPC-web headers.
if (_getContentTypeHeader(metadata) == null) {
Expand All @@ -185,7 +199,8 @@ class XhrClientConnection implements ClientConnection {
}

var requestUri = uri.resolve(path);
if (callOptions is WebCallOptions && callOptions.bypassCorsPreflight == true) {
if (callOptions is WebCallOptions &&
callOptions.bypassCorsPreflight == true) {
requestUri = cors.moveHttpHeadersToQueryParam(metadata, requestUri);
}

Expand All @@ -197,7 +212,8 @@ class XhrClientConnection implements ClientConnection {
// Must set headers after calling open().
_initializeRequest(request, metadata);

final transportStream = XhrTransportStream(request, onError: onError, onDone: _removeStream);
final transportStream =
XhrTransportStream(request, onError: onError, onDone: _removeStream);
_requests.add(transportStream);
return transportStream;
}
Expand Down

0 comments on commit c424253

Please sign in to comment.