@hono/node-server 2.0.0-rc.1 → 2.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,12 +1,11 @@
1
1
  # Node.js Adapter for Hono
2
2
 
3
3
  This adapter `@hono/node-server` allows you to run your Hono application on Node.js.
4
- Initially, Hono wasn't designed for Node.js, but with this adapter, you can now use Hono on Node.js.
5
- It utilizes web standard APIs implemented in Node.js version 18 or higher.
4
+ Initially, Hono wasn't designed for Node.js, but with this adapter, you can now use Hono on Node.js. It utilizes web standard APIs implemented in Node.js.
6
5
 
7
6
  ## Benchmarks
8
7
 
9
- Hono is 3.5 times faster than Express.
8
+ Hono is 4.1 times faster than Express.
10
9
 
11
10
  Express:
12
11
 
@@ -14,12 +13,12 @@ Express:
14
13
  $ bombardier -d 10s --fasthttp http://localhost:3000/
15
14
 
16
15
  Statistics Avg Stdev Max
17
- Reqs/sec 16438.94 1603.39 19155.47
18
- Latency 7.60ms 7.51ms 559.89ms
16
+ Reqs/sec 20803.37 1713.06 24910.85
17
+ Latency 6.01ms 5.21ms 451.37ms
19
18
  HTTP codes:
20
- 1xx - 0, 2xx - 164494, 3xx - 0, 4xx - 0, 5xx - 0
19
+ 1xx - 0, 2xx - 208131, 3xx - 0, 4xx - 0, 5xx - 0
21
20
  others - 0
22
- Throughput: 4.55MB/s
21
+ Throughput: 5.75MB/s
23
22
  ```
24
23
 
25
24
  Hono + `@hono/node-server`:
@@ -28,12 +27,12 @@ Hono + `@hono/node-server`:
28
27
  $ bombardier -d 10s --fasthttp http://localhost:3000/
29
28
 
30
29
  Statistics Avg Stdev Max
31
- Reqs/sec 58296.56 5512.74 74403.56
32
- Latency 2.14ms 1.46ms 190.92ms
30
+ Reqs/sec 85405.51 7250.65 102658.51
31
+ Latency 1.46ms 1.00ms 149.95ms
33
32
  HTTP codes:
34
- 1xx - 0, 2xx - 583059, 3xx - 0, 4xx - 0, 5xx - 0
33
+ 1xx - 0, 2xx - 854120, 3xx - 0, 4xx - 0, 5xx - 0
35
34
  others - 0
36
- Throughput: 12.56MB/s
35
+ Throughput: 18.49MB/s
37
36
  ```
38
37
 
39
38
  ## Requirements
@@ -71,6 +70,34 @@ serve(app, (info) => {
71
70
  })
72
71
  ```
73
72
 
73
+ ## WebSocket
74
+
75
+ You can upgrade WebSocket connections with `upgradeWebSocket` from `@hono/node-server`.
76
+ To enable this, install `ws` (and `@types/ws`) in your project, then create and provide a `WebSocketServer` as shown in the example below.
77
+
78
+ ```ts
79
+ import { serve, upgradeWebSocket } from '@hono/node-server'
80
+ import { WebSocketServer } from 'ws'
81
+ import { Hono } from 'hono'
82
+
83
+ const app = new Hono()
84
+
85
+ app.get(
86
+ '/ws',
87
+ upgradeWebSocket(() => ({
88
+ onMessage(event, ws) {
89
+ ws.send(event.data)
90
+ },
91
+ }))
92
+ )
93
+
94
+ const wss = new WebSocketServer({ noServer: true }) // important to create with `noServer: true`
95
+ serve({
96
+ fetch: app.fetch,
97
+ websocket: { server: wss },
98
+ })
99
+ ```
100
+
74
101
  For example, run it using `ts-node`. Then an HTTP server will be launched. The default port is `3000`.
75
102
 
76
103
  ```sh
@@ -132,6 +159,23 @@ serve({
132
159
  })
133
160
  ```
134
161
 
162
+ ### `websocket`
163
+
164
+ provide a websocket server to enable websocket support.
165
+
166
+ ```ts
167
+ import { serve, upgradeWebSocket } from '@hono/node-server'
168
+ import { WebSocketServer } from 'ws'
169
+
170
+ // ...
171
+ const wss = new WebSocketServer({ noServer: true })
172
+
173
+ serve({
174
+ fetch: app.fetch,
175
+ websocket: { server: wss },
176
+ })
177
+ ```
178
+
135
179
  ## Middleware
136
180
 
137
181
  Most built-in middleware also works with Node.js.