-
Notifications
You must be signed in to change notification settings - Fork 903
/
constants.ts
53 lines (40 loc) · 1.37 KB
/
constants.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
import * as url from "url";
import { Address, Emulators } from "./types";
const DEFAULT_PORTS: { [s in Emulators]: number } = {
hosting: 5000,
functions: 5001,
firestore: 8080,
database: 9000,
};
const DEFAULT_HOST = "localhost";
export class Constants {
static SERVICE_FIRESTORE = "firestore.googleapis.com";
static SERVICE_REALTIME_DATABASE = "firebaseio.com";
static getDefaultHost(emulator: Emulators): string {
return DEFAULT_HOST;
}
static getDefaultPort(emulator: Emulators): number {
return DEFAULT_PORTS[emulator];
}
static getHostKey(emulator: Emulators): string {
return `emulators.${emulator.toString()}.host`;
}
static getPortKey(emulator: Emulators): string {
return `emulators.${emulator.toString()}.port`;
}
static getAddress(emulator: Emulators, options: any): Address {
const hostVal = options.config.get(this.getHostKey(emulator), DEFAULT_HOST);
const portVal = options.config.get(this.getPortKey(emulator), this.getDefaultPort(emulator));
const host = this.normalizeHost(hostVal);
const port = parseInt(portVal, 10);
return { host, port };
}
private static normalizeHost(host: string): string {
let normalized = host;
if (!normalized.startsWith("http")) {
normalized = `http://${normalized}`;
}
const u = url.parse(normalized);
return u.hostname || DEFAULT_HOST;
}
}