-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: centralize environment variable lookups #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c98e127
refactor: centralize environment variable lookups
kevmoo def2748
review cleanup
kevmoo 8fc8dcb
oops
kevmoo f28b37a
cleanup
kevmoo 26793d4
refactor: harden cloud trace id parsing and logging
kevmoo 74854de
format fix
kevmoo 0fdbf4d
add missing license
kevmoo abe418f
Update lib/src/common/environment.dart
kevmoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright 2026 Firebase | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
||
| /// Provides unified access to environment variables, emulator checks, and | ||
| /// Google Cloud / Firebase configuration. | ||
| class FirebaseEnv { | ||
| FirebaseEnv() : environment = mockEnvironment ?? Platform.environment; | ||
|
|
||
| @visibleForTesting | ||
| static Map<String, String>? mockEnvironment; | ||
|
|
||
| final Map<String, String> environment; | ||
|
|
||
| /// Whether running within a Firebase emulator environment. | ||
| bool get isEmulator { | ||
| // Explicit functions emulator flag | ||
| return environment['FUNCTIONS_EMULATOR'] == 'true' || | ||
| // Generic fallback: check if any common emulator hosts are configured | ||
| _emulatorHostKeys.any(environment.containsKey); | ||
| } | ||
kevmoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Timezone setting. | ||
| String get tz => environment['TZ'] ?? 'UTC'; | ||
|
|
||
| /// Whether debug mode is enabled. | ||
| bool get debugMode => environment['FIREBASE_DEBUG_MODE'] == 'true'; | ||
|
|
||
| /// Whether to skip token verification (emulator only). | ||
| bool get skipTokenVerification => _getDebugFeature('skipTokenVerification'); | ||
|
|
||
| /// Whether CORS is enabled (emulator only). | ||
| bool get enableCors => _getDebugFeature('enableCors'); | ||
|
|
||
| bool _getDebugFeature(String key) { | ||
| if (environment['FIREBASE_DEBUG_FEATURES'] case final String json) { | ||
| try { | ||
| if (jsonDecode(json) case final Map<String, dynamic> m) { | ||
| return switch (m[key]) { | ||
| final bool value => value, | ||
| _ => false, | ||
| }; | ||
| } | ||
| } on FormatException { | ||
| // ignore | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /// Returns the current Firebase project ID. | ||
| /// | ||
| /// Checks standard environment variables in order: | ||
| /// 1. FIREBASE_PROJECT | ||
| /// 2. GCLOUD_PROJECT | ||
| /// 3. GOOGLE_CLOUD_PROJECT | ||
| /// 4. GCP_PROJECT | ||
| /// | ||
| /// If none are set, throws [StateError]. | ||
| String get projectId { | ||
| for (final option in _projectIdEnvKeyOptions) { | ||
| final value = environment[option]; | ||
| if (value != null && value.isNotEmpty) return value; | ||
| } | ||
|
|
||
| throw StateError( | ||
| 'No project ID found in environment. Checked: ${_projectIdEnvKeyOptions.join(', ')}', | ||
| ); | ||
| } | ||
|
|
||
| /// The port to listen on. | ||
| /// | ||
| /// Uses the [PORT] environment variable, defaulting to 8080. | ||
| int get port => int.tryParse(environment['PORT'] ?? '8080') ?? 8080; | ||
|
|
||
| /// The name of the Cloud Run service. | ||
| /// | ||
| /// Uses the `K_SERVICE` environment variable. | ||
| /// | ||
| /// See https://cloud.google.com/run/docs/container-contract#env-vars | ||
| String? get kService => environment['K_SERVICE']; | ||
|
|
||
| /// The name of the target function. | ||
| /// | ||
| /// Uses the `FUNCTION_TARGET` environment variable. | ||
| /// | ||
| /// See https://docs.cloud.google.com/run/docs/configuring/services/environment-variables#additional_reserved_environment_variables_when_deploying_functions | ||
| String? get functionTarget => environment['FUNCTION_TARGET']; | ||
|
|
||
| /// Whether the functions control API is enabled. | ||
| /// | ||
| /// Uses the `FUNCTIONS_CONTROL_API` environment variable. | ||
| /// | ||
| /// This is part of the contract with `firebase-tools`. | ||
| bool get functionsControlApi => | ||
| environment['FUNCTIONS_CONTROL_API'] == 'true'; | ||
| } | ||
|
|
||
| /// Common project ID environment variables checked in order. | ||
| const _projectIdEnvKeyOptions = [ | ||
| 'FIREBASE_PROJECT', | ||
| 'GCLOUD_PROJECT', | ||
| 'GOOGLE_CLOUD_PROJECT', | ||
| 'GCP_PROJECT', | ||
| ]; | ||
|
|
||
| /// Common emulator host keys used to detect emulator environment. | ||
| const _emulatorHostKeys = [ | ||
| 'FIRESTORE_EMULATOR_HOST', | ||
| 'FIREBASE_AUTH_EMULATOR_HOST', | ||
| 'FIREBASE_DATABASE_EMULATOR_HOST', | ||
| 'FIREBASE_STORAGE_EMULATOR_HOST', | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.