mirror of
https://github.com/nodejs/node.git
synced 2025-05-08 01:35:51 +00:00

Co-authored-by: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Co-authored-by: James M Snell <jasnell@gmail.com> Co-authored-by: Jordan Harband <ljharb@gmail.com> Co-authored-by: James Sumners <james@sumners.email> PR-URL: https://github.com/nodejs/node/pull/36328 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
const experimentalImportMetaResolve =
|
|
getOptionValue('--experimental-import-meta-resolve');
|
|
const { fetchModule } = require('internal/modules/esm/fetch_module');
|
|
const { URL } = require('internal/url');
|
|
const {
|
|
PromisePrototypeThen,
|
|
PromiseReject,
|
|
StringPrototypeStartsWith,
|
|
} = primordials;
|
|
const asyncESM = require('internal/process/esm_loader');
|
|
|
|
function createImportMetaResolve(defaultParentUrl) {
|
|
return async function resolve(specifier, parentUrl = defaultParentUrl) {
|
|
return PromisePrototypeThen(
|
|
asyncESM.esmLoader.resolve(specifier, parentUrl),
|
|
({ url }) => url,
|
|
(error) => (
|
|
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
|
|
error.url : PromiseReject(error))
|
|
);
|
|
};
|
|
}
|
|
|
|
function initializeImportMeta(meta, context) {
|
|
let url = context.url;
|
|
|
|
// Alphabetical
|
|
if (experimentalImportMetaResolve) {
|
|
meta.resolve = createImportMetaResolve(url);
|
|
}
|
|
|
|
if (
|
|
StringPrototypeStartsWith(url, 'http:') ||
|
|
StringPrototypeStartsWith(url, 'https:')
|
|
) {
|
|
// The request & response have already settled, so they are in fetchModule's
|
|
// cache, in which case, fetchModule returns immediately and synchronously
|
|
url = fetchModule(new URL(url), context).resolvedHREF;
|
|
}
|
|
|
|
meta.url = url;
|
|
}
|
|
|
|
module.exports = {
|
|
initializeImportMeta
|
|
};
|