Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use # for private methods to reduce the minified file size #3596

Merged
merged 6 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,11 @@ export class Context<
return Object.fromEntries(this.#var)
}

newResponse: NewResponse = (
#newResponse(
data: Data | null,
arg?: StatusCode | ResponseInit,
headers?: HeaderRecord
): Response => {
): Response {
// Optimized
if (this.#isFresh && !headers && !arg && this.#status === 200) {
return new Response(data, {
Expand Down Expand Up @@ -689,6 +689,8 @@ export class Context<
})
}

newResponse: NewResponse = (...args) => this.#newResponse(...(args as Parameters<NewResponse>))

/**
* `.body()` can return the HTTP response.
* You can set headers with `.header()` and set HTTP status code with `.status`.
Expand Down Expand Up @@ -716,8 +718,8 @@ export class Context<
headers?: HeaderRecord
): Response => {
return typeof arg === 'number'
? this.newResponse(data, arg, headers)
: this.newResponse(data, arg)
? this.#newResponse(data, arg, headers)
: this.#newResponse(data, arg)
}

/**
Expand Down Expand Up @@ -749,8 +751,8 @@ export class Context<
this.#preparedHeaders['content-type'] = TEXT_PLAIN
// @ts-expect-error `Response` due to missing some types-only keys
return typeof arg === 'number'
? this.newResponse(text, arg, headers)
: this.newResponse(text, arg)
? this.#newResponse(text, arg, headers)
: this.#newResponse(text, arg)
}

/**
Expand Down Expand Up @@ -778,7 +780,7 @@ export class Context<
this.#preparedHeaders['content-type'] = 'application/json; charset=UTF-8'
/* eslint-disable @typescript-eslint/no-explicit-any */
return (
typeof arg === 'number' ? this.newResponse(body, arg, headers) : this.newResponse(body, arg)
typeof arg === 'number' ? this.#newResponse(body, arg, headers) : this.#newResponse(body, arg)
) as any
}

Expand All @@ -793,14 +795,14 @@ export class Context<
if (typeof html === 'object') {
return resolveCallback(html, HtmlEscapedCallbackPhase.Stringify, false, {}).then((html) => {
return typeof arg === 'number'
? this.newResponse(html, arg, headers)
: this.newResponse(html, arg)
? this.#newResponse(html, arg, headers)
: this.#newResponse(html, arg)
})
}

return typeof arg === 'number'
? this.newResponse(html as string, arg, headers)
: this.newResponse(html as string, arg)
? this.#newResponse(html as string, arg, headers)
: this.#newResponse(html as string, arg)
}

/**
Expand Down
58 changes: 29 additions & 29 deletions src/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
if (typeof args1 === 'string') {
this.#path = args1
} else {
this.addRoute(method, this.#path, args1)
this.#addRoute(method, this.#path, args1)
}
args.forEach((handler) => {
if (typeof handler !== 'string') {
this.addRoute(method, this.#path, handler)
this.#addRoute(method, this.#path, handler)
}
})
return this as any
Expand All @@ -147,7 +147,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
this.#path = p
for (const m of [method].flat()) {
handlers.map((handler) => {
this.addRoute(m.toUpperCase(), this.#path, handler)
this.#addRoute(m.toUpperCase(), this.#path, handler)
})
}
}
Expand All @@ -163,7 +163,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
handlers.unshift(arg1)
}
handlers.forEach((handler) => {
this.addRoute(METHOD_NAME_ALL, this.#path, handler)
this.#addRoute(METHOD_NAME_ALL, this.#path, handler)
})
return this as any
}
Expand All @@ -174,7 +174,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
this.getPath = strict ? options.getPath ?? getPath : getPathNoStrict
}

private clone(): Hono<E, S, BasePath> {
#clone(): Hono<E, S, BasePath> {
const clone = new Hono<E, S, BasePath>({
router: this.router,
getPath: this.getPath,
Expand All @@ -183,8 +183,8 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
return clone
}

private notFoundHandler: NotFoundHandler = notFoundHandler
private errorHandler: ErrorHandler = errorHandler
#notFoundHandler: NotFoundHandler = notFoundHandler
#errorHandler: ErrorHandler = errorHandler

/**
* `.route()` allows grouping other Hono instance in routes.
Expand Down Expand Up @@ -216,15 +216,15 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
const subApp = this.basePath(path)
app.routes.map((r) => {
let handler
if (app.errorHandler === errorHandler) {
if (app.#errorHandler === errorHandler) {
handler = r.handler
} else {
handler = async (c: Context, next: Next) =>
(await compose<Context>([], app.errorHandler)(c, () => r.handler(c, next))).res
(await compose<Context>([], app.#errorHandler)(c, () => r.handler(c, next))).res
;(handler as any)[COMPOSED_HANDLER] = r.handler
}

subApp.addRoute(r.method, r.path, handler)
subApp.#addRoute(r.method, r.path, handler)
})
return this
}
Expand All @@ -243,7 +243,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
* ```
*/
basePath<SubPath extends string>(path: SubPath): Hono<E, S, MergePath<BasePath, SubPath>> {
const subApp = this.clone()
const subApp = this.#clone()
subApp._basePath = mergePath(this._basePath, path)
return subApp
}
Expand All @@ -265,7 +265,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
* ```
*/
onError = (handler: ErrorHandler<E>): Hono<E, S, BasePath> => {
this.errorHandler = handler
this.#errorHandler = handler
return this
}

Expand All @@ -285,7 +285,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
* ```
*/
notFound = (handler: NotFoundHandler<E>): Hono<E, S, BasePath> => {
this.notFoundHandler = handler
this.#notFoundHandler = handler
return this
}

