No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

initMiddleware.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.initMiddleware = void 0;
  4. const apiv2_1 = require("../apiv2");
  5. const logger_1 = require("../logger");
  6. const utils = require("../utils");
  7. const SDK_PATH_REGEXP = /^\/__\/firebase\/([^/]+)\/([^/]+)$/;
  8. function initMiddleware(init) {
  9. return (req, res, next) => {
  10. const parsedUrl = new URL(req.url || "", `http://${req.headers.host}`);
  11. const match = RegExp(SDK_PATH_REGEXP).exec(parsedUrl.pathname);
  12. if (match) {
  13. const version = match[1];
  14. const sdkName = match[2];
  15. const u = new URL(`https://www.gstatic.com/firebasejs/${version}/${sdkName}`);
  16. const c = new apiv2_1.Client({ urlPrefix: u.origin, auth: false });
  17. const headers = {};
  18. const acceptEncoding = req.headers["accept-encoding"];
  19. if (typeof acceptEncoding === "string" && acceptEncoding) {
  20. headers["accept-encoding"] = acceptEncoding;
  21. }
  22. c.request({
  23. method: "GET",
  24. path: u.pathname,
  25. headers,
  26. responseType: "stream",
  27. resolveOnHTTPError: true,
  28. compress: false,
  29. })
  30. .then((sdkRes) => {
  31. if (sdkRes.status === 404) {
  32. return next();
  33. }
  34. for (const [key, value] of Object.entries(sdkRes.response.headers.raw())) {
  35. res.setHeader(key, value);
  36. }
  37. sdkRes.body.pipe(res);
  38. })
  39. .catch((e) => {
  40. utils.logLabeledWarning("hosting", `Could not load Firebase SDK ${sdkName} v${version}, check your internet connection.`);
  41. logger_1.logger.debug(e);
  42. });
  43. }
  44. else if (parsedUrl.pathname === "/__/firebase/init.js") {
  45. const query = parsedUrl.searchParams;
  46. res.setHeader("Content-Type", "application/javascript");
  47. if (query.get("useEmulator") === "true") {
  48. res.end(init.emulatorsJs);
  49. }
  50. else {
  51. res.end(init.js);
  52. }
  53. }
  54. else if (parsedUrl.pathname === "/__/firebase/init.json") {
  55. res.setHeader("Content-Type", "application/json");
  56. res.end(init.json);
  57. }
  58. else {
  59. next();
  60. }
  61. };
  62. }
  63. exports.initMiddleware = initMiddleware;