Skip to content

Commit

Permalink
feat(node): complete node:module exports (#269)
Browse files Browse the repository at this point in the history
* feat(node): complete `node:module` exports

* update readme
  • Loading branch information
pi0 authored Jun 18, 2024
1 parent c82ed3e commit 3ab6840
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const envConfig = env(nodeless, vercel, {});
- [node:https](https://nodejs.org/api/https.html) - ✅ polyfilled all exports
- [node:inspector](https://nodejs.org/api/inspector.html) - ✅ polyfilled all exports
- [node:inspector/promises](https://nodejs.org/api/inspector.html) - 🚧 mocked using proxy
- [node:module](https://nodejs.org/api/module.html) - ✅ polyfilled 9/21 exports
- [node:module](https://nodejs.org/api/module.html) - ✅ polyfilled all exports
- [node:net](https://nodejs.org/api/net.html) - ✅ polyfilled 14/18 exports
- [node:os](https://nodejs.org/api/os.html) - ✅ polyfilled all exports
- [node:path](https://nodejs.org/api/path.html) - ✅ polyfilled all exports
Expand Down
142 changes: 114 additions & 28 deletions src/runtime/node/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,137 @@
import type nodeModule from "node:module";
import { notImplemented, notImplementedClass } from "../../_internal/utils";

export const builtinModules: typeof nodeModule.builtinModules = [];
export const _cache = Object.create(null);

export const createRequire = notImplemented(
"module.createRequire",
) as typeof nodeModule.createRequire;
export const _extensions = {
".js": notImplemented("module.require.extensions['.js']"),
".json": notImplemented("module.require.extensions['.json']"),
".node": notImplemented("module.require.extensions['.node']"),
};

export const createRequire = function (filename: string) {
return Object.assign(notImplemented("module.require"), {
resolve: Object.assign(notImplemented("module.require.resolve"), {
paths: notImplemented("module.require.resolve.paths"),
}),
cache: Object.create(null),
extensions: _extensions,
main: undefined,
});
};

export const runMain = notImplemented(
"module.runMain",
) as typeof nodeModule.runMain;
// prettier-ignore
export const builtinModules: typeof nodeModule.builtinModules = [
'_http_agent', '_http_client', '_http_common',
'_http_incoming', '_http_outgoing', '_http_server',
'_stream_duplex', '_stream_passthrough', '_stream_readable',
'_stream_transform', '_stream_wrap', '_stream_writable',
'_tls_common', '_tls_wrap', 'assert',
'assert/strict', 'async_hooks', 'buffer',
'child_process', 'cluster', 'console',
'constants', 'crypto', 'dgram',
'diagnostics_channel', 'dns', 'dns/promises',
'domain', 'events', 'fs',
'fs/promises', 'http', 'http2',
'https', 'inspector', 'inspector/promises',
'module', 'net', 'os',
'path', 'path/posix', 'path/win32',
'perf_hooks', 'process', 'punycode',
'querystring', 'readline', 'readline/promises',
'repl', 'stream', 'stream/consumers',
'stream/promises', 'stream/web', 'string_decoder',
'sys', 'timers', 'timers/promises',
'tls', 'trace_events', 'tty',
'url', 'util', 'util/types',
'v8', 'vm', 'wasi',
'worker_threads', 'zlib'
];

export const isBuiltin: typeof nodeModule.isBuiltin = function (id) {
return id.startsWith("node:") || builtinModules.includes(id);
};

export const isBuiltin = notImplemented(
"module.isBuiltin",
) as typeof nodeModule.isBuiltin;
export const runMain: typeof nodeModule.runMain =
notImplemented("module.runMain");

export const register = notImplemented(
"module.register",
) as typeof nodeModule.register;
export const register: typeof nodeModule.register =
notImplemented("module.register");

export const syncBuiltinESMExports = notImplemented(
"module.syncBuiltinESMExports",
) as typeof nodeModule.syncBuiltinESMExports;
export const syncBuiltinESMExports: typeof nodeModule.syncBuiltinESMExports =
function () {
return [];
};

export const findSourceMap = notImplemented(
"module.syncBuiltinESMExports",
"module.findSourceMap",
) as typeof nodeModule.findSourceMap;

export const wrap = notImplemented("module.wrap") as typeof nodeModule.wrap;

export const Module = notImplementedClass(
"module.Module",
) as typeof nodeModule.Module;
export const wrap: typeof nodeModule.wrap = function (source) {
return `(function (exports, require, module, __filename, __dirname) { ${source}\n});`;
};

export const SourceMap = notImplementedClass(
"module.SourceMap",
) as typeof nodeModule.SourceMap;

export default <typeof nodeModule>{
Module,
export const _debug = console.debug;

export const _findPath = notImplemented("module._findPath");
export const _initPaths = notImplemented("module._initPaths");
export const _load = notImplemented("module._load");
export const _nodeModulePaths = notImplemented("module._nodeModulePaths");
export const _preloadModules = notImplemented("module._preloadModules");
export const _resolveFilename = notImplemented("module._resolveFilename");
export const _resolveLookupPaths = notImplemented("module._resolveLookupPaths");

export const _pathCache = Object.create(null);
export const globalPaths = ["node_modules"];

export class _Module implements NodeJS.Module {
require: NodeJS.Require;
id: string;
filename: string;
exports = Object.create(null);
parent = undefined;
loaded = true;
children = [];
isPreloading: boolean = false;
path: string = "/";
paths: string[] = [];

constructor(id = "index.js") {
this.id = id;
this.filename = id;
this.require = createRequire(id);
}
}

export const Module = {
get Module() {
return Module;
},
SourceMap,
_cache,
_extensions,
_debug,
_pathCache,
_findPath,
_initPaths,
_load,
_nodeModulePaths,
_preloadModules,
_resolveFilename,
_resolveLookupPaths,
builtinModules,
createRequire,
runMain,
wrap,
findSourceMap,
globalPaths,
isBuiltin,
register,
runMain,
syncBuiltinESMExports,
findSourceMap,
};
wrap,
} satisfies Omit<typeof nodeModule.Module, "Module" | "prototype"> &
Record<string, any>;

export default Module;

0 comments on commit 3ab6840

Please sign in to comment.