@cero-base/rpc 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -18
- package/package.json +2 -2
- package/src/build.js +6 -2
- package/src/client.js +9 -0
- package/src/server.js +13 -0
package/README.md
CHANGED
|
@@ -4,13 +4,11 @@ RPC client/server for [@cero-base/core](../core).
|
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
|
-
```
|
|
7
|
+
```bash
|
|
8
8
|
npm install @cero-base/rpc
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
12
|
-
|
|
13
|
-
### Client (renderer / UI process)
|
|
11
|
+
## Client
|
|
14
12
|
|
|
15
13
|
```js
|
|
16
14
|
import { Client, getRpc } from '@cero-base/rpc'
|
|
@@ -19,36 +17,62 @@ const rpc = getRpc(ipcStream, spec)
|
|
|
19
17
|
const client = new Client(rpc, schema, spec)
|
|
20
18
|
await client.ready()
|
|
21
19
|
|
|
22
|
-
//
|
|
20
|
+
// Built-in
|
|
23
21
|
const profile = await client.profile.get()
|
|
24
|
-
client.profile.set({ name: 'Alice' })
|
|
25
|
-
|
|
26
|
-
// Rooms
|
|
27
22
|
const room = await client.rooms.open(null, { name: 'My Room' })
|
|
28
|
-
const messages = room.collection('messages')
|
|
29
|
-
await messages.put({ id: '1', text: 'Hello' })
|
|
30
23
|
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
const invite = await client.devices.invite()
|
|
24
|
+
// Custom methods auto-forward via Proxy
|
|
25
|
+
await client.ping({}) // forwards to rpc.ping({})
|
|
34
26
|
```
|
|
35
27
|
|
|
36
|
-
|
|
28
|
+
Extend with custom logic:
|
|
37
29
|
|
|
38
30
|
```js
|
|
39
|
-
|
|
31
|
+
class ChatClient extends Client {
|
|
32
|
+
constructor(rpc) {
|
|
33
|
+
super(rpc, schema, spec)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async ping() {
|
|
37
|
+
const { time } = await this.rpc.ping({})
|
|
38
|
+
return Date.now() - time
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Server
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
import { Server } from '@cero-base/rpc/server'
|
|
47
|
+
import { getRpc } from '@cero-base/rpc'
|
|
40
48
|
|
|
41
49
|
const rpc = getRpc(ipcStream, spec)
|
|
42
50
|
const server = new Server(db, rpc)
|
|
43
51
|
await server.ready()
|
|
44
52
|
```
|
|
45
53
|
|
|
46
|
-
|
|
54
|
+
Extend with custom handlers — define `onXxx` methods, auto-registered:
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
class ChatServer extends Server {
|
|
58
|
+
onPing() {
|
|
59
|
+
return { time: Date.now() }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async onStats() {
|
|
63
|
+
const rooms = await this.db.rooms.list()
|
|
64
|
+
return { rooms: rooms.length }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Build
|
|
47
70
|
|
|
48
71
|
```js
|
|
49
|
-
import { build } from '@cero-base/rpc'
|
|
72
|
+
import { build } from '@cero-base/rpc/build'
|
|
50
73
|
|
|
51
74
|
await build('./spec', schema)
|
|
75
|
+
await build('./spec', schema, { rpc: customRpc }) // with custom RPC types
|
|
52
76
|
```
|
|
53
77
|
|
|
54
78
|
## Exports
|
|
@@ -60,7 +84,7 @@ await build('./spec', schema)
|
|
|
60
84
|
| `Room` | Client-side room with collections, members, invites |
|
|
61
85
|
| `Collection` | Client-side collection with put/get/del/sub |
|
|
62
86
|
| `getRpc` | Create an RPC instance from an IPC stream + spec |
|
|
63
|
-
| `rpc` | Register RPC types into a hyperschema build
|
|
87
|
+
| `rpc` | Register base RPC types into a hyperschema build |
|
|
64
88
|
| `build` | Build spec with RPC types included |
|
|
65
89
|
|
|
66
90
|
## License
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/rpc",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "RPC client/server for cero-base",
|
|
5
5
|
"files": [
|
|
6
6
|
"src",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"test": "brittle test/*.js"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@cero-base/core": "^0.0.
|
|
27
|
+
"@cero-base/core": "^0.0.2",
|
|
28
28
|
"compact-encoding": "^2.19.1",
|
|
29
29
|
"framed-stream": "^1.0.1",
|
|
30
30
|
"ms": "^2.1.3",
|
package/src/build.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { build as coreBuild } from '@cero-base/core/builder'
|
|
2
2
|
import { rpc } from './schema.js'
|
|
3
3
|
|
|
4
|
-
export async function build(dir, schema) {
|
|
5
|
-
|
|
4
|
+
export async function build(dir, schema, opts = {}) {
|
|
5
|
+
const builder = (ctx) => {
|
|
6
|
+
rpc(ctx)
|
|
7
|
+
if (opts.rpc) opts.rpc(ctx)
|
|
8
|
+
}
|
|
9
|
+
return coreBuild(dir, schema, { rpc: builder })
|
|
6
10
|
}
|
package/src/client.js
CHANGED
|
@@ -57,6 +57,15 @@ export class Client extends ReadyResource {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
this.ready().catch(safetyCatch)
|
|
60
|
+
|
|
61
|
+
return new Proxy(this, {
|
|
62
|
+
get(target, prop) {
|
|
63
|
+
if (prop in target) return target[prop]
|
|
64
|
+
const fn = target.rpc[prop]
|
|
65
|
+
if (typeof fn === 'function') return (...args) => fn.call(target.rpc, ...args)
|
|
66
|
+
return undefined
|
|
67
|
+
}
|
|
68
|
+
})
|
|
60
69
|
}
|
|
61
70
|
|
|
62
71
|
async _open() {
|
package/src/server.js
CHANGED
|
@@ -7,6 +7,19 @@ export class Server {
|
|
|
7
7
|
this.rpc = rpc
|
|
8
8
|
this.rooms = new Map()
|
|
9
9
|
this._registerHandlers()
|
|
10
|
+
this._registerCustomHandlers()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_registerCustomHandlers() {
|
|
14
|
+
let proto = Object.getPrototypeOf(this)
|
|
15
|
+
while (proto && proto !== Server.prototype) {
|
|
16
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
17
|
+
if (key.startsWith('on') && key.length > 2 && typeof this[key] === 'function') {
|
|
18
|
+
this.rpc[key](this[key].bind(this))
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
proto = Object.getPrototypeOf(proto)
|
|
22
|
+
}
|
|
10
23
|
}
|
|
11
24
|
|
|
12
25
|
async ready() {
|