backend-manager 2.4.24 → 2.5.0

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.24",
3
+ "version": "2.5.0",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -66,4 +66,4 @@
66
66
  "src/",
67
67
  "templates/"
68
68
  ]
69
- }
69
+ }
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,9 @@ 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
+ log(`Id: `, chalk.bold(`${self.projectName}`));
257
+ log(`Url:`, chalk.bold(`${self.projectUrl}`));
257
258
 
258
259
  if (!self.package || !self.package.engines || !self.package.engines.node) {
259
260
  throw new Error('Missing <engines.node> in package.json')
@@ -566,29 +567,13 @@ Main.prototype.setup = async function () {
566
567
  // const prepareStatsURL = `https://us-central1-${_.get(self.firebaseRC, 'projects.default')}.cloudfunctions.net/bm_api?authenticationToken=undefined`;
567
568
  const statsFetchResult = await fetch(prepareStatsURL, {
568
569
  method: 'post',
569
- body: JSON.stringify({
570
+ timeout: 10000,
571
+ response: 'json',
572
+ body: {
570
573
  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
- }
574
+ },
591
575
  })
576
+ .then(json => json)
592
577
  .catch(e => e);
593
578
 
594
579
  if (statsFetchResult instanceof Error) {
@@ -659,7 +644,7 @@ Main.prototype.test = async function(name, fn, fix, args) {
659
644
  status = chalk.red('failed');
660
645
  self.testTotal++;
661
646
  }
662
- log(chalk.black.bold(`[${self.testTotal}]`), chalk.black(`${name}:`), status);
647
+ log(chalk.bold(`[${self.testTotal}]`), `${name}:`, status);
663
648
  if (!passed) {
664
649
  log(chalk.yellow(`Fixing...`));
665
650
  fix(self, args)
@@ -1060,7 +1045,7 @@ function fix_firebaseHosting(self) {
1060
1045
 
1061
1046
  function getPkgVersion(package) {
1062
1047
  return new Promise(async function(resolve, reject) {
1063
- let npm = new NpmApi();
1048
+ let npm = new Npm();
1064
1049
  npm.repo(package)
1065
1050
  .package()
1066
1051
  .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;
@@ -48,6 +48,10 @@ Module.prototype.main = function() {
48
48
  self.payload.data = assistant.request.data;
49
49
  self.payload.user = await assistant.authenticate();
50
50
 
51
+ if (self.assistant.request.method === 'OPTIONS') {
52
+ return resolve();
53
+ }
54
+
51
55
  const resolved = self.resolveCommand(self.payload.data.command);
52
56
 
53
57
  self.assistant.log(`Executing: ${resolved.command}`, self.payload, JSON.stringify(self.payload), {environment: 'production'})
@@ -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;
@@ -408,11 +407,6 @@ Manager.prototype._process = function (mod) {
408
407
  return new Promise(async function(resolve, reject) {
409
408
  let error;
410
409
 
411
- console.log('---REQ', method, mod.assistant.request);
412
- if (method === 'OPTIONS') {
413
- return resolve();
414
- }
415
-
416
410
  function _reject(e, log) {
417
411
  if (log) {
418
412
  // self.assistant.error(e, {environment: 'production'});