lixnet 1.3.8 → 1.3.10
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 +366 -0
- package/dist/exports.js +1 -0
- package/dist/lib/peer.js +1 -1
- package/dist/lib/util/getDefaultFormatter.js +1 -0
- package/dist/lib/util/response.d.ts +6 -11
- package/package.json +14 -4
- package/.gitattributes +0 -2
- package/dist/lib/util/formatter.d.ts +0 -2
- package/dist/lib/util/formatter.js +0 -26
package/README.md
CHANGED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>Lixnet</h1>
|
|
3
|
+
<h3>Lightweight, type-safe RPC over HTTP and WebSockets.<br />The tRPC and Next.js Server Actions, but for everyone.</h3>
|
|
4
|
+
|
|
5
|
+
<a href="https://github.com/Nitlix/Lixnet">
|
|
6
|
+
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/Nitlix/Lixnet?style=social">
|
|
7
|
+
</a>
|
|
8
|
+
|
|
9
|
+
<a href="https://www.npmjs.com/package/lixnet">
|
|
10
|
+
<img alt="npm version" src="https://img.shields.io/npm/v/lixnet.svg">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/lixnet">
|
|
13
|
+
<img alt="weekly downloads" src="https://img.shields.io/npm/dm/lixnet.svg">
|
|
14
|
+
</a>
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
<br />
|
|
18
|
+
|
|
19
|
+
## Intro
|
|
20
|
+
|
|
21
|
+
Lixnet is a tiny yet powerful TypeScript-first RPC library for teams who want a **small, explicit RPC layer** with excellent typing, without codegen or framework lock-in.
|
|
22
|
+
|
|
23
|
+
If you like the ergonomics of **tRPC** or **Next.js Server Actions**, but want something that:
|
|
24
|
+
|
|
25
|
+
- stays close to the **Fetch** platform (`Request`/`Response`)
|
|
26
|
+
- works in **Bun / Next.js / Workers**
|
|
27
|
+
- keeps the protocol simple and inspectable
|
|
28
|
+
|
|
29
|
+
Lixnet is a good fit.
|
|
30
|
+
|
|
31
|
+
It gives you:
|
|
32
|
+
|
|
33
|
+
- **HTTP RPC** via `fetch` (`LixnetServer` + `LixnetClient`)
|
|
34
|
+
- **WebSocket events + callbacks** (`LixnetPeer`)
|
|
35
|
+
- **Optional Zod validation** on the server
|
|
36
|
+
- **Next.js-like** `request.cookies()` / `request.headers()` ergonomics inside handlers (without pulling in Next)
|
|
37
|
+
|
|
38
|
+
It’s designed to be minimal: no codegen, no schema registry, no client bundler magic.
|
|
39
|
+
|
|
40
|
+
## Table of contents
|
|
41
|
+
|
|
42
|
+
- [Quickstart (HTTP RPC)](#quickstart-http-rpc)
|
|
43
|
+
- [Framework adapters (server wiring)](#framework-adapters-server-wiring)
|
|
44
|
+
- [Server: `LixnetServer`](#server-lixnetserver)
|
|
45
|
+
- [Client: `LixnetClient`](#client-lixnetclient)
|
|
46
|
+
- [Handlers: `request` and `response`](#handlers-request-and-response)
|
|
47
|
+
- [WebSockets: `LixnetPeer`](#websockets-lixnetpeer)
|
|
48
|
+
- [API reference](#api-reference)
|
|
49
|
+
- [Contributor docs](#contributor-docs)
|
|
50
|
+
- [License](#license)
|
|
51
|
+
|
|
52
|
+
## Quickstart (HTTP RPC)
|
|
53
|
+
|
|
54
|
+
Install:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
bun add lixnet zod
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Other package managers:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
pnpm add lixnet zod
|
|
64
|
+
npm i lixnet zod
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 1) Define an `Events` type
|
|
68
|
+
|
|
69
|
+
This is the contract shared by server and client.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
type Events = {
|
|
73
|
+
greet: (input: { name: string }) => Promise<string>;
|
|
74
|
+
};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 2) Create the RPC server
|
|
78
|
+
|
|
79
|
+
Register handlers with optional Zod schemas, then expose an HTTP endpoint that forwards the incoming `Request` to `server.handle(...)`.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { z } from "zod";
|
|
83
|
+
import { LixnetServer } from "lixnet";
|
|
84
|
+
|
|
85
|
+
type Events = {
|
|
86
|
+
greet: (input: { name: string }) => Promise<string>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const server = new LixnetServer<Events>({
|
|
90
|
+
debugLog: false,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
server.on({
|
|
94
|
+
event: "greet",
|
|
95
|
+
schema: z.object({ name: z.string() }),
|
|
96
|
+
handler: async ({ name, request, response }) => {
|
|
97
|
+
// Optional: read request cookies/headers (Next-like API)
|
|
98
|
+
const userAgent = request.headers().get("user-agent");
|
|
99
|
+
if (userAgent) response.header("x-user-agent", userAgent);
|
|
100
|
+
|
|
101
|
+
return `Hello, ${name}!`;
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 3) Wire the HTTP endpoint
|
|
107
|
+
|
|
108
|
+
Lixnet uses the Fetch standard `Request`/`Response`, so wiring is always “forward `Request` into `server.handle(...)`”.
|
|
109
|
+
|
|
110
|
+
## Framework adapters (server wiring)
|
|
111
|
+
|
|
112
|
+
### Bun
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { server } from "./rpc/server";
|
|
116
|
+
|
|
117
|
+
Bun.serve({
|
|
118
|
+
port: 3000,
|
|
119
|
+
async fetch(req) {
|
|
120
|
+
const url = new URL(req.url);
|
|
121
|
+
if (req.method === "POST" && url.pathname === "/rpc") {
|
|
122
|
+
return server.handle(req);
|
|
123
|
+
}
|
|
124
|
+
return new Response("Not found", { status: 404 });
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Next.js Route Handler
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
export async function POST(req: Request) {
|
|
133
|
+
return server.handle(req);
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Cloudflare Workers / standard Fetch handler
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
export default {
|
|
141
|
+
async fetch(req: Request) {
|
|
142
|
+
const url = new URL(req.url);
|
|
143
|
+
if (req.method === "POST" && url.pathname === "/rpc") {
|
|
144
|
+
return server.handle(req);
|
|
145
|
+
}
|
|
146
|
+
return new Response("Not found", { status: 404 });
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### 4) Create the RPC client
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import { LixnetClient } from "lixnet";
|
|
155
|
+
|
|
156
|
+
type Events = {
|
|
157
|
+
greet: (input: { name: string }) => Promise<string>;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const client = new LixnetClient<Events>({ rpcUrl: "/api/rpc" });
|
|
161
|
+
|
|
162
|
+
const message = await client.call("greet", { name: "World" });
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
If you need auth/cookies, pass `fetch` options:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
await client.call(
|
|
169
|
+
"greet",
|
|
170
|
+
{ name: "World" },
|
|
171
|
+
{ credentials: "include", headers: { Authorization: "Bearer ..." } },
|
|
172
|
+
);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Server: `LixnetServer`
|
|
176
|
+
|
|
177
|
+
### Registering events
|
|
178
|
+
|
|
179
|
+
`server.on(...)` accepts either one registration object or an array.
|
|
180
|
+
|
|
181
|
+
- **`event`**: string key matching your `Events` type
|
|
182
|
+
- **`schema`** (optional): Zod schema to validate `input`
|
|
183
|
+
- **`handler`**: receives validated input plus `{ request, response }` injections
|
|
184
|
+
|
|
185
|
+
Example registering multiple events:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
server.on([
|
|
189
|
+
{
|
|
190
|
+
event: "greet",
|
|
191
|
+
schema: z.object({ name: z.string() }),
|
|
192
|
+
handler: async ({ name }) => `Hello, ${name}!`,
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
event: "health",
|
|
196
|
+
handler: async () => ({ ok: true as const }),
|
|
197
|
+
},
|
|
198
|
+
]);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Handling requests
|
|
202
|
+
|
|
203
|
+
`server.handle(request)` expects the request body to be JSON:
|
|
204
|
+
|
|
205
|
+
```json
|
|
206
|
+
{ "event": "someEvent", "input": { "...": "..." } }
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Responses are JSON and shaped as either:
|
|
210
|
+
|
|
211
|
+
```json
|
|
212
|
+
{ "data": "..." }
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
or
|
|
216
|
+
|
|
217
|
+
```json
|
|
218
|
+
{ "error": "..." }
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Server configuration
|
|
222
|
+
|
|
223
|
+
`new LixnetServer({ ... })` supports:
|
|
224
|
+
|
|
225
|
+
- **`defaultHeaders`**: headers included on every response (unless deleted later)
|
|
226
|
+
- **`formatter`**: customize how `LixnetResponse` becomes a `Response`
|
|
227
|
+
- **`logger`** + **`debugLog`**: currently used for invalid JSON logging
|
|
228
|
+
|
|
229
|
+
Example:
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
const server = new LixnetServer<Events>({
|
|
233
|
+
debugLog: true,
|
|
234
|
+
defaultHeaders: {
|
|
235
|
+
"access-control-allow-origin": "*",
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Client: `LixnetClient`
|
|
241
|
+
|
|
242
|
+
`client.call(event, input, options?)`:
|
|
243
|
+
|
|
244
|
+
- sends a `POST` with `{ event, input }`
|
|
245
|
+
- throws `Error(...)` when the response contains `{ error }`
|
|
246
|
+
- otherwise returns `{ data }`’s value
|
|
247
|
+
|
|
248
|
+
### Client creation
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
const client = new LixnetClient<Events>({
|
|
252
|
+
rpcUrl: "https://api.example.com/rpc",
|
|
253
|
+
});
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Handlers: `request` and `response`
|
|
257
|
+
|
|
258
|
+
Handler signature is:
|
|
259
|
+
|
|
260
|
+
- your validated input fields
|
|
261
|
+
- plus:
|
|
262
|
+
- `request`: a `Request` enhanced with `request.cookies()` and `request.headers()`
|
|
263
|
+
- `response`: a `LixnetResponse` you can use to stage headers/cookies/status
|
|
264
|
+
|
|
265
|
+
Common operations:
|
|
266
|
+
|
|
267
|
+
- **Set status**: `response.code(201)`
|
|
268
|
+
- **Set header**: `response.header("X-Foo", "bar")`
|
|
269
|
+
- **Delete header**: `response.deleteHeader("x-foo")`
|
|
270
|
+
- **Set cookie**: `response.cookie("session", "abc", { httpOnly: true, path: "/" })`
|
|
271
|
+
- **Delete cookie**: `response.deleteCookie("session", { path: "/" })`
|
|
272
|
+
|
|
273
|
+
`request.cookies()` / `request.headers()` also support “read your writes” inside the handler because they merge staged response mutations.
|
|
274
|
+
|
|
275
|
+
## WebSockets: `LixnetPeer`
|
|
276
|
+
|
|
277
|
+
`LixnetPeer` is a typed event dispatcher for WebSockets with:
|
|
278
|
+
|
|
279
|
+
- **fire-and-forget events**
|
|
280
|
+
- **optional callbacks** (request/response style over WS)
|
|
281
|
+
- **optional chunking** for large payloads (see contributor docs for protocol details)
|
|
282
|
+
|
|
283
|
+
Minimal sketch:
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
import { LixnetPeer } from "lixnet";
|
|
287
|
+
|
|
288
|
+
type ClientToServer = {
|
|
289
|
+
ping: (input: { t: number }) => Promise<{ ok: true }>;
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
type ServerToClient = {
|
|
293
|
+
notify: (input: { message: string }) => void;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const peer = new LixnetPeer<ServerToClient, ClientToServer>();
|
|
297
|
+
|
|
298
|
+
peer.on("ping", async ({ t, socket }) => {
|
|
299
|
+
return { ok: true };
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// In your WS message handler:
|
|
303
|
+
// peer.handle({ data: event.data, socket: ws });
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### WebSocket setup (typical pattern)
|
|
307
|
+
|
|
308
|
+
```ts
|
|
309
|
+
const peer = new LixnetPeer<ServerToClient, ClientToServer>();
|
|
310
|
+
peer.setSocket(ws);
|
|
311
|
+
|
|
312
|
+
ws.addEventListener("message", (event) => {
|
|
313
|
+
peer.handle({ data: event.data, socket: ws });
|
|
314
|
+
});
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### Callbacks (request/response over WS)
|
|
318
|
+
|
|
319
|
+
```ts
|
|
320
|
+
await peer.call(
|
|
321
|
+
"ping",
|
|
322
|
+
{ t: Date.now() },
|
|
323
|
+
{
|
|
324
|
+
callback: (result) => {
|
|
325
|
+
// result is typed as Awaited<ReturnType<ClientToServer["ping"]>>
|
|
326
|
+
console.log(result.ok);
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
);
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Large payloads (chunking)
|
|
333
|
+
|
|
334
|
+
If messages can exceed your runtime’s WS frame limits, enable chunking:
|
|
335
|
+
|
|
336
|
+
```ts
|
|
337
|
+
peer.setTransmissionLimit(64_000); // bytes-ish (string length)
|
|
338
|
+
peer.setTransmissionChunksLimit(20); // currently stored, not enforced by sender
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## API reference
|
|
342
|
+
|
|
343
|
+
### Exports
|
|
344
|
+
|
|
345
|
+
From `src/exports.ts`:
|
|
346
|
+
|
|
347
|
+
- **`LixnetServer`**
|
|
348
|
+
- **`LixnetClient`**
|
|
349
|
+
- **`LixnetPeer`**
|
|
350
|
+
- **`LixnetResponse`**
|
|
351
|
+
|
|
352
|
+
Types:
|
|
353
|
+
|
|
354
|
+
- **`LXN_ServerClient_EventType`**
|
|
355
|
+
- **`LXNServerHandler`**
|
|
356
|
+
- **`FunctionInput`**
|
|
357
|
+
- **`LXN_ServerClient_Request`** (alias of `LixnetRequest`)
|
|
358
|
+
- **`LixnetRequest`**, **`LixnetCookies`**, **`LixnetHeaders`**
|
|
359
|
+
|
|
360
|
+
## Contributor docs
|
|
361
|
+
|
|
362
|
+
- **Implementation + extension guide**: [`FOR_AGENTS.md`](FOR_AGENTS.md)
|
|
363
|
+
|
|
364
|
+
## License
|
|
365
|
+
|
|
366
|
+
MIT
|
package/dist/exports.js
CHANGED
package/dist/lib/peer.js
CHANGED
|
@@ -2,6 +2,7 @@ export default function (defaultHeaders) {
|
|
|
2
2
|
function formatter() {
|
|
3
3
|
const headers = new Headers({
|
|
4
4
|
"Content-Type": "application/json",
|
|
5
|
+
"X-Server": "Lixnet",
|
|
5
6
|
...defaultHeaders,
|
|
6
7
|
});
|
|
7
8
|
for (const [headerName, headerValue] of Object.entries(this.responseHeaders)) {
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type CookieBaseOptions = {
|
|
2
2
|
domain?: string;
|
|
3
3
|
path?: string;
|
|
4
|
-
maxAge?: number;
|
|
5
4
|
httpOnly?: boolean;
|
|
6
5
|
secure?: boolean;
|
|
7
6
|
sameSite?: 'lax' | 'strict' | 'none';
|
|
8
7
|
};
|
|
9
|
-
export type
|
|
10
|
-
|
|
11
|
-
path?: string;
|
|
12
|
-
httpOnly?: boolean;
|
|
13
|
-
secure?: boolean;
|
|
14
|
-
sameSite?: 'lax' | 'strict' | 'none';
|
|
8
|
+
export type CookieFullOptions = CookieBaseOptions & {
|
|
9
|
+
maxAge?: number;
|
|
15
10
|
};
|
|
16
11
|
export declare class LixnetResponse {
|
|
17
12
|
responseData: any;
|
|
@@ -22,7 +17,7 @@ export declare class LixnetResponse {
|
|
|
22
17
|
responseCookies: Record<string, {
|
|
23
18
|
type: "value" | "delete";
|
|
24
19
|
value?: string;
|
|
25
|
-
options?:
|
|
20
|
+
options?: CookieFullOptions;
|
|
26
21
|
}>;
|
|
27
22
|
constructor({ formatter }: {
|
|
28
23
|
formatter: (this: LixnetResponse) => Response;
|
|
@@ -33,8 +28,8 @@ export declare class LixnetResponse {
|
|
|
33
28
|
error(error: string): void;
|
|
34
29
|
code(code: number): void;
|
|
35
30
|
headers(headers: Record<string, string>): void;
|
|
36
|
-
cookie(cookieName: string, cookieValue: string, cookieOptions?:
|
|
37
|
-
deleteCookie(cookieName: string, options?:
|
|
31
|
+
cookie(cookieName: string, cookieValue: string, cookieOptions?: CookieFullOptions): void;
|
|
32
|
+
deleteCookie(cookieName: string, options?: CookieBaseOptions): void;
|
|
38
33
|
format: (this: LixnetResponse) => Response;
|
|
39
34
|
setFormatter(formatter: (this: LixnetResponse) => Response): void;
|
|
40
35
|
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lixnet",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"author": "nitlix",
|
|
3
|
+
"version": "1.3.10",
|
|
4
|
+
"author": "nitlix-hq",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/
|
|
8
|
+
"url": "https://github.com/nitlix-hq/Lixnet"
|
|
9
9
|
},
|
|
10
|
-
"homepage": "https://github.com/
|
|
10
|
+
"homepage": "https://github.com/nitlix-hq/Lixnet",
|
|
11
11
|
"description": "RPC over HTTP and WebSockets, simplified.",
|
|
12
12
|
"module": "./dist/exports.js",
|
|
13
13
|
"main": "./dist/exports.js",
|
|
14
|
+
"types": "./dist/exports.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/exports.d.ts",
|
|
18
|
+
"default": "./dist/exports.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
14
24
|
"type": "module",
|
|
15
25
|
"devDependencies": {
|
|
16
26
|
"@types/bun": "latest"
|
package/.gitattributes
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export default function jsonFormatter() {
|
|
2
|
-
const headers = new Headers({
|
|
3
|
-
"Content-Type": "application/json",
|
|
4
|
-
});
|
|
5
|
-
for (const [headerName, headerValue] of Object.entries(this.responseHeaders)) {
|
|
6
|
-
headers.set(headerName, headerValue);
|
|
7
|
-
}
|
|
8
|
-
for (const [cookieName, cookie] of Object.entries(this.responseCookies)) {
|
|
9
|
-
if (cookie.type === "value") {
|
|
10
|
-
const opts = cookie.options ?? {};
|
|
11
|
-
headers.set('Set-Cookie', `${cookieName}=${cookie.value}; ${Object.entries(opts).map(([key, value]) => `${key}=${value}`).join('; ')}`);
|
|
12
|
-
}
|
|
13
|
-
else if (cookie.type === "delete") {
|
|
14
|
-
headers.set('Set-Cookie', `${cookieName}=; Max-Age=0; Path=/; HttpOnly; Secure; SameSite=Strict`);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
throw new Error(`Unknown cookie type: ${cookie.type}`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return Response.json({
|
|
21
|
-
data: this.responseData,
|
|
22
|
-
}, {
|
|
23
|
-
status: 200,
|
|
24
|
-
headers: headers,
|
|
25
|
-
});
|
|
26
|
-
}
|