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

fix(ext/node): Implement isBuiltin in node:module #22817

Merged
merged 3 commits into from
Mar 9, 2024
Merged
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
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));
});