Skip to content

Commit

Permalink
add remaining node:module methods
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 29, 2025
1 parent 2cf2f89 commit 1a6f9a4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
54 changes: 49 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,23 @@ 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;
},
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 +120,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

0 comments on commit 1a6f9a4

Please sign in to comment.