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 28, 2025
1 parent 2cf2f89 commit 5a8a301
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 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,27 @@ export function createRequire(
);
}

return moduleUtil.createRequire(normalizedPath);
// TODO(soon): We should move this to C++ land.
// We do not add extensions field because it is deprecated.
// Ref: https://nodejs.org/docs/latest/api/modules.html#requireid
return Object.assign(moduleUtil.createRequire(normalizedPath), {
resolve(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('require.resolve()');
},
get cache(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('require.cache');
},
get main(): void {
throw new ERR_METHOD_NOT_IMPLEMENTED('require.main');
},
});
}

// 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 +124,40 @@ 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 or in active development 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,
};

0 comments on commit 5a8a301

Please sign in to comment.