262 lines
8.0 KiB
JavaScript
262 lines
8.0 KiB
JavaScript
"use strict";
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
var hono_base_exports = {};
|
|
__export(hono_base_exports, {
|
|
HonoBase: () => Hono
|
|
});
|
|
module.exports = __toCommonJS(hono_base_exports);
|
|
var import_compose = require("./compose");
|
|
var import_context = require("./context");
|
|
var import_router = require("./router");
|
|
var import_constants = require("./utils/constants");
|
|
var import_url = require("./utils/url");
|
|
const notFoundHandler = (c) => {
|
|
return c.text("404 Not Found", 404);
|
|
};
|
|
const errorHandler = (err, c) => {
|
|
if ("getResponse" in err) {
|
|
const res = err.getResponse();
|
|
return c.newResponse(res.body, res);
|
|
}
|
|
console.error(err);
|
|
return c.text("Internal Server Error", 500);
|
|
};
|
|
class Hono {
|
|
get;
|
|
post;
|
|
put;
|
|
delete;
|
|
options;
|
|
patch;
|
|
all;
|
|
on;
|
|
use;
|
|
router;
|
|
getPath;
|
|
_basePath = "/";
|
|
#path = "/";
|
|
routes = [];
|
|
constructor(options = {}) {
|
|
const allMethods = [...import_router.METHODS, import_router.METHOD_NAME_ALL_LOWERCASE];
|
|
allMethods.forEach((method) => {
|
|
this[method] = (args1, ...args) => {
|
|
if (typeof args1 === "string") {
|
|
this.#path = args1;
|
|
} else {
|
|
this.#addRoute(method, this.#path, args1);
|
|
}
|
|
args.forEach((handler) => {
|
|
this.#addRoute(method, this.#path, handler);
|
|
});
|
|
return this;
|
|
};
|
|
});
|
|
this.on = (method, path, ...handlers) => {
|
|
for (const p of [path].flat()) {
|
|
this.#path = p;
|
|
for (const m of [method].flat()) {
|
|
handlers.map((handler) => {
|
|
this.#addRoute(m.toUpperCase(), this.#path, handler);
|
|
});
|
|
}
|
|
}
|
|
return this;
|
|
};
|
|
this.use = (arg1, ...handlers) => {
|
|
if (typeof arg1 === "string") {
|
|
this.#path = arg1;
|
|
} else {
|
|
this.#path = "*";
|
|
handlers.unshift(arg1);
|
|
}
|
|
handlers.forEach((handler) => {
|
|
this.#addRoute(import_router.METHOD_NAME_ALL, this.#path, handler);
|
|
});
|
|
return this;
|
|
};
|
|
const { strict, ...optionsWithoutStrict } = options;
|
|
Object.assign(this, optionsWithoutStrict);
|
|
this.getPath = strict ?? true ? options.getPath ?? import_url.getPath : import_url.getPathNoStrict;
|
|
}
|
|
#clone() {
|
|
const clone = new Hono({
|
|
router: this.router,
|
|
getPath: this.getPath
|
|
});
|
|
clone.errorHandler = this.errorHandler;
|
|
clone.#notFoundHandler = this.#notFoundHandler;
|
|
clone.routes = this.routes;
|
|
return clone;
|
|
}
|
|
#notFoundHandler = notFoundHandler;
|
|
errorHandler = errorHandler;
|
|
route(path, app) {
|
|
const subApp = this.basePath(path);
|
|
app.routes.map((r) => {
|
|
let handler;
|
|
if (app.errorHandler === errorHandler) {
|
|
handler = r.handler;
|
|
} else {
|
|
handler = async (c, next) => (await (0, import_compose.compose)([], app.errorHandler)(c, () => r.handler(c, next))).res;
|
|
handler[import_constants.COMPOSED_HANDLER] = r.handler;
|
|
}
|
|
subApp.#addRoute(r.method, r.path, handler);
|
|
});
|
|
return this;
|
|
}
|
|
basePath(path) {
|
|
const subApp = this.#clone();
|
|
subApp._basePath = (0, import_url.mergePath)(this._basePath, path);
|
|
return subApp;
|
|
}
|
|
onError = (handler) => {
|
|
this.errorHandler = handler;
|
|
return this;
|
|
};
|
|
notFound = (handler) => {
|
|
this.#notFoundHandler = handler;
|
|
return this;
|
|
};
|
|
mount(path, applicationHandler, options) {
|
|
let replaceRequest;
|
|
let optionHandler;
|
|
if (options) {
|
|
if (typeof options === "function") {
|
|
optionHandler = options;
|
|
} else {
|
|
optionHandler = options.optionHandler;
|
|
if (options.replaceRequest === false) {
|
|
replaceRequest = (request) => request;
|
|
} else {
|
|
replaceRequest = options.replaceRequest;
|
|
}
|
|
}
|
|
}
|
|
const getOptions = optionHandler ? (c) => {
|
|
const options2 = optionHandler(c);
|
|
return Array.isArray(options2) ? options2 : [options2];
|
|
} : (c) => {
|
|
let executionContext = void 0;
|
|
try {
|
|
executionContext = c.executionCtx;
|
|
} catch {
|
|
}
|
|
return [c.env, executionContext];
|
|
};
|
|
replaceRequest ||= (() => {
|
|
const mergedPath = (0, import_url.mergePath)(this._basePath, path);
|
|
const pathPrefixLength = mergedPath === "/" ? 0 : mergedPath.length;
|
|
return (request) => {
|
|
const url = new URL(request.url);
|
|
url.pathname = url.pathname.slice(pathPrefixLength) || "/";
|
|
return new Request(url, request);
|
|
};
|
|
})();
|
|
const handler = async (c, next) => {
|
|
const res = await applicationHandler(replaceRequest(c.req.raw), ...getOptions(c));
|
|
if (res) {
|
|
return res;
|
|
}
|
|
await next();
|
|
};
|
|
this.#addRoute(import_router.METHOD_NAME_ALL, (0, import_url.mergePath)(path, "*"), handler);
|
|
return this;
|
|
}
|
|
#addRoute(method, path, handler) {
|
|
method = method.toUpperCase();
|
|
path = (0, import_url.mergePath)(this._basePath, path);
|
|
const r = { basePath: this._basePath, path, method, handler };
|
|
this.router.add(method, path, [handler, r]);
|
|
this.routes.push(r);
|
|
}
|
|
#handleError(err, c) {
|
|
if (err instanceof Error) {
|
|
return this.errorHandler(err, c);
|
|
}
|
|
throw err;
|
|
}
|
|
#dispatch(request, executionCtx, env, method) {
|
|
if (method === "HEAD") {
|
|
return (async () => new Response(null, await this.#dispatch(request, executionCtx, env, "GET")))();
|
|
}
|
|
const path = this.getPath(request, { env });
|
|
const matchResult = this.router.match(method, path);
|
|
const c = new import_context.Context(request, {
|
|
path,
|
|
matchResult,
|
|
env,
|
|
executionCtx,
|
|
notFoundHandler: this.#notFoundHandler
|
|
});
|
|
if (matchResult[0].length === 1) {
|
|
let res;
|
|
try {
|
|
res = matchResult[0][0][0][0](c, async () => {
|
|
c.res = await this.#notFoundHandler(c);
|
|
});
|
|
} catch (err) {
|
|
return this.#handleError(err, c);
|
|
}
|
|
return res instanceof Promise ? res.then(
|
|
(resolved) => resolved || (c.finalized ? c.res : this.#notFoundHandler(c))
|
|
).catch((err) => this.#handleError(err, c)) : res ?? this.#notFoundHandler(c);
|
|
}
|
|
const composed = (0, import_compose.compose)(matchResult[0], this.errorHandler, this.#notFoundHandler);
|
|
return (async () => {
|
|
try {
|
|
const context = await composed(c);
|
|
if (!context.finalized) {
|
|
throw new Error(
|
|
"Context is not finalized. Did you forget to return a Response object or `await next()`?"
|
|
);
|
|
}
|
|
return context.res;
|
|
} catch (err) {
|
|
return this.#handleError(err, c);
|
|
}
|
|
})();
|
|
}
|
|
fetch = (request, ...rest) => {
|
|
return this.#dispatch(request, rest[1], rest[0], request.method);
|
|
};
|
|
request = (input, requestInit, Env, executionCtx) => {
|
|
if (input instanceof Request) {
|
|
return this.fetch(requestInit ? new Request(input, requestInit) : input, Env, executionCtx);
|
|
}
|
|
input = input.toString();
|
|
return this.fetch(
|
|
new Request(
|
|
/^https?:\/\//.test(input) ? input : `http://localhost${(0, import_url.mergePath)("/", input)}`,
|
|
requestInit
|
|
),
|
|
Env,
|
|
executionCtx
|
|
);
|
|
};
|
|
fire = () => {
|
|
addEventListener("fetch", (event) => {
|
|
event.respondWith(this.#dispatch(event.request, event, void 0, event.request.method));
|
|
});
|
|
};
|
|
}
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
HonoBase
|
|
});
|