1313// limitations under the License.
1414
1515const assert = require ( 'assert' ) ;
16- const functions = require ( 'firebase-functions' ) ;
16+ const functionsV1 = require ( 'firebase-functions/v1' ) ;
17+ const functionsV2 = require ( 'firebase-functions/v2' ) ;
1718
18- exports . dataTest = functions . https . onRequest ( ( request , response ) => {
19+ exports . dataTest = functionsV1 . https . onRequest ( ( request , response ) => {
1920 assert . deepEqual ( request . body , {
2021 data : {
2122 bool : true ,
@@ -41,39 +42,39 @@ exports.dataTest = functions.https.onRequest((request, response) => {
4142 } ) ;
4243} ) ;
4344
44- exports . scalarTest = functions . https . onRequest ( ( request , response ) => {
45+ exports . scalarTest = functionsV1 . https . onRequest ( ( request , response ) => {
4546 assert . deepEqual ( request . body , { data : 17 } ) ;
4647 response . send ( { data : 76 } ) ;
4748} ) ;
4849
49- exports . tokenTest = functions . https . onRequest ( ( request , response ) => {
50+ exports . tokenTest = functionsV1 . https . onRequest ( ( request , response ) => {
5051 assert . equal ( 'Bearer token' , request . get ( 'Authorization' ) ) ;
5152 assert . deepEqual ( request . body , { data : { } } ) ;
5253 response . send ( { data : { } } ) ;
5354} ) ;
5455
55- exports . FCMTokenTest = functions . https . onRequest ( ( request , response ) => {
56+ exports . FCMTokenTest = functionsV1 . https . onRequest ( ( request , response ) => {
5657 assert . equal ( request . get ( 'Firebase-Instance-ID-Token' ) , 'fakeFCMToken' ) ;
5758 assert . deepEqual ( request . body , { data : { } } ) ;
5859 response . send ( { data : { } } ) ;
5960} ) ;
6061
61- exports . nullTest = functions . https . onRequest ( ( request , response ) => {
62+ exports . nullTest = functionsV1 . https . onRequest ( ( request , response ) => {
6263 assert . deepEqual ( request . body , { data : null } ) ;
6364 response . send ( { data : null } ) ;
6465} ) ;
6566
66- exports . missingResultTest = functions . https . onRequest ( ( request , response ) => {
67+ exports . missingResultTest = functionsV1 . https . onRequest ( ( request , response ) => {
6768 assert . deepEqual ( request . body , { data : null } ) ;
6869 response . send ( { } ) ;
6970} ) ;
7071
71- exports . unhandledErrorTest = functions . https . onRequest ( ( request , response ) => {
72+ exports . unhandledErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
7273 // Fail in a way that the client shouldn't see.
7374 throw 'nope' ;
7475} ) ;
7576
76- exports . unknownErrorTest = functions . https . onRequest ( ( request , response ) => {
77+ exports . unknownErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
7778 // Send an http error with a body with an explicit code.
7879 response . status ( 400 ) . send ( {
7980 error : {
@@ -83,7 +84,7 @@ exports.unknownErrorTest = functions.https.onRequest((request, response) => {
8384 } ) ;
8485} ) ;
8586
86- exports . explicitErrorTest = functions . https . onRequest ( ( request , response ) => {
87+ exports . explicitErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
8788 // Send an http error with a body with an explicit code.
8889 // Note that eventually the SDK will have a helper to automatically return
8990 // the appropriate http status code for an error.
@@ -103,18 +104,52 @@ exports.explicitErrorTest = functions.https.onRequest((request, response) => {
103104 } ) ;
104105} ) ;
105106
106- exports . httpErrorTest = functions . https . onRequest ( ( request , response ) => {
107+ exports . httpErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
107108 // Send an http error with no body.
108109 response . status ( 400 ) . send ( ) ;
109110} ) ;
110111
111112// Regression test for https://github.com/firebase/firebase-ios-sdk/issues/9855
112- exports . throwTest = functions . https . onCall ( ( data ) => {
113- throw new functions . https . HttpsError ( 'invalid-argument' , 'Invalid test requested.' ) ;
113+ exports . throwTest = functionsV1 . https . onCall ( ( data ) => {
114+ throw new functionsV1 . https . HttpsError ( 'invalid-argument' , 'Invalid test requested.' ) ;
114115} ) ;
115116
116- exports . timeoutTest = functions . https . onRequest ( ( request , response ) => {
117+ exports . timeoutTest = functionsV1 . https . onRequest ( ( request , response ) => {
117118 // Wait for longer than 500ms.
118- setTimeout ( ( ) => response . send ( { data : true } ) , 500 ) ;
119+ setTimeout ( ( ) => response . send ( { data : true } ) , 500 ) ;
119120} ) ;
120121
122+ const streamData = [ "hello" , "world" , "this" , "is" , "cool" ]
123+
124+ function sleep ( ms ) {
125+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
126+ } ;
127+
128+ async function * generateText ( ) {
129+ for ( const chunk of streamData ) {
130+ yield chunk ;
131+ await sleep ( 1000 ) ;
132+ }
133+ } ;
134+
135+ exports . genStream = functionsV2 . https . onCall (
136+ async ( request , response ) => {
137+ if ( request . acceptsStreaming ) {
138+ for await ( const chunk of generateText ( ) ) {
139+ response . sendChunk ( { chunk } ) ;
140+ }
141+ }
142+ return data . join ( " " ) ;
143+ }
144+ ) ;
145+
146+ exports . genStreamError = functionsV2 . https . onCall (
147+ async ( request , response ) => {
148+ if ( request . acceptsStreaming ) {
149+ for await ( const chunk of generateText ( ) ) {
150+ response . write ( { chunk } ) ;
151+ }
152+ throw Error ( "BOOM" )
153+ }
154+ }
155+ ) ;
0 commit comments