Skip to content

Commit

Permalink
feat: add h3 (#2)
Browse files Browse the repository at this point in the history
* feat: h3

* refactor

* minor

---------

Co-authored-by: Thibaut LEBRETON <contact@thibaut-lebreton.fr>
Co-authored-by: Horváth Dániel <nitedani@gmail.com>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent 35eb1e1 commit 12a856c
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/vike-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ app.use(
- Express
- Fastify
- Hono
- H3
- Elysia (Bun)

Express:
Expand Down Expand Up @@ -212,6 +213,28 @@ function startServer() {
}
```

H3:

```js
// server/index.js

import { createApp, toNodeListener } from 'h3'
import { createServer } from 'http'
import vike from 'vike-node/h3'

startServer()

async function startServer() {
const app = createApp()
app.use(vike())
const port = process.env.PORT || 3000
const server = createServer(toNodeListener(app)).listen(port)
server.on('listening', () => {
console.log(`Server running at http://localhost:${port}`)
})
}
```

Elysia (Bun):

```js
Expand Down
5 changes: 5 additions & 0 deletions packages/vike-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"exports": {
"./connect": "./dist/connect.js",
"./fastify": "./dist/fastify.js",
"./h3": "./dist/h3.js",
"./hono": "./dist/hono.js",
"./elysia": "./dist/elysia.js",
"./plugin": "./dist/plugin/index.js"
Expand All @@ -31,6 +32,7 @@
"@brillout/release-me": "^0.4.0",
"@types/node": "^20.14.12",
"fastify": "^4.28.1",
"h3": "^1.12.0",
"hono": "^4.5.1",
"elysia": "^1.1.4",
"typescript": "^5.5.4",
Expand All @@ -45,6 +47,9 @@
"fastify": [
"./dist/fastify.d.ts"
],
"h3": [
"./dist/h3.d.ts"
],
"hono": [
"./dist/hono.d.ts"
],
Expand Down
1 change: 1 addition & 0 deletions packages/vike-node/src/h3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { vike, vike as default } from './runtime/frameworks/h3.js'
50 changes: 50 additions & 0 deletions packages/vike-node/src/runtime/frameworks/h3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export { vike }

import { eventHandler, EventHandler } from 'h3'
import type { IncomingMessage } from 'node:http'
import { globalStore } from '../globalStore.js'
import { createHandler } from '../handler.js'
import type { VikeOptions } from '../types.js'

/**
* Creates an h3 event handler to process Vike requests.
*
* @param {VikeOptions<IncomingMessage>} [options] - Configuration options for Vike.
*
* @returns {EventHandler} An h3 event handler that processes requests with Vike.
*
* @description
* This function creates an h3 event handler that integrates Vike's server-side rendering capabilities.
* The handler:
* 1. Checks for and handles HMR WebSocket upgrade requests.
* 2. Processes regular requests using Vike's handler.
*
* @example
* ```js
* import { createApp } from 'h3'
* import { vike } from 'vike-node/h3'
*
* const app = createApp()
* app.use(vike())
*
* ```
*
* @remarks
* - This handler directly uses Node.js' IncomingMessage and ServerResponse objects from the h3 event.
*
*/
function vike(options?: VikeOptions<IncomingMessage>): EventHandler {
const handler = createHandler(options)
return eventHandler(async (event) => {
const {
node: { req, res }
} = event

globalStore.setupHMRProxy(req)
await handler({
req,
res,
platformRequest: req
})
})
}
Loading

0 comments on commit 12a856c

Please sign in to comment.