@statewalker/webrun-rpc-http 0.1.4 → 0.1.6
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 +120 -188
- package/dist/index.js +1 -1
- package/package.json +8 -4
- package/src/new-rpc-server.ts +1 -1
package/README.md
CHANGED
|
@@ -1,69 +1,53 @@
|
|
|
1
1
|
# @statewalker/webrun-rpc-http
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
`(Request) ⇒ Response` handler; call them from anywhere with `fetch`.
|
|
3
|
+
## What it is
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
handler that answers `fetch()` — over the wire, in the same tab, inside a
|
|
11
|
-
ServiceWorker, across a MessagePort bridge. What they don't give you is a
|
|
12
|
-
layer above that: a way to take a plain service object and turn its methods
|
|
13
|
-
into addressable endpoints.
|
|
5
|
+
HTTP-based service RPC. `newRpcServer` turns a map of plain service objects
|
|
6
|
+
into one standard `(Request) => Promise<Response>` handler, with an endpoint
|
|
7
|
+
per method. `newRpcClient` reads the server's service descriptor and returns
|
|
8
|
+
proxy objects whose method calls go out through `fetch`.
|
|
14
9
|
|
|
15
|
-
|
|
16
|
-
functions plus a handful of types:
|
|
17
|
-
|
|
18
|
-
- `newRpcServer(services, {path?}) ⇒ (Request) ⇒ Response` — one handler
|
|
19
|
-
that routes `GET /`, `GET /{service}`, and
|
|
20
|
-
`GET|POST /{service}/{method}` into object method calls.
|
|
21
|
-
- `newRpcClient({baseUrl, fetch?})` — lazily fetches the service
|
|
22
|
-
descriptor and returns typed proxies whose method calls round-trip
|
|
23
|
-
through `fetch`.
|
|
10
|
+
## Why it exists
|
|
24
11
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
12
|
+
A `(Request) => Response` handler can be reached in many ways: over the
|
|
13
|
+
network, through a ServiceWorker (`@statewalker/webrun-http-browser`), over a
|
|
14
|
+
`Duplex` (`@statewalker/webrun-http-streams`), or by calling it directly. What
|
|
15
|
+
a handler does not give you is a way to expose an object's methods as
|
|
16
|
+
addressable endpoints and call them back as methods. This package is that
|
|
17
|
+
layer, and because the server is just a handler and the client just a `fetch`,
|
|
18
|
+
the same RPC code runs over any of those transports:
|
|
28
19
|
|
|
29
20
|
| Transport | How |
|
|
30
21
|
| --- | --- |
|
|
31
|
-
| Real HTTP
|
|
32
|
-
|
|
|
33
|
-
| In
|
|
34
|
-
| A MessagePort
|
|
35
|
-
| A WebSocket | Same, with `@statewalker/webrun-streams-ws` supplying the `Duplex`. |
|
|
36
|
-
| Deno / Cloudflare Workers / Node's built-in HTTP | The handler is `(Request) ⇒ Response` — drop in as-is. |
|
|
37
|
-
|
|
38
|
-
## Install
|
|
39
|
-
|
|
40
|
-
```sh
|
|
41
|
-
npm install @statewalker/webrun-rpc-http
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
One runtime dependency, [`@statewalker/webrun-streams`](../webrun-streams) (for
|
|
45
|
-
error serialisation); no peer dependencies. ESM only (`"type": "module"`). The
|
|
46
|
-
server is a plain `(Request) => Response` handler and the client takes an
|
|
47
|
-
injectable `fetch`, so it runs in browsers, Node 18+, Deno, Bun and Workers.
|
|
22
|
+
| Real HTTP | Default `fetch` is `globalThis.fetch`; serve the handler from Deno, Bun, Cloudflare Workers, or any server that takes a `Request`. |
|
|
23
|
+
| A ServiceWorker | Register the handler with `@statewalker/webrun-http-browser`; the worker intercepts ordinary `fetch` calls. |
|
|
24
|
+
| In process | `fetch: (request) => handler(request)`; no network. |
|
|
25
|
+
| A `Duplex` (`MessagePort`, WebSocket, WebRTC…) | `serveFetchOverDuplex(handler)` on one side, `fetch: (r) => fetchOverDuplex(call, r)` on the other, both from `@statewalker/webrun-http-streams`. |
|
|
48
26
|
|
|
49
27
|
## How to use
|
|
50
28
|
|
|
51
29
|
```sh
|
|
52
|
-
|
|
30
|
+
pnpm add @statewalker/webrun-rpc-http
|
|
53
31
|
```
|
|
54
32
|
|
|
55
|
-
|
|
33
|
+
- **Peer dependencies:** none.
|
|
34
|
+
- **Runtime dependency:** `@statewalker/webrun-streams`, for error
|
|
35
|
+
serialisation.
|
|
36
|
+
- **Entry point:** one, `.`. ESM only: `dist/index.js` with `dist/index.d.ts`.
|
|
37
|
+
The `source` export condition points at `src/index.ts`.
|
|
38
|
+
- **Environment:** anything with `Request`, `Response`, `FormData`, `Blob` and
|
|
39
|
+
`URL`: browsers, workers, service workers, Node, Deno, Bun.
|
|
56
40
|
|
|
57
41
|
| Export | Purpose |
|
|
58
42
|
| --- | --- |
|
|
59
|
-
| `newRpcServer(services,
|
|
60
|
-
| `
|
|
61
|
-
| `
|
|
62
|
-
| `
|
|
63
|
-
| `
|
|
64
|
-
| `
|
|
65
|
-
| `
|
|
66
|
-
| `
|
|
43
|
+
| `newRpcServer(services, { path? })` | Build a `(Request) => Promise<Response>` handler from `Record<string, object>`. |
|
|
44
|
+
| `NewRpcServerOptions` | `{ path?: string }`: URL prefix to mount under; a trailing slash is stripped; empty (default) mounts at the root. |
|
|
45
|
+
| `newRpcClient({ baseUrl, fetch? })` | Build a client: `{ loadService<T>(name) }`. |
|
|
46
|
+
| `NewRpcClientOptions` | `{ baseUrl: string; fetch?: (request: Request) => Promise<Response> }`. `baseUrl` has no trailing slash. |
|
|
47
|
+
| `RpcClient` | The client's type. |
|
|
48
|
+
| `RpcMethod` | `(params: Json, body?: Blob) => Promise<Blob \| Json>`, the shape of every exposed method. |
|
|
49
|
+
| `Json`, `JsonObject` | JSON value types. |
|
|
50
|
+
| `getInstanceMethods(instance)` | Every function-valued property of `instance` and its prototype chain, up to but not including `Object.prototype`, minus `constructor`. Used by `newRpcServer`. |
|
|
67
51
|
|
|
68
52
|
## Examples
|
|
69
53
|
|
|
@@ -83,21 +67,16 @@ class MathService {
|
|
|
83
67
|
|
|
84
68
|
const handler = newRpcServer({ math: new MathService() });
|
|
85
69
|
|
|
86
|
-
|
|
87
|
-
export default { fetch: handler }; // Deno / Bun / Cloudflare Workers
|
|
88
|
-
// or: Bun.serve({ fetch: handler, port: 8080 });
|
|
89
|
-
// or: http.createServer(/* adapt */) // Node's built-in http
|
|
70
|
+
export default { fetch: handler }; // Deno / Bun / Cloudflare Workers
|
|
90
71
|
```
|
|
91
72
|
|
|
92
|
-
Out of the box the handler answers:
|
|
93
|
-
|
|
94
73
|
| Request | Response |
|
|
95
74
|
| --- | --- |
|
|
96
|
-
| `GET /` | `{ "math": ["add", "bytes"] }
|
|
97
|
-
| `GET /math` | `["add", "bytes"]`
|
|
98
|
-
| `POST /math/add
|
|
99
|
-
| `GET /math/add?a=2&b=3` | `{ "type": "json", "result": "23" }
|
|
100
|
-
| `POST /math/bytes`
|
|
75
|
+
| `GET /` | `{ "math": ["add", "bytes"] }`, the service descriptor |
|
|
76
|
+
| `GET /math` | `["add", "bytes"]` (an unknown service gives `[]`) |
|
|
77
|
+
| `POST /math/add`, multipart with a `params` JSON field | `{ "type": "json", "result": 5 }` |
|
|
78
|
+
| `GET /math/add?a=2&b=3` | `{ "type": "json", "result": "23" }`: query values are strings, so `add` concatenates |
|
|
79
|
+
| `POST /math/bytes` | `application/octet-stream` body |
|
|
101
80
|
|
|
102
81
|
### Call a service
|
|
103
82
|
|
|
@@ -107,20 +86,18 @@ import { newRpcClient } from "@statewalker/webrun-rpc-http";
|
|
|
107
86
|
const client = newRpcClient({ baseUrl: "https://api.example.com/rpc" });
|
|
108
87
|
const math = await client.loadService<MathService>("math");
|
|
109
88
|
|
|
110
|
-
await math.add({ a: 2, b: 3 });
|
|
89
|
+
await math.add({ a: 2, b: 3 }); // 5
|
|
111
90
|
const blob = (await math.bytes({ count: 16 })) as Blob;
|
|
112
91
|
```
|
|
113
92
|
|
|
114
|
-
The descriptor at `baseUrl
|
|
115
|
-
call, and cached for
|
|
116
|
-
|
|
117
|
-
|
|
93
|
+
The descriptor at `GET {baseUrl}` is fetched once, on the first `loadService`
|
|
94
|
+
call, and cached for the client's lifetime; create a new client to refresh it.
|
|
95
|
+
Every method call is one `POST` with no connection held open.
|
|
96
|
+
`loadService` rejects with `Service <name> not found` for a name the descriptor
|
|
97
|
+
does not list, and with `Failed to load services descriptor: <status> <statusText>`
|
|
98
|
+
when the descriptor request fails.
|
|
118
99
|
|
|
119
|
-
### Wire client
|
|
120
|
-
|
|
121
|
-
Pass any `(Request) ⇒ Promise<Response>` as the `fetch` option. This is
|
|
122
|
-
how you run the client against an in-process server — unit tests,
|
|
123
|
-
webrun-http-browser SWs, a MessagePort bridge, a WebSocket bridge:
|
|
100
|
+
### Wire client to server in process
|
|
124
101
|
|
|
125
102
|
```ts
|
|
126
103
|
const handler = newRpcServer({ math: new MathService() });
|
|
@@ -129,17 +106,17 @@ const client = newRpcClient({
|
|
|
129
106
|
fetch: (request) => handler(request),
|
|
130
107
|
});
|
|
131
108
|
const math = await client.loadService<MathService>("math");
|
|
132
|
-
await math.add({ a: 1, b: 2 }); // 3
|
|
109
|
+
await math.add({ a: 1, b: 2 }); // 3, no network
|
|
133
110
|
```
|
|
134
111
|
|
|
135
|
-
###
|
|
112
|
+
### Serve from a browser ServiceWorker
|
|
136
113
|
|
|
137
|
-
|
|
138
|
-
|
|
114
|
+
The server must be mounted at the path the handler is registered under, and the
|
|
115
|
+
client's `baseUrl` must not end with `/`:
|
|
139
116
|
|
|
140
117
|
```ts
|
|
141
118
|
import { SwHttpAdapter } from "@statewalker/webrun-http-browser/sw";
|
|
142
|
-
import { newRpcServer } from "@statewalker/webrun-rpc-http";
|
|
119
|
+
import { newRpcClient, newRpcServer } from "@statewalker/webrun-rpc-http";
|
|
143
120
|
|
|
144
121
|
const adapter = new SwHttpAdapter({
|
|
145
122
|
key: "api",
|
|
@@ -147,13 +124,11 @@ const adapter = new SwHttpAdapter({
|
|
|
147
124
|
});
|
|
148
125
|
await adapter.start();
|
|
149
126
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
);
|
|
127
|
+
let rpc: (request: Request) => Promise<Response> = async () => new Response(null, { status: 503 });
|
|
128
|
+
const { baseUrl } = await adapter.register("api/rpc/", (request) => rpc(request));
|
|
129
|
+
rpc = newRpcServer({ math: new MathService() }, { path: new URL(baseUrl).pathname });
|
|
154
130
|
|
|
155
|
-
|
|
156
|
-
const client = newRpcClient({ baseUrl });
|
|
131
|
+
const client = newRpcClient({ baseUrl: baseUrl.replace(/\/$/, "") });
|
|
157
132
|
const math = await client.loadService<MathService>("math");
|
|
158
133
|
```
|
|
159
134
|
|
|
@@ -162,139 +137,96 @@ const math = await client.loadService<MathService>("math");
|
|
|
162
137
|
```ts
|
|
163
138
|
const handler = newRpcServer(services, { path: "/api/v1" });
|
|
164
139
|
|
|
165
|
-
// GET /api/v1/
|
|
166
|
-
// POST /api/v1/math/add
|
|
167
|
-
// GET /api/v1/
|
|
140
|
+
// GET /api/v1/ -> descriptor
|
|
141
|
+
// POST /api/v1/math/add -> call
|
|
142
|
+
// GET /api/v1/files/read/foo/bar -> call with params.$path === "foo/bar"
|
|
168
143
|
```
|
|
169
144
|
|
|
170
|
-
|
|
171
|
-
shape as any other error.
|
|
172
|
-
|
|
173
|
-
### Errors
|
|
145
|
+
## Internals
|
|
174
146
|
|
|
175
|
-
|
|
176
|
-
returns a JSON object:
|
|
147
|
+
### Wire format
|
|
177
148
|
|
|
178
|
-
```
|
|
179
|
-
|
|
149
|
+
```
|
|
150
|
+
client server
|
|
151
|
+
math.add({a:2,b:3}) ──POST {baseUrl}/math/add──► newRpcServer
|
|
152
|
+
multipart/form-data │ params = JSON.parse(form.params)
|
|
153
|
+
params = '{"a":2,"b":3}' │ params.$path = tail after /math/add/
|
|
154
|
+
body = Blob (optional) ▼
|
|
155
|
+
◄── 200 application/json ── { "type": "json", "result": 5 }
|
|
156
|
+
◄── 200 octet-stream ────── raw bytes, when the method returns a Blob
|
|
157
|
+
◄── 200 application/json ── { "type": "error", message, stack, ... }
|
|
180
158
|
```
|
|
181
159
|
|
|
182
|
-
|
|
183
|
-
| --- | --- | --- |
|
|
184
|
-
| Method body throws | 200 | `{ "type": "error", … }` — the call reached the method. |
|
|
185
|
-
| Method doesn't exist | 500 | Same shape — routing failure. |
|
|
186
|
-
| Unknown path | 404 | Same shape. |
|
|
187
|
-
| `response.ok === false` with non-JSON body | — | Client throws a generic `RPC call failed: status text`. |
|
|
188
|
-
|
|
189
|
-
On the client, every serialized error is rehydrated into an `Error`
|
|
190
|
-
instance via
|
|
191
|
-
[`@statewalker/webrun-streams`](../webrun-streams)'s `deserializeError`
|
|
192
|
-
— preserving `message`, `stack`, and any custom fields the server
|
|
193
|
-
attached to a thrown `Error` subclass.
|
|
194
|
-
|
|
195
|
-
## Internals
|
|
160
|
+
Call encoding:
|
|
196
161
|
|
|
197
|
-
|
|
162
|
+
| Request | Arguments |
|
|
163
|
+
| --- | --- |
|
|
164
|
+
| `POST /svc/method`, `multipart/form-data` | `params` field (JSON) and optional `body` field (`Blob`). |
|
|
165
|
+
| `POST /svc/method`, any other content type | `params` is `{}`; the raw body becomes the `body` Blob. |
|
|
166
|
+
| `GET /svc/method?k=v` | Query string as `params`. Dot-separated keys nest: `?a.b=c&a.d=e` gives `{ a: { b: "c", d: "e" } }`. Values stay strings; repeated keys overwrite. |
|
|
198
167
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
`
|
|
202
|
-
descriptor is built from its keys. That means:
|
|
168
|
+
The URL tail after `/svc/method/` is set as `params.$path` (an empty string when
|
|
169
|
+
there is none), so a method can serve REST-style paths such as
|
|
170
|
+
`GET /files/read/some/deep/path`. It is set only when `params` is an object.
|
|
203
171
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
exposed).
|
|
208
|
-
- Inherited methods work: `class Child extends Parent` instance exposes
|
|
209
|
-
every method from both.
|
|
172
|
+
The descriptor is `Record<string, string[]>`: names only, no signatures. The
|
|
173
|
+
client builds its proxies from the names, and `loadService<T>` restores typing
|
|
174
|
+
through a type argument; there is no code generation.
|
|
210
175
|
|
|
211
|
-
|
|
212
|
-
signatures. Clients build proxy objects from the name list alone.
|
|
176
|
+
### Errors come back as JSON
|
|
213
177
|
|
|
214
|
-
|
|
178
|
+
Every error is a JSON object `{ "type": "error", "message", "stack", ...custom fields }`:
|
|
215
179
|
|
|
216
|
-
|
|
|
180
|
+
| Cause | HTTP status |
|
|
217
181
|
| --- | --- |
|
|
218
|
-
|
|
|
219
|
-
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
If your method expects numbers, POST JSON instead of GET'ing a querystring.
|
|
223
|
-
|
|
224
|
-
### Wire format — sub-path injection
|
|
225
|
-
|
|
226
|
-
The URL tail after `/svc/method/` is injected into `params.$path`:
|
|
182
|
+
| The method throws | `200`; the call reached the method |
|
|
183
|
+
| Unknown method: `Method <m> not found in service <s>` | `500` |
|
|
184
|
+
| Path outside the prefix, or a method other than `GET`/`POST`: `Not found` | `404` |
|
|
185
|
+
| Non-JSON error response | client throws `RPC call failed: <status> <statusText>` |
|
|
227
186
|
|
|
228
|
-
|
|
229
|
-
-
|
|
230
|
-
|
|
187
|
+
The client rehydrates `type: "error"` bodies with `deserializeError` from
|
|
188
|
+
`@statewalker/webrun-streams`, so `message`, `stack` and custom fields of a
|
|
189
|
+
thrown `Error` subclass survive; the class does not. Error bodies include the
|
|
190
|
+
server-side `stack`, so do not expose a handler to callers who should not see
|
|
191
|
+
it.
|
|
231
192
|
|
|
232
|
-
###
|
|
193
|
+
### Constraints
|
|
233
194
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
195
|
+
- **Methods return once.** A method returns one `Json` value or one `Blob`; no
|
|
196
|
+
streaming. For streaming responses use `fetchOverDuplex` /
|
|
197
|
+
`serveFetchOverDuplex` from `@statewalker/webrun-http-streams` directly.
|
|
198
|
+
- **Only the prototype chain below `Object.prototype` is exposed.**
|
|
199
|
+
`toString`, `hasOwnProperty` and the like are never endpoints. Methods of
|
|
200
|
+
every ancestor class are.
|
|
201
|
+
- **`baseUrl` takes no trailing slash.** The client builds
|
|
202
|
+
`${baseUrl}/${service}/${method}`; a trailing slash doubles it and the call
|
|
203
|
+
fails with `Not found`.
|
|
204
|
+
- **The prefix matches whole path segments.** With `path: "/api/v1"`, the
|
|
205
|
+
handler serves `/api/v1` and everything under `/api/v1/`; `/api/v1x/math`
|
|
206
|
+
is outside the prefix and gets `404 Not found`.
|
|
207
|
+
- **No content negotiation.** JSON or multipart in, JSON or binary out.
|
|
240
208
|
|
|
241
209
|
### Design notes
|
|
242
210
|
|
|
243
|
-
- **Factory, not
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
- **Cached descriptor, lazy load.** The first `loadService` call fires
|
|
248
|
-
the `GET baseUrl/` request; every subsequent call returns the same
|
|
249
|
-
proxy object. Restart the client (`newRpcClient(...)`) to refresh.
|
|
250
|
-
- **Errors go through `@statewalker/webrun-streams`.** One
|
|
251
|
-
(de)serialization format shared across the webrun stack; no duplicate
|
|
252
|
-
`errors.ts` file.
|
|
253
|
-
- **No client-side type generation.** The client returns
|
|
254
|
-
`Record<string, RpcMethod>` by default; pass a concrete interface with
|
|
255
|
-
`loadService<MyService>("name")` to recover typing. TypeScript
|
|
256
|
-
structural compatibility does the rest.
|
|
257
|
-
- **The `$path` trick is optional.** If your method doesn't read
|
|
258
|
-
`params.$path`, nothing changes. It's there for REST-style handlers
|
|
259
|
-
that care about the tail.
|
|
260
|
-
|
|
261
|
-
### Constraints
|
|
262
|
-
|
|
263
|
-
- **One prototype chain.** `getInstanceMethods` stops before
|
|
264
|
-
`Object.prototype`. Methods defined on `Object.prototype` are
|
|
265
|
-
deliberately excluded — you can't accidentally expose `toString`.
|
|
266
|
-
- **No streaming.** A method returns once, with one `Json` or `Blob`.
|
|
267
|
-
Use `@statewalker/webrun-http-streams` directly (`fetchOverDuplex` /
|
|
268
|
-
`serveFetchOverDuplex`) for streaming responses.
|
|
269
|
-
- **Path prefix without trailing slash.** `path: "/api"` is right;
|
|
270
|
-
`path: "/api/"` is normalized (trailing slash stripped).
|
|
271
|
-
- **URLSearchParams are strings.** `?a=1` → `params.a === "1"`. POST
|
|
272
|
-
JSON-encoded params whenever the argument type matters.
|
|
273
|
-
- **No content negotiation.** JSON in, JSON or binary out — nothing more.
|
|
274
|
-
- **`baseUrl` without trailing slash.** The client concatenates
|
|
275
|
-
`${baseUrl}/${service}/${method}`; trailing slash would double.
|
|
211
|
+
- **Factory functions, not classes.** Behaviour is configured through options;
|
|
212
|
+
there is no class surface to subclass.
|
|
213
|
+
- **Errors use `@statewalker/webrun-streams`.** One serialisation format shared
|
|
214
|
+
with the rest of the webrun packages.
|
|
276
215
|
|
|
277
216
|
### Dependencies
|
|
278
217
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
(`serializeError` / `deserializeError`). Workspace-local.
|
|
283
|
-
|
|
284
|
-
Otherwise zero runtime deps: platform builtins only (`Request`,
|
|
285
|
-
`Response`, `FormData`, `Blob`, `URL`, `URLSearchParams`).
|
|
286
|
-
|
|
287
|
-
Dev: TypeScript, vitest, rolldown, rimraf, `@types/node` (catalog
|
|
288
|
-
versions from the monorepo root).
|
|
218
|
+
One runtime dependency, `@statewalker/webrun-streams`, for `serializeError` /
|
|
219
|
+
`deserializeError`. Everything else is platform API (`Request`, `Response`,
|
|
220
|
+
`FormData`, `Blob`, `URL`, `URLSearchParams`).
|
|
289
221
|
|
|
290
|
-
|
|
222
|
+
### Scripts
|
|
291
223
|
|
|
292
224
|
```sh
|
|
293
|
-
pnpm test
|
|
294
|
-
pnpm run build
|
|
295
|
-
pnpm lint
|
|
225
|
+
pnpm test # vitest run
|
|
226
|
+
pnpm run build # rolldown + tsc --emitDeclarationOnly
|
|
227
|
+
pnpm run lint # biome check src tests
|
|
296
228
|
```
|
|
297
229
|
|
|
298
230
|
## License
|
|
299
231
|
|
|
300
|
-
MIT
|
|
232
|
+
MIT
|
package/dist/index.js
CHANGED
|
@@ -118,7 +118,7 @@ function buildServiceIndex(services) {
|
|
|
118
118
|
}
|
|
119
119
|
function splitRequestPath(request, prefix) {
|
|
120
120
|
const pathname = new URL(request.url).pathname;
|
|
121
|
-
if (prefix && !pathname.startsWith(prefix)) return null;
|
|
121
|
+
if (prefix && pathname !== prefix && !pathname.startsWith(`${prefix}/`)) return null;
|
|
122
122
|
const [serviceName = "", methodName = "", ...tail] = pathname.substring(prefix.length + 1).split("/");
|
|
123
123
|
return {
|
|
124
124
|
serviceName: serviceName || void 0,
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/webrun-rpc-http",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "HTTP-based service RPC: expose object methods as a (Request) => Response handler and call them with fetch",
|
|
7
|
-
"
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/statewalker/webrun-wire/issues"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/statewalker/webrun-wire/tree/main/packages/webrun-rpc-http#readme",
|
|
8
11
|
"author": {
|
|
9
12
|
"name": "Mikhail Kotelnikov",
|
|
10
13
|
"email": "mikhail.kotelnikov@gmail.com"
|
|
@@ -12,7 +15,8 @@
|
|
|
12
15
|
"license": "MIT",
|
|
13
16
|
"repository": {
|
|
14
17
|
"type": "git",
|
|
15
|
-
"url": "git
|
|
18
|
+
"url": "git+https://github.com/statewalker/webrun-wire.git",
|
|
19
|
+
"directory": "packages/webrun-rpc-http"
|
|
16
20
|
},
|
|
17
21
|
"main": "./dist/index.js",
|
|
18
22
|
"module": "./dist/index.js",
|
|
@@ -29,7 +33,7 @@
|
|
|
29
33
|
"src"
|
|
30
34
|
],
|
|
31
35
|
"dependencies": {
|
|
32
|
-
"@statewalker/webrun-streams": "^0.2.
|
|
36
|
+
"@statewalker/webrun-streams": "^0.2.2"
|
|
33
37
|
},
|
|
34
38
|
"devDependencies": {
|
|
35
39
|
"@biomejs/biome": "^2.5.15",
|
package/src/new-rpc-server.ts
CHANGED
|
@@ -79,7 +79,7 @@ interface ParsedPath {
|
|
|
79
79
|
|
|
80
80
|
function splitRequestPath(request: Request, prefix: string): ParsedPath | null {
|
|
81
81
|
const pathname = new URL(request.url).pathname;
|
|
82
|
-
if (prefix && !pathname.startsWith(prefix)) return null;
|
|
82
|
+
if (prefix && pathname !== prefix && !pathname.startsWith(`${prefix}/`)) return null;
|
|
83
83
|
const rest = pathname.substring(prefix.length + 1);
|
|
84
84
|
const [serviceName = "", methodName = "", ...tail] = rest.split("/");
|
|
85
85
|
return {
|