server.ts 1.1 KB

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