-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
179 lines (162 loc) · 5.05 KB
/
proxy.ts
File metadata and controls
179 lines (162 loc) · 5.05 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @file `createEnvProxy` — wrap `process.env` (or any env-like record) in a
* Proxy that adds case-insensitive lookups for known-Windows-sensitive keys
* (PATH, APPDATA, etc.) and an `overrides` layer. Intended for cross-platform
* test harnesses and child-process spawn env normalization.
*/
import { ProxyCtor } from '../primordials/globals'
import { SetCtor } from '../primordials/map-set'
import { ObjectKeys } from '../primordials/object'
import { findCaseInsensitiveEnvKey } from './case-insensitive'
// Common environment variables that have case sensitivity issues on Windows.
// These are checked with case-insensitive matching when exact matches fail.
const caseInsensitiveKeys = new SetCtor([
'APPDATA',
'COMSPEC',
'HOME',
'LOCALAPPDATA',
'PATH',
'PATHEXT',
'PROGRAMFILES',
'SYSTEMROOT',
'TEMP',
'TMP',
'USERPROFILE',
'WINDIR',
])
/**
* Create a case-insensitive environment variable Proxy for Windows
* compatibility. On Windows, environment variables are case-insensitive (PATH
* vs Path vs path). This Proxy provides consistent access regardless of case,
* with priority given to exact matches, then case-insensitive matches for known
* vars.
*
* **Use Cases:**
*
* - Cross-platform test environments needing consistent env var access
* - Windows compatibility when passing env to child processes
* - Merging environment overrides while preserving case-insensitive lookups
*
* **Performance Note:** Proxy operations have runtime overhead. Only use when
* Windows case-insensitive access is required. For most use cases, process.env
* directly is sufficient.
*
* @example
* // Create a Proxy with overrides
* const env = createEnvProxy(process.env, { NODE_ENV: 'test' })
* console.log(env.PATH) // Works with any case: PATH, Path, path
* console.log(env.NODE_ENV) // 'test'
*
* @example
* // Pass to child process spawn
* import { createEnvProxy } from '@socketsecurity/lib/env/proxy'
* import { spawn } from '@socketsecurity/lib/spawn'
*
* spawn('node', ['script.js'], {
* env: createEnvProxy(process.env, { NODE_ENV: 'test' }),
* })
*
* @param base - Base environment object (usually process.env)
* @param overrides - Optional overrides to merge.
*
* @returns Proxy that handles case-insensitive env var access
*/
export function createEnvProxy(
base: NodeJS.ProcessEnv,
overrides?: Record<string, string | undefined>,
): NodeJS.ProcessEnv {
function lookupEnvValue(prop: string): string | undefined {
// Priority 1: Check overrides for exact match.
if (overrides && prop in overrides) {
return overrides[prop]
}
// Priority 2: Check base for exact match.
if (prop in base) {
return base[prop]
}
// Priority 3: Case-insensitive lookup for known keys. Tests
// exercise direct lookups; case-insensitive variants fire only
// when caller queries with mixed case.
/* c8 ignore start */
const upperProp = prop.toUpperCase()
if (caseInsensitiveKeys.has(upperProp)) {
if (overrides) {
const key = findCaseInsensitiveEnvKey(overrides, upperProp)
if (key !== undefined) {
return overrides[key]
}
}
const key = findCaseInsensitiveEnvKey(base, upperProp)
if (key !== undefined) {
return base[key]
}
}
return undefined
}
/* c8 ignore stop */
return new ProxyCtor({} as NodeJS.ProcessEnv, {
get(_target, prop) {
if (typeof prop !== 'string') {
return undefined
}
return lookupEnvValue(prop)
},
ownKeys(_target) {
const keys = new Set<string>([
...ObjectKeys(base),
...(overrides ? ObjectKeys(overrides) : []),
])
return [...keys]
},
getOwnPropertyDescriptor(_target, prop) {
if (typeof prop !== 'string') {
return undefined
}
// Use the same lookup logic as get().
const value = lookupEnvValue(prop)
return value !== undefined
? {
enumerable: true,
configurable: true,
writable: true,
value,
}
: undefined
},
has(_target, prop) {
// typeof guard, overrides existence, and case-insensitive sub-arms
// all defensive; tests check direct presence of known keys.
/* c8 ignore start */
if (typeof prop !== 'string') {
return false
}
if (overrides && prop in overrides) {
return true
}
if (prop in base) {
return true
}
const upperProp = prop.toUpperCase()
if (caseInsensitiveKeys.has(upperProp)) {
if (
overrides &&
findCaseInsensitiveEnvKey(overrides, upperProp) !== undefined
) {
return true
}
if (findCaseInsensitiveEnvKey(base, upperProp) !== undefined) {
return true
}
}
return false
/* c8 ignore stop */
},
set(_target, prop, value) {
if (typeof prop === 'string' && overrides) {
overrides[prop] = value
return true
}
return false
},
}) as NodeJS.ProcessEnv
}