Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ kj::Promise<void> ServiceWorkerGlobalScope::connectUdp(kj::String host,
kj::Maybe<ExportedHandler&> exportedHandler) {
ExportedHandler& eh = JSG_REQUIRE_NONNULL(exportedHandler, Error,
"Connect ingress is not currently supported with Service Workers syntax.");
KJ_REQUIRE(FeatureFlags::get(lock).getWorkerdExperimental(),
"UDP ingress requires the experimental flag.");
KJ_REQUIRE(isUdpIngressEnabled(FeatureFlags::get(lock)),
"UDP ingress is disabled. Enable the udp_ingress compatibility flag.");

KJ_IF_SOME(handler, eh.connect) {
// Using a neuterable wrapper to manage lifetime, exactly like connect()'s NeuterableIoStream:
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/global-scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ class ServiceWorkerGlobalScope: public WorkerGlobalScope {
JSG_NESTED_TYPE(IdentityTransformStream);
}
JSG_NESTED_TYPE(HTMLRewriter);
if (flags.getWorkerdExperimental()) {
if (isUdpIngressEnabled(flags)) {
JSG_NESTED_TYPE(Datagram);
}

Expand Down
28 changes: 17 additions & 11 deletions src/workerd/api/sockets-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,21 @@ bool socketHasProtocolProperty(CompatibilityFlags::Reader flags) {
return result;
}

KJ_TEST("Socket protocol property requires experimental flag") {
capnp::MallocMessageBuilder disabledFlagsMessage;
auto disabledFlags = disabledFlagsMessage.initRoot<CompatibilityFlags>();
disabledFlags.setStreamsJavaScriptControllers(true);
KJ_EXPECT(!socketHasProtocolProperty(disabledFlags.asReader()));

capnp::MallocMessageBuilder enabledFlagsMessage;
auto enabledFlags = enabledFlagsMessage.initRoot<CompatibilityFlags>();
enabledFlags.setStreamsJavaScriptControllers(true);
enabledFlags.setWorkerdExperimental(true);
KJ_EXPECT(socketHasProtocolProperty(enabledFlags.asReader()));
KJ_TEST("udp_ingress exposes Socket.protocol") {
capnp::MallocMessageBuilder message;
auto flags = message.initRoot<CompatibilityFlags>();
flags.setStreamsJavaScriptControllers(true);
KJ_EXPECT(!socketHasProtocolProperty(flags.asReader()));

flags.setUdpIngress(true);
KJ_EXPECT(socketHasProtocolProperty(flags.asReader()));
}

KJ_TEST("UDP writable stream snapshots bytes before asynchronous send") {
capnp::MallocMessageBuilder flagsMessage;
auto flags = flagsMessage.initRoot<CompatibilityFlags>();
flags.setStreamsJavaScriptControllers(true);
flags.setUdpIngress(true);
TestFixture fixture(TestFixture::SetupParams{.featureFlags = flags.asReader()});
kj::Pin<AsyncSendState> state;

Expand Down Expand Up @@ -182,6 +180,7 @@ void runSocketWriteOutputGateTest(bool useTsStreams) {

capnp::MallocMessageBuilder flagsMessage;
auto flags = flagsMessage.initRoot<CompatibilityFlags>();
flags.setUdpIngress(true);
flags.setTypeScriptImplementedStreams(useTsStreams);

Worker::Actor::Id actorId = kj::str("test-actor-write");
Expand All @@ -206,6 +205,13 @@ void runSocketWriteOutputGateTest(bool useTsStreams) {

// Step 1: Connect before locking the gate so the pipe is established.
auto socket = connectImpl(env.js, kj::none, kj::str("localhost:1234"), kj::none);
{
auto& handler = KJ_ASSERT_NONNULL(env.js.tryGetTypeHandler<jsg::Ref<Socket>>());
auto object = KJ_ASSERT_NONNULL(
jsg::JsValue(handler.wrap(env.js, socket.addRef())).tryCast<jsg::JsObject>());
KJ_EXPECT(object.get(env.js, "protocol"_kj).toString(env.js) == "tcp"_kj);
KJ_EXPECT(!object.has(env.js, "proxyTo"_kj));
}
env.js.runMicrotasks();

// Prepare write data and lock gate BEFORE any co_await (Worker lock still held).
Expand Down
3 changes: 2 additions & 1 deletion src/workerd/api/sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <workerd/api/js-readable-stream.h>
#include <workerd/api/js-writable-stream.h>
#include <workerd/io/features.h>
#include <workerd/io/worker-interface.h>
#include <workerd/jsg/jsg.h>
#include <workerd/jsg/modules-new.h>
Expand Down Expand Up @@ -370,7 +371,7 @@ class Socket: public jsg::Object {
JSG_READONLY_PROTOTYPE_PROPERTY(opened, getOpened);
JSG_READONLY_PROTOTYPE_PROPERTY(upgraded, getUpgraded);
JSG_READONLY_PROTOTYPE_PROPERTY(secureTransport, getSecureTransport);
if (flags.getWorkerdExperimental()) {
if (isUdpIngressEnabled(flags)) {
// non-standard extension, not part of the proposed sockets spec
JSG_READONLY_PROTOTYPE_PROPERTY(protocol, getProtocol);
JSG_TS_OVERRIDE({
Expand Down
7 changes: 5 additions & 2 deletions src/workerd/api/tests/global-scope-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ export const mutableGlobals = {
},
};

export const datagramRequiresExperimental = {
export const datagramRequiresUdpIngress = {
test() {
strictEqual(globalThis.Datagram, undefined);
strictEqual(
typeof globalThis.Datagram,
Cloudflare.compatibilityFlags.udp_ingress ? 'function' : 'undefined'
);
},
};

Expand Down
21 changes: 21 additions & 0 deletions src/workerd/io/compatibility-date-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ KJ_TEST("newest compatibility date") {
KJ_EXPECT(newest == expected, newest, today, maximum);
}

KJ_TEST("UDP ingress compatibility flags") {
auto compileFlags = [](kj::StringPtr date, kj::ArrayPtr<const kj::StringPtr> flags) {
capnp::MallocMessageBuilder message;
auto flagList = message.initRoot<capnp::List<capnp::Text>>(flags.size());
for (auto i: kj::indices(flags)) {
flagList.set(i, flags[i]);
}
auto output = message.getOrphanage().newOrphan<CompatibilityFlags>();
SimpleWorkerErrorReporter errorReporter;
compileCompatibilityFlags(date, flagList.asReader(), output.get(), errorReporter,
/*allowExperimentalFeatures=*/false, CompatibilityDateValidation::FUTURE_FOR_TEST, nullptr);
KJ_ASSERT(errorReporter.errors.empty());
return output.get().getUdpIngress();
};

KJ_EXPECT(!compileFlags("2026-12-15", {}));
KJ_EXPECT(compileFlags("2026-12-16", {}));
KJ_EXPECT(compileFlags("2026-12-15", {"udp_ingress"_kj}));
KJ_EXPECT(!compileFlags("2026-12-16", {"no_udp_ingress"_kj}));
}

KJ_TEST("compatibility flag parsing") {
auto expectCompileCompatibilityFlags =
[](kj::StringPtr compatDate, kj::ArrayPtr<const kj::StringPtr> featureFlags,
Expand Down
7 changes: 7 additions & 0 deletions src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -1879,4 +1879,11 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
# it carries plaintext between the peer and the handler. A handler that returns, throws or
# closes its socket before calling accept() fails the connection, and the peer's `opened`
# rejects. Without the flag, such connections are refused before the handler runs.

udpIngress @210 :Bool
$compatEnableFlag("udp_ingress")
$compatDisableFlag("no_udp_ingress")
$compatEnableDate("2026-12-16");
# Enables delivery of UDP flows to the Worker's connect() handler and exposes the global
# Datagram constructor and Socket.prototype.protocol. The protocol reports "tcp" or "udp".
}
4 changes: 4 additions & 0 deletions src/workerd/io/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ inline void configureJsgIsolate(jsg::Lock& js, CompatibilityFlags::Reader flags)
}
}

inline bool isUdpIngressEnabled(CompatibilityFlags::Reader flags) {
return flags.getUdpIngress() || flags.getWorkerdExperimental();
}

// Returns whether the new module registry implementation should be used for a
// worker with the given flags. Python workers always use the original module
// registry: the new_module_registry flag is ignored for them, whether it was
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/server/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Bazel labels: `//src/workerd/server:workerd`, `//src/workerd/server:workerd-cli`
- **Rust unit tests**: a module's tests are in the `<module>-test.rs` beside it (`config-test.rs` for `config.rs`, `listen/mod-test.rs` for `listen/mod.rs`), compiled only into the crates' test targets (`//src/workerd/server/server:workerd-server_test`, `//src/workerd/server:workerd-cli_test`); see `src/rust/AGENTS.md`, UNIT TEST FILES. The server crate's test binary links `//src/workerd/util:setup-async-io` because the C++ it reaches calls `kj::setupAsyncIo()`.
- **C++ unit tests** (`*-test.c++`, `kj_test`): alarm scheduler, channel tokens, container client, facet tree index, fallback service, JSON logger, actor IDs, `repl/repl-environment-test.c++` (`//src/workerd/server/repl:repl-environment-test@`), which evaluates input and loads configs from text through the in-process server, with real sockets replaced by `loopback:` names, and `repl/repl-api-test.c++`, which drops the REPL's handles under `DISALLOW_KJ_IO_DESTRUCTORS_SCOPE`.
- **`server-test.c++`** (`//src/workerd/server:server-test@`): the server end to end, in the test process. Each `TestServer` builds a `WorkerFactory` on the test's V8, timer, network and a temp directory, and hands it to `InProcessServer` (`server/in_process.rs`, through the server crate's bridge), which runs `run::run` / `run::test` / `run::bench` on it and queues the config errors and warnings for the test. `server.addDeploymentService` and `server.addSkippedDeploymentService` mark config services as coming from deployments; `startWithErrors` starts a server whose deployment errors leave it serving. No connection but UDP's leaves the process: the config's socket and external addresses become `loopback:` names, the test connects to the server's sockets and accepts the server's outbound connections by name (the `network` services' too), and the factory's own C++ clients (Docker, the debug port) dial the same names through the test's `kj::Network`. The factory's timer is the test's mock `kj::TimerImpl`, which `test.wait(n)` advances; the server's own delays run on it too (`Factory::sleep` / `Factory::now`, `server/config.rs`). An unexpected error log, config error or warning fails the case, as does a factory still referenced once the server is closed. Add a case here for every new config error or warning.
- **`tests/`**: `server-harness.mjs`, a Node.js harness spawning the `workerd` binary for E2E tests; it reads the `listen` events off `--control-fd`. Subdirectories: `assets`, `async-trace`, `cluster`, `compile-tests`, `container-client`, `container-session`, `container-shutdown`, `cron`, `d1`, `extensions`, `inspector`, `kv`, `module-fallback`, `queues`, `repl`, `startup-snapshot-console`, `structured-logging`, `udp-connect`, `udp-connect-ts-streams`, `udp-datagram-drop`, `workflows`. Every `.wd-test` in the repository also runs through `workerd test` (`wd_test` in `build/wd_test.bzl`, over `:workerd`), and every `wd_bench()` config through `workerd bench --quick`, in its `@smoke` tests (`build/wd_bench.bzl`).
- **`tests/`**: `server-harness.mjs`, a Node.js harness spawning the `workerd` binary for E2E tests; it reads the `listen` events off `--control-fd`. Subdirectories: `assets`, `async-trace`, `cluster`, `compile-tests`, `container-client`, `container-session`, `container-shutdown`, `cron`, `d1`, `extensions`, `inspector`, `kv`, `module-fallback`, `queues`, `repl`, `startup-snapshot-console`, `structured-logging`, `udp-connect`, `workflows`. Every `.wd-test` in the repository also runs through `workerd test` (`wd_test` in `build/wd_test.bzl`, over `:workerd`), and every `wd_bench()` config through `workerd bench --quick`, in its `@smoke` tests (`build/wd_bench.bzl`).
- **kj-hyper contract tests** (`src/rust/cxx/kj-hyper/tests`): HTTP and WebSocket semantics against kj's interfaces.
- `:rust-io-link-check` inspects the linked binary's symbols (unix): kj's own `setupAsyncIo()` must not be linked in.

Expand Down
6 changes: 2 additions & 4 deletions src/workerd/server/server-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ KJ_TEST("Server: UDP listener survives connect() handler exceptions") {
name = "worker",
worker = (
compatibilityDate = "2024-01-01",
compatibilityFlags = ["experimental"],
compatibilityFlags = ["udp_ingress"],
modules = [(
name = "worker.js",
esModule =
Expand All @@ -959,7 +959,6 @@ KJ_TEST("Server: UDP listener survives connect() handler exceptions") {
)]
))"_kj);

test.server.allowExperimental();
test.start();

// The second flow is only served if the server survived the first exception.
Expand Down Expand Up @@ -7374,7 +7373,7 @@ KJ_TEST("Server: UDP RPC over HTTP connections") {
( name = "worker",
worker = (
compatibilityDate = "2024-02-23",
compatibilityFlags = ["experimental"],
compatibilityFlags = ["udp_ingress"],
modules = [(
name = "main.js",
esModule =
Expand All @@ -7396,7 +7395,6 @@ KJ_TEST("Server: UDP RPC over HTTP connections") {
]
))"_kj);

test.server.allowExperimental();
test.start();

test.sendUdp("udp-address", "peer:1234", "hello"_kjb);
Expand Down
18 changes: 0 additions & 18 deletions src/workerd/server/tests/udp-connect-ts-streams/BUILD.bazel

This file was deleted.

19 changes: 0 additions & 19 deletions src/workerd/server/tests/udp-connect-ts-streams/config.capnp

This file was deleted.

123 changes: 0 additions & 123 deletions src/workerd/server/tests/udp-connect-ts-streams/driver.mjs

This file was deleted.

Loading
Loading