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 6e11db0
Show file tree
Hide file tree
Showing 4 changed files with 58 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
10 changes: 10 additions & 0 deletions src/workerd/api/node/module.c++
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 = "2023-10-01",
compatibilityFlags = ["nodejs_compat_v2"],
)
),
],
);

0 comments on commit 6e11db0

Please sign in to comment.