| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 657cbd4 commit 15c3655
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -135,7 +135,7 @@ const { BuiltinModule } = require('internal/bootstrap/realm'); | |||
| 135 | 135 | const { | |
| 136 | 136 | maybeCacheSourceMap, | |
| 137 | 137 | } = require('internal/source_map/source_map_cache'); | |
| 138 | - const { pathToFileURL, fileURLToPath, isURL } = require('internal/url'); | ||
| 138 | + const { pathToFileURL, fileURLToPath, isURL, URL } = require('internal/url'); | ||
| 139 | 139 | const { | |
| 140 | 140 | pendingDeprecate, | |
| 141 | 141 | emitExperimentalWarning, | |
@@ -1922,7 +1922,7 @@ Module._extensions['.node'] = function(module, filename) { | |||
| 1922 | 1922 | * @param {string} filename The path to the module | |
| 1923 | 1923 | * @returns {any} | |
| 1924 | 1924 | */ | |
| 1925 | - function createRequireFromPath(filename) { | ||
| 1925 | + function createRequireFromPath(filename, fileURL) { | ||
| 1926 | 1926 | // Allow a directory to be passed as the filename | |
| 1927 | 1927 | const trailingSlash = | |
| 1928 | 1928 | StringPrototypeEndsWith(filename, '/') || | |
@@ -1934,6 +1934,10 @@ function createRequireFromPath(filename) { | |||
| 1934 | 1934 | ||
| 1935 | 1935 | const m = new Module(proxyPath); | |
| 1936 | 1936 | m.filename = proxyPath; | |
| 1937 | + if (fileURL !== undefined) { | ||
| 1938 | + // Save the URL if createRequire() was given a URL, to preserve search params, if any. | ||
| 1939 | + m[kURL] = fileURL.href; | ||
| 1940 | + } | ||
| 1937 | 1941 | ||
| 1938 | 1942 | m.paths = Module._nodeModulePaths(m.path); | |
| 1939 | 1943 | return makeRequireFunction(m, null); | |
@@ -1944,28 +1948,32 @@ const createRequireError = 'must be a file URL object, file URL string, or ' + | |||
| 1944 | 1948 | ||
| 1945 | 1949 | /** | |
| 1946 | 1950 | * Creates a new `require` function that can be used to load modules. | |
| 1947 | - * @param {string | URL} filename The path or URL to the module context for this `require` | ||
| 1951 | + * @param {string | URL} filenameOrURL The path or URL to the module context for this `require` | ||
| 1948 | 1952 | * @throws {ERR_INVALID_ARG_VALUE} If `filename` is not a string or URL, or if it is a relative path that cannot be | |
| 1949 | 1953 | * resolved to an absolute path. | |
| 1950 | 1954 | * @returns {object} | |
| 1951 | 1955 | */ | |
| 1952 | - function createRequire(filename) { | ||
| 1953 | - let filepath; | ||
| 1956 | + function createRequire(filenameOrURL) { | ||
| 1957 | + let filepath, fileURL; | ||
| 1954 | 1958 | ||
| 1955 | - if (isURL(filename) || | ||
| 1956 | - (typeof filename === 'string' && !path.isAbsolute(filename))) { | ||
| 1959 | + if (isURL(filenameOrURL) || | ||
| 1960 | + (typeof filenameOrURL === 'string' && !path.isAbsolute(filenameOrURL))) { | ||
| 1957 | 1961 | try { | |
| 1958 | - filepath = fileURLToPath(filename); | ||
| 1962 | + // It might be an URL, try to convert it. | ||
| 1963 | + // If it's a relative path, it would not parse and would be considered invalid per | ||
| 1964 | + // the documented contract. | ||
| 1965 | + fileURL = new URL(filenameOrURL); | ||
| 1966 | + filepath = fileURLToPath(fileURL); | ||
| 1959 | 1967 | } catch { | |
| 1960 | - throw new ERR_INVALID_ARG_VALUE('filename', filename, | ||
| 1968 | + throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL, | ||
| 1961 | 1969 | createRequireError); | |
| 1962 | 1970 | } | |
| 1963 | - } else if (typeof filename !== 'string') { | ||
| 1964 | - throw new ERR_INVALID_ARG_VALUE('filename', filename, createRequireError); | ||
| 1971 | + } else if (typeof filenameOrURL !== 'string') { | ||
| 1972 | + throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL, createRequireError); | ||
| 1965 | 1973 | } else { | |
| 1966 | - filepath = filename; | ||
| 1974 | + filepath = filenameOrURL; | ||
| 1967 | 1975 | } | |
| 1968 | - return createRequireFromPath(filepath); | ||
| 1976 | + return createRequireFromPath(filepath, fileURL); | ||
| 1969 | 1977 | } | |
| 1970 | 1978 | ||
| 1971 | 1979 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + import { createRequire } from 'node:module' | ||
| 2 | + const require = createRequire(import.meta.url); | ||
| 3 | + require('./empty.mjs'); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,26 @@ | |||
| 1 | + // Verify that if URL is used to createRequire, that URL is passed to the resolve hook | ||
| 2 | + // as parentURL. | ||
| 3 | + import * as common from '../common/index.mjs'; | ||
| 4 | + import assert from 'node:assert'; | ||
| 5 | + import { registerHooks } from 'node:module'; | ||
| 6 | + import * as fixtures from '../common/fixtures.mjs'; | ||
| 7 | + | ||
| 8 | + const fixtureURL = fixtures.fileURL('module-hooks/create-require-with-url.mjs').href + '?test=1'; | ||
| 9 | + registerHooks({ | ||
| 10 | + resolve: common.mustCall((specifier, context, defaultResolve) => { | ||
| 11 | + const resolved = defaultResolve(specifier, context, defaultResolve); | ||
| 12 | + if (specifier.startsWith('node:')) { | ||
| 13 | + return resolved; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + if (specifier === fixtureURL) { | ||
| 17 | + assert.strictEqual(context.parentURL, import.meta.url); | ||
| 18 | + } else { // From the createRequire call. | ||
| 19 | + assert.strictEqual(specifier, './empty.mjs'); | ||
| 20 | + assert.strictEqual(context.parentURL, fixtureURL); | ||
| 21 | + } | ||
| 22 | + return resolved; | ||
| 23 | + }, 3), | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + await import(fixtureURL); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments