server.ts 1.3 KB

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