server.ts 1.2 KB

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