UNPKG

@modern-js/server-core

Version:

A Progressive React Framework for modern web development.

147 lines (146 loc) 4 kB
import { _ as _class_private_method_get } from "@swc/helpers/_/_class_private_method_get"; import { _ as _class_private_method_init } from "@swc/helpers/_/_class_private_method_init"; import { server } from "@modern-js/plugin-v2/server"; import { Hono } from "hono"; import { run } from "./context"; import { handleSetupResult } from "./plugins/compat/hooks"; import { loadConfig } from "./utils"; var _applyMiddlewares = /* @__PURE__ */ new WeakSet(); class ServerBase { /** * Order * - server runner * - apply middlewares */ async init() { const { serverConfig, config: cliConfig } = this.options; const mergedConfig = loadConfig({ cliConfig, serverConfig: serverConfig || {} }); const { serverContext } = await server.run({ plugins: this.plugins, options: this.options, config: mergedConfig, handleSetupResult }); serverContext.serverBase = this; this.serverContext = serverContext; await serverContext.hooks.onPrepare.call(); _class_private_method_get(this, _applyMiddlewares, applyMiddlewares).call(this); return this; } addPlugins(plugins) { this.plugins.push(...plugins); } get hooks() { return this.serverContext.hooks; } get all() { return this.app.all.bind(this.app); } get use() { return this.app.use.bind(this.app); } get get() { return this.app.get.bind(this.app); } get post() { return this.app.post.bind(this.app); } get put() { return this.app.put.bind(this.app); } get delete() { return this.app.delete.bind(this.app); } get patch() { return this.app.patch.bind(this.app); } get handle() { return this.app.fetch.bind(this.app); } get request() { return this.app.request.bind(this.app); } get notFound() { return this.app.notFound.bind(this.app); } get onError() { return this.app.onError.bind(this.app); } constructor(options) { _class_private_method_init(this, _applyMiddlewares); this.plugins = []; this.serverContext = null; this.options = options; this.app = new Hono(); this.app.use("*", run); } } function applyMiddlewares() { const { middlewares } = this.serverContext; const preMiddlewares = []; const defaultMiddlewares = []; const postMiddlewares = []; for (const middleware of middlewares) { switch (middleware.order) { case "pre": preMiddlewares.push(middleware); break; case "post": postMiddlewares.push(middleware); break; default: defaultMiddlewares.push(middleware); } } const finalMiddlewares = []; const insertMiddleware = (middleware) => { if (middleware.before) { const targetIndex = finalMiddlewares.findIndex((item) => { var _middleware_before; if ((_middleware_before = middleware.before) === null || _middleware_before === void 0 ? void 0 : _middleware_before.includes(item.name)) { return true; } else { return false; } }); if (targetIndex !== -1) { finalMiddlewares.splice(targetIndex, 0, middleware); } else { finalMiddlewares.push(middleware); } } else { finalMiddlewares.push(middleware); } }; preMiddlewares.forEach(insertMiddleware); defaultMiddlewares.forEach(insertMiddleware); postMiddlewares.forEach(insertMiddleware); for (const middleware of finalMiddlewares) { const { path = "*", method = "all", handler } = middleware; const handlers = handler2Handlers(handler); this.app[method](path, ...handlers); } function handler2Handlers(handler) { if (Array.isArray(handler)) { return handler; } else { return [ handler ]; } } } function createServerBase(options) { if (options == null) { throw new Error("can not start server without options"); } const server2 = new ServerBase(options); return server2; } export { ServerBase, createServerBase };