server.ts 914 B

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