@edgeone/nuxt-pages 1.0.0-beta.1

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.
@@ -0,0 +1,261 @@
1
+
2
+ var require = await (async () => {
3
+ var { createRequire } = await import("node:module");
4
+ return createRequire(import.meta.url);
5
+ })();
6
+
7
+ import {
8
+ getHandlersArrayWithAST,
9
+ getRouteRulesWithAST
10
+ } from "./chunk-5JK44IEA.js";
11
+
12
+ // src/build/routes.ts
13
+ import * as fs from "fs";
14
+ import * as path from "path";
15
+ var CONFIG_VERSION = 3;
16
+ var CONFIG_FILE_NAME = "config.json";
17
+ function escapeRegExp(str) {
18
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
19
+ }
20
+ function normalizeRoutePath(p) {
21
+ if (!p) return "/";
22
+ const clean = p.split("?")[0];
23
+ if (clean === "" || clean === "/") return "/";
24
+ return clean.startsWith("/") ? clean.replace(/\/+$/, "") : `/${clean.replace(/\/+$/, "")}`;
25
+ }
26
+ function ensureServerHandlerDir() {
27
+ const edgeOneDir = path.join(process.cwd(), ".edgeone");
28
+ const serverHandlerDir = path.join(edgeOneDir, "cloud-functions", "ssr-node");
29
+ if (!fs.existsSync(edgeOneDir)) fs.mkdirSync(edgeOneDir, { recursive: true });
30
+ if (!fs.existsSync(serverHandlerDir)) fs.mkdirSync(serverHandlerDir, { recursive: true });
31
+ return serverHandlerDir;
32
+ }
33
+ function getConfigPath() {
34
+ return path.join(ensureServerHandlerDir(), CONFIG_FILE_NAME);
35
+ }
36
+ function writeConfig(config) {
37
+ const configPath = getConfigPath();
38
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
39
+ }
40
+ function normalizeHeaders(headers) {
41
+ if (!headers || typeof headers !== "object") return null;
42
+ const out = {};
43
+ for (const [k, v] of Object.entries(headers)) {
44
+ if (typeof k !== "string" || !k) continue;
45
+ if (typeof v === "string") out[k] = v;
46
+ else if (typeof v === "number" || typeof v === "boolean") out[k] = String(v);
47
+ }
48
+ return Object.keys(out).length ? out : null;
49
+ }
50
+ function extractInlineRuntimeConfigFromNitroMjs(code) {
51
+ if (!code || typeof code !== "string") return null;
52
+ const idx = code.search(/\b_inlineRuntimeConfig\b\s*=\s*\{/);
53
+ if (idx === -1) return null;
54
+ const braceStart = code.indexOf("{", idx);
55
+ if (braceStart === -1) return null;
56
+ let depth = 0;
57
+ let inStr = false;
58
+ let quote = "";
59
+ let escaped = false;
60
+ let end = -1;
61
+ for (let i = braceStart; i < code.length; i++) {
62
+ const ch = code[i];
63
+ if (inStr) {
64
+ if (escaped) {
65
+ escaped = false;
66
+ continue;
67
+ }
68
+ if (ch === "\\") {
69
+ escaped = true;
70
+ continue;
71
+ }
72
+ if (ch === quote) {
73
+ inStr = false;
74
+ quote = "";
75
+ }
76
+ continue;
77
+ }
78
+ if (ch === '"' || ch === "'") {
79
+ inStr = true;
80
+ quote = ch;
81
+ continue;
82
+ }
83
+ if (ch === "{") {
84
+ depth++;
85
+ continue;
86
+ }
87
+ if (ch === "}") {
88
+ depth--;
89
+ if (depth === 0) {
90
+ end = i;
91
+ break;
92
+ }
93
+ continue;
94
+ }
95
+ }
96
+ if (end === -1) return null;
97
+ const objStr = code.slice(braceStart, end + 1);
98
+ try {
99
+ return JSON.parse(objStr);
100
+ } catch {
101
+ return null;
102
+ }
103
+ }
104
+ async function getRouteRulesFromNitroInlineRuntimeConfig(ctx) {
105
+ const nitroMjsPath = resolveNitroMjsPath();
106
+ if (!nitroMjsPath) return null;
107
+ try {
108
+ const nitroContent = fs.readFileSync(nitroMjsPath, "utf-8");
109
+ const inlineCfg = extractInlineRuntimeConfigFromNitroMjs(nitroContent);
110
+ const rr = inlineCfg?.nitro?.routeRules;
111
+ if (rr && typeof rr === "object") return rr;
112
+ } catch {
113
+ }
114
+ return null;
115
+ }
116
+ async function getRouteRulesFromBuildOutput(ctx) {
117
+ const fromNitroMjs = await getRouteRulesFromNitroInlineRuntimeConfig(ctx);
118
+ if (fromNitroMjs) return fromNitroMjs;
119
+ try {
120
+ if (typeof ctx?.getNuxtConfig === "function") {
121
+ const cfg = await ctx.getNuxtConfig();
122
+ const parsed = getRouteRulesWithAST(cfg || "");
123
+ if (parsed && typeof parsed === "object") return parsed;
124
+ }
125
+ } catch {
126
+ }
127
+ return null;
128
+ }
129
+ async function buildHeadersRoutes(ctx) {
130
+ const routeRules = await getRouteRulesFromBuildOutput(ctx);
131
+ if (!routeRules || typeof routeRules !== "object") return [];
132
+ const out = [];
133
+ for (const [srcRaw, rule] of Object.entries(routeRules)) {
134
+ const headers = normalizeHeaders(rule?.headers);
135
+ if (!headers) continue;
136
+ const src = typeof srcRaw === "string" && srcRaw.startsWith("^") ? srcRaw : normalizeRoutePath(String(srcRaw)).replace(/\*\*/g, "(.*)");
137
+ out.push({ src, headers });
138
+ }
139
+ return out;
140
+ }
141
+ async function buildStage1Routes(ctx) {
142
+ const routes = [];
143
+ routes.push({ src: "^/([^.]+[^/.])$", dest: "/$1/", continue: true });
144
+ routes.push(...await buildHeadersRoutes(ctx));
145
+ routes.push({ handle: "filesystem" });
146
+ return routes;
147
+ }
148
+ function resolveNitroMjsPath() {
149
+ const edgeOneDir = path.join(process.cwd(), ".edgeone");
150
+ const defaultNitroPath = path.join(edgeOneDir, "cloud-functions", "ssr-node", "chunks", "nitro", "nitro.mjs");
151
+ const fallbackNitroPath = path.join(edgeOneDir, "cloud-functions", "ssr-node", "chunks", "_", "nitro.mjs");
152
+ const nitroPath = fs.existsSync(defaultNitroPath) ? defaultNitroPath : fallbackNitroPath;
153
+ return fs.existsSync(nitroPath) ? nitroPath : null;
154
+ }
155
+ async function buildExplicitApiRoutes(_ctx) {
156
+ const nitroPath = resolveNitroMjsPath();
157
+ if (!nitroPath) {
158
+ console.error("Nitro entry(nitro.mjs) not found");
159
+ return [];
160
+ }
161
+ try {
162
+ const nitroContent = fs.readFileSync(nitroPath, "utf-8");
163
+ const handlersArray = getHandlersArrayWithAST(nitroContent);
164
+ if (!Array.isArray(handlersArray)) return [];
165
+ const seen = /* @__PURE__ */ new Set();
166
+ const out = [];
167
+ for (const h of handlersArray) {
168
+ const route = typeof h?.route === "string" ? String(h.route) : "";
169
+ if (!route) continue;
170
+ const normalized = normalizeRoutePath(route);
171
+ if (!normalized.startsWith("/api")) continue;
172
+ const rule = route.startsWith("^") ? { src: route } : nuxtRoutePathToRouteRule(normalized);
173
+ const src = rule?.src;
174
+ if (!src || seen.has(src)) continue;
175
+ seen.add(src);
176
+ out.push(rule);
177
+ }
178
+ out.sort((a, b) => String(b.src || "").length - String(a.src || "").length);
179
+ return out;
180
+ } catch (error) {
181
+ console.error("Error parsing nitro.mjs for API routes:", error);
182
+ return [];
183
+ }
184
+ }
185
+ function isCatchAllLikeRoutePath(p) {
186
+ const clean = normalizeRoutePath(p);
187
+ return clean.includes("(.*)") || clean.includes(".*") || clean.includes(":all");
188
+ }
189
+ function nuxtRoutePathToRouteRule(routePath) {
190
+ const p = normalizeRoutePath(routePath);
191
+ if (!p.startsWith("/")) return null;
192
+ if (p === "/") return { src: "^/$" };
193
+ const parts = p.split("/").filter(Boolean);
194
+ const srcParts = [];
195
+ for (const seg of parts) {
196
+ if (seg === "*" || seg === "**") {
197
+ srcParts.push("(.*)");
198
+ continue;
199
+ }
200
+ if (seg.startsWith(":")) {
201
+ const m = seg.match(/^:([A-Za-z_$][\w$]*)(?:\((.*)\))?(\*)?$/);
202
+ const inner = m ? m[2] : null;
203
+ const star = m ? m[3] : null;
204
+ if (star || inner === ".*") {
205
+ srcParts.push("(.*)");
206
+ continue;
207
+ }
208
+ if (!inner || inner.trim() === "") {
209
+ srcParts.push("([^/]+)");
210
+ continue;
211
+ }
212
+ srcParts.push(`((?:${inner}))`);
213
+ continue;
214
+ }
215
+ srcParts.push(escapeRegExp(seg));
216
+ }
217
+ const srcBody = srcParts.join("/");
218
+ return { src: `^/${srcBody}$` };
219
+ }
220
+ async function buildExplicitSsrAndSwrRoutes(ctx) {
221
+ const routesManifest = await ctx.getRoutesManifest();
222
+ const manifestRoutes = Array.isArray(routesManifest?.routes) ? routesManifest.routes : [];
223
+ const seen = /* @__PURE__ */ new Set();
224
+ const tmp = [];
225
+ for (const r of manifestRoutes) {
226
+ const p = typeof r?.path === "string" ? r.path : "";
227
+ if (!p) continue;
228
+ const normalized = normalizeRoutePath(p);
229
+ if (normalized.startsWith("/api")) continue;
230
+ const isSsr = r?.ssr === true;
231
+ const isSwr = r?.swr !== false && r?.swr !== void 0 && r?.swr !== null;
232
+ if (!isSsr && !isSwr) continue;
233
+ const rule = nuxtRoutePathToRouteRule(normalized);
234
+ const src = rule?.src;
235
+ if (!src || seen.has(src)) continue;
236
+ seen.add(src);
237
+ const score = (isCatchAllLikeRoutePath(normalized) ? -1e4 : 0) + normalized.split("/").length;
238
+ tmp.push({ score, rule });
239
+ }
240
+ tmp.sort((a, b) => b.score - a.score);
241
+ return tmp.map((x) => x.rule);
242
+ }
243
+ var createNuxtRoutesMeta = async (ctx) => {
244
+ const routes = await buildStage1Routes(ctx);
245
+ routes.push(...await buildExplicitApiRoutes(ctx));
246
+ routes.push(...await buildExplicitSsrAndSwrRoutes(ctx));
247
+ routes.push({ src: "^/(.*)$" });
248
+ writeConfig({ version: CONFIG_VERSION, routes });
249
+ };
250
+ var createNuxtPagesRouteMeta = async (ctx) => {
251
+ await createNuxtRoutesMeta(ctx);
252
+ };
253
+ var createNuxtApiRoutesMeta = async (ctx) => {
254
+ await createNuxtRoutesMeta(ctx);
255
+ };
256
+
257
+ export {
258
+ createNuxtRoutesMeta,
259
+ createNuxtPagesRouteMeta,
260
+ createNuxtApiRoutesMeta
261
+ };