Skip to content

Commit

Permalink
add node:sys to comply with nodejs_compat
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 8, 2024
1 parent 77ebaf0 commit b0a2f14
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/workerd/api/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions src/workerd/api/node/tests/sys-nodejs-test.js
Original file line number Diff line number Diff line change
@@ -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);
},
};
15 changes: 15 additions & 0 deletions src/workerd/api/node/tests/sys-nodejs-test.wd-test
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 = "nodejs-sys-test",
worker = (
modules = [
(name = "worker", esModule = embed "sys-nodejs-test.js")
],
compatibilityDate = "2024-09-03",
compatibilityFlags = ["nodejs_compat"],
)
),
],
);
8 changes: 8 additions & 0 deletions src/workerd/jsg/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,14 @@ static const std::set<kj::StringPtr> NODEJS_BUILTINS{"_http_agent"_kj, "_http_cl
} // namespace

kj::Maybe<kj::String> checkNodeSpecifier(kj::StringPtr specifier) {
// 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
if (specifier == "sys" || specifier == "node:sys") [[unlikely]] {
return kj::str("node:util");
}
if (NODEJS_BUILTINS.contains(specifier)) {
return kj::str("node:", specifier);
} else if (specifier.startsWith("node:")) {
Expand Down

0 comments on commit b0a2f14

Please sign in to comment.