MCPcopy Index your code
hub / github.com/nodejs/node / defaultLoad

Function defaultLoad

lib/internal/modules/esm/load.js:62–116  ·  view source on GitHub ↗

* Node.js default load hook. * @param {string} url * @param {LoadContext} context * @returns {LoadReturn}

(url, context = kEmptyObject)

Source from the content-addressed store, hash-verified

60 * @returns {LoadReturn}
61 */
62function defaultLoad(url, context = kEmptyObject) {
63 let responseURL = url;
64 let {
65 importAttributes,
66 format,
67 source,
68 } = context;
69
70 if (importAttributes == null && !('importAttributes' in context) && 'importAssertions' in context) {
71 emitImportAssertionWarning();
72 importAttributes = context.importAssertions;
73 // Alias `importAssertions` to `importAttributes`
74 context = {
75 ...context,
76 importAttributes,
77 };
78 }
79
80 const urlInstance = new URL(url);
81
82 throwIfUnsupportedURLScheme(urlInstance);
83
84 if (urlInstance.protocol === 'node:') {
85 source = null;
86 format ??= 'builtin';
87 } else if (format === 'addon') {
88 // Skip loading addon file content. It must be loaded with dlopen from file system.
89 source = null;
90 } else if (format !== 'commonjs') {
91 if (source == null) {
92 ({ responseURL, source } = getSourceSync(urlInstance, context));
93 context = { __proto__: context, source };
94 }
95
96 if (format == null) {
97 // Now that we have the source for the module, run `defaultGetFormat` to detect its format.
98 format = defaultGetFormat(urlInstance, context);
99
100 if (format === 'commonjs') {
101 // For backward compatibility reasons, we need to discard the source in
102 // order for the CJS loader to re-fetch it.
103 source = null;
104 }
105 }
106 }
107
108 validateAttributes(url, format, importAttributes);
109
110 return {
111 __proto__: null,
112 format,
113 responseURL,
114 source,
115 };
116}
117/**
118 * @typedef LoadContext
119 * @property {string} [format] A hint (possibly returned from `resolve`)

Callers 2

loadWithHooksFunction · 0.85
loadFunction · 0.85

Calls 5

getSourceSyncFunction · 0.85
defaultGetFormatFunction · 0.85
validateAttributesFunction · 0.85

Tested by

no test coverage detected