|
3 | 3 | #include <workerd/api/basics.h> |
4 | 4 | #include <workerd/io/io-context.h> |
5 | 5 | #include <workerd/jsg/jsg.h> |
| 6 | +#include <workerd/jsg/modules-new.h> |
6 | 7 | #include <workerd/jsg/ser.h> |
| 8 | +#include <workerd/jsg/url.h> |
7 | 9 | #include <workerd/util/weak-refs.h> |
8 | 10 |
|
9 | 11 | namespace workerd::api { |
@@ -169,7 +171,37 @@ class MessageChannel final: public jsg::Object { |
169 | 171 | jsg::Ref<MessagePort> port2; |
170 | 172 | }; |
171 | 173 |
|
| 174 | +// Module that exposes MessageChannel and MessagePort for internal use by |
| 175 | +// built-in modules like node:worker_threads without requiring the global |
| 176 | +// expose_global_message_channel compat flag. |
| 177 | +class MessageChannelModule final: public jsg::Object { |
| 178 | + public: |
| 179 | + MessageChannelModule() = default; |
| 180 | + MessageChannelModule(jsg::Lock&, const jsg::Url&) {} |
| 181 | + |
| 182 | + JSG_RESOURCE_TYPE(MessageChannelModule) { |
| 183 | + JSG_NESTED_TYPE(MessageChannel); |
| 184 | + JSG_NESTED_TYPE(MessagePort); |
| 185 | + } |
| 186 | +}; |
| 187 | + |
| 188 | +template <class Registry> |
| 189 | +void registerMessageChannelModule(Registry& registry, auto featureFlags) { |
| 190 | + registry.template addBuiltinModule<MessageChannelModule>( |
| 191 | + "cloudflare-internal:messagechannel", workerd::jsg::ModuleRegistry::Type::INTERNAL); |
| 192 | +} |
| 193 | + |
| 194 | +template <typename TypeWrapper> |
| 195 | +kj::Own<jsg::modules::ModuleBundle> getInternalMessageChannelModuleBundle(auto featureFlags) { |
| 196 | + jsg::modules::ModuleBundle::BuiltinBuilder builder( |
| 197 | + jsg::modules::ModuleBundle::BuiltinBuilder::Type::BUILTIN_ONLY); |
| 198 | + static const auto kSpecifier = "cloudflare-internal:messagechannel"_url; |
| 199 | + builder.addObject<MessageChannelModule, TypeWrapper>(kSpecifier); |
| 200 | + return builder.finish(); |
| 201 | +} |
| 202 | + |
172 | 203 | } // namespace workerd::api |
173 | 204 |
|
174 | 205 | #define EW_MESSAGECHANNEL_ISOLATE_TYPES \ |
175 | | - api::MessagePort, api::MessageChannel, api::MessagePort::PostMessageOptions |
| 206 | + api::MessagePort, api::MessageChannel, api::MessagePort::PostMessageOptions, \ |
| 207 | + api::MessageChannelModule |
0 commit comments