* Node.js default load hook. * @param {string} url * @param {LoadContext} context * @returns {LoadReturn}
(url, context = kEmptyObject)
| 60 | * @returns {LoadReturn} |
| 61 | */ |
| 62 | function 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`) |
no test coverage detected