This commit is contained in:
2025-03-24 14:35:08 +01:00
parent 5c8a4c1edc
commit 277f5a313e
10 changed files with 81 additions and 234 deletions

5
dist/container.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
export default class Container {
private services;
register<T>(name: string, factory: () => T): void;
resolve<T>(name: string): T;
}

14
dist/container.js vendored Normal file
View File

@ -0,0 +1,14 @@
export default class Container {
constructor() {
this.services = new Map();
}
register(name, factory) {
this.services.set(name, factory);
}
resolve(name) {
const factory = this.services.get(name);
if (!factory)
throw new Error(`Service "${name}" not found.`);
return factory();
}
}

7
dist/index.d.ts vendored
View File

@ -3,11 +3,12 @@ import Tpl from "./template.js";
export { Router, Tpl, Request, Response, Handler };
export default class Flummpress {
private server?;
private container;
private middleware;
router: Router;
tpl: Tpl;
middleware: Handler[];
constructor();
use(plugin: Router | Tpl | Handler): this;
use<T>(nameOrRouter: string | Router, factory?: () => T): this;
resolve<T>(name: string): T;
private processPipeline;
listen(...args: any[]): this;
private parseRequest;

23
dist/index.js vendored
View File

@ -11,23 +11,27 @@ import http from "node:http";
import { URL } from "node:url";
import querystring from "node:querystring";
import Router from "./router.js";
import Container from "./container.js";
import Tpl from "./template.js";
export { Router, Tpl };
export default class Flummpress {
constructor() {
this.container = new Container();
this.router = new Router();
this.tpl = new Tpl();
this.middleware = [];
}
use(plugin) {
if (plugin instanceof Router)
this.router.use(plugin);
else if (plugin instanceof Tpl)
this.tpl = plugin;
else if (typeof plugin === "function")
this.middleware.push(plugin);
use(nameOrRouter, factory) {
if (typeof nameOrRouter === "string" && factory)
this.container.register(nameOrRouter, factory);
else if (nameOrRouter instanceof Router)
this.router.use(nameOrRouter);
else
throw new TypeError("Invalid arguments provided to use()");
return this;
}
resolve(name) {
return this.container.resolve(name);
}
processPipeline(handlers, req, res) {
return __awaiter(this, void 0, void 0, function* () {
for (const handler of handlers) {
@ -69,7 +73,8 @@ export default class Flummpress {
`${req.method} ${res.statusCode}`,
req.url.pathname,
].join(" | "));
})).listen(...args);
}));
this.server.listen(...args);
return this;
}
parseRequest(request) {

6
dist/router.d.ts vendored
View File

@ -1,5 +1,4 @@
import { IncomingMessage, ServerResponse } from "node:http";
import Tpl from "./template.js";
export interface Request extends Omit<IncomingMessage, 'url'> {
url: {
pathname: string;
@ -25,13 +24,12 @@ export interface Response extends ServerResponse {
export type Handler = (req: Request, res: Response, next?: () => void) => void | Promise<void>;
export default class Router {
private routes;
private tpl?;
private mimes;
constructor();
importRoutesFromPath(p: string, tpl?: Tpl | false): Promise<this>;
importRoutesFromPath(p: string): Promise<this>;
group(basePath: string | RegExp, callback: (methods: any) => void): this;
private combinePaths;
use(obj: Router | Tpl): void;
use(obj: Router): void;
get(path: string | RegExp, ...callback: Handler[]): this;
post(path: string | RegExp, ...callback: Handler[]): this;
head(path: string | RegExp, ...callback: Handler[]): this;

10
dist/router.js vendored
View File

@ -9,19 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
import fs from "node:fs";
import path from "node:path";
import Tpl from "./template.js";
export default class Router {
constructor() {
this.routes = [];
this.mimes = new Map();
}
importRoutesFromPath(p_1) {
return __awaiter(this, arguments, void 0, function* (p, tpl = false) {
importRoutesFromPath(p) {
return __awaiter(this, void 0, void 0, function* () {
const dirEntries = yield fs.promises.readdir(path.resolve() + '/' + p, { withFileTypes: true });
for (const tmp of dirEntries) {
if (tmp.isFile() && (tmp.name.endsWith(".mjs") || tmp.name.endsWith(".js"))) {
const routeModule = (yield import(`${path.resolve()}/${p}/${tmp.name}`)).default;
this.use(routeModule(this, tpl));
this.use(routeModule(this));
}
else if (tmp.isDirectory()) {
yield this.importRoutesFromPath(p + '/' + tmp.name);
@ -75,9 +74,6 @@ export default class Router {
this.routes = [...this.routes, ...obj.routes];
this.sortRoutes();
}
if (obj instanceof Tpl) {
this.tpl = obj;
}
}
get(path, ...callback) {
this.registerRoute(path, "get", callback);