firestore-queue
Version:
A powerful, scalable queue system built on Google Firestore with time-based indexing, auto-configuration, and connection reuse
114 lines • 4.4 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractProjectIdFromServiceAccount = extractProjectIdFromServiceAccount;
exports.loadServiceAccount = loadServiceAccount;
exports.findServiceAccountFile = findServiceAccountFile;
exports.autoLoadFirebaseConfig = autoLoadFirebaseConfig;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
/**
* Extract project ID from service account JSON file
*/
function extractProjectIdFromServiceAccount(serviceAccountPath) {
try {
if (!fs.existsSync(serviceAccountPath)) {
throw new Error(`Service account file not found: ${serviceAccountPath}`);
}
const serviceAccountData = JSON.parse(fs.readFileSync(serviceAccountPath, 'utf8'));
if (!serviceAccountData.project_id) {
throw new Error('project_id not found in service account file');
}
return serviceAccountData.project_id;
}
catch (error) {
throw new Error(`Failed to extract project ID from service account: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Load service account data
*/
function loadServiceAccount(serviceAccountPath) {
try {
if (!fs.existsSync(serviceAccountPath)) {
throw new Error(`Service account file not found: ${serviceAccountPath}`);
}
return JSON.parse(fs.readFileSync(serviceAccountPath, 'utf8'));
}
catch (error) {
throw new Error(`Failed to load service account: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Auto-detect service account file in common locations
*/
function findServiceAccountFile(startDir = process.cwd()) {
const commonPaths = [
path.join(startDir, 'config/firebase-service-account.json'),
path.join(startDir, 'configs/firebase-service-account.json'),
path.join(startDir, 'firebase-service-account.json'),
path.join(startDir, '.firebase/service-account.json'),
];
for (const filePath of commonPaths) {
if (fs.existsSync(filePath)) {
return filePath;
}
}
return null;
}
/**
* Smart configuration loader - auto-detects project ID and service account
*/
function autoLoadFirebaseConfig(options = {}) {
let serviceAccountPath = options.serviceAccountPath;
let projectId = options.projectId;
// Auto-detect service account file if not provided
if (!serviceAccountPath) {
const foundPath = findServiceAccountFile(options.searchDir);
if (!foundPath) {
throw new Error('No service account file found. Please provide serviceAccountPath or place file in config/firebase-service-account.json');
}
serviceAccountPath = foundPath;
}
// Extract project ID if not provided
if (!projectId) {
projectId = extractProjectIdFromServiceAccount(serviceAccountPath);
}
return {
projectId,
serviceAccountPath,
};
}
//# sourceMappingURL=service-account-helper.js.map