server.ts 1.0 KB

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