diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2025-12-07 15:15:35 -0800 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2025-12-07 15:15:35 -0800 |
| commit | eeca787c81ac92e46a9375a0b1ce8baa29852b1a (patch) | |
| tree | 0960a4a76b3f4c225a7e30c2c3e7981eb603b9e8 | |
| parent | a8cc3f5312f6fa9a9675be4c68e209f45621c4a2 (diff) | |
| download | pengueno-eeca787c81ac92e46a9375a0b1ce8baa29852b1a.tar.gz pengueno-eeca787c81ac92e46a9375a0b1ce8baa29852b1a.zip | |
Lazily load hono
| -rw-r--r-- | lib/server/hono/proxy.ts | 24 | ||||
| -rw-r--r-- | package.json | 6 |
2 files changed, 23 insertions, 7 deletions
diff --git a/lib/server/hono/proxy.ts b/lib/server/hono/proxy.ts index f729819..7f93804 100644 --- a/lib/server/hono/proxy.ts +++ b/lib/server/hono/proxy.ts @@ -10,21 +10,33 @@ import { TraceUtil, } from '@emprespresso/pengueno'; -import { serve, ServerType } from '@hono/node-server'; -import { Hono } from 'hono'; +import type { ServerType } from '@hono/node-server'; +import type { Hono } from 'hono'; const AppLifetimeMetric = Metric.fromName('HonoAppLifetime').asResult(); const AppRequestMetric = Metric.fromName('HonoAppRequest'); export class HonoProxy { - private readonly app = LogMetricTraceable.of(new Hono()) - .flatMap(TraceUtil.withTrace(`AppId = ${crypto.randomUUID()}`)) - .flatMap(TraceUtil.withMetricTrace(AppLifetimeMetric)); + private app?: LogMetricTraceable<Hono>; constructor(private readonly server: Server) {} + private async initializeApp() { + if (this.app) return this.app; + + const { Hono } = await import('hono'); + this.app = LogMetricTraceable.of(new Hono()) + .flatMap(TraceUtil.withTrace(`AppId = ${crypto.randomUUID()}`)) + .flatMap(TraceUtil.withMetricTrace(AppLifetimeMetric)) as LogMetricTraceable<Hono>; + + return this.app; + } + public async serve(port: number, hostname: string): Promise<IEither<Error, void>> { - return this.app + const { serve } = await import('@hono/node-server'); + + const app = await this.initializeApp(); + return app .map((tApp) => Either.fromFailable<Error, ServerType>(() => { const app = tApp.get(); diff --git a/package.json b/package.json index 6686eef..3355d62 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@emprespresso/pengueno", - "version": "0.0.16", + "version": "0.0.17", "description": "emprespresso pengueno utils", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -20,6 +20,10 @@ "format": "prettier --write .", "format:check": "prettier --check ." }, + "optionalDependencies": { + "@hono/node-server": "^1.18.2", + "hono": "^4.8.0" + }, "devDependencies": { "@hono/node-server": "^1.18.2", "@types/jest": "^30.0.0", |
