Skip to content

Commit

Permalink
Add support for named entrypoints (#5215)
Browse files Browse the repository at this point in the history
* feature: support named entrypoints in `wrangler (pages) deploy`

* chore: exclude `miniflare` from monorepo's Prettier config

* fix: allow `script`s without `scriptPath`s to import built-ins

* feature: service binding named entrypoints for single-instance JSRPC

* feature: direct socket named entrypoints for multi-instance JSRPC

* refactor: switch middleware to `external` services for dev registry

* feature: support binding to named entrypoints in `wrangler dev`

* feature: add middleware support to `WorkerEntrypoint`s

* feature: add API for starting isolated dev registry

* fix: ensure `url` and `cf` blob preserved across service bindings

* test: add tests for named entrypoints and RPC

* fixup! test: add tests for named entrypoints and RPC

Update error messages

* fix: improve error message for cross-session Durable Object RPC

* chore: move service binding assignment before Durable Objects

* fix: improve error message for RPC on not found service

* test: update deploy snapshot

* Add test for binding to own named entrypoint

* fix: allow named entrypoints to current worker

* Simplify cross-Worker Durable Object RPC error message

* Bump @cloudflare/workers-types@4.20240402.0

---------

Co-authored-by: bcoll <bcoll@cloudflare.com>
  • Loading branch information
GregBrimble and mrbbot committed Apr 3, 2024
1 parent b531b59 commit cd03d1d
Show file tree
Hide file tree
Showing 85 changed files with 2,287 additions and 655 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-jeans-tap.md
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: ensure request `url` and `cf` properties preserved across service bindings

Previously, Wrangler could rewrite `url` and `cf` properties when sending requests via service bindings or Durable Object stubs. To match production behaviour, this change ensures these properties are preserved.
9 changes: 9 additions & 0 deletions .changeset/brown-cycles-live.md
@@ -0,0 +1,9 @@
---
"miniflare": minor
---

feature: customisable unsafe direct sockets entrypoints

Previously, Miniflare provided experimental `unsafeDirectHost` and `unsafeDirectPort` options for starting an HTTP server that pointed directly to a specific Worker. This change replaces these options with a single `unsafeDirectSockets` option that accepts an array of socket objects of the form `{ host?: string, port?: number, entrypoint?: string, proxy?: boolean }`. `host` defaults to `127.0.0.1`, `port` defaults to `0`, `entrypoint` defaults to `default`, and `proxy` defaults to `false`. This allows you to start HTTP servers for specific entrypoints of specific Workers. `proxy` controls the [`Style`](https://github.com/cloudflare/workerd/blob/af35f1e7b0f166ec4ca93a8bf7daeacda029f11d/src/workerd/server/workerd.capnp#L780-L789) of the socket.

Note these sockets set the `capnpConnectHost` `workerd` option to `"miniflare-unsafe-internal-capnp-connect"`. `external` `serviceBindings` will set their `capnpConnectHost` option to the same value allowing RPC over multiple `Miniflare` instances. Refer to https://github.com/cloudflare/workerd/pull/1757 for more information.
44 changes: 44 additions & 0 deletions .changeset/eight-seahorses-serve.md
@@ -0,0 +1,44 @@
---
"wrangler": minor
---

feature: support named entrypoints in service bindings

This change allows service bindings to bind to a named export of another Worker. As an example, consider the following Worker named `bound`:

```ts
import { WorkerEntrypoint } from "cloudflare:workers";

export class EntrypointA extends WorkerEntrypoint {
fetch(request) {
return new Response("Hello from entrypoint A!");
}
}

export const entrypointB: ExportedHandler = {
fetch(request, env, ctx) {
return new Response("Hello from entrypoint B!");
}
};

export default <ExportedHandler>{
fetch(request, env, ctx) {
return new Response("Hello from the default entrypoint!");
}
};
```

Up until now, you could only bind to the `default` entrypoint. With this change, you can bind to `EntrypointA` or `entrypointB` too using the new `entrypoint` option:

```toml
[[services]]
binding = "SERVICE"
service = "bound"
entrypoint = "EntrypointA"
```

To bind to named entrypoints with `wrangler pages dev`, use the `#` character:

```shell
$ wrangler pages dev --service=SERVICE=bound#EntrypointA
```
7 changes: 7 additions & 0 deletions .changeset/lazy-papayas-knock.md
@@ -0,0 +1,7 @@
---
"miniflare": patch
---

fix: allow `script`s without `scriptPath`s to import built-in modules

Previously, if a string `script` option was specified with `modules: true` but without a corresponding `scriptPath`, all `import`s were forbidden. This change relaxes that restriction to allow imports of built-in `node:*`, `cloudflare:*` and `workerd:*` modules without a `scriptPath`.
48 changes: 48 additions & 0 deletions .changeset/tidy-fans-speak.md
@@ -0,0 +1,48 @@
---
"miniflare": minor
---

feature: support named entrypoints for `serviceBindings`

This change allows service bindings to bind to a named export of another Worker using designators of the form `{ name: string | typeof kCurrentWorker, entrypoint?: string }`. Previously, you could only bind to the `default` entrypoint. With this change, you can bind to any exported entrypoint.

```ts
import { kCurrentWorker, Miniflare } from "miniflare";

const mf = new Miniflare({
workers: [
{
name: "a",
serviceBindings: {
A_RPC_SERVICE: { name: kCurrentWorker, entrypoint: "RpcEntrypoint" },
A_NAMED_SERVICE: { name: "a", entrypoint: "namedEntrypoint" },
B_NAMED_SERVICE: { name: "b", entrypoint: "anotherNamedEntrypoint" },
},
compatibilityFlags: ["rpc"],
modules: true,
script: `
import { WorkerEntrypoint } from "cloudflare:workers";
export class RpcEntrypoint extends WorkerEntrypoint {
ping() { return "a:rpc:pong"; }
}
export const namedEntrypoint = {
fetch(request, env, ctx) { return new Response("a:named:pong"); }
};
...
`,
},
{
name: "b",
modules: true,
script: `
export const anotherNamedEntrypoint = {
fetch(request, env, ctx) { return new Response("b:named:pong"); }
};
`,
},
],
});
```
2 changes: 1 addition & 1 deletion fixtures/additional-modules/package.json
Expand Up @@ -12,7 +12,7 @@
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20240320.1",
"@cloudflare/workers-types": "^4.20240402.0",
"undici": "^5.28.3",
"wrangler": "workspace:*"
}
Expand Down
15 changes: 15 additions & 0 deletions fixtures/entrypoints-rpc-tests/package.json
@@ -0,0 +1,15 @@
{
"name": "entrypoints-rpc-tests",
"private": true,
"scripts": {
"test": "vitest run",
"test:ci": "vitest run",
"test:watch": "vitest"
},
"devDependencies": {
"@cloudflare/workers-tsconfig": "workspace:*",
"wrangler": "workspace:*",
"ts-dedent": "^2.2.0",
"undici": "^5.28.3"
}
}

0 comments on commit cd03d1d

Please sign in to comment.