13
13
// limitations under the License.
14
14
15
15
const assert = require ( 'assert' ) ;
16
- const functions = require ( 'firebase-functions' ) ;
16
+ const functionsV1 = require ( 'firebase-functions/v1' ) ;
17
+ const functionsV2 = require ( 'firebase-functions/v2' ) ;
17
18
18
- exports . dataTest = functions . https . onRequest ( ( request , response ) => {
19
+ exports . dataTest = functionsV1 . https . onRequest ( ( request , response ) => {
19
20
assert . deepEqual ( request . body , {
20
21
data : {
21
22
bool : true ,
@@ -41,39 +42,39 @@ exports.dataTest = functions.https.onRequest((request, response) => {
41
42
} ) ;
42
43
} ) ;
43
44
44
- exports . scalarTest = functions . https . onRequest ( ( request , response ) => {
45
+ exports . scalarTest = functionsV1 . https . onRequest ( ( request , response ) => {
45
46
assert . deepEqual ( request . body , { data : 17 } ) ;
46
47
response . send ( { data : 76 } ) ;
47
48
} ) ;
48
49
49
- exports . tokenTest = functions . https . onRequest ( ( request , response ) => {
50
+ exports . tokenTest = functionsV1 . https . onRequest ( ( request , response ) => {
50
51
assert . equal ( 'Bearer token' , request . get ( 'Authorization' ) ) ;
51
52
assert . deepEqual ( request . body , { data : { } } ) ;
52
53
response . send ( { data : { } } ) ;
53
54
} ) ;
54
55
55
- exports . FCMTokenTest = functions . https . onRequest ( ( request , response ) => {
56
+ exports . FCMTokenTest = functionsV1 . https . onRequest ( ( request , response ) => {
56
57
assert . equal ( request . get ( 'Firebase-Instance-ID-Token' ) , 'fakeFCMToken' ) ;
57
58
assert . deepEqual ( request . body , { data : { } } ) ;
58
59
response . send ( { data : { } } ) ;
59
60
} ) ;
60
61
61
- exports . nullTest = functions . https . onRequest ( ( request , response ) => {
62
+ exports . nullTest = functionsV1 . https . onRequest ( ( request , response ) => {
62
63
assert . deepEqual ( request . body , { data : null } ) ;
63
64
response . send ( { data : null } ) ;
64
65
} ) ;
65
66
66
- exports . missingResultTest = functions . https . onRequest ( ( request , response ) => {
67
+ exports . missingResultTest = functionsV1 . https . onRequest ( ( request , response ) => {
67
68
assert . deepEqual ( request . body , { data : null } ) ;
68
69
response . send ( { } ) ;
69
70
} ) ;
70
71
71
- exports . unhandledErrorTest = functions . https . onRequest ( ( request , response ) => {
72
+ exports . unhandledErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
72
73
// Fail in a way that the client shouldn't see.
73
74
throw 'nope' ;
74
75
} ) ;
75
76
76
- exports . unknownErrorTest = functions . https . onRequest ( ( request , response ) => {
77
+ exports . unknownErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
77
78
// Send an http error with a body with an explicit code.
78
79
response . status ( 400 ) . send ( {
79
80
error : {
@@ -83,7 +84,7 @@ exports.unknownErrorTest = functions.https.onRequest((request, response) => {
83
84
} ) ;
84
85
} ) ;
85
86
86
- exports . explicitErrorTest = functions . https . onRequest ( ( request , response ) => {
87
+ exports . explicitErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
87
88
// Send an http error with a body with an explicit code.
88
89
// Note that eventually the SDK will have a helper to automatically return
89
90
// the appropriate http status code for an error.
@@ -103,18 +104,52 @@ exports.explicitErrorTest = functions.https.onRequest((request, response) => {
103
104
} ) ;
104
105
} ) ;
105
106
106
- exports . httpErrorTest = functions . https . onRequest ( ( request , response ) => {
107
+ exports . httpErrorTest = functionsV1 . https . onRequest ( ( request , response ) => {
107
108
// Send an http error with no body.
108
109
response . status ( 400 ) . send ( ) ;
109
110
} ) ;
110
111
111
112
// 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.' ) ;
114
115
} ) ;
115
116
116
- exports . timeoutTest = functions . https . onRequest ( ( request , response ) => {
117
+ exports . timeoutTest = functionsV1 . https . onRequest ( ( request , response ) => {
117
118
// Wait for longer than 500ms.
118
- setTimeout ( ( ) => response . send ( { data : true } ) , 500 ) ;
119
+ setTimeout ( ( ) => response . send ( { data : true } ) , 500 ) ;
119
120
} ) ;
120
121
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