Expand Down Expand Up @@ -370,26 +370,26 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =

await next()
}
this.addRoute(METHOD_NAME_ALL, mergePath(path, '*'), handler)
this.#addRoute(METHOD_NAME_ALL, mergePath(path, '*'), handler)
return this
}

private addRoute(method: string, path: string, handler: H) {
#addRoute(method: string, path: string, handler: H) {
method = method.toUpperCase()
path = mergePath(this._basePath, path)
const r: RouterRoute = { path: path, method: method, handler: handler }
this.router.add(method, path, [handler, r])
this.routes.push(r)
}

private handleError(err: unknown, c: Context<E>) {
#handleError(err: unknown, c: Context<E>) {
if (err instanceof Error) {
return this.errorHandler(err, c)
return this.#errorHandler(err, c)
}
throw err
}

private dispatch(
#dispatch(
request: Request,
executionCtx: ExecutionContext | FetchEventLike | undefined,
env: E['Bindings'],
Expand All @@ -398,7 +398,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
// Handle HEAD method
if (method === 'HEAD') {
return (async () =>
new Response(null, await this.dispatch(request, executionCtx, env, 'GET')))()
new Response(null, await this.#dispatch(request, executionCtx, env, 'GET')))()
}

const path = this.getPath(request, { env })
Expand All @@ -409,31 +409,31 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
matchResult,
env,
executionCtx,
notFoundHandler: this.notFoundHandler,
notFoundHandler: this.#notFoundHandler,
})

