DescriptionFileUtils.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const forEachBail = require("./forEachBail");
  7. /** @typedef {import("./Resolver")} Resolver */
  8. /** @typedef {import("./Resolver").JsonObject} JsonObject */
  9. /** @typedef {import("./Resolver").JsonValue} JsonValue */
  10. /** @typedef {import("./Resolver").ResolveContext} ResolveContext */
  11. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  12. /**
  13. * @typedef {object} DescriptionFileInfo
  14. * @property {JsonObject=} content content
  15. * @property {string} path path
  16. * @property {string} directory directory
  17. */
  18. /**
  19. * @callback ErrorFirstCallback
  20. * @param {Error | null=} error
  21. * @param {DescriptionFileInfo=} result
  22. */
  23. /**
  24. * @typedef {object} Result
  25. * @property {string} path path to description file
  26. * @property {string} directory directory of description file
  27. * @property {JsonObject} content content of description file
  28. */
  29. const CHAR_SLASH = 47;
  30. const CHAR_BACKSLASH = 92;
  31. /**
  32. * Walk up one directory. Called once per package-root candidate and once per
  33. * `described-resolve` (to find the enclosing description file), so it's on
  34. * the resolver's hot path.
  35. *
  36. * Previous implementation called `lastIndexOf("/")` and `lastIndexOf("\\")`
  37. * separately and then picked the larger. For any non-trivial directory
  38. * string on POSIX, `lastIndexOf("\\")` scans the full string just to return
  39. * -1. A single reverse char-code scan does the same work in one pass.
  40. *
  41. * Any single-character directory is treated as a root — `directory.length
  42. * <= 1` collapses the `"/"`, `"\\"` and `""` branches into one compare.
  43. * Without the `"\\"` case, `cdUp("\\")` (reached from a UNC root or a DOS
  44. * device path like `\\?\…`) would return itself via `slice(0, i || 1)`
  45. * and trap `loadDescriptionFile` in an infinite loop. Once single-char
  46. * roots are filtered up front, the reverse scan always produces a
  47. * strictly shorter string.
  48. * @param {string} directory directory
  49. * @returns {string | null} parent directory or null
  50. */
  51. function cdUp(directory) {
  52. if (directory.length <= 1) return null;
  53. for (let i = directory.length - 1; i >= 0; i--) {
  54. const code = directory.charCodeAt(i);
  55. if (code === CHAR_SLASH || code === CHAR_BACKSLASH) {
  56. return directory.slice(0, i || 1);
  57. }
  58. }
  59. return null;
  60. }
  61. /**
  62. * @param {Resolver} resolver resolver
  63. * @param {string} directory directory
  64. * @param {string[]} filenames filenames
  65. * @param {DescriptionFileInfo | undefined} oldInfo oldInfo
  66. * @param {ResolveContext} resolveContext resolveContext
  67. * @param {ErrorFirstCallback} callback callback
  68. */
  69. function loadDescriptionFile(
  70. resolver,
  71. directory,
  72. filenames,
  73. oldInfo,
  74. resolveContext,
  75. callback,
  76. ) {
  77. // Hoist the per-filename iterator and the per-level done callback out
  78. // of `findDescriptionFile`. They both close over `directory`, which we
  79. // reassign as we walk up the tree, so the same closures keep working
  80. // across every level — the previous implementation re-allocated both
  81. // arrows on every recursion step, which adds up on deep walks (multiple
  82. // `DescriptionFilePlugin` taps per resolve, each climbing several
  83. // directories looking for `package.json`).
  84. /**
  85. * @param {string} filename filename
  86. * @param {(err?: null | Error, result?: null | Result) => void} iterCallback callback
  87. * @returns {void}
  88. */
  89. const iterFilename = (filename, iterCallback) => {
  90. const descriptionFilePath = resolver.join(directory, filename);
  91. /**
  92. * @param {(null | Error)=} err error
  93. * @param {JsonObject=} resolvedContent content
  94. * @returns {void}
  95. */
  96. function onJson(err, resolvedContent) {
  97. if (err) {
  98. if (resolveContext.log) {
  99. resolveContext.log(
  100. `${descriptionFilePath} (directory description file): ${err}`,
  101. );
  102. } else {
  103. err.message = `${descriptionFilePath} (directory description file): ${err}`;
  104. }
  105. return iterCallback(err);
  106. }
  107. iterCallback(null, {
  108. content: /** @type {JsonObject} */ (resolvedContent),
  109. directory,
  110. path: descriptionFilePath,
  111. });
  112. }
  113. if (resolver.fileSystem.readJson) {
  114. resolver.fileSystem.readJson(descriptionFilePath, (err, content) => {
  115. if (err) {
  116. if (
  117. typeof (/** @type {NodeJS.ErrnoException} */ (err).code) !==
  118. "undefined"
  119. ) {
  120. if (resolveContext.missingDependencies) {
  121. resolveContext.missingDependencies.add(descriptionFilePath);
  122. }
  123. return iterCallback();
  124. }
  125. if (resolveContext.fileDependencies) {
  126. resolveContext.fileDependencies.add(descriptionFilePath);
  127. }
  128. return onJson(err);
  129. }
  130. if (resolveContext.fileDependencies) {
  131. resolveContext.fileDependencies.add(descriptionFilePath);
  132. }
  133. onJson(null, content);
  134. });
  135. } else {
  136. resolver.fileSystem.readFile(descriptionFilePath, (err, content) => {
  137. if (err) {
  138. if (resolveContext.missingDependencies) {
  139. resolveContext.missingDependencies.add(descriptionFilePath);
  140. }
  141. return iterCallback();
  142. }
  143. if (resolveContext.fileDependencies) {
  144. resolveContext.fileDependencies.add(descriptionFilePath);
  145. }
  146. /** @type {JsonObject | undefined} */
  147. let json;
  148. if (content) {
  149. try {
  150. json = JSON.parse(content.toString());
  151. } catch (/** @type {unknown} */ err_) {
  152. return onJson(/** @type {Error} */ (err_));
  153. }
  154. } else {
  155. return onJson(new Error("No content in file"));
  156. }
  157. onJson(null, json);
  158. });
  159. }
  160. };
  161. // Forward-declared so the helpers below can reference each other
  162. // without falling foul of `no-use-before-define`.
  163. /** @type {() => void} */
  164. let findDescriptionFile;
  165. /**
  166. * @param {(null | Error)=} err error
  167. * @param {(null | Result)=} result result
  168. * @returns {void}
  169. */
  170. const onLevelDone = (err, result) => {
  171. if (err) return callback(err);
  172. if (result) return callback(null, result);
  173. const dir = cdUp(directory);
  174. if (!dir) {
  175. return callback();
  176. }
  177. directory = dir;
  178. return findDescriptionFile();
  179. };
  180. findDescriptionFile = () => {
  181. if (oldInfo && oldInfo.directory === directory) {
  182. // We already have info for this directory and can reuse it
  183. return callback(null, oldInfo);
  184. }
  185. forEachBail(filenames, iterFilename, onLevelDone);
  186. };
  187. findDescriptionFile();
  188. }
  189. /**
  190. * @param {JsonObject} content content
  191. * @param {string | string[]} field field
  192. * @returns {JsonValue | undefined} field data
  193. */
  194. function getField(content, field) {
  195. if (!content) return undefined;
  196. if (Array.isArray(field)) {
  197. /** @type {JsonValue} */
  198. let current = content;
  199. for (let j = 0; j < field.length; j++) {
  200. if (current === null || typeof current !== "object") {
  201. current = null;
  202. break;
  203. }
  204. current = /** @type {JsonValue} */ (
  205. /** @type {JsonObject} */
  206. (current)[field[j]]
  207. );
  208. }
  209. return current;
  210. }
  211. return content[field];
  212. }
  213. module.exports.cdUp = cdUp;
  214. module.exports.getField = getField;
  215. module.exports.loadDescriptionFile = loadDescriptionFile;