-
Notifications
You must be signed in to change notification settings - Fork 874
/
index.ts
123 lines (107 loc) · 3.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { FirebaseAppNext, FirebaseOptionsNext, FirebaseAppConfigNext, FirebaseAppInternalNext } from './types';
import { DEFAULT_ENTRY_NAME, PLATFORM_LOG_STRING } from '../constants';
import { ERROR_FACTORY, AppError } from '../errors';
import { ComponentContainer, Component, Name, ComponentType } from '@firebase/component';
import { version } from '../../../firebase/package.json';
import { FirebaseAppImplNext } from './firebaseApp';
import { apps, components, registerComponent } from './internal';
import { logger } from '../logger';
export const SDK_VERSION = version;
export function initializeApp(
options: FirebaseOptionsNext,
config?: FirebaseAppConfigNext
): FirebaseAppNext;
export function initializeApp(
options: FirebaseOptionsNext,
name?: string
): FirebaseAppNext;
export function initializeApp(
options: FirebaseOptionsNext,
rawConfig = {}
): FirebaseAppNext {
if (typeof rawConfig !== 'object') {
const name = rawConfig;
rawConfig = { name };
}
const config: Required<FirebaseAppConfigNext> = {
name: DEFAULT_ENTRY_NAME,
automaticDataCollectionEnabled: false,
...rawConfig
};
const name = config.name;
if (typeof name !== 'string' || !name) {
throw ERROR_FACTORY.create(AppError.BAD_APP_NAME, {
appName: String(name)
});
}
if (apps.has(name)) {
throw ERROR_FACTORY.create(AppError.DUPLICATE_APP, { appName: name });
}
const container = new ComponentContainer(name);
for (const component of components.values()) {
container.addComponent(component);
}
const newApp = new FirebaseAppImplNext(options, config, container);
apps.set(name, newApp);
return newApp;
}
export function getApp(name: string = DEFAULT_ENTRY_NAME): FirebaseAppNext {
const app = apps.get(name);
if (!app) {
throw ERROR_FACTORY.create(AppError.NO_APP, { appName: name });
}
return app;
}
export function getApps(): FirebaseAppNext[] {
return Array.from(apps.values());
}
export function deleteApp(app: FirebaseAppNext): Promise<void> {
const name = app.name;
if (apps.has(name)) {
apps.delete(name);
(app as FirebaseAppInternalNext).isDeleted = true;
// TODO: what to do with other SDKs?
}
return Promise.resolve();
}
export function registerVersion(
libraryKeyOrName: string,
version: string,
variant?: string
): void {
// TODO: We can use this check to whitelist strings when/if we set up
// a good whitelist system.
let library = PLATFORM_LOG_STRING[libraryKeyOrName] ?? libraryKeyOrName;
if (variant) {
library += `-${variant}`;
}
const libraryMismatch = library.match(/\s|\//);
const versionMismatch = version.match(/\s|\//);
if (libraryMismatch || versionMismatch) {
const warning = [
`Unable to register library "${library}" with version "${version}":`
];
if (libraryMismatch) {
warning.push(
`library name "${library}" contains illegal characters (whitespace or "/")`
);
}
if (libraryMismatch && versionMismatch) {
warning.push('and');
}
if (versionMismatch) {
warning.push(
`version name "${version}" contains illegal characters (whitespace or "/")`
);
}
logger.warn(warning.join(' '));
return;
}
registerComponent(
new Component(
`${library}-version` as Name,
() => ({ library, version }),
ComponentType.VERSION
)
);
}