@greendrake/rpc-server 0.1.0 → 0.3.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/README.md +1 -1
- package/package.json +3 -3
- package/src/dispatcher.ts +3 -3
- package/src/http.ts +1 -1
- package/src/types.ts +3 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ The server half of the [`@greendrake/rpc`](../rpc) wire format: one dispatch tab
|
|
|
8
8
|
bun add @greendrake/rpc-server
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
TypeScript source ships as-is (`src/main.ts` is the entry; no build step, no `.d.ts`). Extend `@greendrake/dev-config/tsconfig/
|
|
11
|
+
TypeScript source ships as-is (`src/main.ts` is the entry; no build step, no `.d.ts`). Extend `@greendrake/dev-config/tsconfig/node.json` with `"types": ["bun"]`, and `strict.json` over it if you like: this package checks itself under both, without the DOM lib, so neither it nor `@greendrake/rpc` — the client this serves, whose wire types it imports — asks anything of your options.
|
|
12
12
|
|
|
13
13
|
## A service, whole
|
|
14
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@greendrake/rpc-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "RPC server over JSON for bun: method dispatch with auth, nonce and Standard Schema argument validation, HTTP and WebSocket transports, connection registry and server push",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Eugene Greendrake <eugene@greendrake.nz>",
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
},
|
|
35
35
|
"prettier": "@greendrake/dev-config/prettier",
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@greendrake/rpc": "0.
|
|
37
|
+
"@greendrake/rpc": "0.3.0",
|
|
38
38
|
"@standard-schema/spec": "^1.1.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@greendrake/dev-config": "0.
|
|
41
|
+
"@greendrake/dev-config": "0.3.0",
|
|
42
42
|
"@types/bun": "^1.3.5",
|
|
43
43
|
"typescript": "6.0.3"
|
|
44
44
|
}
|
package/src/dispatcher.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface Dispatcher {
|
|
|
20
20
|
// Exposed because the transports resolve credentials of their own — a
|
|
21
21
|
// bearer header, a query parameter at the WebSocket handshake, an in-band
|
|
22
22
|
// auth frame — before there is a request to dispatch.
|
|
23
|
-
readonly auth
|
|
23
|
+
readonly auth: AuthResolver | undefined
|
|
24
24
|
dispatch(request: IncomingRequest, context: CallContext): Promise<RpcResponse>
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -41,10 +41,10 @@ export const createDispatcher = (methods: Methods, options: DispatcherOptions =
|
|
|
41
41
|
}
|
|
42
42
|
// Own properties only: a method named `toString` or `constructor`
|
|
43
43
|
// is not a method this service serves.
|
|
44
|
-
|
|
44
|
+
const def = Object.hasOwn(methods, request.method) ? methods[request.method] : undefined
|
|
45
|
+
if (def === undefined) {
|
|
45
46
|
return failure('UNKNOWN_METHOD')
|
|
46
47
|
}
|
|
47
|
-
const def = methods[request.method]
|
|
48
48
|
if (def.auth !== 'none') {
|
|
49
49
|
if (context.auth.userId === null) {
|
|
50
50
|
return failure('AUTH_REQUIRED')
|
package/src/http.ts
CHANGED
|
@@ -19,7 +19,7 @@ export const clientIp = <T>(request: Request, server: Server<T>): string => {
|
|
|
19
19
|
if (forwarded) {
|
|
20
20
|
// The left-most entry is the original client; the rest are the proxies
|
|
21
21
|
// it passed through.
|
|
22
|
-
return forwarded.
|
|
22
|
+
return forwarded.replace(/,.*/, '').trim()
|
|
23
23
|
}
|
|
24
24
|
return request.headers.get('x-real-ip') ?? server.requestIP(request)?.address ?? ''
|
|
25
25
|
}
|
package/src/types.ts
CHANGED
|
@@ -97,7 +97,9 @@ export type ClientMethods<M extends Methods> = {
|
|
|
97
97
|
// response with its request, and the frames the server pushes unasked.
|
|
98
98
|
export interface WsResponse extends RpcResponse {
|
|
99
99
|
type: 'response'
|
|
100
|
-
id
|
|
100
|
+
// Undefined answering a frame whose id could not be read — one that did
|
|
101
|
+
// not parse — and dropped from the wire by JSON, like an absent one.
|
|
102
|
+
id?: string | undefined
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
export interface EventFrame {
|