source: frontend/node_modules/enhanced-resolve/lib/DescriptionFileUtils.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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