backend-manager 3.2.113 → 3.2.115
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,31 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
// Libraries
|
|
2
2
|
const fetch = require('wonderful-fetch');
|
|
3
3
|
|
|
4
4
|
function Module() {
|
|
5
5
|
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
Module.prototype.
|
|
8
|
+
Module.prototype.main = function (assistant, context) {
|
|
9
9
|
const self = this;
|
|
10
|
-
self.Manager = Manager;
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
self.storage = Manager.storage({name: 'usage', temporary: true, clear: false, log: false});
|
|
16
|
-
|
|
17
|
-
self.context = data.context;
|
|
18
|
-
return self;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
Module.prototype.main = function() {
|
|
22
|
-
const self = this;
|
|
23
|
-
const Manager = self.Manager;
|
|
24
|
-
const libraries = self.libraries;
|
|
25
|
-
const assistant = self.assistant;
|
|
26
|
-
const context = self.context;
|
|
11
|
+
// Shortcuts
|
|
12
|
+
const Manager = assistant.Manager;
|
|
13
|
+
const libraries = Manager.libraries;
|
|
27
14
|
|
|
28
15
|
return new Promise(async function(resolve, reject) {
|
|
16
|
+
self.storage = Manager.storage({name: 'usage', temporary: true, clear: false, log: false});
|
|
17
|
+
|
|
29
18
|
assistant.log(`cron/daily/reset-usage(): Starting main...`);
|
|
30
19
|
|
|
31
20
|
// Clear local
|
|
@@ -40,6 +29,8 @@ Module.prototype.main = function() {
|
|
|
40
29
|
|
|
41
30
|
Module.prototype.clearLocal = function() {
|
|
42
31
|
const self = this;
|
|
32
|
+
|
|
33
|
+
// Shortcuts
|
|
43
34
|
const Manager = self.Manager;
|
|
44
35
|
const libraries = self.libraries;
|
|
45
36
|
const assistant = self.assistant;
|
|
@@ -127,7 +118,7 @@ Module.prototype.clearFirestore = function() {
|
|
|
127
118
|
data.usage[metric].total = data.usage[metric].total || 0;
|
|
128
119
|
data.usage[metric].last = data.usage[metric].last || {};
|
|
129
120
|
|
|
130
|
-
//
|
|
121
|
+
// Yeet if its 0
|
|
131
122
|
if (data.usage[metric].period <= 0) {
|
|
132
123
|
continue;
|
|
133
124
|
}
|
|
@@ -8,6 +8,7 @@ function Module() {
|
|
|
8
8
|
Module.prototype.init = function (Manager, data) {
|
|
9
9
|
const self = this;
|
|
10
10
|
|
|
11
|
+
// Shortcuts
|
|
11
12
|
self.Manager = Manager;
|
|
12
13
|
self.libraries = Manager.libraries;
|
|
13
14
|
self.assistant = Manager.Assistant();
|
|
@@ -19,42 +20,88 @@ Module.prototype.init = function (Manager, data) {
|
|
|
19
20
|
|
|
20
21
|
Module.prototype.main = function() {
|
|
21
22
|
const self = this;
|
|
23
|
+
|
|
24
|
+
// Shortcuts
|
|
22
25
|
const Manager = self.Manager;
|
|
23
26
|
const libraries = self.libraries;
|
|
24
27
|
const assistant = self.assistant;
|
|
25
28
|
const context = self.context;
|
|
26
29
|
|
|
27
30
|
return new Promise(async function(resolve, reject) {
|
|
28
|
-
|
|
31
|
+
// Set log prefix
|
|
32
|
+
assistant.setLogPrefix('cron/daily()');
|
|
33
|
+
|
|
34
|
+
// Log
|
|
35
|
+
assistant.log(`Starting...`);
|
|
36
|
+
|
|
37
|
+
// Setup error
|
|
38
|
+
let error;
|
|
39
|
+
|
|
40
|
+
// Load BEM jobs
|
|
41
|
+
await loadAndExecuteJobs(`${__dirname}/daily`, Manager, context).catch((e) => error = e);
|
|
42
|
+
|
|
43
|
+
// Load custom jobs
|
|
44
|
+
await loadAndExecuteJobs(`${process.cwd()}/hooks/cron/daily`, Manager, context).catch((e) => error = e);
|
|
45
|
+
|
|
46
|
+
// If there was an error, reject
|
|
47
|
+
if (error) {
|
|
48
|
+
return reject(error);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Return
|
|
52
|
+
return resolve();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
29
55
|
|
|
30
|
-
|
|
31
|
-
|
|
56
|
+
function loadAndExecuteJobs(jobsPath, Manager, context) {
|
|
57
|
+
const assistant = Manager.assistant;
|
|
58
|
+
|
|
59
|
+
return new Promise(async function(resolve, reject) {
|
|
60
|
+
const jobs = jetpack.list(jobsPath) || [];
|
|
32
61
|
let caught;
|
|
33
62
|
|
|
34
|
-
//
|
|
63
|
+
// Log
|
|
64
|
+
assistant.log(`Located ${jobs.length} jobs @ ${jobsPath}...`);
|
|
65
|
+
|
|
35
66
|
for (let i = 0; i < jobs.length; i++) {
|
|
67
|
+
// Create new assistant for each job
|
|
68
|
+
const assistant = Manager.Assistant();
|
|
69
|
+
|
|
70
|
+
// Load job
|
|
36
71
|
const job = jobs[i];
|
|
37
72
|
const jobName = job.replace('.js', '');
|
|
38
73
|
|
|
74
|
+
// Set log prefix
|
|
75
|
+
assistant.setLogPrefix(`cron/daily/${jobName}()`);
|
|
76
|
+
|
|
39
77
|
// Log
|
|
40
|
-
assistant.log(`
|
|
78
|
+
assistant.log(`Starting...`);
|
|
41
79
|
|
|
42
|
-
// Load
|
|
80
|
+
// Load job
|
|
43
81
|
const Job = require(`${jobsPath}/${job}`);
|
|
44
82
|
const jobInstance = new Job();
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
83
|
+
|
|
84
|
+
// Setup
|
|
85
|
+
jobInstance.Manager = Manager;
|
|
86
|
+
jobInstance.assistant = assistant;
|
|
87
|
+
jobInstance.context = context;
|
|
88
|
+
jobInstance.libraries = Manager.libraries;
|
|
89
|
+
|
|
90
|
+
// Execute job
|
|
91
|
+
await jobInstance.main(assistant, context)
|
|
92
|
+
.then(res => {
|
|
93
|
+
assistant.log(`Completed!`);
|
|
94
|
+
})
|
|
95
|
+
.catch(e => {
|
|
96
|
+
assistant.errorify(`Error executing: ${e}`, {
|
|
97
|
+
code: 500,
|
|
98
|
+
sentry: true
|
|
99
|
+
});
|
|
100
|
+
caught = e;
|
|
101
|
+
})
|
|
56
102
|
}
|
|
57
103
|
|
|
104
|
+
// If there was an error, reject
|
|
58
105
|
if (caught) {
|
|
59
106
|
return reject(caught);
|
|
60
107
|
}
|
|
@@ -81,6 +81,9 @@ BackendAssistant.prototype.init = function (ref, options) {
|
|
|
81
81
|
// Set tag
|
|
82
82
|
self.tag = `${self.meta.name}/${self.id}`;
|
|
83
83
|
|
|
84
|
+
// Set logger prefix
|
|
85
|
+
self.logPrefix = '';
|
|
86
|
+
|
|
84
87
|
// Set stuff about request
|
|
85
88
|
self.request = {};
|
|
86
89
|
self.request.referrer = (self.ref.req.headers || {}).referrer || (self.ref.req.headers || {}).referer || '';
|
|
@@ -136,8 +139,16 @@ BackendAssistant.prototype.init = function (ref, options) {
|
|
|
136
139
|
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA
|
|
137
140
|
*/
|
|
138
141
|
|
|
139
|
-
|
|
140
|
-
|
|
142
|
+
// Set request type
|
|
143
|
+
if (
|
|
144
|
+
self.ref.req.xhr || (self.ref.req?.headers?.accept || '').includes('json')
|
|
145
|
+
|| (self.ref.req?.headers?.['content-type'] || '').includes('json')
|
|
146
|
+
) {
|
|
147
|
+
self.request.type = 'ajax';
|
|
148
|
+
} else {
|
|
149
|
+
self.request.type = 'form';
|
|
150
|
+
}
|
|
151
|
+
self.request.path = self.ref.req.path || '';
|
|
141
152
|
self.request.user = self.resolveAccount({authenticated: false});
|
|
142
153
|
|
|
143
154
|
// Set body and query
|
|
@@ -255,8 +266,15 @@ BackendAssistant.prototype._log = function () {
|
|
|
255
266
|
// 1. Convert args to a normal array
|
|
256
267
|
const logs = [...Array.prototype.slice.call(arguments)];
|
|
257
268
|
|
|
269
|
+
// Add log prefix
|
|
270
|
+
const prefix = self.logPrefix
|
|
271
|
+
? ` ${self.logPrefix}:`
|
|
272
|
+
: ':';
|
|
273
|
+
|
|
258
274
|
// 2. Prepend log prefix log string
|
|
259
|
-
logs.unshift(
|
|
275
|
+
logs.unshift(
|
|
276
|
+
`[${self.tag} @ ${new Date().toISOString()}]${prefix}`
|
|
277
|
+
);
|
|
260
278
|
|
|
261
279
|
// 3. Pass along arguments to console.log
|
|
262
280
|
if (logs[1] === 'error') {
|
|
@@ -309,6 +327,30 @@ BackendAssistant.prototype._log = function () {
|
|
|
309
327
|
}
|
|
310
328
|
}
|
|
311
329
|
|
|
330
|
+
BackendAssistant.prototype.setLogPrefix = function (s) {
|
|
331
|
+
const self = this;
|
|
332
|
+
|
|
333
|
+
// Set logger prefix
|
|
334
|
+
self.logPrefix = s
|
|
335
|
+
|
|
336
|
+
return self;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
BackendAssistant.prototype.clearLogPrefix = function () {
|
|
340
|
+
const self = this;
|
|
341
|
+
|
|
342
|
+
// Set logger prefix
|
|
343
|
+
self.logPrefix = '';
|
|
344
|
+
|
|
345
|
+
return self;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
BackendAssistant.prototype.getLogPrefix = function () {
|
|
349
|
+
const self = this;
|
|
350
|
+
|
|
351
|
+
return self.logPrefix;
|
|
352
|
+
};
|
|
353
|
+
|
|
312
354
|
BackendAssistant.prototype.getUser = function () {
|
|
313
355
|
const self = this;
|
|
314
356
|
|
|
@@ -568,8 +610,8 @@ BackendAssistant.prototype.authenticate = async function (options) {
|
|
|
568
610
|
// Check with custom BEM Token
|
|
569
611
|
let storedApiKey;
|
|
570
612
|
try {
|
|
571
|
-
const workingConfig =
|
|
572
|
-
storedApiKey =
|
|
613
|
+
const workingConfig = self.Manager?.config || functions.config();
|
|
614
|
+
const storedApiKey = workingConfig?.backend_manager?.key || '';
|
|
573
615
|
} catch (e) {
|
|
574
616
|
|
|
575
617
|
}
|
|
@@ -841,13 +883,18 @@ BackendAssistant.prototype.parseMultipartFormData = function (options) {
|
|
|
841
883
|
return reject(new Error('Cannot run .parseMultipartForm() until .init() has been called'));
|
|
842
884
|
}
|
|
843
885
|
const existingData = self.request.multipartData;
|
|
844
|
-
|
|
845
|
-
|
|
886
|
+
const getFields = existingData?.fields || {};
|
|
887
|
+
const getFiles = existingData?.files || {};
|
|
888
|
+
|
|
889
|
+
// If there are already fields or files, return them
|
|
890
|
+
if (Object.keys(getFields).length + Object.keys(getFiles).length > 0) {
|
|
846
891
|
return resolve(existingData);
|
|
847
892
|
}
|
|
848
893
|
|
|
894
|
+
// Set options
|
|
849
895
|
options = options || {};
|
|
850
896
|
|
|
897
|
+
// Set headers
|
|
851
898
|
const fs = require('fs');
|
|
852
899
|
const req = self.ref.req;
|
|
853
900
|
const res = self.ref.res;
|
package/src/manager/index.js
CHANGED
|
@@ -50,6 +50,7 @@ Manager.prototype.init = function (exporter, options) {
|
|
|
50
50
|
options.setupFunctions = typeof options.setupFunctions === 'undefined' ? true : options.setupFunctions;
|
|
51
51
|
options.setupFunctionsLegacy = typeof options.setupFunctionsLegacy === 'undefined' ? false : options.setupFunctionsLegacy;
|
|
52
52
|
options.setupFunctionsIdentity = typeof options.setupFunctionsIdentity === 'undefined' ? true : options.setupFunctionsIdentity;
|
|
53
|
+
options.setupServer = typeof options.setupServer === 'undefined' ? true : options.setupServer;
|
|
53
54
|
options.initializeLocalStorage = typeof options.initializeLocalStorage === 'undefined' ? false : options.initializeLocalStorage;
|
|
54
55
|
options.resourceZone = typeof options.resourceZone === 'undefined' ? 'us-central1' : options.resourceZone;
|
|
55
56
|
options.sentry = typeof options.sentry === 'undefined' ? true : options.sentry;
|
|
@@ -67,7 +68,7 @@ Manager.prototype.init = function (exporter, options) {
|
|
|
67
68
|
// Load libraries
|
|
68
69
|
self.libraries = {
|
|
69
70
|
// Third-party
|
|
70
|
-
functions: require('firebase-functions'),
|
|
71
|
+
functions: options.projectType === 'firebase' ? require('firebase-functions') : null,
|
|
71
72
|
admin: require('firebase-admin'),
|
|
72
73
|
cors: require('cors')({ origin: true }),
|
|
73
74
|
sentry: null,
|
|
@@ -92,7 +93,7 @@ Manager.prototype.init = function (exporter, options) {
|
|
|
92
93
|
self.package = resolveProjectPackage();
|
|
93
94
|
self.config = merge(
|
|
94
95
|
requireJSON5(self.project.backendManagerConfigPath),
|
|
95
|
-
self.libraries.functions.config()
|
|
96
|
+
self.libraries.functions ? self.libraries.functions.config() : {}
|
|
96
97
|
);
|
|
97
98
|
|
|
98
99
|
// Saved config
|
|
@@ -224,7 +225,7 @@ Manager.prototype.init = function (exporter, options) {
|
|
|
224
225
|
}
|
|
225
226
|
|
|
226
227
|
// Setup custom server
|
|
227
|
-
if (options.projectType === 'custom') {
|
|
228
|
+
if (options.projectType === 'custom' && options.setupServer) {
|
|
228
229
|
self.setupCustomServer(exporter, options);
|
|
229
230
|
}
|
|
230
231
|
|
|
@@ -356,8 +357,12 @@ Manager.prototype._preProcess = function (mod) {
|
|
|
356
357
|
|
|
357
358
|
Manager.prototype.Assistant = function(ref, options) {
|
|
358
359
|
const self = this;
|
|
360
|
+
|
|
361
|
+
// Set options defaults
|
|
359
362
|
ref = ref || {};
|
|
360
363
|
options = options || {};
|
|
364
|
+
|
|
365
|
+
// Create assistant instance
|
|
361
366
|
return (new self.libraries.Assistant()).init({
|
|
362
367
|
req: ref.req,
|
|
363
368
|
res: ref.res,
|
|
@@ -365,26 +370,6 @@ Manager.prototype.Assistant = function(ref, options) {
|
|
|
365
370
|
functions: self.libraries.functions,
|
|
366
371
|
Manager: self,
|
|
367
372
|
}, options)
|
|
368
|
-
// return (new self.libraries.Assistant()).init({
|
|
369
|
-
// req: ref.req,
|
|
370
|
-
// res: ref.res,
|
|
371
|
-
// admin: self.libraries.admin,
|
|
372
|
-
// functions: self.libraries.functions,
|
|
373
|
-
// Manager: self,
|
|
374
|
-
// }, {
|
|
375
|
-
// accept: options.accept,
|
|
376
|
-
// functionName: options.functionName,
|
|
377
|
-
// functionType: options.functionType,
|
|
378
|
-
// })
|
|
379
|
-
// self._inner.ip = (!self._inner.ip || self._inner.ip === '127.0.0.1') ? ass.request.ip : self._inner.ip;
|
|
380
|
-
// self._inner.country = self._inner.country || ass.request.country;
|
|
381
|
-
// self._inner.referrer = self._inner.referrer || ass.request.referrer;
|
|
382
|
-
// self._inner.userAgent = (!self._inner.userAgent || self._inner.userAgent === 'empty') ? ass.request.userAgent : self._inner.userAgent;
|
|
383
|
-
// self._inner.name = self._inner.name || ass.meta.name;
|
|
384
|
-
// if (ref.req) {
|
|
385
|
-
// console.log('ref.req.headers', ref.req.headers);
|
|
386
|
-
// }
|
|
387
|
-
// console.log('self._inner', self._inner);
|
|
388
373
|
};
|
|
389
374
|
|
|
390
375
|
Manager.prototype.User = function () {
|
|
@@ -826,7 +811,7 @@ Manager.prototype.setupFunctions = function (exporter, options) {
|
|
|
826
811
|
// Setup cron jobs
|
|
827
812
|
exporter.bm_cronDaily =
|
|
828
813
|
self.libraries.functions
|
|
829
|
-
.runWith({ memory: '256MB', timeoutSeconds:
|
|
814
|
+
.runWith({ memory: '256MB', timeoutSeconds: 190 })
|
|
830
815
|
.pubsub.schedule('every 24 hours')
|
|
831
816
|
.onRun(async (context) => self._process((new (require(`${core}/cron/daily.js`))()).init(self, { context: context, })));
|
|
832
817
|
};
|