2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-09 15:06:01 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
2024-01-29 08:58:08 -05:00
|
|
|
import {
|
2024-09-10 16:12:36 -04:00
|
|
|
op_import_sync,
|
2024-01-29 16:02:26 -05:00
|
|
|
op_napi_open,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_require_as_file_path,
|
|
|
|
op_require_break_on_next_statement,
|
2024-09-10 16:12:36 -04:00
|
|
|
op_require_can_parse_as_esm,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_require_init_paths,
|
|
|
|
op_require_is_deno_dir_package,
|
|
|
|
op_require_is_request_relative,
|
|
|
|
op_require_node_module_paths,
|
|
|
|
op_require_package_imports_resolve,
|
|
|
|
op_require_path_basename,
|
|
|
|
op_require_path_dirname,
|
|
|
|
op_require_path_is_absolute,
|
|
|
|
op_require_path_resolve,
|
|
|
|
op_require_proxy_path,
|
2024-01-29 16:02:26 -05:00
|
|
|
op_require_read_closest_package_json,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_require_read_file,
|
|
|
|
op_require_read_package_scope,
|
|
|
|
op_require_real_path,
|
|
|
|
op_require_resolve_deno_dir,
|
|
|
|
op_require_resolve_exports,
|
|
|
|
op_require_resolve_lookup_paths,
|
|
|
|
op_require_stat,
|
|
|
|
op_require_try_self,
|
|
|
|
op_require_try_self_parent_path,
|
2024-01-29 08:58:08 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypeIndexOf,
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSplice,
|
2024-01-10 17:37:25 -05:00
|
|
|
Error,
|
|
|
|
JSONParse,
|
|
|
|
ObjectCreate,
|
|
|
|
ObjectEntries,
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectGetPrototypeOf,
|
2023-05-02 06:15:45 -04:00
|
|
|
ObjectHasOwn,
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectKeys,
|
|
|
|
ObjectPrototype,
|
2024-01-10 17:37:25 -05:00
|
|
|
ObjectSetPrototypeOf,
|
2023-02-07 14:22:46 -05:00
|
|
|
Proxy,
|
2024-01-10 17:37:25 -05:00
|
|
|
RegExpPrototypeTest,
|
|
|
|
SafeArrayIterator,
|
2023-02-07 14:22:46 -05:00
|
|
|
SafeMap,
|
|
|
|
SafeWeakMap,
|
|
|
|
String,
|
2024-01-10 17:37:25 -05:00
|
|
|
StringPrototypeCharCodeAt,
|
2023-02-07 14:22:46 -05:00
|
|
|
StringPrototypeEndsWith,
|
|
|
|
StringPrototypeIncludes,
|
2024-01-10 17:37:25 -05:00
|
|
|
StringPrototypeIndexOf,
|
2023-02-07 14:22:46 -05:00
|
|
|
StringPrototypeMatch,
|
|
|
|
StringPrototypeSlice,
|
|
|
|
StringPrototypeSplit,
|
|
|
|
StringPrototypeStartsWith,
|
|
|
|
TypeError,
|
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-07-31 15:45:32 -04:00
|
|
|
import { nodeGlobals } from "ext:deno_node/00_globals.js";
|
|
|
|
|
2024-09-11 20:52:08 -04:00
|
|
|
import _httpAgent from "node:_http_agent";
|
|
|
|
import _httpCommon from "node:_http_common";
|
|
|
|
import _httpOutgoing from "node:_http_outgoing";
|
|
|
|
import _httpServer from "node:_http_server";
|
|
|
|
import _streamDuplex from "node:_stream_duplex";
|
|
|
|
import _streamPassthrough from "node:_stream_passthrough";
|
|
|
|
import _streamReadable from "node:_stream_readable";
|
|
|
|
import _streamTransform from "node:_stream_transform";
|
|
|
|
import _streamWritable from "node:_stream_writable";
|
|
|
|
import _tlsCommon from "node:_tls_common";
|
|
|
|
import _tlsWrap from "node:_tls_wrap";
|
2023-07-02 14:19:30 -04:00
|
|
|
import assert from "node:assert";
|
|
|
|
import assertStrict from "node:assert/strict";
|
|
|
|
import asyncHooks from "node:async_hooks";
|
|
|
|
import buffer from "node:buffer";
|
|
|
|
import childProcess from "node:child_process";
|
|
|
|
import cluster from "node:cluster";
|
|
|
|
import console from "node:console";
|
|
|
|
import constants from "node:constants";
|
|
|
|
import crypto from "node:crypto";
|
|
|
|
import dgram from "node:dgram";
|
|
|
|
import diagnosticsChannel from "node:diagnostics_channel";
|
|
|
|
import dns from "node:dns";
|
|
|
|
import dnsPromises from "node:dns/promises";
|
|
|
|
import domain from "node:domain";
|
|
|
|
import events from "node:events";
|
|
|
|
import fs from "node:fs";
|
|
|
|
import fsPromises from "node:fs/promises";
|
|
|
|
import http from "node:http";
|
|
|
|
import http2 from "node:http2";
|
|
|
|
import https from "node:https";
|
2024-08-14 08:33:42 -04:00
|
|
|
import inspector from "node:inspector";
|
2024-09-17 13:27:19 -04:00
|
|
|
import inspectorPromises from "node:inspector/promises";
|
2023-03-20 14:05:13 -04:00
|
|
|
import internalCp from "ext:deno_node/internal/child_process.ts";
|
|
|
|
import internalCryptoCertificate from "ext:deno_node/internal/crypto/certificate.ts";
|
|
|
|
import internalCryptoCipher from "ext:deno_node/internal/crypto/cipher.ts";
|
|
|
|
import internalCryptoDiffiehellman from "ext:deno_node/internal/crypto/diffiehellman.ts";
|
|
|
|
import internalCryptoHash from "ext:deno_node/internal/crypto/hash.ts";
|
|
|
|
import internalCryptoHkdf from "ext:deno_node/internal/crypto/hkdf.ts";
|
|
|
|
import internalCryptoKeygen from "ext:deno_node/internal/crypto/keygen.ts";
|
|
|
|
import internalCryptoKeys from "ext:deno_node/internal/crypto/keys.ts";
|
|
|
|
import internalCryptoPbkdf2 from "ext:deno_node/internal/crypto/pbkdf2.ts";
|
|
|
|
import internalCryptoRandom from "ext:deno_node/internal/crypto/random.ts";
|
|
|
|
import internalCryptoScrypt from "ext:deno_node/internal/crypto/scrypt.ts";
|
|
|
|
import internalCryptoSig from "ext:deno_node/internal/crypto/sig.ts";
|
|
|
|
import internalCryptoUtil from "ext:deno_node/internal/crypto/util.ts";
|
|
|
|
import internalCryptoX509 from "ext:deno_node/internal/crypto/x509.ts";
|
|
|
|
import internalDgram from "ext:deno_node/internal/dgram.ts";
|
|
|
|
import internalDnsPromises from "ext:deno_node/internal/dns/promises.ts";
|
|
|
|
import internalErrors from "ext:deno_node/internal/errors.ts";
|
|
|
|
import internalEventTarget from "ext:deno_node/internal/event_target.mjs";
|
|
|
|
import internalFsUtils from "ext:deno_node/internal/fs/utils.mjs";
|
|
|
|
import internalHttp from "ext:deno_node/internal/http.ts";
|
|
|
|
import internalReadlineUtils from "ext:deno_node/internal/readline/utils.mjs";
|
|
|
|
import internalStreamsAddAbortSignal from "ext:deno_node/internal/streams/add-abort-signal.mjs";
|
|
|
|
import internalStreamsBufferList from "ext:deno_node/internal/streams/buffer_list.mjs";
|
|
|
|
import internalStreamsLazyTransform from "ext:deno_node/internal/streams/lazy_transform.mjs";
|
|
|
|
import internalStreamsState from "ext:deno_node/internal/streams/state.mjs";
|
|
|
|
import internalTestBinding from "ext:deno_node/internal/test/binding.ts";
|
|
|
|
import internalTimers from "ext:deno_node/internal/timers.mjs";
|
|
|
|
import internalUtil from "ext:deno_node/internal/util.mjs";
|
|
|
|
import internalUtilInspect from "ext:deno_node/internal/util/inspect.mjs";
|
2024-09-13 20:28:35 -04:00
|
|
|
import internalConsole from "ext:deno_node/internal/console/constructor.mjs";
|
2023-07-02 14:19:30 -04:00
|
|
|
import net from "node:net";
|
|
|
|
import os from "node:os";
|
|
|
|
import pathPosix from "node:path/posix";
|
|
|
|
import pathWin32 from "node:path/win32";
|
|
|
|
import path from "node:path";
|
|
|
|
import perfHooks from "node:perf_hooks";
|
|
|
|
import punycode from "node:punycode";
|
|
|
|
import process from "node:process";
|
|
|
|
import querystring from "node:querystring";
|
|
|
|
import readline from "node:readline";
|
2024-07-28 15:41:10 -04:00
|
|
|
import readlinePromises from "node:readline/promises";
|
2023-08-04 08:30:48 -04:00
|
|
|
import repl from "node:repl";
|
2023-07-02 14:19:30 -04:00
|
|
|
import stream from "node:stream";
|
|
|
|
import streamConsumers from "node:stream/consumers";
|
|
|
|
import streamPromises from "node:stream/promises";
|
|
|
|
import streamWeb from "node:stream/web";
|
|
|
|
import stringDecoder from "node:string_decoder";
|
|
|
|
import sys from "node:sys";
|
2023-08-01 19:17:38 -04:00
|
|
|
import test from "node:test";
|
2023-07-02 14:19:30 -04:00
|
|
|
import timers from "node:timers";
|
|
|
|
import timersPromises from "node:timers/promises";
|
|
|
|
import tls from "node:tls";
|
2024-09-14 22:45:09 -04:00
|
|
|
import traceEvents from "node:trace_events";
|
2023-07-02 14:19:30 -04:00
|
|
|
import tty from "node:tty";
|
|
|
|
import url from "node:url";
|
|
|
|
import utilTypes from "node:util/types";
|
|
|
|
import util from "node:util";
|
|
|
|
import v8 from "node:v8";
|
|
|
|
import vm from "node:vm";
|
|
|
|
import workerThreads from "node:worker_threads";
|
2024-08-21 23:33:52 -04:00
|
|
|
import wasi from "node:wasi";
|
2023-07-02 14:19:30 -04:00
|
|
|
import zlib from "node:zlib";
|
2023-03-20 14:05:13 -04:00
|
|
|
|
|
|
|
const nativeModuleExports = ObjectCreate(null);
|
|
|
|
const builtinModules = [];
|
|
|
|
|
2023-05-17 08:29:50 -04:00
|
|
|
// NOTE(bartlomieju): keep this list in sync with `ext/node/polyfill.rs`
|
2023-03-20 14:05:13 -04:00
|
|
|
function setupBuiltinModules() {
|
|
|
|
const nodeModules = {
|
|
|
|
"_http_agent": _httpAgent,
|
2024-09-11 20:52:08 -04:00
|
|
|
"_http_common": _httpCommon,
|
2023-03-20 14:05:13 -04:00
|
|
|
"_http_outgoing": _httpOutgoing,
|
2024-09-11 20:52:08 -04:00
|
|
|
"_http_server": _httpServer,
|
2023-03-20 14:05:13 -04:00
|
|
|
"_stream_duplex": _streamDuplex,
|
|
|
|
"_stream_passthrough": _streamPassthrough,
|
|
|
|
"_stream_readable": _streamReadable,
|
|
|
|
"_stream_transform": _streamTransform,
|
|
|
|
"_stream_writable": _streamWritable,
|
2024-09-11 20:52:08 -04:00
|
|
|
"_tls_common": _tlsCommon,
|
|
|
|
"_tls_wrap": _tlsWrap,
|
2023-03-20 14:05:13 -04:00
|
|
|
assert,
|
|
|
|
"assert/strict": assertStrict,
|
|
|
|
"async_hooks": asyncHooks,
|
|
|
|
buffer,
|
|
|
|
crypto,
|
|
|
|
console,
|
|
|
|
constants,
|
|
|
|
child_process: childProcess,
|
|
|
|
cluster,
|
|
|
|
dgram,
|
|
|
|
diagnostics_channel: diagnosticsChannel,
|
|
|
|
dns,
|
|
|
|
"dns/promises": dnsPromises,
|
|
|
|
domain,
|
|
|
|
events,
|
|
|
|
fs,
|
|
|
|
"fs/promises": fsPromises,
|
|
|
|
http,
|
|
|
|
http2,
|
|
|
|
https,
|
|
|
|
inspector,
|
2024-09-17 13:27:19 -04:00
|
|
|
"inspector/promises": inspectorPromises,
|
2024-09-13 20:28:35 -04:00
|
|
|
"internal/console/constructor": internalConsole,
|
2023-03-20 14:05:13 -04:00
|
|
|
"internal/child_process": internalCp,
|
|
|
|
"internal/crypto/certificate": internalCryptoCertificate,
|
|
|
|
"internal/crypto/cipher": internalCryptoCipher,
|
|
|
|
"internal/crypto/diffiehellman": internalCryptoDiffiehellman,
|
|
|
|
"internal/crypto/hash": internalCryptoHash,
|
|
|
|
"internal/crypto/hkdf": internalCryptoHkdf,
|
|
|
|
"internal/crypto/keygen": internalCryptoKeygen,
|
|
|
|
"internal/crypto/keys": internalCryptoKeys,
|
|
|
|
"internal/crypto/pbkdf2": internalCryptoPbkdf2,
|
|
|
|
"internal/crypto/random": internalCryptoRandom,
|
|
|
|
"internal/crypto/scrypt": internalCryptoScrypt,
|
|
|
|
"internal/crypto/sig": internalCryptoSig,
|
|
|
|
"internal/crypto/util": internalCryptoUtil,
|
|
|
|
"internal/crypto/x509": internalCryptoX509,
|
|
|
|
"internal/dgram": internalDgram,
|
|
|
|
"internal/dns/promises": internalDnsPromises,
|
|
|
|
"internal/errors": internalErrors,
|
|
|
|
"internal/event_target": internalEventTarget,
|
|
|
|
"internal/fs/utils": internalFsUtils,
|
|
|
|
"internal/http": internalHttp,
|
|
|
|
"internal/readline/utils": internalReadlineUtils,
|
|
|
|
"internal/streams/add-abort-signal": internalStreamsAddAbortSignal,
|
|
|
|
"internal/streams/buffer_list": internalStreamsBufferList,
|
|
|
|
"internal/streams/lazy_transform": internalStreamsLazyTransform,
|
|
|
|
"internal/streams/state": internalStreamsState,
|
|
|
|
"internal/test/binding": internalTestBinding,
|
|
|
|
"internal/timers": internalTimers,
|
|
|
|
"internal/util/inspect": internalUtilInspect,
|
|
|
|
"internal/util": internalUtil,
|
|
|
|
net,
|
2024-04-05 07:38:00 -04:00
|
|
|
module: Module,
|
2023-03-20 14:05:13 -04:00
|
|
|
os,
|
|
|
|
"path/posix": pathPosix,
|
|
|
|
"path/win32": pathWin32,
|
|
|
|
path,
|
|
|
|
perf_hooks: perfHooks,
|
|
|
|
process,
|
|
|
|
get punycode() {
|
|
|
|
process.emitWarning(
|
|
|
|
"The `punycode` module is deprecated. Please use a userland " +
|
|
|
|
"alternative instead.",
|
|
|
|
"DeprecationWarning",
|
|
|
|
"DEP0040",
|
|
|
|
);
|
|
|
|
return punycode;
|
|
|
|
},
|
|
|
|
querystring,
|
|
|
|
readline,
|
|
|
|
"readline/promises": readlinePromises,
|
|
|
|
repl,
|
|
|
|
stream,
|
|
|
|
"stream/consumers": streamConsumers,
|
|
|
|
"stream/promises": streamPromises,
|
|
|
|
"stream/web": streamWeb,
|
|
|
|
string_decoder: stringDecoder,
|
|
|
|
sys,
|
2023-08-01 19:17:38 -04:00
|
|
|
test,
|
2023-03-20 14:05:13 -04:00
|
|
|
timers,
|
|
|
|
"timers/promises": timersPromises,
|
|
|
|
tls,
|
2024-09-14 22:45:09 -04:00
|
|
|
traceEvents,
|
2023-03-20 14:05:13 -04:00
|
|
|
tty,
|
|
|
|
url,
|
|
|
|
util,
|
|
|
|
"util/types": utilTypes,
|
|
|
|
v8,
|
|
|
|
vm,
|
|
|
|
wasi,
|
|
|
|
worker_threads: workerThreads,
|
|
|
|
zlib,
|
|
|
|
};
|
|
|
|
for (const [name, moduleExports] of ObjectEntries(nodeModules)) {
|
|
|
|
nativeModuleExports[name] = moduleExports;
|
|
|
|
ArrayPrototypePush(builtinModules, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setupBuiltinModules();
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
function pathDirname(filepath) {
|
2023-03-27 15:54:22 -04:00
|
|
|
if (filepath == null) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new Error("Empty filepath.");
|
2023-03-27 15:54:22 -04:00
|
|
|
} else if (filepath === "") {
|
|
|
|
return ".";
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_require_path_dirname(filepath);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function pathResolve(...args) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_require_path_resolve(args);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const nativeModulePolyfill = new SafeMap();
|
|
|
|
|
|
|
|
const relativeResolveCache = ObjectCreate(null);
|
|
|
|
let requireDepth = 0;
|
|
|
|
let statCache = null;
|
|
|
|
let mainModule = null;
|
|
|
|
let hasBrokenOnInspectBrk = false;
|
|
|
|
let hasInspectBrk = false;
|
2024-04-30 23:41:29 -04:00
|
|
|
// Are we running with --node-modules-dir flag or byonm?
|
2023-02-07 14:22:46 -05:00
|
|
|
let usesLocalNodeModulesDir = false;
|
|
|
|
|
|
|
|
function stat(filename) {
|
|
|
|
// TODO: required only on windows
|
|
|
|
// filename = path.toNamespacedPath(filename);
|
|
|
|
if (statCache !== null) {
|
|
|
|
const result = statCache.get(filename);
|
|
|
|
if (result !== undefined) {
|
|
|
|
return result;
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
const result = op_require_stat(filename);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (statCache !== null && result >= 0) {
|
|
|
|
statCache.set(filename, result);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return result;
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function updateChildren(parent, child, scan) {
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const children = parent.children;
|
|
|
|
if (children && !(scan && ArrayPrototypeIncludes(children, child))) {
|
|
|
|
ArrayPrototypePush(children, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryFile(requestPath, _isMain) {
|
|
|
|
const rc = stat(requestPath);
|
|
|
|
if (rc !== 0) return;
|
|
|
|
return toRealPath(requestPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryPackage(requestPath, exts, isMain, originalPath) {
|
|
|
|
const packageJsonPath = pathResolve(
|
|
|
|
requestPath,
|
|
|
|
"package.json",
|
|
|
|
);
|
2024-01-10 17:37:25 -05:00
|
|
|
const pkg = op_require_read_package_scope(packageJsonPath)?.main;
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!pkg) {
|
|
|
|
return tryExtensions(
|
|
|
|
pathResolve(requestPath, "index"),
|
|
|
|
exts,
|
|
|
|
isMain,
|
|
|
|
);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const filename = pathResolve(requestPath, pkg);
|
|
|
|
let actual = tryFile(filename, isMain) ||
|
|
|
|
tryExtensions(filename, exts, isMain) ||
|
|
|
|
tryExtensions(
|
|
|
|
pathResolve(filename, "index"),
|
|
|
|
exts,
|
|
|
|
isMain,
|
|
|
|
);
|
|
|
|
if (actual === false) {
|
|
|
|
actual = tryExtensions(
|
|
|
|
pathResolve(requestPath, "index"),
|
|
|
|
exts,
|
|
|
|
isMain,
|
2022-09-12 15:47:54 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!actual) {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
const err = new Error(
|
|
|
|
`Cannot find module '${filename}'. ` +
|
|
|
|
'Please verify that the package.json has a valid "main" entry',
|
2022-08-09 15:06:01 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
err.code = "MODULE_NOT_FOUND";
|
|
|
|
err.path = pathResolve(
|
|
|
|
requestPath,
|
|
|
|
"package.json",
|
2022-08-09 15:06:01 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
err.requestPath = originalPath;
|
|
|
|
throw err;
|
|
|
|
} else {
|
2023-07-19 04:30:04 -04:00
|
|
|
process.emitWarning(
|
2023-02-07 14:22:46 -05:00
|
|
|
`Invalid 'main' field in '${packageJsonPath}' of '${pkg}'. ` +
|
|
|
|
"Please either fix that or report it to the module author",
|
|
|
|
"DeprecationWarning",
|
|
|
|
"DEP0128",
|
2022-08-09 15:06:01 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
|
|
|
|
const realpathCache = new SafeMap();
|
|
|
|
function toRealPath(requestPath) {
|
|
|
|
const maybeCached = realpathCache.get(requestPath);
|
|
|
|
if (maybeCached) {
|
|
|
|
return maybeCached;
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
const rp = op_require_real_path(requestPath);
|
2023-02-07 14:22:46 -05:00
|
|
|
realpathCache.set(requestPath, rp);
|
|
|
|
return rp;
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function tryExtensions(p, exts, isMain) {
|
|
|
|
for (let i = 0; i < exts.length; i++) {
|
|
|
|
const filename = tryFile(p + exts[i], isMain);
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (filename) {
|
|
|
|
return filename;
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the longest (possibly multi-dot) extension registered in
|
|
|
|
// Module._extensions
|
|
|
|
function findLongestRegisteredExtension(filename) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const name = op_require_path_basename(filename);
|
2023-02-07 14:22:46 -05:00
|
|
|
let currentExtension;
|
|
|
|
let index;
|
|
|
|
let startIndex = 0;
|
|
|
|
while ((index = StringPrototypeIndexOf(name, ".", startIndex)) !== -1) {
|
|
|
|
startIndex = index + 1;
|
|
|
|
if (index === 0) continue; // Skip dotfiles like .gitignore
|
|
|
|
currentExtension = StringPrototypeSlice(name, index);
|
|
|
|
if (Module._extensions[currentExtension]) {
|
|
|
|
return currentExtension;
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return ".js";
|
|
|
|
}
|
|
|
|
|
|
|
|
function getExportsForCircularRequire(module) {
|
|
|
|
if (
|
|
|
|
module.exports &&
|
|
|
|
ObjectGetPrototypeOf(module.exports) === ObjectPrototype &&
|
|
|
|
// Exclude transpiled ES6 modules / TypeScript code because those may
|
|
|
|
// employ unusual patterns for accessing 'module.exports'. That should
|
|
|
|
// be okay because ES6 modules have a different approach to circular
|
|
|
|
// dependencies anyway.
|
|
|
|
!module.exports.__esModule
|
|
|
|
) {
|
|
|
|
// This is later unset once the module is done loading.
|
|
|
|
ObjectSetPrototypeOf(
|
|
|
|
module.exports,
|
|
|
|
CircularRequirePrototypeWarningProxy,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return module.exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
function emitCircularRequireWarning(prop) {
|
2023-07-19 04:30:04 -04:00
|
|
|
process.emitWarning(
|
2023-02-07 14:22:46 -05:00
|
|
|
`Accessing non-existent property '${String(prop)}' of module exports ` +
|
|
|
|
"inside circular dependency",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Proxy that can be used as the prototype of a module.exports object and
|
|
|
|
// warns when non-existent properties are accessed.
|
|
|
|
const CircularRequirePrototypeWarningProxy = new Proxy({}, {
|
|
|
|
get(target, prop) {
|
|
|
|
// Allow __esModule access in any case because it is used in the output
|
|
|
|
// of transpiled code to determine whether something comes from an
|
|
|
|
// ES module, and is not used as a regular key of `module.exports`.
|
|
|
|
if (prop in target || prop === "__esModule") return target[prop];
|
|
|
|
emitCircularRequireWarning(prop);
|
|
|
|
return undefined;
|
|
|
|
},
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
getOwnPropertyDescriptor(target, prop) {
|
2022-08-09 15:06:01 -04:00
|
|
|
if (
|
2023-05-02 06:15:45 -04:00
|
|
|
ObjectHasOwn(target, prop) || prop === "__esModule"
|
2022-08-09 15:06:01 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
return ObjectGetOwnPropertyDescriptor(target, prop);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
emitCircularRequireWarning(prop);
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const moduleParentCache = new SafeWeakMap();
|
|
|
|
function Module(id = "", parent) {
|
|
|
|
this.id = id;
|
|
|
|
this.path = pathDirname(id);
|
|
|
|
this.exports = {};
|
|
|
|
moduleParentCache.set(this, parent);
|
|
|
|
updateChildren(parent, this, false);
|
|
|
|
this.filename = null;
|
|
|
|
this.loaded = false;
|
2024-06-25 14:17:35 -04:00
|
|
|
this.parent = parent;
|
2023-02-07 14:22:46 -05:00
|
|
|
this.children = [];
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:05:13 -04:00
|
|
|
Module.builtinModules = builtinModules;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
Module._extensions = ObjectCreate(null);
|
|
|
|
Module._cache = ObjectCreate(null);
|
|
|
|
Module._pathCache = ObjectCreate(null);
|
|
|
|
let modulePaths = [];
|
|
|
|
Module.globalPaths = modulePaths;
|
|
|
|
|
|
|
|
const CHAR_FORWARD_SLASH = 47;
|
|
|
|
const TRAILING_SLASH_REGEX = /(?:^|\/)\.?\.$/;
|
|
|
|
|
|
|
|
// This only applies to requests of a specific form:
|
|
|
|
// 1. name/.*
|
|
|
|
// 2. @scope/name/.*
|
|
|
|
const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/;
|
|
|
|
function resolveExports(
|
|
|
|
modulesPath,
|
|
|
|
request,
|
|
|
|
parentPath,
|
|
|
|
usesLocalNodeModulesDir,
|
|
|
|
) {
|
|
|
|
// The implementation's behavior is meant to mirror resolution in ESM.
|
|
|
|
const [, name, expansion = ""] =
|
|
|
|
StringPrototypeMatch(request, EXPORTS_PATTERN) || [];
|
|
|
|
if (!name) {
|
|
|
|
return;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_require_resolve_exports(
|
2023-02-07 14:22:46 -05:00
|
|
|
usesLocalNodeModulesDir,
|
2022-12-19 11:09:54 -05:00
|
|
|
modulesPath,
|
|
|
|
request,
|
2023-02-07 14:22:46 -05:00
|
|
|
name,
|
|
|
|
expansion,
|
2024-10-31 13:02:31 -04:00
|
|
|
parentPath ?? "",
|
2023-02-07 14:22:46 -05:00
|
|
|
) ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Module._findPath = function (request, paths, isMain, parentPath) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const absoluteRequest = op_require_path_is_absolute(request);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (absoluteRequest) {
|
|
|
|
paths = [""];
|
|
|
|
} else if (!paths || paths.length === 0) {
|
|
|
|
return false;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const cacheKey = request + "\x00" + ArrayPrototypeJoin(paths, "\x00");
|
|
|
|
const entry = Module._pathCache[cacheKey];
|
|
|
|
if (entry) {
|
|
|
|
return entry;
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let exts;
|
|
|
|
let trailingSlash = request.length > 0 &&
|
|
|
|
StringPrototypeCharCodeAt(request, request.length - 1) ===
|
|
|
|
CHAR_FORWARD_SLASH;
|
|
|
|
if (!trailingSlash) {
|
|
|
|
trailingSlash = RegExpPrototypeTest(TRAILING_SLASH_REGEX, request);
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// For each path
|
|
|
|
for (let i = 0; i < paths.length; i++) {
|
|
|
|
// Don't search further if path doesn't exist
|
|
|
|
const curPath = paths[i];
|
|
|
|
if (curPath && stat(curPath) < 1) continue;
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!absoluteRequest) {
|
|
|
|
const exportsResolved = resolveExports(
|
2022-08-20 11:31:33 -04:00
|
|
|
curPath,
|
|
|
|
request,
|
2023-02-07 14:22:46 -05:00
|
|
|
parentPath,
|
|
|
|
usesLocalNodeModulesDir,
|
2022-08-20 11:31:33 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (exportsResolved) {
|
|
|
|
return exportsResolved;
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-04-24 17:08:11 -04:00
|
|
|
let basePath;
|
|
|
|
|
|
|
|
if (usesLocalNodeModulesDir) {
|
|
|
|
basePath = pathResolve(curPath, request);
|
|
|
|
} else {
|
2024-01-10 17:37:25 -05:00
|
|
|
const isDenoDirPackage = op_require_is_deno_dir_package(
|
2023-04-24 17:08:11 -04:00
|
|
|
curPath,
|
|
|
|
);
|
2024-01-10 17:37:25 -05:00
|
|
|
const isRelative = op_require_is_request_relative(
|
2023-04-24 17:08:11 -04:00
|
|
|
request,
|
|
|
|
);
|
|
|
|
basePath = (isDenoDirPackage && !isRelative)
|
|
|
|
? pathResolve(curPath, packageSpecifierSubPath(request))
|
|
|
|
: pathResolve(curPath, request);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
let filename;
|
|
|
|
|
|
|
|
const rc = stat(basePath);
|
|
|
|
if (!trailingSlash) {
|
|
|
|
if (rc === 0) { // File.
|
|
|
|
filename = toRealPath(basePath);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!filename) {
|
|
|
|
// Try it with each of the extensions
|
2022-08-09 15:06:01 -04:00
|
|
|
if (exts === undefined) {
|
|
|
|
exts = ObjectKeys(Module._extensions);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
filename = tryExtensions(basePath, exts, isMain);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!filename && rc === 1) { // Directory.
|
|
|
|
// try it with each of the extensions at "index"
|
|
|
|
if (exts === undefined) {
|
|
|
|
exts = ObjectKeys(Module._extensions);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
filename = tryPackage(basePath, exts, isMain, request);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (filename) {
|
|
|
|
Module._pathCache[cacheKey] = filename;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return false;
|
|
|
|
};
|
2022-08-09 15:06:01 -04:00
|
|
|
|
fix(node): duplicate node_module suffixes (#19222)
Noticed that we're checking more module paths than necessary. In
particular the module path array contains a couple of entries with a
duplicated `node_modules/node_modules` suffix.
```js
[
// ... more entries before here, where some also contain duplicate suffixes
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/.deno/node_modules",
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/node_modules", // <-- duplicate suffix
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules",
"/Users/marvinhagemeister/dev/node_modules",
"/Users/marvinhagemeister/node_modules",
"/Users/node_modules",
"/node_modules",
"/node_modules" // <-- duplicate entry
]
```
This was caused by a misunderstanding in how Rust's
[`Path::ends_with()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.ends_with)
works. It's designed to match on whole path segments and the suffix
`/node_modules` is not that, except for the root entry. This meant that
our check for if the path already ended with `node_module` always
returned `false`. Removing the leading slash fixes that.
While we're at it, we can remove the last condition where we explicitly
added the root `/node_modules` entry since the while loop prior to that
takes care of it already.
2023-05-23 06:46:14 -04:00
|
|
|
/**
|
|
|
|
* Get a list of potential module directories
|
|
|
|
* @param {string} fromPath The directory name of the module
|
|
|
|
* @returns {string[]} List of module directories
|
|
|
|
*/
|
2023-02-07 14:22:46 -05:00
|
|
|
Module._nodeModulePaths = function (fromPath) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_require_node_module_paths(fromPath);
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2022-11-05 19:58:21 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module._resolveLookupPaths = function (request, parent) {
|
|
|
|
const paths = [];
|
2022-11-05 19:58:21 -04:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
if (op_require_is_request_relative(request)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(
|
|
|
|
paths,
|
2024-01-10 17:37:25 -05:00
|
|
|
parent?.filename ? op_require_path_dirname(parent.filename) : ".",
|
2022-08-23 10:39:19 -04:00
|
|
|
);
|
2022-08-20 11:31:33 -04:00
|
|
|
return paths;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-04-24 17:08:11 -04:00
|
|
|
if (
|
|
|
|
!usesLocalNodeModulesDir && parent?.filename && parent.filename.length > 0
|
|
|
|
) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const denoDirPath = op_require_resolve_deno_dir(
|
2023-02-07 14:22:46 -05:00
|
|
|
request,
|
|
|
|
parent.filename,
|
|
|
|
);
|
|
|
|
if (denoDirPath) {
|
|
|
|
ArrayPrototypePush(paths, denoDirPath);
|
|
|
|
}
|
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
const lookupPathsResult = op_require_resolve_lookup_paths(
|
2023-02-07 14:22:46 -05:00
|
|
|
request,
|
|
|
|
parent?.paths,
|
|
|
|
parent?.filename ?? "",
|
|
|
|
);
|
|
|
|
if (lookupPathsResult) {
|
|
|
|
ArrayPrototypePush(paths, ...new SafeArrayIterator(lookupPathsResult));
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
};
|
|
|
|
|
|
|
|
Module._load = function (request, parent, isMain) {
|
|
|
|
let relResolveCacheIdentifier;
|
|
|
|
if (parent) {
|
|
|
|
// Fast path for (lazy loaded) modules in the same directory. The indirect
|
|
|
|
// caching is required to allow cache invalidation without changing the old
|
|
|
|
// cache key names.
|
|
|
|
relResolveCacheIdentifier = `${parent.path}\x00${request}`;
|
|
|
|
const filename = relativeResolveCache[relResolveCacheIdentifier];
|
|
|
|
if (filename !== undefined) {
|
|
|
|
const cachedModule = Module._cache[filename];
|
|
|
|
if (cachedModule !== undefined) {
|
|
|
|
updateChildren(parent, cachedModule, true);
|
|
|
|
if (!cachedModule.loaded) {
|
|
|
|
return getExportsForCircularRequire(cachedModule);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return cachedModule.exports;
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
delete relativeResolveCache[relResolveCacheIdentifier];
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const filename = Module._resolveFilename(request, parent, isMain);
|
|
|
|
if (StringPrototypeStartsWith(filename, "node:")) {
|
|
|
|
// Slice 'node:' prefix
|
|
|
|
const id = StringPrototypeSlice(filename, 5);
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const module = loadNativeModule(id, id);
|
|
|
|
if (!module) {
|
|
|
|
// TODO:
|
|
|
|
// throw new ERR_UNKNOWN_BUILTIN_MODULE(filename);
|
|
|
|
throw new Error("Unknown built-in module");
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return module.exports;
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const cachedModule = Module._cache[filename];
|
|
|
|
if (cachedModule !== undefined) {
|
|
|
|
updateChildren(parent, cachedModule, true);
|
|
|
|
if (!cachedModule.loaded) {
|
|
|
|
return getExportsForCircularRequire(cachedModule);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return cachedModule.exports;
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const mod = loadNativeModule(filename, request);
|
|
|
|
if (
|
|
|
|
mod
|
|
|
|
) {
|
|
|
|
return mod.exports;
|
|
|
|
}
|
|
|
|
// Don't call updateChildren(), Module constructor already does.
|
|
|
|
const module = cachedModule || new Module(filename, parent);
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (isMain) {
|
2023-07-19 04:30:04 -04:00
|
|
|
process.mainModule = module;
|
2023-02-07 14:22:46 -05:00
|
|
|
mainModule = module;
|
|
|
|
module.id = ".";
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module._cache[filename] = module;
|
|
|
|
if (parent !== undefined) {
|
|
|
|
relativeResolveCache[relResolveCacheIdentifier] = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
let threw = true;
|
|
|
|
try {
|
|
|
|
module.load(filename);
|
|
|
|
threw = false;
|
|
|
|
} finally {
|
|
|
|
if (threw) {
|
|
|
|
delete Module._cache[filename];
|
|
|
|
if (parent !== undefined) {
|
|
|
|
delete relativeResolveCache[relResolveCacheIdentifier];
|
|
|
|
const children = parent?.children;
|
|
|
|
if (ArrayIsArray(children)) {
|
|
|
|
const index = ArrayPrototypeIndexOf(children, module);
|
|
|
|
if (index !== -1) {
|
|
|
|
ArrayPrototypeSplice(children, index, 1);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (
|
|
|
|
module.exports &&
|
|
|
|
ObjectGetPrototypeOf(module.exports) ===
|
|
|
|
CircularRequirePrototypeWarningProxy
|
|
|
|
) {
|
|
|
|
ObjectSetPrototypeOf(module.exports, ObjectPrototype);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return module.exports;
|
|
|
|
};
|
|
|
|
|
|
|
|
Module._resolveFilename = function (
|
|
|
|
request,
|
|
|
|
parent,
|
|
|
|
isMain,
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
StringPrototypeStartsWith(request, "node:") ||
|
|
|
|
nativeModuleCanBeRequiredByUsers(request)
|
2022-08-09 15:06:01 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
return request;
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let paths;
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (typeof options === "object" && options !== null) {
|
|
|
|
if (ArrayIsArray(options.paths)) {
|
2024-04-30 23:41:29 -04:00
|
|
|
const isRelative = op_require_is_request_relative(request);
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (isRelative) {
|
|
|
|
paths = options.paths;
|
|
|
|
} else {
|
|
|
|
const fakeParent = new Module("", null);
|
|
|
|
paths = [];
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < options.paths.length; i++) {
|
|
|
|
const path = options.paths[i];
|
|
|
|
fakeParent.paths = Module._nodeModulePaths(path);
|
|
|
|
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let j = 0; j < lookupPaths.length; j++) {
|
|
|
|
if (!ArrayPrototypeIncludes(paths, lookupPaths[j])) {
|
|
|
|
ArrayPrototypePush(paths, lookupPaths[j]);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (options.paths === undefined) {
|
2022-08-09 15:06:01 -04:00
|
|
|
paths = Module._resolveLookupPaths(request, parent);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
// TODO:
|
|
|
|
// throw new ERR_INVALID_ARG_VALUE("options.paths", options.paths);
|
|
|
|
throw new Error("Invalid arg value options.paths", options.path);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
paths = Module._resolveLookupPaths(request, parent);
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (parent?.filename) {
|
|
|
|
if (request[0] === "#") {
|
2024-01-10 17:37:25 -05:00
|
|
|
const maybeResolved = op_require_package_imports_resolve(
|
2023-02-07 14:22:46 -05:00
|
|
|
parent.filename,
|
|
|
|
request,
|
|
|
|
);
|
|
|
|
if (maybeResolved) {
|
|
|
|
return maybeResolved;
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Try module self resolution first
|
2024-01-10 17:37:25 -05:00
|
|
|
const parentPath = op_require_try_self_parent_path(
|
2023-02-07 14:22:46 -05:00
|
|
|
!!parent,
|
|
|
|
parent?.filename,
|
|
|
|
parent?.id,
|
|
|
|
);
|
2024-01-10 17:37:25 -05:00
|
|
|
const selfResolved = op_require_try_self(parentPath, request);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (selfResolved) {
|
|
|
|
const cacheKey = request + "\x00" +
|
|
|
|
(paths.length === 1 ? paths[0] : ArrayPrototypeJoin(paths, "\x00"));
|
|
|
|
Module._pathCache[cacheKey] = selfResolved;
|
|
|
|
return selfResolved;
|
2022-08-11 10:44:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Look up the filename first, since that's the cache key.
|
|
|
|
const filename = Module._findPath(
|
|
|
|
request,
|
|
|
|
paths,
|
|
|
|
isMain,
|
|
|
|
parentPath,
|
|
|
|
);
|
2023-08-15 03:10:54 -04:00
|
|
|
if (filename) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_require_real_path(filename);
|
2023-08-15 03:10:54 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const requireStack = [];
|
|
|
|
for (let cursor = parent; cursor; cursor = moduleParentCache.get(cursor)) {
|
|
|
|
ArrayPrototypePush(requireStack, cursor.filename || cursor.id);
|
|
|
|
}
|
|
|
|
let message = `Cannot find module '${request}'`;
|
|
|
|
if (requireStack.length > 0) {
|
|
|
|
message = message + "\nRequire stack:\n- " +
|
|
|
|
ArrayPrototypeJoin(requireStack, "\n- ");
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
const err = new Error(message);
|
|
|
|
err.code = "MODULE_NOT_FOUND";
|
|
|
|
err.requireStack = requireStack;
|
2024-04-30 23:41:29 -04:00
|
|
|
|
|
|
|
// fallback and attempt to resolve bare specifiers using
|
|
|
|
// the global cache when not using --node-modules-dir
|
|
|
|
if (
|
|
|
|
!usesLocalNodeModulesDir &&
|
|
|
|
ArrayIsArray(options?.paths) &&
|
|
|
|
request[0] !== "." &&
|
|
|
|
request[0] !== "#" &&
|
|
|
|
!request.startsWith("file:///") &&
|
|
|
|
!op_require_is_request_relative(request) &&
|
|
|
|
!op_require_path_is_absolute(request)
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
return Module._resolveFilename(request, parent, isMain, {
|
|
|
|
...options,
|
|
|
|
paths: undefined,
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// throw the original error
|
2023-02-07 14:22:46 -05:00
|
|
|
throw err;
|
|
|
|
};
|
|
|
|
|
2023-03-27 15:54:22 -04:00
|
|
|
/**
|
|
|
|
* Internal CommonJS API to always require modules before requiring the actual
|
|
|
|
* one when calling `require("my-module")`. This is used by require hooks such
|
|
|
|
* as `ts-node/register`.
|
|
|
|
* @param {string[]} requests List of modules to preload
|
|
|
|
*/
|
|
|
|
Module._preloadModules = function (requests) {
|
|
|
|
if (!ArrayIsArray(requests) || requests.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parent = new Module("internal/preload", null);
|
|
|
|
// All requested files must be resolved against cwd
|
|
|
|
parent.paths = Module._nodeModulePaths(process.cwd());
|
|
|
|
for (let i = 0; i < requests.length; i++) {
|
|
|
|
parent.require(requests[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module.prototype.load = function (filename) {
|
2023-03-20 14:05:13 -04:00
|
|
|
if (this.loaded) {
|
2024-08-28 16:40:37 -04:00
|
|
|
throw new Error("Module already loaded");
|
2023-03-20 14:05:13 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 20:35:33 -04:00
|
|
|
// Canonicalize the path so it's not pointing to the symlinked directory
|
|
|
|
// in `node_modules` directory of the referrer.
|
2024-01-10 17:37:25 -05:00
|
|
|
this.filename = op_require_real_path(filename);
|
2023-02-07 14:22:46 -05:00
|
|
|
this.paths = Module._nodeModulePaths(
|
2023-05-01 20:35:33 -04:00
|
|
|
pathDirname(this.filename),
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
const extension = findLongestRegisteredExtension(filename);
|
2023-08-15 03:10:54 -04:00
|
|
|
Module._extensions[extension](this, this.filename);
|
2023-02-07 14:22:46 -05:00
|
|
|
this.loaded = true;
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO: do caching
|
|
|
|
};
|
2022-11-28 15:59:36 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Loads a module at the given file path. Returns that module's
|
|
|
|
// `exports` property.
|
|
|
|
Module.prototype.require = function (id) {
|
|
|
|
if (typeof id !== "string") {
|
|
|
|
// TODO(bartlomieju): it should use different error type
|
|
|
|
// ("ERR_INVALID_ARG_VALUE")
|
|
|
|
throw new TypeError("Invalid argument type");
|
|
|
|
}
|
2022-11-28 15:59:36 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (id === "") {
|
|
|
|
// TODO(bartlomieju): it should use different error type
|
|
|
|
// ("ERR_INVALID_ARG_VALUE")
|
|
|
|
throw new TypeError("id must be non empty");
|
|
|
|
}
|
|
|
|
requireDepth++;
|
|
|
|
try {
|
|
|
|
return Module._load(id, this, /* isMain */ false);
|
|
|
|
} finally {
|
|
|
|
requireDepth--;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-31 15:45:32 -04:00
|
|
|
// The module wrapper looks slightly different to Node. Instead of using one
|
|
|
|
// wrapper function, we use two. The first one exists to performance optimize
|
2024-09-04 05:04:06 -04:00
|
|
|
// access to magic node globals, like `Buffer`. The second one is the actual
|
|
|
|
// wrapper function we run the users code in. The only observable difference is
|
|
|
|
// that in Deno `arguments.callee` is not null.
|
2023-02-07 14:22:46 -05:00
|
|
|
Module.wrapper = [
|
2024-09-04 05:04:06 -04:00
|
|
|
"(function (exports, require, module, __filename, __dirname, Buffer, clearImmediate, clearInterval, clearTimeout, global, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {",
|
2023-07-31 15:45:32 -04:00
|
|
|
"\n}).call(this, exports, require, module, __filename, __dirname); })",
|
2023-02-07 14:22:46 -05:00
|
|
|
];
|
|
|
|
Module.wrap = function (script) {
|
|
|
|
script = script.replace(/^#!.*?\n/, "");
|
|
|
|
return `${Module.wrapper[0]}${script}${Module.wrapper[1]}`;
|
|
|
|
};
|
|
|
|
|
2023-07-17 16:19:00 -04:00
|
|
|
function isEsmSyntaxError(error) {
|
|
|
|
return error instanceof SyntaxError && (
|
|
|
|
StringPrototypeIncludes(
|
|
|
|
error.message,
|
|
|
|
"Cannot use import statement outside a module",
|
|
|
|
) ||
|
|
|
|
StringPrototypeIncludes(error.message, "Unexpected token 'export'")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function enrichCJSError(error) {
|
2023-07-17 16:19:00 -04:00
|
|
|
if (isEsmSyntaxError(error)) {
|
|
|
|
console.error(
|
|
|
|
'To load an ES module, set "type": "module" in the package.json or use ' +
|
|
|
|
"the .mjs extension.",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapSafe(
|
|
|
|
filename,
|
|
|
|
content,
|
|
|
|
cjsModuleInstance,
|
2024-07-31 19:22:34 -04:00
|
|
|
format,
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
const wrapper = Module.wrap(content);
|
2024-07-31 19:22:34 -04:00
|
|
|
const [f, err] = core.evalContext(
|
|
|
|
wrapper,
|
|
|
|
url.pathToFileURL(filename).toString(),
|
|
|
|
[format !== "module"],
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (err) {
|
2023-07-19 04:30:04 -04:00
|
|
|
if (process.mainModule === cjsModuleInstance) {
|
2023-02-07 14:22:46 -05:00
|
|
|
enrichCJSError(err.thrown);
|
|
|
|
}
|
2024-09-10 16:12:36 -04:00
|
|
|
throw err.thrown;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2024-07-31 19:22:34 -04:00
|
|
|
Module.prototype._compile = function (content, filename, format) {
|
|
|
|
if (format === "module") {
|
2024-09-10 16:12:36 -04:00
|
|
|
return loadESMFromCJS(this, filename, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
let compiledWrapper;
|
|
|
|
try {
|
|
|
|
compiledWrapper = wrapSafe(filename, content, this, format);
|
|
|
|
} catch (err) {
|
2024-09-20 00:10:34 -04:00
|
|
|
if (
|
|
|
|
format !== "commonjs" && err instanceof SyntaxError &&
|
|
|
|
op_require_can_parse_as_esm(content)
|
|
|
|
) {
|
2024-09-10 16:12:36 -04:00
|
|
|
return loadESMFromCJS(this, filename, content);
|
|
|
|
}
|
|
|
|
throw err;
|
2024-07-31 19:22:34 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const dirname = pathDirname(filename);
|
|
|
|
const require = makeRequireFunction(this);
|
|
|
|
const exports = this.exports;
|
|
|
|
const thisValue = exports;
|
|
|
|
if (requireDepth === 0) {
|
|
|
|
statCache = new SafeMap();
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (hasInspectBrk && !hasBrokenOnInspectBrk) {
|
|
|
|
hasBrokenOnInspectBrk = true;
|
2024-01-10 17:37:25 -05:00
|
|
|
op_require_break_on_next_statement();
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-07-31 15:45:32 -04:00
|
|
|
const {
|
|
|
|
Buffer,
|
|
|
|
clearImmediate,
|
|
|
|
clearInterval,
|
|
|
|
clearTimeout,
|
|
|
|
global,
|
|
|
|
setImmediate,
|
|
|
|
setInterval,
|
|
|
|
setTimeout,
|
|
|
|
performance,
|
|
|
|
} = nodeGlobals;
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const result = compiledWrapper.call(
|
|
|
|
thisValue,
|
|
|
|
exports,
|
|
|
|
require,
|
|
|
|
this,
|
|
|
|
filename,
|
|
|
|
dirname,
|
2023-07-31 15:45:32 -04:00
|
|
|
Buffer,
|
|
|
|
clearImmediate,
|
|
|
|
clearInterval,
|
|
|
|
clearTimeout,
|
|
|
|
global,
|
|
|
|
setImmediate,
|
|
|
|
setInterval,
|
|
|
|
setTimeout,
|
|
|
|
performance,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
if (requireDepth === 0) {
|
|
|
|
statCache = null;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
2022-08-29 13:48:27 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module._extensions[".js"] = function (module, filename) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const content = op_require_read_file(filename);
|
2022-08-29 13:48:27 -04:00
|
|
|
|
2024-07-31 19:22:34 -04:00
|
|
|
let format;
|
2023-02-07 14:22:46 -05:00
|
|
|
if (StringPrototypeEndsWith(filename, ".js")) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const pkg = op_require_read_closest_package_json(filename);
|
2024-09-20 00:10:34 -04:00
|
|
|
if (pkg?.type === "module") {
|
2024-07-31 19:22:34 -04:00
|
|
|
format = "module";
|
|
|
|
} else if (pkg?.type === "commonjs") {
|
|
|
|
format = "commonjs";
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 19:22:34 -04:00
|
|
|
module._compile(content, filename, format);
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
Module._extensions[".ts"] =
|
|
|
|
Module._extensions[".jsx"] =
|
|
|
|
Module._extensions[".tsx"] =
|
|
|
|
function (module, filename) {
|
|
|
|
const content = op_require_read_file(filename);
|
|
|
|
|
|
|
|
let format;
|
|
|
|
const pkg = op_require_read_closest_package_json(filename);
|
|
|
|
if (pkg?.type === "module") {
|
|
|
|
format = "module";
|
|
|
|
} else if (pkg?.type === "commonjs") {
|
|
|
|
format = "commonjs";
|
|
|
|
}
|
|
|
|
|
|
|
|
module._compile(content, filename, format);
|
|
|
|
};
|
|
|
|
|
|
|
|
Module._extensions[".cjs"] =
|
|
|
|
Module._extensions[".cts"] =
|
|
|
|
function (module, filename) {
|
|
|
|
const content = op_require_read_file(filename);
|
|
|
|
module._compile(content, filename, "commonjs");
|
|
|
|
};
|
|
|
|
|
2024-09-10 16:12:36 -04:00
|
|
|
function loadESMFromCJS(module, filename, code) {
|
|
|
|
const namespace = op_import_sync(
|
|
|
|
url.pathToFileURL(filename).toString(),
|
|
|
|
code,
|
|
|
|
);
|
2023-07-17 14:00:44 -04:00
|
|
|
|
2024-09-10 16:12:36 -04:00
|
|
|
module.exports = namespace;
|
2023-07-17 14:00:44 -04:00
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
Module._extensions[".mjs"] = Module._extensions[".mts"] = function (
|
|
|
|
module,
|
|
|
|
filename,
|
|
|
|
) {
|
2024-09-10 16:12:36 -04:00
|
|
|
loadESMFromCJS(module, filename);
|
|
|
|
};
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function stripBOM(content) {
|
|
|
|
if (StringPrototypeCharCodeAt(content, 0) === 0xfeff) {
|
|
|
|
content = StringPrototypeSlice(content, 1);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return content;
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Native extension for .json
|
|
|
|
Module._extensions[".json"] = function (module, filename) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const content = op_require_read_file(filename);
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
|
|
|
module.exports = JSONParse(stripBOM(content));
|
|
|
|
} catch (err) {
|
|
|
|
err.message = filename + ": " + err.message;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
};
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Native extension for .node
|
|
|
|
Module._extensions[".node"] = function (module, filename) {
|
2024-08-28 08:33:15 -04:00
|
|
|
if (filename.endsWith("cpufeatures.node")) {
|
|
|
|
throw new Error("Using cpu-features module is currently not supported");
|
|
|
|
}
|
2024-06-10 12:20:44 -04:00
|
|
|
module.exports = op_napi_open(
|
|
|
|
filename,
|
|
|
|
globalThis,
|
|
|
|
nodeGlobals.Buffer,
|
|
|
|
reportError,
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
function createRequireFromPath(filename) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const proxyPath = op_require_proxy_path(filename);
|
2023-02-07 14:22:46 -05:00
|
|
|
const mod = new Module(proxyPath);
|
|
|
|
mod.filename = proxyPath;
|
|
|
|
mod.paths = Module._nodeModulePaths(mod.path);
|
|
|
|
return makeRequireFunction(mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeRequireFunction(mod) {
|
|
|
|
const require = function require(path) {
|
|
|
|
return mod.require(path);
|
|
|
|
};
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function resolve(request, options) {
|
|
|
|
return Module._resolveFilename(request, mod, false, options);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
require.resolve = resolve;
|
2022-11-29 00:13:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function paths(request) {
|
|
|
|
return Module._resolveLookupPaths(request, mod);
|
2022-11-29 00:13:14 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
resolve.paths = paths;
|
|
|
|
require.main = mainModule;
|
|
|
|
// Enable support to add extra extension types.
|
|
|
|
require.extensions = Module._extensions;
|
|
|
|
require.cache = Module._cache;
|
|
|
|
|
|
|
|
return require;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches to:
|
|
|
|
// - /foo/...
|
|
|
|
// - \foo\...
|
|
|
|
// - C:/foo/...
|
|
|
|
// - C:\foo\...
|
|
|
|
const RE_START_OF_ABS_PATH = /^([/\\]|[a-zA-Z]:[/\\])/;
|
|
|
|
|
|
|
|
function isAbsolute(filenameOrUrl) {
|
|
|
|
return RE_START_OF_ABS_PATH.test(filenameOrUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRequire(filenameOrUrl) {
|
|
|
|
let fileUrlStr;
|
|
|
|
if (filenameOrUrl instanceof URL) {
|
|
|
|
if (filenameOrUrl.protocol !== "file:") {
|
|
|
|
throw new Error(
|
|
|
|
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
fileUrlStr = filenameOrUrl.toString();
|
|
|
|
} else if (typeof filenameOrUrl === "string") {
|
|
|
|
if (!filenameOrUrl.startsWith("file:") && !isAbsolute(filenameOrUrl)) {
|
2022-11-19 06:32:39 -05:00
|
|
|
throw new Error(
|
|
|
|
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
fileUrlStr = filenameOrUrl;
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ${filenameOrUrl}`,
|
|
|
|
);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2024-01-10 17:37:25 -05:00
|
|
|
const filename = op_require_as_file_path(fileUrlStr);
|
2023-02-07 14:22:46 -05:00
|
|
|
return createRequireFromPath(filename);
|
|
|
|
}
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2024-03-08 21:06:04 -05:00
|
|
|
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;
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module.createRequire = createRequire;
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module._initPaths = function () {
|
2024-01-10 17:37:25 -05:00
|
|
|
const paths = op_require_init_paths();
|
2023-02-07 14:22:46 -05:00
|
|
|
modulePaths = paths;
|
|
|
|
Module.globalPaths = ArrayPrototypeSlice(modulePaths);
|
|
|
|
};
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
|
|
|
|
throw new Error("not implemented");
|
|
|
|
};
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-05-10 18:13:45 -04:00
|
|
|
// Mostly used by tools like ts-node.
|
|
|
|
Module.runMain = function () {
|
|
|
|
Module._load(process.argv[1], null, true);
|
|
|
|
};
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
Module.Module = Module;
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-03-20 14:05:13 -04:00
|
|
|
nativeModuleExports.module = Module;
|
2022-08-09 15:06:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function loadNativeModule(_id, request) {
|
|
|
|
if (nativeModulePolyfill.has(request)) {
|
|
|
|
return nativeModulePolyfill.get(request);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-03-20 14:05:13 -04:00
|
|
|
const modExports = nativeModuleExports[request];
|
2023-02-07 14:22:46 -05:00
|
|
|
if (modExports) {
|
|
|
|
const nodeMod = new Module(request);
|
|
|
|
nodeMod.exports = modExports;
|
|
|
|
nodeMod.loaded = true;
|
|
|
|
nativeModulePolyfill.set(request, nodeMod);
|
|
|
|
return nodeMod;
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nativeModuleCanBeRequiredByUsers(request) {
|
2023-03-20 14:05:13 -04:00
|
|
|
return !!nativeModuleExports[request];
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function readPackageScope() {
|
|
|
|
throw new Error("not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param specifier {string} */
|
|
|
|
function packageSpecifierSubPath(specifier) {
|
|
|
|
let parts = StringPrototypeSplit(specifier, "/");
|
|
|
|
if (StringPrototypeStartsWith(parts[0], "@")) {
|
|
|
|
parts = ArrayPrototypeSlice(parts, 2);
|
|
|
|
} else {
|
|
|
|
parts = ArrayPrototypeSlice(parts, 1);
|
2022-08-09 15:06:01 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return ArrayPrototypeJoin(parts, "/");
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:05:13 -04:00
|
|
|
// This is a temporary namespace, that will be removed when initializing
|
|
|
|
// in `02_init.js`.
|
|
|
|
internals.requireImpl = {
|
2023-02-07 14:22:46 -05:00
|
|
|
setUsesLocalNodeModulesDir() {
|
|
|
|
usesLocalNodeModulesDir = true;
|
|
|
|
},
|
|
|
|
setInspectBrk() {
|
|
|
|
hasInspectBrk = true;
|
|
|
|
},
|
|
|
|
Module,
|
2023-03-20 14:05:13 -04:00
|
|
|
nativeModuleExports,
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2023-03-20 14:05:13 -04:00
|
|
|
|
2024-05-20 10:42:42 -04:00
|
|
|
/**
|
|
|
|
* @param {string} path
|
|
|
|
* @returns {SourceMap | undefined}
|
|
|
|
*/
|
|
|
|
export function findSourceMap(_path) {
|
|
|
|
// TODO(@marvinhagemeister): Stub implementation for now to unblock ava
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-11-04 16:08:29 -05:00
|
|
|
Module.findSourceMap = findSourceMap;
|
|
|
|
|
2024-08-09 08:25:33 -04:00
|
|
|
/**
|
|
|
|
* @param {string | URL} _specifier
|
|
|
|
* @param {string | URL} _parentUrl
|
|
|
|
* @param {{ parentURL: string | URL, data: any, transferList: any[] }} [_options]
|
|
|
|
*/
|
|
|
|
export function register(_specifier, _parentUrl, _options) {
|
|
|
|
// TODO(@marvinhagemeister): Stub implementation for programs registering
|
|
|
|
// TypeScript loaders. We don't support registering loaders for file
|
|
|
|
// types that Deno itself doesn't support at the moment.
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2024-03-08 21:06:04 -05:00
|
|
|
export { builtinModules, createRequire, isBuiltin, Module };
|
2023-03-20 14:05:13 -04:00
|
|
|
export const _cache = Module._cache;
|
|
|
|
export const _extensions = Module._extensions;
|
|
|
|
export const _findPath = Module._findPath;
|
|
|
|
export const _initPaths = Module._initPaths;
|
|
|
|
export const _load = Module._load;
|
|
|
|
export const _nodeModulePaths = Module._nodeModulePaths;
|
|
|
|
export const _pathCache = Module._pathCache;
|
|
|
|
export const _preloadModules = Module._preloadModules;
|
|
|
|
export const _resolveFilename = Module._resolveFilename;
|
|
|
|
export const _resolveLookupPaths = Module._resolveLookupPaths;
|
|
|
|
export const globalPaths = Module.globalPaths;
|
|
|
|
export const wrap = Module.wrap;
|
|
|
|
|
|
|
|
export default Module;
|