decentralised message store
1import { didSchema } from "@/lib/types/atproto";
2import "dotenv/config";
3import { z } from "zod";
4
5const dbUrl = process.env.DB_URL;
6if (!dbUrl)
7 console.warn(
8 "Environment variable DB_URL not set. Defaulting to `:memory:`",
9 );
10export const DB_URL = dbUrl ?? ":memory:";
11
12const authToken = process.env.AUTH_TOKEN;
13if (!authToken) {
14 if (DB_URL !== ":memory:" && !DB_URL.startsWith("file:")) {
15 console.warn(
16 `You have not set AUTH_TOKEN but are using a remote database at ${DB_URL}. Please ensure that this is correct.`,
17 );
18 } else {
19 console.log(`No AUTH_TOKEN set. DB_URL is set as ${DB_URL}`);
20 }
21}
22export const AUTH_TOKEN = authToken;
23
24const encPassphrase = process.env.ENC_PASSPHRASE;
25if (!encPassphrase)
26 console.warn("No PASSPHRASE set. Contents will NOT be encrypted at rest");
27else console.log("PASSPHRASE is set. Contents will he encrypted at rest");
28export const ENC_PASSPHRASE = encPassphrase;
29
30const serverPort = process.env.SERVER_PORT;
31if (!serverPort)
32 console.warn(
33 "Environment variable SERVER_PORT not set. Defaulting to 7337",
34 );
35export const SERVER_PORT = Number.parseInt(serverPort ?? "7337");
36
37const serviceDid = process.env.SERVICE_DID;
38const {
39 success: serviceDidParseSuccess,
40 error: serviceDidParseError,
41 data: serviceDidParsed,
42} = didSchema.safeParse(serviceDid);
43if (!serviceDidParseSuccess) {
44 console.warn(serviceDidParseError);
45 console.warn(
46 "Environment variable SERVICE_DID not set. Defaulting to `did:web:localhost`",
47 );
48}
49export const SERVICE_DID = serviceDidParsed ?? "did:web:localhost";
50
51const constellationUrl = process.env.CONSTELLATION_URL;
52let constellationUrlParsed: URL;
53try {
54 constellationUrlParsed = new URL(constellationUrl ?? "");
55} catch (err) {
56 console.error(
57 "Invalid CONSTELLATION_URL. Please ensure that the environment variable is a valid URL.",
58 );
59 console.error("https://developer.mozilla.org/en-US/docs/Web/API/URL/URL");
60 // @ts-expect-error we're throwing it anyway so it doesn't really matter what we provide to the error constructor here. it should be a TypeError.
61 throw new Error(err);
62}
63export const CONSTELLATION_URL = constellationUrlParsed;
64
65const ownerDid = process.env.OWNER_DID;
66const {
67 success: ownerDidParseSuccess,
68 error: ownerDidParseError,
69 data: ownerDidParsed,
70} = didSchema.safeParse(ownerDid);
71if (!ownerDidParseSuccess) {
72 console.error(
73 "Could not parse OWNER_DID environment variable. Ensure that it is set and that it is a valid ATProto DID.",
74 );
75 console.error(
76 "See the example environment variables file for more information. `.example.env` in the project root.",
77 );
78 throw new Error(z.prettifyError(ownerDidParseError));
79}
80export const OWNER_DID = ownerDidParsed;
81
82const prismUrl = process.env.PRISM_URL;
83let prismUrlParsed: URL | undefined;
84try {
85 prismUrlParsed = new URL(prismUrl ?? "");
86} catch (err) {
87 console.warn(
88 "Invalid PRISM_URL. Please ensure that the environment variable is a valid URL.",
89 );
90 console.warn("Falling back to default prism instance.");
91 console.warn(err);
92}
93export const PRISM_URL =
94 prismUrlParsed ?? new URL("wss://jetstream.gmstn.systems/subscribe");
95
96const nodeEnv = process.env.NODE_ENV;
97export const NODE_ENV = nodeEnv ?? "development";
98
99export const isDev = NODE_ENV === "development";
100export const __DEV__ = isDev;