From ff27190d0f65e7108f9d49b2605955e18afe23a7 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 21 Aug 2024 11:08:51 -0400 Subject: [PATCH] move functionality behind compatibility flag --- src/node/internal/compatibility-flags.d.ts | 1 + src/node/zlib.ts | 46 +++++++++++-------- .../api/node/tests/zlib-nodejs-test.wd-test | 2 +- src/workerd/io/compatibility-date.capnp | 8 ++++ 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/node/internal/compatibility-flags.d.ts b/src/node/internal/compatibility-flags.d.ts index 1bb1a90c397..a8e830009c6 100644 --- a/src/node/internal/compatibility-flags.d.ts +++ b/src/node/internal/compatibility-flags.d.ts @@ -30,3 +30,4 @@ export const specCompliantResponseRedirect: boolean; export const workerdExperimental: boolean; export const durableObjectGetExisting: boolean; export const vectorizeQueryMetadataOptional: boolean; +export const nodeJsZlib: boolean; diff --git a/src/node/zlib.ts b/src/node/zlib.ts index bbdeb6eff41..651d3b27b46 100644 --- a/src/node/zlib.ts +++ b/src/node/zlib.ts @@ -1,21 +1,31 @@ -import { - crc32, - constants, - Gzip, - Gunzip, - Deflate, - DeflateRaw, - Inflate, - InflateRaw, - Unzip, - createGzip, - createGunzip, - createDeflate, - createDeflateRaw, - createInflate, - createInflateRaw, - createUnzip, -} from 'node-internal:internal_zlib'; +import * as zlib from 'node-internal:internal_zlib'; +import { crc32, constants } from 'node-internal:internal_zlib'; +import { nodeJsZlib } from 'workerd:compatibility-flags'; + +function protectMethod(method: unknown): unknown { + if (nodeJsZlib) { + return function notImplemented() { + throw new Error('Compatibility flag "nodejs_zlib" is not enabled'); + }; + } + + return method; +} + +const Gzip = protectMethod(zlib.Gzip); +const Gunzip = protectMethod(zlib.Gunzip); +const Deflate = protectMethod(zlib.Deflate); +const DeflateRaw = protectMethod(zlib.DeflateRaw); +const Inflate = protectMethod(zlib.Inflate); +const InflateRaw = protectMethod(zlib.InflateRaw); +const Unzip = protectMethod(zlib.Unzip); +const createGzip = protectMethod(zlib.createGzip); +const createGunzip = protectMethod(zlib.createGunzip); +const createDeflate = protectMethod(zlib.createDeflate); +const createDeflateRaw = protectMethod(zlib.createDeflateRaw); +const createInflate = protectMethod(zlib.createInflate); +const createInflateRaw = protectMethod(zlib.createInflateRaw); +const createUnzip = protectMethod(zlib.createUnzip); export { crc32, diff --git a/src/workerd/api/node/tests/zlib-nodejs-test.wd-test b/src/workerd/api/node/tests/zlib-nodejs-test.wd-test index 14bb95faafe..0631bc2fee8 100644 --- a/src/workerd/api/node/tests/zlib-nodejs-test.wd-test +++ b/src/workerd/api/node/tests/zlib-nodejs-test.wd-test @@ -8,7 +8,7 @@ const unitTests :Workerd.Config = ( (name = "worker", esModule = embed "zlib-nodejs-test.js") ], compatibilityDate = "2023-01-15", - compatibilityFlags = ["experimental", "nodejs_compat"], + compatibilityFlags = ["experimental", "nodejs_compat", "nodejs_zlib"], ) ), ], diff --git a/src/workerd/io/compatibility-date.capnp b/src/workerd/io/compatibility-date.capnp index 5f323ab863f..24851cf1d6c 100644 --- a/src/workerd/io/compatibility-date.capnp +++ b/src/workerd/io/compatibility-date.capnp @@ -580,4 +580,12 @@ struct CompatibilityFlags @0x8f8c1b68151b6cef { packages = "2024-03-01", backport = 0) $experimental; # Enables Python Workers and uses the bundle from the Pyodide source directory directly. For testing only. + + nodeJsZlib @59 :Bool + $compatEnableFlag("nodejs_zlib") + $compatDisableFlag("no_nodejs_zlib") + $experimental; + # 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. }