server.ts 1.2 KB

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