// Do not `compose` if it has only one handler
if (matchResult[0].length === 1) {
let res: ReturnType<H>
try {
res = matchResult[0][0][0][0](c, async () => {
c.res = await this.notFoundHandler(c)
c.res = await this.#notFoundHandler(c)
})
} catch (err) {
return this.handleError(err, c)
return this.#handleError(err, c)
}

return res instanceof Promise
? res
.then(
(resolved: Response | undefined) =>
resolved || (c.finalized ? c.res : this.notFoundHandler(c))
resolved || (c.finalized ? c.res : this.#notFoundHandler(c))
)
.catch((err: Error) => this.handleError(err, c))
: res ?? this.notFoundHandler(c)
.catch((err: Error) => this.#handleError(err, c))
: res ?? this.#notFoundHandler(c)
}

const composed = compose<Context>(matchResult[0], this.errorHandler, this.notFoundHandler)
const composed = compose<Context>(matchResult[0], this.#errorHandler, this.#notFoundHandler)

return (async () => {
try {
Expand All @@ -446,7 +446,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =

return context.res
} catch (err) {
return this.handleError(err, c)
return this.#handleError(err, c)
}
})()
}
Expand All @@ -467,7 +467,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
Env?: E['Bindings'] | {},
executionCtx?: ExecutionContext
) => Response | Promise<Response> = (request, ...rest) => {
return this.dispatch(request, rest[1], rest[0], request.method)
return this.#dispatch(request, rest[1], rest[0], request.method)
}

/**
Expand Down Expand Up @@ -511,7 +511,7 @@ class Hono<E extends Env = Env, S extends Schema = {}, BasePath extends string =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
addEventListener('fetch', (event: FetchEventLike): void => {
event.respondWith(this.dispatch(event.request, event, undefined, event.request.method))
event.respondWith(this.#dispatch(event.request, event, undefined, event.request.method))
})
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
param(key: string): string | undefined
param<P2 extends string = P>(): Simplify<UnionToIntersection<ParamKeyToRecord<ParamKeys<P2>>>>
param(key?: string): unknown {
return key ? this.getDecodedParam(key) : this.getAllDecodedParams()
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams()
}

private getDecodedParam(key: string): string | undefined {
#getDecodedParam(key: string): string | undefined {
const paramKey = this.#matchResult[0][this.routeIndex][1][key]
const param = this.getParamValue(paramKey)
const param = this.#getParamValue(paramKey)
return param ? (/\%/.test(param) ? tryDecodeURIComponent(param) : param) : undefined
}

private getAllDecodedParams(): Record<string, string> {
#getAllDecodedParams(): Record<string, string> {
const decoded: Record<string, string> = {}

const keys = Object.keys(this.#matchResult[0][this.routeIndex][1])
for (const key of keys) {
const value = this.getParamValue(this.#matchResult[0][this.routeIndex][1][key])
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key])
if (value && typeof value === 'string') {
decoded[key] = /\%/.test(value) ? tryDecodeURIComponent(value) : value
}
Expand All @@ -114,7 +114,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
return decoded
}

private getParamValue(paramKey: any): string | undefined {
#getParamValue(paramKey: any): string | undefined {
return this.#matchResult[1] ? this.#matchResult[1][paramKey as any] : paramKey
}

Expand Down Expand Up @@ -208,7 +208,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
return (this.bodyCache.parsedBody ??= await parseBody(this, options))
}

private cachedBody = (key: keyof Body) => {
#cachedBody = (key: keyof Body) => {
const { bodyCache, raw } = this
const cachedBody = bodyCache[key]

Expand Down Expand Up @@ -242,7 +242,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* ```
*/
json<T = any>(): Promise<T> {
return this.cachedBody('json')
return this.#cachedBody('json')
}

/**
Expand All @@ -258,7 +258,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* ```
*/
text(): Promise<string> {
return this.cachedBody('text')
return this.#cachedBody('text')
}

/**
Expand All @@ -274,7 +274,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* ```
*/
arrayBuffer(): Promise<ArrayBuffer> {
return this.cachedBody('arrayBuffer')
return this.#cachedBody('arrayBuffer')
}

/**
Expand All @@ -288,7 +288,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* @see https://hono.dev/docs/api/request#blob
*/
blob(): Promise<Blob> {
return this.cachedBody('blob')
return this.#cachedBody('blob')
}

/**
Expand All @@ -302,7 +302,7 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
* @see https://hono.dev/docs/api/request#formdata
*/
formData(): Promise<FormData> {
return this.cachedBody('formData')
return this.#cachedBody('formData')
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/router/pattern-router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Route<T> = [RegExp, string, T] // [pattern, method, handler, path]

export class PatternRouter<T> implements Router<T> {
name: string = 'PatternRouter'
private routes: Route<T>[] = []
#routes: Route<T>[] = []

add(method: string, path: string, handler: T) {
const endsWithWildcard = path[path.length - 1] === '*'
Expand Down Expand Up @@ -34,14 +34,14 @@ export class PatternRouter<T> implements Router<T> {
} catch {
throw new UnsupportedPathError()
}
this.routes.push([re, method, handler])
this.#routes.push([re, method, handler])
}

match(method: string, path: string): Result<T> {
const handlers: [T, Params][] = []

for (let i = 0, len = this.routes.length; i < len; i++) {
const [pattern, routeMethod, handler] = this.routes[i]
for (let i = 0, len = this.#routes.length; i < len; i++) {
const [pattern, routeMethod, handler] = this.#routes[i]

if (routeMethod === method || routeMethod === METHOD_NAME_ALL) {
const match = pattern.exec(path)
Expand Down
Loading
Loading