-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.ts
More file actions
41 lines (36 loc) · 1.48 KB
/
module.ts
File metadata and controls
41 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @file Lazy-loader for `node:module`. See `node/fs.ts` for the design
* rationale shared across all `node/*.ts` lazy-loaders.
*/
// eslint-disable-next-line n/prefer-node-protocol
import type * as NodeModule from 'node:module'
import { IS_NODE } from '../constants/runtime'
let cachedModule: typeof NodeModule | undefined
export function getNodeModule(): typeof NodeModule {
// Skip in browser / non-Node runtimes — `node:module` is not available
// there and the static require() call would throw at runtime or produce an
// UNRESOLVED_IMPORT warning when a browser bundler walks the call.
if (!IS_NODE) {
return undefined as unknown as typeof NodeModule
}
return (cachedModule ??=
/*@__PURE__*/ require('node:module') as typeof NodeModule)
}
/**
* Lazy + cached reference to `node:module`'s `isBuiltin(name)`. First call
* resolves the binding; subsequent calls dispatch through the cached function
* reference. Safe to detach — `isBuiltin` is `this`-free.
*
* Returns `false` in browser / non-CJS environments where `require` is
* undefined — no `node:` modules are built-in there.
*
* Single source of truth for "is this a Node builtin?" probes across socket-lib
* (used by the smol-binding loaders to gate `require('node:smol-*')`).
*/
let cachedIsBuiltin: ((name: string) => boolean) | undefined
export function isNodeBuiltin(name: string): boolean {
if (!IS_NODE) {
return false
}
return (cachedIsBuiltin ??= getNodeModule()!.isBuiltin)(name)
}