From c64037d94c20e56e81530fbe9c13b93bb2bd6593 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Sun, 16 Apr 2023 15:38:34 +0200 Subject: [PATCH] feat(node:process): allow accessing `process.env` from dynamic sources (#95) --- src/runtime/node/process/_process.ts | 32 +++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/runtime/node/process/_process.ts b/src/runtime/node/process/_process.ts index 1cc7483e..bb939fe7 100644 --- a/src/runtime/node/process/_process.ts +++ b/src/runtime/node/process/_process.ts @@ -156,7 +156,37 @@ Item.prototype.run = function () { this.fun.apply(null, this.array); }; process.title = "unenv"; -process.env = globalThis.process?.env || {}; + +const _envShim = Object.create(null); +const _getEnv = (useShim: boolean) => + globalThis.process?.env || + globalThis.__env__ || + (useShim ? _envShim : globalThis); + +process.env = new Proxy(_envShim, { + get(_, prop) { + const env = _getEnv(); + return env[prop] ?? _envShim[prop]; + }, + has(_, prop) { + const env = _getEnv(); + return prop in env || prop in _envShim; + }, + set(_, prop, value) { + const env = _getEnv(true); + env[prop] = value; + return true; + }, + deleteProperty(_, prop) { + const env = _getEnv(true); + delete env[prop]; + }, + ownKeys() { + const env = _getEnv(); + return Object.keys(env); + }, +}); + process.argv = []; // @ts-ignore process.version = ""; // empty string to avoid regexp issues