better-grpc 0.0.1 → 0.1.0
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/LICENSE +21 -0
- package/README.md +156 -2
- package/dist/index.cjs +24460 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +57 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +24454 -0
- package/dist/index.js.map +1 -0
- package/package.json +63 -8
- package/index.js +0 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Photon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,13 +1,167 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# better-grpc
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
> Simple, typed gRPC for TypeScript
|
|
6
|
+
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
[](https://www.typescriptlang.org/)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
[](https://discord.gg/bZd4CMd2H5)
|
|
12
|
+
|
|
13
|
+
**`better-grpc`** is a library that provides a new way to define and use RPC services in TypeScript, focusing on developer experience and type safety. It eliminates the need for `.proto` files and code generation, allowing you to define your services entirely in TypeScript.
|
|
14
|
+
|
|
15
|
+
The core idea is to enable seamless communication between a client and a server, allowing you to call server-side functions from the client and client-side functions from the server, as if they were local.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- **Type-Safe:** Define your services in TypeScript and get full type safety and autocompletion for your clients and servers.
|
|
20
|
+
- **No `.proto` files:** No need to write `.proto` files or use `protoc` to generate code.
|
|
21
|
+
- **Simple API:** The API is designed to be simple and intuitive.
|
|
22
|
+
- **Symmetric Experience:** Call client-side functions from the server with the same syntax as calling server-side functions from the client.
|
|
4
23
|
|
|
5
24
|
## Installation
|
|
6
25
|
|
|
7
26
|
```bash
|
|
27
|
+
bun add better-grpc
|
|
28
|
+
# or
|
|
8
29
|
npm install better-grpc
|
|
30
|
+
# or
|
|
31
|
+
yarn add better-grpc
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
### 1. Define a Service
|
|
37
|
+
|
|
38
|
+
Create an abstract class that extends `Service` to define your service. Use the `server` and `client` helpers to define where your function is implemented and executed.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { Service, client, server } from 'better-grpc';
|
|
42
|
+
|
|
43
|
+
abstract class MyService extends Service('MyService') {
|
|
44
|
+
// This function is implemented and executed on the server.
|
|
45
|
+
sayHello = server<(name: string) => string>();
|
|
46
|
+
|
|
47
|
+
// This function is implemented and executed on the client.
|
|
48
|
+
log = client<(message: string) => void>();
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Implement the Service
|
|
53
|
+
|
|
54
|
+
Provide the implementations for the functions you defined for both the server and the client.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// Server-side implementation
|
|
58
|
+
const myServiceImpl = MyService.Server({
|
|
59
|
+
async sayHello(name: string) {
|
|
60
|
+
return `Hello, ${name}!`;
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Client-side implementation
|
|
65
|
+
const myClientImpl = MyService.Client({
|
|
66
|
+
async log(message: string) {
|
|
67
|
+
console.log(`[Server]: ${message}`);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Create a Server
|
|
73
|
+
|
|
74
|
+
Create and start the server, passing in your service implementation.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { createGrpcServer } from 'better-grpc';
|
|
78
|
+
|
|
79
|
+
const server = await createGrpcServer(50051, myServiceImpl);
|
|
80
|
+
console.log('Server listening on port 50051');
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 4. Create a Client
|
|
84
|
+
|
|
85
|
+
Create a client for your service.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { createGrpcClient } from 'better-grpc';
|
|
89
|
+
|
|
90
|
+
const client = await createGrpcClient('localhost:50051', myClientImpl);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 5. Make remote calls
|
|
94
|
+
|
|
95
|
+
Now you can call remote functions from both the client and the server.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// On the client, call the server's `sayHello` function
|
|
99
|
+
const response = await client.MyService.sayHello('world');
|
|
100
|
+
console.log(response); // Outputs: 'Hello, world!'
|
|
101
|
+
|
|
102
|
+
// On the server, call client's `log` function
|
|
103
|
+
await server.MyService.log('Greeting from server');
|
|
104
|
+
// The client's console will show: '[Server]: Greeting from server'
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Why `better-grpc`?
|
|
108
|
+
|
|
109
|
+
The traditional workflow for creating gRPC services with TypeScript involves writing `.proto` files, using `protoc` to generate TypeScript code, and then using that generated code. This process can be cumbersome and result in a disconnect between your service definition and your code.
|
|
110
|
+
|
|
111
|
+
`better-grpc` solves this problem by allowing you to define your services entirely in TypeScript. This has several advantages:
|
|
112
|
+
|
|
113
|
+
- **Single Source of Truth:** Your service definition lives in your TypeScript code, right next to your implementation.
|
|
114
|
+
- **Improved Type Safety:** Leverage TypeScript's powerful type system for excellent autocompletion and type safety across your client and server.
|
|
115
|
+
- **Simplified Workflow:** No more `.proto` files, no more code generation. Just write TypeScript.
|
|
116
|
+
- **Symmetric Communication:** The server can invoke client functions with the same ease that the client invokes server functions, enabling powerful, bidirectional communication patterns.
|
|
117
|
+
|
|
118
|
+
## API
|
|
119
|
+
|
|
120
|
+
- `Service(name: string)`
|
|
121
|
+
|
|
122
|
+
A factory function that creates an abstract service class.
|
|
123
|
+
|
|
124
|
+
- `server<T>()`
|
|
125
|
+
|
|
126
|
+
A helper function to define a server-side function signature. `T` should be a function type.
|
|
127
|
+
|
|
128
|
+
- `client<T>()`
|
|
129
|
+
|
|
130
|
+
A helper function to define a client-side function signature. `T` should be a function type.
|
|
131
|
+
|
|
132
|
+
- `createGrpcServer(port: number, ...services: ServiceImpl[])`
|
|
133
|
+
|
|
134
|
+
Creates and starts a gRPC server.
|
|
135
|
+
|
|
136
|
+
- `createGrpcClient(address: string, ...services: ServiceImpl[])`
|
|
137
|
+
|
|
138
|
+
Creates and starts a gRPC client.
|
|
139
|
+
|
|
140
|
+
## Benchmarks
|
|
141
|
+
|
|
142
|
+
### Simple "Hello World"
|
|
143
|
+
|
|
144
|
+
> [!NOTE]
|
|
145
|
+
> This benchmark's server and client were run on same local machine.
|
|
146
|
+
|
|
147
|
+
#### tRPC
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
tRPC: 1543.021833ms
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### Elysia
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
Elysia: 128.935791ms
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### better-grpc
|
|
160
|
+
|
|
161
|
+
```
|
|
162
|
+
better-grpc: 126.681042ms
|
|
9
163
|
```
|
|
10
164
|
|
|
11
165
|
## License
|
|
12
166
|
|
|
13
|
-
MIT
|
|
167
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|