Skip to content

Commit

Permalink
Add debug mode (#992)
Browse files Browse the repository at this point in the history
Debug mode is intended to be used by the Functions Emulator to enable/disable certain aspect of the Functions SDK. Debug mode allows the Functions SDK to operate in development friendly way without the need to monkey-patch the Functions SDK. For example, auth or App Check token verification can be disabled during development to make Functions SDK compatible with the Auth Emulator.

This feature is intended for internal use-cases and won't expose any public interfaces. No one outside the Firebase team should use it, and if they do, we can't promise any stability or feature support.
  • Loading branch information
taeold committed Oct 18, 2021
1 parent 63bd14d commit ede96e6
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/common/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// The MIT License (MIT)
//
// Copyright (c) 2021 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Do NOT turn on a debug feature in production.
const debugMode = process.env.FIREBASE_DEBUG_MODE === 'true';

interface DebugFeatures {
skipCallableTokenVerification?: boolean;
}

function loadDebugFeatures(): DebugFeatures {
if (!debugMode) return {};
try {
const obj = JSON.parse(process.env.FIREBASE_DEBUG_FEATURES);
if (typeof obj !== 'object') {
return {};
}
return obj as DebugFeatures;
} catch (e) {
return {};
}
}

/* @internal */
export function debugFeatureValue(feat: keyof DebugFeatures): unknown {
if (!debugMode) return;
return loadDebugFeatures()[feat];
}

/* @internal */
export function isDebugFeatureEnabled(feat: keyof DebugFeatures): boolean {
return debugMode && !!debugFeatureValue(feat);
}

0 comments on commit ede96e6

Please sign in to comment.