Skip to content

Commit 1c87560

Browse files
authored
Three small updates, in particular fixing async functions (#120)
* Mention @hono/capnweb in readme. Fixes #60 * Support serializing async functions. There's a hidden `AsyncFunction` type which they implement. Fixes #115. * Update dependencies. ... Except for playwright because Firefox starts giving opaque NetworkErrors on some tests that connect to localhost and I don't want to debug it right now.
1 parent 89a706f commit 1c87560

File tree

7 files changed

+696
-987
lines changed

7 files changed

+696
-987
lines changed

.changeset/cuddly-lions-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"capnweb": patch
3+
---
4+
5+
Fixed serialization of async functions.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ function newWebSocketRpcSession(
642642
: Disposable;
643643
```
644644

645+
### HTTP server using Hono
646+
647+
If your app is built on [Hono](https://hono.dev/) (on any runtime it supports), check out [`@hono/capnweb`](https://github.com/honojs/middleware/tree/main/packages/capnweb).
648+
645649
### MessagePort
646650

647651
Cap'n Web can also talk over MessagePorts. This can be used in a browser to talk to Web Workers, iframes, etc.

__tests__/index.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ describe("local stub", () => {
222222
expect(await stub(3)).toBe(8);
223223
});
224224

225+
it("supports wrapping an async function", async () => {
226+
let stub = new RpcStub(async (i :number) => { return i + 5; });
227+
expect(await stub(3)).toBe(8);
228+
});
229+
225230
it("supports wrapping an arbitrary object", async () => {
226231
let stub = new RpcStub({abc: "hello"});
227232
expect(await stub.abc).toBe("hello");
@@ -246,6 +251,27 @@ describe("local stub", () => {
246251
expect(await outerStub.inner.square(4)).toBe(16);
247252
});
248253

254+
it("supports wrapping an object with nested functions", async () => {
255+
let outerObject = { square: (x: number) => x * x, value: 42 };
256+
let outerStub = new RpcStub(outerObject);
257+
258+
expect(await outerStub.value).toBe(42);
259+
expect(await outerStub.square(4)).toBe(16);
260+
});
261+
262+
it("supports wrapping an object with nested async functions", async () => {
263+
async function asyncSqare(x: number) {
264+
await Promise.resolve();
265+
return x * x;
266+
}
267+
268+
let outerObject = { square: asyncSqare, value: 42 };
269+
let outerStub = new RpcStub(outerObject);
270+
271+
expect(await outerStub.value).toBe(42);
272+
expect(await outerStub.square(4)).toBe(16);
273+
});
274+
249275
it("supports wrapping an RpcTarget with nested stubs", async () => {
250276
class TargetWithStubs extends RpcTarget {
251277
getValue() { return 42; }
@@ -646,6 +672,17 @@ describe("basic rpc", () => {
646672

647673
expect(await stub.jsonify({x: 123, $remove$toJSON: () => "bad"})).toBe('{"x":123}');
648674
});
675+
676+
it("supports passing async functinos", async () => {
677+
await using harness = new TestHarness(new TestTarget());
678+
679+
async function square(i: number) {
680+
await Promise.resolve();
681+
return i * i;
682+
}
683+
684+
expect(await harness.stub.callFunction(square, 3)).toStrictEqual({result: 9});
685+
});
649686
});
650687

651688
describe("capability-passing", () => {

__tests__/test-util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export class TestTarget extends RpcTarget {
3535
return { result: self.square(i) };
3636
}
3737

38+
async callFunction(func: RpcStub<(i: number) => number>, i: number) {
39+
return { result: await func(i) };
40+
}
41+
3842
throwError() {
3943
throwErrorImpl();
4044
}

0 commit comments

Comments
 (0)