@sneat/app 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,6 +5,24 @@ import { Capacitor } from '@capacitor/core';
|
|
|
5
5
|
import { provideFirebaseApp, initializeApp, FirebaseApp, } from '@angular/fire/app';
|
|
6
6
|
import { getAuth } from 'firebase/auth';
|
|
7
7
|
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
|
|
8
|
+
import { isLocalhost } from './init-helpers';
|
|
9
|
+
// Defense in depth: a production bundle should never carry emulator config
|
|
10
|
+
// (use appEnvironmentConfig()), but if one slips through, refuse to point real
|
|
11
|
+
// users at 127.0.0.1 — that just yields "refused to connect". Warn loudly so a
|
|
12
|
+
// post-deploy smoke test (and the console) surfaces the misconfiguration.
|
|
13
|
+
function emulatorAllowed(hasEmulator) {
|
|
14
|
+
if (!hasEmulator) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (isLocalhost()) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
console.error('[init-firebase] Emulator config present on a non-localhost host ' +
|
|
21
|
+
`("${typeof location !== 'undefined' ? location.hostname : '?'}") — ` +
|
|
22
|
+
'ignoring it. This is a build/deploy misconfiguration: a production ' +
|
|
23
|
+
'build shipped the emulator environment.');
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
8
26
|
export function provideFireApp(firebaseConfig) {
|
|
9
27
|
return provideFirebaseApp(() => initFirebase(firebaseConfig));
|
|
10
28
|
}
|
|
@@ -16,7 +34,7 @@ export function getAngularFireProviders(firebaseConfig) {
|
|
|
16
34
|
const fbApp = injector.get(FirebaseApp);
|
|
17
35
|
const firestore = getFirestore(fbApp);
|
|
18
36
|
const { emulator } = firebaseConfig;
|
|
19
|
-
if (emulator) {
|
|
37
|
+
if (emulator && emulatorAllowed(!!emulator)) {
|
|
20
38
|
connectFirestoreEmulator(firestore, emulator.firestoreHost || '127.0.0.1', emulator.firestorePort);
|
|
21
39
|
if (emulator.firestorePort === 443) {
|
|
22
40
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
@@ -39,7 +57,7 @@ export function getAngularFireProviders(firebaseConfig) {
|
|
|
39
57
|
auth = getAuth(fbApp);
|
|
40
58
|
}
|
|
41
59
|
const { emulator } = firebaseConfig;
|
|
42
|
-
if (emulator?.authPort) {
|
|
60
|
+
if (emulator?.authPort && emulatorAllowed(!!emulator)) {
|
|
43
61
|
// alert('Using firebase auth emulator');
|
|
44
62
|
const authUrl = `${emulator.authPort === 443 ? 'https' : 'http'}://${emulator.authHost || '127.0.0.1'}:${emulator.authPort}`;
|
|
45
63
|
// console.log('authUrl: ', authUrl);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init-firebase.js","sourceRoot":"","sources":["../../../../../libs/app/src/lib/init-firebase.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAEL,mBAAmB,EACnB,yBAAyB,EACzB,cAAc,EACd,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,WAAW,GACZ,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"init-firebase.js","sourceRoot":"","sources":["../../../../../libs/app/src/lib/init-firebase.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAEL,mBAAmB,EACnB,yBAAyB,EACzB,cAAc,EACd,WAAW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A6B;gBAC7B,mBAAmB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YACrC,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC,CAAC;KACH,CAAC;IACF,IAAI,cAAc,EAAE,aAAa,KAAK,qBAAqB,EAAE,CAAC;QAC5D,SAAS,CAAC,IAAI,CACZ,gBAAgB,CAAC,GAAG,EAAE;YACpB,2CAA2C;YAC3C,qCAAqC;YACrC,KAAK;YACL,2CAA2C;YAC3C,MAAM,WAAW,GAAG,YAAY,EAAE,CAAC;YACnC,OAAO,WAAW,CAAC;QACrB,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,YAAY,CAAC,cAA+B;IACnD,OAAO,aAAa,CAAC,cAAc,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import { EnvironmentProviders, Type } from '@angular/core';\nimport { getAnalytics, provideAnalytics } from '@angular/fire/analytics';\nimport {\n Auth,\n connectAuthEmulator,\n indexedDBLocalPersistence,\n initializeAuth,\n provideAuth,\n} from '@angular/fire/auth';\nimport { connectFirestoreEmulator } from '@angular/fire/firestore';\nimport { Capacitor } from '@capacitor/core';\nimport { IFirebaseConfig } from '@sneat/core';\nimport {\n provideFirebaseApp,\n initializeApp,\n FirebaseApp,\n} from '@angular/fire/app';\nimport { getAuth } from 'firebase/auth';\nimport { getFirestore, provideFirestore } from '@angular/fire/firestore';\nimport { isLocalhost } from './init-helpers';\n\n// Defense in depth: a production bundle should never carry emulator config\n// (use appEnvironmentConfig()), but if one slips through, refuse to point real\n// users at 127.0.0.1 — that just yields \"refused to connect\". Warn loudly so a\n// post-deploy smoke test (and the console) surfaces the misconfiguration.\nfunction emulatorAllowed(hasEmulator: boolean): boolean {\n if (!hasEmulator) {\n return false;\n }\n if (isLocalhost()) {\n return true;\n }\n console.error(\n '[init-firebase] Emulator config present on a non-localhost host ' +\n `(\"${typeof location !== 'undefined' ? location.hostname : '?'}\") — ` +\n 'ignoring it. This is a build/deploy misconfiguration: a production ' +\n 'build shipped the emulator environment.',\n );\n return false;\n}\n\nexport function provideFireApp(firebaseConfig: IFirebaseConfig) {\n return provideFirebaseApp(() => initFirebase(firebaseConfig));\n}\n\nexport function getAngularFireProviders(\n firebaseConfig: IFirebaseConfig,\n): EnvironmentProviders[] {\n const providers = [\n provideFirebaseApp(() => initFirebase(firebaseConfig)),\n provideFirestore((injector) => {\n // console.log('AngularFire: provideFirestore');\n const fbApp = injector.get(FirebaseApp);\n const firestore = getFirestore(fbApp);\n const { emulator } = firebaseConfig;\n if (emulator && emulatorAllowed(!!emulator)) {\n connectFirestoreEmulator(\n firestore,\n emulator.firestoreHost || '127.0.0.1',\n emulator.firestorePort,\n );\n if (emulator.firestorePort === 443) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-expect-error\n firestore['_settings']['ssl'] = true;\n }\n }\n return firestore;\n }),\n provideAuth((injector) => {\n // console.log('AngularFire: provideAuth');\n const fbApp = injector.get<FirebaseApp>(FirebaseApp as Type<FirebaseApp>);\n let auth: Auth;\n if (Capacitor.isNativePlatform()) {\n auth = initializeAuth(fbApp, {\n persistence: indexedDBLocalPersistence,\n });\n } else {\n auth = getAuth(fbApp);\n }\n const { emulator } = firebaseConfig;\n if (emulator?.authPort && emulatorAllowed(!!emulator)) {\n // alert('Using firebase auth emulator');\n const authUrl = `${emulator.authPort === 443 ? 'https' : 'http'}://${emulator.authHost || '127.0.0.1'}:${emulator.authPort}`;\n // console.log('authUrl: ', authUrl);\n // noinspection HttpUrlsUsage\n connectAuthEmulator(auth, authUrl);\n }\n return auth;\n }),\n ];\n if (firebaseConfig?.measurementId !== 'G-PROVIDE_IF_NEEDED') {\n providers.push(\n provideAnalytics(() => {\n // const fbApp = injector.get<FirebaseApp>(\n // \tFirebaseApp as Type<FirebaseApp>,\n // );\n // const fbAnalytics = getAnalytics(fbApp);\n const fbAnalytics = getAnalytics();\n return fbAnalytics;\n }),\n );\n }\n return providers;\n}\n\nfunction initFirebase(firebaseConfig: IFirebaseConfig): FirebaseApp {\n return initializeApp(firebaseConfig);\n}\n"]}
|
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
import { emulatorEnvironmentConfig } from '../environments/environment.base';
|
|
2
|
+
// True only when the app is actually running on a developer machine. Used to
|
|
3
|
+
// decide, at RUNTIME, whether to use the Firebase emulator — see
|
|
4
|
+
// appEnvironmentConfig() below.
|
|
5
|
+
export function isLocalhost() {
|
|
6
|
+
return (typeof location !== 'undefined' &&
|
|
7
|
+
['localhost', '127.0.0.1', '[::1]', '0.0.0.0'].includes(location.hostname));
|
|
8
|
+
}
|
|
9
|
+
// Fail-safe environment selection. Use the Firebase emulator ONLY when running
|
|
10
|
+
// on localhost; every deployed domain gets the production config passed in.
|
|
11
|
+
//
|
|
12
|
+
// Because the emulator-vs-production decision is made at runtime from the
|
|
13
|
+
// hostname — NOT from a build-time `fileReplacements` swap — a mis-built or
|
|
14
|
+
// mis-deployed bundle can never ship the emulator config to production. This
|
|
15
|
+
// structurally eliminates the recurring "127.0.0.1 refused to connect" class of
|
|
16
|
+
// bug: forgetting `--configuration production`, a stale `dist/`, an nx cache
|
|
17
|
+
// hit, or deploying a dev build can no longer point real users at the emulator.
|
|
18
|
+
//
|
|
19
|
+
// Apps should define a single environment.ts:
|
|
20
|
+
// export const fooEnvironmentConfig = appEnvironmentConfig({ ...prod config });
|
|
21
|
+
// and drop environment.prod.ts + the production fileReplacements entirely.
|
|
22
|
+
export function appEnvironmentConfig(prod) {
|
|
23
|
+
return isLocalhost()
|
|
24
|
+
? appSpecificConfig(emulatorEnvironmentConfig)
|
|
25
|
+
: prod;
|
|
26
|
+
}
|
|
1
27
|
function firebaseApiKey(useEmulators, apiKey) {
|
|
2
28
|
return useEmulators ? 'emulator-does-not-need-api-key' : apiKey;
|
|
3
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init-helpers.js","sourceRoot":"","sources":["../../../../../libs/app/src/lib/init-helpers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"init-helpers.js","sourceRoot":"","sources":["../../../../../libs/app/src/lib/init-helpers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAE7E,6EAA6E;AAC7E,iEAAiE;AACjE,gCAAgC;AAChC,MAAM,UAAU,WAAW;IACzB,OAAO,CACL,OAAO,QAAQ,KAAK,WAAW;QAC/B,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAC3E,CAAC;AACJ,CAAC;AAED,+EAA+E;AAC/E,4EAA4E;AAC5E,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,6EAA6E;AAC7E,gFAAgF;AAChF,6EAA6E;AAC7E,gFAAgF;AAChF,EAAE;AACF,8CAA8C;AAC9C,kFAAkF;AAClF,2EAA2E;AAC3E,MAAM,UAAU,oBAAoB,CAClC,IAAwB;IAExB,OAAO,WAAW,EAAE;QAClB,CAAC,CAAC,iBAAiB,CAAC,yBAAyB,CAAC;QAC9C,CAAC,CAAC,IAAI,CAAC;AACX,CAAC;AAED,SAAS,cAAc,CAAC,YAAqB,EAAE,MAAc;IAC3D,OAAO,YAAY,CAAC,CAAC,CAAC,gCAAgC,CAAC,CAAC,CAAC,MAAM,CAAC;AAClE,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,YAAqB,EACrB,SAAiB;IAEjB,OAAO,YAAY,CAAC,CAAC,CAAC,OAAO,GAAG,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;AACxD,CAAC;AAED,8FAA8F;AAC9F,2CAA2C;AAC3C,uBAAuB;AACvB,kBAAkB;AAClB,2CAA2C;AAC3C,IAAI;AAEJ,4BAA4B;AAC5B,MAAM,UAAU,iBAAiB,CAC/B,SAA6B;IAG7B,IAAI,MAAM,GAAuB;QAC/B,GAAG,SAAS;QACZ,cAAc,EAAE;YACd,GAAG,SAAS,CAAC,cAAc;SAC5B;KACF,CAAC;IACF,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,CAAC;IAClC,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC,QAAQ,CAAC;IACrD,MAAM,SAAS,GAAG,iBAAiB,CAAC,WAAW,EAAE,cAAc,CAAC,SAAS,CAAC,CAAC;IAC3E,MAAM,GAAG;QACP,GAAG,MAAM;QACT,cAAc,EAAE;YACd,GAAG,cAAc;YACjB,MAAM,EAAE,cAAc,CAAC,WAAW,EAAE,cAAc,CAAC,MAAM,CAAC;YAC1D,SAAS,EAAE,SAAS;SACrB;KACF,CAAC;IACF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { IEnvironmentConfig } from '@sneat/core';\nimport { emulatorEnvironmentConfig } from '../environments/environment.base';\n\n// True only when the app is actually running on a developer machine. Used to\n// decide, at RUNTIME, whether to use the Firebase emulator — see\n// appEnvironmentConfig() below.\nexport function isLocalhost(): boolean {\n return (\n typeof location !== 'undefined' &&\n ['localhost', '127.0.0.1', '[::1]', '0.0.0.0'].includes(location.hostname)\n );\n}\n\n// Fail-safe environment selection. Use the Firebase emulator ONLY when running\n// on localhost; every deployed domain gets the production config passed in.\n//\n// Because the emulator-vs-production decision is made at runtime from the\n// hostname — NOT from a build-time `fileReplacements` swap — a mis-built or\n// mis-deployed bundle can never ship the emulator config to production. This\n// structurally eliminates the recurring \"127.0.0.1 refused to connect\" class of\n// bug: forgetting `--configuration production`, a stale `dist/`, an nx cache\n// hit, or deploying a dev build can no longer point real users at the emulator.\n//\n// Apps should define a single environment.ts:\n// export const fooEnvironmentConfig = appEnvironmentConfig({ ...prod config });\n// and drop environment.prod.ts + the production fileReplacements entirely.\nexport function appEnvironmentConfig(\n prod: IEnvironmentConfig,\n): IEnvironmentConfig {\n return isLocalhost()\n ? appSpecificConfig(emulatorEnvironmentConfig)\n : prod;\n}\n\nfunction firebaseApiKey(useEmulators: boolean, apiKey: string): string {\n return useEmulators ? 'emulator-does-not-need-api-key' : apiKey;\n}\n\nexport function firebaseProjectId(\n useEmulators: boolean,\n projectId: string,\n): string {\n return useEmulators ? 'demo-' + projectId : projectId;\n}\n\n// function firebaseDatabaseURL(useEmulators: boolean, projectId: string): string | undefined{\n// \t// noinspection SpellCheckingInspection\n// \treturn useEmulators\n// \t\t? undefined :\n// \t\t`https://${projectId}.firebaseio.com`;\n// }\n\n// TODO: document why needed\nexport function appSpecificConfig(\n envConfig: IEnvironmentConfig,\n // appConfig: IAppSpecificConfig,\n): IEnvironmentConfig {\n let config: IEnvironmentConfig = {\n ...envConfig,\n firebaseConfig: {\n ...envConfig.firebaseConfig,\n },\n };\n const { firebaseConfig } = config;\n const useEmulator = !!config.firebaseConfig.emulator;\n const projectId = firebaseProjectId(useEmulator, firebaseConfig.projectId);\n config = {\n ...config,\n firebaseConfig: {\n ...firebaseConfig,\n apiKey: firebaseApiKey(useEmulator, firebaseConfig.apiKey),\n projectId: projectId,\n },\n };\n return config;\n}\n"]}
|
package/lib/init-helpers.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import { IEnvironmentConfig } from '@sneat/core';
|
|
2
|
+
export declare function isLocalhost(): boolean;
|
|
3
|
+
export declare function appEnvironmentConfig(prod: IEnvironmentConfig): IEnvironmentConfig;
|
|
2
4
|
export declare function firebaseProjectId(useEmulators: boolean, projectId: string): string;
|
|
3
5
|
export declare function appSpecificConfig(envConfig: IEnvironmentConfig): IEnvironmentConfig;
|