From b96174d219e90ccf6e516e6d7f39a6f4ee9f4416 Mon Sep 17 00:00:00 2001 From: minoic Date: Fri, 16 Aug 2024 07:24:45 +0000 Subject: [PATCH 1/5] update: Migrate off legacy JS/HTML apis --- lib/grpc_or_grpcweb.dart | 4 +- lib/src/client/transport/xhr_transport.dart | 77 +++++++++++---------- pubspec.yaml | 1 + 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/lib/grpc_or_grpcweb.dart b/lib/grpc_or_grpcweb.dart index b23bed5d..dffbde2e 100644 --- a/lib/grpc_or_grpcweb.dart +++ b/lib/grpc_or_grpcweb.dart @@ -13,8 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import 'src/client/grpc_or_grpcweb_channel_grpc.dart' - if (dart.library.html) 'src/client/grpc_or_grpcweb_channel_web.dart'; +import 'src/client/grpc_or_grpcweb_channel_web.dart' + if (dart.library.io) 'src/client/grpc_or_grpcweb_channel_grpc.dart'; import 'src/client/http2_channel.dart'; import 'src/client/options.dart'; diff --git a/lib/src/client/transport/xhr_transport.dart b/lib/src/client/transport/xhr_transport.dart index 16b0dca5..d3293e8e 100644 --- a/lib/src/client/transport/xhr_transport.dart +++ b/lib/src/client/transport/xhr_transport.dart @@ -14,10 +14,11 @@ // limitations under the License. import 'dart:async'; -import 'dart:html'; +import 'dart:js_interop'; import 'dart:typed_data'; import 'package:meta/meta.dart'; +import 'package:web/web.dart'; import '../../client/call.dart'; import '../../shared/message.dart'; @@ -30,7 +31,7 @@ import 'web_streams.dart'; const _contentTypeKey = 'Content-Type'; class XhrTransportStream implements GrpcTransportStream { - final HttpRequest _request; + final XMLHttpRequest _request; final ErrorHandler _onError; final Function(XhrTransportStream stream) _onDone; bool _headersReceived = false; @@ -45,23 +46,22 @@ class XhrTransportStream implements GrpcTransportStream { @override StreamSink> 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(data), cancelOnError: true); + .listen((data) => _request.send(Int8List.fromList(data).toJS), cancelOnError: true, onError: _onError); - _request.onReadyStateChange.listen((data) { + _request.onReadyStateChange.listen((_) { if (_incomingProcessor.isClosed) { return; } switch (_request.readyState) { - case HttpRequest.HEADERS_RECEIVED: + case 2: _onHeadersReceived(); break; - case HttpRequest.DONE: + case 4: _onRequestDone(); _close(); break; @@ -72,8 +72,7 @@ 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(); }); @@ -81,27 +80,21 @@ class XhrTransportStream implements GrpcTransportStream { if (_incomingProcessor.isClosed) { return; } - // Use response over responseText as most browsers don't support - // using responseText during an onProgress event. - final responseString = _request.response as String; - final bytes = Uint8List.fromList( - responseString.substring(_requestBytesRead).codeUnits) - .buffer; - _requestBytesRead = responseString.length; + final responseText = _request.responseText; + 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, _request.responseHeaders, + validateHttpStatusAndContentType(_request.status, _parseHeaders(_request.getAllResponseHeaders()), rawResponse: _request.responseText); return true; } catch (e, st) { @@ -115,17 +108,15 @@ class XhrTransportStream implements GrpcTransportStream { if (!_validateResponseState()) { return; } - _incomingMessages.add(GrpcMetadata(_request.responseHeaders)); + _incomingMessages.add(GrpcMetadata(_parseHeaders(_request.getAllResponseHeaders()))); } void _onRequestDone() { if (!_headersReceived && !_validateResponseState()) { return; } - if (_request.response == null) { - _onError( - GrpcError.unavailable('XhrConnection request null response', null, - _request.responseText), + if (_request.status != 200) { + _onError(GrpcError.unavailable('Request failed with status: ${_request.status}', null, _request.responseText), StackTrace.current); return; } @@ -137,6 +128,20 @@ class XhrTransportStream implements GrpcTransportStream { _onDone(this); } + Map _parseHeaders(String rawHeaders) { + final headers = {}; + final lines = rawHeaders.split('\r\n'); + for (var line in lines) { + final index = line.indexOf(': '); + if (index != -1) { + final key = line.substring(0, index); + final value = line.substring(index + 2); + headers[key] = value; + } + } + return headers; + } + @override Future terminate() async { _close(); @@ -153,24 +158,24 @@ class XhrClientConnection implements ClientConnection { @override String get authority => uri.authority; + @override String get scheme => uri.scheme; - void _initializeRequest(HttpRequest request, Map metadata) { - for (final header in metadata.keys) { - request.setRequestHeader(header, metadata[header]!); - } + void _initializeRequest(XMLHttpRequest request, Map metadata) { + metadata.forEach((key, value) { + request.setRequestHeader(key, value); + }); // Overriding the mimetype allows us to stream and parse the data request.overrideMimeType('text/plain; charset=x-user-defined'); request.responseType = 'text'; } @visibleForTesting - HttpRequest createHttpRequest() => HttpRequest(); + XMLHttpRequest createHttpRequest() => XMLHttpRequest(); @override - GrpcTransportStream makeRequest(String path, Duration? timeout, - Map metadata, ErrorHandler onError, + GrpcTransportStream makeRequest(String path, Duration? timeout, Map metadata, ErrorHandler onError, {CallOptions? callOptions}) { // gRPC-web headers. if (_getContentTypeHeader(metadata) == null) { @@ -180,8 +185,7 @@ 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); } @@ -193,8 +197,7 @@ 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; } diff --git a/pubspec.yaml b/pubspec.yaml index de16e0fa..f455f33e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: http2: ^2.2.0 protobuf: '>=2.0.0 <4.0.0' clock: ^1.1.1 + web: ^1.0.0 dev_dependencies: build_runner: ^2.0.0 From 05756b736fab8dbbf193ae68e4867bda960dc041 Mon Sep 17 00:00:00 2001 From: minoic Date: Tue, 20 Aug 2024 10:44:37 +0800 Subject: [PATCH 2/5] update: use dart.library.js_interop in place of dart.library.html --- CHANGELOG.md | 4 ++++ lib/grpc_or_grpcweb.dart | 4 ++-- pubspec.yaml | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 398f2c81..494d4bad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.0.1 + +* Update xhr transport to migrate off legacy JS/HTML apis. + ## 4.0.0 * Set compressed flag correctly for grpc-encoding = identity. Fixes [#669](https://github.com/grpc/grpc-dart/issues/669) (https://github.com/grpc/grpc-dart/pull/693) diff --git a/lib/grpc_or_grpcweb.dart b/lib/grpc_or_grpcweb.dart index dffbde2e..049ceabf 100644 --- a/lib/grpc_or_grpcweb.dart +++ b/lib/grpc_or_grpcweb.dart @@ -13,8 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import 'src/client/grpc_or_grpcweb_channel_web.dart' - if (dart.library.io) 'src/client/grpc_or_grpcweb_channel_grpc.dart'; +import 'src/client/grpc_or_grpcweb_channel_grpc.dart' + if (dart.library.js_interop) 'src/client/grpc_or_grpcweb_channel_web.dart'; import 'src/client/http2_channel.dart'; import 'src/client/options.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index f455f33e..7dcca90b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,11 @@ name: grpc description: Dart implementation of gRPC, a high performance, open-source universal RPC framework. -version: 4.0.0 +version: 4.0.1 repository: https://github.com/grpc/grpc-dart environment: - sdk: ^3.2.0 + sdk: ^3.4.0 dependencies: async: ^2.5.0 From c424253a30bebadb5ffd05533068631e29a03285 Mon Sep 17 00:00:00 2001 From: minoic Date: Tue, 20 Aug 2024 13:17:13 +0800 Subject: [PATCH 3/5] update: dart format xhr_transport.dart and update dart sdk to v3.4.0 in workflows --- .github/workflows/dart.yml | 4 +- lib/src/client/transport/xhr_transport.dart | 44 ++++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index 2f58e625..81c03343 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -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 @@ -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 diff --git a/lib/src/client/transport/xhr_transport.dart b/lib/src/client/transport/xhr_transport.dart index d3293e8e..69cb78a8 100644 --- a/lib/src/client/transport/xhr_transport.dart +++ b/lib/src/client/transport/xhr_transport.dart @@ -46,12 +46,14 @@ class XhrTransportStream implements GrpcTransportStream { @override StreamSink> 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) { @@ -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(); }); @@ -81,7 +84,9 @@ 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); }); @@ -89,12 +94,14 @@ class XhrTransportStream implements GrpcTransportStream { _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) { @@ -108,7 +115,8 @@ class XhrTransportStream implements GrpcTransportStream { if (!_validateResponseState()) { return; } - _incomingMessages.add(GrpcMetadata(_parseHeaders(_request.getAllResponseHeaders()))); + _incomingMessages + .add(GrpcMetadata(_parseHeaders(_request.getAllResponseHeaders()))); } void _onRequestDone() { @@ -116,7 +124,11 @@ class XhrTransportStream implements GrpcTransportStream { 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; } @@ -162,7 +174,8 @@ class XhrClientConnection implements ClientConnection { @override String get scheme => uri.scheme; - void _initializeRequest(XMLHttpRequest request, Map metadata) { + void _initializeRequest( + XMLHttpRequest request, Map metadata) { metadata.forEach((key, value) { request.setRequestHeader(key, value); }); @@ -175,7 +188,8 @@ class XhrClientConnection implements ClientConnection { XMLHttpRequest createHttpRequest() => XMLHttpRequest(); @override - GrpcTransportStream makeRequest(String path, Duration? timeout, Map metadata, ErrorHandler onError, + GrpcTransportStream makeRequest(String path, Duration? timeout, + Map metadata, ErrorHandler onError, {CallOptions? callOptions}) { // gRPC-web headers. if (_getContentTypeHeader(metadata) == null) { @@ -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); } @@ -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; } From 4621ac7a7fcd1ae4159e58cb554c7712962fce7f Mon Sep 17 00:00:00 2001 From: minoic Date: Tue, 3 Sep 2024 01:04:07 +0800 Subject: [PATCH 4/5] update: use if instead of switch case in xhr_transport.dart --- lib/src/client/transport/xhr_transport.dart | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/src/client/transport/xhr_transport.dart b/lib/src/client/transport/xhr_transport.dart index 69cb78a8..1de0eb78 100644 --- a/lib/src/client/transport/xhr_transport.dart +++ b/lib/src/client/transport/xhr_transport.dart @@ -59,14 +59,12 @@ class XhrTransportStream implements GrpcTransportStream { if (_incomingProcessor.isClosed) { return; } - switch (_request.readyState) { - case 2: - _onHeadersReceived(); - break; - case 4: - _onRequestDone(); - _close(); - break; + // TODO: dart-lang/web#285 use 'if' for now + if (_request.readyState == XMLHttpRequest.HEADERS_RECEIVED) { + _onHeadersReceived(); + } else if (_request.readyState == XMLHttpRequest.DONE) { + _onRequestDone(); + _close(); } }); From 2cda92b73cc8fe5426ba2ec3336e0ed50637d819 Mon Sep 17 00:00:00 2001 From: minoic Date: Fri, 27 Sep 2024 11:56:07 +0800 Subject: [PATCH 5/5] update: upgrade web package to v1.1.0 --- CHANGELOG.md | 10 ++++++- lib/src/client/transport/xhr_transport.dart | 33 +++++++-------------- pubspec.yaml | 2 +- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 494d4bad..d93dd2a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,15 @@ -## 4.0.1 +## 4.0.2-wip +* Internal optimization to client code. +* Small fixes, such as ports in testing and enabling `timeline_test.dart`. +* When the keep alive manager runs into a timeout, it will finish the transport instead of closing + the connection, as defined in the gRPC spec. * Update xhr transport to migrate off legacy JS/HTML apis. +## 4.0.1 + +* Fix header and trailing not completing if the call is terminated. Fixes [#727](https://github.com/grpc/grpc-dart/issues/727) + ## 4.0.0 * Set compressed flag correctly for grpc-encoding = identity. Fixes [#669](https://github.com/grpc/grpc-dart/issues/669) (https://github.com/grpc/grpc-dart/pull/693) diff --git a/lib/src/client/transport/xhr_transport.dart b/lib/src/client/transport/xhr_transport.dart index 1de0eb78..e41a7f39 100644 --- a/lib/src/client/transport/xhr_transport.dart +++ b/lib/src/client/transport/xhr_transport.dart @@ -59,12 +59,14 @@ class XhrTransportStream implements GrpcTransportStream { if (_incomingProcessor.isClosed) { return; } - // TODO: dart-lang/web#285 use 'if' for now - if (_request.readyState == XMLHttpRequest.HEADERS_RECEIVED) { - _onHeadersReceived(); - } else if (_request.readyState == XMLHttpRequest.DONE) { - _onRequestDone(); - _close(); + switch (_request.readyState) { + case XMLHttpRequest.HEADERS_RECEIVED: + _onHeadersReceived(); + break; + case XMLHttpRequest.DONE: + _onRequestDone(); + _close(); + break; } }); @@ -99,7 +101,7 @@ class XhrTransportStream implements GrpcTransportStream { bool _validateResponseState() { try { validateHttpStatusAndContentType( - _request.status, _parseHeaders(_request.getAllResponseHeaders()), + _request.status, _request.responseHeaders, rawResponse: _request.responseText); return true; } catch (e, st) { @@ -113,8 +115,7 @@ class XhrTransportStream implements GrpcTransportStream { if (!_validateResponseState()) { return; } - _incomingMessages - .add(GrpcMetadata(_parseHeaders(_request.getAllResponseHeaders()))); + _incomingMessages.add(GrpcMetadata(_request.responseHeaders)); } void _onRequestDone() { @@ -138,20 +139,6 @@ class XhrTransportStream implements GrpcTransportStream { _onDone(this); } - Map _parseHeaders(String rawHeaders) { - final headers = {}; - final lines = rawHeaders.split('\r\n'); - for (var line in lines) { - final index = line.indexOf(': '); - if (index != -1) { - final key = line.substring(0, index); - final value = line.substring(index + 2); - headers[key] = value; - } - } - return headers; - } - @override Future terminate() async { _close(); diff --git a/pubspec.yaml b/pubspec.yaml index 7dcca90b..c7183bef 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,7 +17,7 @@ dependencies: http2: ^2.2.0 protobuf: '>=2.0.0 <4.0.0' clock: ^1.1.1 - web: ^1.0.0 + web: ^1.1.0 dev_dependencies: build_runner: ^2.0.0