Skip to content

Commit

Permalink
Have nodejs_zlib implied by nodejs_compat_v2
Browse files Browse the repository at this point in the history
Ensures that if either nodejs_compat or nodejs_compat_v2 are enabled,
then nodejs_zlib will be enabled also
  • Loading branch information
jasnell committed Sep 25, 2024
1 parent 3615ece commit 704b836
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
19 changes: 19 additions & 0 deletions src/workerd/api/node/tests/zlib-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2603,3 +2603,22 @@ const PSS_VECTORS_JSON = `{
]
}
}`;

export const compatFlagTest = {
async test(_, env) {
const waitForIt = async (test) => {
const res = await env[test].fetch('http://example.org');
return await res.text();
};
const results = await Promise.allSettled([
waitForIt('compat'),
waitForIt('compatv2'),
waitForIt('compatNoV2'),
]);
assert.deepStrictEqual(results, [
{ status: 'fulfilled', value: 'true' },
{ status: 'fulfilled', value: 'true' },
{ status: 'fulfilled', value: 'true' },
]);
},
};
36 changes: 36 additions & 0 deletions src/workerd/api/node/tests/zlib-nodejs-test.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,43 @@ const unitTests :Workerd.Config = (
],
compatibilityDate = "2023-01-15",
compatibilityFlags = ["experimental", "nodejs_compat", "nodejs_zlib"],
bindings = [
( name = "compat", service = "compat" ),
( name = "compatv2", service = "compatv2" ),
( name = "compatNoV2", service = "compatNoV2" ),
],
)
),
(
name = "compat",
worker = (
modules = [
(name = "worker", esModule = "export default {fetch() {return new Response(`${JSON.stringify(globalThis.Cloudflare.compatibilityFlags.nodejs_zlib)}`);}}")
],
compatibilityDate = "2024-09-23",
compatibilityFlags = ["nodejs_compat"],
)
),
(
name = "compatv2",
worker = (
modules = [
(name = "worker", esModule = "export default {fetch() {return new Response(`${JSON.stringify(globalThis.Cloudflare.compatibilityFlags.nodejs_zlib)}`);}}")
],
compatibilityDate = "2024-09-23",
compatibilityFlags = ["nodejs_compat_v2"],
)
),
(
name = "compatNoV2",
worker = (
modules = [
(name = "worker", esModule = "export default {fetch() {return new Response(`${JSON.stringify(globalThis.Cloudflare.compatibilityFlags.nodejs_zlib)}`);}}")
],
compatibilityDate = "2024-09-23",
compatibilityFlags = ["nodejs_compat", "no_nodejs_compat_v2"],
)
)
],
);

27 changes: 17 additions & 10 deletions src/workerd/io/compatibility-date.c++
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void compileCompatibilityFlags(kj::StringPtr compatDate,
kj::Maybe<CompatDate> enableDate;
kj::StringPtr enableFlagName;
kj::StringPtr disableFlagName;
kj::Maybe<ImpliedBy> maybeImpliedBy;
kj::Vector<ImpliedBy> impliedByVector;

for (auto annotation: field.getProto().getAnnotations()) {
if (annotation.getId() == COMPAT_ENABLE_FLAG_ANNOTATION_ID) {
Expand All @@ -177,22 +177,29 @@ void compileCompatibilityFlags(kj::StringPtr compatDate,
} else if (annotation.getId() == EXPERIMENTAl_ANNOTATION_ID) {
isExperimental = true;
} else if (annotation.getId() == IMPLIED_BY_AFTER_DATE_ANNOTATION_ID) {
if (maybeImpliedBy == kj::none) {
auto value = annotation.getValue();
auto s = value.getStruct().getAs<workerd::ImpliedByAfterDate>();
auto parsedDate = KJ_ASSERT_NONNULL(CompatDate::parse(s.getDate()));
// This flag will be marked as enabled if the flag identified by
// s.getName() is enabled, but only on or after the specified date.
if (parsedCompatDate >= parsedDate && !disableByFlag) {
maybeImpliedBy.emplace(ImpliedBy{
auto value = annotation.getValue();
auto s = value.getStruct().getAs<workerd::ImpliedByAfterDate>();
auto parsedDate = KJ_ASSERT_NONNULL(CompatDate::parse(s.getDate()));
// This flag will be marked as enabled if the flag identified by
// s.getName() is enabled, but only on or after the specified date.
if (parsedCompatDate >= parsedDate && !disableByFlag) {
if (s.hasName()) {
impliedByVector.add(ImpliedBy{
.field = field,
.other = schema.getFieldByName(s.getName()),
});
} else if (s.hasNames()) {
for (auto name: s.getNames()) {
impliedByVector.add(ImpliedBy{
.field = field,
.other = schema.getFieldByName(name),
});
}
}
}
}
}
KJ_IF_SOME(impliedBy, maybeImpliedBy) {
for (auto& impliedBy: impliedByVector) {
// We only want to add the implied by flag if it is not explicitly disabled.
if (!disableByFlag) {
impliedByList.add(kj::mv(impliedBy));
Expand Down
11 changes: 7 additions & 4 deletions src/workerd/io/compatibility-date.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ $Cxx.allowCancellation;

struct ImpliedByAfterDate @0x8f8c1b68151b6cff {
# Annotates a compatibility flag to indicate that it is implied by the enablement
# of the named flag after the specified date.
name @0 :Text;
# of the named flag(s) after the specified date.
union {
name @0 :Text;
names @2 :List(Text);
}
date @1 :Text;
}

Expand Down Expand Up @@ -584,10 +587,10 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef {
nodeJsZlib @59 :Bool
$compatEnableFlag("nodejs_zlib")
$compatDisableFlag("no_nodejs_zlib")
$impliedByAfterDate(name = "nodeJsCompat", date = "2024-09-23");
$impliedByAfterDate(names = ["nodeJsCompat", "nodeJsCompatV2"], date = "2024-09-23");
# Enables node:zlib implementation while it is in-development.
# Once the node:zlib implementation is complete, this will be automatically enabled when
# nodejs_compat is enabled.
# nodejs_compat or nodejs_compat_v2 are enabled.

replicaRouting @60 :Bool
$compatEnableFlag("replica_routing")
Expand Down

0 comments on commit 704b836

Please sign in to comment.