-
Notifications
You must be signed in to change notification settings - Fork 924
/
Copy pathdiscovery.ts
157 lines (144 loc) · 4.16 KB
/
discovery.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @license
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EmulatorConfig, HostAndPort } from '../public_types';
import { makeUrl, fixHostname } from './url';
/**
* Use the Firebase Emulator hub to discover other running emulators.
*
* @param hub the host and port where the Emulator Hub is running
* @private
*/
export async function discoverEmulators(
hub: HostAndPort,
fetchImpl: typeof fetch = fetch
): Promise<DiscoveredEmulators> {
const res = await fetchImpl(makeUrl(hub, '/emulators'));
if (!res.ok) {
throw new Error(
`HTTP Error ${res.status} when attempting to reach Emulator Hub at ${res.url}, are you sure it is running?`
);
}
const emulators: DiscoveredEmulators = {};
const data = await res.json();
if (data.database) {
emulators.database = {
host: data.database.host,
port: data.database.port
};
}
if (data.firestore) {
emulators.firestore = {
host: data.firestore.host,
port: data.firestore.port
};
}
if (data.storage) {
emulators.storage = {
host: data.storage.host,
port: data.storage.port
};
}
if (data.hub) {
emulators.hub = {
host: data.hub.host,
port: data.hub.port
};
}
return emulators;
}
/**
* @private
*/
export interface DiscoveredEmulators {
database?: HostAndPort;
firestore?: HostAndPort;
storage?: HostAndPort;
hub?: HostAndPort;
}
/**
* @private
*/
export function getEmulatorHostAndPort(
emulator: keyof DiscoveredEmulators,
conf?: EmulatorConfig,
discovered?: DiscoveredEmulators
) {
if (conf && ('host' in conf || 'port' in conf)) {
const { host, port } = conf;
if (host || port) {
if (!host || !port) {
throw new Error(
`Invalid configuration ${emulator}.host=${host} and ${emulator}.port=${port}. ` +
'If either parameter is supplied, both must be defined.'
);
}
if (discovered && !discovered[emulator]) {
console.warn(
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
'reports it as not running. This may lead to errors such as connection refused.'
);
}
return {
host: fixHostname(conf.host, discovered?.hub?.host),
port: conf.port
};
}
}
const envVar = EMULATOR_HOST_ENV_VARS[emulator];
const fallback = discovered?.[emulator] || emulatorFromEnvVar(envVar);
if (fallback) {
if (discovered && !discovered[emulator]) {
console.warn(
`Warning: the environment variable ${envVar} is set, but the Emulator hub reports the ` +
`${emulator} emulator as not running. This may lead to errors such as connection refused.`
);
}
return {
host: fixHostname(fallback.host, discovered?.hub?.host),
port: fallback.port
};
}
}
// Visible for testing.
export const EMULATOR_HOST_ENV_VARS = {
'database': 'FIREBASE_DATABASE_EMULATOR_HOST',
'firestore': 'FIRESTORE_EMULATOR_HOST',
'hub': 'FIREBASE_EMULATOR_HUB',
'storage': 'FIREBASE_STORAGE_EMULATOR_HOST'
};
function emulatorFromEnvVar(envVar: string): HostAndPort | undefined {
const hostAndPort = process.env[envVar];
if (!hostAndPort) {
return undefined;
}
let parsed: URL;
try {
parsed = new URL(`http://${hostAndPort}`);
} catch {
throw new Error(
`Invalid format in environment variable ${envVar}=${hostAndPort} (expected host:port)`
);
}
let host = parsed.hostname;
const port = Number(parsed.port || '80');
if (!Number.isInteger(port)) {
throw new Error(
`Invalid port in environment variable ${envVar}=${hostAndPort}`
);
}
return { host, port };
}