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.

index.js 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StorageEmulator = void 0;
  4. const os_1 = require("os");
  5. const utils = require("../../utils");
  6. const constants_1 = require("../constants");
  7. const types_1 = require("../types");
  8. const server_1 = require("./server");
  9. const files_1 = require("./files");
  10. const emulatorLogger_1 = require("../emulatorLogger");
  11. const manager_1 = require("./rules/manager");
  12. const runtime_1 = require("./rules/runtime");
  13. const utils_1 = require("./rules/utils");
  14. const persistence_1 = require("./persistence");
  15. const upload_1 = require("./upload");
  16. const cloudFunctions_1 = require("./cloudFunctions");
  17. class StorageEmulator {
  18. constructor(args) {
  19. this.args = args;
  20. this._logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.STORAGE);
  21. this._files = new Map();
  22. this._buckets = new Map();
  23. this._rulesRuntime = new runtime_1.StorageRulesRuntime();
  24. this._rulesManager = this.createRulesManager(this.args.rules);
  25. this._cloudFunctions = new cloudFunctions_1.StorageCloudFunctions(args.projectId);
  26. this._persistence = new persistence_1.Persistence(this.getPersistenceTmpDir());
  27. this._uploadService = new upload_1.UploadService(this._persistence);
  28. const createStorageLayer = (rulesValidator) => {
  29. return new files_1.StorageLayer(args.projectId, this._files, this._buckets, rulesValidator, (0, utils_1.getAdminCredentialValidator)(), this._persistence, this._cloudFunctions);
  30. };
  31. this._storageLayer = createStorageLayer((0, utils_1.getFirebaseRulesValidator)((resource) => this._rulesManager.getRuleset(resource)));
  32. this._adminStorageLayer = createStorageLayer((0, utils_1.getAdminOnlyFirebaseRulesValidator)());
  33. }
  34. get storageLayer() {
  35. return this._storageLayer;
  36. }
  37. get adminStorageLayer() {
  38. return this._adminStorageLayer;
  39. }
  40. get uploadService() {
  41. return this._uploadService;
  42. }
  43. get rulesManager() {
  44. return this._rulesManager;
  45. }
  46. get logger() {
  47. return this._logger;
  48. }
  49. reset() {
  50. this._files.clear();
  51. this._buckets.clear();
  52. this._persistence.reset(this.getPersistenceTmpDir());
  53. this._uploadService.reset();
  54. }
  55. async start() {
  56. const { host, port } = this.getInfo();
  57. await this._rulesRuntime.start(this.args.auto_download);
  58. await this._rulesManager.start();
  59. this._app = await (0, server_1.createApp)(this.args.projectId, this);
  60. const server = this._app.listen(port, host);
  61. this.destroyServer = utils.createDestroyer(server);
  62. }
  63. async connect() {
  64. }
  65. async stop() {
  66. await this._persistence.deleteAll();
  67. await this._rulesManager.stop();
  68. return this.destroyServer ? this.destroyServer() : Promise.resolve();
  69. }
  70. getInfo() {
  71. const host = this.args.host || constants_1.Constants.getDefaultHost();
  72. const port = this.args.port || constants_1.Constants.getDefaultPort(types_1.Emulators.STORAGE);
  73. return {
  74. name: this.getName(),
  75. host,
  76. port,
  77. };
  78. }
  79. getName() {
  80. return types_1.Emulators.STORAGE;
  81. }
  82. getApp() {
  83. return this._app;
  84. }
  85. async replaceRules(rules) {
  86. await this._rulesManager.stop();
  87. this._rulesManager = this.createRulesManager(rules);
  88. return this._rulesManager.start();
  89. }
  90. createRulesManager(rules) {
  91. return (0, manager_1.createStorageRulesManager)(rules, this._rulesRuntime);
  92. }
  93. getPersistenceTmpDir() {
  94. return `${(0, os_1.tmpdir)()}/firebase/storage/blobs`;
  95. }
  96. }
  97. exports.StorageEmulator = StorageEmulator;