global-builtin-modules 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -0
- package/generated/builtins-map.cjs +79 -0
- package/generated/builtins-map.js +77 -0
- package/generated/types.d.ts +189 -0
- package/index.cjs +4 -0
- package/index.d.ts +18 -0
- package/index.js +4 -0
- package/install.cjs +75 -0
- package/install.js +73 -0
- package/package.json +63 -0
- package/uninstall.cjs +5 -0
- package/uninstall.js +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# global-builtin-modules
|
|
2
|
+
|
|
3
|
+
Installs a global with **lazy-loaded Node.js builtin modules**.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i global-builtin-modules
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage (recommended)
|
|
12
|
+
|
|
13
|
+
Side-effect import at the top of your entrypoint:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import 'global-builtin-modules'
|
|
17
|
+
|
|
18
|
+
const fileContent = await N.fs.promises.readFile('example.txt', 'utf8')
|
|
19
|
+
const fileContent2 = await N.fs.promises.readFile('example2.txt', 'utf8')
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`N.fs` is loaded on first access; subsequent reads hit a normal property (no Proxy trap overhead).
|
|
23
|
+
|
|
24
|
+
## Manual install/uninstall (advanced)
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import { installGlobalBuiltinModules } from 'global-builtin-modules/install'
|
|
28
|
+
import { uninstallGlobalBuiltinModules } from 'global-builtin-modules/uninstall'
|
|
29
|
+
|
|
30
|
+
installGlobalBuiltinModules() // installs globalThis.N
|
|
31
|
+
|
|
32
|
+
// Optional: customize the global name
|
|
33
|
+
installGlobalBuiltinModules({ name: 'NB' })
|
|
34
|
+
await NB.path.join('a', 'b')
|
|
35
|
+
|
|
36
|
+
// Uninstall
|
|
37
|
+
uninstallGlobalBuiltinModules() // removes globalThis.N
|
|
38
|
+
uninstallGlobalBuiltinModules('NB') // removes globalThis.NB
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Options
|
|
42
|
+
|
|
43
|
+
`installGlobalBuiltinModules(options)` supports:
|
|
44
|
+
|
|
45
|
+
- `name` (default `"N"`): the global name to define
|
|
46
|
+
- `allowlist` (default: all Node builtins, minus internal `_...`): which builtins to expose on `N`
|
|
47
|
+
- `overwrite` (default `false`): overwrite an existing global with the same name
|
|
48
|
+
- `preventExtensions` (default `true`): disallow adding new properties onto `N`
|
|
49
|
+
- `require`: custom loader (useful in tests)
|
|
50
|
+
|
|
51
|
+
Example allowlist:
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { installGlobalBuiltinModules } from 'global-builtin-modules/install'
|
|
55
|
+
|
|
56
|
+
installGlobalBuiltinModules({
|
|
57
|
+
allowlist: ['fs', 'path', 'crypto'],
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Notes
|
|
62
|
+
|
|
63
|
+
- Node-only. Requires Node >= 18.
|
|
64
|
+
- Default allowlist is derived from Node’s `builtinModules`.
|
|
65
|
+
- Implementation uses self-rewriting getters (fast after first access).
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Auto-generated by scripts/generate-builtins.js - DO NOT EDIT
|
|
2
|
+
// Generated from Node.js builtinModules
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Maps N property names to Node.js module specifiers.
|
|
6
|
+
* Includes both original names (child_process) and camelCase variants (childProcess).
|
|
7
|
+
* @type {Record<string, string>}
|
|
8
|
+
*/
|
|
9
|
+
const builtinsMap = {
|
|
10
|
+
"assert": "node:assert",
|
|
11
|
+
"assertStrict": "node:assert/strict",
|
|
12
|
+
"async_hooks": "node:async_hooks",
|
|
13
|
+
"asyncHooks": "node:async_hooks",
|
|
14
|
+
"buffer": "node:buffer",
|
|
15
|
+
"child_process": "node:child_process",
|
|
16
|
+
"childProcess": "node:child_process",
|
|
17
|
+
"cluster": "node:cluster",
|
|
18
|
+
"console": "node:console",
|
|
19
|
+
"constants": "node:constants",
|
|
20
|
+
"crypto": "node:crypto",
|
|
21
|
+
"dgram": "node:dgram",
|
|
22
|
+
"diagnostics_channel": "node:diagnostics_channel",
|
|
23
|
+
"diagnosticsChannel": "node:diagnostics_channel",
|
|
24
|
+
"dns": "node:dns",
|
|
25
|
+
"dnsPromises": "node:dns/promises",
|
|
26
|
+
"domain": "node:domain",
|
|
27
|
+
"events": "node:events",
|
|
28
|
+
"fs": "node:fs",
|
|
29
|
+
"fsPromises": "node:fs/promises",
|
|
30
|
+
"http": "node:http",
|
|
31
|
+
"http2": "node:http2",
|
|
32
|
+
"https": "node:https",
|
|
33
|
+
"inspector": "node:inspector",
|
|
34
|
+
"inspectorPromises": "node:inspector/promises",
|
|
35
|
+
"module": "node:module",
|
|
36
|
+
"net": "node:net",
|
|
37
|
+
"os": "node:os",
|
|
38
|
+
"path": "node:path",
|
|
39
|
+
"pathPosix": "node:path/posix",
|
|
40
|
+
"pathWin32": "node:path/win32",
|
|
41
|
+
"perf_hooks": "node:perf_hooks",
|
|
42
|
+
"perfHooks": "node:perf_hooks",
|
|
43
|
+
"process": "node:process",
|
|
44
|
+
"punycode": "node:punycode",
|
|
45
|
+
"querystring": "node:querystring",
|
|
46
|
+
"readline": "node:readline",
|
|
47
|
+
"readlinePromises": "node:readline/promises",
|
|
48
|
+
"repl": "node:repl",
|
|
49
|
+
"stream": "node:stream",
|
|
50
|
+
"streamConsumers": "node:stream/consumers",
|
|
51
|
+
"streamPromises": "node:stream/promises",
|
|
52
|
+
"streamWeb": "node:stream/web",
|
|
53
|
+
"string_decoder": "node:string_decoder",
|
|
54
|
+
"stringDecoder": "node:string_decoder",
|
|
55
|
+
"sys": "node:sys",
|
|
56
|
+
"timers": "node:timers",
|
|
57
|
+
"timersPromises": "node:timers/promises",
|
|
58
|
+
"tls": "node:tls",
|
|
59
|
+
"trace_events": "node:trace_events",
|
|
60
|
+
"traceEvents": "node:trace_events",
|
|
61
|
+
"tty": "node:tty",
|
|
62
|
+
"url": "node:url",
|
|
63
|
+
"util": "node:util",
|
|
64
|
+
"utilTypes": "node:util/types",
|
|
65
|
+
"v8": "node:v8",
|
|
66
|
+
"vm": "node:vm",
|
|
67
|
+
"wasi": "node:wasi",
|
|
68
|
+
"worker_threads": "node:worker_threads",
|
|
69
|
+
"workerThreads": "node:worker_threads",
|
|
70
|
+
"zlib": "node:zlib"
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* List of all available property names on N.
|
|
75
|
+
* @type {readonly string[]}
|
|
76
|
+
*/
|
|
77
|
+
const builtinNames = Object.freeze(Object.keys(builtinsMap))
|
|
78
|
+
|
|
79
|
+
module.exports = { builtinsMap, builtinNames }
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Auto-generated by scripts/generate-builtins.js - DO NOT EDIT
|
|
2
|
+
// Generated from Node.js builtinModules
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Maps N property names to Node.js module specifiers.
|
|
6
|
+
* Includes both original names (child_process) and camelCase variants (childProcess).
|
|
7
|
+
* @type {Record<string, string>}
|
|
8
|
+
*/
|
|
9
|
+
export const builtinsMap = {
|
|
10
|
+
"assert": "node:assert",
|
|
11
|
+
"assertStrict": "node:assert/strict",
|
|
12
|
+
"async_hooks": "node:async_hooks",
|
|
13
|
+
"asyncHooks": "node:async_hooks",
|
|
14
|
+
"buffer": "node:buffer",
|
|
15
|
+
"child_process": "node:child_process",
|
|
16
|
+
"childProcess": "node:child_process",
|
|
17
|
+
"cluster": "node:cluster",
|
|
18
|
+
"console": "node:console",
|
|
19
|
+
"constants": "node:constants",
|
|
20
|
+
"crypto": "node:crypto",
|
|
21
|
+
"dgram": "node:dgram",
|
|
22
|
+
"diagnostics_channel": "node:diagnostics_channel",
|
|
23
|
+
"diagnosticsChannel": "node:diagnostics_channel",
|
|
24
|
+
"dns": "node:dns",
|
|
25
|
+
"dnsPromises": "node:dns/promises",
|
|
26
|
+
"domain": "node:domain",
|
|
27
|
+
"events": "node:events",
|
|
28
|
+
"fs": "node:fs",
|
|
29
|
+
"fsPromises": "node:fs/promises",
|
|
30
|
+
"http": "node:http",
|
|
31
|
+
"http2": "node:http2",
|
|
32
|
+
"https": "node:https",
|
|
33
|
+
"inspector": "node:inspector",
|
|
34
|
+
"inspectorPromises": "node:inspector/promises",
|
|
35
|
+
"module": "node:module",
|
|
36
|
+
"net": "node:net",
|
|
37
|
+
"os": "node:os",
|
|
38
|
+
"path": "node:path",
|
|
39
|
+
"pathPosix": "node:path/posix",
|
|
40
|
+
"pathWin32": "node:path/win32",
|
|
41
|
+
"perf_hooks": "node:perf_hooks",
|
|
42
|
+
"perfHooks": "node:perf_hooks",
|
|
43
|
+
"process": "node:process",
|
|
44
|
+
"punycode": "node:punycode",
|
|
45
|
+
"querystring": "node:querystring",
|
|
46
|
+
"readline": "node:readline",
|
|
47
|
+
"readlinePromises": "node:readline/promises",
|
|
48
|
+
"repl": "node:repl",
|
|
49
|
+
"stream": "node:stream",
|
|
50
|
+
"streamConsumers": "node:stream/consumers",
|
|
51
|
+
"streamPromises": "node:stream/promises",
|
|
52
|
+
"streamWeb": "node:stream/web",
|
|
53
|
+
"string_decoder": "node:string_decoder",
|
|
54
|
+
"stringDecoder": "node:string_decoder",
|
|
55
|
+
"sys": "node:sys",
|
|
56
|
+
"timers": "node:timers",
|
|
57
|
+
"timersPromises": "node:timers/promises",
|
|
58
|
+
"tls": "node:tls",
|
|
59
|
+
"trace_events": "node:trace_events",
|
|
60
|
+
"traceEvents": "node:trace_events",
|
|
61
|
+
"tty": "node:tty",
|
|
62
|
+
"url": "node:url",
|
|
63
|
+
"util": "node:util",
|
|
64
|
+
"utilTypes": "node:util/types",
|
|
65
|
+
"v8": "node:v8",
|
|
66
|
+
"vm": "node:vm",
|
|
67
|
+
"wasi": "node:wasi",
|
|
68
|
+
"worker_threads": "node:worker_threads",
|
|
69
|
+
"workerThreads": "node:worker_threads",
|
|
70
|
+
"zlib": "node:zlib"
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* List of all available property names on N.
|
|
75
|
+
* @type {readonly string[]}
|
|
76
|
+
*/
|
|
77
|
+
export const builtinNames = Object.freeze(Object.keys(builtinsMap))
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Auto-generated by scripts/generate-builtins.js - DO NOT EDIT
|
|
2
|
+
// Generated from Node.js builtinModules
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Union of all available property names on the global builtin modules object.
|
|
6
|
+
* Includes both original names (child_process) and camelCase variants (childProcess).
|
|
7
|
+
*/
|
|
8
|
+
export type BuiltinModuleName =
|
|
9
|
+
| 'assert'
|
|
10
|
+
| 'assertStrict'
|
|
11
|
+
| 'asyncHooks'
|
|
12
|
+
| 'async_hooks'
|
|
13
|
+
| 'buffer'
|
|
14
|
+
| 'childProcess'
|
|
15
|
+
| 'child_process'
|
|
16
|
+
| 'cluster'
|
|
17
|
+
| 'console'
|
|
18
|
+
| 'constants'
|
|
19
|
+
| 'crypto'
|
|
20
|
+
| 'dgram'
|
|
21
|
+
| 'diagnosticsChannel'
|
|
22
|
+
| 'diagnostics_channel'
|
|
23
|
+
| 'dns'
|
|
24
|
+
| 'dnsPromises'
|
|
25
|
+
| 'domain'
|
|
26
|
+
| 'events'
|
|
27
|
+
| 'fs'
|
|
28
|
+
| 'fsPromises'
|
|
29
|
+
| 'http'
|
|
30
|
+
| 'http2'
|
|
31
|
+
| 'https'
|
|
32
|
+
| 'inspector'
|
|
33
|
+
| 'inspectorPromises'
|
|
34
|
+
| 'module'
|
|
35
|
+
| 'net'
|
|
36
|
+
| 'os'
|
|
37
|
+
| 'path'
|
|
38
|
+
| 'pathPosix'
|
|
39
|
+
| 'pathWin32'
|
|
40
|
+
| 'perfHooks'
|
|
41
|
+
| 'perf_hooks'
|
|
42
|
+
| 'process'
|
|
43
|
+
| 'punycode'
|
|
44
|
+
| 'querystring'
|
|
45
|
+
| 'readline'
|
|
46
|
+
| 'readlinePromises'
|
|
47
|
+
| 'repl'
|
|
48
|
+
| 'stream'
|
|
49
|
+
| 'streamConsumers'
|
|
50
|
+
| 'streamPromises'
|
|
51
|
+
| 'streamWeb'
|
|
52
|
+
| 'stringDecoder'
|
|
53
|
+
| 'string_decoder'
|
|
54
|
+
| 'sys'
|
|
55
|
+
| 'timers'
|
|
56
|
+
| 'timersPromises'
|
|
57
|
+
| 'tls'
|
|
58
|
+
| 'traceEvents'
|
|
59
|
+
| 'trace_events'
|
|
60
|
+
| 'tty'
|
|
61
|
+
| 'url'
|
|
62
|
+
| 'util'
|
|
63
|
+
| 'utilTypes'
|
|
64
|
+
| 'v8'
|
|
65
|
+
| 'vm'
|
|
66
|
+
| 'wasi'
|
|
67
|
+
| 'workerThreads'
|
|
68
|
+
| 'worker_threads'
|
|
69
|
+
| 'zlib'
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The global builtin modules object providing lazy-loaded access to Node.js builtin modules.
|
|
73
|
+
* Properties are loaded on first access and cached thereafter.
|
|
74
|
+
*/
|
|
75
|
+
export interface GlobalBuiltinModules {
|
|
76
|
+
readonly assert: typeof import('node:assert')
|
|
77
|
+
readonly assertStrict: typeof import('node:assert/strict')
|
|
78
|
+
readonly async_hooks: typeof import('node:async_hooks')
|
|
79
|
+
readonly asyncHooks: typeof import('node:async_hooks')
|
|
80
|
+
readonly buffer: typeof import('node:buffer')
|
|
81
|
+
readonly child_process: typeof import('node:child_process')
|
|
82
|
+
readonly childProcess: typeof import('node:child_process')
|
|
83
|
+
readonly cluster: typeof import('node:cluster')
|
|
84
|
+
readonly console: typeof import('node:console')
|
|
85
|
+
readonly constants: typeof import('node:constants')
|
|
86
|
+
readonly crypto: typeof import('node:crypto')
|
|
87
|
+
readonly dgram: typeof import('node:dgram')
|
|
88
|
+
readonly diagnostics_channel: typeof import('node:diagnostics_channel')
|
|
89
|
+
readonly diagnosticsChannel: typeof import('node:diagnostics_channel')
|
|
90
|
+
readonly dns: typeof import('node:dns')
|
|
91
|
+
readonly dnsPromises: typeof import('node:dns/promises')
|
|
92
|
+
readonly domain: typeof import('node:domain')
|
|
93
|
+
readonly events: typeof import('node:events')
|
|
94
|
+
readonly fs: typeof import('node:fs')
|
|
95
|
+
readonly fsPromises: typeof import('node:fs/promises')
|
|
96
|
+
readonly http: typeof import('node:http')
|
|
97
|
+
readonly http2: typeof import('node:http2')
|
|
98
|
+
readonly https: typeof import('node:https')
|
|
99
|
+
readonly inspector: typeof import('node:inspector')
|
|
100
|
+
readonly inspectorPromises: typeof import('node:inspector/promises')
|
|
101
|
+
readonly module: typeof import('node:module')
|
|
102
|
+
readonly net: typeof import('node:net')
|
|
103
|
+
readonly os: typeof import('node:os')
|
|
104
|
+
readonly path: typeof import('node:path')
|
|
105
|
+
readonly pathPosix: typeof import('node:path/posix')
|
|
106
|
+
readonly pathWin32: typeof import('node:path/win32')
|
|
107
|
+
readonly perf_hooks: typeof import('node:perf_hooks')
|
|
108
|
+
readonly perfHooks: typeof import('node:perf_hooks')
|
|
109
|
+
readonly process: typeof import('node:process')
|
|
110
|
+
readonly punycode: typeof import('node:punycode')
|
|
111
|
+
readonly querystring: typeof import('node:querystring')
|
|
112
|
+
readonly readline: typeof import('node:readline')
|
|
113
|
+
readonly readlinePromises: typeof import('node:readline/promises')
|
|
114
|
+
readonly repl: typeof import('node:repl')
|
|
115
|
+
readonly stream: typeof import('node:stream')
|
|
116
|
+
readonly streamConsumers: typeof import('node:stream/consumers')
|
|
117
|
+
readonly streamPromises: typeof import('node:stream/promises')
|
|
118
|
+
readonly streamWeb: typeof import('node:stream/web')
|
|
119
|
+
readonly string_decoder: typeof import('node:string_decoder')
|
|
120
|
+
readonly stringDecoder: typeof import('node:string_decoder')
|
|
121
|
+
readonly sys: typeof import('node:sys')
|
|
122
|
+
readonly timers: typeof import('node:timers')
|
|
123
|
+
readonly timersPromises: typeof import('node:timers/promises')
|
|
124
|
+
readonly tls: typeof import('node:tls')
|
|
125
|
+
readonly trace_events: typeof import('node:trace_events')
|
|
126
|
+
readonly traceEvents: typeof import('node:trace_events')
|
|
127
|
+
readonly tty: typeof import('node:tty')
|
|
128
|
+
readonly url: typeof import('node:url')
|
|
129
|
+
readonly util: typeof import('node:util')
|
|
130
|
+
readonly utilTypes: typeof import('node:util/types')
|
|
131
|
+
readonly v8: typeof import('node:v8')
|
|
132
|
+
readonly vm: typeof import('node:vm')
|
|
133
|
+
readonly wasi: typeof import('node:wasi')
|
|
134
|
+
readonly worker_threads: typeof import('node:worker_threads')
|
|
135
|
+
readonly workerThreads: typeof import('node:worker_threads')
|
|
136
|
+
readonly zlib: typeof import('node:zlib')
|
|
137
|
+
|
|
138
|
+
/** Non-enumerable allowlist snapshot */
|
|
139
|
+
readonly __allowlist: readonly string[]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface InstallGlobalBuiltinModulesOptions {
|
|
143
|
+
/**
|
|
144
|
+
* Name of the global variable to define.
|
|
145
|
+
* @default "N"
|
|
146
|
+
*/
|
|
147
|
+
name?: string
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Builtin module names to expose. Supports both original (child_process)
|
|
151
|
+
* and camelCase (childProcess) names.
|
|
152
|
+
* @default All public Node.js builtins
|
|
153
|
+
*/
|
|
154
|
+
allowlist?: BuiltinModuleName[]
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Custom require-like loader (advanced/testing).
|
|
158
|
+
*/
|
|
159
|
+
require?: (id: string) => unknown
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* If true, overwrite an existing global with the same name.
|
|
163
|
+
* @default false
|
|
164
|
+
*/
|
|
165
|
+
overwrite?: boolean
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* If true, prevents adding new properties to the global builtin modules object.
|
|
169
|
+
* @default true
|
|
170
|
+
*/
|
|
171
|
+
preventExtensions?: boolean
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Installs a global "N" (configurable name) whose properties lazily load Node.js builtins.
|
|
176
|
+
*/
|
|
177
|
+
export function installGlobalBuiltinModules(options?: InstallGlobalBuiltinModulesOptions): GlobalBuiltinModules
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Removes the global builtin modules object (or custom name) from globalThis.
|
|
181
|
+
* @returns true if the global was removed, false if it didn't exist
|
|
182
|
+
*/
|
|
183
|
+
export function uninstallGlobalBuiltinModules(name?: string): boolean
|
|
184
|
+
|
|
185
|
+
declare global {
|
|
186
|
+
var N: GlobalBuiltinModules
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export {}
|
package/index.cjs
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Re-export all types from the generated definitions
|
|
2
|
+
export {
|
|
3
|
+
BuiltinModuleName,
|
|
4
|
+
GlobalBuiltinModules,
|
|
5
|
+
InstallGlobalBuiltinModulesOptions,
|
|
6
|
+
installGlobalBuiltinModules,
|
|
7
|
+
uninstallGlobalBuiltinModules,
|
|
8
|
+
} from './generated/types.js'
|
|
9
|
+
|
|
10
|
+
// Re-export the global declaration
|
|
11
|
+
export {}
|
|
12
|
+
|
|
13
|
+
// Augment global scope
|
|
14
|
+
import type { GlobalBuiltinModules } from './generated/types.js'
|
|
15
|
+
|
|
16
|
+
declare global {
|
|
17
|
+
var N: GlobalBuiltinModules
|
|
18
|
+
}
|
package/index.js
ADDED
package/install.cjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const { builtinsMap, builtinNames } = require('./generated/builtins-map.cjs')
|
|
2
|
+
|
|
3
|
+
function defineLazy(obj, prop, load) {
|
|
4
|
+
Object.defineProperty(obj, prop, {
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
get() {
|
|
8
|
+
const value = load()
|
|
9
|
+
Object.defineProperty(obj, prop, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: false,
|
|
12
|
+
writable: false,
|
|
13
|
+
value,
|
|
14
|
+
})
|
|
15
|
+
return value
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Installs a global "N" (configurable name) whose properties lazily load Node.js builtins.
|
|
22
|
+
* Supports both original names (child_process) and camelCase variants (childProcess).
|
|
23
|
+
*
|
|
24
|
+
* @param {object} [options]
|
|
25
|
+
* @param {string} [options.name="N"]
|
|
26
|
+
* @param {string[]} [options.allowlist]
|
|
27
|
+
* @param {(id: string) => any} [options.require]
|
|
28
|
+
* @param {boolean} [options.overwrite=false]
|
|
29
|
+
* @param {boolean} [options.preventExtensions=true]
|
|
30
|
+
*/
|
|
31
|
+
function installGlobalBuiltinModules(options = {}) {
|
|
32
|
+
const {
|
|
33
|
+
name = 'N',
|
|
34
|
+
allowlist = builtinNames,
|
|
35
|
+
require: requireOpt,
|
|
36
|
+
overwrite = false,
|
|
37
|
+
preventExtensions = true,
|
|
38
|
+
} = options
|
|
39
|
+
|
|
40
|
+
const existing = globalThis[name]
|
|
41
|
+
if (existing && !overwrite) return existing
|
|
42
|
+
|
|
43
|
+
const requireFn = requireOpt ?? require
|
|
44
|
+
|
|
45
|
+
const N = Object.create(null)
|
|
46
|
+
|
|
47
|
+
for (const propName of allowlist) {
|
|
48
|
+
const moduleSpecifier = builtinsMap[propName]
|
|
49
|
+
if (!moduleSpecifier) {
|
|
50
|
+
// Unknown name - skip silently
|
|
51
|
+
continue
|
|
52
|
+
}
|
|
53
|
+
defineLazy(N, propName, () => requireFn(moduleSpecifier))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
Object.defineProperty(N, '__allowlist', {
|
|
57
|
+
enumerable: false,
|
|
58
|
+
configurable: false,
|
|
59
|
+
writable: false,
|
|
60
|
+
value: Object.freeze([...allowlist]),
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
if (preventExtensions) Object.preventExtensions(N)
|
|
64
|
+
|
|
65
|
+
Object.defineProperty(globalThis, name, {
|
|
66
|
+
value: N,
|
|
67
|
+
writable: false,
|
|
68
|
+
configurable: true,
|
|
69
|
+
enumerable: false,
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
return N
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = { installGlobalBuiltinModules }
|
package/install.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import { builtinsMap, builtinNames } from './generated/builtins-map.js'
|
|
3
|
+
|
|
4
|
+
function defineLazy(obj, prop, load) {
|
|
5
|
+
Object.defineProperty(obj, prop, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
get() {
|
|
9
|
+
const value = load()
|
|
10
|
+
// Replace getter with a plain, non-configurable value property.
|
|
11
|
+
Object.defineProperty(obj, prop, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: false,
|
|
14
|
+
writable: false,
|
|
15
|
+
value,
|
|
16
|
+
})
|
|
17
|
+
return value
|
|
18
|
+
},
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Installs a global "N" (configurable name) whose properties lazily load Node.js builtins.
|
|
24
|
+
* Supports both original names (child_process) and camelCase variants (childProcess).
|
|
25
|
+
*
|
|
26
|
+
* @param {import('./generated/types.js').InstallGlobalBuiltinModulesOptions} [options]
|
|
27
|
+
* @returns {import('./generated/types.js').GlobalBuiltinModules}
|
|
28
|
+
*/
|
|
29
|
+
export function installGlobalBuiltinModules(options = {}) {
|
|
30
|
+
const {
|
|
31
|
+
name = 'N',
|
|
32
|
+
allowlist = builtinNames,
|
|
33
|
+
require: requireOpt,
|
|
34
|
+
overwrite = false,
|
|
35
|
+
preventExtensions = true,
|
|
36
|
+
} = options
|
|
37
|
+
|
|
38
|
+
const existing = globalThis[name]
|
|
39
|
+
if (existing && !overwrite) return existing
|
|
40
|
+
|
|
41
|
+
// createRequire works in ESM; user can also supply a custom loader.
|
|
42
|
+
const requireFn = requireOpt ?? createRequire(import.meta.url)
|
|
43
|
+
|
|
44
|
+
const N = Object.create(null)
|
|
45
|
+
|
|
46
|
+
for (const propName of allowlist) {
|
|
47
|
+
const moduleSpecifier = builtinsMap[propName]
|
|
48
|
+
if (!moduleSpecifier) {
|
|
49
|
+
// Unknown name - skip silently (user may have passed invalid allowlist entry)
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
defineLazy(N, propName, () => requireFn(moduleSpecifier))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Expose the allowlist for debugging/introspection
|
|
56
|
+
Object.defineProperty(N, '__allowlist', {
|
|
57
|
+
enumerable: false,
|
|
58
|
+
configurable: false,
|
|
59
|
+
writable: false,
|
|
60
|
+
value: Object.freeze([...allowlist]),
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
if (preventExtensions) Object.preventExtensions(N)
|
|
64
|
+
|
|
65
|
+
Object.defineProperty(globalThis, name, {
|
|
66
|
+
value: N,
|
|
67
|
+
writable: false,
|
|
68
|
+
configurable: true, // allow uninstall/replace in tests or alternate installers
|
|
69
|
+
enumerable: false,
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
return N
|
|
73
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "global-builtin-modules",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Installs a global with lazy-loaded Node.js builtin modules",
|
|
5
|
+
"author": "Aaron Casanova <aaronccasanova@gmail.com>",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"import": "./index.js",
|
|
11
|
+
"require": "./index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"./install": {
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"import": "./install.js",
|
|
16
|
+
"require": "./install.cjs"
|
|
17
|
+
},
|
|
18
|
+
"./uninstall": {
|
|
19
|
+
"types": "./index.d.ts",
|
|
20
|
+
"import": "./uninstall.js",
|
|
21
|
+
"require": "./uninstall.cjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"generated",
|
|
26
|
+
"index.js",
|
|
27
|
+
"index.cjs",
|
|
28
|
+
"install.js",
|
|
29
|
+
"install.cjs",
|
|
30
|
+
"uninstall.js",
|
|
31
|
+
"uninstall.cjs",
|
|
32
|
+
"index.d.ts",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"generate": "node scripts/generate-builtins.js",
|
|
37
|
+
"prepublishOnly": "npm run generate"
|
|
38
|
+
},
|
|
39
|
+
"sideEffects": true,
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"node",
|
|
45
|
+
"builtins",
|
|
46
|
+
"builtin",
|
|
47
|
+
"lazy",
|
|
48
|
+
"global",
|
|
49
|
+
"loader"
|
|
50
|
+
],
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^25.0.9"
|
|
53
|
+
},
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "git+https://github.com/aaronccasanova/global-builtin-modules.git"
|
|
58
|
+
},
|
|
59
|
+
"homepage": "https://github.com/aaronccasanova/global-builtin-modules#readme",
|
|
60
|
+
"bugs": {
|
|
61
|
+
"url": "https://github.com/aaronccasanova/global-builtin-modules/issues"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/uninstall.cjs
ADDED
package/uninstall.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes the global created by installGlobalBuiltinModules (default "N").
|
|
3
|
+
* @param {string} [name="N"]
|
|
4
|
+
* @returns {boolean} true if removed, false otherwise
|
|
5
|
+
*/
|
|
6
|
+
export function uninstallGlobalBuiltinModules(name = 'N') {
|
|
7
|
+
return delete globalThis[name]
|
|
8
|
+
}
|