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

Commit 2b6dd97

Browse files
author
v1rtl
committed
Deno 1.9 support (and std bump)
1 parent b4b3147 commit 2b6dd97

File tree

16 files changed

+75
-45
lines changed

16 files changed

+75
-45
lines changed

app.ts

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// deno-lint-ignore-file
2-
import { Router, serve, Server, rg } from './deps.ts'
2+
import { Router, serve, Server, rg, pushMiddleware } from './deps.ts'
33
import { NextFunction, RHandler as Handler, Middleware, UseMethodParams } from './types.ts'
44
import { onErrorHandler, ErrorHandler } from './onError.ts'
5-
import { setImmediate } from 'https://deno.land/std@0.88.0/node/timers.ts'
5+
import { setImmediate } from 'https://deno.land/std@0.93.0/node/timers.ts'
66
import type { Request } from './request.ts'
77
import type { Response } from './response.ts'
88
import { getURLParams, getPathname } from './utils/parseUrl.ts'
99
import { extendMiddleware } from './extend.ts'
10-
import * as path from 'https://deno.land/std@0.88.0/path/mod.ts'
10+
import * as path from 'https://deno.land/std@0.93.0/path/mod.ts'
1111

1212
const lead = (x: string) => (x.charCodeAt(0) === 47 ? x : '/' + x)
1313

14-
const mount = (fn: App | Handler) => (fn instanceof App ? fn.attach : fn)
14+
const mount = (fn: any) => (fn instanceof App ? fn.attach : fn)
1515

