utils.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. * @param {URL} urlParsed parsed url
  125. * @returns {string} commithash
  126. */
  127. function getCommithash(urlParsed) {
  128. let { hostname, pathname, hash } = urlParsed;
  129. hostname = hostname.replace(/^www\./, "");
  130. try {
  131. hash = decodeURIComponent(hash);
  132. // eslint-disable-next-line no-empty
  133. } catch (_err) {}
  134. if (
  135. extractCommithashByDomain[
  136. /** @type {keyof extractCommithashByDomain} */ (hostname)
  137. ]
  138. ) {
  139. return (
  140. extractCommithashByDomain[
  141. /** @type {keyof extractCommithashByDomain} */ (hostname)
  142. ](pathname, hash) || ""
  143. );
  144. }
  145. return hash;
  146. }
  147. /**
  148. * make url right for URL parse
  149. * @param {string} gitUrl git url
  150. * @returns {string} fixed url
  151. */
  152. function correctUrl(gitUrl) {
  153. // like:
  154. // proto://hostname.com:user/repo -> proto://hostname.com/user/repo
  155. return gitUrl.replace(RE_HOSTNAME_WITH_COLON, "$1/$2");
  156. }
  157. /**
  158. * make url protocol right for URL parse
  159. * @param {string} gitUrl git url
  160. * @returns {string} fixed url
  161. */
  162. function correctProtocol(gitUrl) {
  163. // eg: github:foo/bar#v1.0. Should not add double slash, in case of error parsed `pathname`
  164. if (RE_GIT_URL_SHORT.test(gitUrl)) {
  165. return gitUrl;
  166. }
  167. // eg: user@github.com:foo/bar
  168. if (!RE_CUSTOM_PROTOCOL.test(gitUrl)) {
  169. return `${DEF_GIT_PROTOCOL}${gitUrl}`;
  170. }
  171. return gitUrl;
  172. }
  173. /**
  174. * extract git dep version from hash
  175. * @param {string} hash hash
  176. * @returns {string} git dep version
  177. */
  178. function getVersionFromHash(hash) {
  179. const matched = hash.match(RE_URL_HASH_VERSION);
  180. return (matched && matched[1]) || "";
  181. }
  182. /**
  183. * if string can be decoded
  184. * @param {string} str str to be checked
  185. * @returns {boolean} if can be decoded
  186. */
  187. function canBeDecoded(str) {
  188. try {
  189. decodeURIComponent(str);
  190. } catch (_err) {
  191. return false;
  192. }
  193. return true;
  194. }
  195. /**
  196. * get right dep version from git url
  197. * @param {string} gitUrl git url
  198. * @returns {string} dep version
  199. */
  200. function getGitUrlVersion(gitUrl) {
  201. const oriGitUrl = gitUrl;
  202. // github extreme shorthand
  203. gitUrl = RE_URL_GITHUB_EXTREME_SHORT.test(gitUrl)
  204. ? `github:${gitUrl}`
  205. : correctProtocol(gitUrl);
  206. gitUrl = correctUrl(gitUrl);
  207. /** @type {undefined | URL} */
  208. let parsed;
  209. try {
  210. parsed = new URL(gitUrl);
  211. // eslint-disable-next-line no-empty
  212. } catch (_err) {}
  213. if (!parsed) {
  214. return "";
  215. }
  216. const { protocol, hostname, pathname, username, password } = parsed;
  217. if (!RE_PROTOCOL.test(protocol)) {
  218. return "";
  219. }
  220. // pathname shouldn't be empty or URL malformed
  221. if (!pathname || !canBeDecoded(pathname)) {
  222. return "";
  223. }
  224. // without protocol, there should have auth info
  225. if (RE_NO_PROTOCOL.test(oriGitUrl) && !username && !password) {
  226. return "";
  227. }
  228. if (!PROTOCOLS_FOR_SHORT.includes(protocol.toLowerCase())) {
  229. if (!RE_HOSTNAME.test(hostname)) {
  230. return "";
  231. }
  232. const commithash = getCommithash(parsed);
  233. return getVersionFromHash(commithash) || commithash;
  234. }
  235. // for protocol short
  236. return getVersionFromHash(gitUrl);
  237. }
  238. /** @typedef {{ data: JsonObject, path: string }} DescriptionFile */
  239. /**
  240. * @param {InputFileSystem} fs file system
  241. * @param {string} directory directory to start looking into
  242. * @param {string[]} descriptionFiles possible description filenames
  243. * @param {(err?: Error | null, descriptionFile?: DescriptionFile, paths?: string[]) => void} callback callback
  244. * @param {(descriptionFile?: DescriptionFile) => boolean} satisfiesDescriptionFileData file data compliance check
  245. * @param {Set<string>} checkedFilePaths set of file paths that have been checked
  246. */
  247. const getDescriptionFile = (
  248. fs,
  249. directory,
  250. descriptionFiles,
  251. callback,
  252. satisfiesDescriptionFileData,
  253. checkedFilePaths = new Set()
  254. ) => {
  255. let i = 0;
  256. const satisfiesDescriptionFileDataInternal = {
  257. check: satisfiesDescriptionFileData,
  258. checkedFilePaths
  259. };
  260. const tryLoadCurrent = () => {
  261. if (i >= descriptionFiles.length) {
  262. const parentDirectory = dirname(fs, directory);
  263. if (!parentDirectory || parentDirectory === directory) {
  264. return callback(null, undefined, [
  265. ...satisfiesDescriptionFileDataInternal.checkedFilePaths
  266. ]);
  267. }
  268. return getDescriptionFile(
  269. fs,
  270. parentDirectory,
  271. descriptionFiles,
  272. callback,
  273. satisfiesDescriptionFileDataInternal.check,
  274. satisfiesDescriptionFileDataInternal.checkedFilePaths
  275. );
  276. }
  277. const filePath = join(fs, directory, descriptionFiles[i]);
  278. readJson(fs, filePath, (err, data) => {
  279. if (err) {
  280. if ("code" in err && err.code === "ENOENT") {
  281. i++;
  282. return tryLoadCurrent();
  283. }
  284. return callback(err);
  285. }
  286. if (!data || typeof data !== "object" || Array.isArray(data)) {
  287. return callback(
  288. new Error(`Description file ${filePath} is not an object`)
  289. );
  290. }
  291. if (
  292. typeof satisfiesDescriptionFileDataInternal.check === "function" &&
  293. !satisfiesDescriptionFileDataInternal.check({ data, path: filePath })
  294. ) {
  295. i++;
  296. satisfiesDescriptionFileDataInternal.checkedFilePaths.add(filePath);
  297. return tryLoadCurrent();
  298. }
  299. callback(null, { data, path: filePath });
  300. });
  301. };
  302. tryLoadCurrent();
  303. };
  304. module.exports.getDescriptionFile = getDescriptionFile;
  305. /**
  306. * @param {JsonObject} data description file data i.e.: package.json
  307. * @param {string} packageName name of the dependency
  308. * @returns {string | undefined} normalized version
  309. */
  310. const getRequiredVersionFromDescriptionFile = (data, packageName) => {
  311. const dependencyTypes = [
  312. "optionalDependencies",
  313. "dependencies",
  314. "peerDependencies",
  315. "devDependencies"
  316. ];
  317. for (const dependencyType of dependencyTypes) {
  318. const dependency = /** @type {JsonObject} */ (data[dependencyType]);
  319. if (
  320. dependency &&
  321. typeof dependency === "object" &&
  322. packageName in dependency
  323. ) {
  324. return normalizeVersion(
  325. /** @type {Exclude<JsonPrimitive, null | boolean | number>} */ (
  326. dependency[packageName]
  327. )
  328. );
  329. }
  330. }
  331. };
  332. module.exports.getRequiredVersionFromDescriptionFile =
  333. getRequiredVersionFromDescriptionFile;
  334. /**
  335. * @param {string} str maybe required version
  336. * @returns {boolean} true, if it looks like a version
  337. */
  338. function isRequiredVersion(str) {
  339. return VERSION_PATTERN_REGEXP.test(str);
  340. }
  341. module.exports.isRequiredVersion = isRequiredVersion;
  342. /**
  343. * @see https://docs.npmjs.com/cli/v7/configuring-npm/package-json#urls-as-dependencies
  344. * @param {string} versionDesc version to be normalized
  345. * @returns {string} normalized version
  346. */
  347. function normalizeVersion(versionDesc) {
  348. versionDesc = (versionDesc && versionDesc.trim()) || "";
  349. if (isRequiredVersion(versionDesc)) {
  350. return versionDesc;
  351. }
  352. // add handle for URL Dependencies
  353. return getGitUrlVersion(versionDesc.toLowerCase());
  354. }
  355. module.exports.normalizeVersion = normalizeVersion;