Skip to content
File

Blob: src/node/module.ts

typescript406 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import { default as moduleUtil } from 'node-internal:module';
6import {
7 ERR_INVALID_ARG_VALUE,
8 ERR_METHOD_NOT_IMPLEMENTED,
9} from 'node-internal:internal_errors';
10import { builtinModules } from 'node-internal:internal_module';
11 
12export function enableCompileCache(): void {
13 // We don't plan to support this in the future.
14 // Since, this method shouldn't throw an error, we just act like a no-op.
15}
16 
17export function getCompileCacheDir(): undefined {
18 // We don't plan to support this in the future.
19 // Since, compile cache acts as a no-op, we just return undefined.
20 return undefined;
21}
22 
23// We are unlikely to implement this in the future.
24export const _extensions = {
25 '.js': (): void => {
26 throw new ERR_METHOD_NOT_IMPLEMENTED('module._extensions.js');
27 },
28 '.json': (): void => {
29 throw new ERR_METHOD_NOT_IMPLEMENTED('module._extensions.json');
30 },
31 '.node': (): void => {
32 throw new ERR_METHOD_NOT_IMPLEMENTED('module._extensions.node');
33 },
34};
35 
36export function createRequire(
37 path: string | URL
38): (specifier: string) => unknown {
39 // Note that per Node.js' requirements, path must be one of either
40 // an absolute file path or a file URL. We do not currently handle
41 // module specifiers as URLs yet, but we'll try to get close.
42 
43 const normalizedPath = `${path}`;
44 if (!normalizedPath.startsWith('/') && !normalizedPath.startsWith('file:')) {
45 throw new ERR_INVALID_ARG_VALUE(
46 'path',
47 normalizedPath,
48 'The argument must be a file URL object, a file URL string, or an absolute path string.'
49 );
50 }
51 
52 return moduleUtil.createRequire(normalizedPath);
53}
54 
55Object.defineProperties(createRequire, {
56 resolve: {
57 value: (): void => {
58 // TODO(soon): We could support this in the future.
59 throw new ERR_METHOD_NOT_IMPLEMENTED('module.createRequire.resolve');
60 },
61 enumerable: true,
62 writable: true,
63 },
64 paths: {
65 value: (): void => {
66 throw new ERR_METHOD_NOT_IMPLEMENTED(
67 'module.createRequire.resolve.paths'
68 );
69 },
70 enumerable: true,
71 writable: true,
72 },
73 cache: {
74 value: { __proto__: null },
75 enumerable: true,
76 writable: true,
77 },
78 extensions: {
79 value: _extensions,
80 enumerable: true,
81 writable: true,
82 },
83 main: {
84 value: undefined,
85 enumerable: true,
86 writable: true,
87 },
88});
89 
90// Indicates only that the given specifier is known to be a
91// Node.js built-in module specifier with or with the the
92// 'node:' prefix. A true return value does not guarantee that
93// the module is actually implemented in the runtime.
94export function isBuiltin(specifier: string): boolean {
95 return moduleUtil.isBuiltin(specifier);
96}
97 
98export { builtinModules };
99 
100export function register(): void {
101 // TODO(soon): We might support this in the future.
102 throw new ERR_METHOD_NOT_IMPLEMENTED('module.register');
103}
104 
105export function runMain(): void {
106 // We don't plan to support this in the future.
107 throw new ERR_METHOD_NOT_IMPLEMENTED('module.runMain');
108}
109 
110export function syncBuiltinESMExports(): void {
111 // We are unlikely to ever support this.
112 throw new ERR_METHOD_NOT_IMPLEMENTED('module.syncBuiltinESMExports');
113}
114 
115export function wrap(): void {
116 // TODO(soon): Implement this feature.
117 throw new ERR_METHOD_NOT_IMPLEMENTED('module.wrap');
118}
119 
120export const globalPaths: string[] = [];
121 
122const compileCacheStatus = Object.freeze({
123 __proto__: null,
124 FAILED: 0,
125 ENABLED: 1,
126 ALREADY_ENABLED: 2,
127 DISABLED: 3,
128});
129 
130export const constants = Object.freeze({
131 __proto__: null,
132 compileCacheStatus,
133});
134 
135export const _cache = { __proto__: null };
136export const _pathCache = { __proto__: null };
137 
138export function _debug(): void {
139 // This is deprecated and will be removed in the future.
140 throw new ERR_METHOD_NOT_IMPLEMENTED('module._debug');
141}
142 
143export function _findPath(): void {
144 // It doesn't make sense to support this.
145 throw new ERR_METHOD_NOT_IMPLEMENTED('module._findPath');
146}
147 
148export function _initPaths(): void {
149 // It doesn't make sense to support this.
150 throw new ERR_METHOD_NOT_IMPLEMENTED('module._initPaths');
151}
152 
153export function _load(): void {
154 // TODO(soon): Investigate the possibility of supporting this in the future.
155 throw new ERR_METHOD_NOT_IMPLEMENTED('module._load');
156}
157 
158export function _preloadModules(): void {
159 // It doesn't make sense to support this.
160 throw new ERR_METHOD_NOT_IMPLEMENTED('module._preloadModules');
161}
162 
163export function _resolveFilename(): void {
164 // TODO(soon): Investigate the possibility of supporting this in the future.
165 throw new ERR_METHOD_NOT_IMPLEMENTED('module._resolveFilename');
166}
167 
168export function _resolveLookupPaths(): void {
169 // TODO(soon): Investigate the possibility of supporting this in the future.
170 throw new ERR_METHOD_NOT_IMPLEMENTED('module._resolveLookupPaths');
171}
172 
173export function _nodeModulePaths(): void {
174 // It doesn't make sense to support this.
175 throw new ERR_METHOD_NOT_IMPLEMENTED('module._nodeModulePaths');
176}
177 
178export function findSourceMap(): void {
179 // TODO(soon): Investigate the possibility of supporting this in the future.
180 throw new ERR_METHOD_NOT_IMPLEMENTED('module.findSourceMap');
181}
182 
183export function findPackageJSON(): void {
184 // It doesn't make sense to support this.
185 throw new ERR_METHOD_NOT_IMPLEMENTED('module.findPackageJSON');
186}
187 
188export function flushCompileCache(): void {
189 // We don't implement compile cache. This acts as a no-op.
190}
191 
192export function getSourceMapsSupport(): Record<string, boolean | null> {
193 return Object.freeze({
194 __proto__: null,
195 enabled: false,
196 nodeModules: false,
197 generatedCode: false,
198 });
199}
200 
201export function setSourceMapsSupport(): void {
202 // We don't implement source maps support.
203 throw new ERR_METHOD_NOT_IMPLEMENTED('module.setSourceMapsSupport');
204}
205 
206export function stripTypeScriptTypes(): void {
207 // We don't implement stripping TypeScript types.
208 throw new ERR_METHOD_NOT_IMPLEMENTED('module.stripTypeScriptTypes');
209}
210 
211export function registerHooks(): void {
212 // We don't implement hooks registration.
213 throw new ERR_METHOD_NOT_IMPLEMENTED('module.registerHooks');
214}
215 
216export function SourceMap(): void {
217 // We don't support source maps.
218 throw new ERR_METHOD_NOT_IMPLEMENTED('module.SourceMap');
219}
220 
221export function Module(): void {
222 // TODO(soon): Investigate implementing Module class fully.
223 throw new ERR_METHOD_NOT_IMPLEMENTED('module.Module');
224}
225 
226// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
227Module.prototype.load = function load(): void {
228 // Acts as a no-op.
229};
230 
231// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
232Module.prototype.require = function require(): void {
233 // Acts as a no-op.
234};
235 
236// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
237Module.prototype._compile = function _compile(): void {
238 // Acts as a no-op.
239};
240 
241Object.defineProperty(Module.prototype, 'isPreloading', {
242 get() {
243 return false;
244 },
245});
246 
247Object.defineProperties(Module, {
248 register: {
249 value: register,
250 writable: true,
251 enumerable: true,
252 },
253 constants: {
254 value: constants,
255 writable: true,
256 enumerable: true,
257 },
258 enableCompileCache: {
259 value: enableCompileCache,
260 writable: true,
261 enumerable: true,
262 },
263 findPackageJSON: {
264 value: findPackageJSON,
265 writable: true,
266 enumerable: true,
267 },
268 flushCompileCache: {
269 value: flushCompileCache,
270 writable: true,
271 enumerable: true,
272 },
273 getCompileCacheDir: {
274 value: getCompileCacheDir,
275 writable: true,
276 enumerable: true,
277 },
278 stripTypeScriptTypes: {
279 value: stripTypeScriptTypes,
280 writable: true,
281 enumerable: true,
282 },
283 findSourceMap: {
284 value: findSourceMap,
285 writable: true,
286 enumerable: true,
287 },
288 SourceMap: {
289 value: SourceMap,
290 writable: true,
291 enumerable: true,
292 },
293 getSourceMapsSupport: {
294 value: getSourceMapsSupport,
295 writable: true,
296 enumerable: true,
297 },
298 setSourceMapsSupport: {
299 value: setSourceMapsSupport,
300 writable: true,
301 enumerable: true,
302 },
303 createRequire: {
304 value: createRequire,
305 writable: true,
306 enumerable: true,
307 },
308 builtinModules: {
309 value: builtinModules,
310 writable: true,
311 enumerable: true,
312 },
313 globalPaths: {
314 value: globalPaths,
315 writable: true,
316 enumerable: true,
317 },
318 isBuiltin: {
319 value: isBuiltin,
320 writable: true,
321 enumerable: true,
322 },
323 runMain: {
324 value: runMain,
325 writable: true,
326 enumerable: true,
327 },
328 syncBuiltinESMExports: {
329 value: syncBuiltinESMExports,
330 writable: true,
331 enumerable: true,
332 },
333 wrap: {
334 value: wrap,
335 writable: true,
336 enumerable: true,
337 },
338 _cache: {
339 value: _cache,
340 writable: true,
341 enumerable: true,
342 },
343 _debug: {
344 value: _debug,
345 writable: true,
346 enumerable: true,
347 },
348 _extensions: {
349 value: _extensions,
350 writable: true,
351 enumerable: true,
352 },
353 _findPath: {
354 value: _findPath,
355 writable: true,
356 enumerable: true,
357 },
358 _initPaths: {
359 value: _initPaths,
360 writable: true,
361 enumerable: true,
362 },
363 _load: {
364 value: _load,
365 writable: true,
366 enumerable: true,
367 },
368 _pathCache: {
369 value: _pathCache,
370 writable: true,
371 enumerable: true,
372 },
373 _preloadModules: {
374 value: _preloadModules,
375 writable: true,
376 enumerable: true,
377 },
378 _resolveFilename: {
379 value: _resolveFilename,
380 writable: true,
381 enumerable: true,
382 },
383 _resolveLookupPaths: {
384 value: _resolveLookupPaths,
385 writable: true,
386 enumerable: true,
387 },
388 _nodeModulePaths: {
389 value: _nodeModulePaths,
390 writable: true,
391 enumerable: true,
392 },
393 Module: {
394 value: Module,
395 writable: true,
396 enumerable: true,
397 },
398 registerHooks: {
399 value: registerHooks,
400 writable: true,
401 enumerable: true,
402 },
403});
404 
405export default Module;