From 4fab3aadd42150c76f83b40d8167d376aae258e5 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 7 Oct 2021 14:34:19 -0700 Subject: [PATCH 1/9] Add module to determine whether a debug feature is enabled. --- src/debug.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/debug.ts diff --git a/src/debug.ts b/src/debug.ts new file mode 100644 index 000000000..39d3f7f01 --- /dev/null +++ b/src/debug.ts @@ -0,0 +1,35 @@ +// 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. + +// Debug features must only be used in non-production environment. +// DO NOT turn on a debug feature in prod. +const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; + +const feats = { + callableSkipTokenVerification: + process.env.FIREBASE_FUNCTIONS_DEBUG_CALLABLE_SKIP_TOKEN_VERIFICATION === 'true', +} as const; + +/* @internal */ +export const isDebugFeatureEnabled = (feat: keyof typeof feats): boolean => { + return debugMode && !!feats[feat]; +}; From 4bdc70087c94243eab878ba8811116253b577d92 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 7 Oct 2021 16:06:45 -0700 Subject: [PATCH 2/9] Try to be more clever. --- src/debug.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/debug.ts b/src/debug.ts index 39d3f7f01..d937d2e9e 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -23,13 +23,29 @@ // Debug features must only be used in non-production environment. // DO NOT turn on a debug feature in prod. const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; +const camelToSnake = str => str.replace(/[A-Z]/g, c => `_${c}`).toUpperCase(); -const feats = { - callableSkipTokenVerification: - process.env.FIREBASE_FUNCTIONS_DEBUG_CALLABLE_SKIP_TOKEN_VERIFICATION === 'true', -} as const; +const supportedDebugFeatures = ['callableSkipTokenVerification'] as const; +type DebugFeature = typeof supportedDebugFeatures[number]; + +const debugFeatureValues: Record< + DebugFeature, + string +> = supportedDebugFeatures.reduce( + (prev, cur) => ({ + ...prev, + [cur]: process.env[`FIREBASE_FUNCTIONS_DEBUG_${camelToSnake(cur)}`], + }), + {} as Record +); + +/* @internal */ +export const isDebugFeatureEnabled = (feat: DebugFeature): boolean => { + return debugMode && !!debugFeatureValues[feat]; +}; /* @internal */ -export const isDebugFeatureEnabled = (feat: keyof typeof feats): boolean => { - return debugMode && !!feats[feat]; +export const debugFeatureValue = (feat: DebugFeature): string | undefined => { + if (!debugMode) return; + return debugFeatureValues[feat]; }; From d34c828149668daabc09ea0eb04461048e747445 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 7 Oct 2021 16:09:13 -0700 Subject: [PATCH 3/9] Minor cleanups. --- src/debug.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/debug.ts b/src/debug.ts index d937d2e9e..3927f4b01 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -20,14 +20,11 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -// Debug features must only be used in non-production environment. -// DO NOT turn on a debug feature in prod. -const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; -const camelToSnake = str => str.replace(/[A-Z]/g, c => `_${c}`).toUpperCase(); - +// Do NOT turn on a debug feature in production. Debug features must only be used in non-prod environment. const supportedDebugFeatures = ['callableSkipTokenVerification'] as const; type DebugFeature = typeof supportedDebugFeatures[number]; +const camelToSnake = str => str.replace(/[A-Z]/g, c => `_${c}`).toUpperCase(); const debugFeatureValues: Record< DebugFeature, string @@ -39,6 +36,8 @@ const debugFeatureValues: Record< {} as Record ); +const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; + /* @internal */ export const isDebugFeatureEnabled = (feat: DebugFeature): boolean => { return debugMode && !!debugFeatureValues[feat]; From 9af95999aff8cdbb81621d12a832b27f52422bf5 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 7 Oct 2021 20:03:36 -0700 Subject: [PATCH 4/9] Meaningless nits. --- src/debug.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/debug.ts b/src/debug.ts index 3927f4b01..8d0aa393c 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -21,9 +21,10 @@ // SOFTWARE. // Do NOT turn on a debug feature in production. Debug features must only be used in non-prod environment. +const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; const supportedDebugFeatures = ['callableSkipTokenVerification'] as const; -type DebugFeature = typeof supportedDebugFeatures[number]; +type DebugFeature = typeof supportedDebugFeatures[number]; const camelToSnake = str => str.replace(/[A-Z]/g, c => `_${c}`).toUpperCase(); const debugFeatureValues: Record< DebugFeature, @@ -36,8 +37,6 @@ const debugFeatureValues: Record< {} as Record ); -const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; - /* @internal */ export const isDebugFeatureEnabled = (feat: DebugFeature): boolean => { return debugMode && !!debugFeatureValues[feat]; From 7be95bac315f672bb7290e07c76b1121e0d9e868 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 7 Oct 2021 22:13:50 -0700 Subject: [PATCH 5/9] Prettier. --- src/debug.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/debug.ts b/src/debug.ts index 8d0aa393c..9aef0862d 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -25,7 +25,8 @@ const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; const supportedDebugFeatures = ['callableSkipTokenVerification'] as const; type DebugFeature = typeof supportedDebugFeatures[number]; -const camelToSnake = str => str.replace(/[A-Z]/g, c => `_${c}`).toUpperCase(); +const camelToSnake = (str) => + str.replace(/[A-Z]/g, (c) => `_${c}`).toUpperCase(); const debugFeatureValues: Record< DebugFeature, string @@ -44,6 +45,6 @@ export const isDebugFeatureEnabled = (feat: DebugFeature): boolean => { /* @internal */ export const debugFeatureValue = (feat: DebugFeature): string | undefined => { - if (!debugMode) return; - return debugFeatureValues[feat]; + if (!debugMode) return; + return debugFeatureValues[feat]; }; From c8eb62b0ac1b12f0db06df389eb620386f304bc2 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Mon, 11 Oct 2021 17:07:05 -0700 Subject: [PATCH 6/9] Use one env var for feature toggle. --- src/debug.ts | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/debug.ts b/src/debug.ts index 9aef0862d..84377a3a2 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -20,31 +20,33 @@ // 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. Debug features must only be used in non-prod environment. -const debugMode = process.env.FIREBASE_FUNCTIONS_DEBUG_MODE === 'true'; -const supportedDebugFeatures = ['callableSkipTokenVerification'] as const; +// Do NOT turn on a debug feature in production. Debug features should only be used in non-prod environment. +const debugMode = process.env.FIREBASE_DEBUG_MODE === 'true'; -type DebugFeature = typeof supportedDebugFeatures[number]; -const camelToSnake = (str) => - str.replace(/[A-Z]/g, (c) => `_${c}`).toUpperCase(); -const debugFeatureValues: Record< - DebugFeature, - string -> = supportedDebugFeatures.reduce( - (prev, cur) => ({ - ...prev, - [cur]: process.env[`FIREBASE_FUNCTIONS_DEBUG_${camelToSnake(cur)}`], - }), - {} as Record -); +interface DebugFeatures { + skipCallableTokenVerification?: boolean; +} + +const debugFeatureValues: 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 const isDebugFeatureEnabled = (feat: DebugFeature): boolean => { +export function isDebugFeatureEnabled(feat: keyof DebugFeatures): boolean { return debugMode && !!debugFeatureValues[feat]; -}; +} /* @internal */ -export const debugFeatureValue = (feat: DebugFeature): string | undefined => { +export function debugFeatureValue(feat: keyof DebugFeatures): unknown { if (!debugMode) return; return debugFeatureValues[feat]; -}; +} From 67fc723890461ea1f6aaf26c464ece9db82b67e8 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 14 Oct 2021 15:46:31 -0700 Subject: [PATCH 7/9] Allow reloading debug feature config in env var. --- src/debug.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/debug.ts b/src/debug.ts index 84377a3a2..3a93ec98a 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -27,7 +27,7 @@ interface DebugFeatures { skipCallableTokenVerification?: boolean; } -const debugFeatureValues: DebugFeatures = (() => { +function loadDebugFeatures(): DebugFeatures { if (!debugMode) return {}; try { const obj = JSON.parse(process.env.FIREBASE_DEBUG_FEATURES); @@ -38,15 +38,15 @@ const debugFeatureValues: DebugFeatures = (() => { } catch (e) { return {}; } -})(); - -/* @internal */ -export function isDebugFeatureEnabled(feat: keyof DebugFeatures): boolean { - return debugMode && !!debugFeatureValues[feat]; } /* @internal */ export function debugFeatureValue(feat: keyof DebugFeatures): unknown { if (!debugMode) return; - return debugFeatureValues[feat]; + return loadDebugFeatures()[feat]; +} + +/* @internal */ +export function isDebugFeatureEnabled(feat: keyof DebugFeatures): boolean { + return debugMode && !!debugFeatureValue(feat); } From f6e5c96c479560504ca813674a1a244249caf6d6 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Thu, 14 Oct 2021 15:47:48 -0700 Subject: [PATCH 8/9] Shorten comments. --- src/debug.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/debug.ts b/src/debug.ts index 3a93ec98a..c3cbd86e1 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -20,7 +20,7 @@ // 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. Debug features should only be used in non-prod environment. +// Do NOT turn on a debug feature in production. const debugMode = process.env.FIREBASE_DEBUG_MODE === 'true'; interface DebugFeatures { From 543e59cbbd3d52911af112fe33888d584704a211 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Mon, 18 Oct 2021 15:35:46 -0700 Subject: [PATCH 9/9] Move debug.ts under src/common. --- src/{ => common}/debug.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{ => common}/debug.ts (100%) diff --git a/src/debug.ts b/src/common/debug.ts similarity index 100% rename from src/debug.ts rename to src/common/debug.ts