Skip to content

Commit

Permalink
Check for a validate function to enable an interposition step in JSG_…
Browse files Browse the repository at this point in the history
…STRUCT
  • Loading branch information
AdityaAtulTewari committed Jul 26, 2024
1 parent 2faac4f commit 0a12f2c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,6 @@ jsg::Ref<Request> Request::constructor(
}

KJ_IF_SOME(c, initDict.cache) {
JSG_REQUIRE(FeatureFlags::get(js).getCacheOptionEnabled(), Error, kj::str(
"The 'cache' field on 'RequestInitializerDict' is not implemented."));
cacheMode = getCacheModeFromName(c);
}

Expand Down Expand Up @@ -1200,6 +1198,13 @@ kj::Maybe<kj::String> Request::serializeCfBlobJson(jsg::Lock& js) {
return cf.serialize(js);
}

void RequestInitializerDict::validate() {
if(cache != kj::none) {
JSG_REQUIRE(Worker::Api::current().getFeatureFlags().getCacheOptionEnabled(), Error, kj::str(
"The 'cache' field on 'RequestInitializerDict' is not implemented."));
}
}

void Request::serialize(
jsg::Lock& js, jsg::Serializer& serializer,
const jsg::TypeHandler<RequestInitializerDict>& initDictHandler) {
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/api/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ struct RequestInitializerDict {
// jsg::Optional<kj::String> priority;
// TODO(conform): Might support later?

void validate();

JSG_STRUCT(method, headers, body, redirect, fetcher, cf, mode, credentials, cache,
referrer, referrerPolicy, integrity, signal);
JSG_STRUCT_TS_OVERRIDE(RequestInit<Cf = CfProperties> {
Expand Down
9 changes: 7 additions & 2 deletions src/workerd/jsg/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ class StructWrapper<Self, T, TypeTuple<FieldWrappers...>, kj::_::Indexes<indices
// it prescribes lexicographically-ordered member initialization, with base members ordered
// before derived members. Objects with mutating getters might be broken by this, but it
// doesn't seem worth fixing absent a compelling use case.

return T {
auto t = T {
kj::get<indices>(fields).unwrap(static_cast<Self&>(*this), isolate, context, in)...
};

if constexpr (requires { t.validate(); }) {
t.validate();
}

return t;
}

void newContext() = delete;
Expand Down

0 comments on commit 0a12f2c

Please sign in to comment.