From f0b82d535e4718d7e1f3e2464fe918f693d0a26c Mon Sep 17 00:00:00 2001 From: Kat <49850194+datkat21@users.noreply.github.com> Date: Tue, 7 Nov 2023 00:54:28 -0600 Subject: [PATCH] Publish --- README.md | 57 ++------------ examples/helloWorld.ts | 12 +++ examples/test_foh.ts | 29 +++++++ examples/test_server.ts | 48 ++++++++++++ handlers/handling.ts | 113 +++++++++++++++++++++++++++ handlers/requestHandler.ts | 25 ------ handlers/serverConstructor.ts | 141 ++++++++++++++++------------------ index.ts | 17 +--- package.json | 4 +- public/index.html | 2 +- public/sample.png | Bin 0 -> 8428 bytes 11 files changed, 282 insertions(+), 166 deletions(-) create mode 100644 examples/helloWorld.ts create mode 100644 examples/test_foh.ts create mode 100644 examples/test_server.ts create mode 100644 handlers/handling.ts delete mode 100644 handlers/requestHandler.ts create mode 100644 public/sample.png diff --git a/README.md b/README.md index 10c9d7f..36e6520 100644 --- a/README.md +++ b/README.md @@ -3,58 +3,13 @@ Mini-web server for Bun.js. This minimal server thing extends `Bun.serve()` and creates a simple wrapper with useful features such as a public directory, websocket support built-in, etc. -## Usage +## Specification -This isn't exactly a library (yet) so you'll have to copy the files from `handlers` into your project. - -Here are some server examples: - -**Basic HTTP server** -```ts -import { requestHandler } from "./handlers/requestHandler"; -import { ReqHandlerType, makeServer } from "./handlers/serverConstructor"; - -const server = makeServer({ - // Set up the server basics - port: 3000, - publicFolderPath: "./public", - - // Set up the request handler - requestHandler, - requestHandlerType: ReqHandlerType.UrlOnly -}); - -console.log(`Listening on localhost:${server.port}`); -``` - -**Basic websocket server** -```ts -import { requestHandler } from "./handlers/requestHandler"; -import { ReqHandlerType, makeServer } from "./handlers/serverConstructor"; - -const server = makeServer({ - // Set up the server basics - port: 3000, - publicFolderPath: "./public", - - // Set up the request handler - requestHandler, - requestHandlerType: ReqHandlerType.UrlAndWs, - - // Instance of WebSocketHandler - websocketConfig: { - message(ws, message) { - console.log(`${ws.data} said: ${message}`); - }, - // . . . (open, close, ping, pong, etc.) - }, - // Also see websocketHandler to set custom `ws.data`. -}); - -console.log(`Listening on localhost:${server.port}`); -``` - -## Setup +- Custom Handlers +- File-only mode +- WebSockets +- Very easy to understand configuration +- Ease of use handler builders To install dependencies: diff --git a/examples/helloWorld.ts b/examples/helloWorld.ts new file mode 100644 index 0000000..343dbff --- /dev/null +++ b/examples/helloWorld.ts @@ -0,0 +1,12 @@ +import miniDeluxe from "../index"; +const m = miniDeluxe.miniServer({ + port: 3000, + handlers: [ + { + urlPath: "/", + handler: async function (req: Request) { + return new Response("Hello, world!"); + }, + }, + ], +}); \ No newline at end of file diff --git a/examples/test_foh.ts b/examples/test_foh.ts new file mode 100644 index 0000000..57dd1dc --- /dev/null +++ b/examples/test_foh.ts @@ -0,0 +1,29 @@ +import { + createCookie, + editCookie, + pageBuilder, + rawFile, +} from "../handlers/handling"; +import miniDeluxe from "../index"; + +function fofHandler() { + return new Response( + `
The file you were looking for does not exist.
`, + { + status: 404, + headers: { + "Content-Type": "text/html", + }, + statusText: "Not Found", + } + ); +} + +const m = miniDeluxe.miniServer({ + port: 3000, + fofHandler, + options: { + fileOnlyMode: true, + staticFilePath: "../public", + }, +}); diff --git a/examples/test_server.ts b/examples/test_server.ts new file mode 100644 index 0000000..21586ff --- /dev/null +++ b/examples/test_server.ts @@ -0,0 +1,48 @@ +import { + createCookie, + editCookie, + pageBuilder, + rawFile, +} from "../handlers/handling"; +import miniDeluxe from "../index"; + +const m = miniDeluxe.miniServer({ + port: 3000, + // options: { + // fileOnlyMode: true + // }, + handlers: [ + { + urlPath: "/", + handler: async function (req: Request) { + return pageBuilder( + "${ + error.message + }
${error.stack + ?.replaceAll("<", "<") + .replaceAll(">", ">")}`, + { + status: 500, + headers: { + "Content-Type": "text/html", + }, + statusText: "Internal Server Error", + } + ); }; - console.log("[debug]", "fetchFunction for default handler"); - } else if (config.requestHandlerType === ReqHandlerType.Custom) { - fetchFunction = async function fetch(req: Request, server: Server) { - return await config.requestHandler(req, server); + if (!config.options) + config.options = { + enableWs: false, + fileOnlyMode: false, + staticFilePath: "public", }; - console.log("[debug]", "fetchFunction for custom handler"); - } else if ( - type === ServerType.SupportWs || - config.requestHandlerType === ReqHandlerType.UrlAndWs - ) { - fetchFunction = async function fetch(req: Request, server: Server) { - // determine if this is a websocket or http connection - if ( - req.headers.get("Connection") && - req.headers.get("Connection")?.includes("Upgrade") && - req.headers.get("Upgrade") !== null && - req.headers.get("Upgrade") === "websocket" - ) { - // Assume this is a WebSocket since we have verified the headers - if (config.websocketHandler === undefined) { - server.upgrade(req, { - // Example data passed to the websocket handler. - data: performance.now(), - }); - } else await config.websocketHandler(server, req); - } else { - // Fallback to request handler - return await config.requestHandler(new URL(req.url)); - } - }; - console.log("[debug]", "fetchFunction for ws handler"); + if (config.handlers === undefined && config.options.fileOnlyMode !== true) { + /** + * This text shows up somewhere + */ + throw new Error("No handlers assigned to your server"); } + if (!config.handlers) config.handlers = []; - if ( - type === ServerType.SupportWs || - config.requestHandlerType === ReqHandlerType.UrlAndWs - ) { - console.log("[debug]", "fetchFunction set on ws..."); - return Bun.serve({ - fetch: fetchFunction, - websocket: config.websocketConfig, - }); - } else { - // Assume httponly - console.log("[debug]", "fetchFunction set on httponly..."); - return Bun.serve({ - port: 3000, - fetch: fetchFunction, - }); - } + // Generate request handler + const fetchHandler = + config.options.fileOnlyMode == false + ? generateRequestHandler(config.handlers, config.fofHandler) + : fileOnlyReqHandler(config.options.staticFilePath); + + // Return final server + return Bun.serve({ + port: config.port, + fetch: fetchHandler, + error: config.errorHandler, + }); } diff --git a/index.ts b/index.ts index 1fff628..a45b72f 100644 --- a/index.ts +++ b/index.ts @@ -1,14 +1,5 @@ -import { requestHandler } from "./handlers/requestHandler"; -import { ReqHandlerType, makeServer } from "./handlers/serverConstructor"; +import { miniServer } from "./handlers/serverConstructor"; -const server = makeServer({ - // Set up the server basics - port: 3000, - publicFolderPath: "./public", - - // Set up the request handler - requestHandler, - requestHandlerType: ReqHandlerType.UrlOnly -}); - -console.log(`Listening on localhost:${server.port}`); +export default { + miniServer +}; diff --git a/package.json b/package.json index 85fe6bd..127d592 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,7 @@ { - "name": "silly-web-server", + "name": "mini-deluxe", + "description": "A minimal HTTP server library by cherries.to devs", + "version": "0.0.1", "module": "index.ts", "type": "module", "devDependencies": { diff --git a/public/index.html b/public/index.html index 60217a8..ea1865a 100644 --- a/public/index.html +++ b/public/index.html @@ -6,6 +6,6 @@