gitea push
This commit is contained in:
+48
-61
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user