-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1028 from cloudflare/milan/rpc-session-event
JS RPC for Durable Objects
- Loading branch information
Showing
10 changed files
with
384 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2023 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
export class DurableObjectExample { | ||
constructor() {} | ||
|
||
async fetch(request) { | ||
return new Response("OK"); | ||
} | ||
|
||
foo() { | ||
// Server side impl of foo. | ||
// Cannot be called over rpc because we haven't enabled the `js_rpc` feature flag. | ||
throw new Error("This should not be callable over RPC!"); | ||
} | ||
} | ||
|
||
export default { | ||
async test(ctrl, env, ctx) { | ||
let id = env.ns.idFromName("foo"); | ||
let obj = env.ns.get(id); | ||
// We have `experimental` enabled, but the server side rpc call will fail. | ||
const expected = "TypeError: The receiving Worker does not allow its methods to be called over RPC."; | ||
try { | ||
await obj.foo(); | ||
throw new Error("Didn't throw on server side!"); | ||
} catch(e) { | ||
if (e != expected) { | ||
throw new Error(`Expected ${expected} but got ${e}`); | ||
} | ||
} | ||
|
||
// End it with a call to the DO. | ||
return await obj.fetch("http://foo/"); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const config :Workerd.Config = ( | ||
services = [ | ||
(name = "main", worker = .mainWorker), | ||
(name = "TEST_TMPDIR", disk = (writable = true)), | ||
], | ||
); | ||
|
||
const mainWorker :Workerd.Worker = ( | ||
compatibilityDate = "2022-09-16", | ||
compatibilityFlags = ["experimental", "nodejs_compat"], | ||
|
||
modules = [ | ||
(name = "worker", esModule = embed "js-rpc-disabled.js"), | ||
], | ||
|
||
durableObjectNamespaces = [ | ||
(className = "DurableObjectExample", uniqueKey = "210bd0cbd803ef7883a1ee9d86cce06e"), | ||
], | ||
|
||
durableObjectStorage = (localDisk = "TEST_TMPDIR"), | ||
|
||
bindings = [ | ||
(name = "ns", durableObjectNamespace = "DurableObjectExample"), | ||
], | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.