utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { dirname, join, readJson } = require("../util/fs");
  7. /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
  8. /** @typedef {import("../util/fs").JsonObject} JsonObject */
  9. /** @typedef {import("../util/fs").JsonPrimitive} JsonPrimitive */
  10. // Extreme shorthand only for github. eg: foo/bar
  11. const RE_URL_GITHUB_EXTREME_SHORT = /^[^/@:.\s][^/@:\s]*\/[^@:\s]*[^/@:\s]#\S+/;
  12. // Short url with specific protocol. eg: github:foo/bar
  13. const RE_GIT_URL_SHORT = /^(github|gitlab|bitbucket|gist):\/?[^/.]+\/?/i;
  14. // Currently supported protocols
  15. const RE_PROTOCOL =
  16. /^((git\+)?(ssh|https?|file)|git|github|gitlab|bitbucket|gist):$/i;
  17. // Has custom protocol
  18. const RE_CUSTOM_PROTOCOL = /^((git\+)?(ssh|https?|file)|git):\/\//i;
  19. // Valid hash format for npm / yarn ...
  20. const RE_URL_HASH_VERSION = /#(?:semver:)?(.+)/;
  21. // Simple hostname validate
  22. const RE_HOSTNAME = /^(?:[^/.]+(\.[^/]+)+|localhost)$/;
  23. // For hostname with colon. eg: ssh://user@github.com:foo/bar
  24. const RE_HOSTNAME_WITH_COLON =
  25. /([^/@#:.]+(?:\.[^/@#:.]+)+|localhost):([^#/0-9]+)/;
  26. // Reg for url without protocol
  27. const RE_NO_PROTOCOL = /^([^/@#:.]+(?:\.[^/@#:.]+)+)/;
  28. // RegExp for version string
  29. const VERSION_PATTERN_REGEXP = /^([\d^=v<>~]|[*xX]$)/;
  30. // Specific protocol for short url without normal hostname
  31. const PROTOCOLS_FOR_SHORT = [
  32. "github:",
  33. "gitlab:",
  34. "bitbucket:",
  35. "gist:",
  36. "file:"
  37. ];
  38. // Default protocol for git url
  39. const DEF_GIT_PROTOCOL = "git+ssh://";
  40. // thanks to https://github.com/npm/hosted-git-info/blob/latest/git-host-info.js
  41. const extractCommithashByDomain = {
  42. /**
  43. * @param {string} pathname pathname
  44. * @param {string} hash hash
  45. * @returns {string | undefined} hash
  46. */
  47. "github.com": (pathname, hash) => {
  48. let [, user, project, type, commithash] = pathname.split("/", 5);
  49. if (type && type !== "tree") {
  50. return;
  51. }
  52. commithash = !type ? hash : `#${commithash}`;
  53. if (project && project.endsWith(".git")) {
  54. project = project.slice(0, -4);
  55. }
  56. if (!user || !project) {
  57. return;
  58. }
  59. return commithash;
  60. },
  61. /**
  62. * @param {string} pathname pathname
  63. * @param {string} hash hash
  64. * @returns {string | undefined} hash
  65. */
  66. "gitlab.com": (pathname, hash) => {
  67. const path = pathname.slice(1);
  68. if (path.includes("/-/") || path.includes("/archive.tar.gz")) {
  69. return;
  70. }
  71. const segments = path.split("/");
  72. let project = /** @type {string} */ (segments.pop());
  73. if (project.endsWith(".git")) {
  74. project = project.slice(0, -4);
  75. }
  76. const user = segments.join("/");
  77. if (!user || !project) {
  78. return;
  79. }
  80. return hash;
  81. },
  82. /**
  83. * @param {string} pathname pathname
  84. * @param {string} hash hash
  85. * @returns {string | undefined} hash
  86. */
  87. "bitbucket.org": (pathname, hash) => {
  88. let [, user, project, aux] = pathname.split("/", 4);
  89. if (["get"].includes(aux)) {
  90. return;
  91. }
  92. if (project && project.endsWith(".git")) {
  93. project = project.slice(0, -4);
  94. }
  95. if (!user || !project) {
  96. return;
  97. }
  98. return hash;
  99. },
  100. /**
  101. * @param {string} pathname pathname
  102. * @param {string} hash hash
  103. * @returns {string | undefined} hash
  104. */
  105. "gist.github.com": (pathname, hash) => {
  106. let [, user, project, aux] = pathname.split("/", 4);
  107. if (aux === "raw") {
  108. return;
  109. }
  110. if (!project) {
  111. if (!user) {
  112. return;
  113. }
  114. project = user;
  115. }
  116. if (project.endsWith(".git")) {
  117. project = project.slice(0, -4);
  118. }
  119. return hash;
  120. }
  121. };
  122. /**
  123. * extract commit hash from parsed url
  124. * @inner
  125. * @param {URL} urlParsed parsed url
  126. * @returns {string} commithash
  127. */
  128. function getCommithash(urlParsed) {
  129. let { hostname, pathname, hash } = urlParsed;
  130. hostname = hostname.replace(/^www\./, "");
  131. try {
  132. hash = decodeURIComponent(hash);
  133. // eslint-disable-next-line no-empty
  134. } catch (_err) {}
  135. if (
  136. extractCommithashByDomain[
  137. /** @type {keyof extractCommithashByDomain} */ (hostname)
  138. ]
  139. ) {
  140. return (
  141. extractCommithashByDomain[
  142. /** @type {keyof extractCommithashByDomain} */ (hostname)
  143. ](pathname, hash) || ""
  144. );
  145. }
  146. return hash;
  147. }
  148. /**
  149. * make url right for URL parse
  150. * @inner
  151. * @param {string} gitUrl git url
  152. * @returns {string} fixed url
  153. */
  154. function correctUrl(gitUrl) {
  155. // like:
  156. // proto://hostname.com:user/repo -> proto://hostname.com/user/repo
  157. return gitUrl.replace(RE_HOSTNAME_WITH_COLON, "$1/$2");
  158. }
  159. /**
  160. * make url protocol right for URL parse
  161. * @inner
  162. * @param {string} gitUrl git url
  163. * @returns {string} fixed url
  164. */
  165. function correctProtocol(gitUrl) {
  166. // eg: github:foo/bar#v1.0. Should not add double slash, in case of error parsed `pathname`
  167. if (RE_GIT_URL_SHORT.test(gitUrl)) {
  168. return gitUrl;
  169. }
  170. // eg: user@github.com:foo/bar
  171. if (!RE_CUSTOM_PROTOCOL.test(gitUrl)) {
  172. return `${DEF_GIT_PROTOCOL}${gitUrl}`;
  173. }
  174. return gitUrl;
  175. }
  176. /**
  177. * extract git dep version from hash
  178. * @inner
  179. * @param {string} hash hash
  180. * @returns {string} git dep version
  181. */
  182. function getVersionFromHash(hash) {
  183. const matched = hash.match(RE_URL_HASH_VERSION);
  184. return (matched && matched[1]) || "";
  185. }
  186. /**
  187. * if string can be decoded
  188. * @inner
  189. * @param {string} str str to be checked
  190. * @returns {boolean} if can be decoded
  191. */
  192. function canBeDecoded(str) {
  193. try {
  194. decodeURIComponent(str);
  195. } catch (_err) {
  196. return false;
  197. }
  198. return true;
  199. }
  200. /**
  201. * get right dep version from git url
  202. * @inner
  203. * @param {string} gitUrl git url
  204. * @returns {string} dep version
  205. */
  206. function getGitUrlVersion(gitUrl) {
  207. const oriGitUrl = gitUrl;
  208. // github extreme shorthand
  209. gitUrl = RE_URL_GITHUB_EXTREME_SHORT.test(gitUrl)
  210. ? `github:${gitUrl}`
  211. : correctProtocol(gitUrl);
  212. gitUrl = correctUrl(gitUrl);
  213. let parsed;
  214. try {
  215. parsed = new URL(gitUrl);
  216. // eslint-disable-next-line no-empty
  217. } catch (_err) {}
  218. if (!parsed) {
  219. return "";
  220. }
  221. const { protocol, hostname, pathname, username, password } = parsed;
  222. if (!RE_PROTOCOL.test(protocol)) {
  223. return "";
  224. }
  225. // pathname shouldn't be empty or URL malformed
  226. if (!pathname || !canBeDecoded(pathname)) {
  227. return "";
  228. }
  229. // without protocol, there should have auth info
  230. if (RE_NO_PROTOCOL.test(oriGitUrl) && !username && !password) {
  231. return "";
  232. }
  233. if (!PROTOCOLS_FOR_SHORT.includes(protocol.toLowerCase())) {
  234. if (!RE_HOSTNAME.test(hostname)) {
  235. return "";
  236. }
  237. const commithash = getCommithash(parsed);
  238. return getVersionFromHash(commithash) || commithash;
  239. }
  240. // for protocol short
  241. return getVersionFromHash(gitUrl);
  242. }
  243. /** @typedef {{ data: JsonObject, path: string }} DescriptionFile */
  244. /**
  245. * @param {InputFileSystem} fs file system
  246. * @param {string} directory directory to start looking into
  247. * @param {string[]} descriptionFiles possible description filenames
  248. * @param {(err?: Error | null, descriptionFile?: DescriptionFile, paths?: string[]) => void} callback callback
  249. * @param {(descriptionFile?: DescriptionFile) => boolean} satisfiesDescriptionFileData file data compliance check
  250. * @param {Set<string>} checkedFilePaths set of file paths that have been checked
  251. */
  252. const getDescriptionFile = (
  253. fs,
  254. directory,
  255. descriptionFiles,
  256. callback,
  257. satisfiesDescriptionFileData,
  258. checkedFilePaths = new Set()
  259. ) => {
  260. let i = 0;
  261. const satisfiesDescriptionFileDataInternal = {
  262. check: satisfiesDescriptionFileData,
  263. checkedFilePaths
  264. };
  265. const tryLoadCurrent = () => {
  266. if (i >= descriptionFiles.length) {
  267. const parentDirectory = dirname(fs, directory);
  268. if (!parentDirectory || parentDirectory === directory) {
  269. return callback(null, undefined, [
  270. ...satisfiesDescriptionFileDataInternal.checkedFilePaths
  271. ]);
  272. }
  273. return getDescriptionFile(
  274. fs,
  275. parentDirectory,
  276. descriptionFiles,
  277. callback,
  278. satisfiesDescriptionFileDataInternal.check,
  279. satisfiesDescriptionFileDataInternal.checkedFilePaths
  280. );
  281. }
  282. const filePath = join(fs, directory, descriptionFiles[i]);
  283. readJson(fs, filePath, (err, data) => {
  284. if (err) {
  285. if ("code" in err && err.code === "ENOENT") {
  286. i++;
  287. return tryLoadCurrent();
  288. }
  289. return callback(err);
  290. }
  291. if (!data || typeof data !== "object" || Array.isArray(data)) {
  292. return callback(
  293. new Error(`Description file ${filePath} is not an object`)
  294. );
  295. }
  296. if (
  297. typeof satisfiesDescriptionFileDataInternal.check === "function" &&
  298. !satisfiesDescriptionFileDataInternal.check({ data, path: filePath })
  299. ) {
  300. i++;
  301. satisfiesDescriptionFileDataInternal.checkedFilePaths.add(filePath);
  302. return tryLoadCurrent();
  303. }
  304. callback(null, { data, path: filePath });
  305. });
  306. };
  307. tryLoadCurrent();
  308. };
  309. module.exports.getDescriptionFile = getDescriptionFile;
  310. /**
  311. * @param {JsonObject} data description file data i.e.: package.json
  312. * @param {string} packageName name of the dependency
  313. * @returns {string | undefined} normalized version
  314. */
  315. const getRequiredVersionFromDescriptionFile = (data, packageName) => {
  316. const dependencyTypes = [
  317. "optionalDependencies",
  318. "dependencies",
  319. "peerDependencies",
  320. "devDependencies"
  321. ];
  322. for (const dependencyType of dependencyTypes) {
  323. const dependency = /** @type {JsonObject} */ (data[dependencyType]);
  324. if (
  325. dependency &&
  326. typeof dependency === "object" &&
  327. packageName in dependency
  328. ) {
  329. return normalizeVersion(
  330. /** @type {Exclude<JsonPrimitive, null | boolean| number>} */ (
  331. dependency[packageName]
  332. )
  333. );
  334. }
  335. }
  336. };
  337. module.exports.getRequiredVersionFromDescriptionFile =
  338. getRequiredVersionFromDescriptionFile;
  339. /**
  340. * @param {string} str maybe required version
  341. * @returns {boolean} true, if it looks like a version
  342. */
  343. function isRequiredVersion(str) {
  344. return VERSION_PATTERN_REGEXP.test(str);
  345. }
  346. module.exports.isRequiredVersion = isRequiredVersion;
  347. /**
  348. * @see https://docs.npmjs.com/cli/v7/configuring-npm/package-json#urls-as-dependencies
  349. * @param {string} versionDesc version to be normalized
  350. * @returns {string} normalized version
  351. */
  352. function normalizeVersion(versionDesc) {
  353. versionDesc = (versionDesc && versionDesc.trim()) || "";
  354. if (isRequiredVersion(versionDesc)) {
  355. return versionDesc;
  356. }
  357. // add handle for URL Dependencies
  358. return getGitUrlVersion(versionDesc.toLowerCase());
  359. }
  360. module.exports.normalizeVersion = normalizeVersion;