-
Notifications
You must be signed in to change notification settings - Fork 772
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for named entrypoints (#5215)
* 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
1 parent
b531b59
commit cd03d1d
Showing
85 changed files
with
2,287 additions
and
655 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
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,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. |
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,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 | ||
``` |
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,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`. |
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,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"); } | ||
}; | ||
`, | ||
}, | ||
], | ||
}); | ||
``` |
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,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" | ||
} | ||
} |
Oops, something went wrong.