@cloudflare/workers-utils 0.35.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.d.mts +1 -1
- package/dist/browser.mjs +1 -1
- package/dist/{chunk-6VP24PQU.mjs → chunk-CDB2RWUW.mjs} +1 -1
- package/dist/{compliance-j3-tqeao.d.mts → compliance-b1X-Ss7e.d.mts} +24 -1
- package/dist/compliance.d.mts +1 -1
- package/dist/index.d.mts +18 -3
- package/dist/index.mjs +1307 -1031
- package/dist/local-env.d.mts +54 -0
- package/dist/local-env.mjs +176 -0
- package/dist/metafile-esm.json +1 -1
- package/dist/test-helpers/index.d.mts +1 -1
- package/package.json +9 -3
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
type LocalEnvValueSource = {
|
|
2
|
+
type: "file";
|
|
3
|
+
path: string;
|
|
4
|
+
} | {
|
|
5
|
+
type: "process";
|
|
6
|
+
};
|
|
7
|
+
interface LoadedEnv {
|
|
8
|
+
values: Record<string, string>;
|
|
9
|
+
sources: Record<string, LocalEnvValueSource>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Returns the local `.env` candidates in increasing precedence order.
|
|
13
|
+
*
|
|
14
|
+
* @param envDir The resolved environment directory. `false` disables file
|
|
15
|
+
* loading, matching Vite's `envDir` convention.
|
|
16
|
+
* @param mode The optional mode used for mode-specific files.
|
|
17
|
+
* @returns Absolute candidate paths in load order.
|
|
18
|
+
*/
|
|
19
|
+
declare function getEnvPaths(envDir: string | false, mode?: string): string[];
|
|
20
|
+
/**
|
|
21
|
+
* Returns the `.dev.vars` candidates in decreasing selection priority.
|
|
22
|
+
*
|
|
23
|
+
* @param envDir The resolved environment directory. `false` disables file
|
|
24
|
+
* loading, matching Vite's `envDir` convention.
|
|
25
|
+
* @param mode The optional mode used to select `.dev.vars.<mode>`.
|
|
26
|
+
* @returns Absolute candidate paths in selection order.
|
|
27
|
+
*/
|
|
28
|
+
declare function getDevVarsCandidatePaths(envDir: string | false, mode?: string): string[];
|
|
29
|
+
/**
|
|
30
|
+
* Loads local `.env` files using Vite-compatible file precedence and
|
|
31
|
+
* Wrangler-compatible parsing and expansion.
|
|
32
|
+
*
|
|
33
|
+
* Expansion is progressive: references can use process values and values
|
|
34
|
+
* declared earlier in the merged environment, but not values declared later.
|
|
35
|
+
* Loading has no process-global side effects.
|
|
36
|
+
*
|
|
37
|
+
* @param envDir The resolved environment directory. `false` disables file
|
|
38
|
+
* loading, matching Vite's `envDir` convention.
|
|
39
|
+
* @param mode The optional mode used for mode-specific files.
|
|
40
|
+
* @returns Resolved values and metadata describing their sources.
|
|
41
|
+
*/
|
|
42
|
+
declare function loadEnv(envDir: string | false, mode?: string): Promise<LoadedEnv>;
|
|
43
|
+
/**
|
|
44
|
+
* Loads the first existing `.dev.vars` candidate without merging it with other
|
|
45
|
+
* files or process-environment values.
|
|
46
|
+
*
|
|
47
|
+
* @param envDir The resolved environment directory. `false` disables file
|
|
48
|
+
* loading, matching Vite's `envDir` convention.
|
|
49
|
+
* @param mode The optional mode used to select `.dev.vars.<mode>`.
|
|
50
|
+
* @returns The selected `.dev.vars` values, or `undefined` when no candidate exists.
|
|
51
|
+
*/
|
|
52
|
+
declare function loadDevVars(envDir: string | false, mode?: string): Promise<Record<string, string> | undefined>;
|
|
53
|
+
|
|
54
|
+
export { type LoadedEnv, type LocalEnvValueSource, getDevVarsCandidatePaths, getEnvPaths, loadDevVars, loadEnv };
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { __commonJS, __name, __toESM } from './chunk-Q72B4Q5Z.mjs';
|
|
2
|
+
import * as fs from 'node:fs/promises';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { parse } from 'dotenv';
|
|
5
|
+
|
|
6
|
+
// ../../node_modules/.pnpm/dotenv-expand@13.0.0/node_modules/dotenv-expand/lib/main.js
|
|
7
|
+
var require_main = __commonJS({
|
|
8
|
+
"../../node_modules/.pnpm/dotenv-expand@13.0.0/node_modules/dotenv-expand/lib/main.js"(exports$1, module) {
|
|
9
|
+
function _resolveEscapeSequences(value) {
|
|
10
|
+
return value.replace(/\\\$/g, "$");
|
|
11
|
+
}
|
|
12
|
+
__name(_resolveEscapeSequences, "_resolveEscapeSequences");
|
|
13
|
+
function expandValue(value, processEnv, runningParsed) {
|
|
14
|
+
const env = { ...runningParsed, ...processEnv };
|
|
15
|
+
const regex = /(?<!\\)\${([^{}]+)}|(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
16
|
+
let result = value;
|
|
17
|
+
let match;
|
|
18
|
+
const seen = /* @__PURE__ */ new Set();
|
|
19
|
+
while ((match = regex.exec(result)) !== null) {
|
|
20
|
+
seen.add(result);
|
|
21
|
+
const [template, bracedExpression, unbracedExpression] = match;
|
|
22
|
+
const expression = bracedExpression || unbracedExpression;
|
|
23
|
+
const opRegex = /(:\+|\+|:-|-)/;
|
|
24
|
+
const opMatch = expression.match(opRegex);
|
|
25
|
+
const splitter = opMatch ? opMatch[0] : null;
|
|
26
|
+
const r = expression.split(splitter);
|
|
27
|
+
let defaultValue;
|
|
28
|
+
let value2;
|
|
29
|
+
const key = r.shift();
|
|
30
|
+
if ([":+", "+"].includes(splitter)) {
|
|
31
|
+
defaultValue = env[key] ? r.join(splitter) : "";
|
|
32
|
+
value2 = null;
|
|
33
|
+
} else {
|
|
34
|
+
defaultValue = r.join(splitter);
|
|
35
|
+
value2 = env[key];
|
|
36
|
+
}
|
|
37
|
+
if (value2) {
|
|
38
|
+
if (seen.has(value2)) {
|
|
39
|
+
result = result.replace(template, defaultValue);
|
|
40
|
+
} else {
|
|
41
|
+
result = result.replace(template, value2);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
result = result.replace(template, defaultValue);
|
|
45
|
+
}
|
|
46
|
+
if (result === runningParsed[key]) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
regex.lastIndex = 0;
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
__name(expandValue, "expandValue");
|
|
54
|
+
function expand2(options) {
|
|
55
|
+
const runningParsed = {};
|
|
56
|
+
let processEnv = process.env;
|
|
57
|
+
if (options && options.processEnv != null) {
|
|
58
|
+
processEnv = options.processEnv;
|
|
59
|
+
}
|
|
60
|
+
for (const key in options.parsed) {
|
|
61
|
+
let value = options.parsed[key];
|
|
62
|
+
if (processEnv[key] && processEnv[key] !== value) {
|
|
63
|
+
value = processEnv[key];
|
|
64
|
+
} else {
|
|
65
|
+
value = expandValue(value, processEnv, runningParsed);
|
|
66
|
+
}
|
|
67
|
+
options.parsed[key] = _resolveEscapeSequences(value);
|
|
68
|
+
runningParsed[key] = _resolveEscapeSequences(value);
|
|
69
|
+
}
|
|
70
|
+
for (const processKey in options.parsed) {
|
|
71
|
+
processEnv[processKey] = options.parsed[processKey];
|
|
72
|
+
}
|
|
73
|
+
return options;
|
|
74
|
+
}
|
|
75
|
+
__name(expand2, "expand");
|
|
76
|
+
module.exports.expand = expand2;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// src/local-env.ts
|
|
81
|
+
var import_dotenv_expand = __toESM(require_main());
|
|
82
|
+
function getEnvPaths(envDir, mode) {
|
|
83
|
+
if (envDir === false) {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
const filenames = [
|
|
87
|
+
".env",
|
|
88
|
+
".env.local",
|
|
89
|
+
...mode === void 0 ? [] : [`.env.${mode}`, `.env.${mode}.local`]
|
|
90
|
+
];
|
|
91
|
+
return filenames.map((filename) => path.resolve(envDir, filename));
|
|
92
|
+
}
|
|
93
|
+
__name(getEnvPaths, "getEnvPaths");
|
|
94
|
+
function getDevVarsCandidatePaths(envDir, mode) {
|
|
95
|
+
if (envDir === false) {
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
return [
|
|
99
|
+
...mode === void 0 ? [] : [`.dev.vars.${mode}`],
|
|
100
|
+
".dev.vars"
|
|
101
|
+
].map((filename) => path.resolve(envDir, filename));
|
|
102
|
+
}
|
|
103
|
+
__name(getDevVarsCandidatePaths, "getDevVarsCandidatePaths");
|
|
104
|
+
async function loadEnv(envDir, mode) {
|
|
105
|
+
if (mode === "local") {
|
|
106
|
+
throw new Error(
|
|
107
|
+
'"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.'
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
const candidateFiles = getEnvPaths(envDir, mode);
|
|
111
|
+
const parsed = {};
|
|
112
|
+
const parsedSources = {};
|
|
113
|
+
for (const candidate of candidateFiles) {
|
|
114
|
+
const loaded = await tryParseFile(candidate, parse);
|
|
115
|
+
if (loaded === void 0) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
for (const [key, value] of Object.entries(loaded.values)) {
|
|
119
|
+
parsed[key] = value;
|
|
120
|
+
parsedSources[key] = { type: "file", path: loaded.path };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const processEnv = copyProcessEnv();
|
|
124
|
+
(0, import_dotenv_expand.expand)({ parsed, processEnv: { ...processEnv } });
|
|
125
|
+
const values = { ...parsed };
|
|
126
|
+
const sources = { ...parsedSources };
|
|
127
|
+
for (const [key, value] of Object.entries(processEnv)) {
|
|
128
|
+
values[key] = value;
|
|
129
|
+
sources[key] = { type: "process" };
|
|
130
|
+
}
|
|
131
|
+
return { values, sources };
|
|
132
|
+
}
|
|
133
|
+
__name(loadEnv, "loadEnv");
|
|
134
|
+
async function loadDevVars(envDir, mode) {
|
|
135
|
+
const candidateFiles = getDevVarsCandidatePaths(envDir, mode);
|
|
136
|
+
for (const candidate of candidateFiles) {
|
|
137
|
+
const loaded = await tryParseFile(candidate, parse);
|
|
138
|
+
if (loaded === void 0) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
return loaded.values;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
__name(loadDevVars, "loadDevVars");
|
|
145
|
+
async function tryParseFile(filePath, parse) {
|
|
146
|
+
try {
|
|
147
|
+
const stat2 = await fs.stat(filePath);
|
|
148
|
+
if (!stat2.isFile() && !stat2.isFIFO()) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
path: filePath,
|
|
153
|
+
values: parse(await fs.readFile(filePath, "utf8"))
|
|
154
|
+
};
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (isMissingFileError(error)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
__name(tryParseFile, "tryParseFile");
|
|
163
|
+
function isMissingFileError(error) {
|
|
164
|
+
return error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR");
|
|
165
|
+
}
|
|
166
|
+
__name(isMissingFileError, "isMissingFileError");
|
|
167
|
+
function copyProcessEnv() {
|
|
168
|
+
return Object.fromEntries(
|
|
169
|
+
Object.entries(process.env).filter(
|
|
170
|
+
(entry) => entry[1] !== void 0
|
|
171
|
+
)
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
__name(copyProcessEnv, "copyProcessEnv");
|
|
175
|
+
|
|
176
|
+
export { getDevVarsCandidatePaths, getEnvPaths, loadDevVars, loadEnv };
|