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.

databaseEmulator.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DatabaseEmulator = void 0;
  4. const chokidar = require("chokidar");
  5. const clc = require("colorette");
  6. const fs = require("fs");
  7. const path = require("path");
  8. const http = require("http");
  9. const downloadableEmulators = require("./downloadableEmulators");
  10. const types_1 = require("../emulator/types");
  11. const constants_1 = require("./constants");
  12. const registry_1 = require("./registry");
  13. const emulatorLogger_1 = require("./emulatorLogger");
  14. const error_1 = require("../error");
  15. const parseBoltRules_1 = require("../parseBoltRules");
  16. const utils_1 = require("../utils");
  17. class DatabaseEmulator {
  18. constructor(args) {
  19. this.args = args;
  20. this.importedNamespaces = [];
  21. this.logger = emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.DATABASE);
  22. }
  23. async start() {
  24. const functionsInfo = registry_1.EmulatorRegistry.getInfo(types_1.Emulators.FUNCTIONS);
  25. if (functionsInfo) {
  26. this.args.functions_emulator_host = functionsInfo.host;
  27. this.args.functions_emulator_port = functionsInfo.port;
  28. }
  29. if (this.args.rules) {
  30. for (const c of this.args.rules) {
  31. if (!c.instance) {
  32. this.logger.log("DEBUG", `args.rules=${JSON.stringify(this.args.rules)}`);
  33. this.logger.logLabeled("WARN_ONCE", "database", "Could not determine your Realtime Database instance name, so rules hot reloading is disabled.");
  34. continue;
  35. }
  36. this.rulesWatcher = chokidar.watch(c.rules, { persistent: true, ignoreInitial: true });
  37. this.rulesWatcher.on("change", async () => {
  38. await new Promise((res) => setTimeout(res, 5));
  39. this.logger.logLabeled("BULLET", "database", `Change detected, updating rules for ${c.instance}...`);
  40. try {
  41. await this.updateRules(c.instance, c.rules);
  42. this.logger.logLabeled("SUCCESS", "database", "Rules updated.");
  43. }
  44. catch (e) {
  45. this.logger.logLabeled("WARN", "database", this.prettyPrintRulesError(c.rules, e));
  46. this.logger.logLabeled("WARN", "database", "Failed to update rules");
  47. }
  48. });
  49. }
  50. }
  51. return downloadableEmulators.start(types_1.Emulators.DATABASE, this.args);
  52. }
  53. async connect() {
  54. if (this.args.rules) {
  55. for (const c of this.args.rules) {
  56. if (!c.instance) {
  57. continue;
  58. }
  59. try {
  60. await this.updateRules(c.instance, c.rules);
  61. }
  62. catch (e) {
  63. const rulesError = this.prettyPrintRulesError(c.rules, e);
  64. this.logger.logLabeled("WARN", "database", rulesError);
  65. this.logger.logLabeled("WARN", "database", "Failed to update rules");
  66. throw new error_1.FirebaseError(`Failed to load initial ${constants_1.Constants.description(this.getName())} rules:\n${rulesError}`);
  67. }
  68. }
  69. }
  70. }
  71. stop() {
  72. return downloadableEmulators.stop(types_1.Emulators.DATABASE);
  73. }
  74. getInfo() {
  75. const host = this.args.host || constants_1.Constants.getDefaultHost();
  76. const port = this.args.port || constants_1.Constants.getDefaultPort(types_1.Emulators.DATABASE);
  77. return {
  78. name: this.getName(),
  79. host,
  80. port,
  81. pid: downloadableEmulators.getPID(types_1.Emulators.DATABASE),
  82. };
  83. }
  84. getName() {
  85. return types_1.Emulators.DATABASE;
  86. }
  87. getImportedNamespaces() {
  88. return this.importedNamespaces;
  89. }
  90. async importData(ns, fPath) {
  91. this.logger.logLabeled("BULLET", "database", `Importing data from ${fPath}`);
  92. const readStream = fs.createReadStream(fPath);
  93. const { host, port } = this.getInfo();
  94. await new Promise((resolve, reject) => {
  95. const req = http.request({
  96. method: "PUT",
  97. host: (0, utils_1.connectableHostname)(host),
  98. port,
  99. path: `/.json?ns=${ns}&disableTriggers=true&writeSizeLimit=unlimited`,
  100. headers: {
  101. Authorization: "Bearer owner",
  102. "Content-Type": "application/json",
  103. },
  104. }, (response) => {
  105. if (response.statusCode === 200) {
  106. this.importedNamespaces.push(ns);
  107. resolve();
  108. }
  109. else {
  110. this.logger.log("DEBUG", "Database import failed: " + response.statusCode);
  111. response
  112. .on("data", (d) => {
  113. this.logger.log("DEBUG", d.toString());
  114. })
  115. .on("end", reject);
  116. }
  117. });
  118. req.on("error", reject);
  119. readStream.pipe(req, { end: true });
  120. }).catch((e) => {
  121. throw new error_1.FirebaseError("Error during database import.", { original: e, exit: 1 });
  122. });
  123. }
  124. async updateRules(instance, rulesPath) {
  125. var _a;
  126. const rulesExt = path.extname(rulesPath);
  127. const content = rulesExt === ".bolt"
  128. ? (0, parseBoltRules_1.parseBoltRules)(rulesPath).toString()
  129. : fs.readFileSync(rulesPath, "utf8");
  130. try {
  131. await registry_1.EmulatorRegistry.client(types_1.Emulators.DATABASE).put(`/.settings/rules.json`, content, {
  132. headers: { Authorization: "Bearer owner" },
  133. queryParams: { ns: instance },
  134. });
  135. }
  136. catch (e) {
  137. if (e.context && e.context.body) {
  138. throw e.context.body.error;
  139. }
  140. throw (_a = e.original) !== null && _a !== void 0 ? _a : e;
  141. }
  142. }
  143. prettyPrintRulesError(filePath, error) {
  144. let errStr;
  145. switch (typeof error) {
  146. case "string":
  147. errStr = error;
  148. break;
  149. case "object":
  150. if (error != null && "message" in error) {
  151. const message = error.message;
  152. errStr = `${message}`;
  153. if (typeof message === "string") {
  154. try {
  155. const parsed = JSON.parse(message);
  156. if (typeof parsed === "object" && parsed.error) {
  157. errStr = `${parsed.error}`;
  158. }
  159. }
  160. catch (_) {
  161. }
  162. }
  163. break;
  164. }
  165. default:
  166. errStr = `Unknown error: ${JSON.stringify(error)}`;
  167. }
  168. const relativePath = path.relative(process.cwd(), filePath);
  169. return `${clc.cyan(relativePath)}:${errStr.trim()}`;
  170. }
  171. }
  172. exports.DatabaseEmulator = DatabaseEmulator;