Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add remaining node:module methods #3420

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions src/node/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// https://opensource.org/licenses/Apache-2.0

import { default as moduleUtil } from 'node-internal:module';
import { ERR_INVALID_ARG_VALUE } from 'node-internal:internal_errors';
import {
ERR_INVALID_ARG_VALUE,
ERR_METHOD_NOT_IMPLEMENTED,
} from 'node-internal:internal_errors';

export function createRequire(
path: string | URL
Expand All @@ -21,16 +24,24 @@ export function createRequire(
);
}

return moduleUtil.createRequire(normalizedPath);
// TODO(soon): We should move this to C++ land.
// Ref: https://nodejs.org/docs/latest/api/modules.html#requireid
return Object.assign(moduleUtil.createRequire(normalizedPath), {
// We don't throw ERR_METHOD_NOT_IMPLEMENTED because it's too disruptive.
resolve(): void {
return undefined;
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
cache: Object.create(null),
main: undefined,
});
}

// Indicates only that the given specifier is known to be a
// Node.js built-in module specifier with or with the the
// 'node:' prefix. A true return value does not guarantee that
// the module is actually implemented in the runtime.
export function isBuiltin(specifier: string): boolean {
return moduleUtil.isBuiltin(specifier);
}
export const isBuiltin = moduleUtil.isBuiltin.bind(moduleUtil);

// Intentionally does not include modules with mandatory 'node:'
// prefix like `node:test`.
Expand Down Expand Up @@ -110,8 +121,42 @@ export const builtinModules = [
];
Object.freeze(builtinModules);

export function findSourceMap(): void {
// Not meaningful to implement in the context of workerd.
throw new ERR_METHOD_NOT_IMPLEMENTED('module.findSourceMap');
}

export function register(): void {
// Not meaningful to implement in the context of workerd.
throw new ERR_METHOD_NOT_IMPLEMENTED('module.register');
}

export function syncBuiltinESMExports(): void {
// Not meaningful to implement in the context of workerd.
throw new ERR_METHOD_NOT_IMPLEMENTED('module.syncBuiltinESMExports');
}

//
// IMPORTANT NOTE!
//
// We are deliberately not including any of these functions below because
// they are either experimental, in active development or not relevant
// as of January 2025.
//
// - module.registerHooks()
// - module.stripTypeScriptTypes()
// - module.SourceMap
// - module.constants.compileCacheStatus
// - module.enableCompileCache()
// - module.flushCompileCache()
// - module.getCompileCacheDir()
//

export default {
createRequire,
isBuiltin,
builtinModules,
findSourceMap,
register,
syncBuiltinESMExports,
};
4 changes: 2 additions & 2 deletions src/workerd/api/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ wd_test(
)

wd_test(
src = "tests/module-create-require-test.wd-test",
src = "tests/module-nodejs-test.wd-test",
args = ["--experimental"],
data = ["tests/module-create-require-test.js"],
data = ["tests/module-nodejs-test.js"],
)

wd_test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ export const isBuiltinTest = {
});
},
};

export const testErrorMethodNotImplemented = {
async test() {
const m = await import('node:module');
const methods = ['findSourceMap', 'register', 'syncBuiltinESMExports'];

for (const method of methods) {
throws(() => m[method](), {
name: 'Error',
code: 'ERR_METHOD_NOT_IMPLEMENTED',
});
}
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "module-create-require-test",
( name = "nodejs-module-test",
worker = (
modules = [
(name = "worker", esModule = embed "module-create-require-test.js"),
(name = "worker", esModule = embed "module-nodejs-test.js"),
(name = "foo", esModule = "export default 1;"),
(name = "bar", esModule = "export default 2; export const __cjsUnwrapDefault = true;"),
(name = "baz", commonJsModule = "module.exports = 3;"),
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"allowJs": true
},
"include": ["**/*.ts"],
"exclude": []
"exclude": ["wpt/**"]
}
Loading