index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2018 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. const assert = require('assert');
  15. const functionsV1 = require('firebase-functions/v1');
  16. const functionsV2 = require('firebase-functions/v2');
  17. // MARK: - Utilities
  18. function sleep(ms) {
  19. return new Promise(resolve => setTimeout(resolve, ms));
  20. };
  21. // MARK: - Callable Functions
  22. exports.dataTest = functionsV1.https.onRequest((request, response) => {
  23. assert.deepEqual(request.body, {
  24. data: {
  25. bool: true,
  26. int: 2,
  27. long: {
  28. value: '9876543210',
  29. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  30. },
  31. string: 'four',
  32. array: [5, 6],
  33. 'null': null,
  34. }
  35. });
  36. response.send({
  37. data: {
  38. message: 'stub response',
  39. code: 42,
  40. long: {
  41. value: '420',
  42. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  43. },
  44. }
  45. });
  46. });
  47. exports.scalarTest = functionsV1.https.onRequest((request, response) => {
  48. assert.deepEqual(request.body, { data: 17 });
  49. response.send({ data: 76 });
  50. });
  51. exports.tokenTest = functionsV1.https.onRequest((request, response) => {
  52. assert.equal('Bearer token', request.get('Authorization'));
  53. assert.deepEqual(request.body, { data: {} });
  54. response.send({ data: {} });
  55. });
  56. exports.FCMTokenTest = functionsV1.https.onRequest((request, response) => {
  57. assert.equal(request.get('Firebase-Instance-ID-Token'), 'fakeFCMToken');
  58. assert.deepEqual(request.body, { data: {} });
  59. response.send({ data: {} });
  60. });
  61. exports.nullTest = functionsV1.https.onRequest((request, response) => {
  62. assert.deepEqual(request.body, { data: null });
  63. response.send({ data: null });
  64. });
  65. exports.missingResultTest = functionsV1.https.onRequest((request, response) => {
  66. assert.deepEqual(request.body, { data: null });
  67. response.send({});
  68. });
  69. exports.unhandledErrorTest = functionsV1.https.onRequest((request, response) => {
  70. // Fail in a way that the client shouldn't see.
  71. throw 'nope';
  72. });
  73. exports.unknownErrorTest = functionsV1.https.onRequest((request, response) => {
  74. // Send an http error with a body with an explicit code.
  75. response.status(400).send({
  76. error: {
  77. status: 'THIS_IS_NOT_VALID',
  78. message: 'this should be ignored',
  79. },
  80. });
  81. });
  82. exports.explicitErrorTest = functionsV1.https.onRequest((request, response) => {
  83. // Send an http error with a body with an explicit code.
  84. // Note that eventually the SDK will have a helper to automatically return
  85. // the appropriate http status code for an error.
  86. response.status(400).send({
  87. error: {
  88. status: 'OUT_OF_RANGE',
  89. message: 'explicit nope',
  90. details: {
  91. start: 10,
  92. end: 20,
  93. long: {
  94. value: '30',
  95. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  96. },
  97. },
  98. },
  99. });
  100. });
  101. exports.httpErrorTest = functionsV1.https.onRequest((request, response) => {
  102. // Send an http error with no body.
  103. response.status(400).send();
  104. });
  105. // Regression test for https://github.com/firebase/firebase-ios-sdk/issues/9855
  106. exports.throwTest = functionsV1.https.onCall((data) => {
  107. throw new functionsV1.https.HttpsError('invalid-argument', 'Invalid test requested.');
  108. });
  109. exports.timeoutTest = functionsV1.https.onRequest((request, response) => {
  110. // Wait for longer than 500ms.
  111. setTimeout(() => response.send({ data: true }), 500);
  112. });
  113. const streamData = ["hello", "world", "this", "is", "cool"]
  114. async function* generateText() {
  115. for (const chunk of streamData) {
  116. yield chunk;
  117. await sleep(100);
  118. }
  119. };
  120. exports.genStream = functionsV2.https.onCall(
  121. async (request, response) => {
  122. if (request.acceptsStreaming) {
  123. for await (const chunk of generateText()) {
  124. response.sendChunk(chunk);
  125. }
  126. }
  127. return streamData.join(" ");
  128. }
  129. );
  130. exports.genStreamError = functionsV2.https.onCall(
  131. async (request, response) => {
  132. // Note: The functions backend does not pass the error message to the
  133. // client at this time.
  134. throw Error("BOOM")
  135. }
  136. );
  137. const weatherForecasts = {
  138. Toronto: { conditions: 'snowy', temperature: 25 },
  139. London: { conditions: 'rainy', temperature: 50 },
  140. Dubai: { conditions: 'sunny', temperature: 75 }
  141. };
  142. async function* generateForecast(locations) {
  143. for (const location of locations) {
  144. yield { 'location': location, ...weatherForecasts[location.name] };
  145. await sleep(100);
  146. }
  147. };
  148. exports.genStreamWeather = functionsV2.https.onCall(
  149. async (request, response) => {
  150. const forecasts = [];
  151. if (request.acceptsStreaming) {
  152. for await (const chunk of generateForecast(request.data)) {
  153. forecasts.push(chunk)
  154. response.sendChunk(chunk);
  155. }
  156. }
  157. return { forecasts };
  158. }
  159. );
  160. exports.genStreamWeatherError = functionsV2.https.onCall(
  161. async (request, response) => {
  162. if (request.acceptsStreaming) {
  163. for await (const chunk of generateForecast(request.data)) {
  164. // Remove the location field, since the SDK cannot decode the message
  165. // if it's there.
  166. delete chunk.location;
  167. response.sendChunk(chunk);
  168. }
  169. }
  170. return "Number of forecasts generated: " + request.data.length;
  171. }
  172. );
  173. exports.genStreamEmpty = functionsV2.https.onCall(
  174. async (request, response) => {
  175. if (request.acceptsStreaming) {
  176. // Send no chunks
  177. }
  178. // Implicitly return null.
  179. }
  180. );
  181. exports.genStreamResultOnly = functionsV2.https.onCall(
  182. async (request, response) => {
  183. if (request.acceptsStreaming) {
  184. // Do not send any chunks.
  185. }
  186. return "Only a result";
  187. }
  188. );
  189. exports.genStreamLargeData = functionsV2.https.onCall(
  190. async (request, response) => {
  191. if (request.acceptsStreaming) {
  192. const largeString = 'A'.repeat(10000);
  193. const chunkSize = 1024;
  194. for (let i = 0; i < largeString.length; i += chunkSize) {
  195. const chunk = largeString.substring(i, i + chunkSize);
  196. response.sendChunk(chunk);
  197. await sleep(100);
  198. }
  199. }
  200. return "Stream Completed";
  201. }
  202. );