0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-12 16:59:32 -05:00

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

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.
This commit is contained in:
Nathan Whitaker 2024-03-08 18:06:04 -08:00 committed by Nathan Whitaker
parent 1e1d0410a4
commit 7f99b968fc
No known key found for this signature in database
7 changed files with 38 additions and 78 deletions

View file

@ -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 () {
@ -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;

View file

@ -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

Binary file not shown.

File diff suppressed because one or more lines are too long

View file

@ -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

View file

@ -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

View file

@ -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";
@ -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));
});