Skip to content

Commit

Permalink
Merge pull request #722 from cloudflare/jsnell/url-standard-delete-ha…
Browse files Browse the repository at this point in the history
…s-value-arg
  • Loading branch information
jasnell committed Jun 1, 2023
2 parents 2533ed6 + d4e83c0 commit d3788af
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
48 changes: 30 additions & 18 deletions src/workerd/api/url-standard.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1943,15 +1943,23 @@ void URLSearchParams::append(jsg::UsvString name, jsg::UsvString value) {
update();
}

void URLSearchParams::delete_(jsg::UsvString name, jsg::Optional<jsg::Value> value) {
KJ_IF_MAYBE(v, value) {
// The WHATWG recently added new arguments to the delete() and has() methods.
// We need to determine if adding those will break anyone if they are added
// without a compat flag. For now, we're just logging so we can know for sure.
// If we get this warning even once in production we'll have to introduce the
// new arguments behind a compat flag.
// https://github.com/whatwg/url/pull/735
LOG_WARNING_ONCE("URLSearchParams.prototype.delete() called with a second argument.");
void URLSearchParams::delete_(jsg::UsvString name,
jsg::Optional<jsg::UsvString> value,
CompatibilityFlags::Reader featureFlags) {
if (featureFlags.getUrlSearchParamsDeleteHasValueArg()) {
// The whatwg url spec was updated to add a second optional argument to delete()
// and has(). While it was determined that it likely didn't break browser users,
// the change could break at least a couple existing deployed workers so we have
// to gate support behind a compat flag.
KJ_IF_MAYBE(v, value) {
auto pivot = std::remove_if(list.begin(), list.end(),
[&name,&v=*v](auto& kv) {
return kv.name == name && kv.value == v;
});
list.truncate(pivot - list.begin());
update();
return;
}
}
auto pivot = std::remove_if(list.begin(), list.end(),
[&name](auto& kv) { return kv.name == name; });
Expand All @@ -1974,15 +1982,19 @@ kj::Array<jsg::UsvStringPtr> URLSearchParams::getAll(jsg::UsvString name) {
return result.releaseAsArray();
}

bool URLSearchParams::has(jsg::UsvString name, jsg::Optional<jsg::Value> value) {
KJ_IF_MAYBE(v, value) {
// The WHATWG recently added new arguments to the delete() and has() methods.
// We need to determine if adding those will break anyone if they are added
// without a compat flag. For now, we're just logging so we can know for sure.
// If we get this warning even once in production we'll have to introduce the
// new arguments behind a compat flag.
// https://github.com/whatwg/url/pull/735
LOG_WARNING_ONCE("URLSearchParams.prototype.has() called with a second argument.");
bool URLSearchParams::has(jsg::UsvString name, jsg::Optional<jsg::UsvString> value,
CompatibilityFlags::Reader featureFlags) {
if (featureFlags.getUrlSearchParamsDeleteHasValueArg()) {
// The whatwg url spec was updated to add a second optional argument to delete()
// and has(). While it was determined that it likely didn't break browser users,
// the change could break at least a couple existing deployed workers so we have
// to gate support behind a compat flag.
KJ_IF_MAYBE(v, value) {
for (auto& entry : list) {
if (entry.name == name && entry.value == *v) return true;
}
return false;
}
}
for (auto& entry : list) {
if (entry.name == name) return true;
Expand Down
6 changes: 4 additions & 2 deletions src/workerd/api/url-standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ class URLSearchParams: public jsg::Object {
}

void append(jsg::UsvString name, jsg::UsvString value);
void delete_(jsg::UsvString name, jsg::Optional<jsg::Value> value);
void delete_(jsg::UsvString name, jsg::Optional<jsg::UsvString> value,
CompatibilityFlags::Reader featureFlags);
kj::Maybe<jsg::UsvStringPtr> get(jsg::UsvString name);
kj::Array<jsg::UsvStringPtr> getAll(jsg::UsvString name);
bool has(jsg::UsvString name, jsg::Optional<jsg::Value> value);
bool has(jsg::UsvString name, jsg::Optional<jsg::UsvString> value,
CompatibilityFlags::Reader featureFlags);
void set(jsg::UsvString name, jsg::UsvString value);
void sort();

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 @@ -297,4 +297,11 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
# This one operates a bit backwards. With the flag *enabled* no default cfBotManagement
# data will be included. The the flag *disable*, default cfBotManagement data will be
# included in the request.cf if the field is not present.

urlSearchParamsDeleteHasValueArg @30 :Bool
$compatEnableFlag("urlsearchparams_delete_has_value_arg")
$compatDisableFlag("no_urlsearchparams_delete_has_value_arg")
$compatEnableDate("2023-07-01");
# When enabled, the delete() and has() methods of the standard URLSearchParams object
# (see url-standard.h) will have the recently added second value argument enabled.
}

0 comments on commit d3788af

Please sign in to comment.