1616
declare global {
1717
namespace tinyhttp {
@@ -140,6 +140,10 @@ export class App<
140140
applyExtensions?: (req: Req, res: Res, next: NextFunction) => void
141141
attach: (req: Req) => void
142142

143+
mountpath = '/'
144+
145+
apps: Record<string, App> = {}
146+
143147
#eventHandler?: FetchEventListenerObject
144148

145149
constructor(options: AppConstructor<Req, Res> = {}) {
@@ -215,27 +219,52 @@ export class App<
215219
use(...args: UseMethodParams<Req, Res, App>) {
216220
const base = args[0]
217221

218-
const fns = args.slice(1)
222+
const fns = args.slice(1).flat()
223+
224+
if (base instanceof App) {
225+
// Set App parent to current App
226+
// @ts-ignore
227+
base.parent = this
228+
229+
// Mount on root
230+
base.mountpath = '/'
231+
232+
this.apps['/'] = base
233+
}
234+
235+
const path = typeof base === 'string' ? base : '/'
236+
237+
let regex: { keys: string[]; pattern: RegExp }
238+
239+
for (const fn of fns) {
240+
if (fn instanceof App) {
241+
regex = rg(path, true)
242+
243+
fn.mountpath = path
244+
245+
this.apps[path] = fn
246+
247+
// @ts-ignore
248+
fn.parent = this
249+
}
250+
}
219251

220252
if (base === '/') {
221-
for (const fn of fns.flat()) super.use(base, mount(fn as Handler))
253+
for (const fn of fns) super.use(base, mount(fn as Handler))
222254
} else if (typeof base === 'function' || base instanceof App) {
223-
super.use('/', [base, ...fns].map(mount as any))
224-
} else if (fns.some((fn) => fn instanceof App)) {
225-
super.use(
226-
base,
227-
fns.flatMap((fn) => {
228-
if (fn instanceof App) {
229-
fn = fn as App
230-
fn.mountpath = typeof base === 'string' ? base : '/'
231-
232-
fn.parent = (this as unknown) as App
233-
}
234-
235-
return mount(fn as App)
236-
})
237-
)
238-
} else super.use(...args)
255+
super.use('/', [base, ...fns].map(mount))
256+
} else if (Array.isArray(base)) {
257+
super.use('/', [...base, ...fns].map(mount))
258+
} else {
259+
pushMiddleware(this.middleware)({
260+
path: base as string,
261+
// @ts-ignore
262+
regex,
263+
type: 'mw',
264+
handler: mount(fns[0] as Handler),
265+
handlers: fns.slice(1).map(mount)
266+
})
267+
}
239268

240269
return this // chainable
241270
}
@@ -244,7 +273,7 @@ export class App<
244273
if (!m.path) m.path = '/'
245274
m.regex = m.type === 'mw' ? rg(m.path, true) : rg(m.path)
246275

247-
return (m.method ? m.method === method : true) && m.regex.pattern.test(url)
276+
return (m.method ? m.method === method : true) && m.regex?.pattern.test(url)
248277
})
249278
}
250279
/**

deps.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ export { vary } from 'https://deno.land/x/vary@1.0.0/mod.ts'
77
export { isIP } from 'https://deno.land/x/isIP@1.0.0/mod.ts'
88
export { Accepts } from 'https://deno.land/x/accepts@2.1.0/mod.ts'
99
export { encodeUrl } from 'https://deno.land/x/encodeurl@1.0.0/mod.ts'
10-
export { charset, contentType, lookup } from 'https://deno.land/x/media_types@v2.6.1/mod.ts'
10+
export { charset, contentType, lookup } from 'https://deno.land/x/media_types@v2.8.1/mod.ts'
1111
export { default as rg } from 'https://esm.sh/regexparam'
12-
export { forwarded } from 'https://deno.land/x/forwarded@v0.0.2/mod.ts'
13-
export * from 'https://deno.land/x/proxy_addr@v0.0.0/mod.ts'
14-
import type { ServerRequest as Req, Response as ServerResponse } from 'https://deno.land/std@0.88.0/http/server.ts'
12+
export { forwarded } from 'https://deno.land/x/forwarded@0.0.4/mod.ts'
13+
export * from 'https://deno.land/x/proxy_addr@0.0.1/mod.ts'
14+
import type { ServerRequest as Req, Response as ServerResponse } from 'https://deno.land/std@0.93.0/http/server.ts'
1515

1616
interface Res extends ServerResponse {
1717
headers: Headers
1818
}
1919

2020
export type { Req, Res }
2121

22-
export { serve, Server } from 'https://deno.land/std@0.88.0/http/server.ts'
22+
export { serve, Server } from 'https://deno.land/std@0.93.0/http/server.ts'
2323

24-
export { Router } from 'https://esm.sh/@tinyhttp/router'
24+
export { Router, pushMiddleware } from 'https://esm.sh/@tinyhttp/router@1.2.7'

egg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"entry": "./mod.ts",
55
"description": "0-legacy, tiny & fast web framework as a replacement of Express",
66
"homepage": "https://github.com/talentlessguy/tinyhttp-deno",
7-
"version": "0.0.29",
7+
"version": "0.0.30",
88
"files": [
99
"./*.ts",
1010
"./utils/*.ts",

examples/basic/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ app
66
.get('/', (req, res) => {
77
const greeting = `Hello on ${req.url} from Deno v${Deno.version.deno} and tinyhttp! 🦕`
88
res.format({
9-
html: () => `<h1>${greeting}</h1>`,
10-
text: () => greeting
9+
text: (_, res) => res.send(greeting),
10+
html: (_, res) => res.send(`<h1>${greeting}</h1>`)
1111
})
1212
})
1313
.get('/page/:page/', (req, res) => {

extensions/req/accepts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Accepts } from '../../deps.ts'
2-
import { Req } from '../../deps.ts'
2+
import type { Req } from '../../deps.ts'
33

44
export const getAccepts = <Request extends Req = Req>(req: Request) => (...types: string[]) =>
55
new Accepts(req.headers).types(types)

extensions/req/headers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Req, Res, parseRange, ParseRangeOptions } from '../../deps.ts'
22
import fresh from 'https://deno.land/x/fresh@v1.0.0/mod.ts'
3-
import { is } from 'https://deno.land/x/type_is@1.0.1/mod.ts'
3+
import { is } from 'https://denopkg.com/talentlessguy/type_is'
44

55
export const getRequestHeader = <Request extends Req = Req>(req: Request) => (header: string) => {
66
const lc = header.toLowerCase()

extensions/res/cookie.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Req, Res } from '../../deps.ts'
2-
import * as cookie from 'https://deno.land/std@0.88.0/http/cookie.ts'
2+
import * as cookie from 'https://deno.land/std@0.93.0/http/cookie.ts'
33

44
export const setCookie = <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => (
55
name: string,

extensions/res/download.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { contentDisposition } from 'https://esm.sh/@tinyhttp/content-disposition'
22
import { SendFileOptions, sendFile } from './send/sendFile.ts'
3-
import { resolve, extname } from 'https://deno.land/std@0.88.0/path/mod.ts'
3+
import { extname } from 'https://deno.land/std@0.93.0/path/mod.ts'
44
import { setContentType, setHeader } from './headers.ts'
55
import { Req, Res } from '../../deps.ts'
66

@@ -18,7 +18,7 @@ export const download = <Request extends Req = Req, Response extends Res = Res>(
1818
let opts: DownloadOptions = options
1919

2020
// set Content-Disposition when file is sent
21-
const headers: Record<string, any> = {
21+
const headers: Record<string, string> = {
2222
'Content-Disposition': contentDisposition(name || path)
2323
}
2424

extensions/res/format.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const formatResponse = <Request extends Req = Req, Response extends Res =
2525

2626
if (key) {
2727
res.headers?.set('Content-Type', normalizeType(key).value || '')
28+
2829
obj[key](req, res, next)
2930
} else if (fn) {
3031
fn()

extensions/res/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { format, parse } from 'https://deno.land/x/content_type/mod.ts'
2-
import { etag as eTag } from 'https://deno.land/x/opine@1.1.0/src/utils/etag.ts'
2+
import { etag as eTag } from 'https://deno.land/x/opine@1.3.2/src/utils/etag.ts'
33
import { lookup } from '../../deps.ts'
44

55
export const createETag = (body: Parameters<typeof eTag>[0]) => {

0 commit comments

Comments
 (0)