diff options
author | Elizabeth Hunt <me@liz.coffee> | 2025-08-17 10:54:13 -0700 |
---|---|---|
committer | Elizabeth Hunt <me@liz.coffee> | 2025-08-17 10:54:34 -0700 |
commit | 3d61a9b0c29deb576ecd5d0e8de7b4426d5ab41c (patch) | |
tree | 152b668cf128803fb7636874ecec109fa86034d5 /lib/server/hono | |
parent | 4d469ea6d4cbab34dbb772bcfe3dc53bb67fb224 (diff) | |
download | pengueno-3d61a9b0c29deb576ecd5d0e8de7b4426d5ab41c.tar.gz pengueno-3d61a9b0c29deb576ecd5d0e8de7b4426d5ab41c.zip |
Add common hono proxy and bump minor minor version.
Diffstat (limited to 'lib/server/hono')
-rw-r--r-- | lib/server/hono/index.ts | 1 | ||||
-rw-r--r-- | lib/server/hono/proxy.ts | 71 |
2 files changed, 72 insertions, 0 deletions
diff --git a/lib/server/hono/index.ts b/lib/server/hono/index.ts new file mode 100644 index 0000000..9bcd868 --- /dev/null +++ b/lib/server/hono/index.ts @@ -0,0 +1 @@ +export * from './proxy'; diff --git a/lib/server/hono/proxy.ts b/lib/server/hono/proxy.ts new file mode 100644 index 0000000..f729819 --- /dev/null +++ b/lib/server/hono/proxy.ts @@ -0,0 +1,71 @@ +import { + BaseRequest, + Either, + IEither, + LogMetricTraceable, + Metric, + PenguenoRequest, + Server, + Signals, + TraceUtil, +} from '@emprespresso/pengueno'; + +import { serve, ServerType } from '@hono/node-server'; +import { 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)); + + constructor(private readonly server: Server) {} + + public async serve(port: number, hostname: string): Promise<IEither<Error, void>> { + return this.app + .map((tApp) => + Either.fromFailable<Error, ServerType>(() => { + const app = tApp.get(); + app.all('*', async (c) => + tApp + .flatMap(TraceUtil.withMetricTrace(AppRequestMetric)) + .move(<BaseRequest>c.req) + .flatMap((tRequest) => PenguenoRequest.from(tRequest)) + .map((req) => this.server.serve(req)) + .map( + TraceUtil.promiseify((tResponse) => { + tResponse.trace.trace(AppRequestMetric.count.withValue(1.0)); + return new Response(tResponse.get().body(), tResponse.get()); + }), + ) + .get(), + ); + return serve({ + fetch: (_r) => app.fetch(_r), + port, + hostname, + }); + }), + ) + .peek(TraceUtil.traceResultingEither()) + .peek((tServe) => + tServe + .get() + .mapRight(() => + tServe.trace.trace( + `haii im still listening at http://${hostname}:${port} ~uwu dont think i forgot`, + ), + ), + ) + .map((tEitherServer) => + tEitherServer + .get() + .mapRight((server) => tEitherServer.move(server)) + .flatMapAsync((tServer) => Signals.awaitClose(tServer)), + ) + .peek(TraceUtil.promiseify(TraceUtil.traceResultingEither(AppLifetimeMetric))) + .get(); + } +} |