From 0ee11d2d6d3b79c79fdd07156da4dfc6164c054c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Moreau?= Date: Tue, 5 Nov 2024 16:38:26 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20You=20can=20now=20use=20a?= =?UTF-8?q?=20custom=20node=20server=20(#14)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📦 NEW: You can now use a custom node server * 📖 DOC: add example --- .../app/server/index.ts | 32 ++++++++++++++++- .../remix-unstable-custom-build/package.json | 1 + package-lock.json | 36 +++++++++++++++++-- package.json | 2 +- src/node.ts | 34 ++++++++++++++++++ 5 files changed, 101 insertions(+), 4 deletions(-) diff --git a/examples/remix-unstable-custom-build/app/server/index.ts b/examples/remix-unstable-custom-build/app/server/index.ts index 76c510e..537f8f5 100644 --- a/examples/remix-unstable-custom-build/app/server/index.ts +++ b/examples/remix-unstable-custom-build/app/server/index.ts @@ -1,12 +1,42 @@ +import { createSecureServer } from "node:http2"; +import mkcert from "mkcert"; import { createHonoServer } from "react-router-hono-server/node"; + import { exampleMiddleware } from "./middleware"; +const getSSLConfig = async () => { + const ca = await mkcert.createCA({ + organization: "Development CA", + countryCode: "US", + state: "California", + locality: "San Francisco", + validity: 365, + }); + + const cert = await mkcert.createCert({ + domains: ["127.0.0.1", "localhost"], + validity: 365, + ca: { key: ca.key, cert: ca.cert }, + }); + + return { cert: cert.cert, key: cert.key }; +}; + +const cert = await getSSLConfig(); + export const server = await createHonoServer({ buildDirectory: "dist", configure(server) { server.use("*", exampleMiddleware()); }, listeningListener(info) { - console.log(`Server is listening on http://localhost:${info.port}`); + console.log(`Server is listening on https://localhost:${info.port}`); + }, + customNodeServer: { + createServer: createSecureServer, + serverOptions: { + cert: cert.cert, + key: cert.key, + }, }, }); diff --git a/examples/remix-unstable-custom-build/package.json b/examples/remix-unstable-custom-build/package.json index fd5ed27..152a76c 100644 --- a/examples/remix-unstable-custom-build/package.json +++ b/examples/remix-unstable-custom-build/package.json @@ -14,6 +14,7 @@ "@remix-run/node": "^2.11.1", "@remix-run/react": "^2.11.1", "isbot": "^4.1.0", + "mkcert": "^3.2.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-hono-server": "*" diff --git a/package-lock.json b/package-lock.json index 023469a..1757c1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "react-router-hono-server", - "version": "0.5.0", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "react-router-hono-server", - "version": "0.5.0", + "version": "1.0.0", "license": "ISC", "workspaces": [ ".", @@ -107,6 +107,7 @@ "@remix-run/node": "^2.11.1", "@remix-run/react": "^2.11.1", "isbot": "^4.1.0", + "mkcert": "^3.2.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-hono-server": "*" @@ -9798,6 +9799,29 @@ "node": ">=8" } }, + "node_modules/mkcert": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/mkcert/-/mkcert-3.2.0.tgz", + "integrity": "sha512-026Eivq9RoOjOuLJGzbhGwXUAjBxRX11Z7Jbm4/7lqT/Av+XNy9SPrJte6+UpEt7i+W3e/HZYxQqlQcqXZWSzg==", + "dependencies": { + "commander": "^11.0.0", + "node-forge": "^1.3.1" + }, + "bin": { + "mkcert": "dist/cli.js" + }, + "engines": { + "node": ">=16" + } + }, + "node_modules/mkcert/node_modules/commander": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-11.1.0.tgz", + "integrity": "sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==", + "engines": { + "node": ">=16" + } + }, "node_modules/mkdirp": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", @@ -9913,6 +9937,14 @@ "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "dev": true }, + "node_modules/node-forge": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", + "integrity": "sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==", + "engines": { + "node": ">= 6.13.0" + } + }, "node_modules/node-releases": { "version": "2.0.18", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz", diff --git a/package.json b/package.json index 863eb3f..897baa9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-router-hono-server", - "version": "0.5.0", + "version": "1.0.0", "description": "This package includes helper function to create an Hono app in your entry.server.tsx file. It allows you to customize your server.", "exports": { "./node": { diff --git a/src/node.ts b/src/node.ts index 295cd55..3aa516f 100644 --- a/src/node.ts +++ b/src/node.ts @@ -1,3 +1,11 @@ +import type { ServerOptions as ServerOptions$1, createServer } from "node:http"; +import type { + SecureServerOptions, + ServerOptions as ServerOptions$3, + createSecureServer, + createServer as createServer$2, +} from "node:http2"; +import type { ServerOptions as ServerOptions$2, createServer as createServer$1 } from "node:https"; import type { AddressInfo } from "node:net"; import path from "node:path"; import url from "node:url"; @@ -9,9 +17,28 @@ import type { HonoOptions } from "hono/hono-base"; import { logger } from "hono/logger"; import type { BlankEnv } from "hono/types"; import { type RemixMiddlewareOptions, remix } from "remix-hono/handler"; + import { importDevBuild } from "./dev-build"; import { cache } from "./middleware"; +type createHttpOptions = { + serverOptions?: ServerOptions$1; + createServer?: typeof createServer; +}; +type createHttpsOptions = { + serverOptions?: ServerOptions$2; + createServer?: typeof createServer$1; +}; +type createHttp2Options = { + serverOptions?: ServerOptions$3; + createServer?: typeof createServer$2; +}; +type createSecureHttp2Options = { + serverOptions?: SecureServerOptions; + createServer?: typeof createSecureServer; +}; +type CreateNodeServerOptions = createHttpOptions | createHttpsOptions | createHttp2Options | createSecureHttp2Options; + export type HonoServerOptions = { /** * Enable the default logger @@ -86,6 +113,12 @@ export type HonoServerOptions = { * {@link HonoOptions} */ honoOptions?: HonoOptions; + /** + * Customize the node server (ex: using http2) + * + * {@link https://hono.dev/docs/getting-started/nodejs#http2} + */ + customNodeServer?: CreateNodeServerOptions; }; const defaultOptions: HonoServerOptions = { @@ -187,6 +220,7 @@ export async function createHonoServer(options: HonoSe serve( { ...server, + ...mergedOptions.customNodeServer, port: mergedOptions.port, }, mergedOptions.listeningListener