@statewalker/webrun-rpc-http 0.1.1 → 0.1.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 +15 -2
- package/dist/index.js +1 -25
- package/package.json +2 -2
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -31,10 +31,21 @@ transport you wire it to:
|
|
|
31
31
|
| Real HTTP over the network | Default: `fetch = globalThis.fetch`. |
|
|
32
32
|
| An in-browser ServiceWorker | `@statewalker/webrun-http-browser` — the SW intercepts the standard `fetch` call with no special wiring. |
|
|
33
33
|
| In-process tests | Pass `fetch: (request) => handler(request)` — no network at all. |
|
|
34
|
-
| A MessagePort channel | `@statewalker/webrun-
|
|
34
|
+
| A MessagePort channel | `@statewalker/webrun-rpc` for the `Duplex`, then `fetchOverDuplex` / `serveFetchOverDuplex` from `@statewalker/webrun-http-streams`. |
|
|
35
35
|
| A WebSocket | Same, with `@statewalker/webrun-streams-ws` supplying the `Duplex`. |
|
|
36
36
|
| Deno / Cloudflare Workers / Node's built-in HTTP | The handler is `(Request) ⇒ Response` — drop in as-is. |
|
|
37
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.
|
|
48
|
+
|
|
38
49
|
## How to use
|
|
39
50
|
|
|
40
51
|
```sh
|
|
@@ -51,6 +62,8 @@ npm install @statewalker/webrun-rpc-http
|
|
|
51
62
|
| `RpcClient` | Shape of the object returned from `newRpcClient`: `{ loadService<T>(name) }`. |
|
|
52
63
|
| `Json` / `JsonObject` | Recursive JSON types — everything that survives a `JSON` round-trip. |
|
|
53
64
|
| `getInstanceMethods(instance)` | Reflect callable properties of `instance` into a `Record<string, Function>`, walking the prototype chain up to (but not including) `Object.prototype`. Used internally by `newRpcServer`; exposed for callers that need it. |
|
|
65
|
+
| `NewRpcServerOptions` | `{ path?: string }` — URL prefix to mount under; a trailing slash is stripped, empty (default) mounts at the origin root. |
|
|
66
|
+
| `NewRpcClientOptions` | `{ baseUrl: string; fetch?: (request) => Promise<Response> }` — `fetch` defaults to `globalThis.fetch`; pass a webrun-http handler to run in-process. |
|
|
54
67
|
|
|
55
68
|
## Examples
|
|
56
69
|
|
|
@@ -284,4 +297,4 @@ pnpm lint # biome check src tests
|
|
|
284
297
|
|
|
285
298
|
## License
|
|
286
299
|
|
|
287
|
-
MIT © statewalker
|
|
300
|
+
MIT © statewalker — see [LICENSE](../../LICENSE).
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { deserializeError, serializeError } from "@statewalker/webrun-streams";
|
|
1
2
|
//#region src/get-instance-methods.ts
|
|
2
3
|
/**
|
|
3
4
|
* Collect every callable property of `instance` — including inherited ones —
|
|
@@ -18,31 +19,6 @@ function getInstanceMethods(instance) {
|
|
|
18
19
|
return methods;
|
|
19
20
|
}
|
|
20
21
|
//#endregion
|
|
21
|
-
//#region ../webrun-streams/src/errors.ts
|
|
22
|
-
function serializeError(error) {
|
|
23
|
-
if (error instanceof Error) {
|
|
24
|
-
const out = {
|
|
25
|
-
message: error.message,
|
|
26
|
-
stack: error.stack
|
|
27
|
-
};
|
|
28
|
-
const bag = error;
|
|
29
|
-
for (const key of Object.keys(bag)) out[key] = bag[key];
|
|
30
|
-
return out;
|
|
31
|
-
}
|
|
32
|
-
if (typeof error === "object" && error !== null) {
|
|
33
|
-
const bag = error;
|
|
34
|
-
return {
|
|
35
|
-
message: String(bag.message ?? error),
|
|
36
|
-
...bag
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
return { message: String(error) };
|
|
40
|
-
}
|
|
41
|
-
function deserializeError(error) {
|
|
42
|
-
const payload = typeof error === "string" ? { message: error } : error;
|
|
43
|
-
return Object.assign(new Error(payload.message), payload);
|
|
44
|
-
}
|
|
45
|
-
//#endregion
|
|
46
22
|
//#region src/new-rpc-client.ts
|
|
47
23
|
/**
|
|
48
24
|
* Build an RPC client that calls services exposed by {@link newRpcServer}.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/webrun-rpc-http",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
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",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"src"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@statewalker/webrun-streams": "0.
|
|
31
|
+
"@statewalker/webrun-streams": "0.2.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/node": "^26.2.0",
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022-2026 statewalker
|
|
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.
|