From 6e11db09dda7d093bf0b8f6f8c1e8672185767c5 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 8 Oct 2024 10:09:34 -0400 Subject: [PATCH] add node:sys to comply with nodejs_compat --- src/workerd/api/node/BUILD.bazel | 6 +++++ src/workerd/api/node/module.c++ | 10 +++++++ src/workerd/api/node/tests/sys-nodejs-test.js | 27 +++++++++++++++++++ .../api/node/tests/sys-nodejs-test.wd-test | 15 +++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/workerd/api/node/tests/sys-nodejs-test.js create mode 100644 src/workerd/api/node/tests/sys-nodejs-test.wd-test diff --git a/src/workerd/api/node/BUILD.bazel b/src/workerd/api/node/BUILD.bazel index e4818bf06ce..33aa9c94706 100644 --- a/src/workerd/api/node/BUILD.bazel +++ b/src/workerd/api/node/BUILD.bazel @@ -189,6 +189,12 @@ wd_test( data = ["tests/util-nodejs-test.js"], ) +wd_test( + src = "tests/sys-nodejs-test.wd-test", + args = ["--experimental"], + data = ["tests/sys-nodejs-test.js"], +) + wd_test( size = "large", src = "tests/zlib-nodejs-test.wd-test", diff --git a/src/workerd/api/node/module.c++ b/src/workerd/api/node/module.c++ index 16c4fbfd697..b028f71c421 100644 --- a/src/workerd/api/node/module.c++ +++ b/src/workerd/api/node/module.c++ @@ -61,6 +61,16 @@ jsg::JsValue ModuleUtil::createRequire(jsg::Lock& js, kj::String path) { // If the specifier begins with one of our known prefixes, let's not resolve // it against the referrer. try { + if (spec == "node:sys" || spec == "sys") [[unlikely]] { + // The sys module was renamed to 'util'. This shim remains to keep old programs + // working. `sys` is deprecated and shouldn't be used. + + // Note to maintainers: Although this module has been deprecated for a while + // Node.js do not plan to remove it. + // See: https://github.com/nodejs/node/pull/35407#issuecomment-700693439 + return kj::Path::parse("node:util"); + } + if (spec.startsWith("node:") || spec.startsWith("cloudflare:") || spec.startsWith("workerd:")) { return kj::Path::parse(spec); diff --git a/src/workerd/api/node/tests/sys-nodejs-test.js b/src/workerd/api/node/tests/sys-nodejs-test.js new file mode 100644 index 00000000000..59d354ae6ac --- /dev/null +++ b/src/workerd/api/node/tests/sys-nodejs-test.js @@ -0,0 +1,27 @@ +import assert from 'node:assert'; +import module from 'node:module'; + +export const getBuiltinModule = { + async test() { + assert.deepStrictEqual( + process.getBuiltinModule('node:sys'), + process.getBuiltinModule('node:util') + ); + }, +}; + +export const canBeRequired = { + async test() { + const require = module.createRequire('/hello'); + assert.deepStrictEqual(require('node:sys'), require('node:util')); + assert.deepStrictEqual(require('sys'), require('node:util')); + }, +}; + +export const canBeImported = { + async test() { + const sys = await import('node:sys'); + const util = await import('node:util'); + assert.deepStrictEqual(sys, util); + }, +}; diff --git a/src/workerd/api/node/tests/sys-nodejs-test.wd-test b/src/workerd/api/node/tests/sys-nodejs-test.wd-test new file mode 100644 index 00000000000..3f34cd64fc2 --- /dev/null +++ b/src/workerd/api/node/tests/sys-nodejs-test.wd-test @@ -0,0 +1,15 @@ +using Workerd = import "/workerd/workerd.capnp"; + +const unitTests :Workerd.Config = ( + services = [ + ( name = "nodejs-sys-test", + worker = ( + modules = [ + (name = "worker", esModule = embed "sys-nodejs-test.js") + ], + compatibilityDate = "2023-10-01", + compatibilityFlags = ["nodejs_compat_v2"], + ) + ), + ], +);