821 lines
No EOL
26 KiB
JavaScript
821 lines
No EOL
26 KiB
JavaScript
import { a as legacyRootDirArgs, i as extendsArgs, n as dotEnvArgs, o as logLevelArgs, t as cwdArgs } from "./_shared-BCYCnX0T.mjs";
|
|
import { n as logger } from "./logger-B4ge7MhP.mjs";
|
|
import { t as overrideEnv } from "./env-DV8TWRZt.mjs";
|
|
import { r as relativeToProcess, t as loadKit } from "./kit-B3S8uoS_.mjs";
|
|
import { n as clearDir } from "./fs-CQH7NJn6.mjs";
|
|
import process from "node:process";
|
|
import { defineCommand } from "citty";
|
|
import { colors } from "consola/utils";
|
|
import { intro, note, outro, taskLog } from "@clack/prompts";
|
|
import { defu as defu$1 } from "defu";
|
|
import { promises } from "node:fs";
|
|
import { join, resolve } from "pathe";
|
|
import { FastResponse, FastURL, serve } from "srvx";
|
|
|
|
//#region ../../node_modules/.pnpm/rou3@0.7.10/node_modules/rou3/dist/index.mjs
|
|
const NullProtoObj = /* @__PURE__ */ (() => {
|
|
const e = function() {};
|
|
return e.prototype = Object.create(null), Object.freeze(e.prototype), e;
|
|
})();
|
|
/**
|
|
* Create a new router context.
|
|
*/
|
|
function createRouter() {
|
|
return {
|
|
root: { key: "" },
|
|
static: new NullProtoObj()
|
|
};
|
|
}
|
|
function splitPath(path) {
|
|
const [_, ...s] = path.split("/");
|
|
return s[s.length - 1] === "" ? s.slice(0, -1) : s;
|
|
}
|
|
function getMatchParams(segments, paramsMap) {
|
|
const params = new NullProtoObj();
|
|
for (const [index, name] of paramsMap) {
|
|
const segment = index < 0 ? segments.slice(-1 * index).join("/") : segments[index];
|
|
if (typeof name === "string") params[name] = segment;
|
|
else {
|
|
const match = segment.match(name);
|
|
if (match) for (const key in match.groups) params[key] = match.groups[key];
|
|
}
|
|
}
|
|
return params;
|
|
}
|
|
/**
|
|
* Add a route to the router context.
|
|
*/
|
|
function addRoute(ctx, method = "", path, data) {
|
|
method = method.toUpperCase();
|
|
if (path.charCodeAt(0) !== 47) path = `/${path}`;
|
|
const segments = splitPath(path);
|
|
let node = ctx.root;
|
|
let _unnamedParamIndex = 0;
|
|
const paramsMap = [];
|
|
const paramsRegexp = [];
|
|
for (let i = 0; i < segments.length; i++) {
|
|
const segment = segments[i];
|
|
if (segment.startsWith("**")) {
|
|
if (!node.wildcard) node.wildcard = { key: "**" };
|
|
node = node.wildcard;
|
|
paramsMap.push([
|
|
-i,
|
|
segment.split(":")[1] || "_",
|
|
segment.length === 2
|
|
]);
|
|
break;
|
|
}
|
|
if (segment === "*" || segment.includes(":")) {
|
|
if (!node.param) node.param = { key: "*" };
|
|
node = node.param;
|
|
if (segment === "*") paramsMap.push([
|
|
i,
|
|
`_${_unnamedParamIndex++}`,
|
|
true
|
|
]);
|
|
else if (segment.includes(":", 1)) {
|
|
const regexp = getParamRegexp(segment);
|
|
paramsRegexp[i] = regexp;
|
|
node.hasRegexParam = true;
|
|
paramsMap.push([
|
|
i,
|
|
regexp,
|
|
false
|
|
]);
|
|
} else paramsMap.push([
|
|
i,
|
|
segment.slice(1),
|
|
false
|
|
]);
|
|
continue;
|
|
}
|
|
const child = node.static?.[segment];
|
|
if (child) node = child;
|
|
else {
|
|
const staticNode = { key: segment };
|
|
if (!node.static) node.static = new NullProtoObj();
|
|
node.static[segment] = staticNode;
|
|
node = staticNode;
|
|
}
|
|
}
|
|
const hasParams = paramsMap.length > 0;
|
|
if (!node.methods) node.methods = new NullProtoObj();
|
|
node.methods[method] ??= [];
|
|
node.methods[method].push({
|
|
data: data || null,
|
|
paramsRegexp,
|
|
paramsMap: hasParams ? paramsMap : void 0
|
|
});
|
|
if (!hasParams) ctx.static[path] = node;
|
|
}
|
|
function getParamRegexp(segment) {
|
|
const regex = segment.replace(/:(\w+)/g, (_, id) => `(?<${id}>[^/]+)`).replace(/\./g, "\\.");
|
|
return /* @__PURE__ */ new RegExp(`^${regex}$`);
|
|
}
|
|
/**
|
|
* Find a route by path.
|
|
*/
|
|
function findRoute(ctx, method = "", path, opts) {
|
|
if (path.charCodeAt(path.length - 1) === 47) path = path.slice(0, -1);
|
|
const staticNode = ctx.static[path];
|
|
if (staticNode && staticNode.methods) {
|
|
const staticMatch = staticNode.methods[method] || staticNode.methods[""];
|
|
if (staticMatch !== void 0) return staticMatch[0];
|
|
}
|
|
const segments = splitPath(path);
|
|
const match = _lookupTree(ctx, ctx.root, method, segments, 0)?.[0];
|
|
if (match === void 0) return;
|
|
if (opts?.params === false) return match;
|
|
return {
|
|
data: match.data,
|
|
params: match.paramsMap ? getMatchParams(segments, match.paramsMap) : void 0
|
|
};
|
|
}
|
|
function _lookupTree(ctx, node, method, segments, index) {
|
|
if (index === segments.length) {
|
|
if (node.methods) {
|
|
const match = node.methods[method] || node.methods[""];
|
|
if (match) return match;
|
|
}
|
|
if (node.param && node.param.methods) {
|
|
const match = node.param.methods[method] || node.param.methods[""];
|
|
if (match) {
|
|
const pMap = match[0].paramsMap;
|
|
if (pMap?.[pMap?.length - 1]?.[2]) return match;
|
|
}
|
|
}
|
|
if (node.wildcard && node.wildcard.methods) {
|
|
const match = node.wildcard.methods[method] || node.wildcard.methods[""];
|
|
if (match) {
|
|
const pMap = match[0].paramsMap;
|
|
if (pMap?.[pMap?.length - 1]?.[2]) return match;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
const segment = segments[index];
|
|
if (node.static) {
|
|
const staticChild = node.static[segment];
|
|
if (staticChild) {
|
|
const match = _lookupTree(ctx, staticChild, method, segments, index + 1);
|
|
if (match) return match;
|
|
}
|
|
}
|
|
if (node.param) {
|
|
const match = _lookupTree(ctx, node.param, method, segments, index + 1);
|
|
if (match) {
|
|
if (node.param.hasRegexParam) {
|
|
const exactMatch = match.find((m) => m.paramsRegexp[index]?.test(segment)) || match.find((m) => !m.paramsRegexp[index]);
|
|
return exactMatch ? [exactMatch] : void 0;
|
|
}
|
|
return match;
|
|
}
|
|
}
|
|
if (node.wildcard && node.wildcard.methods) return node.wildcard.methods[method] || node.wildcard.methods[""];
|
|
}
|
|
function routeToRegExp(route = "/") {
|
|
const reSegments = [];
|
|
let idCtr = 0;
|
|
for (const segment of route.split("/")) {
|
|
if (!segment) continue;
|
|
if (segment === "*") reSegments.push(`(?<_${idCtr++}>[^/]*)`);
|
|
else if (segment.startsWith("**")) reSegments.push(segment === "**" ? "?(?<_>.*)" : `?(?<${segment.slice(3)}>.+)`);
|
|
else if (segment.includes(":")) reSegments.push(segment.replace(/:(\w+)/g, (_, id) => `(?<${id}>[^/]+)`).replace(/\./g, "\\."));
|
|
else reSegments.push(segment);
|
|
}
|
|
return /* @__PURE__ */ new RegExp(`^/${reSegments.join("/")}/?$`);
|
|
}
|
|
|
|
//#endregion
|
|
//#region ../../node_modules/.pnpm/h3@2.0.1-rc.6_crossws@0.4.1_srvx@0.9.8_/node_modules/h3/dist/h3.mjs
|
|
const kEventNS = "h3.internal.event.";
|
|
const kEventRes = /* @__PURE__ */ Symbol.for(`${kEventNS}res`);
|
|
const kEventResHeaders = /* @__PURE__ */ Symbol.for(`${kEventNS}res.headers`);
|
|
var H3Event = class {
|
|
app;
|
|
req;
|
|
url;
|
|
context;
|
|
static __is_event__ = true;
|
|
constructor(req, context, app) {
|
|
this.context = context || req.context || new NullProtoObj();
|
|
this.req = req;
|
|
this.app = app;
|
|
const _url = req._url;
|
|
this.url = _url && _url instanceof URL ? _url : new FastURL(req.url);
|
|
}
|
|
get res() {
|
|
return this[kEventRes] ||= new H3EventResponse();
|
|
}
|
|
get runtime() {
|
|
return this.req.runtime;
|
|
}
|
|
waitUntil(promise) {
|
|
this.req.waitUntil?.(promise);
|
|
}
|
|
toString() {
|
|
return `[${this.req.method}] ${this.req.url}`;
|
|
}
|
|
toJSON() {
|
|
return this.toString();
|
|
}
|
|
get node() {
|
|
return this.req.runtime?.node;
|
|
}
|
|
get headers() {
|
|
return this.req.headers;
|
|
}
|
|
get path() {
|
|
return this.url.pathname + this.url.search;
|
|
}
|
|
get method() {
|
|
return this.req.method;
|
|
}
|
|
};
|
|
var H3EventResponse = class {
|
|
status;
|
|
statusText;
|
|
get headers() {
|
|
return this[kEventResHeaders] ||= new Headers();
|
|
}
|
|
};
|
|
const DISALLOWED_STATUS_CHARS = /[^\u0009\u0020-\u007E]/g;
|
|
function sanitizeStatusMessage(statusMessage = "") {
|
|
return statusMessage.replace(DISALLOWED_STATUS_CHARS, "");
|
|
}
|
|
function sanitizeStatusCode(statusCode, defaultStatusCode = 200) {
|
|
if (!statusCode) return defaultStatusCode;
|
|
if (typeof statusCode === "string") statusCode = +statusCode;
|
|
if (statusCode < 100 || statusCode > 599) return defaultStatusCode;
|
|
return statusCode;
|
|
}
|
|
var HTTPError = class HTTPError$1 extends Error {
|
|
get name() {
|
|
return "HTTPError";
|
|
}
|
|
status;
|
|
statusText;
|
|
headers;
|
|
cause;
|
|
data;
|
|
body;
|
|
unhandled;
|
|
static isError(input) {
|
|
return input instanceof Error && input?.name === "HTTPError";
|
|
}
|
|
static status(status, statusText, details) {
|
|
return new HTTPError$1({
|
|
...details,
|
|
statusText,
|
|
status
|
|
});
|
|
}
|
|
constructor(arg1, arg2) {
|
|
let messageInput;
|
|
let details;
|
|
if (typeof arg1 === "string") {
|
|
messageInput = arg1;
|
|
details = arg2;
|
|
} else details = arg1;
|
|
const status = sanitizeStatusCode(details?.status || (details?.cause)?.status || details?.status || details?.statusCode, 500);
|
|
const statusText = sanitizeStatusMessage(details?.statusText || (details?.cause)?.statusText || details?.statusText || details?.statusMessage);
|
|
const message = messageInput || details?.message || (details?.cause)?.message || details?.statusText || details?.statusMessage || [
|
|
"HTTPError",
|
|
status,
|
|
statusText
|
|
].filter(Boolean).join(" ");
|
|
super(message, { cause: details });
|
|
this.cause = details;
|
|
Error.captureStackTrace?.(this, this.constructor);
|
|
this.status = status;
|
|
this.statusText = statusText || void 0;
|
|
const rawHeaders = details?.headers || (details?.cause)?.headers;
|
|
this.headers = rawHeaders ? new Headers(rawHeaders) : void 0;
|
|
this.unhandled = details?.unhandled ?? (details?.cause)?.unhandled ?? void 0;
|
|
this.data = details?.data;
|
|
this.body = details?.body;
|
|
}
|
|
get statusCode() {
|
|
return this.status;
|
|
}
|
|
get statusMessage() {
|
|
return this.statusText;
|
|
}
|
|
toJSON() {
|
|
const unhandled = this.unhandled;
|
|
return {
|
|
status: this.status,
|
|
statusText: this.statusText,
|
|
unhandled,
|
|
message: unhandled ? "HTTPError" : this.message,
|
|
data: unhandled ? void 0 : this.data,
|
|
...unhandled ? void 0 : this.body
|
|
};
|
|
}
|
|
};
|
|
function isJSONSerializable(value, _type) {
|
|
if (value === null || value === void 0) return true;
|
|
if (_type !== "object") return _type === "boolean" || _type === "number" || _type === "string";
|
|
if (typeof value.toJSON === "function") return true;
|
|
if (Array.isArray(value)) return true;
|
|
if (typeof value.pipe === "function" || typeof value.pipeTo === "function") return false;
|
|
if (value instanceof NullProtoObj) return true;
|
|
const proto = Object.getPrototypeOf(value);
|
|
return proto === Object.prototype || proto === null;
|
|
}
|
|
const kNotFound = /* @__PURE__ */ Symbol.for("h3.notFound");
|
|
const kHandled = /* @__PURE__ */ Symbol.for("h3.handled");
|
|
function toResponse(val, event, config = {}) {
|
|
if (typeof val?.then === "function") return (val.catch?.((error) => error) || Promise.resolve(val)).then((resolvedVal) => toResponse(resolvedVal, event, config));
|
|
const response = prepareResponse(val, event, config);
|
|
if (typeof response?.then === "function") return toResponse(response, event, config);
|
|
const { onResponse: onResponse$1 } = config;
|
|
return onResponse$1 ? Promise.resolve(onResponse$1(response, event)).then(() => response) : response;
|
|
}
|
|
var HTTPResponse = class {
|
|
#headers;
|
|
#init;
|
|
body;
|
|
constructor(body, init) {
|
|
this.body = body;
|
|
this.#init = init;
|
|
}
|
|
get status() {
|
|
return this.#init?.status || 200;
|
|
}
|
|
get statusText() {
|
|
return this.#init?.statusText || "OK";
|
|
}
|
|
get headers() {
|
|
return this.#headers ||= new Headers(this.#init?.headers);
|
|
}
|
|
};
|
|
function prepareResponse(val, event, config, nested) {
|
|
if (val === kHandled) return new FastResponse(null);
|
|
if (val === kNotFound) val = new HTTPError({
|
|
status: 404,
|
|
message: `Cannot find any route matching [${event.req.method}] ${event.url}`
|
|
});
|
|
if (val && val instanceof Error) {
|
|
const isHTTPError = HTTPError.isError(val);
|
|
const error = isHTTPError ? val : new HTTPError(val);
|
|
if (!isHTTPError) {
|
|
error.unhandled = true;
|
|
if (val?.stack) error.stack = val.stack;
|
|
}
|
|
if (error.unhandled && !config.silent) console.error(error);
|
|
const { onError: onError$1 } = config;
|
|
return onError$1 && !nested ? Promise.resolve(onError$1(error, event)).catch((error$1) => error$1).then((newVal) => prepareResponse(newVal ?? val, event, config, true)) : errorResponse(error, config.debug);
|
|
}
|
|
const preparedRes = event[kEventRes];
|
|
const preparedHeaders = preparedRes?.[kEventResHeaders];
|
|
event[kEventRes] = void 0;
|
|
if (!(val instanceof Response)) {
|
|
const res = prepareResponseBody(val, event, config);
|
|
const status = res.status || preparedRes?.status;
|
|
return new FastResponse(nullBody(event.req.method, status) ? null : res.body, {
|
|
status,
|
|
statusText: res.statusText || preparedRes?.statusText,
|
|
headers: res.headers && preparedHeaders ? mergeHeaders$1(res.headers, preparedHeaders) : res.headers || preparedHeaders
|
|
});
|
|
}
|
|
if (!preparedHeaders || nested || !val.ok) return val;
|
|
try {
|
|
mergeHeaders$1(val.headers, preparedHeaders, val.headers);
|
|
return val;
|
|
} catch {
|
|
return new FastResponse(nullBody(event.req.method, val.status) ? null : val.body, {
|
|
status: val.status,
|
|
statusText: val.statusText,
|
|
headers: mergeHeaders$1(val.headers, preparedHeaders)
|
|
});
|
|
}
|
|
}
|
|
function mergeHeaders$1(base, overrides, target = new Headers(base)) {
|
|
for (const [name, value] of overrides) if (name === "set-cookie") target.append(name, value);
|
|
else target.set(name, value);
|
|
return target;
|
|
}
|
|
const frozenHeaders = () => {
|
|
throw new Error("Headers are frozen");
|
|
};
|
|
var FrozenHeaders = class extends Headers {
|
|
constructor(init) {
|
|
super(init);
|
|
this.set = this.append = this.delete = frozenHeaders;
|
|
}
|
|
};
|
|
const emptyHeaders = /* @__PURE__ */ new FrozenHeaders({ "content-length": "0" });
|
|
const jsonHeaders = /* @__PURE__ */ new FrozenHeaders({ "content-type": "application/json;charset=UTF-8" });
|
|
function prepareResponseBody(val, event, config) {
|
|
if (val === null || val === void 0) return {
|
|
body: "",
|
|
headers: emptyHeaders
|
|
};
|
|
const valType = typeof val;
|
|
if (valType === "string") return { body: val };
|
|
if (val instanceof Uint8Array) {
|
|
event.res.headers.set("content-length", val.byteLength.toString());
|
|
return { body: val };
|
|
}
|
|
if (val instanceof HTTPResponse || val?.constructor?.name === "HTTPResponse") return val;
|
|
if (isJSONSerializable(val, valType)) return {
|
|
body: JSON.stringify(val, void 0, config.debug ? 2 : void 0),
|
|
headers: jsonHeaders
|
|
};
|
|
if (valType === "bigint") return {
|
|
body: val.toString(),
|
|
headers: jsonHeaders
|
|
};
|
|
if (val instanceof Blob) {
|
|
const headers = new Headers({
|
|
"content-type": val.type,
|
|
"content-length": val.size.toString()
|
|
});
|
|
let filename = val.name;
|
|
if (filename) {
|
|
filename = encodeURIComponent(filename);
|
|
headers.set("content-disposition", `filename="${filename}"; filename*=UTF-8''${filename}`);
|
|
}
|
|
return {
|
|
body: val.stream(),
|
|
headers
|
|
};
|
|
}
|
|
if (valType === "symbol") return { body: val.toString() };
|
|
if (valType === "function") return { body: `${val.name}()` };
|
|
return { body: val };
|
|
}
|
|
function nullBody(method, status) {
|
|
return method === "HEAD" || status === 100 || status === 101 || status === 102 || status === 204 || status === 205 || status === 304;
|
|
}
|
|
function errorResponse(error, debug) {
|
|
return new FastResponse(JSON.stringify({
|
|
...error.toJSON(),
|
|
stack: debug && error.stack ? error.stack.split("\n").map((l) => l.trim()) : void 0
|
|
}, void 0, debug ? 2 : void 0), {
|
|
status: error.status,
|
|
statusText: error.statusText,
|
|
headers: error.headers ? mergeHeaders$1(jsonHeaders, error.headers) : new Headers(jsonHeaders)
|
|
});
|
|
}
|
|
function normalizeMiddleware(input, opts = {}) {
|
|
const matcher = createMatcher(opts);
|
|
if (!matcher && (input.length > 1 || input.constructor?.name === "AsyncFunction")) return input;
|
|
return (event, next) => {
|
|
if (matcher && !matcher(event)) return next();
|
|
const res = input(event, next);
|
|
return res === void 0 || res === kNotFound ? next() : res;
|
|
};
|
|
}
|
|
function createMatcher(opts) {
|
|
if (!opts.route && !opts.method && !opts.match) return;
|
|
const routeMatcher = opts.route ? routeToRegExp(opts.route) : void 0;
|
|
const method = opts.method?.toUpperCase();
|
|
return function _middlewareMatcher(event) {
|
|
if (method && event.req.method !== method) return false;
|
|
if (opts.match && !opts.match(event)) return false;
|
|
if (!routeMatcher) return true;
|
|
const match = event.url.pathname.match(routeMatcher);
|
|
if (!match) return false;
|
|
if (match.groups) event.context.middlewareParams = {
|
|
...event.context.middlewareParams,
|
|
...match.groups
|
|
};
|
|
return true;
|
|
};
|
|
}
|
|
function callMiddleware(event, middleware, handler, index = 0) {
|
|
if (index === middleware.length) return handler(event);
|
|
const fn = middleware[index];
|
|
let nextCalled;
|
|
let nextResult;
|
|
const next = () => {
|
|
if (nextCalled) return nextResult;
|
|
nextCalled = true;
|
|
nextResult = callMiddleware(event, middleware, handler, index + 1);
|
|
return nextResult;
|
|
};
|
|
const ret = fn(event, next);
|
|
return isUnhandledResponse(ret) ? next() : typeof ret?.then === "function" ? ret.then((resolved) => isUnhandledResponse(resolved) ? next() : resolved) : ret;
|
|
}
|
|
function isUnhandledResponse(val) {
|
|
return val === void 0 || val === kNotFound;
|
|
}
|
|
function toRequest(input, options) {
|
|
if (typeof input === "string") {
|
|
let url = input;
|
|
if (url[0] === "/") {
|
|
const headers = options?.headers ? new Headers(options.headers) : void 0;
|
|
const host = headers?.get("host") || "localhost";
|
|
url = `${headers?.get("x-forwarded-proto") === "https" ? "https" : "http"}://${host}${url}`;
|
|
}
|
|
return new Request(url, options);
|
|
} else if (options || input instanceof URL) return new Request(input, options);
|
|
return input;
|
|
}
|
|
function defineHandler(input) {
|
|
if (typeof input === "function") return handlerWithFetch(input);
|
|
const handler = input.handler || (input.fetch ? function _fetchHandler(event) {
|
|
return input.fetch(event.req);
|
|
} : NoHandler);
|
|
return Object.assign(handlerWithFetch(input.middleware?.length ? function _handlerMiddleware(event) {
|
|
return callMiddleware(event, input.middleware, handler);
|
|
} : handler), input);
|
|
}
|
|
function handlerWithFetch(handler) {
|
|
if ("fetch" in handler) return handler;
|
|
return Object.assign(handler, { fetch: (req) => {
|
|
if (typeof req === "string") req = new URL(req, "http://_");
|
|
if (req instanceof URL) req = new Request(req);
|
|
const event = new H3Event(req);
|
|
try {
|
|
return Promise.resolve(toResponse(handler(event), event));
|
|
} catch (error) {
|
|
return Promise.resolve(toResponse(error, event));
|
|
}
|
|
} });
|
|
}
|
|
function defineLazyEventHandler(loader) {
|
|
let handler;
|
|
let promise;
|
|
const resolveLazyHandler = () => {
|
|
if (handler) return Promise.resolve(handler);
|
|
return promise ??= Promise.resolve(loader()).then((r) => {
|
|
handler = toEventHandler(r) || toEventHandler(r.default);
|
|
if (typeof handler !== "function") throw new TypeError("Invalid lazy handler", { cause: { resolved: r } });
|
|
return handler;
|
|
});
|
|
};
|
|
return defineHandler(function lazyHandler(event) {
|
|
return handler ? handler(event) : resolveLazyHandler().then((r) => r(event));
|
|
});
|
|
}
|
|
function toEventHandler(handler) {
|
|
if (typeof handler === "function") return handler;
|
|
if (typeof handler?.handler === "function") return handler.handler;
|
|
if (typeof handler?.fetch === "function") return function _fetchHandler(event) {
|
|
return handler.fetch(event.req);
|
|
};
|
|
}
|
|
const NoHandler = () => kNotFound;
|
|
var H3Core = class {
|
|
config;
|
|
"~middleware";
|
|
"~routes" = [];
|
|
constructor(config = {}) {
|
|
this["~middleware"] = [];
|
|
this.config = config;
|
|
this.fetch = this.fetch.bind(this);
|
|
this.handler = this.handler.bind(this);
|
|
}
|
|
fetch(request) {
|
|
return this["~request"](request);
|
|
}
|
|
handler(event) {
|
|
const route = this["~findRoute"](event);
|
|
if (route) {
|
|
event.context.params = route.params;
|
|
event.context.matchedRoute = route.data;
|
|
}
|
|
const routeHandler = route?.data.handler || NoHandler;
|
|
const middleware = this["~getMiddleware"](event, route);
|
|
return middleware.length > 0 ? callMiddleware(event, middleware, routeHandler) : routeHandler(event);
|
|
}
|
|
"~request"(request, context) {
|
|
const event = new H3Event(request, context, this);
|
|
let handlerRes;
|
|
try {
|
|
if (this.config.onRequest) {
|
|
const hookRes = this.config.onRequest(event);
|
|
handlerRes = typeof hookRes?.then === "function" ? hookRes.then(() => this.handler(event)) : this.handler(event);
|
|
} else handlerRes = this.handler(event);
|
|
} catch (error) {
|
|
handlerRes = Promise.reject(error);
|
|
}
|
|
return toResponse(handlerRes, event, this.config);
|
|
}
|
|
"~findRoute"(_event) {}
|
|
"~addRoute"(_route) {
|
|
this["~routes"].push(_route);
|
|
}
|
|
"~getMiddleware"(_event, route) {
|
|
const routeMiddleware = route?.data.middleware;
|
|
const globalMiddleware = this["~middleware"];
|
|
return routeMiddleware ? [...globalMiddleware, ...routeMiddleware] : globalMiddleware;
|
|
}
|
|
};
|
|
const H3 = /* @__PURE__ */ (() => {
|
|
class H3$1 extends H3Core {
|
|
"~rou3";
|
|
constructor(config = {}) {
|
|
super(config);
|
|
this["~rou3"] = createRouter();
|
|
this.request = this.request.bind(this);
|
|
config.plugins?.forEach((plugin) => plugin(this));
|
|
}
|
|
register(plugin) {
|
|
plugin(this);
|
|
return this;
|
|
}
|
|
request(_req, _init, context) {
|
|
return this["~request"](toRequest(_req, _init), context);
|
|
}
|
|
mount(base, input) {
|
|
if ("handler" in input) {
|
|
if (input["~middleware"].length > 0) this["~middleware"].push((event, next) => {
|
|
const originalPathname = event.url.pathname;
|
|
if (!originalPathname.startsWith(base)) return next();
|
|
event.url.pathname = event.url.pathname.slice(base.length) || "/";
|
|
return callMiddleware(event, input["~middleware"], () => {
|
|
event.url.pathname = originalPathname;
|
|
return next();
|
|
});
|
|
});
|
|
for (const r of input["~routes"]) this["~addRoute"]({
|
|
...r,
|
|
route: base + r.route
|
|
});
|
|
} else {
|
|
const fetchHandler = "fetch" in input ? input.fetch : input;
|
|
this.all(`${base}/**`, function _mountedMiddleware(event) {
|
|
const url = new URL(event.url);
|
|
url.pathname = url.pathname.slice(base.length) || "/";
|
|
return fetchHandler(new Request(url, event.req));
|
|
});
|
|
}
|
|
return this;
|
|
}
|
|
on(method, route, handler, opts) {
|
|
const _method = (method || "").toUpperCase();
|
|
route = new URL(route, "http://_").pathname;
|
|
this["~addRoute"]({
|
|
method: _method,
|
|
route,
|
|
handler: toEventHandler(handler),
|
|
middleware: opts?.middleware,
|
|
meta: {
|
|
...handler.meta,
|
|
...opts?.meta
|
|
}
|
|
});
|
|
return this;
|
|
}
|
|
all(route, handler, opts) {
|
|
return this.on("", route, handler, opts);
|
|
}
|
|
"~findRoute"(_event) {
|
|
return findRoute(this["~rou3"], _event.req.method, _event.url.pathname);
|
|
}
|
|
"~addRoute"(_route) {
|
|
addRoute(this["~rou3"], _route.method, _route.route, _route);
|
|
super["~addRoute"](_route);
|
|
}
|
|
use(arg1, arg2, arg3) {
|
|
let route;
|
|
let fn;
|
|
let opts;
|
|
if (typeof arg1 === "string") {
|
|
route = arg1;
|
|
fn = arg2;
|
|
opts = arg3;
|
|
} else {
|
|
fn = arg1;
|
|
opts = arg2;
|
|
}
|
|
this["~middleware"].push(normalizeMiddleware(fn, {
|
|
...opts,
|
|
route
|
|
}));
|
|
return this;
|
|
}
|
|
}
|
|
for (const method of [
|
|
"GET",
|
|
"POST",
|
|
"PUT",
|
|
"DELETE",
|
|
"PATCH",
|
|
"HEAD",
|
|
"OPTIONS",
|
|
"CONNECT",
|
|
"TRACE"
|
|
]) H3Core.prototype[method.toLowerCase()] = function(route, handler, opts) {
|
|
return this.on(method, route, handler, opts);
|
|
};
|
|
return H3$1;
|
|
})();
|
|
const lazyEventHandler = defineLazyEventHandler;
|
|
|
|
//#endregion
|
|
//#region ../nuxi/src/commands/analyze.ts
|
|
const indexHtml = `
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Nuxt Bundle Stats (experimental)</title>
|
|
</head>
|
|
<h1>Nuxt Bundle Stats (experimental)</h1>
|
|
<ul>
|
|
<li>
|
|
<a href="/nitro">Nitro server bundle stats</a>
|
|
</li>
|
|
<li>
|
|
<a href="/client">Client bundle stats</a>
|
|
</li>
|
|
</ul>
|
|
</html>
|
|
`.trim();
|
|
var analyze_default = defineCommand({
|
|
meta: {
|
|
name: "analyze",
|
|
description: "Build nuxt and analyze production bundle (experimental)"
|
|
},
|
|
args: {
|
|
...cwdArgs,
|
|
...logLevelArgs,
|
|
...legacyRootDirArgs,
|
|
...dotEnvArgs,
|
|
...extendsArgs,
|
|
name: {
|
|
type: "string",
|
|
description: "Name of the analysis",
|
|
default: "default",
|
|
valueHint: "name"
|
|
},
|
|
serve: {
|
|
type: "boolean",
|
|
description: "Serve the analysis results",
|
|
negativeDescription: "Skip serving the analysis results",
|
|
default: true
|
|
}
|
|
},
|
|
async run(ctx) {
|
|
overrideEnv("production");
|
|
const cwd = resolve(ctx.args.cwd || ctx.args.rootDir);
|
|
const name = ctx.args.name || "default";
|
|
const slug = name.trim().replace(/[^\w-]/g, "_");
|
|
intro(colors.cyan("Analyzing bundle size..."));
|
|
const startTime = Date.now();
|
|
const { loadNuxt, buildNuxt } = await loadKit(cwd);
|
|
const nuxt = await loadNuxt({
|
|
cwd,
|
|
dotenv: {
|
|
cwd,
|
|
fileName: ctx.args.dotenv
|
|
},
|
|
overrides: defu$1(ctx.data?.overrides, {
|
|
...ctx.args.extends && { extends: ctx.args.extends },
|
|
build: { analyze: { enabled: true } },
|
|
vite: { build: { rollupOptions: { output: {
|
|
chunkFileNames: "_nuxt/[name].js",
|
|
entryFileNames: "_nuxt/[name].js"
|
|
} } } },
|
|
logLevel: ctx.args.logLevel
|
|
})
|
|
});
|
|
const analyzeDir = nuxt.options.analyzeDir;
|
|
const buildDir = nuxt.options.buildDir;
|
|
const outDir = nuxt.options.nitro.output?.dir || join(nuxt.options.rootDir, ".output");
|
|
nuxt.options.build.analyze = defu$1(nuxt.options.build.analyze, { filename: join(analyzeDir, "client.html") });
|
|
const tasklog = taskLog({
|
|
title: "Building Nuxt with analysis enabled",
|
|
retainLog: false,
|
|
limit: 1
|
|
});
|
|
tasklog.message("Clearing analyze directory...");
|
|
await clearDir(analyzeDir);
|
|
tasklog.message("Building Nuxt...");
|
|
await buildNuxt(nuxt);
|
|
tasklog.success("Build complete");
|
|
const meta = {
|
|
name,
|
|
slug,
|
|
startTime,
|
|
endTime: Date.now(),
|
|
analyzeDir,
|
|
buildDir,
|
|
outDir
|
|
};
|
|
await nuxt.callHook("build:analyze:done", meta);
|
|
await promises.writeFile(join(analyzeDir, "meta.json"), JSON.stringify(meta, null, 2), "utf-8");
|
|
note(`${relativeToProcess(analyzeDir)}\n\nDo not deploy analyze results! Use ${colors.cyan("nuxt build")} before deploying.`, "Build location");
|
|
if (ctx.args.serve !== false && !process.env.CI) {
|
|
const app = new H3();
|
|
const opts = { headers: { "content-type": "text/html" } };
|
|
const serveFile = (filePath) => lazyEventHandler(async () => {
|
|
const contents = await promises.readFile(filePath, "utf-8");
|
|
return () => new Response(contents, opts);
|
|
});
|
|
logger.step("Starting stats server...");
|
|
app.use("/client", serveFile(join(analyzeDir, "client.html")));
|
|
app.use("/nitro", serveFile(join(analyzeDir, "nitro.html")));
|
|
app.use(() => new Response(indexHtml, opts));
|
|
await serve(app).serve();
|
|
} else outro("✨ Analysis build complete!");
|
|
}
|
|
});
|
|
|
|
//#endregion
|
|
export { analyze_default as default }; |