server.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import md5 from "spark-md5";
  2. declare global {
  3. namespace NodeJS {
  4. interface ProcessEnv {
  5. OPENAI_API_KEY?: string;
  6. CODE?: string;
  7. BASE_URL?: string;
  8. PROXY_URL?: string;
  9. VERCEL?: string;
  10. HIDE_USER_API_KEY?: string; // disable user's api key input
  11. DISABLE_GPT4?: string; // allow user to use gpt-4 or not
  12. BUILD_MODE?: "standalone" | "export";
  13. BUILD_APP?: string; // is building desktop app
  14. ENABLE_BALANCE_QUERY?: string; // allow user to query balance or not
  15. DISABLE_FAST_LINK?: string; // disallow parse settings from url or not
  16. }
  17. }
  18. }
  19. const ACCESS_CODES = (function getAccessCodes(): Set<string> {
  20. const code = process.env.CODE;
  21. try {
  22. const codes = (code?.split(",") ?? [])
  23. .filter((v) => !!v)
  24. .map((v) => md5.hash(v.trim()));
  25. return new Set(codes);
  26. } catch (e) {
  27. return new Set();
  28. }
  29. })();
  30. export const getServerSideConfig = () => {
  31. if (typeof process === "undefined") {
  32. throw Error(
  33. "[Server Config] you are importing a nodejs-only module outside of nodejs",
  34. );
  35. }
  36. return {
  37. apiKey: process.env.OPENAI_API_KEY,
  38. code: process.env.CODE,
  39. codes: ACCESS_CODES,
  40. needCode: ACCESS_CODES.size > 0,
  41. baseUrl: process.env.BASE_URL,
  42. proxyUrl: process.env.PROXY_URL,
  43. isVercel: !!process.env.VERCEL,
  44. hideUserApiKey: !!process.env.HIDE_USER_API_KEY,
  45. disableGPT4: !!process.env.DISABLE_GPT4,
  46. hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
  47. disableFastLink: !!process.env.DISABLE_FAST_LINK,
  48. };
  49. };