index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // src/helper/adapter/index.ts
  2. var env = (c, runtime) => {
  3. const global = globalThis;
  4. const globalEnv = global?.process?.env;
  5. runtime ??= getRuntimeKey();
  6. const runtimeEnvHandlers = {
  7. bun: () => globalEnv,
  8. node: () => globalEnv,
  9. "edge-light": () => globalEnv,
  10. deno: () => {
  11. return Deno.env.toObject();
  12. },
  13. workerd: () => c.env,
  14. // On Fastly Compute, you can use the ConfigStore to manage user-defined data.
  15. fastly: () => ({}),
  16. other: () => ({})
  17. };
  18. return runtimeEnvHandlers[runtime]();
  19. };
  20. var knownUserAgents = {
  21. deno: "Deno",
  22. bun: "Bun",
  23. workerd: "Cloudflare-Workers",
  24. node: "Node.js"
  25. };
  26. var getRuntimeKey = () => {
  27. const global = globalThis;
  28. const userAgentSupported = typeof navigator !== "undefined" && typeof navigator.userAgent === "string";
  29. if (userAgentSupported) {
  30. for (const [runtimeKey, userAgent] of Object.entries(knownUserAgents)) {
  31. if (checkUserAgentEquals(userAgent)) {
  32. return runtimeKey;
  33. }
  34. }
  35. }
  36. if (typeof global?.EdgeRuntime === "string") {
  37. return "edge-light";
  38. }
  39. if (global?.fastly !== void 0) {
  40. return "fastly";
  41. }
  42. if (global?.process?.release?.name === "node") {
  43. return "node";
  44. }
  45. return "other";
  46. };
  47. var checkUserAgentEquals = (platform) => {
  48. const userAgent = navigator.userAgent;
  49. return userAgent.startsWith(platform);
  50. };
  51. export {
  52. checkUserAgentEquals,
  53. env,
  54. getRuntimeKey,
  55. knownUserAgents
  56. };