firebase-tools 11.2.2 → 11.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -104,7 +104,7 @@ class Proxied {
104
104
  return this.proxy;
105
105
  }
106
106
  }
107
- async function resolveDeveloperNodeModule(frb, name) {
107
+ async function resolveDeveloperNodeModule(name) {
108
108
  const pkg = requirePackageJson();
109
109
  if (!pkg) {
110
110
  new types_1.EmulatorLog("SYSTEM", "missing-package-json", "").log();
@@ -130,20 +130,20 @@ async function resolveDeveloperNodeModule(frb, name) {
130
130
  logDebug(`Resolved module ${name}`, moduleResolution);
131
131
  return moduleResolution;
132
132
  }
133
- async function assertResolveDeveloperNodeModule(frb, name) {
134
- const resolution = await resolveDeveloperNodeModule(frb, name);
133
+ async function assertResolveDeveloperNodeModule(name) {
134
+ const resolution = await resolveDeveloperNodeModule(name);
135
135
  if (!(resolution.installed && resolution.declared && resolution.resolution && resolution.version)) {
136
136
  throw new Error(`Assertion failure: could not fully resolve ${name}: ${JSON.stringify(resolution)}`);
137
137
  }
138
138
  return resolution;
139
139
  }
140
- async function verifyDeveloperNodeModules(frb) {
140
+ async function verifyDeveloperNodeModules() {
141
141
  const modBundles = [
142
142
  { name: "firebase-admin", isDev: false, minVersion: "8.9.0" },
143
143
  { name: "firebase-functions", isDev: false, minVersion: "3.13.1" },
144
144
  ];
145
145
  for (const modBundle of modBundles) {
146
- const resolution = await resolveDeveloperNodeModule(frb, modBundle.name);
146
+ const resolution = await resolveDeveloperNodeModule(modBundle.name);
147
147
  if (!resolution.declared) {
148
148
  new types_1.EmulatorLog("SYSTEM", "missing-module", "", modBundle).log();
149
149
  return false;
@@ -240,8 +240,8 @@ function initializeNetworkFiltering() {
240
240
  });
241
241
  logDebug("Outgoing network have been stubbed.", results);
242
242
  }
243
- async function initializeFirebaseFunctionsStubs(frb) {
244
- const firebaseFunctionsResolution = await assertResolveDeveloperNodeModule(frb, "firebase-functions");
243
+ async function initializeFirebaseFunctionsStubs() {
244
+ const firebaseFunctionsResolution = await assertResolveDeveloperNodeModule("firebase-functions");
245
245
  const firebaseFunctionsRoot = (0, functionsEmulatorShared_1.findModuleRoot)("firebase-functions", firebaseFunctionsResolution.resolution);
246
246
  const httpsProviderResolution = path.join(firebaseFunctionsRoot, "lib/providers/https");
247
247
  const httpsProviderV1Resolution = path.join(firebaseFunctionsRoot, "lib/v1/providers/https");
@@ -343,10 +343,10 @@ function initializeRuntimeConfig() {
343
343
  }
344
344
  }
345
345
  }
346
- async function initializeFirebaseAdminStubs(frb) {
347
- const adminResolution = await assertResolveDeveloperNodeModule(frb, "firebase-admin");
346
+ async function initializeFirebaseAdminStubs() {
347
+ const adminResolution = await assertResolveDeveloperNodeModule("firebase-admin");
348
348
  const localAdminModule = require(adminResolution.resolution);
349
- const functionsResolution = await assertResolveDeveloperNodeModule(frb, "firebase-functions");
349
+ const functionsResolution = await assertResolveDeveloperNodeModule("firebase-functions");
350
350
  const localFunctionsModule = require(functionsResolution.resolution);
351
351
  const defaultConfig = getDefaultConfig();
352
352
  const adminModuleProxy = new Proxied(localAdminModule);
@@ -360,7 +360,7 @@ async function initializeFirebaseAdminStubs(frb) {
360
360
  new types_1.EmulatorLog("SYSTEM", "default-admin-app-used", `config=${defaultAppOptions}`, {
361
361
  opts: defaultAppOptions,
362
362
  }).log();
363
- const defaultApp = makeProxiedFirebaseApp(frb, adminModuleTarget.initializeApp(defaultAppOptions));
363
+ const defaultApp = makeProxiedFirebaseApp(adminModuleTarget.initializeApp(defaultAppOptions));
364
364
  logDebug("initializeApp(DEFAULT)", defaultAppOptions);
365
365
  localFunctionsModule.app.setEmulatedAdminApp(defaultApp);
366
366
  if (process.env[constants_1.Constants.FIREBASE_AUTH_EMULATOR_HOST]) {
@@ -405,7 +405,7 @@ async function initializeFirebaseAdminStubs(frb) {
405
405
  adminResolution,
406
406
  });
407
407
  }
408
- function makeProxiedFirebaseApp(frb, original) {
408
+ function makeProxiedFirebaseApp(original) {
409
409
  const appProxy = new Proxied(original);
410
410
  return appProxy
411
411
  .when("firestore", (target) => {
@@ -450,8 +450,8 @@ function warnAboutStorageProd() {
450
450
  }
451
451
  new types_1.EmulatorLog("WARN_ONCE", "runtime-status", "The Firebase Storage emulator is not running, so calls to Firebase Storage will affect production.").log();
452
452
  }
453
- async function initializeFunctionsConfigHelper(frb) {
454
- const functionsResolution = await assertResolveDeveloperNodeModule(frb, "firebase-functions");
453
+ async function initializeFunctionsConfigHelper() {
454
+ const functionsResolution = await assertResolveDeveloperNodeModule("firebase-functions");
455
455
  const localFunctionsModule = require(functionsResolution.resolution);
456
456
  logDebug("Checked functions.config()", {
457
457
  config: localFunctionsModule.config(),
@@ -487,27 +487,7 @@ function rawBodySaver(req, res, buf) {
487
487
  }
488
488
  async function processHTTPS(trigger) {
489
489
  const ephemeralServer = express();
490
- const functionRouter = express.Router();
491
490
  await new Promise((resolveEphemeralServer, rejectEphemeralServer) => {
492
- const handler = async (req, res) => {
493
- try {
494
- logDebug(`Ephemeral server handling ${req.method} request`);
495
- res.on("finish", () => {
496
- instance.close((err) => {
497
- if (err) {
498
- rejectEphemeralServer(err);
499
- }
500
- else {
501
- resolveEphemeralServer();
502
- }
503
- });
504
- });
505
- await runHTTPS(trigger, [req, res]);
506
- }
507
- catch (err) {
508
- rejectEphemeralServer(err);
509
- }
510
- };
511
491
  ephemeralServer.enable("trust proxy");
512
492
  ephemeralServer.use(bodyParser.json({
513
493
  limit: "10mb",
@@ -527,14 +507,39 @@ async function processHTTPS(trigger) {
527
507
  limit: "10mb",
528
508
  verify: rawBodySaver,
529
509
  }));
530
- functionRouter.all("*", handler);
531
- ephemeralServer.use([`/`, `/*`], functionRouter);
532
- logDebug(`Attempting to listen to port: ${process.env.PORT}`);
533
- const instance = ephemeralServer.listen(process.env.PORT, () => {
534
- logDebug(`Listening to port: ${process.env.PORT}`);
535
- new types_1.EmulatorLog("SYSTEM", "runtime-status", "ready", { state: "ready" }).log();
510
+ let server;
511
+ function closeServer() {
512
+ if (server) {
513
+ server.close((err) => {
514
+ if (err) {
515
+ rejectEphemeralServer(err);
516
+ }
517
+ else {
518
+ resolveEphemeralServer();
519
+ }
520
+ });
521
+ }
522
+ }
523
+ ephemeralServer.get("/__/health", (req, res) => {
524
+ res.status(200).send();
536
525
  });
537
- instance.on("error", rejectEphemeralServer);
526
+ ephemeralServer.all("/favicon.ico|/robots.txt", (req, res) => {
527
+ res.on("finish", closeServer);
528
+ res.status(404).send();
529
+ });
530
+ ephemeralServer.all(`/*`, async (req, res) => {
531
+ try {
532
+ logDebug(`Ephemeral server handling ${req.method} request`);
533
+ res.on("finish", closeServer);
534
+ await runHTTPS(trigger, [req, res]);
535
+ }
536
+ catch (err) {
537
+ rejectEphemeralServer(err);
538
+ }
539
+ });
540
+ server = ephemeralServer.listen(process.env.PORT);
541
+ logDebug(`Listening to port: ${process.env.PORT}`);
542
+ server.on("error", rejectEphemeralServer);
538
543
  });
539
544
  }
540
545
  async function processBackground(trigger, frb, signature) {
@@ -587,7 +592,7 @@ async function runHTTPS(trigger, args) {
587
592
  return trigger(args[0], args[1]);
588
593
  });
589
594
  }
590
- async function moduleResolutionDetective(frb, error) {
595
+ async function moduleResolutionDetective(error) {
591
596
  const clues = {
592
597
  tsconfigJSON: await requireAsync("./tsconfig.json", { paths: [process.cwd()] }).catch(noOp),
593
598
  packageJSON: await requireAsync("./package.json", { paths: [process.cwd()] }).catch(noOp),
@@ -645,7 +650,7 @@ async function invokeTrigger(trigger, frb) {
645
650
  clearInterval(timerId);
646
651
  new types_1.EmulatorLog("INFO", "runtime-status", `Finished "${FUNCTION_TARGET_NAME}" in ~${Math.max(seconds, 1)}s`).log();
647
652
  }
648
- async function initializeRuntime(frb) {
653
+ async function initializeRuntime() {
649
654
  FUNCTION_DEBUG_MODE = process.env.FUNCTION_DEBUG_MODE || "";
650
655
  if (!FUNCTION_DEBUG_MODE) {
651
656
  FUNCTION_TARGET_NAME = process.env.FUNCTION_TARGET || "";
@@ -659,19 +664,18 @@ async function initializeRuntime(frb) {
659
664
  await flushAndExit(1);
660
665
  }
661
666
  }
662
- logDebug(`Disabled runtime features: ${JSON.stringify(frb.disabled_features)}`);
663
- const verified = await verifyDeveloperNodeModules(frb);
667
+ const verified = await verifyDeveloperNodeModules();
664
668
  if (!verified) {
665
669
  new types_1.EmulatorLog("INFO", "runtime-status", `Your functions could not be parsed due to an issue with your node_modules (see above)`).log();
666
670
  return;
667
671
  }
668
672
  initializeRuntimeConfig();
669
673
  initializeNetworkFiltering();
670
- await initializeFunctionsConfigHelper(frb);
671
- await initializeFirebaseFunctionsStubs(frb);
672
- await initializeFirebaseAdminStubs(frb);
674
+ await initializeFunctionsConfigHelper();
675
+ await initializeFirebaseFunctionsStubs();
676
+ await initializeFirebaseAdminStubs();
673
677
  }
674
- async function loadTriggers(frb, serializedFunctionTrigger) {
678
+ async function loadTriggers(serializedFunctionTrigger) {
675
679
  let triggerModule;
676
680
  if (serializedFunctionTrigger) {
677
681
  triggerModule = eval(serializedFunctionTrigger)();
@@ -682,7 +686,7 @@ async function loadTriggers(frb, serializedFunctionTrigger) {
682
686
  }
683
687
  catch (err) {
684
688
  if (err.code !== "ERR_REQUIRE_ESM") {
685
- await moduleResolutionDetective(frb, err);
689
+ await moduleResolutionDetective(err);
686
690
  throw err;
687
691
  }
688
692
  const modulePath = require.resolve(process.cwd());
@@ -712,9 +716,8 @@ async function handleMessage(message) {
712
716
  }
713
717
  if (!functionModule) {
714
718
  try {
715
- await initializeRuntime(runtimeArgs.frb);
716
719
  const serializedTriggers = runtimeArgs.opts ? runtimeArgs.opts.serializedTriggers : undefined;
717
- functionModule = await loadTriggers(runtimeArgs.frb, serializedTriggers);
720
+ functionModule = await loadTriggers(serializedTriggers);
718
721
  }
719
722
  catch (e) {
720
723
  logDebug(e);
@@ -748,7 +751,7 @@ async function handleMessage(message) {
748
751
  await flushAndExit(1);
749
752
  }
750
753
  }
751
- function main() {
754
+ async function main() {
752
755
  let lastSignal = new Date().getTime();
753
756
  let signalCount = 0;
754
757
  process.on("SIGINT", () => {
@@ -762,10 +765,7 @@ function main() {
762
765
  process.exit(1);
763
766
  }
764
767
  });
765
- logDebug("Functions runtime initialized.", {
766
- cwd: process.cwd(),
767
- node_version: process.versions.node,
768
- });
768
+ await initializeRuntime();
769
769
  let messageHandlePromise = Promise.resolve();
770
770
  process.on("message", (message) => {
771
771
  messageHandlePromise = messageHandlePromise
@@ -780,5 +780,15 @@ function main() {
780
780
  });
781
781
  }
782
782
  if (require.main === module) {
783
- main();
783
+ main()
784
+ .then(() => {
785
+ logDebug("Functions runtime initialized.", {
786
+ cwd: process.cwd(),
787
+ node_version: process.versions.node,
788
+ });
789
+ })
790
+ .catch((err) => {
791
+ new types_1.EmulatorLog("FATAL", "runtime-error", err.message || err, err).log();
792
+ return flushAndExit(1);
793
+ });
784
794
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RuntimeWorkerPool = exports.RuntimeWorker = exports.RuntimeWorkerState = void 0;
4
+ const http = require("http");
4
5
  const uuid = require("uuid");
5
6
  const types_1 = require("./types");
6
7
  const events_1 = require("events");
@@ -49,17 +50,11 @@ class RuntimeWorker {
49
50
  return this._state;
50
51
  }
51
52
  set state(state) {
52
- if (state === RuntimeWorkerState.BUSY) {
53
- this.socketReady = types_1.EmulatorLog.waitForLog(this.runtime.events, "SYSTEM", "runtime-status", (el) => {
54
- return el.data.state === "ready";
55
- });
56
- }
57
53
  if (state === RuntimeWorkerState.IDLE) {
58
54
  for (const l of this.logListeners) {
59
55
  this.runtime.events.removeListener("log", l);
60
56
  }
61
57
  this.logListeners = [];
62
- this.socketReady = undefined;
63
58
  }
64
59
  if (state === RuntimeWorkerState.FINISHED) {
65
60
  this.runtime.events.removeAllListeners();
@@ -88,9 +83,40 @@ class RuntimeWorker {
88
83
  this.stateEvents.once(RuntimeWorkerState.FINISHED, listener);
89
84
  });
90
85
  }
91
- waitForSocketReady() {
92
- return (this.socketReady ||
93
- Promise.reject(new Error("Cannot call waitForSocketReady() if runtime is not BUSY")));
86
+ isSocketReady() {
87
+ return new Promise((resolve, reject) => {
88
+ const req = http
89
+ .request({
90
+ method: "GET",
91
+ path: "/__/health",
92
+ socketPath: this.runtime.socketPath,
93
+ }, () => resolve())
94
+ .end();
95
+ req.on("error", (error) => {
96
+ reject(error);
97
+ });
98
+ });
99
+ }
100
+ async waitForSocketReady() {
101
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
102
+ const timeout = new Promise((resolve, reject) => {
103
+ setTimeout(() => {
104
+ reject(new error_1.FirebaseError("Failed to load function."));
105
+ }, 7000);
106
+ });
107
+ while (true) {
108
+ try {
109
+ await Promise.race([this.isSocketReady(), timeout]);
110
+ break;
111
+ }
112
+ catch (err) {
113
+ if (["ECONNREFUSED", "ENOENT"].includes(err === null || err === void 0 ? void 0 : err.code)) {
114
+ await sleep(100);
115
+ continue;
116
+ }
117
+ throw err;
118
+ }
119
+ }
94
120
  }
95
121
  log(msg) {
96
122
  emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.FUNCTIONS).log("DEBUG", `[worker-${this.key}-${this.id}]: ${msg}`);
@@ -324,7 +324,7 @@ function sendFileBytes(md, data, req, res) {
324
324
  res.setHeader("Accept-Ranges", "bytes");
325
325
  res.setHeader("Content-Type", md.contentType);
326
326
  res.setHeader("Content-Disposition", md.contentDisposition);
327
- res.setHeader("Content-Encoding", md.contentEncoding);
327
+ res.setHeader("Content-Encoding", isGZipped ? "identity" : md.contentEncoding);
328
328
  res.setHeader("ETag", md.etag);
329
329
  res.setHeader("Cache-Control", md.cacheControl);
330
330
  res.setHeader("x-goog-generation", `${md.generation}`);
@@ -18,9 +18,8 @@ const adminSdkConfig_1 = require("../adminSdkConfig");
18
18
  const types_1 = require("./rules/types");
19
19
  const upload_1 = require("./upload");
20
20
  class StoredFile {
21
- constructor(metadata, path) {
21
+ constructor(metadata) {
22
22
  this.metadata = metadata;
23
- this._path = path;
24
23
  }
25
24
  get metadata() {
26
25
  return this._metadata;
@@ -28,12 +27,6 @@ class StoredFile {
28
27
  set metadata(value) {
29
28
  this._metadata = value;
30
29
  }
31
- get path() {
32
- return this._path;
33
- }
34
- set path(value) {
35
- this._path = value;
36
- }
37
30
  }
38
31
  exports.StoredFile = StoredFile;
39
32
  class StorageLayer {
@@ -163,7 +156,7 @@ class StorageLayer {
163
156
  }
164
157
  this._persistence.deleteFile(filePath, true);
165
158
  this._persistence.renameFile(upload.path, filePath);
166
- this._files.set(filePath, new StoredFile(metadata, this._persistence.getDiskPath(filePath)));
159
+ this._files.set(filePath, new StoredFile(metadata));
167
160
  this._cloudFunctions.dispatch("finalize", new metadata_1.CloudStorageObjectMetadata(metadata));
168
161
  return metadata;
169
162
  }
@@ -202,7 +195,7 @@ class StorageLayer {
202
195
  cacheControl: newMetadata.cacheControl,
203
196
  customMetadata: newMetadata.metadata,
204
197
  }, this._cloudFunctions, sourceBytes, incomingMetadata);
205
- const file = new StoredFile(copiedFileMetadata, this._persistence.getDiskPath(destinationFilePath));
198
+ const file = new StoredFile(copiedFileMetadata);
206
199
  this._files.set(destinationFilePath, file);
207
200
  this._cloudFunctions.dispatch("finalize", new metadata_1.CloudStorageObjectMetadata(file.metadata));
208
201
  return file.metadata;
@@ -306,13 +299,14 @@ class StorageLayer {
306
299
  await fse.writeFile(bucketsFilePath, JSON.stringify(bucketsList, undefined, 2));
307
300
  const blobsDirPath = path.join(storageExportPath, "blobs");
308
301
  await fse.ensureDir(blobsDirPath);
309
- await fse.copy(this.dirPath, blobsDirPath, { recursive: true });
310
302
  const metadataDirPath = path.join(storageExportPath, "metadata");
311
303
  await fse.ensureDir(metadataDirPath);
312
304
  try {
313
305
  for (var _b = __asyncValues(this._files.entries()), _c; _c = await _b.next(), !_c.done;) {
314
- const [p, file] = _c.value;
315
- const metadataExportPath = path.join(metadataDirPath, encodeURIComponent(p)) + ".json";
306
+ const [, file] = _c.value;
307
+ const diskFileName = this._persistence.getDiskFileName(this.path(file.metadata.bucket, file.metadata.name));
308
+ await fse.copy(path.join(this.dirPath, diskFileName), path.join(blobsDirPath, diskFileName));
309
+ const metadataExportPath = path.join(metadataDirPath, encodeURIComponent(diskFileName)) + ".json";
316
310
  await fse.writeFile(metadataExportPath, metadata_1.StoredFileMetadata.toJSON(file.metadata));
317
311
  }
318
312
  }
@@ -348,15 +342,14 @@ class StorageLayer {
348
342
  logger_1.logger.warn(`Could not find file "${blobPath}" in storage export.`);
349
343
  continue;
350
344
  }
351
- let decodedBlobPath = decodeURIComponent(blobPath);
352
- const decodedBlobPathSep = getPathSep(decodedBlobPath);
353
- if (decodedBlobPathSep !== path.sep) {
354
- decodedBlobPath = decodedBlobPath.split(decodedBlobPathSep).join(path.sep);
345
+ let fileName = metadata.name;
346
+ const objectNameSep = getPathSep(fileName);
347
+ if (fileName !== path.sep) {
348
+ fileName = fileName.split(objectNameSep).join(path.sep);
355
349
  }
356
- const blobDiskPath = this._persistence.getDiskPath(decodedBlobPath);
357
- const file = new StoredFile(metadata, blobDiskPath);
358
- this._files.set(decodedBlobPath, file);
359
- fse.copyFileSync(blobAbsPath, blobDiskPath);
350
+ const filepath = this.path(metadata.bucket, fileName);
351
+ this._persistence.copyFromExternalPath(blobAbsPath, filepath);
352
+ this._files.set(filepath, new StoredFile(metadata));
360
353
  }
361
354
  }
362
355
  *walkDirSync(dir) {
@@ -374,6 +367,6 @@ class StorageLayer {
374
367
  }
375
368
  exports.StorageLayer = StorageLayer;
376
369
  function getPathSep(decodedPath) {
377
- const firstSepIndex = decodedPath.search(/[^a-z0-9-_.]/g);
370
+ const firstSepIndex = decodedPath.search(/[\/|\\\\]/g);
378
371
  return decodedPath[firstSepIndex];
379
372
  }
@@ -4,9 +4,12 @@ exports.Persistence = void 0;
4
4
  const fs_1 = require("fs");
5
5
  const rimraf = require("rimraf");
6
6
  const fs = require("fs");
7
+ const fse = require("fs-extra");
7
8
  const path = require("path");
9
+ const uuid = require("uuid");
8
10
  class Persistence {
9
11
  constructor(dirPath) {
12
+ this._diskPathMap = new Map();
10
13
  this.reset(dirPath);
11
14
  }
12
15
  reset(dirPath) {
@@ -14,22 +17,18 @@ class Persistence {
14
17
  (0, fs_1.mkdirSync)(dirPath, {
15
18
  recursive: true,
16
19
  });
20
+ this._diskPathMap = new Map();
17
21
  }
18
22
  get dirPath() {
19
23
  return this._dirPath;
20
24
  }
21
25
  appendBytes(fileName, bytes) {
22
- const filepath = this.getDiskPath(fileName);
23
- let fd;
24
- try {
25
- fs.appendFileSync(filepath, bytes);
26
- return filepath;
27
- }
28
- finally {
29
- if (fd) {
30
- (0, fs_1.closeSync)(fd);
31
- }
26
+ if (!this._diskPathMap.has(fileName)) {
27
+ this._diskPathMap.set(fileName, this.generateNewDiskName());
32
28
  }
29
+ const filepath = this.getDiskPath(fileName);
30
+ fs.appendFileSync(filepath, bytes);
31
+ return filepath;
33
32
  }
34
33
  readBytes(fileName, size, fileOffset) {
35
34
  let fd;
@@ -55,6 +54,7 @@ class Persistence {
55
54
  throw err;
56
55
  }
57
56
  }
57
+ this._diskPathMap.delete(fileName);
58
58
  }
59
59
  deleteAll() {
60
60
  return new Promise((resolve, reject) => {
@@ -63,16 +63,30 @@ class Persistence {
63
63
  reject(err);
64
64
  }
65
65
  else {
66
+ this._diskPathMap = new Map();
66
67
  resolve();
67
68
  }
68
69
  });
69
70
  });
70
71
  }
71
72
  renameFile(oldName, newName) {
72
- (0, fs_1.renameSync)(this.getDiskPath(oldName), this.getDiskPath(newName));
73
+ const oldNameId = this.getDiskFileName(oldName);
74
+ this._diskPathMap.set(newName, oldNameId);
75
+ this._diskPathMap.delete(oldName);
73
76
  }
74
77
  getDiskPath(fileName) {
75
- return path.join(this._dirPath, encodeURIComponent(fileName));
78
+ const shortenedDiskPath = this.getDiskFileName(fileName);
79
+ return path.join(this._dirPath, encodeURIComponent(shortenedDiskPath));
80
+ }
81
+ getDiskFileName(fileName) {
82
+ return this._diskPathMap.get(fileName);
83
+ }
84
+ copyFromExternalPath(sourcePath, newName) {
85
+ this._diskPathMap.set(newName, this.generateNewDiskName());
86
+ fse.copyFileSync(sourcePath, this.getDiskPath(newName));
87
+ }
88
+ generateNewDiskName() {
89
+ return uuid.v4();
76
90
  }
77
91
  }
78
92
  exports.Persistence = Persistence;
@@ -51,6 +51,17 @@ const ESCAPE_SEQUENCES_TO_CHARACTERS = {
51
51
  "\\'": "'",
52
52
  '\\"': '"',
53
53
  };
54
+ const ALL_ESCAPE_SEQUENCES_RE = /\\[nrtv\\'"]/g;
55
+ const CHARACTERS_TO_ESCAPE_SEQUENCES = {
56
+ "\n": "\\n",
57
+ "\r": "\\r",
58
+ "\t": "\\t",
59
+ "\v": "\\v",
60
+ "\\": "\\\\",
61
+ "'": "\\'",
62
+ '"': '\\"',
63
+ };
64
+ const ALL_ESCAPABLE_CHARACTERS_RE = /[\n\r\t\v\\'"]/g;
54
65
  function parse(data) {
55
66
  const envs = {};
56
67
  const errors = [];
@@ -63,7 +74,7 @@ function parse(data) {
63
74
  if ((quotesMatch = /^(["'])(.*)\1$/ms.exec(v)) != null) {
64
75
  v = quotesMatch[2];
65
76
  if (quotesMatch[1] === '"') {
66
- v = v.replace(/\\[nrtv\\'"]/g, (match) => ESCAPE_SEQUENCES_TO_CHARACTERS[match]);
77
+ v = v.replace(ALL_ESCAPE_SEQUENCES_RE, (match) => ESCAPE_SEQUENCES_TO_CHARACTERS[match]);
67
78
  }
68
79
  }
69
80
  envs[k] = v;
@@ -146,9 +157,43 @@ function hasUserEnvs({ functionsSource, projectId, projectAlias, isEmulator, })
146
157
  }
147
158
  exports.hasUserEnvs = hasUserEnvs;
148
159
  function writeUserEnvs(toWrite, envOpts) {
149
- throw new error_1.FirebaseError("Persisting user-defined parameters to .env files is not yet implemented.");
160
+ if (Object.keys(toWrite).length === 0) {
161
+ return;
162
+ }
163
+ const { functionsSource, projectId, projectAlias, isEmulator } = envOpts;
164
+ let envFiles = findEnvfiles(functionsSource, projectId, projectAlias, isEmulator);
165
+ if (envFiles.length === 0) {
166
+ envFiles = [createEnvFile(envOpts)];
167
+ }
168
+ const currentEnvs = loadUserEnvs(envOpts);
169
+ for (const k of Object.keys(toWrite)) {
170
+ validateKey(k);
171
+ if (currentEnvs.hasOwnProperty(k)) {
172
+ throw new error_1.FirebaseError(`Attempted to write param-defined key ${k} to .env files, but it was already defined.`);
173
+ }
174
+ }
175
+ const mostSpecificEnv = path.join(functionsSource, envFiles[envFiles.length - 1]);
176
+ (0, utils_1.logBullet)(clc.cyan.bold("functions: ") + `Writing new parameter values to disk: ${mostSpecificEnv}`);
177
+ for (const k of Object.keys(toWrite)) {
178
+ fs.appendFileSync(mostSpecificEnv, formatUserEnvForWrite(k, toWrite[k]));
179
+ }
150
180
  }
151
181
  exports.writeUserEnvs = writeUserEnvs;
182
+ function createEnvFile(envOpts) {
183
+ const fileToWrite = envOpts.isEmulator
184
+ ? FUNCTIONS_EMULATOR_DOTENV
185
+ : `.env.${envOpts.projectAlias || envOpts.projectId}`;
186
+ logger_1.logger.debug(`Creating ${fileToWrite}...`);
187
+ fs.writeFileSync(path.join(envOpts.functionsSource, fileToWrite), "", { flag: "wx" });
188
+ return fileToWrite;
189
+ }
190
+ function formatUserEnvForWrite(key, value) {
191
+ const escapedValue = value.replace(ALL_ESCAPABLE_CHARACTERS_RE, (match) => CHARACTERS_TO_ESCAPE_SEQUENCES[match]);
192
+ if (escapedValue !== value) {
193
+ return `${key}="${escapedValue}"\n`;
194
+ }
195
+ return `${key}=${escapedValue}\n`;
196
+ }
152
197
  function loadUserEnvs({ functionsSource, projectId, projectAlias, isEmulator, }) {
153
198
  var _a;
154
199
  const envFiles = findEnvfiles(functionsSource, projectId, projectAlias, isEmulator);
package/lib/gcp/iam.js CHANGED
@@ -2,11 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.testIamPermissions = exports.testResourceIamPermissions = exports.getRole = exports.deleteServiceAccount = exports.createServiceAccountKey = exports.getServiceAccount = exports.createServiceAccount = void 0;
4
4
  const api_1 = require("../api");
5
- const lodash_1 = require("lodash");
6
5
  const logger_1 = require("../logger");
7
6
  const apiv2_1 = require("../apiv2");
8
- const API_VERSION = "v1";
9
- const apiClient = new apiv2_1.Client({ urlPrefix: api_1.iamOrigin, apiVersion: API_VERSION });
7
+ const apiClient = new apiv2_1.Client({ urlPrefix: api_1.iamOrigin, apiVersion: "v1" });
10
8
  async function createServiceAccount(projectId, accountId, description, displayName) {
11
9
  const response = await apiClient.post(`/projects/${projectId}/serviceAccounts`, {
12
10
  accountId,
@@ -31,8 +29,8 @@ async function createServiceAccountKey(projectId, serviceAccountName) {
31
29
  return response.body;
32
30
  }
33
31
  exports.createServiceAccountKey = createServiceAccountKey;
34
- function deleteServiceAccount(projectId, accountEmail) {
35
- return apiClient.delete(`/projects/${projectId}/serviceAccounts/${accountEmail}`, {
32
+ async function deleteServiceAccount(projectId, accountEmail) {
33
+ await apiClient.delete(`/projects/${projectId}/serviceAccounts/${accountEmail}`, {
36
34
  resolveOnHTTPError: true,
37
35
  });
38
36
  }
@@ -44,25 +42,30 @@ async function getRole(role) {
44
42
  return response.body;
45
43
  }
46
44
  exports.getRole = getRole;
47
- async function testResourceIamPermissions(origin, apiVersion, resourceName, permissions) {
45
+ async function testResourceIamPermissions(origin, apiVersion, resourceName, permissions, quotaUser = "") {
48
46
  const localClient = new apiv2_1.Client({ urlPrefix: origin, apiVersion });
49
47
  if (process.env.FIREBASE_SKIP_INFORMATIONAL_IAM) {
50
- logger_1.logger.debug("[iam] skipping informational check of permissions", JSON.stringify(permissions), "on resource", resourceName);
51
- return { allowed: permissions, missing: [], passed: true };
48
+ logger_1.logger.debug(`[iam] skipping informational check of permissions ${JSON.stringify(permissions)} on resource ${resourceName}`);
49
+ return { allowed: Array.from(permissions).sort(), missing: [], passed: true };
50
+ }
51
+ const headers = {};
52
+ if (quotaUser) {
53
+ headers["x-goog-quota-user"] = quotaUser;
54
+ }
55
+ const response = await localClient.post(`/${resourceName}:testIamPermissions`, { permissions }, { headers });
56
+ const allowed = new Set(response.body.permissions || []);
57
+ const missing = new Set(permissions);
58
+ for (const p of allowed) {
59
+ missing.delete(p);
52
60
  }
53
- const response = await localClient.post(`/${resourceName}:testIamPermissions`, {
54
- permissions,
55
- });
56
- const allowed = (response.body.permissions || []).sort();
57
- const missing = (0, lodash_1.difference)(permissions, allowed);
58
61
  return {
59
- allowed,
60
- missing,
61
- passed: missing.length === 0,
62
+ allowed: Array.from(allowed).sort(),
63
+ missing: Array.from(missing).sort(),
64
+ passed: missing.size === 0,
62
65
  };
63
66
  }
64
67
  exports.testResourceIamPermissions = testResourceIamPermissions;
65
68
  async function testIamPermissions(projectId, permissions) {
66
- return testResourceIamPermissions(api_1.resourceManagerOrigin, "v1", `projects/${projectId}`, permissions);
69
+ return testResourceIamPermissions(api_1.resourceManagerOrigin, "v1", `projects/${projectId}`, permissions, `projects/${projectId}`);
67
70
  }
68
71
  exports.testIamPermissions = testIamPermissions;
@@ -48,7 +48,7 @@ function listVariables(configPath) {
48
48
  retryCodes: [500, 503],
49
49
  })
50
50
  .then((resp) => {
51
- return Promise.resolve(resp.body.variables);
51
+ return Promise.resolve(resp.body.variables || []);
52
52
  });
53
53
  }
54
54
  function getVariable(varPath) {
package/lib/index.js CHANGED
@@ -9,7 +9,7 @@ program.version(pkg.version);
9
9
  program.option("-P, --project <alias_or_project_id>", "the Firebase project to use for this command");
10
10
  program.option("--account <email>", "the Google account to use for authorization");
11
11
  program.option("-j, --json", "output JSON instead of text, also triggers non-interactive mode");
12
- program.option("--token <token>", "supply an auth token for this command");
12
+ program.option("--token <token>", "DEPRECATED - will be removed in a future major version - supply an auth token for this command");
13
13
  program.option("--non-interactive", "error out of the command instead of waiting for prompts");
14
14
  program.option("-i, --interactive", "force prompts to be displayed");
15
15
  program.option("--debug", "print verbose debug output and keep a debug log file");