| Server IP : 159.203.156.69 / Your IP : 216.73.217.172 Web Server : nginx/1.24.0 System : Linux main-ubuntu 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/tanviranik.com/node_modules/vinext/dist/server/ |
Upload File : |
{"version":3,"file":"middleware-matcher.js","names":[],"sources":["../../src/server/middleware-matcher.ts"],"sourcesContent":["import {\n checkHasConditions,\n requestContextFromRequest,\n safeRegExp,\n type RequestContext,\n} from \"../config/config-matchers.js\";\nimport type { HasCondition, NextI18nConfig } from \"../config/next-config.js\";\nimport { removeTrailingSlash } from \"../utils/base-path.js\";\n\nexport type MiddlewareMatcherObject = {\n source: string;\n locale?: false;\n has?: HasCondition[];\n missing?: HasCondition[];\n};\n\nexport type MatcherConfig = string | Array<string | MiddlewareMatcherObject>;\n\nconst EMPTY_MIDDLEWARE_REQUEST_CONTEXT: RequestContext = {\n headers: new Headers(),\n cookies: {},\n query: new URLSearchParams(),\n host: \"\",\n};\n\nconst _mwPatternCache = new Map<string, RegExp | null>();\n\nexport function matchesMiddleware(\n pathname: string,\n matcher: MatcherConfig | undefined,\n request?: Request,\n i18nConfig?: NextI18nConfig | null,\n): boolean {\n if (!matcher) {\n return true;\n }\n\n if (typeof matcher === \"string\") {\n return matchMatcherPattern(pathname, matcher, i18nConfig);\n }\n if (!Array.isArray(matcher)) {\n return false;\n }\n\n const requestContext = request\n ? requestContextFromRequest(request)\n : EMPTY_MIDDLEWARE_REQUEST_CONTEXT;\n\n for (const m of matcher) {\n if (typeof m === \"string\") {\n if (matchMatcherPattern(pathname, m, i18nConfig)) {\n return true;\n }\n continue;\n }\n\n if (isValidMiddlewareMatcherObject(m)) {\n if (!matchObjectMatcher(pathname, m, i18nConfig)) {\n continue;\n }\n\n if (!checkHasConditions(m.has, m.missing, requestContext)) {\n continue;\n }\n\n return true;\n }\n }\n\n return false;\n}\n\nfunction isValidMiddlewareMatcherObject(value: unknown): value is MiddlewareMatcherObject {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n\n if (!(\"source\" in value) || typeof value.source !== \"string\") return false;\n\n for (const key of Object.keys(value)) {\n if (key !== \"source\" && key !== \"locale\" && key !== \"has\" && key !== \"missing\") {\n return false;\n }\n }\n\n if (\"locale\" in value && value.locale !== undefined && value.locale !== false) return false;\n if (\"has\" in value && value.has !== undefined && !Array.isArray(value.has)) return false;\n if (\"missing\" in value && value.missing !== undefined && !Array.isArray(value.missing)) {\n return false;\n }\n\n return true;\n}\n\nfunction matchMatcherPattern(\n pathname: string,\n pattern: string,\n i18nConfig?: NextI18nConfig | null,\n): boolean {\n if (!i18nConfig) return matchPattern(pathname, pattern);\n\n const localeStrippedPathname = stripLocalePrefix(pathname, i18nConfig);\n return matchPattern(localeStrippedPathname ?? pathname, pattern);\n}\n\nfunction matchObjectMatcher(\n pathname: string,\n matcher: MiddlewareMatcherObject,\n i18nConfig?: NextI18nConfig | null,\n): boolean {\n return matcher.locale === false\n ? matchPattern(pathname, matcher.source)\n : matchMatcherPattern(pathname, matcher.source, i18nConfig);\n}\n\nfunction stripLocalePrefix(pathname: string, i18nConfig: NextI18nConfig): string | null {\n if (pathname === \"/\") return null;\n\n const segments = pathname.split(\"/\");\n const firstSegment = segments[1];\n if (!firstSegment || !i18nConfig.locales.includes(firstSegment)) {\n return null;\n }\n\n const stripped = \"/\" + segments.slice(2).join(\"/\");\n return removeTrailingSlash(stripped);\n}\n\nexport function matchPattern(pathname: string, pattern: string): boolean {\n let cached = _mwPatternCache.get(pattern);\n if (cached === undefined) {\n cached = compileMatcherPattern(pattern);\n _mwPatternCache.set(pattern, cached);\n }\n if (cached === null) return pathname === pattern;\n return cached.test(pathname);\n}\n\nfunction extractConstraint(str: string, re: RegExp): string | null {\n if (str[re.lastIndex] !== \"(\") return null;\n const start = re.lastIndex + 1;\n let depth = 1;\n let i = start;\n while (i < str.length && depth > 0) {\n if (str[i] === \"(\") depth++;\n else if (str[i] === \")\") depth--;\n i++;\n }\n if (depth !== 0) return null;\n re.lastIndex = i;\n return str.slice(start, i - 1);\n}\n\nfunction compileMatcherPattern(pattern: string): RegExp | null {\n const hasConstraints = /:[\\w-]+[*+]?\\(/.test(pattern);\n\n if (!hasConstraints && (pattern.includes(\"(\") || pattern.includes(\"\\\\\"))) {\n return safeRegExp(\"^\" + pattern + \"$\");\n }\n\n let regexStr = \"\";\n const tokenRe = /\\/:([\\w-]+)\\*|\\/:([\\w-]+)\\+|:([\\w-]+)|[.]|[^/:.]+|./g;\n let tok: RegExpExecArray | null;\n while ((tok = tokenRe.exec(pattern)) !== null) {\n if (tok[1] !== undefined) {\n const constraint = hasConstraints ? extractConstraint(pattern, tokenRe) : null;\n regexStr += constraint !== null ? `(?:/(${constraint}))?` : \"(?:/.*)?\";\n } else if (tok[2] !== undefined) {\n const constraint = hasConstraints ? extractConstraint(pattern, tokenRe) : null;\n regexStr += constraint !== null ? `(?:/(${constraint}))` : \"(?:/.+)\";\n } else if (tok[3] !== undefined) {\n const constraint = hasConstraints ? extractConstraint(pattern, tokenRe) : null;\n const isOptional = pattern[tokenRe.lastIndex] === \"?\";\n if (isOptional) tokenRe.lastIndex += 1;\n\n const group = constraint !== null ? `(${constraint})` : \"([^/]+)\";\n\n if (isOptional && regexStr.endsWith(\"/\")) {\n regexStr = regexStr.slice(0, -1) + `(?:/${group})?`;\n } else if (isOptional) {\n regexStr += `${group}?`;\n } else {\n regexStr += group;\n }\n } else if (tok[0] === \".\") {\n regexStr += \"\\\\.\";\n } else {\n regexStr += tok[0];\n }\n }\n\n return safeRegExp(\"^\" + regexStr + \"$\");\n}\n"],"mappings":";;;AAkBA,MAAM,mCAAmD;CACvD,SAAS,IAAI,SAAS;CACtB,SAAS,EAAE;CACX,OAAO,IAAI,iBAAiB;CAC5B,MAAM;CACP;AAED,MAAM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