@statewalker/webrun-rpc-http 0.1.1 → 0.1.4
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 +7 -5
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.4",
|
|
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",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"types": "./dist/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
+
"source": "./src/index.ts",
|
|
22
23
|
"types": "./dist/index.d.ts",
|
|
23
24
|
"import": "./dist/index.js"
|
|
24
25
|
}
|
|
@@ -28,14 +29,15 @@
|
|
|
28
29
|
"src"
|
|
29
30
|
],
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@statewalker/webrun-streams": "0.
|
|
32
|
+
"@statewalker/webrun-streams": "^0.2.1"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
|
-
"@
|
|
35
|
+
"@biomejs/biome": "^2.5.15",
|
|
36
|
+
"@types/node": "^26.6.4",
|
|
35
37
|
"rimraf": "^6.1.3",
|
|
36
|
-
"rolldown": "^1.2.
|
|
38
|
+
"rolldown": "^1.2.12",
|
|
37
39
|
"typescript": "^7.0.2",
|
|
38
|
-
"vitest": "^
|
|
40
|
+
"vitest": "^5.0.3"
|
|
39
41
|
},
|
|
40
42
|
"sideEffects": false,
|
|
41
43
|
"publishConfig": {
|