Compatibility flags·续5

function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }

>
> If the `queue_consumer_no_wait_for_wait_until` flag is enabled, Queues consumers will no longer wait for promises passed to `ctx.waitUntil()` to resolve before acknowledging messages. This can improve the performance of queue consumers which utilize `ctx.waitUntil()`. With the flag enabled, in the above example, the consumer Worker will acknowledge the batch without waiting for the sleep function to resolve.
>
> Using this flag will not affect the behavior of `ctx.waitUntil()`. `ctx.waitUntil()` will continue to extend the lifetime of your consumer Worker to continue to work even after the batch of messages has been acknowledged.
>
> ### Apply TransformStream backpressure fix
>
> | **Default as of** | 2024-12-16 |
> | --- | --- |
> | **Flag to enable** | `fixup-transform-stream-backpressure` |
> | **Flag to disable** | `original-transform-stream-backpressure` |
>
> The original implementation of `TransformStream` included a bug that would cause backpressure signaling to fail after the first write to the transform. Unfortunately, the fix can cause existing code written to address the bug to fail. Therefore, the `fixup-transform-stream-backpressure` compat flag is provided to enable the fix.
>
> The fix is enabled by default with compatibility dates of 2024-12-16 or later.
>
> To restore the original backpressure logic, disable the fix using the `original-transform-stream-backpressure` flag.
>
> ### Disable top-level await in require(...)
>
> | **Default as of** | 2024-12-02 |
> | --- | --- |
> | **Flag to enable** | `disable_top_level_await_in_require` |
> | **Flag to disable** | `enable_top_level_await_in_require` |
>
> Workers implements the ability to use the Node.js style `require(...)` method to import modules in the Worker bundle. Historically, this mechanism allowed required modules to use top-level await. This, however, is not Node.js compatible.
>
> The `disable_top_level_await_in_require` compat flag will cause `require()` to fail if the module uses a top-level await. This flag is default enabled with a compatibility date of 2024-12-02 or later.
>
> To restore the original behavior allowing top-level await, use the `enable_top_level_await_in_require` compatibility flag.
>
> ### Enable `cache: no-store` HTTP standard API
>
> | **Default as of** | 2024-11-11 |
> | --- | --- |
> | **Flag to enable** | `cache_option_enabled` |
> | **Flag to disable** | `cache_option_disabled` |
>
> When you enable the `cache_option_enabled` compatibility flag, you can specify a value for the `cache` property of the Request interface. When this compatibility flag is not enabled, or `cache_option_disabled` is set, the Workers runtime will throw an `Error` saying `The 'cache' field on 'RequestInitializerDict' is not implemented.`
>
> When this flag is enabled you can instruct Cloudflare not to cache the response from a subrequest you make from your Worker using the [`fetch()` API](https://developers.cloudflare.com/workers/runtime-apis/fetch/):
>
> The only cache option enabled with `cache_option_enabled` is `'no-store'`. Specifying any other value will cause the Workers runtime to throw a `TypeError` with the message `Unsupported cache mode: <the-mode-you-specified>`.
>
> When `no-store` is specified:
>
> - All requests have the headers `Pragma: no-cache` and `Cache-Control: no-cache` are set on them.
> - Subrequests to origins not hosted by Cloudflare bypass Cloudflare's cache.
>
> Examples using `cache: 'no-store'`:
>
```js
const response = await fetch("https://example.com", { cache: "no-store" });

The cache value can also be set on a Request object.

const request = new Request("https://example.com", { cache: "no-store" });
const response = await fetch(request);

Global fetch() strictly public

Flag to enable global_fetch_strictly_public
Flag to disable global_fetch_private_origin

When the global_fetch_strictly_public compatibility flag is enabled, the global fetch() function will strictly route requests as if they were made on the public Internet.

This means requests to a Worker's own zone will loop back to the "front door" of Cloudflare and will be treated like a request from the Internet, possibly even looping back to the same Worker again.

When the global_fetch_strictly_public is not enabled, such requests are routed to the zone's origin server, ignoring any Workers mapped to the URL and also bypassing Cloudflare security settings.

Handle cross-request promise resolution correctly

Default as of 2024-10-14
Flag to enable handle_cross_request_promise_resolution
Flag to disable no_handle_cross_request_promise_resolution

Historically, it was possible to resolve a promise from an incorrect request context, which could lead to promise continuations being scheduled in the wrong context, causing errors and difficult-to-diagnose bugs.

With handle_cross_request_promise_resolution enabled, promise continuations are scheduled to run in the correct request context if it is still alive, or dropped with a warning if the correct context has already ended.

Upper-case HTTP methods

Default as of 2024-10-14
Flag to enable upper_case_all_http_methods
Flag to disable no_upper_case_all_http_methods

HTTP methods are expected to be upper-cased. Per the fetch spec, if the method is specified as get, post, put, delete, head, or options, implementations are expected to uppercase the method. All other method names would generally be expected to throw as unrecognized (for example, patch would be an error while PATCH is accepted). This is a bit restrictive, even if it is in the spec. This flag modifies the behavior to uppercase all methods prior to parsing so that the method is always recognized if it is a known method.

To restore the standard behavior, use the no_upper_case_all_http_methods compatibility flag.

Automatically set the Symbol.toStringTag for Workers API objects

Default as of 2024-09-26
Flag to enable set_tostring_tag
Flag to disable do_not_set_tostring_tag

A change was made to set the Symbol.toStringTag on all Workers API objects in order to fix several spec compliance bugs. Unfortunately, this change was more breaking than anticipated. The do_not_set_tostring_tag compat flag restores the original behavior with compatibility dates of 2024-09-26 or earlier.

Enable node:zlib module

Default as of 2024-09-23
Flag to enable nodejs_zlib
Flag to disable no_nodejs_zlib

The nodejs_zlib flag enables the node:zlib module in Workers.

This flag is automatically enabled for Workers using a compatibility date of 2024-09-23 or later when nodejs_compat is enabled.

See the Node.js documentation ↗︎ for more details about the node:zlib API.

Allow specifying a custom port when making a subrequest with the fetch() API

Default as of 2024-09-02
Flag to enable allow_custom_ports
Flag to disable ignore_custom_ports

When this flag is enabled, and you specify a port when making a subrequest with the fetch() API, the port number you specify will be used.

When you make a subrequest to a website that uses Cloudflare ("Orange Clouded") — only ports supported by Cloudflare's reverse proxy can be specified. If you attempt to specify an unsupported port, it will be ignored.

When you make a subrequest to a website that does not use Cloudflare ("Grey Clouded") - any port can be specified.

For example:

const response = await fetch("https://example.com:8000");

With allow_custom_ports the above example would fetch https://example.com:8000 rather than https://example.com:443.

Note that creating a WebSocket client with a call to new WebSocket(url) will also obey this flag.

WritableStream abort clears pending write queue

Default as of 2024-09-02
Flag to enable internal_writable_stream_abort_clears_queue
Flag to disable internal_writable_stream_abort_does_not_clear_queue

When using the original WritableStream implementation ("internal" streams), the abort() operation was previously handled lazily, meaning that the queue of pending writes would not be cleared until the next time the queue was processed. This behavior could cause the stream to hang if the consumer stopped consuming.

With internal_writable_stream_abort_clears_queue enabled, the queue is cleared immediately upon abort(), preventing hangs in cases where the consumer has stopped processing writes.

Properly extract blob MIME type from content-type headers

Default as of 2024-06-03
Flag to enable blob_standard_mime_type
Flag to disable blob_legacy_mime_type

When calling response.blob.type(), the MIME type will now be properly extracted from content-type headers, per the WHATWG spec ↗︎.

Use standard URL parsing in fetch()

Default as of 2024-06-03
Flag to enable fetch_standard_url
Flag to disable fetch_legacy_url

The fetch_standard_url flag makes fetch() use WHATWG URL Standard ↗︎ parsing rules. The original implementation would throw TypeError: Fetch API cannot load errors with some URLs where standard parsing does not, for instance with the inclusion of whitespace before the URL. URL errors will now be thrown immediately upon calling new Request() with an improper URL. Previously, URL errors were thrown only once fetch() was called.

Returning empty Uint8Array on final BYOB read

Default as of 2024-05-13
Flag to enable internal_stream_byob_return_view
Flag to disable internal_stream_byob_return_undefined

In the original implementation of BYOB ("Bring your own buffer") ReadableStreams, the read() method would return undefined when the stream was closed and there was no more data to read. This behavior was inconsistent with the standard ReadableStream behavior, which returns an empty Uint8Array when the stream is closed.

When the internal_stream_byob_return_view flag is used, the BYOB read() will implement standard behavior.

```js const resp = await fetch('https://example.org'); const reader = resp.body.getReader({ mode: 'byob' }); await result = await reader.read(new Uint8Array(10));

if (result.done) { // The result gives us an empty Uint8Array... console.log(result.value.byteLength); // 0