File size: 3,521 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
// Manually “tree shaken” from:
// <https://github.com/nodejs/node/blob/7c3dce0/lib/internal/modules/esm/get_format.js>
// Last checked on: Apr 29, 2023.
import {fileURLToPath} from 'node:url'
import {getPackageType} from './package-json-reader.js'
import {codes} from './errors.js'
const {ERR_UNKNOWN_FILE_EXTENSION} = codes
const hasOwnProperty = {}.hasOwnProperty
/** @type {Record<string, string>} */
const extensionFormatMap = {
// @ts-expect-error: hush.
__proto__: null,
'.cjs': 'commonjs',
'.js': 'module',
'.json': 'json',
'.mjs': 'module'
}
/**
* @param {string | null} mime
* @returns {string | null}
*/
function mimeToFormat(mime) {
if (
mime &&
/\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?/i.test(mime)
)
return 'module'
if (mime === 'application/json') return 'json'
return null
}
/**
* @callback ProtocolHandler
* @param {URL} parsed
* @param {{parentURL: string, source?: Buffer}} context
* @param {boolean} ignoreErrors
* @returns {string | null | void}
*/
/**
* @type {Record<string, ProtocolHandler>}
*/
const protocolHandlers = {
// @ts-expect-error: hush.
__proto__: null,
'data:': getDataProtocolModuleFormat,
'file:': getFileProtocolModuleFormat,
'http:': getHttpProtocolModuleFormat,
'https:': getHttpProtocolModuleFormat,
'node:'() {
return 'builtin'
}
}
/**
* @param {URL} parsed
*/
function getDataProtocolModuleFormat(parsed) {
const {1: mime} = /^([^/]+\/[^;,]+)[^,]*?(;base64)?,/.exec(
parsed.pathname
) || [null, null, null]
return mimeToFormat(mime)
}
/**
* Returns the file extension from a URL.
*
* Should give similar result to
* `require('node:path').extname(require('node:url').fileURLToPath(url))`
* when used with a `file:` URL.
*
* @param {URL} url
* @returns {string}
*/
function extname(url) {
const pathname = url.pathname
let index = pathname.length
while (index--) {
const code = pathname.codePointAt(index)
if (code === 47 /* `/` */) {
return ''
}
if (code === 46 /* `.` */) {
return pathname.codePointAt(index - 1) === 47 /* `/` */
? ''
: pathname.slice(index)
}
}
return ''
}
/**
* @type {ProtocolHandler}
*/
function getFileProtocolModuleFormat(url, _context, ignoreErrors) {
const value = extname(url)
if (value === '.js') {
const packageType = getPackageType(url)
if (packageType !== 'none') {
return packageType
}
return 'commonjs'
}
if (value === '') {
const packageType = getPackageType(url)
// Legacy behavior
if (packageType === 'none' || packageType === 'commonjs') {
return 'commonjs'
}
// Note: we don’t implement WASM, so we don’t need
// `getFormatOfExtensionlessFile` from `formats`.
return 'module'
}
const format = extensionFormatMap[value]
if (format) return format
// Explicit undefined return indicates load hook should rerun format check
if (ignoreErrors) {
return undefined
}
const filepath = fileURLToPath(url)
throw new ERR_UNKNOWN_FILE_EXTENSION(value, filepath)
}
function getHttpProtocolModuleFormat() {
// To do: HTTPS imports.
}
/**
* @param {URL} url
* @param {{parentURL: string}} context
* @returns {string | null}
*/
export function defaultGetFormatWithoutErrors(url, context) {
const protocol = url.protocol
if (!hasOwnProperty.call(protocolHandlers, protocol)) {
return null
}
return protocolHandlers[protocol](url, context, true) || null
}
|