@@ -8,39 +8,41 @@ import { format } from 'url'
8
8
import {
9
9
backupFirestoreMiddleware ,
10
10
checkFirestoreBackupStatusMiddleware ,
11
- getCollectionsMiddleware
11
+ getCollectionsMiddleware ,
12
12
} from './firestore'
13
13
import {
14
- listFilesMiddleware ,
14
+ defaultControllerDomain ,
15
+ defaultMemory ,
16
+ defaultRegion ,
17
+ defaultTimeout ,
18
+ } from './options'
19
+ import {
15
20
createStorageMiddleware ,
21
+ listFilesMiddleware ,
16
22
storageListMiddleware ,
17
- updateStorageMiddleware
23
+ updateStorageMiddleware ,
18
24
} from './storage'
19
25
import {
20
26
AgentOptions ,
21
27
BackupFireEnvConfig ,
22
28
BackupFireHTTPSHandler ,
23
29
BackupFireOptions ,
24
- RuntimeEnvironment
30
+ RuntimeEnvironment ,
25
31
} from './types'
26
32
import { backupUsersMiddleware } from './users'
27
33
import version from './version'
28
34
import {
29
35
configureExceptionsScope ,
30
36
createCrashedApp ,
31
37
exceptionHandlerMiddleware ,
32
- initExceptionsTracker
38
+ initExceptionsTracker ,
33
39
} from './_lib/exceptions'
34
40
35
- export const defaultControllerDomain = 'backupfire.dev'
36
-
37
- export const defaultRegion = 'us-central1'
38
-
39
41
export enum BackupFireConfig {
40
42
Token = 'BACKUPFIRE_TOKEN' ,
41
43
Password = 'BACKUPFIRE_PASSWORD' ,
42
44
Domain = 'BACKUPFIRE_DOMAIN' ,
43
- Allowlist = 'BACKUPFIRE_ALLOWLIST'
45
+ Allowlist = 'BACKUPFIRE_ALLOWLIST' ,
44
46
}
45
47
46
48
// Fallback for CommonJS
@@ -60,7 +62,7 @@ export default function backupFire(agentOptions?: AgentOptions) {
60
62
return dummyHandler ( {
61
63
region : agentOptions ?. region ,
62
64
memory : agentOptions ?. memory ,
63
- timeout : agentOptions ?. timeout
65
+ timeout : agentOptions ?. timeout ,
64
66
} )
65
67
66
68
// Derive Backup Fire options from environment configuration
@@ -80,7 +82,7 @@ export default function backupFire(agentOptions?: AgentOptions) {
80
82
controllerToken : envConfig . token ,
81
83
adminPassword : envConfig . password ,
82
84
bucketsAllowlist : envConfig . allowlist ?. split ( ',' ) ,
83
- debug : envConfig . debug === 'true'
85
+ debug : envConfig . debug === 'true' ,
84
86
} ,
85
87
agentOptions
86
88
)
@@ -111,7 +113,7 @@ export default function backupFire(agentOptions?: AgentOptions) {
111
113
}
112
114
113
115
// Set additional context
114
- configureExceptionsScope ( scope => {
116
+ configureExceptionsScope ( ( scope ) => {
115
117
scope . setUser ( { id : envConfig . token } )
116
118
scope . setTag ( 'project_id' , runtimeEnv . projectId )
117
119
scope . setTag ( 'node_version' , process . version )
@@ -131,12 +133,12 @@ export default function backupFire(agentOptions?: AgentOptions) {
131
133
return httpsHandler ( {
132
134
handler : createApp ( runtimeEnv , options ) ,
133
135
agentOptions,
134
- runtimeEnv
136
+ runtimeEnv,
135
137
} )
136
138
} catch ( err ) {
137
139
return httpsHandler ( {
138
140
handler : createCrashedApp ( err ) ,
139
- agentOptions
141
+ agentOptions,
140
142
} )
141
143
}
142
144
}
@@ -173,7 +175,7 @@ export function createApp(
173
175
'/firestore' ,
174
176
backupFirestoreMiddleware ( {
175
177
projectId : runtimeEnv . projectId ,
176
- ...globalOptions
178
+ ...globalOptions ,
177
179
} )
178
180
)
179
181
// Check Firestore backup status
@@ -185,7 +187,13 @@ export function createApp(
185
187
// Backup Firebase users
186
188
app . post (
187
189
'/users' ,
188
- backupUsersMiddleware ( { projectId : runtimeEnv . projectId , ...globalOptions } )
190
+ backupUsersMiddleware ( {
191
+ projectId : runtimeEnv . projectId ,
192
+ controllerToken : options . controllerToken ,
193
+ controllerDomain : options . controllerDomain ,
194
+ agentURL : agentURL ( runtimeEnv ) ,
195
+ ...globalOptions ,
196
+ } )
189
197
)
190
198
191
199
// List storage
@@ -197,7 +205,7 @@ export function createApp(
197
205
'/storage/:storageId' ,
198
206
updateStorageMiddleware ( {
199
207
adminPassword : options . adminPassword ,
200
- ...globalOptions
208
+ ...globalOptions ,
201
209
} )
202
210
)
203
211
// List files in the storage
@@ -217,21 +225,16 @@ interface HTTPSHandlerProps {
217
225
function httpsHandler ( {
218
226
handler,
219
227
agentOptions,
220
- runtimeEnv
228
+ runtimeEnv,
221
229
} : HTTPSHandlerProps ) {
222
230
if ( runtimeEnv ?. extensionId ) {
223
231
return functions . handler . https . onRequest ( handler )
224
232
} else {
225
- const runtimeOptions : functions . RuntimeOptions = {
226
- secrets : Object . values ( BackupFireConfig )
227
- }
228
-
229
- if ( agentOptions ?. memory ) runtimeOptions . memory = agentOptions . memory
230
- if ( agentOptions ?. timeout )
231
- runtimeOptions . timeoutSeconds = agentOptions . timeout
232
-
233
233
return functions
234
- . runWith ( runtimeOptions )
234
+ . runWith ( {
235
+ ...getRuntimeOptions ( agentOptions ) ,
236
+ secrets : Object . values ( BackupFireConfig ) ,
237
+ } )
235
238
. region ( agentOptions ?. region || defaultRegion )
236
239
. https . onRequest ( handler )
237
240
}
@@ -253,8 +256,8 @@ function sendInitializationPing(
253
256
token : options . controllerToken ,
254
257
projectId : runtimeEnv . projectId ,
255
258
runtime : runtimeEnv . region ,
256
- agentURL : agentURL ( runtimeEnv )
257
- }
259
+ agentURL : agentURL ( runtimeEnv ) ,
260
+ } ,
258
261
} )
259
262
return fetch ( pingURL )
260
263
}
@@ -290,7 +293,7 @@ function getRuntimeEnv(
290
293
// Node.js v8 runtime uses FUNCTION_NAME, v10 — FUNCTION_TARGET
291
294
// See: https://cloud.google.com/functions/docs/env-var#environment_variables_set_automatically
292
295
functionName : process . env . FUNCTION_NAME || process . env . FUNCTION_TARGET ,
293
- extensionId
296
+ extensionId,
294
297
}
295
298
}
296
299
@@ -326,7 +329,7 @@ function dummyHandler(
326
329
if ( options ?. timeout ) runtimeOptions . timeoutSeconds = options . timeout
327
330
328
331
return functions
329
- . runWith ( runtimeOptions )
332
+ . runWith ( getRuntimeOptions ( options ) )
330
333
. region ( options . region || defaultRegion )
331
334
. https . onRequest ( ( _req , resp ) => {
332
335
resp . end ( )
@@ -336,3 +339,24 @@ function dummyHandler(
336
339
function prettyJSON ( obj : any ) {
337
340
return JSON . stringify ( obj , null , 2 )
338
341
}
342
+
343
+ /**
344
+ *
345
+ * @param agentOptions - TODO
346
+ * @returns
347
+ */
348
+ function getRuntimeOptions (
349
+ agentOptions : AgentOptions | undefined
350
+ ) : functions . RuntimeOptions {
351
+ const options : functions . RuntimeOptions = {
352
+ // Always assign timeout to runtime options. Unless the user defines
353
+ // a custom timeout, we want to use the default timeout of 9 minutes,
354
+ // to make sure the user backups are completed regardless of how many
355
+ // there are.
356
+ timeoutSeconds : agentOptions ?. timeout || defaultTimeout ,
357
+ }
358
+
359
+ if ( agentOptions ?. memory ) options . memory = agentOptions . memory
360
+
361
+ return options
362
+ }
0 commit comments