From 7818ff7dc0b53555a7fb3c3427e6761e88bde3a2 Mon Sep 17 00:00:00 2001 From: Eryu Xia Date: Thu, 2 Nov 2023 10:31:59 -0700 Subject: [PATCH] [webchannel][js] Fix `useFetchStreams` duplicate message dispatch issue. RELNOTES: n/a PiperOrigin-RevId: 578901815 Change-Id: Ifc4357edd504cdb30224e64bfb7fbde9a05b6cd7 --- .../labs/net/webchannel/channelrequest.js | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/closure/goog/labs/net/webchannel/channelrequest.js b/closure/goog/labs/net/webchannel/channelrequest.js index 3c3ed7d627..80de41503e 100644 --- a/closure/goog/labs/net/webchannel/channelrequest.js +++ b/closure/goog/labs/net/webchannel/channelrequest.js @@ -867,7 +867,7 @@ ChannelRequest.prototype.decodeXmlHttpResponse_ = function() { responseText += this.fetchResponseState_.textDecoder.decode( responseChunks[i], {stream: isLastChunk}); } - responseChunks.splice(0, responseLength); + responseChunks.length = 0; // Empty the `responseChunks` array. this.fetchResponseState_.responseBuffer += responseText; this.xmlHttpChunkStart_ = 0; return this.fetchResponseState_.responseBuffer; @@ -906,22 +906,6 @@ ChannelRequest.prototype.useFetchStreamsForResponse_ = function() { }; -/** - * Resets the response buffer if the saved chunk has been processed. - * @private - * @param {string|!Object|undefined} chunkText - */ -ChannelRequest.prototype.maybeResetBuffer_ = function(chunkText) { - 'use strict'; - if (this.useFetchStreamsForResponse_() && - chunkText != ChannelRequest.INCOMPLETE_CHUNK_ && - chunkText != ChannelRequest.INVALID_CHUNK_) { - this.fetchResponseState_.responseBuffer = ''; - this.xmlHttpChunkStart_ = 0; - } -}; - - /** * Decodes the next set of available chunks in the response. * @param {number} readyState The value of readyState. @@ -960,7 +944,12 @@ ChannelRequest.prototype.decodeNextChunks_ = function( } } - this.maybeResetBuffer_(chunkText); + if (this.useFetchStreamsForResponse_() && this.xmlHttpChunkStart_ != 0) { + // Remove processed chunk text from response buffer. + this.fetchResponseState_.responseBuffer = + this.fetchResponseState_.responseBuffer.slice(this.xmlHttpChunkStart_); + this.xmlHttpChunkStart_ = 0; + } if (readyState == goog.net.XmlHttp.ReadyState.COMPLETE && responseText.length == 0 &&