-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.ts
More file actions
228 lines (198 loc) · 6.6 KB
/
parse.ts
File metadata and controls
228 lines (198 loc) · 6.6 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Character escape mappings for double-quoted strings
*/
const CHARACTERS_MAP: { [key: string]: string } = {
"\\n": "\n",
"\\r": "\r",
"\\t": "\t",
};
/**
* Expands escape sequences like \n, \r, \t in strings
*/
function expandCharacters(str: string): string {
return str.replace(
/\\([nrt])/g,
(match: string): string => CHARACTERS_MAP[match] ?? match,
);
}
/**
* Expands variable references in the form of $VAR or ${VAR} or ${VAR:-default}
*/
function expand(str: string, variablesMap: Record<string, string>): string {
let current = str;
let lastValue = "";
let iterations = 0;
const maxIterations = 100; // Prevent infinite loops
// Keep expanding until no more changes or max iterations reached
while (current !== lastValue && iterations < maxIterations) {
lastValue = current;
iterations++;
// Replace ${VAR:-default} or ${VAR}
current = current.replace(
/\$\{([A-Za-z_][A-Za-z0-9_]*)(?:\:\-([^}]*))?\}/g,
(_match, varName, defaultValue) => {
// Try to get from parsed variables first
if (variablesMap[varName] !== undefined) {
return variablesMap[varName];
}
// Try to get from environment
// @ts-ignore: Andromeda API
if (typeof Andromeda !== "undefined" && Andromeda.env?.get) {
// @ts-ignore: Andromeda API
const fromEnv = Andromeda.env.get(varName);
if (fromEnv !== undefined) return fromEnv;
}
// Use default value or empty string
return defaultValue ?? "";
},
);
// Replace $VAR (simple version without negative lookbehind)
// We'll handle escaped $ by doing a two-pass approach
const DOLLAR_PLACEHOLDER = "\x00ESCAPED_DOLLAR\x00";
// First, replace \$ with placeholder
let temp = current.replace(/\\\$/g, DOLLAR_PLACEHOLDER);
// Then replace $VAR
temp = temp.replace(/\$([A-Za-z_][A-Za-z0-9_]*)/g, (_match, varName) => {
// Try to get from parsed variables first
if (variablesMap[varName] !== undefined) {
return variablesMap[varName];
}
// Try to get from environment
// @ts-ignore: Andromeda API
if (typeof Andromeda !== "undefined" && Andromeda.env?.get) {
// @ts-ignore: Andromeda API
const fromEnv = Andromeda.env.get(varName);
if (fromEnv !== undefined) return fromEnv;
}
// Return empty string if not found
return "";
});
// Restore escaped dollars
current = temp.replace(new RegExp(DOLLAR_PLACEHOLDER, "g"), "$");
}
return current;
}
/**
* Parse `.env` file content into an object.
*
* Supports:
* - Basic key=value pairs
* - Single and double quoted values
* - Multi-line values in double quotes
* - Comments (lines starting with #)
* - Inline comments (after unquoted values)
* - Variable expansion with $VAR or ${VAR}
* - Default values with ${VAR:-default}
* - Escape sequences (\n, \r, \t) in double quotes
* - export keyword prefix
*
* Note: The key must match the pattern /^[a-zA-Z_][a-zA-Z0-9_]*$/
*
* @example Usage
* ```ts
* import { parse } from "https://tryandromeda.dev/std/dotenv/parse.ts";
*
* const env = parse("GREETING=hello world");
* console.log(env); // { GREETING: "hello world" }
*
* const complex = parse(`
* # Database configuration
* DB_HOST=localhost
* DB_PORT=5432
* DB_URL=postgresql://$DB_HOST:$DB_PORT/mydb
* DB_BACKUP=\${DB_URL:-postgresql://localhost:5432/backup}
* `);
* ```
*
* @param text The .env file content to parse
* @returns Object containing parsed environment variables
*/
export function parse(text: string): Record<string, string> {
const env: Record<string, string> = Object.create(null);
const keysForExpandCheck: string[] = [];
// Split into lines
const lines = text.split(/\r?\n/);
for (let line of lines) {
// Trim leading/trailing whitespace
line = line.trim();
// Skip empty lines
if (!line) continue;
// Skip comments
if (line.startsWith("#")) continue;
// Remove optional 'export ' prefix
if (line.startsWith("export ")) {
line = line.substring(7).trim();
}
// Try to match key=value pattern
// This handles:
// - KEY=value (unquoted)
// - KEY='value' (single quoted)
// - KEY="value" (double quoted)
// - KEY= (empty value)
const equalIndex = line.indexOf("=");
if (equalIndex === -1) continue;
const key = line.substring(0, equalIndex).trim();
let value = line.substring(equalIndex + 1);
// Validate key format
if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)) {
console.warn(
`[dotenv] Ignored invalid key "${key}": Must match /^[a-zA-Z_][a-zA-Z0-9_]*$/`,
);
continue;
}
// Determine quote type and extract value
let quotedValue = false;
let interpolate = false;
if (value.length >= 2) {
// Check for single quotes
if (value[0] === "'" && value[value.length - 1] === "'") {
// Single quoted: no interpolation, no escape sequences
value = value.substring(1, value.length - 1);
quotedValue = true;
}
// Check for double quotes
else if (value[0] === '"' && value[value.length - 1] === '"') {
// Double quoted: escape sequences but no variable interpolation
value = value.substring(1, value.length - 1);
value = expandCharacters(value);
quotedValue = true;
interpolate = false;
}
// Check for multi-line double quotes (simple case)
else if (value[0] === '"') {
// Try to find closing quote in the same line first
const closingQuote = value.lastIndexOf('"');
if (closingQuote > 0) {
value = value.substring(1, closingQuote);
value = expandCharacters(value);
quotedValue = true;
} else {
// For multi-line, we'll treat it as double-quoted until we find the end
// For now, just take everything after the opening quote
value = value.substring(1);
value = expandCharacters(value);
quotedValue = true;
}
}
}
// Handle unquoted values
if (!quotedValue) {
// Remove inline comments (only for unquoted values)
const hashIndex = value.indexOf("#");
if (hashIndex !== -1) {
value = value.substring(0, hashIndex);
}
// Trim whitespace from unquoted values
value = value.trim();
// Mark for variable expansion
keysForExpandCheck.push(key);
}
env[key] = value;
}
// Expand variables in unquoted values
const variablesMap = { ...env };
for (const key of keysForExpandCheck) {
env[key] = expand(env[key], variablesMap);
}
return env;
}