11import type { ElysiaAdapter } from 'elysia'
22import { WebStandardAdapter } from 'elysia/adapter/web-standard'
33
4- import { serve } from '@hono/node-server '
4+ import { serve , FastResponse } from 'srvx '
55
66import { isNumericString , randomId } from 'elysia/utils'
77import type { Server } from 'elysia/universal'
@@ -19,13 +19,7 @@ export const node = () => {
1919 options = parseInt ( options )
2020 }
2121
22- const { promise : serverInfo , resolve : setServerInfo } =
23- Promise . withResolvers < Server > ( )
24-
25- // @ts -expect-error closest possible type
26- app . server = serverInfo
27-
28- const serverOptions : any =
22+ const serverOptions =
2923 typeof options === 'number'
3024 ? {
3125 port : options ,
@@ -37,98 +31,89 @@ export const node = () => {
3731 host : options ?. hostname
3832 }
3933
40- let server = serve ( serverOptions , ( ) => {
41- const address = server . address ( )
42- const hostname =
43- typeof address === 'string'
44- ? address
45- : address
46- ? address . address
47- : 'localhost'
48-
49- const port =
50- typeof address === 'string' ? 0 : ( address ?. port ?? 0 )
51-
52- const serverInfo : Server = {
53- ...server ,
54- id : randomId ( ) ,
55- development : process . env . NODE_ENV !== 'production' ,
56- fetch : app . fetch ,
57- hostname,
58- // @ts -expect-error
59- get pendingRequests ( ) {
60- const { promise, resolve, reject } =
61- Promise . withResolvers < number > ( )
62-
63- server . getConnections ( ( error , total ) => {
64- if ( error ) reject ( error )
65-
66- resolve ( total )
67- } )
68-
69- return promise
70- } ,
71- get pendingWebSockets ( ) {
72- return 0
73- } ,
74- port,
75- publish ( ) {
76- throw new Error (
77- "This adapter doesn't support uWebSocket Publish method"
78- )
79- } ,
80- ref ( ) {
81- server . ref ( )
82- } ,
83- unref ( ) {
84- server . unref ( )
85- } ,
86- reload ( ) {
87- server . close ( ( ) => {
88- server = serve ( serverOptions )
89- } )
90- } ,
91- requestIP ( ) {
92- throw new Error (
93- "This adapter doesn't support Bun requestIP method"
94- )
95- } ,
96- stop ( ) {
97- server . close ( )
98- } ,
99- upgrade ( ) {
100- throw new Error (
101- "This adapter doesn't support Web Standard Upgrade method"
102- )
103- } ,
104- url : new URL (
105- `http://${ hostname === '::' ? 'localhost' : hostname } :${ port } `
106- ) ,
107- [ Symbol . dispose ] ( ) {
108- server . close ( )
109- } ,
110- // @ts -expect-error additional property
111- raw : server
112- } satisfies Server
113-
114- setServerInfo ( serverInfo )
115-
116- if ( callback ) callback ( serverInfo )
117-
118- app . modules . then ( ( ) => {
119- try {
120- serverInfo . reload (
121- typeof options === 'object'
122- ? ( options as any )
123- : {
124- port : options
125- }
126- )
127- } catch { }
128- } )
129- } )
34+ let server = serve ( serverOptions )
35+ const nodeServer = server . node ?. server
36+
37+ console . log ( nodeServer )
13038
13139 // @ts -ignore
40+ const hostname = server . serveOptions . host ?? 'localhost'
41+ const port = server . options . port
42+
43+ const serverInfo : Server = {
44+ ...server ,
45+ id : randomId ( ) ,
46+ development : process . env . NODE_ENV !== 'production' ,
47+ fetch : app . fetch ,
48+ hostname,
49+ get pendingRequests ( ) {
50+ const { promise, resolve, reject } =
51+ Promise . withResolvers < number > ( )
52+
53+ nodeServer ?. getConnections ( ( error , total ) => {
54+ if ( error ) reject ( error )
55+
56+ resolve ( total )
57+ } )
58+
59+ return promise
60+ } ,
61+ get pendingWebSockets ( ) {
62+ return 0
63+ } ,
64+ port,
65+ publish ( ) {
66+ throw new Error (
67+ "This adapter doesn't support uWebSocket Publish method"
68+ )
69+ } ,
70+ ref ( ) {
71+ nodeServer ?. ref ( )
72+ } ,
73+ unref ( ) {
74+ nodeServer ?. unref ( )
75+ } ,
76+ reload ( ) {
77+ nodeServer ?. close ( )
78+ server = serve ( serverOptions )
79+ } ,
80+ requestIP ( ) {
81+ throw new Error (
82+ "This adapter doesn't support Bun requestIP method"
83+ )
84+ } ,
85+ stop ( ) {
86+ server . close ( )
87+ } ,
88+ upgrade ( ) {
89+ throw new Error (
90+ "This adapter doesn't support Web Standard Upgrade method"
91+ )
92+ } ,
93+ url : new URL (
94+ `http://${ hostname === '::' ? 'localhost' : hostname } :${ port } `
95+ ) ,
96+ [ Symbol . dispose ] ( ) {
97+ server . close ( )
98+ } ,
99+ raw : server
100+ } satisfies Server
101+
102+ if ( callback ) callback ( serverInfo )
103+
104+ app . modules . then ( ( ) => {
105+ try {
106+ serverInfo . reload (
107+ typeof options === 'object'
108+ ? ( options as any )
109+ : {
110+ port : options
111+ }
112+ )
113+ } catch { }
114+ } )
115+
116+ // @ts -ignore private property
132117 app . router . http . build ?.( )
133118
134119 if ( app . event . start )
0 commit comments