From 5072977c6414ac44923215fba36fa6dbc687527f Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 25 Jan 2024 05:14:47 +0100 Subject: [PATCH 1/4] add `incoming` and `outgoing` to `c.env` --- src/listener.ts | 2 +- src/types.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/listener.ts b/src/listener.ts index 1e02c5c..cb870cb 100644 --- a/src/listener.ts +++ b/src/listener.ts @@ -115,7 +115,7 @@ export const getRequestListener = (fetchCallback: FetchCallback) => { const req = newRequest(incoming) try { - res = fetchCallback(req) as Response | Promise + res = fetchCallback(req, { incoming, outgoing }) as Response | Promise if (cacheKey in res) { // synchronous, cacheable response return responseViaCache(res as Response, outgoing) diff --git a/src/types.ts b/src/types.ts index 94461b0..950976e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,8 +11,9 @@ import type { createServer as createHttpsServer, ServerOptions as HttpsServerOptions, } from 'node:https' +import type { Hono } from 'hono' -export type FetchCallback = (request: Request) => Promise | unknown +export type FetchCallback = typeof Hono['prototype']['fetch'] export type NextHandlerOption = { fetch: FetchCallback From d7e73af5de567ba191eb2c2ee9169947d2604d79 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 25 Jan 2024 05:22:31 +0100 Subject: [PATCH 2/4] add test --- test/server.test.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/test/server.test.ts b/test/server.test.ts index a7a111c..232eabd 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -1,4 +1,5 @@ import fs from 'node:fs' +import type { IncomingMessage, ServerResponse } from 'node:http' import { createServer as createHttp2Server } from 'node:http2' import { createServer as createHTTPSServer } from 'node:https' import { Response as PonyfillResponse } from '@whatwg-node/fetch' @@ -462,10 +463,10 @@ describe('SSL', () => { describe('HTTP2', () => { const app = new Hono() app.get('/', (c) => c.text('Hello! Node!')) - app.get('/headers', (c) => { + app.get('/headers', (c) => { // call newRequestFromIncoming c.req.header('Accept') - return c.text('Hello! Node!') + return c.text('Hello! Node!') }) app.get('/url', (c) => c.text(c.req.url)) @@ -541,3 +542,29 @@ describe('set child response to c.res', () => { expect(res.headers['content-type']).toMatch(/application\/json/) }) }) + +type RawBindings = { + incoming: IncomingMessage + outgoing: ServerResponse +} + +describe('forwarding IncomingMessage and ServerResponse in env', () => { + const app = new Hono<{ Bindings: RawBindings }>() + app.get('/', (c) => c.json({ + incoming: c.env.incoming.constructor.name, + url: c.env.incoming.url, + outgoing: c.env.outgoing.constructor.name, + status: c.env.outgoing.statusCode + })) + + it('Should add `incoming` and `outgoing` to env', async () => { + const server = createAdaptorServer(app) + const res = await request(server).get('/') + + expect(res.status).toBe(200) + expect(res.body.incoming).toBe('IncomingMessage') + expect(res.body.url).toBe('/') + expect(res.body.outgoing).toBe('ServerResponse') + expect(res.body.status).toBe(200) + }) +}) From ce25f4d981fe94804f9e1a2ee815c3b105a0d7b1 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 26 Jan 2024 04:00:47 +0100 Subject: [PATCH 3/4] improve types - create and export `HttpBindings` and `Http2Bindings` - make `FetchCallback` not depend on `hono` --- src/index.ts | 1 + src/listener.ts | 4 ++-- src/types.ts | 23 ++++++++++++++++++++--- test/server.test.ts | 7 +------ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index b2fc616..6de5948 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { serve, createAdaptorServer } from './server' export { getRequestListener } from './listener' +export type { HttpBindings, Http2Bindings } from './types' diff --git a/src/listener.ts b/src/listener.ts index cb870cb..d25f512 100644 --- a/src/listener.ts +++ b/src/listener.ts @@ -2,7 +2,7 @@ import type { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node: import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2' import { newRequest } from './request' import { cacheKey } from './response' -import type { FetchCallback } from './types' +import type { FetchCallback, HttpBindings } from './types' import { writeFromReadableStream, buildOutgoingHttpHeaders } from './utils' import './globals' @@ -115,7 +115,7 @@ export const getRequestListener = (fetchCallback: FetchCallback) => { const req = newRequest(incoming) try { - res = fetchCallback(req, { incoming, outgoing }) as Response | Promise + res = fetchCallback(req, { incoming, outgoing } as HttpBindings) as Response | Promise if (cacheKey in res) { // synchronous, cacheable response return responseViaCache(res as Response, outgoing) diff --git a/src/types.ts b/src/types.ts index 950976e..7ccef14 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,8 +1,16 @@ -import type { createServer, Server, ServerOptions as HttpServerOptions } from 'node:http' +import type { + createServer, + IncomingMessage, + Server, + ServerOptions as HttpServerOptions, + ServerResponse as HttpServerResponse, +} from 'node:http' import type { createSecureServer as createSecureHttp2Server, createServer as createHttp2Server, + Http2ServerRequest, Http2Server, + Http2ServerResponse, Http2SecureServer, SecureServerOptions as SecureHttp2ServerOptions, ServerOptions as Http2ServerOptions, @@ -11,9 +19,18 @@ import type { createServer as createHttpsServer, ServerOptions as HttpsServerOptions, } from 'node:https' -import type { Hono } from 'hono' -export type FetchCallback = typeof Hono['prototype']['fetch'] +export type HttpBindings = { + incoming: IncomingMessage + outgoing: HttpServerResponse +} + +export type Http2Bindings = { + incoming: Http2ServerRequest + outgoing: Http2ServerResponse +} + +export type FetchCallback = (request: Request, env: HttpBindings | Http2Bindings) => Promise | unknown export type NextHandlerOption = { fetch: FetchCallback diff --git a/test/server.test.ts b/test/server.test.ts index 232eabd..923d8a0 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -1,5 +1,4 @@ import fs from 'node:fs' -import type { IncomingMessage, ServerResponse } from 'node:http' import { createServer as createHttp2Server } from 'node:http2' import { createServer as createHTTPSServer } from 'node:https' import { Response as PonyfillResponse } from '@whatwg-node/fetch' @@ -9,6 +8,7 @@ import { compress } from 'hono/compress' import { poweredBy } from 'hono/powered-by' import request from 'supertest' import { createAdaptorServer } from '../src/server' +import type { RawBindings } from '../src/types' describe('Basic', () => { const app = new Hono() @@ -543,11 +543,6 @@ describe('set child response to c.res', () => { }) }) -type RawBindings = { - incoming: IncomingMessage - outgoing: ServerResponse -} - describe('forwarding IncomingMessage and ServerResponse in env', () => { const app = new Hono<{ Bindings: RawBindings }>() app.get('/', (c) => c.json({ From 5fb4c1b092cc0d4272c5f225c0f3d69529978a0a Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 26 Jan 2024 04:02:11 +0100 Subject: [PATCH 4/4] fix imported type name --- test/server.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/server.test.ts b/test/server.test.ts index 923d8a0..6c8920b 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -8,7 +8,7 @@ import { compress } from 'hono/compress' import { poweredBy } from 'hono/powered-by' import request from 'supertest' import { createAdaptorServer } from '../src/server' -import type { RawBindings } from '../src/types' +import type { HttpBindings } from '../src/types' describe('Basic', () => { const app = new Hono() @@ -544,7 +544,7 @@ describe('set child response to c.res', () => { }) describe('forwarding IncomingMessage and ServerResponse in env', () => { - const app = new Hono<{ Bindings: RawBindings }>() + const app = new Hono<{ Bindings: HttpBindings }>() app.get('/', (c) => c.json({ incoming: c.env.incoming.constructor.name, url: c.env.incoming.url,