Gathering detailed insights and metrics for picorpc
Gathering detailed insights and metrics for picorpc
Gathering detailed insights and metrics for picorpc
Gathering detailed insights and metrics for picorpc
A tiny RPC library and spec, inspired by JSON-RPC 2.0 and tRPC.
npm install picorpc
Typescript
Module System
Node Version
NPM Version
73.9
Supply Chain
98.1
Quality
75.1
Maintenance
100
Vulnerability
100
License
TypeScript (66.41%)
JavaScript (33.59%)
Total Downloads
2,299
Last Day
2
Last Week
23
Last Month
49
Last Year
1,245
94 Stars
19 Commits
2 Forks
2 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.2
Package Id
picorpc@1.1.2
Unpacked Size
62.43 kB
Size
13.35 kB
File Count
55
NPM Version
10.2.3
Node Version
18.19.0
Publised On
07 Jan 2024
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
228.6%
23
Compared to previous week
Last month
81.5%
49
Compared to previous month
Last year
18.1%
1,245
Compared to previous year
2
4
A tiny RPC library and spec, inspired by JSON-RPC 2.0 and tRPC.
1npm install --save picorpc
1import {createAbstractClient, createAbstractServer} from 'picorpc'; 2import {createMemoryClient, createMemoryServer} from 'picorpc'; 3import {createHttpClient, createHttpServer} from 'picorpc'; 4 5// Custom map of procedures to expose to the client 6 7const Procedures = { 8 ctx () { // This procedure reads something from the context, which will be attached to "this" 9 return this.value; 10 }, 11 sum ( a, b ) { // This procedure is a simple pure function that doesn't use the context 12 return a + b; 13 }, 14 throw () { // This procedure throws, with a custom error code and data value 15 const error = new Error ( 'Some custom error' ); 16 error.code = 1; 17 error.data = 'Some data'; 18 throw error; 19 } 20}; 21 22// Create an in-memory client/server 23// An in-memory server behind a proper HTTP server is the recommended way to expose procedures via an API 24// An in-memory client is mostly useful for testing 25 26const server = createMemoryServer ({ 27 procedures: Procedures 28}); 29 30const client = createMemoryClient ({ 31 server, // The in-memory server to pass requests to 32 context: () => ({ // Custom, optional, context object, which will be attached to every request automatically 33 value: 123 34 }) 35}); 36 37await client.sum ( 1, 2 ); // => 3 38await client.ctx (); // => 123 39await client.throw (); // => This will throw 40 41// Create an HTTP client/server 42// The HTTP server is intended for simple internal inter-process communication 43// The HTTP client makes request with the fetch API, so it works everywhere 44 45const client = createHttpClient ({ 46 context: () => ({ // Custom, optional, context object, which will be attached to every request automatically 47 value: 123 48 }), 49 serializer: JSON.stringify, // Custom, optional, serializer 50 deserializer: JSON.parse, // Custom, optional, deserializer 51 url: 'http://localhost:6000' // Required endpoint URL 52}); 53 54const server = createHttpServer ({ 55 serializer: JSON.stringify, // Custom, optional, serializer 56 deserializer: JSON.parse, // Custom, optional, deserializer 57 port: 6000, // The port to start listening at 58 procedures: Procedures 59}); 60 61await client.sum ( 1, 2 ); // => 3 62await client.ctx (); // => 123 63await client.throw (); // => This will throw 64 65server.close (); // Close the server, stopping listening 66 67// Create an abstract server, to use your own transport protocol 68 69const client = createAbstractClient ({ 70 handler: request => { 71 //TODO: Send the request somewhere and read the result back 72 } 73}); 74 75const client = createAbstractServer ({ 76 procedures: Procedures, 77 handler: response => { 78 //TODO: Do something with the response object, optionally 79 } 80});
MIT © Fabio Spampinato
No vulnerabilities found.
No security vulnerabilities found.