{"library":"protomux-rpc-router","title":"Protomux RPC Router","description":"Protomux RPC Router v1.2.0 wraps protomux-rpc responders with an onion-style middleware stack for HyperDHT servers, enabling composable global and per-method middleware (logging, auth, rate limiting). It integrates with HyperDHT connections and uses compact-encoding for request/response. Alternative to manual middleware wiring in protomux-rpc, with a predictable nested invocation order. Currently maintained, with monthly releases.","language":"javascript","status":"active","last_verified":"Sat Apr 25","install":{"commands":["npm install protomux-rpc-router"],"cli":null},"imports":["import ProtomuxRpcRouter from 'protomux-rpc-router'","import type ProtomuxRpcRouter from 'protomux-rpc-router'","import type { Middleware } from 'protomux-rpc-router'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import ProtomuxRpcRouter from 'protomux-rpc-router'\nimport c from 'compact-encoding'\n\n// Create router with optional capability\nconst router = new ProtomuxRpcRouter({ capability: process.env.CAPABILITY_KEY ? Buffer.from(process.env.CAPABILITY_KEY, 'hex') : undefined })\n\n// Add global middleware (logging example)\nrouter.use({\n  onrequest: async (ctx, next) => {\n    console.log('Request:', ctx.method)\n    const res = await next()\n    console.log('Response:', res)\n    return res\n  },\n  onopen: async () => { console.log('Router started') },\n  onclose: async () => { console.log('Router stopped') }\n})\n\n// Register a method\nconst add = router.method('add', {\n  requestEncoding: c.json,\n  responseEncoding: c.json\n}, async (req) => {\n  return { result: req.a + req.b }\n})\n\n// Add per-method middleware (validation example)\nadd.use({\n  onrequest: async (ctx, next) => {\n    if (typeof ctx.value.a !== 'number' || typeof ctx.value.b !== 'number') {\n      throw new Error('Invalid input')\n    }\n    return next()\n  }\n})\n\n// Simulate a connection (in practice, from HyperDHT)\nconst fakeConnection = {\n  publicKey: Buffer.from('somekey'),\n  open: () => {},\n  close: () => {}\n}\nrouter.handleConnection(fakeConnection)\nawait router.ready()\n// ... later\nawait router.close()","lang":"typescript","description":"Creates a ProtomuxRpcRouter, adds global logging middleware, registers an 'add' method with per-method validation, and attaches to a connection.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}