Skip to content

Commit

Permalink
update node server for middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
ragokan committed Sep 27, 2024
1 parent 322e93c commit 2922dd7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [0.0.30] - 2024-09-27

- Update "createNodeServer" to use static routes and handlers, such as openapi

## [0.0.29] - 2024-09-27

- Add "createNodeServer" wrapper for NodeJS environments
Expand Down
8 changes: 4 additions & 4 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.0.29",
"useNx": true
}
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.0.29",
"useNx": true
}
9 changes: 1 addition & 8 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@
"types": "./dist/index.d.ts"
}
},
"keywords": [
"bunicorn",
"client",
"fast",
"edge",
"bun",
"node"
],
"keywords": ["bunicorn", "client", "fast", "edge", "bun", "node"],
"devDependencies": {
"@bunicorn/server": "workspace:*"
},
Expand Down
6 changes: 1 addition & 5 deletions packages/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
"types": "./dist/index.d.ts"
}
},
"keywords": [
"bunicorn",
"bun",
"openapi"
],
"keywords": ["bunicorn", "bun", "openapi"],
"homepage": "https://github.com/ragokan/bunicorn",
"author": "ragokan",
"license": "MIT",
Expand Down
10 changes: 1 addition & 9 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,7 @@
"types": "./dist/app/createNodeServer.d.ts"
}
},
"keywords": [
"bunicorn",
"server",
"fast",
"edge",
"bun",
"deno",
"node"
],
"keywords": ["bunicorn", "server", "fast", "edge", "bun", "deno", "node"],
"peerDependencies": {
"valibot": "^0.41.0",
"zod": "^3.23.8"
Expand Down
29 changes: 22 additions & 7 deletions packages/server/src/app/createNodeServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { IncomingMessage, ServerResponse } from "node:http";
import { createServer } from "node:http";
import type { Readable } from "node:stream";
import type { BunicornApp } from "./index.ts";
import { Router } from "../router/base.ts";
import type { Route } from "../router/route.ts";
import type { BasePath, BunicornResponse } from "../router/types.ts";
import { BunicornApp } from "./index.ts";

// Convert Node.js Readable to Web API ReadableStream
function nodeReadableToWebReadableStream(
Expand Down Expand Up @@ -68,19 +72,30 @@ async function webToNodeResponse(
nodeResponse.end();
}

export default async function createNodeServer(
handleRequest: BunicornApp<any>["handleRequest"],
) {
export default function createNodeServer(app: BunicornApp<any>) {
// Wrapper for static routes
const router = new Router();
const routes: Route[] = [];
for (const [path, response] of Object.entries(app.staticRoutes)) {
const route = router.get(
path as BasePath,
() => response as BunicornResponse<any>,
);
routes.push(route);
}
app.addRoutes(routes);

// NodeJS HTTP Server
const { createServer } = await import("node:http");
const server = createServer(async (req, res) => {
// Call the handleRequest function
try {
const webRequest = nodeToWebRequest(req);
const webResponse = await handleRequest(webRequest);
const webResponse = await app.handleRequest(webRequest);
await webToNodeResponse(webResponse, res);
} catch (error) {
console.error("Error handling request:", error);
if (error instanceof Error) {
BunicornApp.onGlobalError(error);
}
res.statusCode = 500;
res.end("Internal Server Error");
}
Expand Down
7 changes: 1 addition & 6 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
"types": "./dist/index.d.ts"
}
},
"keywords": [
"bunicorn",
"bun",
"utils",
"utility"
],
"keywords": ["bunicorn", "bun", "utils", "utility"],
"homepage": "https://github.com/ragokan/bunicorn",
"author": "ragokan",
"license": "MIT",
Expand Down

0 comments on commit 2922dd7

Please sign in to comment.