-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements globalThis.Cloudflare.compatibilityFlags API
A simple built-in module and API for determining if a given compat flag is set. ``` const { compatibilityFlags } = globalThis.Cloudflare; console.log(compatibilityFlags['url_standard']); // 'on' or 'off' console.log(compatibilityFlags['url_original']); // 'on' or 'off' ```
- Loading branch information
Showing
9 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { ok, strictEqual, throws } from 'node:assert'; | ||
|
||
const { compatibilityFlags } = globalThis.Cloudflare; | ||
ok(Object.isSealed(compatibilityFlags)); | ||
ok(Object.isFrozen(compatibilityFlags)); | ||
ok(!Object.isExtensible(compatibilityFlags)); | ||
|
||
// It shuld be possible to shadow the Cloudflare global | ||
const Cloudflare = 1; | ||
strictEqual(Cloudflare, 1); | ||
|
||
export const compatFlagsTest = { | ||
test() { | ||
// The compatibility flags object should be sealed, frozen, and not extensible. | ||
throws(() => (compatibilityFlags.no_nodejs_compat_v2 = '...')); | ||
throws(() => (compatibilityFlags.not_a_real_compat_flag = '...')); | ||
throws(() => { | ||
delete compatibilityFlags['nodejs_compat_v2']; | ||
}); | ||
|
||
// The compatibility flags object should have no prototype. | ||
strictEqual(Object.getPrototypeOf(compatibilityFlags), null); | ||
|
||
// The compatibility flags object should have the expected properties... | ||
// That is... the only keys that should appear on the compatibilityFlags | ||
// object are the enable flags. | ||
// | ||
// If a key does not appear, it can mean one of three things: | ||
// 1. It is a disable flag. | ||
// 2. It is an experimental flag and the experimental option is not set. | ||
// 3. The flag does not exist. | ||
// | ||
// At this level we make no attempt to differentiate between these cases | ||
ok(compatibilityFlags['nodejs_compat_v2']); | ||
ok(compatibilityFlags['url_standard']); | ||
|
||
ok(!compatibilityFlags['no_nodejs_compat_v2']); | ||
ok(!compatibilityFlags['url_original']); | ||
strictEqual(compatibilityFlags['no_nodejs_compat_v2'], undefined); | ||
strictEqual(compatibilityFlags['url_original'], undefined); | ||
|
||
// Since we are not specifying the experimental flag, experimental flags should | ||
// not be included in the output. | ||
strictEqual(compatibilityFlags['durable_object_rename'], undefined); | ||
strictEqual('durable_object_rename' in compatibilityFlags, false); | ||
|
||
// If a flag does not exist, the value will be undefined. | ||
strictEqual(compatibilityFlags['not-a-real-compat-flag'], undefined); | ||
strictEqual('not-a-real-compat-flag' in compatibilityFlags, false); | ||
|
||
// The compatibility flags object should have the expected keys. | ||
const keys = Object.keys(compatibilityFlags); | ||
ok(keys.includes('nodejs_compat_v2')); | ||
ok(keys.includes('url_standard')); | ||
ok(!keys.includes('url_original')); | ||
ok(!keys.includes('not-a-real-compat-flag')); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const unitTests :Workerd.Config = ( | ||
services = [ | ||
( name = "compat-flags-test", | ||
worker = ( | ||
modules = [ | ||
(name = "worker", esModule = embed "compat-flags-test.js") | ||
], | ||
compatibilityDate = "2023-01-15", | ||
compatibilityFlags = ["nodejs_compat_v2"], | ||
) | ||
), | ||
], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters