forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemulator_config.mjs
More file actions
41 lines (36 loc) · 1.37 KB
/
Copy pathemulator_config.mjs
File metadata and controls
41 lines (36 loc) · 1.37 KB
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
import { createServer } from "node:net";
import { writeFile } from "node:fs/promises";
const LOOPBACK_HOST = "127.0.0.1";
export async function allocateLoopbackPort() {
return await new Promise((resolve, reject) => {
const server = createServer();
server.once("error", reject);
server.listen(0, LOOPBACK_HOST, () => {
const address = server.address();
if (!address || typeof address === "string") {
reject(new Error("Could not allocate a loopback emulator port"));
return;
}
server.close((error) => (error ? reject(error) : resolve(address.port)));
});
});
}
export async function writeEmulatorConfig(configPath) {
const [port, websocketPort] = await Promise.all([allocateLoopbackPort(), allocateLoopbackPort()]);
if (port === websocketPort) {
throw new Error("Firestore API and websocket ports must be distinct");
}
await writeFile(
configPath,
`${JSON.stringify({ emulators: { firestore: { host: LOOPBACK_HOST, port, websocketPort } } })}\n`,
);
return { port, websocketPort };
}
if (import.meta.url === `file://${process.argv[1]}`) {
const [configPath] = process.argv.slice(2);
if (!configPath) {
throw new Error("Usage: emulator_config.mjs <firebase.json path>");
}
const { port, websocketPort } = await writeEmulatorConfig(configPath);
process.stdout.write(`${port} ${websocketPort}\n`);
}