Skip to content

Commit b0c36d9

Browse files
committed
Couple of improvements on streaming
- Better code organization to make it easier to know which headers are being set - Process events from the buffer in case there's no empty line at the end
1 parent 36df125 commit b0c36d9

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

firebase-functions/src/main/java/com/google/firebase/functions/PublisherStream.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ internal class PublisherStream(
136136
MediaType.parse("application/json"),
137137
JSONObject(mapOf("data" to serializer.encode(data))).toString()
138138
)
139-
val requestBuilder =
140-
Request.Builder().url(url).post(requestBody).header("Accept", "text/event-stream")
141-
context?.authToken?.let { requestBuilder.header("Authorization", "Bearer $it") }
142-
context?.instanceIdToken?.let { requestBuilder.header("Firebase-Instance-ID-Token", it) }
143-
context?.appCheckToken?.let { requestBuilder.header("X-Firebase-AppCheck", it) }
144-
val request = requestBuilder.build()
139+
val request = Request.Builder().url(url).post(requestBody).apply {
140+
header("Accept", "text/event-stream")
141+
header("Content-Type", "application/json")
142+
context?.apply {
143+
authToken?.let { header("Authorization", "Bearer $it") }
144+
instanceIdToken?.let { header("Firebase-Instance-ID-Token", it) }
145+
appCheckToken?.let { header("X-Firebase-AppCheck", it) }
146+
}
147+
}.build()
145148
val call = configuredClient.newCall(request)
146149
activeCall = call
147150

@@ -206,6 +209,9 @@ internal class PublisherStream(
206209
eventBuffer.append(dataChunk.trim()).append("\n")
207210
}
208211
}
212+
if (eventBuffer.isNotEmpty()) {
213+
processEvent(eventBuffer.toString())
214+
}
209215
} catch (e: Exception) {
210216
notifyError(
211217
FirebaseFunctionsException(

0 commit comments

Comments
 (0)