|
| 1 | +// Copyright 2026 Firebase |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'dart:convert'; |
| 16 | +import 'dart:io'; |
| 17 | + |
| 18 | +import 'package:meta/meta.dart'; |
| 19 | + |
| 20 | +/// Provides unified access to environment variables, emulator checks, and |
| 21 | +/// Google Cloud / Firebase configuration. |
| 22 | +class FirebaseEnv { |
| 23 | + FirebaseEnv() : environment = mockEnvironment ?? Platform.environment; |
| 24 | + |
| 25 | + @visibleForTesting |
| 26 | + static Map<String, String>? mockEnvironment; |
| 27 | + |
| 28 | + final Map<String, String> environment; |
| 29 | + |
| 30 | + /// Whether running within a Firebase emulator environment. |
| 31 | + bool get isEmulator { |
| 32 | + // Explicit functions emulator flag |
| 33 | + return environment['FUNCTIONS_EMULATOR'] == 'true' || |
| 34 | + // Generic fallback: check if any common emulator hosts are configured |
| 35 | + _emulatorHostKeys.any(environment.containsKey); |
| 36 | + } |
| 37 | + |
| 38 | + /// Timezone setting. |
| 39 | + String get tz => environment['TZ'] ?? 'UTC'; |
| 40 | + |
| 41 | + /// Whether debug mode is enabled. |
| 42 | + bool get debugMode => environment['FIREBASE_DEBUG_MODE'] == 'true'; |
| 43 | + |
| 44 | + /// Whether to skip token verification (emulator only). |
| 45 | + bool get skipTokenVerification => _getDebugFeature('skipTokenVerification'); |
| 46 | + |
| 47 | + /// Whether CORS is enabled (emulator only). |
| 48 | + bool get enableCors => _getDebugFeature('enableCors'); |
| 49 | + |
| 50 | + bool _getDebugFeature(String key) { |
| 51 | + if (environment['FIREBASE_DEBUG_FEATURES'] case final String json) { |
| 52 | + try { |
| 53 | + if (jsonDecode(json) case final Map<String, dynamic> m) { |
| 54 | + return switch (m[key]) { |
| 55 | + final bool value => value, |
| 56 | + _ => false, |
| 57 | + }; |
| 58 | + } |
| 59 | + } on FormatException { |
| 60 | + // ignore |
| 61 | + } |
| 62 | + } |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + /// Returns the current Firebase project ID. |
| 67 | + /// |
| 68 | + /// Checks standard environment variables in order: |
| 69 | + /// 1. FIREBASE_PROJECT |
| 70 | + /// 2. GCLOUD_PROJECT |
| 71 | + /// 3. GOOGLE_CLOUD_PROJECT |
| 72 | + /// 4. GCP_PROJECT |
| 73 | + /// |
| 74 | + /// If none are set, throws [StateError]. |
| 75 | + String get projectId { |
| 76 | + for (final option in _projectIdEnvKeyOptions) { |
| 77 | + final value = environment[option]; |
| 78 | + if (value != null && value.isNotEmpty) return value; |
| 79 | + } |
| 80 | + |
| 81 | + throw StateError( |
| 82 | + 'No project ID found in environment. Checked: ${_projectIdEnvKeyOptions.join(', ')}', |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /// The port to listen on. |
| 87 | + /// |
| 88 | + /// Uses the [PORT] environment variable, defaulting to 8080. |
| 89 | + int get port => int.tryParse(environment['PORT'] ?? '8080') ?? 8080; |
| 90 | + |
| 91 | + /// The name of the Cloud Run service. |
| 92 | + /// |
| 93 | + /// Uses the `K_SERVICE` environment variable. |
| 94 | + /// |
| 95 | + /// See https://cloud.google.com/run/docs/container-contract#env-vars |
| 96 | + String? get kService => environment['K_SERVICE']; |
| 97 | + |
| 98 | + /// The name of the target function. |
| 99 | + /// |
| 100 | + /// Uses the `FUNCTION_TARGET` environment variable. |
| 101 | + /// |
| 102 | + /// See https://docs.cloud.google.com/run/docs/configuring/services/environment-variables#additional_reserved_environment_variables_when_deploying_functions |
| 103 | + String? get functionTarget => environment['FUNCTION_TARGET']; |
| 104 | + |
| 105 | + /// Whether the functions control API is enabled. |
| 106 | + /// |
| 107 | + /// Uses the `FUNCTIONS_CONTROL_API` environment variable. |
| 108 | + /// |
| 109 | + /// This is part of the contract with `firebase-tools`. |
| 110 | + bool get functionsControlApi => |
| 111 | + environment['FUNCTIONS_CONTROL_API'] == 'true'; |
| 112 | +} |
| 113 | + |
| 114 | +/// Common project ID environment variables checked in order. |
| 115 | +const _projectIdEnvKeyOptions = [ |
| 116 | + 'FIREBASE_PROJECT', |
| 117 | + 'GCLOUD_PROJECT', |
| 118 | + 'GOOGLE_CLOUD_PROJECT', |
| 119 | + 'GCP_PROJECT', |
| 120 | +]; |
| 121 | + |
| 122 | +/// Common emulator host keys used to detect emulator environment. |
| 123 | +const _emulatorHostKeys = [ |
| 124 | + 'FIRESTORE_EMULATOR_HOST', |
| 125 | + 'FIREBASE_AUTH_EMULATOR_HOST', |
| 126 | + 'FIREBASE_DATABASE_EMULATOR_HOST', |
| 127 | + 'FIREBASE_STORAGE_EMULATOR_HOST', |
| 128 | +]; |
0 commit comments