Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 1x 1x 1x 3x 3x 3x 3x 3x 1x | // Firebase Admin SDK bootstrap (firebase-admin v14, modular API).
//
// The Admin SDK authenticates exactly like the Pub/Sub / Storage clients: it
// reads the service account key pointed to by GOOGLE_APPLICATION_CREDENTIALS
// (applicationDefault()). No extra credentials are required.
//
// Everything is initialised lazily and guarded by config.firebaseEnabled so
// that the test suite (and any environment without a service account) never
// touches a live database.
const config = require('../config');
let app = null;
let initialized = false;
// Initialise the singleton Admin app the first time it is needed. Returns null
// when Firebase is disabled or when initialisation fails (we log and degrade
// gracefully rather than crash the request/worker).
function getApp() {
Eif (!config.firebaseEnabled) {
return null;
}
if (initialized) {
return app;
}
initialized = true;
try {
// Required lazily so the dependency is only loaded when Firebase is enabled
// (keeps the CommonJS test runner from pulling in the whole Admin SDK).
const { initializeApp, getApps, getApp: getExistingApp, applicationDefault } =
require('firebase-admin/app');
// Reuse an app if one was already created elsewhere in the process.
app = getApps().length
? getExistingApp()
: initializeApp({
credential: applicationDefault(),
databaseURL: config.firebaseDatabaseUrl
});
} catch (error) {
console.error('[firebase] failed to initialise Admin SDK:', error);
app = null;
}
return app;
}
// Realtime Database handle, or null when Firebase is unavailable.
function getDb() {
const current = getApp();
Eif (!current) {
return null;
}
const { getDatabase } = require('firebase-admin/database');
return getDatabase(current);
}
// Auth handle (used to verify web ID tokens), or null when unavailable.
function getAuth() {
const current = getApp();
if (!current) {
return null;
}
const { getAuth: getAdminAuth } = require('firebase-admin/auth');
return getAdminAuth(current);
}
module.exports = {
getApp,
getDb,
getAuth,
enabled: () => config.firebaseEnabled
};
|