sailfork / t3code-github / .repos/ alchemy-effect/ packages/ cloudflare-runtime/ src/ vite/ module-runner/ env.worker.ts

import { ENTRY_ENVIRONMENT_KEY, type EntryEnvironment } from "./constants.shared.ts";
import type { ModuleRunnerDO } from "./module-runner.worker.ts";

export interface Env extends Cloudflare.Env {
  __DISTILLED_MODULE_RUNNER__: {
    get(id: "singleton"): ModuleRunnerDO;
  };
  __DISTILLED_UNSAFE_EVAL__: {
    eval: (code: string, id: string) => Function;
  };
  __DISTILLED_INVOKE_MODULE__: Fetcher;
  __DISTILLED_MODULE_FALLBACK__: Fetcher;
  [key: string]: unknown;
}

export function stripInternalEnv(env: Env) {
  const {
    __DISTILLED_MODULE_RUNNER__,
    __DISTILLED_UNSAFE_EVAL__,
    __DISTILLED_INVOKE_MODULE__,
    __DISTILLED_MODULE_FALLBACK__,
    ...userEnv
  } = env;
  return userEnv;
}

export function getEntryEnvironment(): EntryEnvironment {
  return (globalThis as Record<symbol, EntryEnvironment>)[Symbol.for(ENTRY_ENVIRONMENT_KEY)]!;
}