@hono/node-server
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -307,4 +307,18 @@ "use strict"; | ||
}; | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
var responseViaResponseObject = async (res, outgoing, options = {}) => { | ||
if (res instanceof Promise) { | ||
if (options.errorHandler) { | ||
try { | ||
res = await res; | ||
} catch (err) { | ||
const errRes = await options.errorHandler(err); | ||
if (!errRes) { | ||
return; | ||
} | ||
res = errRes; | ||
} | ||
} else { | ||
res = await res.catch(handleFetchError); | ||
} | ||
} | ||
try { | ||
@@ -346,4 +360,4 @@ const isCached = cacheKey in res; | ||
}; | ||
var getRequestListener = (fetchCallback) => { | ||
return (incoming, outgoing) => { | ||
var getRequestListener = (fetchCallback, options = {}) => { | ||
return async (incoming, outgoing) => { | ||
let res; | ||
@@ -358,3 +372,10 @@ const req = newRequest(incoming); | ||
if (!res) { | ||
res = handleFetchError(e); | ||
if (options.errorHandler) { | ||
res = await options.errorHandler(e); | ||
if (!res) { | ||
return; | ||
} | ||
} else { | ||
res = handleFetchError(e); | ||
} | ||
} else { | ||
@@ -364,3 +385,3 @@ return handleResponseError(e, outgoing); | ||
} | ||
return responseViaResponseObject(res, outgoing); | ||
return responseViaResponseObject(res, outgoing, options); | ||
}; | ||
@@ -367,0 +388,0 @@ }; |
import { IncomingMessage, ServerResponse } from 'node:http'; | ||
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2'; | ||
import { FetchCallback } from './types.js'; | ||
import { FetchCallback, CustomErrorHandler } from './types.js'; | ||
import 'node:https'; | ||
declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => void | Promise<void>; | ||
declare const getRequestListener: (fetchCallback: FetchCallback, options?: { | ||
errorHandler?: CustomErrorHandler; | ||
}) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>; | ||
export { getRequestListener }; |
@@ -302,4 +302,18 @@ "use strict"; | ||
}; | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
var responseViaResponseObject = async (res, outgoing, options = {}) => { | ||
if (res instanceof Promise) { | ||
if (options.errorHandler) { | ||
try { | ||
res = await res; | ||
} catch (err) { | ||
const errRes = await options.errorHandler(err); | ||
if (!errRes) { | ||
return; | ||
} | ||
res = errRes; | ||
} | ||
} else { | ||
res = await res.catch(handleFetchError); | ||
} | ||
} | ||
try { | ||
@@ -341,4 +355,4 @@ const isCached = cacheKey in res; | ||
}; | ||
var getRequestListener = (fetchCallback) => { | ||
return (incoming, outgoing) => { | ||
var getRequestListener = (fetchCallback, options = {}) => { | ||
return async (incoming, outgoing) => { | ||
let res; | ||
@@ -353,3 +367,10 @@ const req = newRequest(incoming); | ||
if (!res) { | ||
res = handleFetchError(e); | ||
if (options.errorHandler) { | ||
res = await options.errorHandler(e); | ||
if (!res) { | ||
return; | ||
} | ||
} else { | ||
res = handleFetchError(e); | ||
} | ||
} else { | ||
@@ -359,3 +380,3 @@ return handleResponseError(e, outgoing); | ||
} | ||
return responseViaResponseObject(res, outgoing); | ||
return responseViaResponseObject(res, outgoing, options); | ||
}; | ||
@@ -362,0 +383,0 @@ }; |
@@ -304,4 +304,18 @@ "use strict"; | ||
}; | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
var responseViaResponseObject = async (res, outgoing, options = {}) => { | ||
if (res instanceof Promise) { | ||
if (options.errorHandler) { | ||
try { | ||
res = await res; | ||
} catch (err) { | ||
const errRes = await options.errorHandler(err); | ||
if (!errRes) { | ||
return; | ||
} | ||
res = errRes; | ||
} | ||
} else { | ||
res = await res.catch(handleFetchError); | ||
} | ||
} | ||
try { | ||
@@ -343,4 +357,4 @@ const isCached = cacheKey in res; | ||
}; | ||
var getRequestListener = (fetchCallback) => { | ||
return (incoming, outgoing) => { | ||
var getRequestListener = (fetchCallback, options = {}) => { | ||
return async (incoming, outgoing) => { | ||
let res; | ||
@@ -355,3 +369,10 @@ const req = newRequest(incoming); | ||
if (!res) { | ||
res = handleFetchError(e); | ||
if (options.errorHandler) { | ||
res = await options.errorHandler(e); | ||
if (!res) { | ||
return; | ||
} | ||
} else { | ||
res = handleFetchError(e); | ||
} | ||
} else { | ||
@@ -361,3 +382,3 @@ return handleResponseError(e, outgoing); | ||
} | ||
return responseViaResponseObject(res, outgoing); | ||
return responseViaResponseObject(res, outgoing, options); | ||
}; | ||
@@ -364,0 +385,0 @@ }; |
@@ -40,3 +40,4 @@ import { IncomingMessage, ServerResponse, Server, ServerOptions as ServerOptions$1, createServer } from 'node:http'; | ||
} & ServerOptions; | ||
type CustomErrorHandler = (err: unknown) => void | Response | Promise<void | Response>; | ||
export { FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType }; | ||
export { CustomErrorHandler, FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType }; |
@@ -5,4 +5,4 @@ import * as http2 from 'http2'; | ||
declare const handle: (app: Hono<any, any, any>) => (incoming: http.IncomingMessage | http2.Http2ServerRequest, outgoing: http.ServerResponse<http.IncomingMessage> | http2.Http2ServerResponse) => void | Promise<void>; | ||
declare const handle: (app: Hono<any, any, any>) => (incoming: http.IncomingMessage | http2.Http2ServerRequest, outgoing: http.ServerResponse<http.IncomingMessage> | http2.Http2ServerResponse) => Promise<void>; | ||
export { handle }; |
@@ -302,4 +302,18 @@ "use strict"; | ||
}; | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
var responseViaResponseObject = async (res, outgoing, options = {}) => { | ||
if (res instanceof Promise) { | ||
if (options.errorHandler) { | ||
try { | ||
res = await res; | ||
} catch (err) { | ||
const errRes = await options.errorHandler(err); | ||
if (!errRes) { | ||
return; | ||
} | ||
res = errRes; | ||
} | ||
} else { | ||
res = await res.catch(handleFetchError); | ||
} | ||
} | ||
try { | ||
@@ -341,4 +355,4 @@ const isCached = cacheKey in res; | ||
}; | ||
var getRequestListener = (fetchCallback) => { | ||
return (incoming, outgoing) => { | ||
var getRequestListener = (fetchCallback, options = {}) => { | ||
return async (incoming, outgoing) => { | ||
let res; | ||
@@ -353,3 +367,10 @@ const req = newRequest(incoming); | ||
if (!res) { | ||
res = handleFetchError(e); | ||
if (options.errorHandler) { | ||
res = await options.errorHandler(e); | ||
if (!res) { | ||
return; | ||
} | ||
} else { | ||
res = handleFetchError(e); | ||
} | ||
} else { | ||
@@ -359,3 +380,3 @@ return handleResponseError(e, outgoing); | ||
} | ||
return responseViaResponseObject(res, outgoing); | ||
return responseViaResponseObject(res, outgoing, options); | ||
}; | ||
@@ -362,0 +383,0 @@ }; |
{ | ||
"name": "@hono/node-server", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Node.js Adapter for Hono", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -181,2 +181,36 @@ # Node.js Adapter for Hono | ||
## Accessing Node.js API | ||
You can access the Node.js API from `c.env` in Node.js. For example, if you want to specify a type, you can write the following. | ||
```ts | ||
import { serve } from '@hono/node-server' | ||
import type { HttpBindings } from '@hono/node-server' | ||
import { Hono } from 'hono' | ||
const app = new Hono<{ Bindings: HttpBindings }>() | ||
app.get('/', (c) => { | ||
return c.json({ | ||
remoteAddress: c.env.incoming.socket.remoteAddress, | ||
}) | ||
}) | ||
serve(app) | ||
``` | ||
The APIs that you can get from `c.env` are as follows. | ||
```ts | ||
type HttpBindings = { | ||
incoming: IncomingMessage | ||
outgoing: ServerResponse | ||
} | ||
type Http2Bindings = { | ||
incoming: Http2ServerRequest | ||
outgoing: Http2ServerResponse | ||
} | ||
``` | ||
## Related projects | ||
@@ -183,0 +217,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
130692
4108
227