-
Notifications
You must be signed in to change notification settings - Fork 782
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 built-in
workerd
services (#435)
Specifically, this allows custom `network`, `external` and `disk` services to be specified as service bindings. Requests to `network` services are dispatched according to URL. Requests to `external` services are dispatched to the specified remote server. Requests to `disk` services are dispatched to an HTTP service backed by an on-disk directory. This PR also fixes a bug where custom function service bindings with the same name in different Workers would dispatch all requests to the first Workers' function.
- Loading branch information
Showing
6 changed files
with
146 additions
and
36 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
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,83 @@ | ||
import { Request, Response } from "undici"; | ||
import { z } from "zod"; | ||
import { | ||
ExternalServer, | ||
HttpOptions_Style, | ||
TlsOptions_Version, | ||
} from "../../runtime"; | ||
import { zAwaitable } from "../../shared"; | ||
|
||
// Zod validators for types in runtime/config/workerd.ts. | ||
// All options should be optional except where specifically stated. | ||
// TODO: autogenerate these with runtime/config/workerd.ts from capnp | ||
|
||
export const HttpOptionsHeaderSchema = z.object({ | ||
name: z.string(), // name should be required | ||
value: z.ostring(), // If omitted, the header will be removed | ||
}); | ||
const HttpOptionsSchema = z.object({ | ||
style: z.nativeEnum(HttpOptions_Style).optional(), | ||
forwardedProtoHeader: z.ostring(), | ||
cfBlobHeader: z.ostring(), | ||
injectRequestHeaders: HttpOptionsHeaderSchema.array().optional(), | ||
injectResponseHeaders: HttpOptionsHeaderSchema.array().optional(), | ||
}); | ||
|
||
const TlsOptionsKeypairSchema = z.object({ | ||
privateKey: z.ostring(), | ||
certificateChain: z.ostring(), | ||
}); | ||
|
||
const TlsOptionsSchema = z.object({ | ||
keypair: TlsOptionsKeypairSchema.optional(), | ||
requireClientCerts: z.oboolean(), | ||
trustBrowserCas: z.oboolean(), | ||
trustedCertificates: z.string().array().optional(), | ||
minVersion: z.nativeEnum(TlsOptions_Version).optional(), | ||
cipherList: z.ostring(), | ||
}); | ||
|
||
const NetworkSchema = z.object({ | ||
allow: z.string().array().optional(), | ||
deny: z.string().array().optional(), | ||
tlsOptions: TlsOptionsSchema.optional(), | ||
}); | ||
|
||
export const ExternalServerSchema = z.intersection( | ||
z.object({ address: z.string() }), // address should be required | ||
z.union([ | ||
z.object({ http: z.optional(HttpOptionsSchema) }), | ||
z.object({ | ||
https: z.optional( | ||
z.object({ | ||
options: HttpOptionsSchema.optional(), | ||
tlsOptions: TlsOptionsSchema.optional(), | ||
certificateHost: z.ostring(), | ||
}) | ||
), | ||
}), | ||
]) | ||
) as z.ZodType<ExternalServer>; | ||
// This type cast is required for `api-extractor` to produce a `.d.ts` rollup. | ||
// Rather than outputting a `z.ZodIntersection<...>` for this type, it will | ||
// just use `z.ZodType<ExternalServer>`. Without this, the extractor process | ||
// just ends up pinned at 100% CPU. Probably unbounded recursion? I guess this | ||
// type is too complex? Something to investigate... :thinking_face: | ||
|
||
const DiskDirectorySchema = z.object({ | ||
path: z.string(), // path should be required | ||
writable: z.oboolean(), | ||
}); | ||
|
||
export const ServiceFetchSchema = z | ||
.function() | ||
.args(z.instanceof(Request)) | ||
.returns(zAwaitable(z.instanceof(Response))); | ||
|
||
export const ServiceDesignatorSchema = z.union([ | ||
z.string(), | ||
z.object({ network: NetworkSchema }), | ||
z.object({ external: ExternalServerSchema }), | ||
z.object({ disk: DiskDirectorySchema }), | ||
ServiceFetchSchema, | ||
]); |
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