Code Monkey home page Code Monkey logo

Comments (6)

peterreisz avatar peterreisz commented on July 21, 2024 1

Haven't tried, but something like this should work:

diff --git a/src/internal/configProvider.ts b/src/internal/configProvider.ts
index 44af3ae58..6de804e54 100644
--- a/src/internal/configProvider.ts
+++ b/src/internal/configProvider.ts
@@ -95,6 +95,37 @@ export const fromFlat = (flat: ConfigProvider.ConfigProvider.Flat): ConfigProvid
     flattened: flat
   })
 
+interface Environment {
+  getVariable(name: string): string | undefined;
+  getAllVariables(): { [key: string]: string | undefined; }
+}
+
+interface DenoRuntime {
+  Deno: {
+    env: {
+      get(key: string): string | undefined;
+      toObject(): { [index: string]: string; };
+    }
+  }
+}
+
+const isDenoRuntime = (runtime: {}): runtime is DenoRuntime => 'Deno' in runtime
+
+const getEnvironment = (): Environment => {
+  const runtime = globalThis;
+  if (isDenoRuntime(runtime)) {
+    return {
+      getVariable: (name) => runtime.Deno.env.get(name),
+      getAllVariables: () => runtime.Deno.env.toObject(),
+    }
+  }
+
+  return {
+    getVariable: (name) => process.env[name],
+    getAllVariables: () => process.env,
+  }
+}
+
 /** @internal */
 export const fromEnv = (
   config: Partial<ConfigProvider.ConfigProvider.FromEnvConfig> = {}
@@ -103,17 +134,14 @@ export const fromEnv = (
   const makePathString = (path: ReadonlyArray<string>): string => pipe(path, RA.join(pathDelim))
   const unmakePathString = (pathString: string): ReadonlyArray<string> => pathString.split(pathDelim)
 
-  const getEnv = () =>
-    typeof process !== "undefined" && "env" in process && typeof process.env === "object" ? process.env : {}
-
   const load = <A>(
     path: ReadonlyArray<string>,
     primitive: Config.Config.Primitive<A>,
     split = true
   ): Effect.Effect<never, ConfigError.ConfigError, ReadonlyArray<A>> => {
     const pathString = makePathString(path)
-    const current = getEnv()
-    const valueOpt = pathString in current ? Option.some(current[pathString]!) : Option.none()
+    const current = getEnvironment()
+    const valueOpt = Option.fromNullable(current.getVariable(pathString)) // this is doing a different thing, but otherwise you have to grant access to all env in Deno
     return pipe(
       valueOpt,
       core.mapError(() => configError.MissingData(path, `Expected ${pathString} to exist in the process context`)),
@@ -125,8 +153,8 @@ export const fromEnv = (
     path: ReadonlyArray<string>
   ): Effect.Effect<never, ConfigError.ConfigError, HashSet.HashSet<string>> =>
     core.sync(() => {
-      const current = getEnv()
-      const keys = Object.keys(current)
+      const current = getEnvironment()
+      const keys = Object.keys(current.getAllVariables())
       const keyPaths = Array.from(keys).map((value) => unmakePathString(value.toUpperCase()))
       const filteredKeyPaths = keyPaths.filter((keyPath) => {
         for (let i = 0; i < path.length; i++) {

from effect.

peterreisz avatar peterreisz commented on July 21, 2024 1

I'm glad you are open to support Deno. I'll submit a PR then, just have to figure out some things. For example how to test the project with Deno.

from effect.

mikearnaldi avatar mikearnaldi commented on July 21, 2024 1

cc @fubhy who's proposing a multi-runtime test matrix

from effect.

mikearnaldi avatar mikearnaldi commented on July 21, 2024

How would supporting Deno look like?

from effect.

mikearnaldi avatar mikearnaldi commented on July 21, 2024

If you want to open a PR I'd be happy to review & merge

from effect.

fubhy avatar fubhy commented on July 21, 2024

I can add a PR for the test matrix later today.

from effect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.