UNPKG

nstdlib-nightly

Version:

Node.js standard library converted to runtime-agnostic ES modules.

72 lines (63 loc) 2.05 kB
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/modules/esm/formats.js import { getOptionValue } from "nstdlib/lib/internal/options"; import { getValidatedPath } from "nstdlib/lib/internal/fs/utils"; import * as fsBindings from "nstdlib/stub/binding/fs"; import { fs as fsConstants } from "nstdlib/stub/binding/constants"; const experimentalWasmModules = getOptionValue("--experimental-wasm-modules"); const extensionFormatMap = { __proto__: null, ".cjs": "commonjs", ".js": "module", ".json": "json", ".mjs": "module", }; if (experimentalWasmModules) { extensionFormatMap[".wasm"] = "wasm"; } if (getOptionValue("--experimental-strip-types")) { extensionFormatMap[".ts"] = "module-typescript"; extensionFormatMap[".mts"] = "module-typescript"; extensionFormatMap[".cts"] = "commonjs-typescript"; } /** * @param {string} mime * @returns {string | null} */ function mimeToFormat(mime) { if ( RegExp.prototype.exec.call( /^\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?$/i, mime, ) !== null ) { return "module"; } if (mime === "application/json") { return "json"; } if (experimentalWasmModules && mime === "application/wasm") { return "wasm"; } return null; } /** * For extensionless files in a `module` package scope, or a default `module` scope enabled by the * `--experimental-default-type` flag, we check the file contents to disambiguate between ES module JavaScript and Wasm. * We do this by taking advantage of the fact that all Wasm files start with the header `0x00 0x61 0x73 0x6d` (`_asm`). * @param {URL} url */ function getFormatOfExtensionlessFile(url) { if (!experimentalWasmModules) { return "module"; } const path = getValidatedPath(url); switch (fsBindings.getFormatOfExtensionlessFile(path)) { case fsConstants.EXTENSIONLESS_FORMAT_WASM: return "wasm"; default: return "module"; } } export { extensionFormatMap }; export { getFormatOfExtensionlessFile }; export { mimeToFormat };