Registry / communication / http-rpc

http-rpc

JSON →
library2.5.1jsnpmunverified

A JavaScript/TypeScript RPC library inspired by JSON-RPC and gRPC that works over HTTP with browser fetch(). Current stable version is 2.5.1 (released 2023). Provides single-trip CORS handling, built-in edge caching (browser and CDN), JWT token support, configurable timeout and idempotency, and both client and server invocation. Key differentiators: designed specifically for browser-to-server communication with minimal overhead, no preflight CORS requests, and integrated caching headers. Requires Node.js >=14.0.0.

npm install http-rpc
INSTALL
IMPORT
SIG · HTTP-RPC
H
http-rpc
communicationjavascriptv2.5.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Serv
import { Serv } from 'http-rpc/node-srv/lib/Serv'
const Serv = require('http-rpc').Serv
ESM-only package; must use dynamic import or ESM syntax. The deep import path is required for server-side usage.
BaseRPCMethodHandler
import { BaseRPCMethodHandler } from 'http-rpc/node-srv/lib/Serv'
import { BaseRPCMethodHandler } from 'http-rpc'
Not exported from package root; must use the specific subpath.
HttpRPC (client)
import { HttpRPC } from 'https://cdn.jsdelivr.net/npm/http-rpc@2.5.0/webApp/httpRPC.min.js'
import { HttpRPC } from 'http-rpc'
Client-side import uses a CDN URL; the npm package root does not export a browser-compatible build.

Shows setup of an HTTP-RPC server with a method handler and client-side invocation.

// Server-side import { BaseRPCMethodHandler, Serv } from 'http-rpc/node-srv/lib/Serv'; function doMultiply(a, b) { return a * b; } class Handler1 extends BaseRPCMethodHandler { constructor() { super(2, 1); // browser cache 2s, CDN cache 1s } multiply(params) { const a = params.a; const b = params.b; const token = params.token; return [token, doMultiply(a, b)]; } } const service = new Serv(['*']); const h1 = new Handler1(); service.routeRPC('api', h1); service.listen(8888); // Client-side (browser) import { HttpRPC } from 'https://cdn.jsdelivr.net/npm/http-rpc@2.5.0/webApp/httpRPC.min.js'; const rpc = new HttpRPC('http', 'localhost', 8888); const params = { a: 5, b: 2, token: 'YOUR_JWT_TOKEN' }; const [newToken, result] = await rpc.invoke('api', 'multiply', params); console.log(result); // 10
Debug
Known issues
gotchaDefault export is not available; must use deep imports for server code.
fix
Use: import { Serv, BaseRPCMethodHandler } from 'http-rpc/node-srv/lib/Serv'
affects: >=2.0.0
gotchaClient-side code must be loaded from CDN, not from npm package root.
fix
Use URL: https://cdn.jsdelivr.net/npm/http-rpc@2.5.0/webApp/httpRPC.min.js
affects: >=2.0.0
gotchaConstructor expects cache parameters; super(cacheBrowser, cacheCDN) not optional.
fix
Always call super(browserCacheSeconds, cdnCacheSeconds)
affects: >=2.0.0
deprecatedThe method signature expects returning an array [token, data] where token is required.
fix
Always return [token, result] from handler methods, even if token is unchanged.
affects: >=2.0.0
Errors
Common errors & fixes
Cannot find module 'http-rpc' or its corresponding type declarations.
Importing from package root instead of deep import path.
fix
Use: import { Serv } from 'http-rpc/node-srv/lib/Serv'
TypeError: httpRPC.invoke is not a function
Using wrong import (e.g., require or default import) for client library.
fix
Use named import: import { HttpRPC } from 'https://cdn.jsdelivr.net/npm/http-rpc@2.5.0/webApp/httpRPC.min.js'
Property 'multiply' does not exist on type 'BaseRPCMethodHandler'
TypeScript does not infer methods added via class extension.
fix
Define an interface for the handler: interface Handler1Type extends BaseRPCMethodHandler { multiply(params: any): [string, any]; }
Upgrade
Version history
2.5.1latest on npm
Audit
Dependencies
node-fetchoptionalUsed server-side for HTTP requests in Node.js environments
jsonwebtokenoptionalRequired for JWT token generation and verification if JWT features used
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
http-rpc — npm install http-rpc · libregistry