server.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. HIDE_BALANCE_QUERY?: string; // allow user to query balance or not
  15. }
  16. }
  17. }
  18. const ACCESS_CODES = (function getAccessCodes(): Set<string> {
  19. const code = process.env.CODE;
  20. try {
  21. const codes = (code?.split(",") ?? [])
  22. .filter((v) => !!v)
  23. .map((v) => md5.hash(v.trim()));
  24. return new Set(codes);
  25. } catch (e) {
  26. return new Set();
  27. }
  28. })();
  29. export const getServerSideConfig = () => {
  30. if (typeof process === "undefined") {
  31. throw Error(
  32. "[Server Config] you are importing a nodejs-only module outside of nodejs",
  33. );
  34. }
  35. return {
  36. apiKey: process.env.OPENAI_API_KEY,
  37. code: process.env.CODE,
  38. codes: ACCESS_CODES,
  39. needCode: ACCESS_CODES.size > 0,
  40. baseUrl: process.env.BASE_URL,
  41. proxyUrl: process.env.PROXY_URL,
  42. isVercel: !!process.env.VERCEL,
  43. hideUserApiKey: !!process.env.HIDE_USER_API_KEY,
  44. disableGPT4: !!process.env.DISABLE_GPT4,
  45. hideBalanceQuery: !!process.env.HIDE_BALANCE_QUERY,
  46. };
  47. };