Skip to content

Commit

Permalink
feat(node:process): allow accessing process.env from dynamic sources (
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 16, 2023
1 parent a62896c commit c64037d
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/runtime/node/process/_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c64037d

Please sign in to comment.