backend-manager 2.4.26 → 2.5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "2.4.26",
3
+ "version": "2.5.1",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
package/src/cli/cli.js CHANGED
@@ -11,13 +11,13 @@ const path = require('path');
11
11
  const chalk = require('chalk');
12
12
  const _ = require('lodash');
13
13
  const log = console.log;
14
- let NpmApi = require('npm-api');
14
+ const Npm = require('npm-api');
15
15
  const semver = require('semver');
16
- let inquirer = require('inquirer');
16
+ const inquirer = require('inquirer');
17
17
  const { spawn } = require('child_process');
18
- let argv = require('yargs').argv;
19
18
  const JSON5 = require('json5');
20
- const fetch = require('node-fetch');
19
+ const fetch = require('wonderful-fetch');
20
+ const argv = require('yargs').argv;
21
21
 
22
22
  // function parseArgumentsIntoOptions(rawArgs) {
23
23
  // const args = arg(
@@ -252,8 +252,14 @@ Main.prototype.setup = async function () {
252
252
  // tests
253
253
  self.projectName = self.firebaseRC.projects.default;
254
254
  self.projectUrl = `https://console.firebase.google.com/project/${self.projectName}`;
255
- log(chalk.black(`Id: `, chalk.bold(`${self.projectName}`)));
256
- log(chalk.black(`Url:`, chalk.bold(`${self.projectUrl}`)));
255
+
256
+ self.bemApiURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=${_.get(self.runtimeConfigJSON, 'backend_manager.key')}`;
257
+ // const prepareStatsURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=undefined`;
258
+
259
+
260
+
261
+ log(`Id: `, chalk.bold(`${self.projectName}`));
262
+ log(`Url:`, chalk.bold(`${self.projectUrl}`));
257
263
 
258
264
  if (!self.package || !self.package.engines || !self.package.engines.node) {
259
265
  throw new Error('Missing <engines.node> in package.json')
@@ -487,7 +493,7 @@ Main.prototype.setup = async function () {
487
493
  await self.test('set storage lifecycle policy', async function () {
488
494
  const result = await cmd_setStorageLifecycle(self).catch(e => e);
489
495
  return !(result instanceof Error);
490
- }, NOFIX);
496
+ }, fix_setStoragePolicy);
491
497
 
492
498
  // Update actual files
493
499
  await self.test('update firestore rules file', function () {
@@ -561,34 +567,15 @@ Main.prototype.setup = async function () {
561
567
  console.log('\n');
562
568
  }
563
569
 
564
-
565
- const prepareStatsURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=${_.get(self.runtimeConfigJSON, 'backend_manager.key')}`;
566
- // const prepareStatsURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=undefined`;
567
- const statsFetchResult = await fetch(prepareStatsURL, {
570
+ const statsFetchResult = await fetch(self.bemApiURL, {
568
571
  method: 'post',
569
- body: JSON.stringify({
572
+ timeout: 10000,
573
+ response: 'json',
574
+ body: {
570
575
  command: 'admin:get-stats',
571
- }),
572
- timeout: 3000,
573
- })
574
- .then(async (res) => {
575
- if (!res.ok) {
576
- return res.text()
577
- .then(data => {
578
- throw new Error(data || res.statusText || 'Unknown error.');
579
- })
580
- .catch(e => e)
581
- } else {
582
- return res.text()
583
- .then(data => {
584
- try {
585
- return JSON5.parse(data);
586
- } catch (e) {
587
- return e;
588
- }
589
- })
590
- }
576
+ },
591
577
  })
578
+ .then(json => json)
592
579
  .catch(e => e);
593
580
 
594
581
  if (statsFetchResult instanceof Error) {
@@ -659,7 +646,7 @@ Main.prototype.test = async function(name, fn, fix, args) {
659
646
  status = chalk.red('failed');
660
647
  self.testTotal++;
661
648
  }
662
- log(chalk.black.bold(`[${self.testTotal}]`), chalk.black(`${name}:`), status);
649
+ log(chalk.bold(`[${self.testTotal}]`), `${name}:`, status);
663
650
  if (!passed) {
664
651
  log(chalk.yellow(`Fixing...`));
665
652
  fix(self, args)
@@ -918,6 +905,27 @@ function fix_indexesSync(self) {
918
905
  });
919
906
  };
920
907
 
908
+ function fix_setStoragePolicy(self) {
909
+ return new Promise(function(resolve, reject) {
910
+ fetch(self.bemApiURL, {
911
+ method: 'post',
912
+ timeout: 10000,
913
+ response: 'json',
914
+ body: {
915
+ command: 'admin:backup',
916
+ },
917
+ })
918
+ .then(json => {
919
+ console.log('Response', json);
920
+ return resolve();
921
+ })
922
+ .catch(e => {
923
+ console.error(chalk.red(`There is no automatic fix. Please run: \n${chalk.bold('firebase deploy && npx bm setup')}`));
924
+ return reject();
925
+ });
926
+ });
927
+ };
928
+
921
929
  function fix_firestoreRulesFile(self) {
922
930
  return new Promise(function(resolve, reject) {
923
931
  const name = 'firestore.rules'
@@ -1060,7 +1068,7 @@ function fix_firebaseHosting(self) {
1060
1068
 
1061
1069
  function getPkgVersion(package) {
1062
1070
  return new Promise(async function(resolve, reject) {
1063
- let npm = new NpmApi();
1071
+ let npm = new Npm();
1064
1072
  npm.repo(package)
1065
1073
  .package()
1066
1074
  .then(function(pkg) {
@@ -0,0 +1,53 @@
1
+ const uuid4 = require('uuid').v4;
2
+ const UIDGenerator = require('uid-generator');
3
+ const _ = require('lodash')
4
+ const powertools = require('node-powertools')
5
+ const uidgen = new UIDGenerator(256);
6
+
7
+ function Module() {
8
+
9
+ }
10
+
11
+ Module.prototype.main = function () {
12
+ const self = this;
13
+ const Manager = self.Manager;
14
+ const Api = self.Api;
15
+ const assistant = self.assistant;
16
+ const payload = self.payload;
17
+
18
+ return new Promise(async function(resolve, reject) {
19
+ self.Api.resolveUser({adminRequired: true})
20
+ .then(async (user) => {
21
+ const keys = powertools.arrayify(_.get(payload.data.payload, 'keys') || ['clientId', 'privateKey']);
22
+ const newKeys = {};
23
+
24
+ keys
25
+ .forEach(key => {
26
+ if (key.match(/client/ig)) {
27
+ newKeys.clientId = uuid4();
28
+ } else if (key.match(/private/ig)) {
29
+ newKeys.privateKey = uidgen.generateSync();
30
+ }
31
+ });
32
+
33
+ self.libraries.admin.firestore().doc(`users/${user.auth.uid}`)
34
+ .set({
35
+ api: newKeys,
36
+ }, {merge: true})
37
+ .then(r => {
38
+ return resolve({data: newKeys});
39
+ })
40
+ .catch(e => {
41
+ return reject(assistant.errorManager(`Failed to generate keys: ${e}`, {code: 500, sentry: true, send: false, log: false}).error)
42
+ })
43
+
44
+ })
45
+ .catch(e => {
46
+ return reject(e);
47
+ })
48
+ });
49
+
50
+ };
51
+
52
+
53
+ module.exports = Module;
@@ -400,7 +400,6 @@ Manager.prototype.init = function (exporter, options) {
400
400
  Manager.prototype._process = function (mod) {
401
401
  const self = this;
402
402
  const name = mod.assistant.meta.name;
403
- const method = mod.assistant.request.method;
404
403
  const hook = self.handlers && self.handlers[name];
405
404
  const req = mod.req;
406
405
  const res = mod.res;