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.

server.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createApp = void 0;
  4. const cors = require("cors");
  5. const express = require("express");
  6. const emulatorLogger_1 = require("../emulatorLogger");
  7. const types_1 = require("../types");
  8. const bodyParser = require("body-parser");
  9. const gcloud_1 = require("./apis/gcloud");
  10. const firebase_1 = require("./apis/firebase");
  11. const errors_1 = require("../auth/errors");
  12. function createApp(defaultProjectId, emulator) {
  13. const { storageLayer } = emulator;
  14. const app = express();
  15. emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.STORAGE).log("DEBUG", `Temp file directory for storage emulator: ${storageLayer.dirPath}`);
  16. app.use("/", (req, res, next) => {
  17. if (req.headers["access-control-request-private-network"]) {
  18. res.setHeader("access-control-allow-private-network", "true");
  19. }
  20. next();
  21. });
  22. app.use(cors({
  23. origin: true,
  24. exposedHeaders: [
  25. "content-type",
  26. "x-firebase-storage-version",
  27. "X-Goog-Upload-Size-Received",
  28. "x-goog-upload-url",
  29. "x-goog-upload-command",
  30. "x-gupload-uploadid",
  31. "x-goog-upload-header-content-length",
  32. "x-goog-upload-header-content-type",
  33. "x-goog-upload-protocol",
  34. "x-goog-upload-status",
  35. "x-goog-upload-chunk-granularity",
  36. "x-goog-upload-control-url",
  37. ],
  38. }));
  39. app.use(bodyParser.raw({ limit: "130mb", type: "application/x-www-form-urlencoded" }));
  40. app.use(bodyParser.raw({ limit: "130mb", type: "multipart/related" }));
  41. app.use(express.json({
  42. type: ["application/json"],
  43. }));
  44. app.post("/internal/export", async (req, res) => {
  45. const initiatedBy = req.body.initiatedBy || "unknown";
  46. const path = req.body.path;
  47. if (!path) {
  48. res.status(400).send("Export request body must include 'path'.");
  49. return;
  50. }
  51. await storageLayer.export(path, { initiatedBy });
  52. res.sendStatus(200);
  53. });
  54. app.put("/internal/setRules", async (req, res) => {
  55. const rulesRaw = req.body.rules;
  56. if (!(rulesRaw && Array.isArray(rulesRaw.files) && rulesRaw.files.length > 0)) {
  57. res.status(400).json({
  58. message: "Request body must include 'rules.files' array",
  59. });
  60. return;
  61. }
  62. const { files } = rulesRaw;
  63. function parseRulesFromFiles(files) {
  64. if (files.length === 1) {
  65. const file = files[0];
  66. if (!isRulesFile(file)) {
  67. throw new errors_1.InvalidArgumentError("Each member of 'rules.files' array must contain 'name' and 'content'");
  68. }
  69. return { name: file.name, content: file.content };
  70. }
  71. const rules = [];
  72. for (const file of files) {
  73. if (!isRulesFile(file) || !file.resource) {
  74. throw new errors_1.InvalidArgumentError("Each member of 'rules.files' array must contain 'name', 'content', and 'resource'");
  75. }
  76. rules.push({ resource: file.resource, rules: { name: file.name, content: file.content } });
  77. }
  78. return rules;
  79. }
  80. let rules;
  81. try {
  82. rules = parseRulesFromFiles(files);
  83. }
  84. catch (err) {
  85. if (err instanceof errors_1.InvalidArgumentError) {
  86. res.status(400).json({ message: err.message });
  87. return;
  88. }
  89. throw err;
  90. }
  91. const issues = await emulator.replaceRules(rules);
  92. if (issues.errors.length > 0) {
  93. res.status(400).json({
  94. message: "There was an error updating rules, see logs for more details",
  95. });
  96. return;
  97. }
  98. res.status(200).json({
  99. message: "Rules updated successfully",
  100. });
  101. });
  102. app.post("/internal/reset", (req, res) => {
  103. emulator.reset();
  104. res.sendStatus(200);
  105. });
  106. app.use("/v0", (0, firebase_1.createFirebaseEndpoints)(emulator));
  107. app.use("/", (0, gcloud_1.createCloudEndpoints)(emulator));
  108. return Promise.resolve(app);
  109. }
  110. exports.createApp = createApp;
  111. function isRulesFile(file) {
  112. return (typeof file.name === "string" && typeof file.content === "string");
  113. }