devflare 1.0.0-next.21 → 1.0.0-next.23
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/LLM.md +144 -5
- package/dist/account-j8nfggg4.js +475 -0
- package/dist/account-qhe8vtds.js +475 -0
- package/dist/bridge/gateway-runtime.d.ts +1 -1
- package/dist/bridge/gateway-runtime.d.ts.map +1 -1
- package/dist/bridge/miniflare.d.ts +1 -1
- package/dist/bridge/miniflare.d.ts.map +1 -1
- package/dist/bridge/proxy.d.ts +2 -0
- package/dist/bridge/proxy.d.ts.map +1 -1
- package/dist/bridge/server.d.ts.map +1 -1
- package/dist/browser.d.ts +13 -13
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +5 -3
- package/dist/build-qsgnme4z.js +54 -0
- package/dist/build-vy95gy3f.js +54 -0
- package/dist/build-yzx0gsaj.js +54 -0
- package/dist/cli/commands/build-artifacts.d.ts.map +1 -1
- package/dist/cli/commands/config.d.ts.map +1 -1
- package/dist/cli/commands/type-generation/generator.d.ts +4 -2
- package/dist/cli/commands/type-generation/generator.d.ts.map +1 -1
- package/dist/cli/commands/types.d.ts.map +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/config/compiler/types.d.ts +1 -1
- package/dist/config/compiler/types.d.ts.map +1 -1
- package/dist/config/define.d.ts +7 -4
- package/dist/config/define.d.ts.map +1 -1
- package/dist/config/env-vars.d.ts +309 -0
- package/dist/config/env-vars.d.ts.map +1 -0
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/loader.d.ts.map +1 -1
- package/dist/config/local-dev-vars.d.ts +2 -2
- package/dist/config/local-dev-vars.d.ts.map +1 -1
- package/dist/config/schema-env.d.ts +6 -6
- package/dist/config/schema-types-bindings-platform.d.ts +378 -0
- package/dist/config/schema-types-bindings-platform.d.ts.map +1 -0
- package/dist/config/schema-types-bindings-resources.d.ts +551 -0
- package/dist/config/schema-types-bindings-resources.d.ts.map +1 -0
- package/dist/config/schema-types-bindings.d.ts +254 -0
- package/dist/config/schema-types-bindings.d.ts.map +1 -0
- package/dist/config/schema-types-build.d.ts +86 -0
- package/dist/config/schema-types-build.d.ts.map +1 -0
- package/dist/config/schema-types-runtime.d.ts +882 -0
- package/dist/config/schema-types-runtime.d.ts.map +1 -0
- package/dist/config/schema-types.d.ts +377 -0
- package/dist/config/schema-types.d.ts.map +1 -0
- package/dist/config/schema.d.ts +14 -15
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config-entry.d.ts +2 -0
- package/dist/config-entry.d.ts.map +1 -1
- package/dist/config-entry.js +3 -1
- package/dist/config-gq5jh4cx.js +105 -0
- package/dist/config-vec13050.js +105 -0
- package/dist/deploy-01j0ep5n.js +1055 -0
- package/dist/deploy-nh5tbv45.js +1055 -0
- package/dist/deploy-tjypkhg7.js +1055 -0
- package/dist/dev-bh581ew3.js +2597 -0
- package/dist/dev-cme5de75.js +2551 -0
- package/dist/dev-gn5y93z9.js +2597 -0
- package/dist/dev-server/server.d.ts.map +1 -1
- package/dist/doctor-h5q28qt1.js +259 -0
- package/dist/doctor-khk550tw.js +259 -0
- package/dist/env.d.ts +10 -0
- package/dist/env.d.ts.map +1 -1
- package/dist/index-0bv2qjs1.js +1555 -0
- package/dist/index-35bmgpfw.js +573 -0
- package/dist/index-3tkzn06q.js +413 -0
- package/dist/index-4se6krdj.js +574 -0
- package/dist/index-8fyz6gcm.js +699 -0
- package/dist/index-97z629zr.js +109 -0
- package/dist/index-b28c4yr4.js +1205 -0
- package/dist/index-c1cj9085.js +2250 -0
- package/dist/index-c8p4njqy.js +479 -0
- package/dist/index-cr06zrgw.js +1033 -0
- package/dist/index-cwjjdtgn.js +74 -0
- package/dist/index-dref9ecb.js +476 -0
- package/dist/index-e151t4ge.js +895 -0
- package/dist/index-e7kakw0j.js +1033 -0
- package/dist/index-f1g5jdm8.js +1426 -0
- package/dist/index-f46984zs.js +1554 -0
- package/dist/index-grk8pzhr.js +185 -0
- package/dist/index-hbxkmb1q.js +1426 -0
- package/dist/index-hzmpecq9.js +52 -0
- package/dist/index-j1csb7gb.js +581 -0
- package/dist/index-j7x7f72h.js +185 -0
- package/dist/index-jkqbjwt2.js +476 -0
- package/dist/index-jwd3fanx.js +412 -0
- package/dist/index-mh5renra.js +895 -0
- package/dist/index-p9xq83p7.js +147 -0
- package/dist/index-q15nj71j.js +52 -0
- package/dist/index-qqp65pyv.js +699 -0
- package/dist/index-s0fmwxbk.js +74 -0
- package/dist/index-s9q605sq.js +1033 -0
- package/dist/index-stzx8nc4.js +111 -0
- package/dist/index-th4vrnbk.js +1205 -0
- package/dist/index-vtcmsgaf.js +581 -0
- package/dist/index-w36q6819.js +895 -0
- package/dist/index-x2k3awjs.js +147 -0
- package/dist/index-x8x547tz.js +1426 -0
- package/dist/index-xp0qkkxf.js +68 -0
- package/dist/index-xxxd0mvw.js +109 -0
- package/dist/index-zawn5tte.js +109 -0
- package/dist/index-zpy9caxn.js +1193 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -7
- package/dist/login-280p2cm9.js +77 -0
- package/dist/login-4n266whq.js +77 -0
- package/dist/previews-3m3ffpaw.js +1337 -0
- package/dist/previews-tr8sm03d.js +1337 -0
- package/dist/productions-62y489ff.js +505 -0
- package/dist/productions-cgn3fz7d.js +505 -0
- package/dist/runtime/exports.d.ts +23 -0
- package/dist/runtime/exports.d.ts.map +1 -1
- package/dist/runtime/index.d.ts +1 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +6 -4
- package/dist/secrets-4050kqf5.js +91 -0
- package/dist/secrets-p112cajt.js +91 -0
- package/dist/sveltekit/index.js +8 -7
- package/dist/sveltekit/local-bindings.d.ts.map +1 -1
- package/dist/test/index.js +24 -12
- package/dist/test/resolve-service-bindings.d.ts +1 -1
- package/dist/test/resolve-service-bindings.d.ts.map +1 -1
- package/dist/test/simple-context-lifecycle.d.ts.map +1 -1
- package/dist/types-apmt10yj.js +705 -0
- package/dist/types-ttrrgdfj.js +705 -0
- package/dist/vite/index.js +5 -5
- package/dist/vite/plugin-context.d.ts.map +1 -1
- package/dist/vite/plugin-programmatic.d.ts.map +1 -1
- package/dist/worker-2k1jyr6p.js +513 -0
- package/dist/worker-jqgn6jyj.js +513 -0
- package/package.json +1 -1
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
// src/config/env-vars.ts
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, resolve } from "pathe";
|
|
5
|
+
var ENV_DESCRIPTOR_FLAG = "__devflareEnvDescriptor";
|
|
6
|
+
function createDescriptor(state) {
|
|
7
|
+
const descriptor = {
|
|
8
|
+
[ENV_DESCRIPTOR_FLAG]: true,
|
|
9
|
+
__state: state,
|
|
10
|
+
optional() {
|
|
11
|
+
return createDescriptor({
|
|
12
|
+
...state,
|
|
13
|
+
optional: true
|
|
14
|
+
});
|
|
15
|
+
},
|
|
16
|
+
parse(parser) {
|
|
17
|
+
return createDescriptor({
|
|
18
|
+
...state,
|
|
19
|
+
parser
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
parser(parser) {
|
|
23
|
+
return this.parse(parser);
|
|
24
|
+
},
|
|
25
|
+
default(value) {
|
|
26
|
+
return createDescriptor({
|
|
27
|
+
...state,
|
|
28
|
+
defaultValue: value,
|
|
29
|
+
hasDefault: true,
|
|
30
|
+
optional: false
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
dev(value) {
|
|
34
|
+
return createDescriptor({
|
|
35
|
+
...state,
|
|
36
|
+
devValue: value,
|
|
37
|
+
hasDevDefault: true
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return descriptor;
|
|
42
|
+
}
|
|
43
|
+
function createEnvVarDescriptor(name) {
|
|
44
|
+
return createDescriptor({
|
|
45
|
+
name,
|
|
46
|
+
optional: false,
|
|
47
|
+
hasDefault: false,
|
|
48
|
+
hasDevDefault: false
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
var env = new Proxy({}, {
|
|
52
|
+
get(_target, prop) {
|
|
53
|
+
if (typeof prop !== "string") {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
return createEnvVarDescriptor(prop);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
function isEnvVarDescriptor(value) {
|
|
60
|
+
return Boolean(value && typeof value === "object" && value[ENV_DESCRIPTOR_FLAG] === true);
|
|
61
|
+
}
|
|
62
|
+
function parseEnvValue(rawValue) {
|
|
63
|
+
const trimmed = rawValue.trim();
|
|
64
|
+
const quote = trimmed[0];
|
|
65
|
+
if ((quote === '"' || quote === "'" || quote === "`") && trimmed.endsWith(quote) && trimmed.length >= 2) {
|
|
66
|
+
const inner = trimmed.slice(1, -1);
|
|
67
|
+
if (quote !== '"') {
|
|
68
|
+
return inner;
|
|
69
|
+
}
|
|
70
|
+
return inner.replace(/\\n/g, `
|
|
71
|
+
`).replace(/\\r/g, "\r").replace(/\\t/g, "\t").replace(/\\"/g, '"').replace(/\\\\/g, "\\");
|
|
72
|
+
}
|
|
73
|
+
return trimmed;
|
|
74
|
+
}
|
|
75
|
+
function parseDevflareEnvFile(contents) {
|
|
76
|
+
const values = {};
|
|
77
|
+
for (const line of contents.split(/\r?\n/)) {
|
|
78
|
+
const trimmed = line.trim();
|
|
79
|
+
if (!trimmed || trimmed.startsWith("#")) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const assignment = trimmed.startsWith("export ") ? trimmed.slice("export ".length).trimStart() : trimmed;
|
|
83
|
+
const equalsIndex = assignment.indexOf("=");
|
|
84
|
+
if (equalsIndex <= 0) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const key = assignment.slice(0, equalsIndex).trim();
|
|
88
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
values[key] = parseEnvValue(assignment.slice(equalsIndex + 1));
|
|
92
|
+
}
|
|
93
|
+
return values;
|
|
94
|
+
}
|
|
95
|
+
function collectAncestorDirectories(startDir) {
|
|
96
|
+
const directories = [];
|
|
97
|
+
let current = resolve(startDir);
|
|
98
|
+
while (true) {
|
|
99
|
+
directories.push(current);
|
|
100
|
+
const parent = dirname(current);
|
|
101
|
+
if (parent === current) {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
current = parent;
|
|
105
|
+
}
|
|
106
|
+
return directories.reverse();
|
|
107
|
+
}
|
|
108
|
+
function getDevflareDotenvPaths(startDir) {
|
|
109
|
+
return collectAncestorDirectories(startDir).flatMap((directory) => [
|
|
110
|
+
resolve(directory, ".env.dev"),
|
|
111
|
+
resolve(directory, ".env")
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
114
|
+
async function loadDevflareDotenv(startDir) {
|
|
115
|
+
const values = {};
|
|
116
|
+
const files = [];
|
|
117
|
+
for (const filePath of getDevflareDotenvPaths(startDir)) {
|
|
118
|
+
if (!existsSync(filePath)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
Object.assign(values, parseDevflareEnvFile(await readFile(filePath, "utf8")));
|
|
122
|
+
files.push(filePath);
|
|
123
|
+
}
|
|
124
|
+
return { values, files };
|
|
125
|
+
}
|
|
126
|
+
async function loadDevflareDotenvIntoProcess(startDir) {
|
|
127
|
+
const loaded = await loadDevflareDotenv(startDir);
|
|
128
|
+
for (const [key, value] of Object.entries(loaded.values)) {
|
|
129
|
+
if (process.env[key] === undefined) {
|
|
130
|
+
process.env[key] = value;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return loaded;
|
|
134
|
+
}
|
|
135
|
+
function formatMissingEnvTree(missing) {
|
|
136
|
+
const root = {};
|
|
137
|
+
for (const item of missing) {
|
|
138
|
+
let current = root;
|
|
139
|
+
for (const segment of item.path.slice(0, -1)) {
|
|
140
|
+
const next = current[segment];
|
|
141
|
+
if (!next || typeof next !== "object") {
|
|
142
|
+
current[segment] = {};
|
|
143
|
+
}
|
|
144
|
+
current = current[segment];
|
|
145
|
+
}
|
|
146
|
+
current[item.path[item.path.length - 1] ?? item.name] = item.name;
|
|
147
|
+
}
|
|
148
|
+
const lines = [];
|
|
149
|
+
const writeNode = (node, depth) => {
|
|
150
|
+
const indent = "\t".repeat(depth);
|
|
151
|
+
for (const [key, value] of Object.entries(node)) {
|
|
152
|
+
if (value && typeof value === "object") {
|
|
153
|
+
lines.push(`${indent}${key}:`);
|
|
154
|
+
writeNode(value, depth + 1);
|
|
155
|
+
} else {
|
|
156
|
+
lines.push(`${indent}${key}: ${String(value)}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
writeNode(root, 1);
|
|
161
|
+
return lines.join(`
|
|
162
|
+
`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
class EnvVarResolutionError extends Error {
|
|
166
|
+
missing;
|
|
167
|
+
mode;
|
|
168
|
+
code = "ENV_VARS_MISSING";
|
|
169
|
+
constructor(missing, mode) {
|
|
170
|
+
super([
|
|
171
|
+
"These environment variables are missing:",
|
|
172
|
+
"",
|
|
173
|
+
formatMissingEnvTree(missing)
|
|
174
|
+
].join(`
|
|
175
|
+
`));
|
|
176
|
+
this.missing = missing;
|
|
177
|
+
this.mode = mode;
|
|
178
|
+
this.name = "EnvVarResolutionError";
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
class EnvVarParseError extends Error {
|
|
183
|
+
variableName;
|
|
184
|
+
path;
|
|
185
|
+
code = "ENV_VAR_PARSE_FAILED";
|
|
186
|
+
constructor(variableName, path, cause) {
|
|
187
|
+
super(`Could not parse environment variable ${variableName} for vars.${path.join(".")}.
|
|
188
|
+
` + `Parser error: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
189
|
+
this.variableName = variableName;
|
|
190
|
+
this.path = path;
|
|
191
|
+
this.name = "EnvVarParseError";
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function isPlainObject(value) {
|
|
195
|
+
if (!value || typeof value !== "object" || Array.isArray(value) || isEnvVarDescriptor(value)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
const prototype = Object.getPrototypeOf(value);
|
|
199
|
+
return prototype === Object.prototype || prototype === null;
|
|
200
|
+
}
|
|
201
|
+
var OMIT_VALUE = Symbol("omit optional env var");
|
|
202
|
+
function resolveDescriptorValue(descriptor, sources, path, mode, missing) {
|
|
203
|
+
const state = descriptor.__state;
|
|
204
|
+
const rawValue = sources[state.name];
|
|
205
|
+
if (rawValue !== undefined) {
|
|
206
|
+
try {
|
|
207
|
+
return state.parser ? state.parser(rawValue) : rawValue;
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw new EnvVarParseError(state.name, path, error);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (mode === "dev" && state.hasDevDefault) {
|
|
213
|
+
return state.devValue;
|
|
214
|
+
}
|
|
215
|
+
if (state.hasDefault) {
|
|
216
|
+
return state.defaultValue;
|
|
217
|
+
}
|
|
218
|
+
if (state.optional) {
|
|
219
|
+
return OMIT_VALUE;
|
|
220
|
+
}
|
|
221
|
+
missing.push({ path, name: state.name });
|
|
222
|
+
return OMIT_VALUE;
|
|
223
|
+
}
|
|
224
|
+
function resolveVarValue(value, sources, path, mode, missing) {
|
|
225
|
+
if (isEnvVarDescriptor(value)) {
|
|
226
|
+
return resolveDescriptorValue(value, sources, path, mode, missing);
|
|
227
|
+
}
|
|
228
|
+
if (Array.isArray(value)) {
|
|
229
|
+
return value.map((item, index) => resolveVarValue(item, sources, [...path, String(index)], mode, missing)).filter((item) => item !== OMIT_VALUE);
|
|
230
|
+
}
|
|
231
|
+
if (isPlainObject(value)) {
|
|
232
|
+
const resolved = {};
|
|
233
|
+
for (const [key, childValue] of Object.entries(value)) {
|
|
234
|
+
const child = resolveVarValue(childValue, sources, [...path, key], mode, missing);
|
|
235
|
+
if (child !== OMIT_VALUE) {
|
|
236
|
+
resolved[key] = child;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return resolved;
|
|
240
|
+
}
|
|
241
|
+
return value;
|
|
242
|
+
}
|
|
243
|
+
function resolveVarsObject(vars, sources, mode, missing) {
|
|
244
|
+
if (!vars) {
|
|
245
|
+
return vars;
|
|
246
|
+
}
|
|
247
|
+
const resolved = resolveVarValue(vars, sources, [], mode, missing);
|
|
248
|
+
return resolved === OMIT_VALUE ? undefined : resolved;
|
|
249
|
+
}
|
|
250
|
+
async function resolveConfigEnvVars(config, options) {
|
|
251
|
+
const startDir = options.configPath ? dirname(resolve(options.cwd, options.configPath)) : options.cwd;
|
|
252
|
+
const dotenv = await loadDevflareDotenv(startDir);
|
|
253
|
+
const sources = {
|
|
254
|
+
...dotenv.values,
|
|
255
|
+
...process.env
|
|
256
|
+
};
|
|
257
|
+
const missing = [];
|
|
258
|
+
const vars = resolveVarsObject(config.vars, sources, options.mode, missing);
|
|
259
|
+
if (missing.length > 0) {
|
|
260
|
+
throw new EnvVarResolutionError(missing, options.mode);
|
|
261
|
+
}
|
|
262
|
+
return vars === config.vars ? config : {
|
|
263
|
+
...config,
|
|
264
|
+
vars
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/config/preview.ts
|
|
269
|
+
var PREVIEW_SCOPED_NAME_PREFIX = "__DEVFLARE_PREVIEW_SCOPE__:";
|
|
270
|
+
function getPreviewScopedSeparator(options) {
|
|
271
|
+
return options?.separator ?? "-";
|
|
272
|
+
}
|
|
273
|
+
function encodePreviewScopedName(value) {
|
|
274
|
+
return `${PREVIEW_SCOPED_NAME_PREFIX}${JSON.stringify(value)}`;
|
|
275
|
+
}
|
|
276
|
+
function invalidPreviewScopedName(reason) {
|
|
277
|
+
throw new Error(`Invalid Devflare preview-scoped value: ${reason}. Recreate it with preview.scope(...) instead of constructing preview markers manually.`);
|
|
278
|
+
}
|
|
279
|
+
function decodePreviewScopedName(value) {
|
|
280
|
+
const payload = value.slice(PREVIEW_SCOPED_NAME_PREFIX.length);
|
|
281
|
+
let parsed;
|
|
282
|
+
try {
|
|
283
|
+
parsed = JSON.parse(payload);
|
|
284
|
+
} catch {
|
|
285
|
+
invalidPreviewScopedName("the encoded payload is not valid JSON");
|
|
286
|
+
}
|
|
287
|
+
const baseName = typeof parsed.baseName === "string" ? parsed.baseName : "";
|
|
288
|
+
if (!baseName.trim()) {
|
|
289
|
+
invalidPreviewScopedName("the encoded payload is missing a non-empty baseName");
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
baseName,
|
|
293
|
+
separator: typeof parsed.separator === "string" && parsed.separator.length > 0 ? parsed.separator : "-"
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
function normalizePreviewFragment(rawValue) {
|
|
297
|
+
let normalized = rawValue.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
298
|
+
if (!normalized) {
|
|
299
|
+
normalized = "preview";
|
|
300
|
+
}
|
|
301
|
+
if (!/^[a-z]/.test(normalized)) {
|
|
302
|
+
normalized = `b-${normalized}`;
|
|
303
|
+
}
|
|
304
|
+
return normalized;
|
|
305
|
+
}
|
|
306
|
+
function getPreviewIdentifierFromEnv(env2) {
|
|
307
|
+
const explicitIdentifier = env2.DEVFLARE_PREVIEW_IDENTIFIER?.trim();
|
|
308
|
+
if (explicitIdentifier) {
|
|
309
|
+
return {
|
|
310
|
+
identifier: normalizePreviewFragment(explicitIdentifier),
|
|
311
|
+
source: "env-identifier"
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
const previewPr = env2.DEVFLARE_PREVIEW_PR?.trim();
|
|
315
|
+
if (previewPr) {
|
|
316
|
+
return {
|
|
317
|
+
identifier: normalizePreviewFragment(`pr-${previewPr}`),
|
|
318
|
+
source: "env-pr"
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
const previewBranch = env2.DEVFLARE_PREVIEW_BRANCH?.trim();
|
|
322
|
+
if (previewBranch) {
|
|
323
|
+
return {
|
|
324
|
+
identifier: normalizePreviewFragment(previewBranch),
|
|
325
|
+
source: "env-branch"
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
identifier: undefined,
|
|
330
|
+
source: "none"
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function resolvePreviewIdentifier(options = {}) {
|
|
334
|
+
if (options.identifier?.trim()) {
|
|
335
|
+
return {
|
|
336
|
+
identifier: normalizePreviewFragment(options.identifier),
|
|
337
|
+
source: "identifier"
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
const env2 = options.env ?? process.env;
|
|
341
|
+
const envIdentifier = getPreviewIdentifierFromEnv(env2);
|
|
342
|
+
if (envIdentifier.identifier) {
|
|
343
|
+
return envIdentifier;
|
|
344
|
+
}
|
|
345
|
+
return options.environment === "preview" ? {
|
|
346
|
+
identifier: "preview",
|
|
347
|
+
source: "environment"
|
|
348
|
+
} : {
|
|
349
|
+
identifier: undefined,
|
|
350
|
+
source: "none"
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
function mapRecordValues(record, mapper) {
|
|
354
|
+
return Object.fromEntries(Object.entries(record).map(([key, value]) => [key, mapper(value)]));
|
|
355
|
+
}
|
|
356
|
+
var preview = {
|
|
357
|
+
scope(defaults = {}) {
|
|
358
|
+
return (baseName, options = {}) => {
|
|
359
|
+
if (!baseName.trim()) {
|
|
360
|
+
throw new Error("preview.scope(...) requires a non-empty baseName.");
|
|
361
|
+
}
|
|
362
|
+
return encodePreviewScopedName({
|
|
363
|
+
baseName,
|
|
364
|
+
separator: getPreviewScopedSeparator({
|
|
365
|
+
...defaults,
|
|
366
|
+
...options
|
|
367
|
+
})
|
|
368
|
+
});
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
function isPreviewScopedName(value) {
|
|
373
|
+
return typeof value === "string" && value.startsWith(PREVIEW_SCOPED_NAME_PREFIX);
|
|
374
|
+
}
|
|
375
|
+
function materializePreviewScopedString(value, options = {}) {
|
|
376
|
+
if (!isPreviewScopedName(value)) {
|
|
377
|
+
return value;
|
|
378
|
+
}
|
|
379
|
+
const scoped = decodePreviewScopedName(value);
|
|
380
|
+
const previewIdentifier = resolvePreviewIdentifier(options).identifier;
|
|
381
|
+
return previewIdentifier ? `${scoped.baseName}${scoped.separator}${previewIdentifier}` : scoped.baseName;
|
|
382
|
+
}
|
|
383
|
+
function materializePreviewScopedConfig(config, options = {}) {
|
|
384
|
+
if (!config.bindings) {
|
|
385
|
+
return config;
|
|
386
|
+
}
|
|
387
|
+
const bindings = config.bindings;
|
|
388
|
+
const hasPreviewIdentifier = Boolean(resolvePreviewIdentifier(options).identifier);
|
|
389
|
+
return {
|
|
390
|
+
...config,
|
|
391
|
+
bindings: {
|
|
392
|
+
...bindings,
|
|
393
|
+
...bindings.kv ? {
|
|
394
|
+
kv: mapRecordValues(bindings.kv, (binding) => {
|
|
395
|
+
return typeof binding === "string" ? materializePreviewScopedString(binding, options) : binding;
|
|
396
|
+
})
|
|
397
|
+
} : {},
|
|
398
|
+
...bindings.d1 ? {
|
|
399
|
+
d1: mapRecordValues(bindings.d1, (binding) => {
|
|
400
|
+
return typeof binding === "string" ? materializePreviewScopedString(binding, options) : binding;
|
|
401
|
+
})
|
|
402
|
+
} : {},
|
|
403
|
+
...bindings.r2 ? {
|
|
404
|
+
r2: mapRecordValues(bindings.r2, (binding) => {
|
|
405
|
+
return materializePreviewScopedString(binding, options);
|
|
406
|
+
})
|
|
407
|
+
} : {},
|
|
408
|
+
...bindings.queues ? {
|
|
409
|
+
queues: {
|
|
410
|
+
...bindings.queues,
|
|
411
|
+
...bindings.queues.producers ? {
|
|
412
|
+
producers: mapRecordValues(bindings.queues.producers, (queueName) => {
|
|
413
|
+
return materializePreviewScopedString(queueName, options);
|
|
414
|
+
})
|
|
415
|
+
} : {},
|
|
416
|
+
...bindings.queues.consumers ? {
|
|
417
|
+
consumers: bindings.queues.consumers.map((consumer) => ({
|
|
418
|
+
...consumer,
|
|
419
|
+
queue: materializePreviewScopedString(consumer.queue, options),
|
|
420
|
+
...consumer.deadLetterQueue ? {
|
|
421
|
+
deadLetterQueue: materializePreviewScopedString(consumer.deadLetterQueue, options)
|
|
422
|
+
} : {}
|
|
423
|
+
}))
|
|
424
|
+
} : {}
|
|
425
|
+
}
|
|
426
|
+
} : {},
|
|
427
|
+
...bindings.services ? {
|
|
428
|
+
services: mapRecordValues(bindings.services, (binding) => ({
|
|
429
|
+
...binding,
|
|
430
|
+
service: materializePreviewScopedString(binding.service, options)
|
|
431
|
+
}))
|
|
432
|
+
} : {},
|
|
433
|
+
...bindings.vectorize ? {
|
|
434
|
+
vectorize: mapRecordValues(bindings.vectorize, (binding) => ({
|
|
435
|
+
...binding,
|
|
436
|
+
indexName: materializePreviewScopedString(binding.indexName, options)
|
|
437
|
+
}))
|
|
438
|
+
} : {},
|
|
439
|
+
...bindings.hyperdrive ? {
|
|
440
|
+
hyperdrive: mapRecordValues(bindings.hyperdrive, (binding) => {
|
|
441
|
+
if (typeof binding === "string") {
|
|
442
|
+
return materializePreviewScopedString(binding, options);
|
|
443
|
+
}
|
|
444
|
+
if (binding && typeof binding === "object" && "name" in binding && typeof binding.name === "string") {
|
|
445
|
+
if (hasPreviewIdentifier && binding.previewId) {
|
|
446
|
+
return {
|
|
447
|
+
id: binding.previewId,
|
|
448
|
+
...binding.localConnectionString && {
|
|
449
|
+
localConnectionString: binding.localConnectionString
|
|
450
|
+
},
|
|
451
|
+
...!binding.localConnectionString && binding.previewLocalConnectionString && {
|
|
452
|
+
localConnectionString: binding.previewLocalConnectionString
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
...binding,
|
|
458
|
+
name: materializePreviewScopedString(binding.name, options)
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
return binding;
|
|
462
|
+
})
|
|
463
|
+
} : {},
|
|
464
|
+
...bindings.browser ? {
|
|
465
|
+
browser: mapRecordValues(bindings.browser, (binding) => {
|
|
466
|
+
return typeof binding === "string" ? materializePreviewScopedString(binding, options) : binding;
|
|
467
|
+
})
|
|
468
|
+
} : {},
|
|
469
|
+
...bindings.analyticsEngine ? {
|
|
470
|
+
analyticsEngine: mapRecordValues(bindings.analyticsEngine, (binding) => ({
|
|
471
|
+
...binding,
|
|
472
|
+
dataset: materializePreviewScopedString(binding.dataset, options)
|
|
473
|
+
}))
|
|
474
|
+
} : {}
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export { env, isEnvVarDescriptor, getDevflareDotenvPaths, loadDevflareDotenvIntoProcess, EnvVarResolutionError, resolveConfigEnvVars, resolvePreviewIdentifier, preview, isPreviewScopedName, materializePreviewScopedString, materializePreviewScopedConfig };
|