1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/node): process.getBuiltinModule (#26833)

Closes https://github.com/denoland/deno/issues/26832
This commit is contained in:
Divy Srivastava 2024-11-13 08:02:09 +05:30 committed by GitHub
parent 119910f339
commit 43812ee8ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 3 deletions

View file

@ -1233,6 +1233,24 @@ function isBuiltin(moduleName) {
!StringPrototypeStartsWith(moduleName, "internal/"); !StringPrototypeStartsWith(moduleName, "internal/");
} }
function getBuiltinModule(id) {
if (!isBuiltin(id)) {
return undefined;
}
if (StringPrototypeStartsWith(id, "node:")) {
// Slice 'node:' prefix
id = StringPrototypeSlice(id, 5);
}
const mod = loadNativeModule(id, id);
if (mod) {
return mod.exports;
}
return undefined;
}
Module.isBuiltin = isBuiltin; Module.isBuiltin = isBuiltin;
Module.createRequire = createRequire; Module.createRequire = createRequire;
@ -1327,7 +1345,7 @@ export function register(_specifier, _parentUrl, _options) {
return undefined; return undefined;
} }
export { builtinModules, createRequire, isBuiltin, Module }; export { builtinModules, createRequire, getBuiltinModule, isBuiltin, Module };
export const _cache = Module._cache; export const _cache = Module._cache;
export const _extensions = Module._extensions; export const _extensions = Module._extensions;
export const _findPath = Module._findPath; export const _findPath = Module._findPath;

View file

@ -15,7 +15,7 @@ import {
import { warnNotImplemented } from "ext:deno_node/_utils.ts"; import { warnNotImplemented } from "ext:deno_node/_utils.ts";
import { EventEmitter } from "node:events"; import { EventEmitter } from "node:events";
import Module from "node:module"; import Module, { getBuiltinModule } from "node:module";
import { report } from "ext:deno_node/internal/process/report.ts"; import { report } from "ext:deno_node/internal/process/report.ts";
import { validateString } from "ext:deno_node/internal/validators.mjs"; import { validateString } from "ext:deno_node/internal/validators.mjs";
import { import {
@ -38,7 +38,15 @@ import {
versions, versions,
} from "ext:deno_node/_process/process.ts"; } from "ext:deno_node/_process/process.ts";
import { _exiting } from "ext:deno_node/_process/exiting.ts"; import { _exiting } from "ext:deno_node/_process/exiting.ts";
export { _nextTick as nextTick, chdir, cwd, env, version, versions }; export {
_nextTick as nextTick,
chdir,
cwd,
env,
getBuiltinModule,
version,
versions,
};
import { import {
createWritableStdioStream, createWritableStdioStream,
initStdin, initStdin,
@ -728,6 +736,8 @@ Process.prototype.getegid = getegid;
/** This method is removed on Windows */ /** This method is removed on Windows */
Process.prototype.geteuid = geteuid; Process.prototype.geteuid = geteuid;
Process.prototype.getBuiltinModule = getBuiltinModule;
// TODO(kt3k): Implement this when we added -e option to node compat mode // TODO(kt3k): Implement this when we added -e option to node compat mode
Process.prototype._eval = undefined; Process.prototype._eval = undefined;

View file

@ -1152,3 +1152,9 @@ Deno.test("process.stdout.columns writable", () => {
process.stdout.columns = 80; process.stdout.columns = 80;
assertEquals(process.stdout.columns, 80); assertEquals(process.stdout.columns, 80);
}); });
Deno.test("getBuiltinModule", () => {
assert(process.getBuiltinModule("fs"));
assert(process.getBuiltinModule("node:fs"));
assertEquals(process.getBuiltinModule("something"), undefined);
});