Skip to content

Commit b2ce1af

Browse files
committed
refactor: move middleware normalization out of core
1 parent f45dd27 commit b2ce1af

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/h3.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ export const H3Core = /* @__PURE__ */ (() => {
178178
return this as unknown as H3Type;
179179
}
180180

181+
_normalizeMiddleware(fn: Middleware, _opts: any): Middleware {
182+
return fn;
183+
}
184+
181185
use(arg1: unknown, arg2?: unknown, arg3?: unknown): H3Type {
182186
let route: string | undefined;
183187
let fn: Middleware | H3Type;
@@ -191,10 +195,7 @@ export const H3Core = /* @__PURE__ */ (() => {
191195
opts = arg2 as MiddlewareOptions;
192196
}
193197
this._middleware.push(
194-
normalizeMiddleware(
195-
fn as Middleware,
196-
route ? { ...opts, route } : opts,
197-
),
198+
this._normalizeMiddleware(fn as Middleware, { ...opts, route }),
198199
);
199200
return this as unknown as H3Type;
200201
}
@@ -231,4 +232,11 @@ export class H3 extends H3Core {
231232
addRoute(this._rou3, _route.method, _route.route!, _route);
232233
super._addRoute(_route);
233234
}
235+
236+
override _normalizeMiddleware(
237+
fn: Middleware,
238+
opts?: MiddlewareOptions & { route?: string },
239+
): Middleware {
240+
return normalizeMiddleware(fn as Middleware, opts);
241+
}
234242
}

src/types/h3.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ export declare class H3 {
113113
/** @internal */
114114
_getMiddleware(route: MatchedRoute<H3Route> | void): Middleware[];
115115

116+
/** @internal */
117+
_normalizeMiddleware(
118+
fn: Middleware,
119+
_opts?: MiddlewareOptions & { route?: string },
120+
): Middleware;
121+
116122
/** @internal */
117123
_addRoute(_route: H3Route): void;
118124

test/bench/bundle.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("benchmark", () => {
2020
`Bundle size (H3): ${bundle.bytes} (gzip: ${bundle.gzipSize})`,
2121
);
2222
}
23-
expect(bundle.bytes).toBeLessThanOrEqual(10_000); // <10kb
23+
expect(bundle.bytes).toBeLessThanOrEqual(11_000); // <11kb
2424
expect(bundle.gzipSize).toBeLessThanOrEqual(4000); // <4kb
2525
});
2626

@@ -38,8 +38,8 @@ describe("benchmark", () => {
3838
`Bundle size (H3Core): ${bundle.bytes} (gzip: ${bundle.gzipSize})`,
3939
);
4040
}
41-
expect(bundle.bytes).toBeLessThanOrEqual(8000); // <8kb
42-
expect(bundle.gzipSize).toBeLessThanOrEqual(3500); // <3.5kb
41+
expect(bundle.bytes).toBeLessThanOrEqual(7500); // <7.5kb
42+
expect(bundle.gzipSize).toBeLessThanOrEqual(3000); // <3kb
4343
});
4444

4545
it("bundle size (defineHandler)", async () => {
@@ -56,7 +56,7 @@ describe("benchmark", () => {
5656
`Bundle size (defineHandler): ${bundle.bytes} (gzip: ${bundle.gzipSize})`,
5757
);
5858
}
59-
expect(bundle.bytes).toBeLessThanOrEqual(5400); // <5.4kb
59+
expect(bundle.bytes).toBeLessThanOrEqual(5300); // <5.3kb
6060
expect(bundle.gzipSize).toBeLessThanOrEqual(2200); // <2.2kb
6161
});
6262
});

0 commit comments

Comments
 (0)