@@ -151,32 +151,19 @@ async function* generateText() {
151151exports . genStream = functionsV2 . https . onCall ( async ( request , response ) => {
152152 if ( request . acceptsStreaming ) {
153153 for await ( const chunk of generateText ( ) ) {
154- response . sendChunk ( { chunk} ) ;
154+ response . sendChunk ( chunk ) ;
155155 }
156156 }
157157 return streamData . join ( ' ' ) ;
158158} ) ;
159159
160160exports . genStreamError = functionsV2 . https . onCall (
161161 async ( request , response ) => {
162- if ( request . acceptsStreaming ) {
163- for await ( const chunk of generateText ( ) ) {
164- response . sendChunk ( { chunk} ) ;
165- }
166- }
167- throw new Error ( 'BOOM' ) ;
162+ // Note: The functions backend does not pass the error message to the
163+ // client at this time.
164+ throw Error ( "BOOM" )
168165 } ) ;
169166
170- exports . genStreamNoReturn = functionsV2 . https . onCall (
171- async ( request , response ) => {
172- if ( request . acceptsStreaming ) {
173- for await ( const chunk of generateText ( ) ) {
174- response . sendChunk ( { chunk} ) ;
175- }
176- }
177- } ,
178- ) ;
179-
180167const weatherForecasts = {
181168 Toronto : { conditions : 'snowy' , temperature : 25 } ,
182169 London : { conditions : 'rainy' , temperature : 50 } ,
@@ -209,3 +196,36 @@ exports.genStreamWeather = functionsV2.https.onCall(
209196 }
210197 return { forecasts} ;
211198 } ) ;
199+
200+ exports . genStreamEmpty = functionsV2 . https . onCall (
201+ async ( request , response ) => {
202+ if ( request . acceptsStreaming ) {
203+ // Send no chunks
204+ }
205+ // Implicitly return null.
206+ }
207+ ) ;
208+
209+ exports . genStreamResultOnly = functionsV2 . https . onCall (
210+ async ( request , response ) => {
211+ if ( request . acceptsStreaming ) {
212+ // Do not send any chunks.
213+ }
214+ return "Only a result" ;
215+ }
216+ ) ;
217+
218+ exports . genStreamLargeData = functionsV2 . https . onCall (
219+ async ( request , response ) => {
220+ if ( request . acceptsStreaming ) {
221+ const largeString = 'A' . repeat ( 10000 ) ;
222+ const chunkSize = 1024 ;
223+ for ( let i = 0 ; i < largeString . length ; i += chunkSize ) {
224+ const chunk = largeString . substring ( i , i + chunkSize ) ;
225+ response . sendChunk ( chunk ) ;
226+ await sleep ( 100 ) ;
227+ }
228+ }
229+ return "Stream Completed" ;
230+ }
231+ ) ;
0 commit comments