gitea push

This commit is contained in:
2026-05-09 12:19:29 -06:00
parent 06113c95b8
commit 429461e985
1481 changed files with 74306 additions and 52475 deletions
+48 -61
View File
@@ -78,6 +78,7 @@ const slashCode = "/".charCodeAt(0);
const dotCode = ".".charCodeAt(0);
const hashCode = "#".charCodeAt(0);
const patternRegEx = /\*/g;
const DOLLAR_ESCAPE_RE = /\$/g;
/** @typedef {Record<string, MappingValue>} RecordMapping */
@@ -139,7 +140,12 @@ function getFieldKeyInfos(field) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const patternIndex = key.indexOf("*");
const lastStar = patternIndex === -1 ? -1 : key.lastIndexOf("*");
// `isValidPattern` is true when the key has at most one `*`. Searching
// from `patternIndex + 1` stops as soon as a second `*` is found, so
// we avoid the full-string scan that `lastIndexOf` would do — and the
// single-star common case finishes in one pass.
const isValidPattern =
patternIndex === -1 || !key.includes("*", patternIndex + 1);
const keyLen = key.length;
const endsWithSlash =
keyLen > 0 && key.charCodeAt(keyLen - 1) === slashCode;
@@ -151,7 +157,7 @@ function getFieldKeyInfos(field) {
isLegacySubpath: patternIndex === -1 && endsWithSlash,
isPattern: patternIndex !== -1,
isSubpathMapping: endsWithSlash,
isValidPattern: patternIndex === -1 || lastStar === patternIndex,
isValidPattern,
};
}
_fieldKeyInfoCache.set(fieldKey, infos);
@@ -286,16 +292,17 @@ function computeFindMatch(request, field) {
function findMatch(request, field) {
const fieldKey = /** @type {RecordMapping} */ (field);
let perRequest = _findMatchCache.get(fieldKey);
if (perRequest !== undefined) {
const cached = perRequest.get(request);
if (cached !== undefined) return cached;
// `null` is a valid cached value (= "no match"), so a `get(...)`
// that returns undefined could either mean "not cached yet" or
// "cached null". Do the explicit `has` only in the undefined case.
if (perRequest.has(request)) return null;
} else {
if (perRequest === undefined) {
perRequest = new Map();
_findMatchCache.set(fieldKey, perRequest);
} else {
// `computeFindMatch` only ever returns `MatchTuple | null` — never
// `undefined` — and `Map.set(k, null)` then `Map.get(k)` returns
// `null`, not `undefined`. So `get(...) === undefined` already
// unambiguously means "not cached yet"; one Map lookup is enough,
// no follow-up `has` needed to disambiguate "cached null".
const cached = perRequest.get(request);
if (cached !== undefined) return cached;
}
const result = computeFindMatch(request, field);
@@ -303,16 +310,6 @@ function findMatch(request, field) {
return result;
}
/**
* @param {ConditionalMapping | DirectMapping | null} mapping mapping
* @returns {boolean} is conditional mapping
*/
function isConditionalMapping(mapping) {
return (
mapping !== null && typeof mapping === "object" && !Array.isArray(mapping)
);
}
/**
* Sentinel stored in the conditional-mapping cache for inputs whose walk
* returns `null` ("no condition matched"). Using a non-null marker lets the
@@ -352,35 +349,22 @@ function computeConditionalMapping(conditionalMapping_, conditionNames) {
const lookup = [[conditionalMapping_, cachedKeys(conditionalMapping_), 0]];
loop: while (lookup.length > 0) {
const [mapping, conditions, j] = lookup[lookup.length - 1];
const top = lookup[lookup.length - 1];
const [mapping, conditions, j] = top;
for (let i = j; i < conditions.length; i++) {
const condition = conditions[i];
if (condition === "default") {
if (condition === "default" || conditionNames.has(condition)) {
const innerMapping = mapping[condition];
// is nested
if (isConditionalMapping(innerMapping)) {
const conditionalMapping = /** @type {ConditionalMapping} */ (
innerMapping
);
lookup[lookup.length - 1][2] = i + 1;
lookup.push([conditionalMapping, cachedKeys(conditionalMapping), 0]);
continue loop;
}
return /** @type {DirectMapping} */ (innerMapping);
}
if (conditionNames.has(condition)) {
const innerMapping = mapping[condition];
// is nested
if (isConditionalMapping(innerMapping)) {
const conditionalMapping = /** @type {ConditionalMapping} */ (
innerMapping
);
lookup[lookup.length - 1][2] = i + 1;
lookup.push([conditionalMapping, cachedKeys(conditionalMapping), 0]);
if (
innerMapping !== null &&
typeof innerMapping === "object" &&
!Array.isArray(innerMapping)
) {
const nested = /** @type {ConditionalMapping} */ (innerMapping);
top[2] = i + 1;
lookup.push([nested, cachedKeys(nested), 0]);
continue loop;
}
@@ -449,10 +433,10 @@ function targetMapping(
let result = mappingTarget;
if (isPattern) {
result = result.replace(
patternRegEx,
remainingRequest.replace(/\$/g, "$$"),
);
const escapedRemainder = remainingRequest.includes("$")
? remainingRequest.replace(DOLLAR_ESCAPE_RE, "$$")
: remainingRequest;
result = result.replace(patternRegEx, escapedRemainder);
}
return result;
@@ -492,7 +476,8 @@ function directMapping(
/** @type {string[]} */
const targets = [];
for (const exp of mappingTarget) {
for (let i = 0, len = mappingTarget.length; i < len; i++) {
const exp = mappingTarget[i];
if (typeof exp === "string") {
targets.push(
targetMapping(
@@ -516,14 +501,17 @@ function directMapping(
conditionNames,
assert,
);
for (const innerExport of innerExports) {
targets.push(innerExport);
for (let j = 0, innerLen = innerExports.length; j < innerLen; j++) {
targets.push(innerExports[j]);
}
}
return targets;
}
/** @type {[string[], null]} */
const EMPTY_NO_MATCH = /** @type {[string[], null]} */ ([[], null]);
/**
* @param {ExportsField | ImportsField} field root
* @param {(s: string) => string} normalizeRequest Normalize request, for `imports` field it adds `#`, for `exports` field it adds `.` or `./`
@@ -538,26 +526,25 @@ function createFieldProcessor(
assertTarget,
) {
return function fieldProcessor(request, conditionNames) {
request = assertRequest(request);
const match = findMatch(normalizeRequest(assertRequest(request)), field);
const match = findMatch(normalizeRequest(request), field);
if (match === null) return [[], null];
if (match === null) return EMPTY_NO_MATCH;
const [mapping, remainingRequest, isSubpathMapping, isPattern, usedField] =
match;
/** @type {DirectMapping | null} */
let direct = null;
if (isConditionalMapping(mapping)) {
let direct;
if (
mapping !== null &&
typeof mapping === "object" &&
!Array.isArray(mapping)
) {
direct = conditionalMapping(
/** @type {ConditionalMapping} */ (mapping),
conditionNames,
);
// matching not found
if (direct === null) return [[], null];
if (direct === null) return EMPTY_NO_MATCH;
} else {
direct = /** @type {DirectMapping} */ (mapping);
}
+42 -34
View File
@@ -19,49 +19,57 @@ const stripJsonComments = require("./strip-json-comments");
const _stripCommentsCache = new WeakMap();
/**
* Read and parse JSON file (supports JSONC with comments)
* @template T
* Read and parse JSON file (supports JSONC with comments).
* Callback-based so a synchronous `fileSystem` stays synchronous all the
* way through — Promise wrapping would defer resolution by a Promise tick
* and break `resolveSync` when `tsconfig` is used together with
* `useSyncFileSystemCalls: true`.
* @param {FileSystem} fileSystem the file system
* @param {string} jsonFilePath absolute path to JSON file
* @param {ReadJsonOptions} options Options
* @returns {Promise<T>} parsed JSON content
* @param {(err: NodeJS.ErrnoException | Error | null, content?: JsonObject) => void} callback callback
* @returns {void}
*/
async function readJson(fileSystem, jsonFilePath, options = {}) {
function readJson(fileSystem, jsonFilePath, options, callback) {
const { stripComments = false } = options;
const { readJson } = fileSystem;
if (readJson && !stripComments) {
return new Promise((resolve, reject) => {
readJson(jsonFilePath, (err, content) => {
if (err) return reject(err);
resolve(/** @type {T} */ (content));
});
const { readJson: fsReadJson } = fileSystem;
if (fsReadJson && !stripComments) {
fsReadJson(jsonFilePath, (err, content) => {
if (err) return callback(err);
callback(null, /** @type {JsonObject} */ (content));
});
return;
}
const buf = await new Promise((resolve, reject) => {
fileSystem.readFile(jsonFilePath, (err, data) => {
if (err) return reject(err);
resolve(data);
});
fileSystem.readFile(jsonFilePath, (err, data) => {
if (err) return callback(err);
const buf = /** @type {Buffer} */ (data);
if (stripComments) {
const cached = _stripCommentsCache.get(buf);
if (cached !== undefined) return callback(null, cached);
}
let result;
try {
const jsonText = buf.toString();
const jsonWithoutComments = stripComments
? stripJsonComments(jsonText, {
trailingCommas: true,
whitespace: true,
})
: jsonText;
result = JSON.parse(jsonWithoutComments);
} catch (parseErr) {
return callback(/** @type {Error} */ (parseErr));
}
if (stripComments) {
_stripCommentsCache.set(buf, result);
}
callback(null, result);
});
if (stripComments) {
const cached = _stripCommentsCache.get(buf);
if (cached !== undefined) return /** @type {T} */ (cached);
}
const jsonText = /** @type {string} */ (buf.toString());
// Strip comments to support JSONC (e.g., tsconfig.json with comments)
const jsonWithoutComments = stripComments
? stripJsonComments(jsonText, { trailingCommas: true, whitespace: true })
: jsonText;
const result = JSON.parse(jsonWithoutComments);
if (stripComments) {
_stripCommentsCache.set(buf, result);
}
return result;
}
module.exports.readJson = readJson;