Skip to content

Commit

Permalink
fix(ext/node): Implement isBuiltin in node:module (#22817)
Browse files Browse the repository at this point in the history
Fixes #22502

Implements the
[`isBuiltin`](https://nodejs.org/api/module.html#moduleisbuiltinmodulename)
function in `node:module`. I had to update the version of `@types/node`
in the test registry in order to get the test I added to typecheck.
  • Loading branch information
nathanwhit authored Mar 9, 2024
1 parent 529f795 commit 26cee4e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 78 deletions.
21 changes: 20 additions & 1 deletion ext/node/polyfills/01_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,25 @@ function createRequire(filenameOrUrl) {
return createRequireFromPath(filename);
}

function isBuiltin(moduleName) {
if (typeof moduleName !== "string") {
return false;
}

if (StringPrototypeStartsWith(moduleName, "node:")) {
moduleName = StringPrototypeSlice(moduleName, 5);
} else if (moduleName === "test") {
// test is only a builtin if it has the "node:" scheme
// see https://github.com/nodejs/node/blob/73025c4dec042e344eeea7912ed39f7b7c4a3991/test/parallel/test-module-isBuiltin.js#L14
return false;
}

return moduleName in nativeModuleExports &&
!StringPrototypeStartsWith(moduleName, "internal/");
}

Module.isBuiltin = isBuiltin;

Module.createRequire = createRequire;

Module._initPaths = function () {
Expand Down Expand Up @@ -1249,7 +1268,7 @@ internals.requireImpl = {
nativeModuleExports,
};

export { builtinModules, createRequire, Module };
export { builtinModules, createRequire, isBuiltin, Module };
export const _cache = Module._cache;
export const _extensions = Module._extensions;
export const _findPath = Module._findPath;
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/npm/compare_globals/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Download http://localhost:4545/npm/registry/@denotest/globals
[UNORDERED_END]
[UNORDERED_START]
Download http://localhost:4545/npm/registry/@denotest/globals/1.0.0.tgz
Download http://localhost:4545/npm/registry/@types/node/node-18.8.2.tgz
Download http://localhost:4545/npm/registry/@types/node/node-18.16.19.tgz
[UNORDERED_END]
Check file:///[WILDCARD]/npm/compare_globals/main.ts
true
Expand Down
Binary file not shown.
74 changes: 1 addition & 73 deletions tests/testdata/npm/registry/@types/node/registry.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/testdata/publish/bare_node_builtins.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Warning: Resolving "url" as "node:url" at file:///[WILDCARD]/publish/bare_node_builtins/mod.ts:1:22. If you want to use a built-in Node module, add a "node:" prefix.
Warning: Resolving "url" as "node:url" at file:///[WILDCARD]/publish/bare_node_builtins/mod.ts:1:22. If you want to use a built-in Node module, add a "node:" prefix.
Download http://localhost:4545/npm/registry/@types/node
Download http://localhost:4545/npm/registry/@types/node/node-18.8.2.tgz
Download http://localhost:4545/npm/registry/@types/node/node-18.16.19.tgz
Check file:///[WILDCARD]/publish/bare_node_builtins/mod.ts
Checking for slow types in the public API...
Check file:///[WILDCARD]/publish/bare_node_builtins/mod.ts
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/publish/bare_node_builtins_no_warnings.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Download http://localhost:4545/npm/registry/@types/node
Download http://localhost:4545/npm/registry/@types/node/node-18.8.2.tgz
Download http://localhost:4545/npm/registry/@types/node/node-18.16.19.tgz
Check file:///[WILDCARD]/publish/bare_node_builtins/mod.ts
Checking for slow types in the public API...
Check file:///[WILDCARD]/publish/bare_node_builtins/mod.ts
Expand Down
15 changes: 14 additions & 1 deletion tests/unit_node/module_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { createRequire, Module } from "node:module";
import { createRequire, isBuiltin, Module } from "node:module";
import { assert, assertEquals } from "@std/assert/mod.ts";
import process from "node:process";
import * as path from "node:path";
Expand Down Expand Up @@ -70,3 +70,16 @@ Deno.test("Built-in Node modules have `node:` prefix", () => {

assert(thrown);
});

Deno.test("[node/module isBuiltin] recognizes node builtins", () => {
assert(isBuiltin("node:fs"));
assert(isBuiltin("node:test"));
assert(isBuiltin("fs"));
assert(isBuiltin("buffer"));

assert(!isBuiltin("internal/errors"));
assert(!isBuiltin("test"));
assert(!isBuiltin(""));
// deno-lint-ignore no-explicit-any
assert(!isBuiltin(undefined as any));
});

0 comments on commit 26cee4e

Please sign in to comment.