Skip to content

Commit 5ad33ab

Browse files
authored
Add Node debug token support (#4841)
1 parent ac4ad08 commit 5ad33ab

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

packages/app-check/src/debug.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,13 @@
1717

1818
import { getDebugState } from './state';
1919
import { readOrCreateDebugTokenFromStorage } from './storage';
20-
import { Deferred } from '@firebase/util';
20+
import { Deferred, getGlobal } from '@firebase/util';
2121

2222
declare global {
23-
interface Window {
24-
/**
25-
* When it is a string, we treat it as the debug token and give it to consumers as the app check token
26-
* When it is `true`, we will try to read the debug token from the indexeddb,
27-
* if it doesn't exist, create one, print it to console, and ask developers to register it in the Firebase console.
28-
* When it is `undefined`, `false` or any unsupported value type, the SDK will operate in production mode
29-
*/
30-
FIREBASE_APPCHECK_DEBUG_TOKEN?: boolean | string;
31-
}
23+
// var must be used for global scopes
24+
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#type-checking-for-globalthis
25+
// eslint-disable-next-line no-var
26+
var FIREBASE_APPCHECK_DEBUG_TOKEN: boolean | string | undefined;
3227
}
3328

3429
export function isDebugMode(): boolean {
@@ -50,9 +45,10 @@ export async function getDebugToken(): Promise<string> {
5045
}
5146

5247
export function initializeDebugMode(): void {
48+
const globals = getGlobal();
5349
if (
54-
typeof self.FIREBASE_APPCHECK_DEBUG_TOKEN !== 'string' &&
55-
self.FIREBASE_APPCHECK_DEBUG_TOKEN !== true
50+
typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== 'string' &&
51+
globals.FIREBASE_APPCHECK_DEBUG_TOKEN !== true
5652
) {
5753
return;
5854
}
@@ -62,8 +58,8 @@ export function initializeDebugMode(): void {
6258
const deferredToken = new Deferred<string>();
6359
debugState.token = deferredToken;
6460

65-
if (typeof self.FIREBASE_APPCHECK_DEBUG_TOKEN === 'string') {
66-
deferredToken.resolve(self.FIREBASE_APPCHECK_DEBUG_TOKEN);
61+
if (typeof globals.FIREBASE_APPCHECK_DEBUG_TOKEN === 'string') {
62+
deferredToken.resolve(globals.FIREBASE_APPCHECK_DEBUG_TOKEN);
6763
} else {
6864
deferredToken.resolve(readOrCreateDebugTokenFromStorage());
6965
}

packages/database/src/realtime/WebSocketConnection.ts

+7
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,16 @@ export class WebSocketConnection implements Transport {
166166
}
167167
};
168168

169+
// If using Node with admin creds, AppCheck-related checks are unnecessary.
170+
// It will send the authorization token.
169171
if (this.nodeAdmin) {
170172
options.headers['Authorization'] = this.authToken || '';
171173
} else {
174+
// If using Node without admin creds (which includes all uses of the
175+
// client-side Node SDK), it will send an AppCheck token if available.
176+
// Any other auth credentials will eventually be sent after the connection
177+
// is established, but aren't needed here as they don't effect the initial
178+
// request to establish a connection.
172179
options.headers['X-Firebase-AppCheck'] = this.appCheckToken || '';
173180
}
174181

packages/util/src/environment.ts

+17
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,20 @@ export function areCookiesEnabled(): boolean {
189189
}
190190
return true;
191191
}
192+
193+
/**
194+
* Polyfill for `globalThis` object.
195+
* @returns the `globalThis` object for the given environment.
196+
*/
197+
export function getGlobal(): typeof globalThis {
198+
if (typeof self !== 'undefined') {
199+
return self;
200+
}
201+
if (typeof window !== 'undefined') {
202+
return window;
203+
}
204+
if (typeof global !== 'undefined') {
205+
return global;
206+
}
207+
throw new Error('Unable to locate global object.');
208+
}

0 commit comments

Comments
 (0)