Skip to content

Commit 69c11f8

Browse files
Add API to README.md (#4)
1 parent baa198a commit 69c11f8

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,148 @@
66
npm i bare-rpc
77
```
88

9+
## Usage
10+
11+
```js
12+
import RPC from 'bare-rpc'
13+
14+
const rpc = new RPC(stream, (req) => {
15+
if (req.command === 42) {
16+
console.log(req.data.toString()) // ping
17+
req.reply('pong')
18+
}
19+
})
20+
21+
// On the other end
22+
const req = rpc.request(42)
23+
req.send('ping')
24+
25+
const replyBuffer = await req.reply()
26+
console.log(replyBuffer.toString()) // pong
27+
```
28+
29+
## API
30+
31+
### `RPC`
32+
33+
#### `const rpc = new RPC(stream, [onrequest])`
34+
35+
Create an RPC instance using a duplex `stream`. `onrequest` is an optional callback run when a remote request is received. This is where processing and responding to an RPC request happens. `onrequest` receives a `RPCIncomingRequest` as an argument, for example:
36+
37+
```js
38+
const rpc = new RPC(stream, (req) => {
39+
if (req.command === 42) {
40+
req.reply('pong')
41+
}
42+
})
43+
```
44+
45+
See [`RPCIncomingRequest`](#rpcincomingrequest) for properties and methods for processing the request.
46+
47+
`onrequest` can also be a [`RPCCommandRouter`](#rpccommandrouter).
48+
49+
#### `const req = rpc.request(command)`
50+
51+
Create a request for `command`. `command` is a unique number that should be used to differentiate different requests on the remote end. Returns a `RPCOutgoingRequest`.
52+
53+
### `RPCOutgoingRequest`
54+
55+
#### `req.command`
56+
57+
The command that the request was created with. A command is a unique number.
58+
59+
#### `req.sent`
60+
61+
A boolean for whether the request has been sent.
62+
63+
#### `req.received`
64+
65+
A boolean for whether the request has received a reply.
66+
67+
#### `req.send(data, [encoding])`
68+
69+
Send the request with the provided `data`. `data` can be a buffer or a string which will be encoded using `encoding`.
70+
71+
`.send()` can only be called once per request.
72+
73+
#### `const data = await req.reply([encoding])`
74+
75+
Await the reply from the remote end to the request. `encoding` can be defined for decoding the response `data` buffer back into a string.
76+
77+
#### `const stream = req.createRequestStream([opts])`
78+
79+
Create a [`Writable`](https://github.com/mafintosh/streamx?tab=readme-ov-file#writable-stream) stream for sending data with the request.
80+
81+
#### `const stream = req.createResponseStream([opts])`
82+
83+
Create a [`Readable`](https://github.com/mafintosh/streamx?tab=readme-ov-file#readable-stream) stream for receiving data in reply to the request.
84+
85+
### `RPCIncomingRequest`
86+
87+
#### `req.command`
88+
89+
The command that the request was sent as. A command is a unique number.
90+
91+
#### `req.data`
92+
93+
The data buffer sent with the request.
94+
95+
#### `req.sent`
96+
97+
A boolean for whether a reply has been sent.
98+
99+
#### `req.received`
100+
101+
A boolean for whether the request has been received as a stream. See [`req.createRequestStream()`](#const-stream--reqcreaterequeststream) for receiving requests as a stream.
102+
103+
#### `req.reply(data, [encoding])`
104+
105+
Reply to the request with the provided `data`. `data` can be a buffer or a string which will be encoded using `encoding`.
106+
107+
#### `const stream = req.createRequestStream([opts])`
108+
109+
Create a `Readable` stream for receiving data from the request.
110+
111+
#### `const stream = req.createResponseStream([opts])`
112+
113+
Create a `Writable` stream for sending data in reply to the request.
114+
115+
### `RPCCommandRouter`
116+
117+
An alternative way to define commands and handlers for receiving them.
118+
119+
#### `const router = new RPC.CommandRouter()`
120+
121+
Create a new command router. This router can then be used when creating an
122+
`rpc`. For example:
123+
124+
```js
125+
const router = new RPC.CommandRouter()
126+
127+
router.respond(42, (req, data) => {
128+
console.log(data.toString()) // ping
129+
130+
return Buffer.from('pong')
131+
})
132+
133+
const rpc = new RPC(stream, router)
134+
const req = rpc.request(42)
135+
req.send('ping')
136+
```
137+
138+
#### `router.respond(command, [opts], async (req, data) => {})`
139+
140+
Define a command and the handler for it. The callback for a command receives both the request (`req`) and the `data` buffer and can return a value to respond. If the request is responded to in the callback, the return value is ignored.
141+
142+
`opts` can include:
143+
144+
```
145+
{
146+
requestEncoding: c.raw, // Encoding for decoding incoming request
147+
responseEncoding: c.raw, // Encoding for outgoing response
148+
}
149+
```
150+
9151
## License
10152

11153
Apache-2.0

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface RPCOutgoingRequest {
5050

5151
send(data: Buffer | string, encoding?: BufferEncoding): void
5252

53-
reply(encoding?: BufferEncoding): Promise<Buffer>
53+
reply(encoding?: BufferEncoding): Promise<Buffer | string>
5454

5555
createRequestStream(opts?: WritableOptions): RPCOutgoingStream
5656
createResponseStream(opts?: ReadableOptions): RPCIncomingStream

0 commit comments

Comments
 (0)