Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 960f7c0

Browse files
author
v1rtl
committed
use Deno.servettp instead of std/http/server.ts
1 parent e80657b commit 960f7c0

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

app.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConnInfo, path, serve, ServeInit } from './deps.ts'
1+
import { ConnInfo, path, ServeInit } from './deps.ts'
22
import { pushMiddleware, Router, UseMethodParams } from './router.ts'
33
import { THResponse } from './response.ts'
44
import { extendMiddleware } from './extend.ts'
@@ -46,7 +46,7 @@ export class App<
4646
settings: AppSettings & Record<string, any>
4747
locals: Record<string, string> = {}
4848
engines: Record<string, TemplateFunc<RenderOptions>> = {}
49-
onError: ServeInit['onError']
49+
onError: (err: unknown) => Response | Promise<Response>
5050
notFound: Handler<Req, Res>
5151
attach: (req: Req, res: Res, next: NextFunction) => void
5252

@@ -67,7 +67,7 @@ export class App<
6767
*/
6868
render(
6969
file: string,
70-
data: Record<string, any> = {},
70+
data: Record<string, unknown> = {},
7171
cb: (err: unknown, html: unknown) => void,
7272
options: TemplateEngineOptions<RenderOptions> = {},
7373
) {
@@ -190,7 +190,7 @@ export class App<
190190
}
191191
mw.push({ type: 'mw', handler: this.notFound, path: '/' })
192192

193-
let idx = 0, err: unknown | null = null
193+
let idx = 0, err
194194
const next: NextFunction = (error) => {
195195
if (error) err = error
196196
return loop()
@@ -203,12 +203,11 @@ export class App<
203203
))
204204

205205
await loop()
206-
207-
if (err) throw err // so that serve catches it
206+
if (err) throw err
208207
}
209-
handler = async (_req: Request, connInfo: ConnInfo) => {
208+
handler = async (_req: Request, connInfo?: ConnInfo) => {
210209
const req = _req.clone() as Req
211-
req.conn = connInfo
210+
req.conn = connInfo!
212211
const res = {
213212
_init: {
214213
headers: new Headers({
@@ -219,7 +218,11 @@ export class App<
219218
},
220219
_body: undefined,
221220
}
222-
await this.#prepare(req, res)
221+
try {
222+
await this.#prepare(req, res)
223+
} catch (e) {
224+
return this.onError(e)
225+
}
223226
return new Response(res._body, res._init)
224227
}
225228
/**
@@ -229,11 +232,17 @@ export class App<
229232
* @param host server listening host
230233
*/
231234
async listen(port: number, cb?: () => void, hostname?: string) {
232-
await serve(this.handler, {
233-
port,
234-
onListen: cb,
235-
hostname,
236-
onError: this.onError,
237-
})
235+
const listener = Deno.listen({ hostname, port })
236+
for await (const conn of listener) {
237+
;(async () => {
238+
const requests = Deno.serveHttp(conn)
239+
for await (const { request, respondWith } of requests) {
240+
const response = await this.handler(request, conn)
241+
if (response) {
242+
respondWith(response)
243+
}
244+
}
245+
})
246+
}
238247
}
239248
}

deps.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export type { ServeInit } from 'https://deno.land/[email protected]/http/server.ts'
22
export { parse as parseRange } from 'https://deno.land/x/[email protected]/parse.ts'
33
export type { RangesSpecifier } from 'https://deno.land/x/[email protected]/parse.ts'
4-
export { serve } from 'https://deno.land/[email protected]/http/server.ts'
54
export type { ConnInfo } from 'https://deno.land/[email protected]/http/server.ts'
65
export { typeByExtension } from 'https://deno.land/[email protected]/media_types/type_by_extension.ts'
76
export { getCharset } from 'https://deno.land/[email protected]/media_types/get_charset.ts'

examples/https/mod.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { App } from '../../app.ts'
22
import { serveTls } from 'https://deno.land/[email protected]/http/server.ts'
3-
import { THRequest } from '../../request.ts'
43

54
const app = new App()
65

7-
app.get('/', (req, res) => void res.send(`Hello World from ${req.protocol}`))
6+
app.get('/', (req, res) => {
7+
throw new Error(`Hello World from ${req.protocol}`)
8+
})
89

910
await serveTls(app.handler, {
1011
port: 3000,

mod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './app.ts'
2-
export type { Handler, AppConstructor } from './types.ts'
2+
export type { AppConstructor, Handler } from './types.ts'
33
export type { THRequest } from './request.ts'
4-
export type { THResponse } from './response.ts'
4+
export type { THResponse } from './response.ts'

onError.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { STATUS_CODES } from './constants.ts'
2-
import { ServeInit } from './deps.ts'
32

4-
export const onErrorHandler: ServeInit['onError'] = (err) => {
3+
export const onErrorHandler = (err: unknown) => {
54
if (err instanceof Error) console.error(err)
65

76
const error = err as Error & { code?: number; status?: number }

tests/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const randomPort = async () => await getFreePort(random(2048, 8064))
1010
export const BindToSuperDeno = <Req extends THRequest, Res extends THResponse>(
1111
app: App<unknown, Req, Res>,
1212
) => {
13-
return superdeno(app.listen)
13+
return superdeno(app.handler)
1414
}
1515

1616
export const InitAppAndTest = (

0 commit comments

Comments
 (0)