feat: add new hono adapter (and some niceties based on that)#6
Open
ultraviolet10 wants to merge 1 commit intoVanshSahay:mainfrom
Open
feat: add new hono adapter (and some niceties based on that)#6ultraviolet10 wants to merge 1 commit intoVanshSahay:mainfrom
ultraviolet10 wants to merge 1 commit intoVanshSahay:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add Hono Framework Adapter with Architectural Refactoring
Summary
This PR introduces first-class Hono framework support alongside Express, while refactoring the adapter architecture for better extensibility, type safety, and memory efficiency.
🚀 New Feature: Hono Adapter
Added complete Hono framework support as a peer dependency, enabling users to integrate x402 payment flows into Hono-based applications.
Usage:
New entry point:
x402-open/hono— separate from the main entry point to allow tree-shaking when Hono isn't used.🏗️ Architectural Improvements
Shared Gateway Core (
src/gateway/core.ts)Extracted all gateway logic into a framework-agnostic core module. Both Express and Hono gateway adapters now consume the same underlying implementation:
StickyRouter— Maintains payer/header → peer mappings with TTL for sticky routingPeerRegistry— Tracks dynamically registered peers with heartbeat-based TTLpostJson<T>()— Generic typed HTTP client with timeout supportnormalizeUrl()— Consistent URL trailing-slash handlingaggregateSupportedKinds()— Aggregates payment kinds from multiple peersThis design allows adding new framework adapters (Fastify, Koa, etc.) by implementing thin wrappers around the core.
Type Definitions (
src/gateway/types.ts)New centralized type definitions with proper x402 type integration:
Memory Management
Added automatic cleanup for TTL-based caches to prevent memory leaks in long-running processes:
Both
StickyRouterandPeerRegistryuse.unref()on timers to allow clean process exit.Shared Error Handler (
src/adapters/shared/errorHandler.ts)Extracted duplicated error formatting into a shared utility:
Used by both
expressAdapter.tsandhonoAdapter.ts.📁 New Files
src/hono.tscreateHonoAdapterandcreateHonoGatewayAdaptersrc/adapters/honoAdapter.tssrc/adapters/honoGateway.tssrc/gateway/core.tssrc/gateway/types.tssrc/adapters/shared/errorHandler.tstest/cleanup.test.tsStickyRouterandPeerRegistrycleanuptest/e2e.hono.test.ts📦 Package Bundling
Dual Entry Points
{ "exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" }, "./hono": { "types": "./dist/hono.d.ts", "import": "./dist/hono.js" } } }x402-open) — Express adapter, HTTP gateway, facilitator, registrarx402-open/hono) — Hono-specific adaptersPeer Dependencies
{ "peerDependencies": { "express": ">=4", "hono": ">=4" }, "peerDependenciesMeta": { "express": { "optional": true }, "hono": { "optional": true } } }Both frameworks are optional — users only install what they need.
ESM Compliance
.jsextensions for Node.js ESM resolutionnode --input-type=moduleimportsDependency Cleanup
honofromdevDependencies(already inpeerDependencies)🧪 Testing
New Test Files
test/cleanup.test.ts— 9 unit tests for memory management:StickyRouter: removes expired entries, retains valid entries,destroy()clears statePeerRegistry: removes stale peers, retains recent peers, heartbeat refresh workstest/e2e.hono.test.ts— 8 E2E tests:/supportedendpointTest Results
Verification Commands
🔧 Type Safety Improvements
anytypes with proper x402 imports (PaymentPayload,PaymentRequirements,SupportedPaymentKind)catch (e: any)tocatch (e: unknown)with proper error extractionpostJson<T>()andPeerResponse<T>for typed peer responses/registerendpointsFile Changes Summary
src/gateway/core.tssrc/gateway/types.tssrc/hono.tssrc/adapters/honoAdapter.tssrc/adapters/honoGateway.tssrc/adapters/shared/errorHandler.tssrc/adapters/expressAdapter.tssrc/, uses shared error handlersrc/httpGateway.tsgateway/core.tssrc/registrar.tsSupportedPaymentKind[]typingsrc/facilitator.tssrc/index.tspackage.jsontest/cleanup.test.tstest/e2e.hono.test.ts