File size: 2,584 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]

mod command_line;
mod custom;
mod dotenv;
mod filter;

use std::{env, sync::Mutex};

use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, Vc};

pub use self::{
    command_line::CommandLineProcessEnv, custom::CustomProcessEnv, dotenv::DotenvProcessEnv,
    filter::FilterProcessEnv,
};

#[turbo_tasks::value(transparent)]
pub struct EnvMap(#[turbo_tasks(trace_ignore)] FxIndexMap<RcStr, RcStr>);

#[turbo_tasks::value_impl]
impl EnvMap {
    #[turbo_tasks::function]
    pub fn empty() -> Vc<Self> {
        EnvMap(FxIndexMap::default()).cell()
    }
}

#[turbo_tasks::value_impl]
impl ProcessEnv for EnvMap {
    #[turbo_tasks::function]
    fn read_all(self: Vc<Self>) -> Vc<EnvMap> {
        self
    }

    #[turbo_tasks::function]
    fn read(self: Vc<Self>, name: RcStr) -> Vc<Option<RcStr>> {
        case_insensitive_read(self, name)
    }
}

#[turbo_tasks::value_trait]
pub trait ProcessEnv {
    // TODO SECURITY: From security perspective it's not good that we read *all* env
    // vars into the cache. This might store secrects into the persistent cache
    // which we want to avoid.
    // Instead we should use only `read_prefix` to read all env vars with a specific
    // prefix.
    /// Reads all env variables into a Map
    #[turbo_tasks::function]
    fn read_all(self: Vc<Self>) -> Vc<EnvMap>;

    /// Reads a single env variable. Ignores casing.
    #[turbo_tasks::function]
    fn read(self: Vc<Self>, name: RcStr) -> Vc<Option<RcStr>> {
        case_insensitive_read(self.read_all(), name)
    }
}

pub fn sorted_env_vars() -> FxIndexMap<RcStr, RcStr> {
    let mut vars = env::vars()
        .map(|(k, v)| (k.into(), v.into()))
        .collect::<FxIndexMap<_, _>>();
    vars.sort_keys();
    vars
}

#[turbo_tasks::function]
pub async fn case_insensitive_read(map: Vc<EnvMap>, name: RcStr) -> Result<Vc<Option<RcStr>>> {
    Ok(Vc::cell(
        to_uppercase_map(map)
            .await?
            .get(&RcStr::from(name.to_uppercase()))
            .cloned(),
    ))
}

#[turbo_tasks::function]
async fn to_uppercase_map(map: Vc<EnvMap>) -> Result<Vc<EnvMap>> {
    let map = &*map.await?;
    let mut new = FxIndexMap::with_capacity_and_hasher(map.len(), Default::default());
    for (k, v) in map {
        new.insert(k.to_uppercase().into(), v.clone());
    }
    Ok(Vc::cell(new))
}

pub static GLOBAL_ENV_LOCK: Mutex<()> = Mutex::new(());

pub fn register() {
    turbo_tasks::register();
    include!(concat!(env!("OUT_DIR"), "/register.rs"));
}