@aziontech/bundler 1.0.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/LICENSE.md +21 -0
- package/README.md +413 -0
- package/dist/index-D5_BuqHI.js +2120 -0
- package/dist/index.js +143 -0
- package/dist/utils-DkUMNTN3.js +329 -0
- package/package.json +88 -0
|
@@ -0,0 +1,2120 @@
|
|
|
1
|
+
import { fetchContext as je, FirewallEventContext as Ue, FetchEventContext as Re, AsyncHooksContext as Ne, StorageContext as Ie, EnvVarsContext as Be, NetworkListContext as De, fsContext as We, streamContext as Me, cryptoContext as Le, promisesContext as He, KVContext as Ve } from "@aziontech/builder/polyfills";
|
|
2
|
+
import { load as ze } from "@edge-runtime/primitives/dist/load.js";
|
|
3
|
+
import { d as g, D as O, S as ue, B as k, a as _, m as Ge, e as qe, c as Ke, b as U, T as R, r as se, w as ee, f as ve, g as Je, h as Te } from "./utils-DkUMNTN3.js";
|
|
4
|
+
import * as A from "@aziontech/utils/node";
|
|
5
|
+
import { getPackageManager as Xe, feedback as p } from "@aziontech/utils/node";
|
|
6
|
+
import Ye from "chokidar";
|
|
7
|
+
import { runPhaseWithTelemetry as x, TelemetryCollector as Ze, formatHeader as Qe, formatBuildConfig as et } from "@aziontech/bundler-telemetry";
|
|
8
|
+
import { validateConfig as te, processConfig as tt, convertJsonConfigToObject as rt } from "@aziontech/config";
|
|
9
|
+
import h, { rm as oe, mkdir as Se, readFile as nt, stat as st, writeFile as ot } from "fs/promises";
|
|
10
|
+
import S, { resolve as ie, join as C, basename as re, extname as q, dirname as $e, relative as it } from "path";
|
|
11
|
+
import J from "os";
|
|
12
|
+
import { executeESBuildBuild as at, executeWebpackBuild as ct, createAzionWebpackConfig as lt, createAzionESBuildConfig as ft } from "@aziontech/builder";
|
|
13
|
+
import ut from "lodash";
|
|
14
|
+
import { runInContext as ae } from "vm";
|
|
15
|
+
import { pathToFileURL as dt } from "url";
|
|
16
|
+
import I from "node:vm";
|
|
17
|
+
import pt from "net";
|
|
18
|
+
import { runServer as mt } from "edge-runtime";
|
|
19
|
+
import * as K from "@aziontech/presets";
|
|
20
|
+
import de, { promises as H, existsSync as ht } from "fs";
|
|
21
|
+
import { listFrameworks as gt } from "@netlify/framework-info";
|
|
22
|
+
import * as Pe from "prettier";
|
|
23
|
+
import { cosmiconfig as yt } from "cosmiconfig";
|
|
24
|
+
import { TypeScriptLoader as X } from "cosmiconfig-typescript-loader";
|
|
25
|
+
class wt {
|
|
26
|
+
context;
|
|
27
|
+
constructor(t = {}) {
|
|
28
|
+
const r = I.createContext(
|
|
29
|
+
{},
|
|
30
|
+
{
|
|
31
|
+
name: "Edge Runtime",
|
|
32
|
+
codeGeneration: t.codeGeneration ?? {
|
|
33
|
+
strings: !1,
|
|
34
|
+
wasm: !0
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
this.context = t.extend?.(r) ?? r;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Allows to run arbitrary code within the VM.
|
|
42
|
+
*/
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
evaluate(t) {
|
|
45
|
+
return I.runInContext(t, this.context, {
|
|
46
|
+
importModuleDynamically: async (r) => {
|
|
47
|
+
try {
|
|
48
|
+
return await import(dt(ie(r)).href);
|
|
49
|
+
} catch {
|
|
50
|
+
process.env.DEBUG && console.warn(
|
|
51
|
+
">>> [edge-runtime] dynamic import failed, returning empty module:",
|
|
52
|
+
r
|
|
53
|
+
);
|
|
54
|
+
const n = new I.SourceTextModule("export default {}", { context: this.context });
|
|
55
|
+
return await n.link(() => new I.SourceTextModule("export {}", { context: this.context })), await n.evaluate(), n;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
let V, z;
|
|
63
|
+
class bt extends wt {
|
|
64
|
+
dispatchFetch;
|
|
65
|
+
constructor(t) {
|
|
66
|
+
super({
|
|
67
|
+
...t,
|
|
68
|
+
extend: (r) => t?.extend ? t.extend(pe(r)) : pe(r)
|
|
69
|
+
}), Object.defineProperty(this.context, "__onUnhandledRejectionHandlers", {
|
|
70
|
+
set: vt,
|
|
71
|
+
configurable: !1,
|
|
72
|
+
enumerable: !1
|
|
73
|
+
}), Object.defineProperty(this, "__rejectionHandlers", {
|
|
74
|
+
get: () => V,
|
|
75
|
+
configurable: !1,
|
|
76
|
+
enumerable: !1
|
|
77
|
+
}), Object.defineProperty(this.context, "__onErrorHandlers", {
|
|
78
|
+
set: Tt,
|
|
79
|
+
configurable: !1,
|
|
80
|
+
enumerable: !1
|
|
81
|
+
}), Object.defineProperty(this, "__errorHandlers", {
|
|
82
|
+
get: () => z,
|
|
83
|
+
configurable: !1,
|
|
84
|
+
enumerable: !1
|
|
85
|
+
}), this.evaluate(St()), this.dispatchFetch = this.evaluate($t());
|
|
86
|
+
for (const r of xe)
|
|
87
|
+
Et(r, this.context);
|
|
88
|
+
t?.initialCode && this.evaluate(t.initialCode);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const xe = [
|
|
92
|
+
"Object",
|
|
93
|
+
"Array",
|
|
94
|
+
"RegExp",
|
|
95
|
+
"Uint8Array",
|
|
96
|
+
"ArrayBuffer",
|
|
97
|
+
"Error",
|
|
98
|
+
"SyntaxError",
|
|
99
|
+
"TypeError"
|
|
100
|
+
];
|
|
101
|
+
function Et(e, t) {
|
|
102
|
+
const r = eval;
|
|
103
|
+
return t[/* @__PURE__ */ Symbol.for(`node:${e}`)] = r(e), ae(
|
|
104
|
+
`
|
|
105
|
+
globalThis.${e} = new Proxy(${e}, {
|
|
106
|
+
get(target, prop, receiver) {
|
|
107
|
+
if (prop === Symbol.hasInstance && receiver === globalThis.${e}) {
|
|
108
|
+
const nodeTarget = globalThis[Symbol.for('node:${e}')];
|
|
109
|
+
if (nodeTarget) {
|
|
110
|
+
return function(instance) {
|
|
111
|
+
return instance instanceof target || instance instanceof nodeTarget;
|
|
112
|
+
};
|
|
113
|
+
} else {
|
|
114
|
+
throw new Error('node target must exist')
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return Reflect.get(target, prop, receiver);
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
`,
|
|
122
|
+
t
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
function vt(e) {
|
|
126
|
+
V || process.on("unhandledRejection", function(r, n) {
|
|
127
|
+
V.forEach((s) => s({ reason: r, promise: n }));
|
|
128
|
+
}), V = e;
|
|
129
|
+
}
|
|
130
|
+
function Tt(e) {
|
|
131
|
+
z || process.on("uncaughtException", function(r) {
|
|
132
|
+
z.forEach((n) => n(r));
|
|
133
|
+
}), z = e;
|
|
134
|
+
}
|
|
135
|
+
function St() {
|
|
136
|
+
return `
|
|
137
|
+
Object.defineProperty(self, '__listeners', {
|
|
138
|
+
configurable: false,
|
|
139
|
+
enumerable: false,
|
|
140
|
+
value: {},
|
|
141
|
+
writable: true,
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
function __conditionallyUpdatesHandlerList(eventType) {
|
|
145
|
+
if (eventType === 'unhandledrejection') {
|
|
146
|
+
self.__onUnhandledRejectionHandlers = self.__listeners[eventType];
|
|
147
|
+
} else if (eventType === 'error') {
|
|
148
|
+
self.__onErrorHandlers = self.__listeners[eventType];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function addEventListener(type, handler) {
|
|
153
|
+
const eventType = type.toLowerCase();
|
|
154
|
+
if (eventType === 'fetch' && self.__listeners.fetch) {
|
|
155
|
+
throw new TypeError('You can register just one "fetch" event listener');
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
self.__listeners[eventType] = self.__listeners[eventType] || [];
|
|
159
|
+
self.__listeners[eventType].push(handler);
|
|
160
|
+
__conditionallyUpdatesHandlerList(eventType);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function removeEventListener(type, handler) {
|
|
164
|
+
const eventType = type.toLowerCase();
|
|
165
|
+
if (self.__listeners[eventType]) {
|
|
166
|
+
self.__listeners[eventType] = self.__listeners[eventType].filter(item => {
|
|
167
|
+
return item !== handler;
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
if (self.__listeners[eventType].length === 0) {
|
|
171
|
+
delete self.__listeners[eventType];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
__conditionallyUpdatesHandlerList(eventType);
|
|
175
|
+
}
|
|
176
|
+
`;
|
|
177
|
+
}
|
|
178
|
+
function $t() {
|
|
179
|
+
return `(async function dispatchFetch(input, init) {
|
|
180
|
+
const request = new Request(input, init);
|
|
181
|
+
const event = new FetchEvent(request);
|
|
182
|
+
if (!self.__listeners.fetch) {
|
|
183
|
+
throw new Error("No fetch event listeners found");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const getResponse = ({ response, error }) => {
|
|
187
|
+
if (error || !response || !(response instanceof Response)) {
|
|
188
|
+
console.error(error ? error.toString() : 'The event listener did not respond')
|
|
189
|
+
response = new Response(null, {
|
|
190
|
+
statusText: 'Internal Server Error',
|
|
191
|
+
status: 500
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
response.waitUntil = () => Promise.all(event.awaiting);
|
|
196
|
+
|
|
197
|
+
if (response.status < 300 || response.status >= 400 ) {
|
|
198
|
+
response.headers.delete('content-encoding');
|
|
199
|
+
response.headers.delete('transform-encoding');
|
|
200
|
+
response.headers.delete('content-length');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return response;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
await self.__listeners.fetch[0].call(event, event)
|
|
208
|
+
} catch (error) {
|
|
209
|
+
return getResponse({ error })
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return Promise.resolve(event.response)
|
|
213
|
+
.then(response => getResponse({ response }))
|
|
214
|
+
.catch(error => getResponse({ error }))
|
|
215
|
+
})`;
|
|
216
|
+
}
|
|
217
|
+
function pe(e) {
|
|
218
|
+
F(e, "self", { enumerable: !0, value: e }), F(e, "globalThis", { value: e }), F(e, "Symbol", { value: Symbol }), F(e, "clearInterval", { value: clearInterval }), F(e, "clearTimeout", { value: clearTimeout }), F(e, "queueMicrotask", { value: queueMicrotask }), F(e, "EdgeRuntime", { value: "edge-runtime" });
|
|
219
|
+
const t = xt(e);
|
|
220
|
+
return Pt(e, {
|
|
221
|
+
exports: ze({
|
|
222
|
+
...t,
|
|
223
|
+
WeakRef: ae("WeakRef", e)
|
|
224
|
+
}),
|
|
225
|
+
enumerable: ["crypto"],
|
|
226
|
+
nonenumerable: [
|
|
227
|
+
// Crypto
|
|
228
|
+
"Crypto",
|
|
229
|
+
"CryptoKey",
|
|
230
|
+
"SubtleCrypto",
|
|
231
|
+
// Fetch APIs
|
|
232
|
+
"fetch",
|
|
233
|
+
"File",
|
|
234
|
+
"FormData",
|
|
235
|
+
"Headers",
|
|
236
|
+
"Request",
|
|
237
|
+
"Response",
|
|
238
|
+
"WebSocket",
|
|
239
|
+
// Structured Clone
|
|
240
|
+
"structuredClone",
|
|
241
|
+
// Blob
|
|
242
|
+
"Blob",
|
|
243
|
+
// URL
|
|
244
|
+
"URL",
|
|
245
|
+
"URLSearchParams",
|
|
246
|
+
"URLPattern",
|
|
247
|
+
// AbortController
|
|
248
|
+
"AbortController",
|
|
249
|
+
"AbortSignal",
|
|
250
|
+
"DOMException",
|
|
251
|
+
// Streams
|
|
252
|
+
"ReadableStream",
|
|
253
|
+
"ReadableStreamBYOBReader",
|
|
254
|
+
"ReadableStreamDefaultReader",
|
|
255
|
+
"TextDecoderStream",
|
|
256
|
+
"TextEncoderStream",
|
|
257
|
+
"TransformStream",
|
|
258
|
+
"WritableStream",
|
|
259
|
+
"WritableStreamDefaultWriter",
|
|
260
|
+
// Encoding
|
|
261
|
+
"atob",
|
|
262
|
+
"btoa",
|
|
263
|
+
"TextEncoder",
|
|
264
|
+
"TextDecoder",
|
|
265
|
+
// Events
|
|
266
|
+
"Event",
|
|
267
|
+
"EventTarget",
|
|
268
|
+
"FetchEvent",
|
|
269
|
+
"PromiseRejectionEvent",
|
|
270
|
+
// Console
|
|
271
|
+
"console",
|
|
272
|
+
// Timers
|
|
273
|
+
"setTimeout",
|
|
274
|
+
"setInterval"
|
|
275
|
+
]
|
|
276
|
+
}), e;
|
|
277
|
+
}
|
|
278
|
+
function F(e, t, r) {
|
|
279
|
+
Object.defineProperty(e, t, {
|
|
280
|
+
configurable: r.configurable ?? !1,
|
|
281
|
+
enumerable: r.enumerable ?? !1,
|
|
282
|
+
value: r.value,
|
|
283
|
+
writable: r.writable ?? !0
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
function Pt(e, t) {
|
|
287
|
+
for (const r of t.enumerable ?? []) {
|
|
288
|
+
if (!t.exports[r])
|
|
289
|
+
throw new Error(`Attempt to export a nullable value for "${r}"`);
|
|
290
|
+
F(e, r, {
|
|
291
|
+
enumerable: !0,
|
|
292
|
+
value: t.exports[r]
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
for (const r of t.nonenumerable ?? []) {
|
|
296
|
+
if (!t.exports[r])
|
|
297
|
+
throw new Error(`Attempt to export a nullable value for "${r}"`);
|
|
298
|
+
F(e, r, {
|
|
299
|
+
value: t.exports[r]
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
function xt(e) {
|
|
304
|
+
const r = `({${xe.join(",")}})`;
|
|
305
|
+
return ae(r, e);
|
|
306
|
+
}
|
|
307
|
+
function ke(e, t = !1) {
|
|
308
|
+
const r = (s) => (s.RESERVED_FETCH = s.fetch.bind(s), s.fetch = async (o, i) => je(
|
|
309
|
+
s,
|
|
310
|
+
o,
|
|
311
|
+
i,
|
|
312
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
313
|
+
globalThis?.AZION_BUCKET_NAME,
|
|
314
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
315
|
+
globalThis?.AZION_BUCKET_PREFIX
|
|
316
|
+
), s.FetchEvent = t ? Ue : Re, s.Response = t ? Response : s.Response, s.eval = eval, s.process = { env: process.env }, s.File = void 0, s.WebSocket = void 0, s.ASYNC_LOCAL_STORAGE = Ne, s.STORAGE_CONTEXT = Ie, s.ENV_VARS_CONTEXT = Be, s.NETWORK_LIST_CONTEXT = De, s.FS_CONTEXT = We, s.STREAM_CONTEXT = Me, s.CRYPTO_CONTEXT = Le, s.TextDecoderStream = TextDecoderStream, s.TextEncoderStream = TextEncoderStream, s.perfomance = { now: performance.now }, s.CompressionStream = CompressionStream, s.CountQueuingStrategy = CountQueuingStrategy, s.DecompressionStream = DecompressionStream, s.Promise = Promise, s.Promise.withResolvers = He, s.KV_CONTEXT = Ve, s);
|
|
317
|
+
return new bt({ extend: r, initialCode: e, codeGeneration: { strings: !0 } });
|
|
318
|
+
}
|
|
319
|
+
class Ce extends Error {
|
|
320
|
+
constructor(t, r) {
|
|
321
|
+
super(t), this.code = r, this.name = "PackageJsonError";
|
|
322
|
+
}
|
|
323
|
+
code;
|
|
324
|
+
}
|
|
325
|
+
class kt extends Error {
|
|
326
|
+
constructor(t) {
|
|
327
|
+
super(`Please install dependencies using ${t}`), this.name = "DependenciesError";
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const Ct = async () => {
|
|
331
|
+
const e = C(process.cwd(), "package.json");
|
|
332
|
+
try {
|
|
333
|
+
const t = await nt(e, "utf8");
|
|
334
|
+
return JSON.parse(t);
|
|
335
|
+
} catch (t) {
|
|
336
|
+
throw new Ce("Failed to read package.json", t.code);
|
|
337
|
+
}
|
|
338
|
+
}, Ft = async () => {
|
|
339
|
+
const e = C(process.cwd(), "node_modules");
|
|
340
|
+
try {
|
|
341
|
+
return (await st(e)).isDirectory();
|
|
342
|
+
} catch {
|
|
343
|
+
return !1;
|
|
344
|
+
}
|
|
345
|
+
}, At = async () => {
|
|
346
|
+
let e;
|
|
347
|
+
try {
|
|
348
|
+
e = await Ct();
|
|
349
|
+
} catch (t) {
|
|
350
|
+
if (t instanceof Ce && t.code === "ENOENT")
|
|
351
|
+
return;
|
|
352
|
+
throw t;
|
|
353
|
+
}
|
|
354
|
+
if (e && (e.dependencies || e.devDependencies)) {
|
|
355
|
+
const t = await Xe();
|
|
356
|
+
if (!await Ft())
|
|
357
|
+
throw new kt(t);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
function B({
|
|
361
|
+
inputValue: e,
|
|
362
|
+
fileValue: t,
|
|
363
|
+
storeValue: r,
|
|
364
|
+
defaultValue: n
|
|
365
|
+
}) {
|
|
366
|
+
return ((i) => i === "true" ? !0 : i === "false" ? !1 : i)(e ?? t ?? r ?? n);
|
|
367
|
+
}
|
|
368
|
+
const _t = async (e) => {
|
|
369
|
+
try {
|
|
370
|
+
await Promise.all(
|
|
371
|
+
e.map(async (t) => {
|
|
372
|
+
try {
|
|
373
|
+
await oe(t, { recursive: !0, force: !0 });
|
|
374
|
+
} catch (r) {
|
|
375
|
+
if (r.code !== "ENOENT")
|
|
376
|
+
throw r;
|
|
377
|
+
}
|
|
378
|
+
await Se(t, { recursive: !0 });
|
|
379
|
+
})
|
|
380
|
+
);
|
|
381
|
+
} catch (t) {
|
|
382
|
+
throw new Error(`Failed to clean directories: ${t}`);
|
|
383
|
+
}
|
|
384
|
+
}, Ot = (e) => `//---
|
|
385
|
+
//storages:
|
|
386
|
+
// - name: assets
|
|
387
|
+
// bucket: ${e.bucket}
|
|
388
|
+
// prefix: ${e.prefix}
|
|
389
|
+
//---
|
|
390
|
+
/* this temporary binding is used to inject the storage name and prefix into the function file */
|
|
391
|
+
globalThis.AZION_BUCKET_NAME = '${e.bucket}';
|
|
392
|
+
globalThis.AZION_BUCKET_PREFIX = '${e.prefix}';`, jt = async (e) => {
|
|
393
|
+
try {
|
|
394
|
+
return await h.access(e), !0;
|
|
395
|
+
} catch {
|
|
396
|
+
return !1;
|
|
397
|
+
}
|
|
398
|
+
}, Ut = (e) => S.isAbsolute(e) ? e : S.resolve(process.cwd(), O.OUTPUT_BASE_PATH, e), Rt = (e, t) => t.find((r) => r.name === e), Nt = async (e, t, r) => {
|
|
399
|
+
if (!e.path) {
|
|
400
|
+
g.warn(`Function ${e.name} does not have a defined path`);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const n = e.bindings?.storage;
|
|
404
|
+
if (!n) {
|
|
405
|
+
g.info(`Function ${e.name} does not have storage bindings`);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const s = Ut(
|
|
409
|
+
e.path.replace(/\.js$/, r ? ".js" : ".dev.js")
|
|
410
|
+
);
|
|
411
|
+
if (!await jt(s)) {
|
|
412
|
+
p.bindings.warn(`Function file not found: ${s}.`), p.bindings.info(`Binding injection skipped for function ${e.name}`);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
const o = await h.readFile(s, "utf-8");
|
|
417
|
+
if (o.includes("//storages:") || o.includes("//---")) {
|
|
418
|
+
g.info(`Function ${e.name} already has injected bindings`);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const i = String(n.bucket), c = Rt(i, t);
|
|
422
|
+
if (!c) {
|
|
423
|
+
g.warn(`Storage '${i}' not found for function ${e.name}`);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
const a = n.prefix, l = a || c.prefix;
|
|
427
|
+
a || p.postbuild.info(
|
|
428
|
+
`No prefix provided for binding in function '${e.name}', using storage prefix: ${l}`
|
|
429
|
+
);
|
|
430
|
+
const f = Ot({
|
|
431
|
+
bucket: i,
|
|
432
|
+
prefix: l
|
|
433
|
+
});
|
|
434
|
+
g.info(`Generated binding template for ${e.name}: ${f}`);
|
|
435
|
+
const u = `${f}
|
|
436
|
+
${o}`;
|
|
437
|
+
await h.writeFile(s, u), g.info(`Bindings injected into ${s} for function ${e.name}`);
|
|
438
|
+
} catch (o) {
|
|
439
|
+
throw g.error(`Failed to process bindings for function ${e.name}:`, o), o;
|
|
440
|
+
}
|
|
441
|
+
}, It = async ({
|
|
442
|
+
config: e,
|
|
443
|
+
storageSetup: t,
|
|
444
|
+
isProduction: r
|
|
445
|
+
}) => {
|
|
446
|
+
try {
|
|
447
|
+
const n = e.functions || [];
|
|
448
|
+
if (n.length === 0) {
|
|
449
|
+
g.info("No functions found to inject bindings");
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
g.info("Injecting bindings into functions..."), await Promise.all(
|
|
453
|
+
n.map((s) => Nt(s, t, r))
|
|
454
|
+
);
|
|
455
|
+
} catch (n) {
|
|
456
|
+
return g.error("Failed to execute bindings setup:", n), Promise.reject(n);
|
|
457
|
+
}
|
|
458
|
+
}, Bt = () => (/* @__PURE__ */ new Date()).toISOString().slice(0, 10).replace(/-/g, ""), Dt = async ({
|
|
459
|
+
entry: e,
|
|
460
|
+
ext: t = "js",
|
|
461
|
+
basePath: r = ".edge/functions",
|
|
462
|
+
production: n = !0,
|
|
463
|
+
bundler: s
|
|
464
|
+
}) => {
|
|
465
|
+
const o = Bt(), i = (c, a) => {
|
|
466
|
+
const l = re(c, q(c)), f = $e(c), u = `azion-${l}-${o}.temp.${t}`, m = ie(f, u), b = s === "webpack" ? ".js" : "", d = n ? "" : ".dev";
|
|
467
|
+
let y;
|
|
468
|
+
if (a) {
|
|
469
|
+
const T = a.replace(/\.[^/.]+$/, "");
|
|
470
|
+
y = C(r, `${T}${d}${b}`);
|
|
471
|
+
} else
|
|
472
|
+
y = C(r, f, `${l}${d}${b}`);
|
|
473
|
+
return { [y]: m };
|
|
474
|
+
};
|
|
475
|
+
if (typeof e == "string") {
|
|
476
|
+
const a = `${re(e, q(e))}${n ? "" : ".dev"}`;
|
|
477
|
+
return { ...i(e, a) };
|
|
478
|
+
}
|
|
479
|
+
return Array.isArray(e) ? e.reduce((c, a) => ({ ...c, ...i(a) }), {}) : Object.entries(e).reduce(
|
|
480
|
+
(c, [a, l]) => ({ ...c, ...i(l, a) }),
|
|
481
|
+
{}
|
|
482
|
+
);
|
|
483
|
+
}, Wt = async (e, t, r) => {
|
|
484
|
+
let n = {};
|
|
485
|
+
const s = e.build?.entry, o = async (i) => await Dt({
|
|
486
|
+
entry: i,
|
|
487
|
+
ext: t.metadata.ext ?? k.DEFAULT_OUTPUT_EXTENSION,
|
|
488
|
+
production: r,
|
|
489
|
+
bundler: e.build?.bundler ?? t.config.build?.bundler ?? ue.DEFAULT
|
|
490
|
+
});
|
|
491
|
+
if (s ? n = await o(s) : t.config.build?.entry ? n = await o(t.config.build.entry) : t.handler && (n = await o(k.DEFAULT_HANDLER_FILENAME)), Object.keys(n).length === 0) {
|
|
492
|
+
const i = t.config.build?.entry ? `(default is "${t.config.build.entry}")` : "";
|
|
493
|
+
throw new Error(
|
|
494
|
+
`No entry point found ${i}. Please specify one using --entry or create a default entry file in your project.`
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
return {
|
|
498
|
+
...e.build,
|
|
499
|
+
entry: n,
|
|
500
|
+
bundler: e.build?.bundler ?? t.config.build?.bundler ?? ue.DEFAULT,
|
|
501
|
+
preset: t,
|
|
502
|
+
setup: {
|
|
503
|
+
contentToInject: void 0,
|
|
504
|
+
defineVars: {}
|
|
505
|
+
},
|
|
506
|
+
polyfills: !!e.build?.polyfills
|
|
507
|
+
};
|
|
508
|
+
}, Mt = (e, t) => ft(e, t), Lt = (e, t) => lt(e, t), Ht = async (e) => at(e), Vt = async (e) => ct(e), D = {
|
|
509
|
+
createAzionESBuildConfigWrapper: Mt,
|
|
510
|
+
createAzionWebpackConfigWrapper: Lt,
|
|
511
|
+
executeESBuildBuildWrapper: Ht,
|
|
512
|
+
executeWebpackBuildWrapper: Vt
|
|
513
|
+
}, zt = (e) => {
|
|
514
|
+
const t = /import\s+.*?from\s*['"](.*?)['"];?/g, r = /(const\s+.*?=\s*require\(.*\).*);/g, n = (e.match(t) || []).map((i) => i.trim()), s = (e.match(r) || []).map((i) => i.trim());
|
|
515
|
+
let o = e.replace(t, "").replace(r, "").replace(/\n\s*\n\s*\n/g, `
|
|
516
|
+
|
|
517
|
+
`);
|
|
518
|
+
return (n.length || s.length) && (o = `${[...n, ...s].join(`
|
|
519
|
+
`)}
|
|
520
|
+
|
|
521
|
+
${o.trim()}`), o;
|
|
522
|
+
}, Gt = (e, t, r) => t.polyfills && r.production ? `import SRC_NODE_FS from "node:fs";
|
|
523
|
+
${e}` : e, W = async (e, t, r, n, s, o) => !e || !t ? o() : x(
|
|
524
|
+
e,
|
|
525
|
+
t,
|
|
526
|
+
r,
|
|
527
|
+
n,
|
|
528
|
+
s,
|
|
529
|
+
o
|
|
530
|
+
), qt = async ({
|
|
531
|
+
buildConfig: e,
|
|
532
|
+
prebuildResult: t,
|
|
533
|
+
ctx: r,
|
|
534
|
+
telemetry: n,
|
|
535
|
+
telemetryEnabled: s,
|
|
536
|
+
telemetryOutputFormat: o
|
|
537
|
+
}) => {
|
|
538
|
+
try {
|
|
539
|
+
const i = typeof e.entry == "string" ? [e.entry] : Array.isArray(e.entry) ? e.entry : Object.values(e.entry);
|
|
540
|
+
if (t.filesToInject.length > 0) {
|
|
541
|
+
const a = await Promise.all(
|
|
542
|
+
t.filesToInject.map((l) => h.readFile(l, "utf-8"))
|
|
543
|
+
).then((l) => l.join(" "));
|
|
544
|
+
await Promise.all(
|
|
545
|
+
i.map(async (l) => {
|
|
546
|
+
const f = await h.readFile(l, "utf-8"), u = `${a} ${f}`, m = zt(u);
|
|
547
|
+
return h.writeFile(l, m);
|
|
548
|
+
})
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
const c = {
|
|
552
|
+
...e,
|
|
553
|
+
setup: {
|
|
554
|
+
contentToInject: t.injection.banner,
|
|
555
|
+
defineVars: Object.fromEntries(
|
|
556
|
+
// Get all entries from defineVars
|
|
557
|
+
Object.entries(t.bundler.defineVars).filter(([a, l]) => l !== void 0).map(([a, l]) => [a, l])
|
|
558
|
+
)
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
return await Kt(c, r, n, s, o), Promise.all(
|
|
562
|
+
Object.entries(c.entry).map(async ([a]) => {
|
|
563
|
+
const l = a.endsWith(".js") ? a : `${a}.js`, f = await h.readFile(l, "utf-8");
|
|
564
|
+
if (!r.production) return f;
|
|
565
|
+
const u = Gt(
|
|
566
|
+
f,
|
|
567
|
+
e,
|
|
568
|
+
r
|
|
569
|
+
);
|
|
570
|
+
return await h.writeFile(l, u), u;
|
|
571
|
+
})
|
|
572
|
+
);
|
|
573
|
+
} catch (i) {
|
|
574
|
+
return Promise.reject(i);
|
|
575
|
+
}
|
|
576
|
+
}, Kt = async (e, t, r, n, s) => {
|
|
577
|
+
switch (e.bundler) {
|
|
578
|
+
case "esbuild": {
|
|
579
|
+
k.IS_DEBUG && (p.build.info("=== System Info ==="), p.build.info("CPUs:", J.cpus().length), p.build.info("Total Memory:", J.totalmem() / 1024 / 1024, "MB"), p.build.info("Free Memory:", J.freemem() / 1024 / 1024, "MB"));
|
|
580
|
+
const o = await W(
|
|
581
|
+
r,
|
|
582
|
+
n,
|
|
583
|
+
s,
|
|
584
|
+
"esbuild-config-create",
|
|
585
|
+
"ESBuild Config Create",
|
|
586
|
+
async () => D.createAzionESBuildConfigWrapper(e, t)
|
|
587
|
+
);
|
|
588
|
+
let i, c;
|
|
589
|
+
k.IS_DEBUG && (p.build.info("=== Build Test ==="), i = Date.now(), c = process.memoryUsage());
|
|
590
|
+
const a = await W(
|
|
591
|
+
r,
|
|
592
|
+
n,
|
|
593
|
+
s,
|
|
594
|
+
"esbuild-execution",
|
|
595
|
+
"ESBuild Execution",
|
|
596
|
+
async () => D.executeESBuildBuildWrapper(o)
|
|
597
|
+
);
|
|
598
|
+
if (k.IS_DEBUG) {
|
|
599
|
+
const l = Date.now() - i, f = process.memoryUsage();
|
|
600
|
+
p.build.info("=== Results ==="), p.build.info("Duration:", l, "ms");
|
|
601
|
+
const u = {
|
|
602
|
+
heapUsed: `${(f.heapUsed - c.heapUsed) / 1024 / 1024} MB`,
|
|
603
|
+
rss: `${(f.rss - c.rss) / 1024 / 1024} MB`
|
|
604
|
+
};
|
|
605
|
+
p.build.info("Memory heapUsed:", u.heapUsed), p.build.info("Memory rss:", u.rss);
|
|
606
|
+
}
|
|
607
|
+
return a;
|
|
608
|
+
}
|
|
609
|
+
case "webpack": {
|
|
610
|
+
const o = await W(
|
|
611
|
+
r,
|
|
612
|
+
n,
|
|
613
|
+
s,
|
|
614
|
+
"webpack-config-create",
|
|
615
|
+
"Webpack Config Create",
|
|
616
|
+
async () => D.createAzionWebpackConfigWrapper(e, t)
|
|
617
|
+
);
|
|
618
|
+
return W(
|
|
619
|
+
r,
|
|
620
|
+
n,
|
|
621
|
+
s,
|
|
622
|
+
"webpack-execution",
|
|
623
|
+
"Webpack Execution",
|
|
624
|
+
async () => D.executeWebpackBuildWrapper(o)
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
default:
|
|
628
|
+
throw new Error(`Unsupported bundler: ${e.bundler}`);
|
|
629
|
+
}
|
|
630
|
+
}, Jt = (e, t) => {
|
|
631
|
+
const r = (n, s) => {
|
|
632
|
+
if (n && !n.build || Array.isArray(n) && !Array.isArray(s))
|
|
633
|
+
return s;
|
|
634
|
+
};
|
|
635
|
+
return ut.mergeWith({}, e, t, r);
|
|
636
|
+
}, Xt = { mergeConfigWithUserOverrides: Jt }, me = async ({
|
|
637
|
+
config: e,
|
|
638
|
+
preset: t
|
|
639
|
+
}) => {
|
|
640
|
+
try {
|
|
641
|
+
const { config: r } = t;
|
|
642
|
+
e.firewall && (delete r.applications, delete r.workloads, delete r.connectors, delete r.functions);
|
|
643
|
+
const n = Xt.mergeConfigWithUserOverrides(
|
|
644
|
+
r,
|
|
645
|
+
e
|
|
646
|
+
);
|
|
647
|
+
return n.build?.preset || (n.build = {
|
|
648
|
+
...n.build,
|
|
649
|
+
preset: t.metadata.name,
|
|
650
|
+
...!t.handler && e.build?.entry && {
|
|
651
|
+
entry: e.build?.entry
|
|
652
|
+
}
|
|
653
|
+
}), await we.readAzionConfig() || await we.writeUserConfig(n), n;
|
|
654
|
+
} catch (r) {
|
|
655
|
+
throw new Error(`Failed to set environment: ${r.message}`);
|
|
656
|
+
}
|
|
657
|
+
}, he = (e) => e ? typeof e == "string" ? [e] : Array.isArray(e) ? e : Object.values(e) : [], Yt = async ({
|
|
658
|
+
entrypoint: e,
|
|
659
|
+
preset: t
|
|
660
|
+
}) => {
|
|
661
|
+
if (e && !t.handler) {
|
|
662
|
+
const r = he(e), n = r.map((s) => S.resolve(s));
|
|
663
|
+
return await Promise.all(
|
|
664
|
+
n.map(async (s) => {
|
|
665
|
+
try {
|
|
666
|
+
await h.access(s);
|
|
667
|
+
} catch (o) {
|
|
668
|
+
throw g.error(o), new Error(
|
|
669
|
+
`Entry point "${s}" was not found. Please verify the path and try again.`
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
})
|
|
673
|
+
), A.feedback.build.info(
|
|
674
|
+
`Using entry point(s): ${r.map((s) => it(process.cwd(), s)).join(", ")}`
|
|
675
|
+
), n;
|
|
676
|
+
}
|
|
677
|
+
if (t.handler) {
|
|
678
|
+
const r = globalThis.bundler.root.includes("node_modules") ? S.resolve(globalThis.bundler.root, "../../") : S.resolve(globalThis.bundler.root, "node_modules"), n = S.resolve(
|
|
679
|
+
r,
|
|
680
|
+
"@aziontech/presets/dist/presets",
|
|
681
|
+
t.metadata.name,
|
|
682
|
+
"handler.js"
|
|
683
|
+
);
|
|
684
|
+
try {
|
|
685
|
+
await h.access(n);
|
|
686
|
+
} catch (s) {
|
|
687
|
+
throw g.error(s), new Error(
|
|
688
|
+
`Missing handler "${n}" (default for "${t.metadata.name}" preset). Either create this file or specify a custom entry point using --entry.`
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
return A.feedback.build.info(`Using built-in handler from "${t.metadata.name}" preset.`), [n];
|
|
692
|
+
}
|
|
693
|
+
if (t.config.build?.entry) {
|
|
694
|
+
const r = he(t.config.build.entry), n = r.map((s) => S.resolve(s));
|
|
695
|
+
return await Promise.all(
|
|
696
|
+
n.map(async (s) => {
|
|
697
|
+
try {
|
|
698
|
+
await h.access(s);
|
|
699
|
+
} catch (o) {
|
|
700
|
+
throw g.error(o), new Error(
|
|
701
|
+
`Missing default entry point "${s}" for "${t.metadata.name}" preset. Either create this file or specify a custom entry point using --entry.`
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
})
|
|
705
|
+
), A.feedback.build.info(`Using preset default entry: ${r.join(", ")}`), n;
|
|
706
|
+
}
|
|
707
|
+
throw new Error(
|
|
708
|
+
"Cannot determine entry point. Please specify one using --entry or in your configuration."
|
|
709
|
+
);
|
|
710
|
+
}, Zt = async ({ buildConfig: e, ctx: t }) => {
|
|
711
|
+
const { postbuild: r } = e.preset;
|
|
712
|
+
r && await r(e, { ...t });
|
|
713
|
+
}, Qt = async ({
|
|
714
|
+
namespace: e,
|
|
715
|
+
property: t,
|
|
716
|
+
dirs: r
|
|
717
|
+
}) => {
|
|
718
|
+
const n = {}, s = async (o) => {
|
|
719
|
+
await Promise.all(
|
|
720
|
+
o.map(async (i) => {
|
|
721
|
+
const c = await h.readdir(i);
|
|
722
|
+
await Promise.all(
|
|
723
|
+
c.map(async (a) => {
|
|
724
|
+
const l = C(i, a), f = await h.stat(l);
|
|
725
|
+
if (f.isDirectory())
|
|
726
|
+
await s([l]);
|
|
727
|
+
else if (f.isFile()) {
|
|
728
|
+
const u = await h.readFile(l);
|
|
729
|
+
let m = l;
|
|
730
|
+
l.startsWith("/") || (m = `/${l}`), n[m] = { content: u.toString("base64") };
|
|
731
|
+
}
|
|
732
|
+
})
|
|
733
|
+
);
|
|
734
|
+
})
|
|
735
|
+
);
|
|
736
|
+
};
|
|
737
|
+
return await s(r), `globalThis.${e}.${t}=${JSON.stringify(n)};`;
|
|
738
|
+
}, er = async ({ dirs: e, prefix: t, outputPath: r }) => {
|
|
739
|
+
await Promise.all(
|
|
740
|
+
e.map(async (n) => {
|
|
741
|
+
const s = t ? n.replace(t, "") : n, o = C(r, s);
|
|
742
|
+
await h.access(o).then(() => !0).catch(() => !1) || await h.mkdir(o, { recursive: !0 }), await h.cp(n, o, { recursive: !0 });
|
|
743
|
+
})
|
|
744
|
+
);
|
|
745
|
+
}, tr = ({ namespace: e, property: t, vars: r }) => Object.entries(r).reduce(
|
|
746
|
+
(n, [s, o]) => {
|
|
747
|
+
const i = t ? `${e}.${t}` : e;
|
|
748
|
+
return `${n} globalThis.${i}.${s}=${o};`;
|
|
749
|
+
},
|
|
750
|
+
t ? `globalThis.${e}.${t}={};` : `globalThis.${e}={};`
|
|
751
|
+
), rr = async ({
|
|
752
|
+
namespace: e,
|
|
753
|
+
property: t,
|
|
754
|
+
prefix: r
|
|
755
|
+
}) => `globalThis.${e} = { ...globalThis.${e}, ${t}: '${r && typeof r == "string" && r !== "" ? r : '""'}'};`, M = {
|
|
756
|
+
injectWorkerMemoryFiles: Qt,
|
|
757
|
+
copyFilesToLocalEdgeStorage: er,
|
|
758
|
+
injectWorkerGlobals: tr,
|
|
759
|
+
injectWorkerPathPrefix: rr
|
|
760
|
+
}, nr = {
|
|
761
|
+
filesToInject: [],
|
|
762
|
+
injection: {
|
|
763
|
+
globals: {}
|
|
764
|
+
},
|
|
765
|
+
bundler: {
|
|
766
|
+
defineVars: {},
|
|
767
|
+
plugins: []
|
|
768
|
+
}
|
|
769
|
+
}, sr = async ({
|
|
770
|
+
buildConfig: e,
|
|
771
|
+
ctx: t
|
|
772
|
+
}) => {
|
|
773
|
+
const r = await e.preset.prebuild?.(e, t) || nr, n = M.injectWorkerGlobals({
|
|
774
|
+
namespace: k.NAMESPACE,
|
|
775
|
+
// Transform globals object:
|
|
776
|
+
// 1. Convert object to entries
|
|
777
|
+
// 2. Remove any entries with undefined values
|
|
778
|
+
// 3. Ensure remaining values are typed as string
|
|
779
|
+
vars: Object.fromEntries(
|
|
780
|
+
Object.entries(r.injection?.globals || {}).filter(([c, a]) => a !== void 0).map(([c, a]) => [c, a])
|
|
781
|
+
)
|
|
782
|
+
}), s = await M.injectWorkerMemoryFiles({
|
|
783
|
+
namespace: k.NAMESPACE,
|
|
784
|
+
property: "__FILES__",
|
|
785
|
+
dirs: e.memoryFS?.injectionDirs || []
|
|
786
|
+
});
|
|
787
|
+
e.memoryFS && M.copyFilesToLocalEdgeStorage({
|
|
788
|
+
dirs: e.memoryFS?.injectionDirs,
|
|
789
|
+
prefix: e.memoryFS?.removePathPrefix,
|
|
790
|
+
outputPath: O.OUTPUT_STORAGE_PATH
|
|
791
|
+
});
|
|
792
|
+
const o = await M.injectWorkerPathPrefix({
|
|
793
|
+
namespace: k.NAMESPACE,
|
|
794
|
+
property: "FS_PATH_PREFIX_TO_REMOVE",
|
|
795
|
+
prefix: e.memoryFS?.removePathPrefix || ""
|
|
796
|
+
}), i = `${n}${o}
|
|
797
|
+
${s}`;
|
|
798
|
+
return {
|
|
799
|
+
filesToInject: r.filesToInject || [],
|
|
800
|
+
injection: {
|
|
801
|
+
globals: r.injection?.globals || {},
|
|
802
|
+
entry: "",
|
|
803
|
+
banner: i
|
|
804
|
+
},
|
|
805
|
+
bundler: {
|
|
806
|
+
defineVars: r.bundler?.defineVars || {},
|
|
807
|
+
plugins: r.bundler?.plugins || []
|
|
808
|
+
}
|
|
809
|
+
};
|
|
810
|
+
}, or = ({ projectDir: e = process.cwd() }) => gt({
|
|
811
|
+
projectDir: e
|
|
812
|
+
}), ir = { listFrameworks: or };
|
|
813
|
+
async function ar() {
|
|
814
|
+
try {
|
|
815
|
+
const e = await ir.listFrameworks({
|
|
816
|
+
projectDir: process.cwd()
|
|
817
|
+
});
|
|
818
|
+
if (e[0]?.id && Object.values(K).some(
|
|
819
|
+
(i) => i.metadata?.name === e[0].id
|
|
820
|
+
))
|
|
821
|
+
return e[0].id;
|
|
822
|
+
const t = C(process.cwd(), "tsconfig.json");
|
|
823
|
+
return de.existsSync(t) || de.readdirSync(process.cwd()).some((o) => [".ts", ".tsx"].includes(q(o))) ? "typescript" : "javascript";
|
|
824
|
+
} catch {
|
|
825
|
+
return "javascript";
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
const cr = { inferPreset: ar }, lr = (e) => Object.values(K).find((t) => t.metadata?.name === e), fr = async (e) => {
|
|
829
|
+
if (e) {
|
|
830
|
+
const t = typeof e == "string" ? e : e.metadata?.name;
|
|
831
|
+
A.feedback.build.info(`Using preset: ${t}`);
|
|
832
|
+
}
|
|
833
|
+
if (e || (A.feedback.build.info("No preset specified, using automatic detection..."), e = await cr.inferPreset(), A.feedback.build.info(`Detected preset: ${e}`)), typeof e == "string") {
|
|
834
|
+
const t = lr(e);
|
|
835
|
+
if (!t)
|
|
836
|
+
throw new Error(`Invalid build preset name: '${e}'`);
|
|
837
|
+
return t;
|
|
838
|
+
}
|
|
839
|
+
return e;
|
|
840
|
+
}, ur = () => `${Date.now()}`, dr = async (e) => {
|
|
841
|
+
try {
|
|
842
|
+
return await h.access(e), (await h.stat(e)).isDirectory();
|
|
843
|
+
} catch {
|
|
844
|
+
return !1;
|
|
845
|
+
}
|
|
846
|
+
}, pr = async (e, t) => {
|
|
847
|
+
try {
|
|
848
|
+
const r = O.OUTPUT_STORAGE_METADATA_PATH;
|
|
849
|
+
let n = [];
|
|
850
|
+
try {
|
|
851
|
+
const o = await h.readFile(r, "utf-8");
|
|
852
|
+
n = JSON.parse(o);
|
|
853
|
+
} catch {
|
|
854
|
+
g.info("Creating new storage metadata file");
|
|
855
|
+
}
|
|
856
|
+
const s = {
|
|
857
|
+
...t,
|
|
858
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
859
|
+
};
|
|
860
|
+
n = n.filter((o) => o.name !== e), n.push(s), await h.writeFile(r, JSON.stringify(n, null, 2), "utf-8"), g.info(`Storage metadata saved for: ${e}`);
|
|
861
|
+
} catch (r) {
|
|
862
|
+
throw g.error(`Failed to save storage metadata for ${e}:`, r), r;
|
|
863
|
+
}
|
|
864
|
+
}, mr = async (e, t, r, n) => {
|
|
865
|
+
const s = e.name;
|
|
866
|
+
try {
|
|
867
|
+
const o = S.join(r, s, n);
|
|
868
|
+
await h.mkdir(S.join(r, s), { recursive: !0 });
|
|
869
|
+
try {
|
|
870
|
+
const i = await h.readdir(S.join(r, s));
|
|
871
|
+
await Promise.all(
|
|
872
|
+
i.filter((c) => c !== n).map(async (c) => {
|
|
873
|
+
const a = S.join(r, s, c);
|
|
874
|
+
try {
|
|
875
|
+
const l = await h.lstat(a);
|
|
876
|
+
l.isSymbolicLink() ? await h.unlink(a) : l.isDirectory() ? await h.rmdir(a, { recursive: !0 }) : await h.unlink(a);
|
|
877
|
+
} catch (l) {
|
|
878
|
+
g.warn(`Failed to remove ${a}:`, l);
|
|
879
|
+
}
|
|
880
|
+
})
|
|
881
|
+
), g.info(`Cleaned up existing storage items in: ${S.join(r, s)}`);
|
|
882
|
+
} catch {
|
|
883
|
+
g.warn(`Storage directory not found or empty: ${S.join(r, s)}`);
|
|
884
|
+
}
|
|
885
|
+
try {
|
|
886
|
+
const i = await h.lstat(o);
|
|
887
|
+
i.isSymbolicLink() ? (await h.unlink(o), g.info(`Removed existing symlink: ${o}`)) : i.isDirectory() && (await h.rmdir(o, { recursive: !0 }), g.info(`Removed existing directory: ${o}`));
|
|
888
|
+
} catch {
|
|
889
|
+
}
|
|
890
|
+
await h.symlink(t, o, "dir"), g.info(`Storage link created: ${s} -> ${t}`), await pr(s, {
|
|
891
|
+
name: s,
|
|
892
|
+
workloadsAccess: e.workloadsAccess || "read_only",
|
|
893
|
+
sourceDir: t,
|
|
894
|
+
targetDir: o,
|
|
895
|
+
prefix: n
|
|
896
|
+
});
|
|
897
|
+
} catch (o) {
|
|
898
|
+
throw g.error(`Failed to create storage link for ${s}:`, o), o;
|
|
899
|
+
}
|
|
900
|
+
}, hr = (e) => !e.name || !e.dir ? (g.warn("Storage configuration is missing required fields (name or dir)"), !1) : !0, gr = async ({ config: e }) => {
|
|
901
|
+
try {
|
|
902
|
+
const t = e.storage || [], r = [];
|
|
903
|
+
if (t.length === 0)
|
|
904
|
+
return g.info("No storages found to setup"), r;
|
|
905
|
+
await h.mkdir(O.OUTPUT_STORAGE_PATH, { recursive: !0 });
|
|
906
|
+
for (const n of t) {
|
|
907
|
+
if (!hr(n))
|
|
908
|
+
continue;
|
|
909
|
+
const s = S.resolve(process.cwd(), n.dir);
|
|
910
|
+
if (!await dr(s))
|
|
911
|
+
throw new Error(
|
|
912
|
+
`Storage directory not found: ${s}.
|
|
913
|
+
- Please check the path provided in the azion.config file on storage[].dir`
|
|
914
|
+
);
|
|
915
|
+
const o = n.prefix, i = o || ur();
|
|
916
|
+
o || p.storage.info(
|
|
917
|
+
`No prefix provided for storage '${n.name}', generating version prefix: ${i}`
|
|
918
|
+
), o && p.storage.info(`Using provided prefix for storage '${n.name}': ${i}`), await mr(n, s, O.OUTPUT_STORAGE_PATH, i), r.push({
|
|
919
|
+
...n,
|
|
920
|
+
prefix: i
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
return g.info("Storage setup completed successfully"), r;
|
|
924
|
+
} catch (t) {
|
|
925
|
+
throw g.error("Failed to setup storages:", t), t;
|
|
926
|
+
}
|
|
927
|
+
}, Y = {
|
|
928
|
+
LEGACY_DEPRECATION: "DEPRECATED: Migrate handler to → export default { fetch: (request, env, ctx) => {...} }",
|
|
929
|
+
UNSUPPORTED_PATTERN_DETECTED: "Unsupported handler pattern detected. Generating Service Worker wrapper as fallback.",
|
|
930
|
+
UNSUPPORTED_PATTERN_SUGGESTIONS: "Consider updating your handler to use ES Modules pattern."
|
|
931
|
+
}, L = `
|
|
932
|
+
const request = event.request;
|
|
933
|
+
const env = {};
|
|
934
|
+
const ctx = { waitUntil: event.waitUntil?.bind(event) };`, Z = {
|
|
935
|
+
baseImport: (e) => `
|
|
936
|
+
import module from '${e}';
|
|
937
|
+
|
|
938
|
+
// Object export pattern: export default { fetch, firewall }
|
|
939
|
+
const handlers = module.default || module;`,
|
|
940
|
+
contextSetup: L,
|
|
941
|
+
firewallHandler: `
|
|
942
|
+
const firewallHandler = handlers.firewall;
|
|
943
|
+
addEventListener('firewall', (event) => {
|
|
944
|
+
(async () => {${L}
|
|
945
|
+
|
|
946
|
+
await firewallHandler(request, env, ctx);
|
|
947
|
+
})().catch(console.error);
|
|
948
|
+
});`,
|
|
949
|
+
fetchHandler: (e) => `
|
|
950
|
+
${e ? "export default module;" : `
|
|
951
|
+
const fetchHandler = handlers.fetch;
|
|
952
|
+
addEventListener('fetch', (event) => {${L}
|
|
953
|
+
event.respondWith((async () => {
|
|
954
|
+
return fetchHandler(request, env, ctx);
|
|
955
|
+
})());
|
|
956
|
+
});`}
|
|
957
|
+
`,
|
|
958
|
+
fallbackHandler: (e) => `${`
|
|
959
|
+
import module from '${e}';
|
|
960
|
+
|
|
961
|
+
// Object export pattern: export default { fetch, firewall }
|
|
962
|
+
const handlers = module.default || module;`}
|
|
963
|
+
const fetchHandler = handlers.fetch;
|
|
964
|
+
|
|
965
|
+
if (fetchHandler) {
|
|
966
|
+
addEventListener('fetch', (event) => {${L}
|
|
967
|
+
|
|
968
|
+
event.respondWith((async () => {
|
|
969
|
+
return fetchHandler(request, env, ctx);
|
|
970
|
+
})());
|
|
971
|
+
});
|
|
972
|
+
} else {
|
|
973
|
+
throw new Error("No fetch handler found in default export object.");
|
|
974
|
+
}`
|
|
975
|
+
}, Fe = (e) => {
|
|
976
|
+
const t = e.split(`
|
|
977
|
+
`), r = /addEventListener\s*\(\s*['"`](fetch|firewall)['"`]\s*,/;
|
|
978
|
+
return t.some((n) => {
|
|
979
|
+
const s = n.trim();
|
|
980
|
+
return s.startsWith("//") || s.startsWith("/*") ? !1 : r.test(n);
|
|
981
|
+
});
|
|
982
|
+
}, yr = async (e) => {
|
|
983
|
+
try {
|
|
984
|
+
const t = await import(e), r = t.default || t;
|
|
985
|
+
return {
|
|
986
|
+
hasFirewall: !!r.firewall,
|
|
987
|
+
hasFetch: !!r.fetch
|
|
988
|
+
};
|
|
989
|
+
} catch {
|
|
990
|
+
return {
|
|
991
|
+
hasFirewall: !1,
|
|
992
|
+
hasFetch: !0
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
}, wr = async (e, t) => {
|
|
996
|
+
const { hasFirewall: r, hasFetch: n } = await yr(e);
|
|
997
|
+
if (!r && !n)
|
|
998
|
+
return `// No fetch or firewall handlers found in: ${e}
|
|
999
|
+
// The original file will be used as-is in production mode.
|
|
1000
|
+
// Consider adding: export default { fetch: (request, env, ctx) => { ... } }
|
|
1001
|
+
|
|
1002
|
+
console.warn('No Edge Function handlers found. File will run as-is.');`;
|
|
1003
|
+
const s = [Z.baseImport(e)];
|
|
1004
|
+
return r && s.push(Z.firewallHandler), n && s.push(Z.fetchHandler(t)), s.join(`
|
|
1005
|
+
`);
|
|
1006
|
+
}, br = (e) => `
|
|
1007
|
+
import handler from '${e}';
|
|
1008
|
+
|
|
1009
|
+
// Legacy pattern wrapper: export default function → addEventListener
|
|
1010
|
+
addEventListener('fetch', (event) => {
|
|
1011
|
+
event.respondWith((async () => {
|
|
1012
|
+
return await handler(event);
|
|
1013
|
+
})());
|
|
1014
|
+
});
|
|
1015
|
+
`, ce = (e) => /export\s+default\s*\{[\s\S]*fetch[\s\S]*\}/.test(e), Ae = (e) => !(!/export\s+default\s+/.test(e) || ce(e)), Er = async (e) => {
|
|
1016
|
+
try {
|
|
1017
|
+
const t = await import(e);
|
|
1018
|
+
return typeof t.default > "u" && !t.fetch && !t.firewall ? "serviceWorker" : t.default && typeof t.default == "object" && (t.default.fetch || t.default.firewall) ? "ESModules" : t.default ? "legacy" : "unsupported";
|
|
1019
|
+
} catch {
|
|
1020
|
+
const r = await (await import("fs/promises")).readFile(e, "utf-8");
|
|
1021
|
+
return Fe(r) ? "serviceWorker" : ce(r) ? "ESModules" : Ae(r) ? "legacy" : "unsupported";
|
|
1022
|
+
}
|
|
1023
|
+
}, vr = (e) => typeof e == "string" ? [e] : Array.isArray(e) ? e : Object.values(e), Tr = (e) => Fe(e) ? "serviceWorker" : ce(e) ? "ESModules" : Ae(e) ? "legacy" : "unsupported", Sr = async (e, t, r) => {
|
|
1024
|
+
let n;
|
|
1025
|
+
try {
|
|
1026
|
+
n = await Er(t);
|
|
1027
|
+
} catch {
|
|
1028
|
+
n = Tr(e);
|
|
1029
|
+
}
|
|
1030
|
+
switch (n) {
|
|
1031
|
+
case "serviceWorker":
|
|
1032
|
+
return e;
|
|
1033
|
+
case "ESModules":
|
|
1034
|
+
return await wr(t, r);
|
|
1035
|
+
case "legacy":
|
|
1036
|
+
return p.build.warn(Y.LEGACY_DEPRECATION), br(t);
|
|
1037
|
+
default:
|
|
1038
|
+
return p.build.warn(Y.UNSUPPORTED_PATTERN_DETECTED), p.build.info(Y.UNSUPPORTED_PATTERN_SUGGESTIONS), e;
|
|
1039
|
+
}
|
|
1040
|
+
}, $r = async (e, t) => {
|
|
1041
|
+
try {
|
|
1042
|
+
const r = vr(t.handler), n = e.entry || {}, s = await Promise.all(
|
|
1043
|
+
r.map(async (o, i) => {
|
|
1044
|
+
const c = Object.values(n)[i], a = await h.readFile(o, "utf-8"), l = await Sr(a, o, t.production);
|
|
1045
|
+
return [c, l];
|
|
1046
|
+
})
|
|
1047
|
+
);
|
|
1048
|
+
return Object.fromEntries(s);
|
|
1049
|
+
} catch (r) {
|
|
1050
|
+
throw new Error(
|
|
1051
|
+
`Failed to setup worker code: ${r instanceof Error ? r.message : String(r)}`
|
|
1052
|
+
);
|
|
1053
|
+
}
|
|
1054
|
+
}, Pr = (e) => e === !0 ? !0 : process.env[R.ENV_ENABLED] === "true", xr = () => {
|
|
1055
|
+
const e = process.env[R.ENV_FORMAT];
|
|
1056
|
+
return e === "console" || e === "json" || e === "html" || e === "both" ? e : R.DEFAULT_FORMAT;
|
|
1057
|
+
}, kr = () => process.env[R.ENV_OUTPUT] || R.DEFAULT_OUTPUT_PATH, Cr = () => R.DEFAULT_HTML_OUTPUT_PATH, Fr = async (e) => {
|
|
1058
|
+
const { config: t, options: r } = e, n = !!r.production, s = Pr(r.telemetry), o = t.build?.bundler || "esbuild", i = {
|
|
1059
|
+
enabled: s,
|
|
1060
|
+
outputFormat: xr(),
|
|
1061
|
+
outputPath: kr(),
|
|
1062
|
+
htmlOutputPath: Cr()
|
|
1063
|
+
}, c = {
|
|
1064
|
+
bundlerVersion: k.VERSION,
|
|
1065
|
+
preset: t.build?.preset?.toString() || "javascript",
|
|
1066
|
+
bundler: o,
|
|
1067
|
+
production: n,
|
|
1068
|
+
entry: typeof t.build?.entry == "string" || Array.isArray(t.build?.entry) ? t.build.entry : t.build?.entry ? Object.values(t.build.entry) : ["./main.js"]
|
|
1069
|
+
}, a = new Ze(i, c);
|
|
1070
|
+
try {
|
|
1071
|
+
const l = a.startSpan("build", {
|
|
1072
|
+
preset: c.preset,
|
|
1073
|
+
bundler: c.bundler,
|
|
1074
|
+
production: c.production
|
|
1075
|
+
});
|
|
1076
|
+
s && i.outputFormat !== "json" && (console.log(Qe(k.VERSION)), console.log(et(c)), console.log(`
|
|
1077
|
+
⚙️ Starting build pipeline...
|
|
1078
|
+
`)), r.skipFrameworkBuild && p.build.warn("Skipping framework build"), await x(
|
|
1079
|
+
a,
|
|
1080
|
+
s,
|
|
1081
|
+
i.outputFormat,
|
|
1082
|
+
"dependencies-check",
|
|
1083
|
+
"Dependencies Check",
|
|
1084
|
+
async () => At()
|
|
1085
|
+
);
|
|
1086
|
+
const f = await x(
|
|
1087
|
+
a,
|
|
1088
|
+
s,
|
|
1089
|
+
i.outputFormat,
|
|
1090
|
+
"preset-resolution",
|
|
1091
|
+
"Preset Resolution",
|
|
1092
|
+
async () => fr(t.build?.preset)
|
|
1093
|
+
), u = await x(
|
|
1094
|
+
a,
|
|
1095
|
+
s,
|
|
1096
|
+
i.outputFormat,
|
|
1097
|
+
"build-config-setup",
|
|
1098
|
+
"Build Config Setup",
|
|
1099
|
+
async () => Wt(t, f, n)
|
|
1100
|
+
);
|
|
1101
|
+
if (r.onlyGenerateConfig) {
|
|
1102
|
+
const E = await me({
|
|
1103
|
+
config: t,
|
|
1104
|
+
preset: f,
|
|
1105
|
+
ctx: {
|
|
1106
|
+
production: n ?? _.PRODUCTION,
|
|
1107
|
+
skipFrameworkBuild: !!r.skipFrameworkBuild,
|
|
1108
|
+
handler: ""
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
return p.build.success("Build completed successfully with only azion.config"), te(E), a.endSpan(l), a.printToConsole(), await a.saveReport(), {
|
|
1112
|
+
config: E,
|
|
1113
|
+
ctx: {
|
|
1114
|
+
production: n ?? _.PRODUCTION,
|
|
1115
|
+
skipFrameworkBuild: !!r.skipFrameworkBuild,
|
|
1116
|
+
handler: ""
|
|
1117
|
+
},
|
|
1118
|
+
setup: u
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
let m = {
|
|
1122
|
+
production: n ?? _.PRODUCTION,
|
|
1123
|
+
skipFrameworkBuild: !!r.skipFrameworkBuild,
|
|
1124
|
+
handler: ""
|
|
1125
|
+
};
|
|
1126
|
+
const b = await x(
|
|
1127
|
+
a,
|
|
1128
|
+
s,
|
|
1129
|
+
i.outputFormat,
|
|
1130
|
+
"environment-setup",
|
|
1131
|
+
"Environment Setup",
|
|
1132
|
+
async () => {
|
|
1133
|
+
const E = await me({
|
|
1134
|
+
config: t,
|
|
1135
|
+
preset: f,
|
|
1136
|
+
ctx: m
|
|
1137
|
+
});
|
|
1138
|
+
return te(E), E;
|
|
1139
|
+
}
|
|
1140
|
+
);
|
|
1141
|
+
p.prebuild.info("Starting pre-build...");
|
|
1142
|
+
const d = await x(
|
|
1143
|
+
a,
|
|
1144
|
+
s,
|
|
1145
|
+
i.outputFormat,
|
|
1146
|
+
"prebuild",
|
|
1147
|
+
"Prebuild",
|
|
1148
|
+
async () => sr({
|
|
1149
|
+
buildConfig: u,
|
|
1150
|
+
ctx: m
|
|
1151
|
+
})
|
|
1152
|
+
);
|
|
1153
|
+
p.prebuild.info("Pre-build completed successfully");
|
|
1154
|
+
const y = await x(
|
|
1155
|
+
a,
|
|
1156
|
+
s,
|
|
1157
|
+
i.outputFormat,
|
|
1158
|
+
"handler-resolution",
|
|
1159
|
+
"Handler Resolution",
|
|
1160
|
+
async () => Yt({
|
|
1161
|
+
entrypoint: t.build?.entry,
|
|
1162
|
+
preset: f
|
|
1163
|
+
})
|
|
1164
|
+
);
|
|
1165
|
+
m = {
|
|
1166
|
+
production: n ?? _.PRODUCTION,
|
|
1167
|
+
handler: y,
|
|
1168
|
+
skipFrameworkBuild: !!r.skipFrameworkBuild
|
|
1169
|
+
}, await x(
|
|
1170
|
+
a,
|
|
1171
|
+
s,
|
|
1172
|
+
i.outputFormat,
|
|
1173
|
+
"worker-setup",
|
|
1174
|
+
"Worker Setup",
|
|
1175
|
+
async () => {
|
|
1176
|
+
const E = await $r(u, m), w = [];
|
|
1177
|
+
await Promise.all(
|
|
1178
|
+
Object.entries(E).map(async ([v, j]) => {
|
|
1179
|
+
await Se($e(v), { recursive: !0 }), await ot(v, j, "utf-8"), w.push(v);
|
|
1180
|
+
})
|
|
1181
|
+
);
|
|
1182
|
+
for (const v of w)
|
|
1183
|
+
await Ge(v);
|
|
1184
|
+
return E;
|
|
1185
|
+
}
|
|
1186
|
+
), p.build.info("Starting build..."), await x(
|
|
1187
|
+
a,
|
|
1188
|
+
s,
|
|
1189
|
+
i.outputFormat,
|
|
1190
|
+
"core-build",
|
|
1191
|
+
"Core Build",
|
|
1192
|
+
async () => qt({
|
|
1193
|
+
buildConfig: u,
|
|
1194
|
+
prebuildResult: d,
|
|
1195
|
+
ctx: m,
|
|
1196
|
+
telemetry: a,
|
|
1197
|
+
telemetryEnabled: s,
|
|
1198
|
+
telemetryOutputFormat: i.outputFormat
|
|
1199
|
+
})
|
|
1200
|
+
), p.build.success("Build completed successfully"), await qe(), p.postbuild.info("Starting post-build..."), await x(
|
|
1201
|
+
a,
|
|
1202
|
+
s,
|
|
1203
|
+
i.outputFormat,
|
|
1204
|
+
"postbuild",
|
|
1205
|
+
"Postbuild",
|
|
1206
|
+
async () => {
|
|
1207
|
+
await Zt({ buildConfig: u, ctx: m });
|
|
1208
|
+
}
|
|
1209
|
+
), p.postbuild.success("Post-build completed successfully");
|
|
1210
|
+
const T = await x(
|
|
1211
|
+
a,
|
|
1212
|
+
s,
|
|
1213
|
+
i.outputFormat,
|
|
1214
|
+
"storage-setup",
|
|
1215
|
+
"Storage Setup",
|
|
1216
|
+
async () => gr({ config: b })
|
|
1217
|
+
);
|
|
1218
|
+
return await x(
|
|
1219
|
+
a,
|
|
1220
|
+
s,
|
|
1221
|
+
i.outputFormat,
|
|
1222
|
+
"bindings-setup",
|
|
1223
|
+
"Bindings Setup",
|
|
1224
|
+
async () => {
|
|
1225
|
+
await It({ config: b, storageSetup: T, isProduction: n });
|
|
1226
|
+
}
|
|
1227
|
+
), await x(
|
|
1228
|
+
a,
|
|
1229
|
+
s,
|
|
1230
|
+
i.outputFormat,
|
|
1231
|
+
"env-vars-copy",
|
|
1232
|
+
"Copy Env Vars",
|
|
1233
|
+
async () => {
|
|
1234
|
+
await Ke();
|
|
1235
|
+
}
|
|
1236
|
+
), a.endSpan(l), s && (a.printToConsole(), await a.saveReport()), {
|
|
1237
|
+
config: b,
|
|
1238
|
+
ctx: m,
|
|
1239
|
+
setup: u
|
|
1240
|
+
};
|
|
1241
|
+
} catch (l) {
|
|
1242
|
+
g.error("Build process failed:", l), p.build.error(
|
|
1243
|
+
`${l instanceof Error ? l.message : String(l)}${U}`
|
|
1244
|
+
), a.markFailed(l instanceof Error ? l.message : String(l)), s && (a.printToConsole(), await a.saveReport()), process.exit(1);
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
async function Ar(e) {
|
|
1248
|
+
const t = await se() || {}, { build: r } = t, n = {
|
|
1249
|
+
preset: B({
|
|
1250
|
+
inputValue: e.preset,
|
|
1251
|
+
fileValue: r?.preset,
|
|
1252
|
+
defaultValue: _.PRESET
|
|
1253
|
+
}),
|
|
1254
|
+
entry: B({
|
|
1255
|
+
inputValue: e.entry,
|
|
1256
|
+
fileValue: r?.entry,
|
|
1257
|
+
defaultValue: _.ENTRY
|
|
1258
|
+
}),
|
|
1259
|
+
bundler: B({
|
|
1260
|
+
inputValue: void 0,
|
|
1261
|
+
fileValue: r?.bundler,
|
|
1262
|
+
defaultValue: _.BUNDLER
|
|
1263
|
+
}),
|
|
1264
|
+
polyfills: B({
|
|
1265
|
+
inputValue: r?.polyfills,
|
|
1266
|
+
fileValue: e.polyfills,
|
|
1267
|
+
defaultValue: _.POLYFILLS
|
|
1268
|
+
})
|
|
1269
|
+
}, s = {
|
|
1270
|
+
...t,
|
|
1271
|
+
build: {
|
|
1272
|
+
...n,
|
|
1273
|
+
memoryFS: t?.build?.memoryFS,
|
|
1274
|
+
extend: t?.build?.extend
|
|
1275
|
+
}
|
|
1276
|
+
};
|
|
1277
|
+
return e.production && await _t([O.OUTPUT_BASE_PATH]), Fr({
|
|
1278
|
+
config: s,
|
|
1279
|
+
options: {
|
|
1280
|
+
production: e.production,
|
|
1281
|
+
skipFrameworkBuild: e.skipFrameworkBuild,
|
|
1282
|
+
onlyGenerateConfig: e.onlyGenerateConfig,
|
|
1283
|
+
telemetry: e.telemetry
|
|
1284
|
+
}
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
let Q, G = !1;
|
|
1288
|
+
const _r = (e, t, r, n = !0) => {
|
|
1289
|
+
let s = r;
|
|
1290
|
+
const o = new RegExp(`addEventListener\\((['"]?)${e}\\1,`, "g"), i = /firewall:\s*\(event\)\s*=>\s*{/g, c = !!r.match(i), a = /eventType\s*=\s*['"]firewall['"];/g, l = !!r.match(o);
|
|
1291
|
+
return (n && l || c) && (s = r.replace(o, `addEventListener("${t}",`), c && (s = s.replace(a, "eventType = 'fetch';"))), { matchEvent: l || c, codeChanged: s };
|
|
1292
|
+
};
|
|
1293
|
+
function Or(e) {
|
|
1294
|
+
return new Promise((t) => {
|
|
1295
|
+
const r = new pt.Socket();
|
|
1296
|
+
r.setTimeout(1e3), r.on("connect", () => {
|
|
1297
|
+
r.destroy(), t(!0);
|
|
1298
|
+
}), r.on("timeout", () => {
|
|
1299
|
+
r.destroy(), t(!1);
|
|
1300
|
+
}), r.on("error", (n) => {
|
|
1301
|
+
r.destroy(), n.message.includes("ECONNREFUSED") ? t(!1) : t(!0);
|
|
1302
|
+
}), r.connect(e, "127.0.0.1");
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
async function jr(e) {
|
|
1306
|
+
try {
|
|
1307
|
+
return await h.access(e), await h.readFile(e, "utf8");
|
|
1308
|
+
} catch (t) {
|
|
1309
|
+
if (t.message.includes("ENOENT")) {
|
|
1310
|
+
const r = re(e);
|
|
1311
|
+
throw new Error(
|
|
1312
|
+
`Server entry file "${r}" not found. Please specify your entry point using "azion dev <path>" or create the default handler file.${U}`
|
|
1313
|
+
);
|
|
1314
|
+
}
|
|
1315
|
+
throw new Error(`Error reading file ${e}: ${t.message}${U}`);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
async function Ur(e, t) {
|
|
1319
|
+
const { matchEvent: r, codeChanged: n } = (
|
|
1320
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1321
|
+
_r("firewall", "fetch", t)
|
|
1322
|
+
), o = ke(r ? n : t, r);
|
|
1323
|
+
return mt({ port: e, host: "0.0.0.0", runtime: o });
|
|
1324
|
+
}
|
|
1325
|
+
function ge(e, t) {
|
|
1326
|
+
const r = e?.bindings?.storage?.bucket || t?.storage?.[0].name || "", n = e?.bindings?.storage?.prefix || t?.storage?.[0].prefix || "";
|
|
1327
|
+
return { bucketName: r, prefix: n };
|
|
1328
|
+
}
|
|
1329
|
+
function Rr(e, t, r) {
|
|
1330
|
+
const n = Object.keys(e).find((s) => {
|
|
1331
|
+
const o = s.replace(/\.dev$/, "").replace(/\.edge\//, "");
|
|
1332
|
+
return r.replace(/\.js$/, "").endsWith(o);
|
|
1333
|
+
});
|
|
1334
|
+
if (!n)
|
|
1335
|
+
throw new Error(`Entry path not found for function "${t}"`);
|
|
1336
|
+
return n;
|
|
1337
|
+
}
|
|
1338
|
+
function ye(e) {
|
|
1339
|
+
return e.endsWith(".js") ? e : `${e}.js`;
|
|
1340
|
+
}
|
|
1341
|
+
function Nr(e, t, r) {
|
|
1342
|
+
if (!e || Object.keys(e).length === 0)
|
|
1343
|
+
throw new Error("No entries provided");
|
|
1344
|
+
if (r) {
|
|
1345
|
+
const i = t.functions?.find((l) => l.name === r);
|
|
1346
|
+
if (!i)
|
|
1347
|
+
throw new Error(`Function "${r}" not found in edge functions configuration`);
|
|
1348
|
+
const c = Rr(e, r, i.path), a = ge(i, t);
|
|
1349
|
+
return {
|
|
1350
|
+
name: i.name,
|
|
1351
|
+
bucket: a.bucketName,
|
|
1352
|
+
prefix: a.prefix,
|
|
1353
|
+
path: ye(c)
|
|
1354
|
+
};
|
|
1355
|
+
}
|
|
1356
|
+
const n = Object.keys(e)[0], s = t.functions?.[0], o = ge(s, t);
|
|
1357
|
+
return {
|
|
1358
|
+
name: s?.name,
|
|
1359
|
+
bucket: o.bucketName,
|
|
1360
|
+
prefix: o.prefix,
|
|
1361
|
+
path: ye(n)
|
|
1362
|
+
};
|
|
1363
|
+
}
|
|
1364
|
+
async function le(e, t, r = !1, n) {
|
|
1365
|
+
try {
|
|
1366
|
+
Q && await Q.close();
|
|
1367
|
+
const {
|
|
1368
|
+
setup: { entry: s },
|
|
1369
|
+
config: { functions: o, storage: i }
|
|
1370
|
+
} = await Ar({ production: !1, skipFrameworkBuild: r });
|
|
1371
|
+
let c;
|
|
1372
|
+
try {
|
|
1373
|
+
const {
|
|
1374
|
+
path: a,
|
|
1375
|
+
bucket: l,
|
|
1376
|
+
prefix: f
|
|
1377
|
+
} = Nr(s, { functions: o, storage: i }, n);
|
|
1378
|
+
globalThis.AZION_BUCKET_NAME = l, globalThis.AZION_BUCKET_PREFIX = f, c = await jr(a);
|
|
1379
|
+
} catch (a) {
|
|
1380
|
+
p.server.error(a.message), g.error(`Error reading worker file: ${a}`), process.exit(1);
|
|
1381
|
+
}
|
|
1382
|
+
try {
|
|
1383
|
+
Q = await Ur(t, c), p.server.success(
|
|
1384
|
+
`Function running on port 0.0.0.0:${t}, url: http://localhost:${t}`
|
|
1385
|
+
);
|
|
1386
|
+
} catch (a) {
|
|
1387
|
+
if (a.message.includes("EADDRINUSE"))
|
|
1388
|
+
await le(e, t + 1);
|
|
1389
|
+
else
|
|
1390
|
+
throw a;
|
|
1391
|
+
}
|
|
1392
|
+
} catch (s) {
|
|
1393
|
+
p.server.error(
|
|
1394
|
+
`${s instanceof Error ? s.message : String(s)}${U}`
|
|
1395
|
+
), g.error(`Server management error: ${s}`), process.exit(1);
|
|
1396
|
+
} finally {
|
|
1397
|
+
G = !1;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
async function Ir(e, t, r) {
|
|
1401
|
+
if (!G && !(e.startsWith(".azion-bundler") || e.startsWith("azion") && e.includes(".temp") || e.startsWith(".edge") || e.startsWith("node_modules") || e.startsWith(".vercel"))) {
|
|
1402
|
+
G = !0;
|
|
1403
|
+
try {
|
|
1404
|
+
p.build.info("Rebuilding with the new changes..."), await le(t, r);
|
|
1405
|
+
} catch (n) {
|
|
1406
|
+
g.error(`Build or server restart failed: ${n}`);
|
|
1407
|
+
} finally {
|
|
1408
|
+
G = !1;
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
async function _e(e, t, r = !1, n) {
|
|
1413
|
+
await Or(t) && (p.server.error(`Port ${t} is in use. Please choose another port.`), process.exit(1)), await le(e, t, r, n);
|
|
1414
|
+
const o = Ye.watch("./", {
|
|
1415
|
+
persistent: !0,
|
|
1416
|
+
ignoreInitial: !0,
|
|
1417
|
+
// Ignore the initial add events
|
|
1418
|
+
depth: 99,
|
|
1419
|
+
ignored: [".git", ".vscode", ".idea", ".sublime-text", ".history"]
|
|
1420
|
+
// Added common IDE-related folders
|
|
1421
|
+
}), i = async (c) => {
|
|
1422
|
+
await Ir(c, e, t);
|
|
1423
|
+
};
|
|
1424
|
+
o.on("add", i).on("change", i).on("unlink", i).on("addDir", i).on("unlinkDir", i).on("error", (c) => g.error(`Watcher error: ${c}`)).on("ready", () => p.server.info("Initial scan complete. Ready for changes."));
|
|
1425
|
+
}
|
|
1426
|
+
const we = {
|
|
1427
|
+
runtime: ke,
|
|
1428
|
+
server: _e,
|
|
1429
|
+
readStore: Je,
|
|
1430
|
+
readAzionConfig: se,
|
|
1431
|
+
writeStore: ve,
|
|
1432
|
+
writeUserConfig: ee
|
|
1433
|
+
};
|
|
1434
|
+
async function $n({
|
|
1435
|
+
entry: e,
|
|
1436
|
+
port: t,
|
|
1437
|
+
skipFrameworkBuild: r = !1,
|
|
1438
|
+
functionName: n
|
|
1439
|
+
}) {
|
|
1440
|
+
const s = parseInt(t, 10);
|
|
1441
|
+
_e(e || null, s, r, n);
|
|
1442
|
+
}
|
|
1443
|
+
function be() {
|
|
1444
|
+
return Object.values(K).filter((t) => t.metadata?.name).map((t) => t.metadata.name);
|
|
1445
|
+
}
|
|
1446
|
+
function Br(e) {
|
|
1447
|
+
const t = K[e];
|
|
1448
|
+
if (!t)
|
|
1449
|
+
throw new Error(
|
|
1450
|
+
`Preset '${e}' not found. Run 'ef presets ls' to see available presets.`
|
|
1451
|
+
);
|
|
1452
|
+
return t.config;
|
|
1453
|
+
}
|
|
1454
|
+
async function Pn(e, t = {}) {
|
|
1455
|
+
const r = process.env.CLEAN_OUTPUT_MODE === "true";
|
|
1456
|
+
switch (e) {
|
|
1457
|
+
case "ls":
|
|
1458
|
+
r && be().forEach((n) => console.log(n?.toLowerCase())), r || be().forEach((n) => p.option(n));
|
|
1459
|
+
break;
|
|
1460
|
+
case "config":
|
|
1461
|
+
if (!t.preset) {
|
|
1462
|
+
p.error("Preset name is required. Use: ef presets config <preset-name>");
|
|
1463
|
+
return;
|
|
1464
|
+
}
|
|
1465
|
+
try {
|
|
1466
|
+
const n = Br(t.preset);
|
|
1467
|
+
console.log(JSON.stringify(n, null, 2));
|
|
1468
|
+
} catch (n) {
|
|
1469
|
+
p.error(n instanceof Error ? n.message : "Unknown error occurred");
|
|
1470
|
+
}
|
|
1471
|
+
break;
|
|
1472
|
+
default:
|
|
1473
|
+
p.error("Invalid argument provided. Available commands: ls, config");
|
|
1474
|
+
break;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
function Dr(e) {
|
|
1478
|
+
return tt(e);
|
|
1479
|
+
}
|
|
1480
|
+
const Wr = { processConfigWrapper: Dr }, Mr = ".edge/manifest.json", Lr = "azion.config", Hr = async (e, t = C(process.cwd(), ".edge")) => {
|
|
1481
|
+
try {
|
|
1482
|
+
await H.access(t);
|
|
1483
|
+
} catch {
|
|
1484
|
+
await H.mkdir(t, { recursive: !0 });
|
|
1485
|
+
}
|
|
1486
|
+
let r;
|
|
1487
|
+
if (typeof e == "object")
|
|
1488
|
+
r = e;
|
|
1489
|
+
else {
|
|
1490
|
+
const o = await Te.readAzionConfig(e);
|
|
1491
|
+
if (!o)
|
|
1492
|
+
throw new Error(
|
|
1493
|
+
e ? `Failed to load config from ${e}` : "No configuration found. Please provide a config file or object."
|
|
1494
|
+
);
|
|
1495
|
+
r = o;
|
|
1496
|
+
}
|
|
1497
|
+
te(r);
|
|
1498
|
+
const n = Wr.processConfigWrapper(r), s = C(t, "manifest.json");
|
|
1499
|
+
await H.writeFile(s, JSON.stringify(n, null, 2)), A.feedback.manifest.success(`Manifest generated successfully at ${s}`);
|
|
1500
|
+
}, Vr = async (e, t = Lr) => {
|
|
1501
|
+
const n = await (async (s) => {
|
|
1502
|
+
const o = ie(process.cwd(), s);
|
|
1503
|
+
if (q(o) !== ".json")
|
|
1504
|
+
throw new Error("Input file must be .json");
|
|
1505
|
+
const i = await H.readFile(o, "utf8");
|
|
1506
|
+
return rt(i);
|
|
1507
|
+
})(e || Mr);
|
|
1508
|
+
await Te.writeUserConfig(n, t), A.feedback.manifest.success(`Config file generated successfully at ${t}`);
|
|
1509
|
+
};
|
|
1510
|
+
async function xn(e) {
|
|
1511
|
+
try {
|
|
1512
|
+
const t = e.action || "generate", n = {
|
|
1513
|
+
generate: async () => {
|
|
1514
|
+
const s = e.entry || e.config;
|
|
1515
|
+
await Hr(s, e.output);
|
|
1516
|
+
},
|
|
1517
|
+
transform: async () => {
|
|
1518
|
+
await Vr(e.entry, e.output);
|
|
1519
|
+
}
|
|
1520
|
+
}[t];
|
|
1521
|
+
n && await n(), n || (p.manifest.error(
|
|
1522
|
+
"Only transform and generate actions are supported"
|
|
1523
|
+
), process.exit(1));
|
|
1524
|
+
} catch (t) {
|
|
1525
|
+
g.error(t), p.error(`${t instanceof Error ? t.message : String(t)}${U}`), await oe(O.OUTPUT_BASE_PATH, { recursive: !0, force: !0 }), process.exit(1);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
async function kn({ command: e, options: t }) {
|
|
1529
|
+
const r = JSON.parse(
|
|
1530
|
+
typeof t.config == "string" ? t.config : "{}"
|
|
1531
|
+
), n = t.scope || "global";
|
|
1532
|
+
try {
|
|
1533
|
+
switch (e) {
|
|
1534
|
+
case "init": {
|
|
1535
|
+
const s = { ...r };
|
|
1536
|
+
await ve(s, n), p.info(`Store file initialized with scope: ${n}`);
|
|
1537
|
+
break;
|
|
1538
|
+
}
|
|
1539
|
+
case "destroy":
|
|
1540
|
+
await oe(globalThis.bundler.tempPath, { recursive: !0, force: !0 }), p.info("Temporary store directory removed");
|
|
1541
|
+
break;
|
|
1542
|
+
default:
|
|
1543
|
+
throw new Error(`Invalid command: ${e}`);
|
|
1544
|
+
}
|
|
1545
|
+
} catch (s) {
|
|
1546
|
+
p.error(`${s instanceof Error ? s.message : String(s)}${U}`), process.exit(1);
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
function ne(e) {
|
|
1550
|
+
if (typeof e != "string")
|
|
1551
|
+
return e;
|
|
1552
|
+
const t = e.trim();
|
|
1553
|
+
if (!t.startsWith("{") && !t.startsWith("["))
|
|
1554
|
+
return e;
|
|
1555
|
+
try {
|
|
1556
|
+
return JSON.parse(e);
|
|
1557
|
+
} catch {
|
|
1558
|
+
return e;
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
async function zr() {
|
|
1562
|
+
const t = await yt("azion", {
|
|
1563
|
+
searchPlaces: [
|
|
1564
|
+
"azion.config.ts",
|
|
1565
|
+
"azion.config.mts",
|
|
1566
|
+
"azion.config.cts",
|
|
1567
|
+
"azion.config.js",
|
|
1568
|
+
"azion.config.mjs",
|
|
1569
|
+
"azion.config.cjs",
|
|
1570
|
+
"azion.config.json"
|
|
1571
|
+
],
|
|
1572
|
+
loaders: {
|
|
1573
|
+
".ts": X(),
|
|
1574
|
+
".mts": X(),
|
|
1575
|
+
".cts": X()
|
|
1576
|
+
}
|
|
1577
|
+
}).search();
|
|
1578
|
+
if (!t)
|
|
1579
|
+
throw new Error("No azion config file found");
|
|
1580
|
+
const r = t.filepath, n = await h.readFile(r, "utf8");
|
|
1581
|
+
return { configPath: r, fileContent: n };
|
|
1582
|
+
}
|
|
1583
|
+
const Oe = {
|
|
1584
|
+
findAndReadConfigFile: zr,
|
|
1585
|
+
tryParseJSON: ne
|
|
1586
|
+
};
|
|
1587
|
+
function Gr(e) {
|
|
1588
|
+
const t = {}, r = e.key.split(".");
|
|
1589
|
+
let n = t;
|
|
1590
|
+
for (let s = 0; s < r.length; s++) {
|
|
1591
|
+
const o = r[s], i = o.match(/^(.+)\[(\d+)\]$/);
|
|
1592
|
+
if (i) {
|
|
1593
|
+
const c = i[1], a = parseInt(i[2]);
|
|
1594
|
+
s === r.length - 1 ? (n[c] || (n[c] = []), n[c][a] = ne(e.value)) : (n[c] || (n[c] = []), n[c][a] || (n[c][a] = {}), n = n[c][a]);
|
|
1595
|
+
} else
|
|
1596
|
+
s === r.length - 1 ? n[o] = ne(e.value) : (n[o] || (n[o] = {}), n = n[o]);
|
|
1597
|
+
}
|
|
1598
|
+
return t;
|
|
1599
|
+
}
|
|
1600
|
+
function qr(e) {
|
|
1601
|
+
if (!e.config)
|
|
1602
|
+
throw new Error("Config is required for read");
|
|
1603
|
+
const t = e.config, r = e.key.split(".").flatMap((s) => {
|
|
1604
|
+
const o = s.match(/^(.+)\[(\d+)\]$/);
|
|
1605
|
+
return o ? [o[1], parseInt(o[2])] : [s];
|
|
1606
|
+
});
|
|
1607
|
+
let n = t;
|
|
1608
|
+
for (let s = 0; s < r.length; s++) {
|
|
1609
|
+
const o = r[s], i = r[s + 1];
|
|
1610
|
+
if (typeof o == "number") {
|
|
1611
|
+
if (!Array.isArray(n))
|
|
1612
|
+
throw new Error("Property is not an array");
|
|
1613
|
+
if (!n[o])
|
|
1614
|
+
throw new Error(`Array index ${o} does not exist`);
|
|
1615
|
+
n = n[o];
|
|
1616
|
+
} else {
|
|
1617
|
+
if (!(o in n))
|
|
1618
|
+
throw new Error(`Property '${o}' does not exist`);
|
|
1619
|
+
if (typeof i == "number") {
|
|
1620
|
+
if (!Array.isArray(n[o]))
|
|
1621
|
+
throw new Error(`Property '${o}' is not an array`);
|
|
1622
|
+
if (!n[o][i])
|
|
1623
|
+
throw new Error(`Array index ${i} does not exist in '${o}'`);
|
|
1624
|
+
n = n[o][i], s++;
|
|
1625
|
+
} else
|
|
1626
|
+
n = n[o];
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
return n;
|
|
1630
|
+
}
|
|
1631
|
+
function Kr(e) {
|
|
1632
|
+
if (!e.config)
|
|
1633
|
+
throw new Error("Config is required for delete");
|
|
1634
|
+
const t = { ...e.config }, r = e.key.split(".");
|
|
1635
|
+
let n = t;
|
|
1636
|
+
for (let i = 0; i < r.length - 1; i++) {
|
|
1637
|
+
const c = r[i], a = c.match(/^(.+)\[(\d+)\]$/);
|
|
1638
|
+
if (a) {
|
|
1639
|
+
const l = a[1], f = parseInt(a[2]);
|
|
1640
|
+
if (!n[l])
|
|
1641
|
+
throw new Error(`Property '${l}' does not exist`);
|
|
1642
|
+
if (!Array.isArray(n[l]))
|
|
1643
|
+
throw new Error(`Property '${l}' is not an array`);
|
|
1644
|
+
if (f >= n[l].length)
|
|
1645
|
+
throw new Error(`Array index ${f} does not exist in '${l}'`);
|
|
1646
|
+
n = n[l][f];
|
|
1647
|
+
} else {
|
|
1648
|
+
if (!n[c])
|
|
1649
|
+
throw new Error(`Property '${c}' does not exist`);
|
|
1650
|
+
n = n[c];
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
const s = r[r.length - 1], o = s.match(/^(.+)\[(\d+)\]$/);
|
|
1654
|
+
if (o) {
|
|
1655
|
+
const i = o[1], c = parseInt(o[2]);
|
|
1656
|
+
if (!n[i])
|
|
1657
|
+
throw new Error(`Property '${i}' does not exist`);
|
|
1658
|
+
if (!Array.isArray(n[i]))
|
|
1659
|
+
throw new Error(`Property '${i}' is not an array`);
|
|
1660
|
+
if (c >= n[i].length)
|
|
1661
|
+
throw new Error(`Array index ${c} does not exist in '${i}'`);
|
|
1662
|
+
n[i].splice(c, 1);
|
|
1663
|
+
} else {
|
|
1664
|
+
if (!(s in n))
|
|
1665
|
+
throw new Error(`Property '${s}' does not exist`);
|
|
1666
|
+
delete n[s];
|
|
1667
|
+
}
|
|
1668
|
+
return t;
|
|
1669
|
+
}
|
|
1670
|
+
function Jr(e) {
|
|
1671
|
+
const t = e.match(
|
|
1672
|
+
/^\s*export\s+default\s+defineConfig\s*\(\s*/m
|
|
1673
|
+
), r = e.match(
|
|
1674
|
+
/^\s*module\.exports\s*=\s*defineConfig\s*\(\s*/m
|
|
1675
|
+
), n = e.match(/^\s*export\s+default\s+/m), s = e.match(/^\s*module\.exports\s*=\s*/m);
|
|
1676
|
+
let o = -1;
|
|
1677
|
+
if (t)
|
|
1678
|
+
o = t.index + t[0].length;
|
|
1679
|
+
else if (r)
|
|
1680
|
+
o = r.index + r[0].length;
|
|
1681
|
+
else if (n) {
|
|
1682
|
+
const b = n.index + n[0].length;
|
|
1683
|
+
let d = b;
|
|
1684
|
+
for (; d < e.length && e[d].match(/\s/); )
|
|
1685
|
+
d++;
|
|
1686
|
+
const y = e.slice(d).match(/^defineConfig\s*\(\s*/);
|
|
1687
|
+
y ? o = d + y[0].length : o = b;
|
|
1688
|
+
} else if (s) {
|
|
1689
|
+
const b = s.index + s[0].length;
|
|
1690
|
+
let d = b;
|
|
1691
|
+
for (; d < e.length && e[d].match(/\s/); )
|
|
1692
|
+
d++;
|
|
1693
|
+
const y = e.slice(d).match(/^defineConfig\s*\(\s*/);
|
|
1694
|
+
y ? o = d + y[0].length : o = b;
|
|
1695
|
+
}
|
|
1696
|
+
if (o === -1)
|
|
1697
|
+
throw new Error("Could not find module.exports or export default in config file");
|
|
1698
|
+
let i = o;
|
|
1699
|
+
for (; i < e.length && e[i].match(/\s/); )
|
|
1700
|
+
i++;
|
|
1701
|
+
if (i >= e.length)
|
|
1702
|
+
throw new Error("Could not find opening brace or parenthesis for config object");
|
|
1703
|
+
const c = e[i], a = c === "{" ? "}" : c === "(" ? ")" : null;
|
|
1704
|
+
if (!a)
|
|
1705
|
+
throw new Error("Config object must start with { or (");
|
|
1706
|
+
let l = 1, f = !1, u = "", m = e.length;
|
|
1707
|
+
for (let b = i + 1; b < e.length; b++) {
|
|
1708
|
+
const d = e[b], y = b > 0 ? e[b - 1] : "";
|
|
1709
|
+
if ((d === '"' || d === "'" || d === "`") && y !== "\\") {
|
|
1710
|
+
f ? d === u && (f = !1, u = "") : (f = !0, u = d);
|
|
1711
|
+
continue;
|
|
1712
|
+
}
|
|
1713
|
+
if (!f && (d === c ? l++ : d === a && l--, l === 0)) {
|
|
1714
|
+
m = b + 1;
|
|
1715
|
+
break;
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
return { start: o, end: m };
|
|
1719
|
+
}
|
|
1720
|
+
async function Xr(e, t) {
|
|
1721
|
+
try {
|
|
1722
|
+
const r = Array.isArray(e) ? e : [{ placeholder: e, value: t }];
|
|
1723
|
+
for (const { placeholder: d, value: y } of r)
|
|
1724
|
+
if (typeof d != "string" || typeof y != "string")
|
|
1725
|
+
throw new Error("Placeholder and value must be strings");
|
|
1726
|
+
const { configPath: n, fileContent: s } = await Oe.findAndReadConfigFile(), { start: o, end: i } = Jr(s), c = s.slice(0, o);
|
|
1727
|
+
let a = s.slice(o, i);
|
|
1728
|
+
const l = s.slice(i), f = /* @__PURE__ */ new Map();
|
|
1729
|
+
for (const { placeholder: d, value: y } of r) {
|
|
1730
|
+
let T = 0, E = 0;
|
|
1731
|
+
for (; E < a.length; ) {
|
|
1732
|
+
const w = a.indexOf(d, E);
|
|
1733
|
+
if (w === -1) break;
|
|
1734
|
+
const v = w > 0 ? a[w - 1] : "", j = w + d.length < a.length ? a[w + d.length] : "";
|
|
1735
|
+
let N = !0;
|
|
1736
|
+
if (typeof d == "string" && d.match(/[A-Za-z0-9_$]/)) {
|
|
1737
|
+
const P = v && v.match(/[A-Za-z0-9_$]/), $ = j && j.match(/[A-Za-z0-9_]/);
|
|
1738
|
+
(P || $) && (N = !1);
|
|
1739
|
+
}
|
|
1740
|
+
N ? (a = a.slice(0, w) + y + a.slice(w + d.length), E = w + y.length, T++) : E = w + 1;
|
|
1741
|
+
}
|
|
1742
|
+
T > 0 && f.set(d, T);
|
|
1743
|
+
}
|
|
1744
|
+
const u = c + a + l;
|
|
1745
|
+
let m;
|
|
1746
|
+
try {
|
|
1747
|
+
m = await Pe.format(u, {
|
|
1748
|
+
parser: "babel",
|
|
1749
|
+
semi: !1,
|
|
1750
|
+
singleQuote: !0,
|
|
1751
|
+
trailingComma: "none"
|
|
1752
|
+
});
|
|
1753
|
+
} catch {
|
|
1754
|
+
m = u;
|
|
1755
|
+
}
|
|
1756
|
+
await h.writeFile(n, m, "utf8");
|
|
1757
|
+
const b = Array.from(f.values()).reduce((d, y) => d + y, 0);
|
|
1758
|
+
if (b === 0)
|
|
1759
|
+
p.info(`No placeholders found to replace in ${S.basename(n)}`);
|
|
1760
|
+
else if (f.size === 1) {
|
|
1761
|
+
const [d, y] = Array.from(f.entries())[0], T = r.find((E) => E.placeholder === d);
|
|
1762
|
+
p.info(
|
|
1763
|
+
`Successfully replaced "${d}" with "${T?.value}" (${y} occurrence${y > 1 ? "s" : ""}) in ${S.basename(n)}`
|
|
1764
|
+
);
|
|
1765
|
+
} else {
|
|
1766
|
+
const d = Array.from(f.entries()).map(([y, T]) => {
|
|
1767
|
+
const E = r.find((w) => w.placeholder === y);
|
|
1768
|
+
return `"${y}" -> "${E?.value}" (${T})`;
|
|
1769
|
+
}).join(", ");
|
|
1770
|
+
p.info(
|
|
1771
|
+
`Successfully replaced ${b} occurrences of ${f.size} placeholders (${d}) in ${S.basename(n)}`
|
|
1772
|
+
);
|
|
1773
|
+
}
|
|
1774
|
+
} catch (r) {
|
|
1775
|
+
throw new Error(
|
|
1776
|
+
`Failed to replace in config file: ${r instanceof Error ? r.message : String(r)}`
|
|
1777
|
+
);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
async function Yr(e, t) {
|
|
1781
|
+
try {
|
|
1782
|
+
const { configPath: r, fileContent: n } = await Oe.findAndReadConfigFile(), s = Zr(n, e, t);
|
|
1783
|
+
let o;
|
|
1784
|
+
try {
|
|
1785
|
+
o = await Pe.format(s, {
|
|
1786
|
+
parser: "babel",
|
|
1787
|
+
semi: !1,
|
|
1788
|
+
singleQuote: !0,
|
|
1789
|
+
trailingComma: "none"
|
|
1790
|
+
});
|
|
1791
|
+
} catch {
|
|
1792
|
+
o = s;
|
|
1793
|
+
}
|
|
1794
|
+
await h.writeFile(r, o, "utf8"), A.feedback.info(
|
|
1795
|
+
`Successfully updated "${e}" to "${t}" in ${S.basename(r)}`
|
|
1796
|
+
);
|
|
1797
|
+
} catch (r) {
|
|
1798
|
+
throw new Error(
|
|
1799
|
+
`Failed to update config file: ${r instanceof Error ? r.message : String(r)}`
|
|
1800
|
+
);
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
function Zr(e, t, r) {
|
|
1804
|
+
return Qr(e, t, r);
|
|
1805
|
+
}
|
|
1806
|
+
function Qr(e, t, r) {
|
|
1807
|
+
const n = tn(r), s = e.replace(/\n\s*/g, " ").replace(/\s+/g, " "), o = s.match(
|
|
1808
|
+
/(.*(?:module\.exports\s*=(?:\s+\w+\()?|export default(?:\s+\w+\()?)\s*)\{/
|
|
1809
|
+
);
|
|
1810
|
+
if (!o)
|
|
1811
|
+
return e;
|
|
1812
|
+
const i = o[0].length;
|
|
1813
|
+
let c = 1, a = i, l = !1, f = "";
|
|
1814
|
+
for (let P = i; P < s.length && c > 0; P++) {
|
|
1815
|
+
const $ = s[P];
|
|
1816
|
+
if (l ? $ === f && s[P - 1] !== "\\" && (l = !1) : $ === '"' || $ === "'" ? (l = !0, f = $) : $ === "{" ? c++ : $ === "}" && c--, c === 0) {
|
|
1817
|
+
a = P;
|
|
1818
|
+
break;
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
if (c !== 0)
|
|
1822
|
+
return e;
|
|
1823
|
+
const u = s.substring(i, a), m = en(t), b = fe(u, m, n), d = e.match(
|
|
1824
|
+
/(.*(?:module\.exports\s*=(?:\s+\w+\()?|export default(?:\s+\w+\()?)\s*)\{/s
|
|
1825
|
+
);
|
|
1826
|
+
if (!d)
|
|
1827
|
+
return e;
|
|
1828
|
+
const y = d[1] + "{", T = d[0].length;
|
|
1829
|
+
let E = 1, w = T, v = !1, j = "";
|
|
1830
|
+
for (let P = T; P < e.length && E > 0; P++) {
|
|
1831
|
+
const $ = e[P];
|
|
1832
|
+
if (v ? $ === j && e[P - 1] !== "\\" && (v = !1) : $ === '"' || $ === "'" ? (v = !0, j = $) : $ === "{" ? E++ : $ === "}" && E--, E === 0) {
|
|
1833
|
+
w = P;
|
|
1834
|
+
break;
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
const N = "}" + e.substring(w + 1);
|
|
1838
|
+
return y + b + N;
|
|
1839
|
+
}
|
|
1840
|
+
function en(e) {
|
|
1841
|
+
const t = [];
|
|
1842
|
+
let r = "", n = 0;
|
|
1843
|
+
for (; n < e.length; ) {
|
|
1844
|
+
const s = e[n];
|
|
1845
|
+
if (s === ".")
|
|
1846
|
+
r && (t.push(r), r = "");
|
|
1847
|
+
else if (s === "[") {
|
|
1848
|
+
r && (t.push(r), r = ""), n++;
|
|
1849
|
+
let o = "";
|
|
1850
|
+
for (; n < e.length && e[n] !== "]"; )
|
|
1851
|
+
o += e[n], n++;
|
|
1852
|
+
t.push(parseInt(o));
|
|
1853
|
+
} else
|
|
1854
|
+
r += s;
|
|
1855
|
+
n++;
|
|
1856
|
+
}
|
|
1857
|
+
return r && t.push(r), t;
|
|
1858
|
+
}
|
|
1859
|
+
function fe(e, t, r) {
|
|
1860
|
+
if (t.length === 0) return e;
|
|
1861
|
+
const [n, ...s] = t;
|
|
1862
|
+
if (typeof n == "string") {
|
|
1863
|
+
const o = n.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
1864
|
+
if (s.length === 0) {
|
|
1865
|
+
const i = new RegExp(`(${o}\\s*:\\s*)([^,}]+)`);
|
|
1866
|
+
return e.replace(i, `$1${r}`);
|
|
1867
|
+
} else {
|
|
1868
|
+
const i = e.match(
|
|
1869
|
+
new RegExp(`(.*${o}\\s*:\\s*)([^,}]+)(.*)`)
|
|
1870
|
+
);
|
|
1871
|
+
if (i) {
|
|
1872
|
+
const [, , c] = i;
|
|
1873
|
+
if (c.trim().startsWith("{")) {
|
|
1874
|
+
const a = e.indexOf(c.trim());
|
|
1875
|
+
if (a === -1)
|
|
1876
|
+
return e;
|
|
1877
|
+
let l = 0, f = !1, u = "", m = -1;
|
|
1878
|
+
for (let w = a; w < e.length; w++) {
|
|
1879
|
+
const v = e[w];
|
|
1880
|
+
if (f)
|
|
1881
|
+
v === u && e[w - 1] !== "\\" && (f = !1);
|
|
1882
|
+
else if (v === '"' || v === "'")
|
|
1883
|
+
f = !0, u = v;
|
|
1884
|
+
else if (v === "{")
|
|
1885
|
+
l++;
|
|
1886
|
+
else if (v === "}" && (l--, l === 0)) {
|
|
1887
|
+
m = w;
|
|
1888
|
+
break;
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
if (m === -1)
|
|
1892
|
+
return e;
|
|
1893
|
+
const d = e.substring(a, m + 1).slice(1, -1), y = fe(d, s, r), T = e.substring(0, a), E = e.substring(m + 1);
|
|
1894
|
+
return T + `{ ${y} }` + E;
|
|
1895
|
+
} else if (c.trim().startsWith("[")) {
|
|
1896
|
+
const a = e.indexOf(c.trim());
|
|
1897
|
+
if (a === -1)
|
|
1898
|
+
return e;
|
|
1899
|
+
let l = 0, f = !1, u = "", m = -1;
|
|
1900
|
+
for (let w = a; w < e.length; w++) {
|
|
1901
|
+
const v = e[w];
|
|
1902
|
+
if (f)
|
|
1903
|
+
v === u && e[w - 1] !== "\\" && (f = !1);
|
|
1904
|
+
else if (v === '"' || v === "'")
|
|
1905
|
+
f = !0, u = v;
|
|
1906
|
+
else if (v === "[")
|
|
1907
|
+
l++;
|
|
1908
|
+
else if (v === "]" && (l--, l === 0)) {
|
|
1909
|
+
m = w;
|
|
1910
|
+
break;
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
if (m === -1)
|
|
1914
|
+
return e;
|
|
1915
|
+
const d = e.substring(a, m + 1).slice(1, -1), y = Ee(d, s, r), T = e.substring(0, a), E = e.substring(m + 1);
|
|
1916
|
+
return T + `[ ${y} ]` + E;
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
} else if (typeof n == "number")
|
|
1921
|
+
return Ee(e, t, r);
|
|
1922
|
+
return e;
|
|
1923
|
+
}
|
|
1924
|
+
function Ee(e, t, r) {
|
|
1925
|
+
if (t.length === 0) return e;
|
|
1926
|
+
const [n, ...s] = t;
|
|
1927
|
+
if (typeof n != "number") return e;
|
|
1928
|
+
const o = [];
|
|
1929
|
+
let i = 0;
|
|
1930
|
+
for (; i < e.length; ) {
|
|
1931
|
+
for (; i < e.length && /\s/.test(e[i]); )
|
|
1932
|
+
i++;
|
|
1933
|
+
if (i >= e.length) break;
|
|
1934
|
+
if (e[i] === "{") {
|
|
1935
|
+
let c = 0, a = !1, l = "";
|
|
1936
|
+
const f = i;
|
|
1937
|
+
for (; i < e.length; ) {
|
|
1938
|
+
const u = e[i];
|
|
1939
|
+
if (a)
|
|
1940
|
+
u === l && e[i - 1] !== "\\" && (a = !1);
|
|
1941
|
+
else if (u === '"' || u === "'")
|
|
1942
|
+
a = !0, l = u;
|
|
1943
|
+
else if (u === "{")
|
|
1944
|
+
c++;
|
|
1945
|
+
else if (u === "}" && (c--, c === 0)) {
|
|
1946
|
+
const m = e.substring(f, i + 1);
|
|
1947
|
+
for (o.push(m.trim()), i++; i < e.length && /[\s,]/.test(e[i]); )
|
|
1948
|
+
i++;
|
|
1949
|
+
break;
|
|
1950
|
+
}
|
|
1951
|
+
i++;
|
|
1952
|
+
}
|
|
1953
|
+
} else {
|
|
1954
|
+
const c = i;
|
|
1955
|
+
let a = !1, l = "";
|
|
1956
|
+
for (; i < e.length; ) {
|
|
1957
|
+
const f = e[i];
|
|
1958
|
+
if (a)
|
|
1959
|
+
f === l && e[i - 1] !== "\\" && (a = !1);
|
|
1960
|
+
else if (f === '"' || f === "'")
|
|
1961
|
+
a = !0, l = f;
|
|
1962
|
+
else if (f === ",") {
|
|
1963
|
+
const u = e.substring(c, i);
|
|
1964
|
+
for (o.push(u.trim()), i++; i < e.length && /\s/.test(e[i]); )
|
|
1965
|
+
i++;
|
|
1966
|
+
break;
|
|
1967
|
+
}
|
|
1968
|
+
i++;
|
|
1969
|
+
}
|
|
1970
|
+
if (i >= e.length && c < e.length) {
|
|
1971
|
+
const f = e.substring(c);
|
|
1972
|
+
f.trim() && o.push(f.trim());
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
}
|
|
1976
|
+
if (n < o.length)
|
|
1977
|
+
if (s.length === 0)
|
|
1978
|
+
o[n] = r;
|
|
1979
|
+
else {
|
|
1980
|
+
const c = o[n];
|
|
1981
|
+
if (c.startsWith("{") && c.endsWith("}")) {
|
|
1982
|
+
const a = c.slice(1, -1), l = fe(a, s, r);
|
|
1983
|
+
o[n] = `{ ${l} }`;
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
return o.join(", ");
|
|
1987
|
+
}
|
|
1988
|
+
function tn(e) {
|
|
1989
|
+
if (typeof e == "string") {
|
|
1990
|
+
if (e.startsWith('"') && e.endsWith('"') || e.startsWith("'") && e.endsWith("'"))
|
|
1991
|
+
return e;
|
|
1992
|
+
const t = e.trim();
|
|
1993
|
+
return t.startsWith("{") && t.endsWith("}") || t.startsWith("[") && t.endsWith("]") || e.includes("=>") || e.trim().startsWith("function") ? e : `'${e}'`;
|
|
1994
|
+
}
|
|
1995
|
+
return String(e);
|
|
1996
|
+
}
|
|
1997
|
+
async function Cn({ command: e, options: t }) {
|
|
1998
|
+
try {
|
|
1999
|
+
const r = await se();
|
|
2000
|
+
if (t.all) {
|
|
2001
|
+
let l;
|
|
2002
|
+
switch (e) {
|
|
2003
|
+
case "read":
|
|
2004
|
+
return l = r || {}, console.log(JSON.stringify(l, null, 2)), l;
|
|
2005
|
+
case "delete":
|
|
2006
|
+
return await ee({}), {};
|
|
2007
|
+
default:
|
|
2008
|
+
throw new Error("--all flag is only supported for read and delete commands");
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
const n = Array.isArray(t.key) ? t.key : t.key ? [t.key] : [], s = Array.isArray(t.value) ? t.value : t.value !== void 0 ? [t.value] : [];
|
|
2012
|
+
if (n.length === 0 && e !== "replace")
|
|
2013
|
+
throw new Error("Key is required when --all is not used");
|
|
2014
|
+
const o = n[0], i = s[0];
|
|
2015
|
+
let c, a;
|
|
2016
|
+
switch (e) {
|
|
2017
|
+
case "create":
|
|
2018
|
+
if (r)
|
|
2019
|
+
throw new Error("Configuration already exists. Use update command instead.");
|
|
2020
|
+
if (i === void 0)
|
|
2021
|
+
throw new Error("Value is required for create command");
|
|
2022
|
+
c = Gr({
|
|
2023
|
+
key: o,
|
|
2024
|
+
value: i
|
|
2025
|
+
});
|
|
2026
|
+
break;
|
|
2027
|
+
case "update":
|
|
2028
|
+
if (!r)
|
|
2029
|
+
throw new Error("No configuration found. Use create command first.");
|
|
2030
|
+
if (i === void 0)
|
|
2031
|
+
throw new Error("Value is required for update command");
|
|
2032
|
+
return await Yr(o, i), {};
|
|
2033
|
+
case "read":
|
|
2034
|
+
if (!r)
|
|
2035
|
+
throw new Error("No configuration found");
|
|
2036
|
+
return a = qr({
|
|
2037
|
+
key: o,
|
|
2038
|
+
config: r
|
|
2039
|
+
}), a;
|
|
2040
|
+
case "delete":
|
|
2041
|
+
if (!r)
|
|
2042
|
+
throw new Error("No configuration found");
|
|
2043
|
+
c = Kr({
|
|
2044
|
+
key: o,
|
|
2045
|
+
config: r
|
|
2046
|
+
});
|
|
2047
|
+
break;
|
|
2048
|
+
case "replace": {
|
|
2049
|
+
const l = [];
|
|
2050
|
+
if (t.replacements) {
|
|
2051
|
+
let f;
|
|
2052
|
+
if (typeof t.replacements == "string")
|
|
2053
|
+
try {
|
|
2054
|
+
f = JSON.parse(t.replacements);
|
|
2055
|
+
} catch {
|
|
2056
|
+
try {
|
|
2057
|
+
const u = t.replacements.replace(/\\"/g, '"');
|
|
2058
|
+
f = JSON.parse(u);
|
|
2059
|
+
} catch (u) {
|
|
2060
|
+
const m = u instanceof Error ? u.message : String(u);
|
|
2061
|
+
throw new Error(
|
|
2062
|
+
`Invalid JSON format for --replacements option: ${m}. Received: ${t.replacements}`
|
|
2063
|
+
);
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
else if (typeof t.replacements == "object")
|
|
2067
|
+
f = t.replacements;
|
|
2068
|
+
else
|
|
2069
|
+
throw new Error("Invalid format for --replacements option");
|
|
2070
|
+
for (const [u, m] of Object.entries(f))
|
|
2071
|
+
l.push({ placeholder: u, value: String(m) });
|
|
2072
|
+
} else if (n.length > 0 && s.length > 0)
|
|
2073
|
+
for (let f = 0; f < Math.min(n.length, s.length); f++)
|
|
2074
|
+
l.push({ placeholder: n[f], value: String(s[f]) });
|
|
2075
|
+
if (l.length === 0)
|
|
2076
|
+
throw new Error(
|
|
2077
|
+
"Replacements are required for replace command. Use -k/-v pairs or --replacements JSON"
|
|
2078
|
+
);
|
|
2079
|
+
return await Xr(l), {};
|
|
2080
|
+
}
|
|
2081
|
+
default:
|
|
2082
|
+
throw new Error(`Unknown command: ${e}`);
|
|
2083
|
+
}
|
|
2084
|
+
return await ee(c), c;
|
|
2085
|
+
} catch (r) {
|
|
2086
|
+
g.error(r), p.error(`${r instanceof Error ? r.message : String(r)}`), process.exit(1);
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
async function Fn(e = {}) {
|
|
2090
|
+
const t = e.path || C(O.OUTPUT_BASE_PATH, "telemetry-report.html");
|
|
2091
|
+
ht(t) || (p.error(
|
|
2092
|
+
`Telemetry report not found at: ${t}
|
|
2093
|
+
|
|
2094
|
+
Make sure you have run a build with the --telemetry flag first.
|
|
2095
|
+
Example: ef build --telemetry html
|
|
2096
|
+
Or: ef build --telemetry`
|
|
2097
|
+
), process.exit(1));
|
|
2098
|
+
try {
|
|
2099
|
+
const r = process.platform;
|
|
2100
|
+
let n;
|
|
2101
|
+
r === "darwin" ? n = `open "${t}"` : r === "win32" ? n = `start "" "${t}"` : n = `xdg-open "${t}"`;
|
|
2102
|
+
const { exec: s } = await import("child_process");
|
|
2103
|
+
s(n, (o) => {
|
|
2104
|
+
o && (p.error(`Failed to open telemetry report: ${o.message}`), process.exit(1)), p.success(`Opened telemetry report: ${t}`);
|
|
2105
|
+
});
|
|
2106
|
+
} catch (r) {
|
|
2107
|
+
p.error(
|
|
2108
|
+
`Failed to open telemetry report: ${r instanceof Error ? r.message : "Unknown error"}`
|
|
2109
|
+
), process.exit(1);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
export {
|
|
2113
|
+
Ar as buildCommand,
|
|
2114
|
+
Cn as configCommand,
|
|
2115
|
+
$n as devCommand,
|
|
2116
|
+
xn as manifestCommand,
|
|
2117
|
+
Pn as presetsCommand,
|
|
2118
|
+
kn as storeCommand,
|
|
2119
|
+
Fn as telemetryCommand
|
|
2120
|
+
};
|