@next/routing 16.2.0-canary.95 → 16.2.0-canary.96
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/types.d.ts +25 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -39,8 +39,10 @@ const result = await resolveRoutes({
|
|
|
39
39
|
},
|
|
40
40
|
})
|
|
41
41
|
|
|
42
|
-
if (result.
|
|
43
|
-
console.log('
|
|
42
|
+
if (result.resolvedPathname) {
|
|
43
|
+
console.log('Resolved pathname:', result.resolvedPathname)
|
|
44
|
+
console.log('Resolved query:', result.resolvedQuery)
|
|
45
|
+
console.log('Invocation target:', result.invocationTarget)
|
|
44
46
|
}
|
|
45
47
|
```
|
|
46
48
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { resolveRoutes } from './resolve-routes';
|
|
2
|
-
export type { RouteHas, Route, MiddlewareContext, MiddlewareResult, ResolveRoutesParams, ResolveRoutesResult, } from './types';
|
|
2
|
+
export type { RouteHas, Route, MiddlewareContext, MiddlewareResult, ResolveRoutesParams, ResolveRoutesResult, ResolveRoutesQuery, ResolveRoutesQueryValue, RouteInvocationTarget, } from './types';
|
|
3
3
|
export type { I18nConfig, I18nDomain } from './i18n';
|
|
4
4
|
export { detectLocale, detectDomainLocale, normalizeLocalePath, getAcceptLanguageLocale, getCookieLocale, } from './i18n';
|
|
5
5
|
export { responseToMiddlewareResult } from './middleware';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{"use strict";var e={};(()=>{e.d=(t,n)=>{for(var a in n){if(e.o(n,a)&&!e.o(t,a)){Object.defineProperty(t,a,{enumerable:true,get:n[a]})}}}})();(()=>{e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)})();(()=>{e.r=e=>{if(typeof Symbol!=="undefined"&&Symbol.toStringTag){Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}Object.defineProperty(e,"__esModule",{value:true})}})();if(typeof e!=="undefined")e.ab=__dirname+"/";var t={};e.r(t);e.d(t,{detectDomainLocale:()=>detectDomainLocale,detectLocale:()=>detectLocale,getAcceptLanguageLocale:()=>getAcceptLanguageLocale,getCookieLocale:()=>getCookieLocale,normalizeLocalePath:()=>normalizeLocalePath,resolveRoutes:()=>resolveRoutes,responseToMiddlewareResult:()=>responseToMiddlewareResult});function matchesCondition(e,t){if(e===undefined){return{matched:false}}if(t===undefined){return{matched:true,capturedValue:e}}try{const n=new RegExp(t);const a=e.match(n);if(a){return{matched:true,capturedValue:a[0]}}}catch(e){}if(e===t){return{matched:true,capturedValue:e}}return{matched:false}}function getConditionValue(e,t,n){switch(e.type){case"header":return n.get(e.key)||undefined;case"cookie":{const t=n.get("cookie");if(!t)return undefined;const a=t.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n){e[n]=a.join("=")}return e}),{});return a[e.key]}case"query":return t.searchParams.get(e.key)||undefined;case"host":return t.hostname;default:return""}}function normalizeCaptureKey(e){return e.replace(/[^a-zA-Z]/g,"")}function checkHasConditions(e,t,n){if(!e||e.length===0){return{matched:true,captures:{}}}const a={};for(const s of e){const e=getConditionValue(s,t,n);const r=matchesCondition(e,s.value);if(!r.matched){return{matched:false,captures:{}}}if(r.capturedValue!==undefined&&s.type!=="host"){const e=normalizeCaptureKey(s.key);a[e]=r.capturedValue}}return{matched:true,captures:a}}function checkMissingConditions(e,t,n){if(!e||e.length===0){return true}for(const a of e){const e=getConditionValue(a,t,n);const s=matchesCondition(e,a.value);if(s.matched){return false}}return true}function replaceDestination(e,t,n){let a=e;if(t){for(let e=1;e<t.length;e++){const n=t[e];if(n!==undefined){a=a.replace(new RegExp(`\\$${e}`,"g"),n)}}if(t.groups){for(const[e,n]of Object.entries(t.groups)){if(n!==undefined){a=a.replace(new RegExp(`\\$${e}`,"g"),n)}}}}for(const[e,t]of Object.entries(n)){a=a.replace(new RegExp(`\\$${e}`,"g"),t)}return a}function isExternalDestination(e){return e.startsWith("http://")||e.startsWith("https://")}function applyDestination(e,t){if(isExternalDestination(t)){return new URL(t)}const n=new URL(e.toString());const[a,s]=t.split("?");n.pathname=a;if(s){const e=new URLSearchParams(s);for(const[t,a]of e.entries()){n.searchParams.set(t,a)}}return n}function isRedirectStatus(e){if(!e)return false;return e>=300&&e<400}function hasRedirectHeaders(e){const t=Object.keys(e).map((e=>e.toLowerCase()));return t.includes("location")||t.includes("refresh")}function normalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let s=a.pathname;const r=`${t}/_next/data/${n}/`;if(s.startsWith(r)){let e=s.slice(r.length);if(e.endsWith(".json")){e=e.slice(0,-5)}s=t?`${t}/${e}`:`/${e}`;a.pathname=s}return a}function denormalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let s=a.pathname;const r=`${t}/_next/data/${n}/`;if(!s.startsWith(r)){let e=s;if(t&&s.startsWith(t)){e=s.slice(t.length)}s=`${t}/_next/data/${n}${e}.json`;a.pathname=s}return a}function detectDomainLocale(e,t,n){if(!e)return undefined;const a=t?.toLowerCase();const s=n?.toLowerCase();for(const t of e){const e=t.domain.split(":",1)[0].toLowerCase();if(a===e||s===t.defaultLocale.toLowerCase()||t.locales?.some((e=>e.toLowerCase()===s))){return t}}return undefined}function normalizeLocalePath(e,t){if(!t||t.length===0){return{pathname:e}}const n=e.split("/",2);if(!n[1]){return{pathname:e}}const a=n[1].toLowerCase();const s=t.map((e=>e.toLowerCase()));const r=s.indexOf(a);if(r<0){return{pathname:e}}const o=t[r];const i=e.slice(o.length+1)||"/";return{pathname:i,detectedLocale:o}}function getAcceptLanguageLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(",").map((e=>{const t=e.trim().split(";");const n=t[0];let a=1;if(t[1]){const e=t[1].match(/q=([0-9.]+)/);if(e&&e[1]){a=parseFloat(e[1])}}return{locale:n,quality:a}})).filter((e=>e.quality>0)).sort(((e,t)=>t.quality-e.quality));const a=new Map;for(const e of t){a.set(e.toLowerCase(),e)}for(const{locale:e}of n){const t=e.toLowerCase();if(a.has(t)){return a.get(t)}}for(const{locale:e}of n){const t=e.toLowerCase().split("-")[0];if(a.has(t)){return a.get(t)}for(const[e,n]of a){if(e.startsWith(t+"-")){return n}}}return undefined}catch(e){return undefined}}function getCookieLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n&&a.length>0){e[n]=decodeURIComponent(a.join("="))}return e}),{});const a=n.NEXT_LOCALE?.toLowerCase();if(!a){return undefined}return t.find((e=>e.toLowerCase()===a))}catch(e){return undefined}}function detectLocale(e){const{pathname:t,hostname:n,cookieHeader:a,acceptLanguageHeader:s,i18n:r}=e;const o=normalizeLocalePath(t,r.locales);if(o.detectedLocale){return{locale:o.detectedLocale,pathnameWithoutLocale:o.pathname,localeInPath:true}}if(r.localeDetection===false){const e=detectDomainLocale(r.domains,n);return{locale:e?.defaultLocale||r.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}const i=getCookieLocale(a,r.locales);if(i){return{locale:i,pathnameWithoutLocale:t,localeInPath:false}}const c=getAcceptLanguageLocale(s||"",r.locales);if(c){return{locale:c,pathnameWithoutLocale:t,localeInPath:false}}const d=detectDomainLocale(r.domains,n);if(d){return{locale:d.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}return{locale:r.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}function matchRoute(e,t,n){const a=new RegExp(e.sourceRegex);const s=t.pathname.match(a);if(!s){return{matched:false}}const r=checkHasConditions(e.has,t,n);if(!r.matched){return{matched:false}}const o=checkMissingConditions(e.missing,t,n);if(!o){return{matched:false}}const i=e.destination?replaceDestination(e.destination,s,r.captures):undefined;const c=e.headers?Object.fromEntries(Object.entries(e.headers).map((([e,t])=>[replaceDestination(e,s,r.captures),replaceDestination(t,s,r.captures)]))):undefined;return{matched:true,destination:i,headers:c,regexMatches:s,hasCaptures:r.captures}}function processRoutes(e,t,n,a,s){let r=t;let o;for(const t of e){const e=matchRoute(t,r,n);if(e.matched){if(e.headers){for(const[t,n]of Object.entries(e.headers)){a.set(t,n)}}if(t.status){o=t.status}if(e.destination){if(isRedirectStatus(t.status)&&e.headers&&hasRedirectHeaders(e.headers)){const n=isExternalDestination(e.destination)?new URL(e.destination):applyDestination(r,e.destination);return{url:r,redirect:{url:n,status:t.status},stopped:true,status:o}}if(isExternalDestination(e.destination)){return{url:r,externalRewrite:new URL(e.destination),stopped:true,status:o}}r=applyDestination(r,e.destination);if(r.origin!==s){return{url:r,externalRewrite:r,stopped:true,status:o}}}}}return{url:r,stopped:false,status:o}}function matchesPathname(e,t){for(const n of t){if(e===n){return n}}return undefined}function matchDynamicRoute(e,t){const n=new RegExp(t.sourceRegex);const a=e.match(n);if(!a){return{matched:false}}const s={};for(let e=1;e<a.length;e++){if(a[e]!==undefined){s[String(e)]=a[e]}}if(a.groups){Object.assign(s,a.groups)}return{matched:true,params:s,regexMatches:a}}function applyOnMatchHeaders(e,t,n,a){const s=new Headers(a);for(const a of e){const e=matchRoute(a,t,n);if(e.matched&&e.headers){for(const[t,n]of Object.entries(e.headers)){s.set(t,n)}}}return s}function checkDynamicRoutes(e,t,n,a,s,r,o,i,c,d){let l=t;if(d&&c){l=denormalizeNextDataUrl(t,o,i)}for(const t of e){const e=matchDynamicRoute(l.pathname,t);if(e.matched){const o=checkHasConditions(t.has,l,a);const i=checkMissingConditions(t.missing,l,a);if(o.matched&&i){const i=t.destination?replaceDestination(t.destination,e.regexMatches||null,o.captures).split("?")[0]:l.pathname;const c=matchesPathname(i,n);if(c){const t=applyOnMatchHeaders(r,l,a,s);return{matched:true,result:{matchedPathname:c,routeMatches:e.params,resolvedHeaders:t},resetUrl:l}}}}}return{matched:false}}async function resolveRoutes(e){const{url:t,basePath:n,requestBody:a,headers:s,pathnames:r,routes:o,invokeMiddleware:i,buildId:c,i18n:d}=e;const{shouldNormalizeNextData:l}=o;let u=new URL(t.toString());let f=new Headers(s);let h=new Headers;let m;const p=t.origin;let g=false;if(l){const e=`${n}/_next/data/${c}/`;g=t.pathname.startsWith(e);if(g){u=normalizeNextDataUrl(u,n,c)}}if(d&&!g){const e=u.pathname.startsWith(n)?u.pathname.slice(n.length)||"/":u.pathname;if(!e.startsWith("/_next/")&&!e.startsWith("/api/")){const t=u.hostname;const a=f.get("cookie")||undefined;const s=f.get("accept-language")||undefined;const r=normalizeLocalePath(e,d.locales);const o=!!r.detectedLocale;const i=detectDomainLocale(d.domains,t);const c=i?.defaultLocale||d.defaultLocale;let l=r.detectedLocale||c;if(d.localeDetection!==false&&!o){const r=detectLocale({pathname:e,hostname:t,cookieHeader:a,acceptLanguageHeader:s,i18n:d});l=r.locale;if(l!==c){const a=detectDomainLocale(d.domains,undefined,l);if(a&&a.domain!==t){const t=a.http?"http":"https";const s=l===a.defaultLocale?"":`/${l}`;const r=new URL(`${t}://${a.domain}${n}${s}${e}${u.search}`);return{redirect:{url:r,status:307},resolvedHeaders:h}}if(!a||a&&a.domain===t){const t=new URL(u.toString());t.pathname=`${n}/${l}${e}`;return{redirect:{url:t,status:307},resolvedHeaders:h}}}}if(!o){const t=l||i?.defaultLocale||d.defaultLocale;u.pathname=`${n}/${t}${e}`}}}const w=processRoutes(o.beforeMiddleware,u,f,h,p);if(w.status){m=w.status}if(w.redirect){return{redirect:w.redirect,resolvedHeaders:h,status:m}}if(w.externalRewrite){return{externalRewrite:w.externalRewrite,resolvedHeaders:h,status:m}}u=w.url;if(g&&l){u=denormalizeNextDataUrl(u,n,c)}const L=await i({url:u,headers:f,requestBody:a});if(L.bodySent){return{middlewareResponded:true}}if(L.requestHeaders){f=new Headers(L.requestHeaders)}if(L.responseHeaders){L.responseHeaders.forEach(((e,t)=>{if(t.toLowerCase()==="set-cookie"){h.append(t,e)}else{h.set(t,e)}}))}if(L.redirect){if(!h.has("location")){h.set("Location",L.redirect.url.toString())}return{resolvedHeaders:h,status:L.redirect.status}}if(L.rewrite){u=L.rewrite;if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}}if(g&&l){u=normalizeNextDataUrl(u,n,c)}const R=processRoutes(o.beforeFiles,u,f,h,p);if(R.status){m=R.status}if(R.redirect){return{redirect:R.redirect,resolvedHeaders:h,status:m}}if(R.externalRewrite){return{externalRewrite:R.externalRewrite,resolvedHeaders:h,status:m}}u=R.url;if(g&&l){u=denormalizeNextDataUrl(u,n,c)}let y=matchesPathname(u.pathname,r);if(y){for(const e of o.dynamicRoutes){const t=matchDynamicRoute(u.pathname,e);if(t.matched){const n=checkHasConditions(e.has,u,f);const a=checkMissingConditions(e.missing,u,f);if(n.matched&&a){const e=applyOnMatchHeaders(o.onMatch,u,f,h);return{matchedPathname:y,routeMatches:t.params,resolvedHeaders:e,status:m}}}}const e=applyOnMatchHeaders(o.onMatch,u,f,h);return{matchedPathname:y,resolvedHeaders:e,status:m}}if(g&&l){u=normalizeNextDataUrl(u,n,c)}for(const e of o.afterFiles){const t=matchRoute(e,u,f);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){h.set(e,n)}}if(e.status){m=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:h,status:m}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:h,status:m}}u=applyDestination(u,t.destination);if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}const a=checkDynamicRoutes(o.dynamicRoutes,u,r,f,h,o.onMatch,n,c,l,g);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:m}}let s=u;if(g&&l){s=denormalizeNextDataUrl(u,n,c)}y=matchesPathname(s.pathname,r);if(y){const e=applyOnMatchHeaders(o.onMatch,s,f,h);return{matchedPathname:y,resolvedHeaders:e,status:m}}}}}for(const e of o.dynamicRoutes){const t=matchDynamicRoute(u.pathname,e);if(t.matched){const n=checkHasConditions(e.has,u,f);const a=checkMissingConditions(e.missing,u,f);if(n.matched&&a){const a=e.destination?replaceDestination(e.destination,t.regexMatches||null,n.captures).split("?")[0]:u.pathname;y=matchesPathname(a,r);if(y){const e=applyOnMatchHeaders(o.onMatch,u,f,h);return{matchedPathname:y,routeMatches:t.params,resolvedHeaders:e,status:m}}}}}for(const e of o.fallback){const t=matchRoute(e,u,f);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){h.set(e,n)}}if(e.status){m=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:h,status:m}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:h,status:m}}u=applyDestination(u,t.destination);if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}const a=checkDynamicRoutes(o.dynamicRoutes,u,r,f,h,o.onMatch,n,c,l,g);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:m}}let s=u;if(g&&l){s=denormalizeNextDataUrl(u,n,c)}y=matchesPathname(s.pathname,r);if(y){const e=applyOnMatchHeaders(o.onMatch,s,f,h);return{matchedPathname:y,resolvedHeaders:e,status:m}}}}}return{resolvedHeaders:h,status:m}}function responseToMiddlewareResult(e,t,a){const s={};const r={};e.headers.forEach(((e,t)=>{if(r[t]){const n=r[t];if(Array.isArray(n)){n.push(e)}else{r[t]=[n,e]}}else{r[t]=e}}));if(r["x-middleware-override-headers"]){const e=new Set;let n=r["x-middleware-override-headers"];if(typeof n==="string"){n=n.split(",")}for(const t of n){e.add(t.trim())}delete r["x-middleware-override-headers"];const a=[];t.forEach(((t,n)=>{if(!e.has(n)){a.push(n)}}));for(const e of a){t.delete(e)}for(const n of e.keys()){const e="x-middleware-request-"+n;const a=r[e];if(a===undefined||a===null){t.delete(n)}else if(Array.isArray(a)){t.set(n,a[0]);for(let e=1;e<a.length;e++){t.append(n,a[e])}}else{t.set(n,a)}delete r[e]}}if(!r["x-middleware-rewrite"]&&!r["x-middleware-next"]&&!r["location"]){r["x-middleware-refresh"]="1"}delete r["x-middleware-next"];const o=new Headers;for(const[e,n]of Object.entries(r)){if(["content-length","x-middleware-rewrite","x-middleware-redirect","x-middleware-refresh"].includes(e)){continue}if(e==="x-middleware-set-cookie"){if(n!==undefined){if(Array.isArray(n)){for(const a of n){t.append(e,a)}}else{t.set(e,n)}}continue}if(n!==undefined){if(Array.isArray(n)){for(const a of n){o.append(e,a);t.append(e,a)}}else{o.set(e,n);t.set(e,n)}}}s.responseHeaders=o;s.requestHeaders=t;if(r["x-middleware-rewrite"]){const e=r["x-middleware-rewrite"];const t=getRelativeURL(e,a.toString());o.set("x-middleware-rewrite",t);try{const e=new URL(t,a);if(e.origin!==a.origin){s.rewrite=e;return s}s.rewrite=e}catch{s.rewrite=new URL(t,a)}}if(r["location"]){const t=r["location"];const i=n.has(e.status);if(i){const n=getRelativeURL(t,a.toString());o.set("location",n);try{const t=new URL(n,a);s.redirect={url:t,status:e.status};return s}catch{s.redirect={url:new URL(n,a),status:e.status};return s}}else{o.set("location",t);return s}}if(r["x-middleware-refresh"]){s.bodySent=true;return s}return s}function getRelativeURL(e,t){try{const n=new URL(e,t);const a=new URL(t);if(n.origin===a.origin){return n.pathname+n.search+n.hash}return n.toString()}catch{return e}}const n=new Set([301,302,303,307,308]);module.exports=t})();
|
|
1
|
+
(()=>{"use strict";var e={};(()=>{e.d=(t,n)=>{for(var a in n){if(e.o(n,a)&&!e.o(t,a)){Object.defineProperty(t,a,{enumerable:true,get:n[a]})}}}})();(()=>{e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)})();(()=>{e.r=e=>{if(typeof Symbol!=="undefined"&&Symbol.toStringTag){Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}Object.defineProperty(e,"__esModule",{value:true})}})();if(typeof e!=="undefined")e.ab=__dirname+"/";var t={};e.r(t);e.d(t,{detectDomainLocale:()=>detectDomainLocale,detectLocale:()=>detectLocale,getAcceptLanguageLocale:()=>getAcceptLanguageLocale,getCookieLocale:()=>getCookieLocale,normalizeLocalePath:()=>normalizeLocalePath,resolveRoutes:()=>resolveRoutes,responseToMiddlewareResult:()=>responseToMiddlewareResult});function matchesCondition(e,t){if(e===undefined){return{matched:false}}if(t===undefined){return{matched:true,capturedValue:e}}try{const n=new RegExp(t);const a=e.match(n);if(a){return{matched:true,capturedValue:a[0]}}}catch(e){}if(e===t){return{matched:true,capturedValue:e}}return{matched:false}}function getConditionValue(e,t,n){switch(e.type){case"header":return n.get(e.key)||undefined;case"cookie":{const t=n.get("cookie");if(!t)return undefined;const a=t.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n){e[n]=a.join("=")}return e}),{});return a[e.key]}case"query":return t.searchParams.get(e.key)||undefined;case"host":return t.hostname;default:return""}}function normalizeCaptureKey(e){return e.replace(/[^a-zA-Z]/g,"")}function checkHasConditions(e,t,n){if(!e||e.length===0){return{matched:true,captures:{}}}const a={};for(const r of e){const e=getConditionValue(r,t,n);const s=matchesCondition(e,r.value);if(!s.matched){return{matched:false,captures:{}}}if(s.capturedValue!==undefined&&r.type!=="host"){const e=normalizeCaptureKey(r.key);a[e]=s.capturedValue}}return{matched:true,captures:a}}function checkMissingConditions(e,t,n){if(!e||e.length===0){return true}for(const a of e){const e=getConditionValue(a,t,n);const r=matchesCondition(e,a.value);if(r.matched){return false}}return true}function replaceDestination(e,t,n){let a=e;if(t){for(let e=1;e<t.length;e++){const n=t[e];if(n!==undefined){a=a.replace(new RegExp(`\\$${e}`,"g"),n)}}if(t.groups){for(const[e,n]of Object.entries(t.groups)){if(n!==undefined){a=a.replace(new RegExp(`\\$${e}`,"g"),n)}}}}for(const[e,t]of Object.entries(n)){a=a.replace(new RegExp(`\\$${e}`,"g"),t)}return a}function isExternalDestination(e){return e.startsWith("http://")||e.startsWith("https://")}function applyDestination(e,t){if(isExternalDestination(t)){return new URL(t)}const n=new URL(e.toString());const[a,r]=t.split("?");n.pathname=a;if(r){const e=new URLSearchParams(r);for(const[t,a]of e.entries()){n.searchParams.set(t,a)}}return n}function isRedirectStatus(e){if(!e)return false;return e>=300&&e<400}function hasRedirectHeaders(e){const t=Object.keys(e).map((e=>e.toLowerCase()));return t.includes("location")||t.includes("refresh")}function normalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let r=a.pathname;const s=`${t}/_next/data/${n}/`;if(r.startsWith(s)){let e=r.slice(s.length);if(e.endsWith(".json")){e=e.slice(0,-5)}r=t?`${t}/${e}`:`/${e}`;a.pathname=r}return a}function denormalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let r=a.pathname;const s=`${t}/_next/data/${n}/`;if(!r.startsWith(s)){let e=r;if(t&&r.startsWith(t)){e=r.slice(t.length)}r=`${t}/_next/data/${n}${e}.json`;a.pathname=r}return a}function detectDomainLocale(e,t,n){if(!e)return undefined;const a=t?.toLowerCase();const r=n?.toLowerCase();for(const t of e){const e=t.domain.split(":",1)[0].toLowerCase();if(a===e||r===t.defaultLocale.toLowerCase()||t.locales?.some((e=>e.toLowerCase()===r))){return t}}return undefined}function normalizeLocalePath(e,t){if(!t||t.length===0){return{pathname:e}}const n=e.split("/",2);if(!n[1]){return{pathname:e}}const a=n[1].toLowerCase();const r=t.map((e=>e.toLowerCase()));const s=r.indexOf(a);if(s<0){return{pathname:e}}const o=t[s];const i=e.slice(o.length+1)||"/";return{pathname:i,detectedLocale:o}}function getAcceptLanguageLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(",").map((e=>{const t=e.trim().split(";");const n=t[0];let a=1;if(t[1]){const e=t[1].match(/q=([0-9.]+)/);if(e&&e[1]){a=parseFloat(e[1])}}return{locale:n,quality:a}})).filter((e=>e.quality>0)).sort(((e,t)=>t.quality-e.quality));const a=new Map;for(const e of t){a.set(e.toLowerCase(),e)}for(const{locale:e}of n){const t=e.toLowerCase();if(a.has(t)){return a.get(t)}}for(const{locale:e}of n){const t=e.toLowerCase().split("-")[0];if(a.has(t)){return a.get(t)}for(const[e,n]of a){if(e.startsWith(t+"-")){return n}}}return undefined}catch(e){return undefined}}function getCookieLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n&&a.length>0){e[n]=decodeURIComponent(a.join("="))}return e}),{});const a=n.NEXT_LOCALE?.toLowerCase();if(!a){return undefined}return t.find((e=>e.toLowerCase()===a))}catch(e){return undefined}}function detectLocale(e){const{pathname:t,hostname:n,cookieHeader:a,acceptLanguageHeader:r,i18n:s}=e;const o=normalizeLocalePath(t,s.locales);if(o.detectedLocale){return{locale:o.detectedLocale,pathnameWithoutLocale:o.pathname,localeInPath:true}}if(s.localeDetection===false){const e=detectDomainLocale(s.domains,n);return{locale:e?.defaultLocale||s.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}const i=getCookieLocale(a,s.locales);if(i){return{locale:i,pathnameWithoutLocale:t,localeInPath:false}}const c=getAcceptLanguageLocale(r||"",s.locales);if(c){return{locale:c,pathnameWithoutLocale:t,localeInPath:false}}const l=detectDomainLocale(s.domains,n);if(l){return{locale:l.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}return{locale:s.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}function matchRoute(e,t,n){const a=new RegExp(e.sourceRegex);const r=t.pathname.match(a);if(!r){return{matched:false}}const s=checkHasConditions(e.has,t,n);if(!s.matched){return{matched:false}}const o=checkMissingConditions(e.missing,t,n);if(!o){return{matched:false}}const i=e.destination?replaceDestination(e.destination,r,s.captures):undefined;const c=e.headers?Object.fromEntries(Object.entries(e.headers).map((([e,t])=>[replaceDestination(e,r,s.captures),replaceDestination(t,r,s.captures)]))):undefined;return{matched:true,destination:i,headers:c,regexMatches:r,hasCaptures:s.captures}}function processRoutes(e,t,n,a,r){let s=t;let o;for(const t of e){const e=matchRoute(t,s,n);if(e.matched){if(e.headers){for(const[t,n]of Object.entries(e.headers)){a.set(t,n)}}if(t.status){o=t.status}if(e.destination){if(isRedirectStatus(t.status)&&e.headers&&hasRedirectHeaders(e.headers)){const n=isExternalDestination(e.destination)?new URL(e.destination):applyDestination(s,e.destination);return{url:s,redirect:{url:n,status:t.status},stopped:true,status:o}}if(isExternalDestination(e.destination)){return{url:s,externalRewrite:new URL(e.destination),stopped:true,status:o}}s=applyDestination(s,e.destination);if(s.origin!==r){return{url:s,externalRewrite:s,stopped:true,status:o}}}}}return{url:s,stopped:false,status:o}}function matchesPathname(e,t){for(const n of t){if(e===n){return n}}return undefined}function toResolvedQuery(e){const t={};for(const[n,a]of e.searchParams.entries()){const e=t[n];if(e===undefined){t[n]=a;continue}t[n]=Array.isArray(e)?[...e,a]:[e,a]}return t}function mergeDestinationQueryIntoUrl(e,t){const n=new URL(e.toString());const a=t.split("?")[1];if(!a){return n}const r=new URLSearchParams(a);for(const[e,t]of r.entries()){n.searchParams.set(e,t)}return n}function withResolvedInvocationTarget({result:e,url:t,resolvedPathname:n,invocationPathname:a}){const r=toResolvedQuery(t);return{...e,resolvedPathname:n,resolvedQuery:r,invocationTarget:{pathname:a,query:r}}}function matchDynamicRoute(e,t){const n=new RegExp(t.sourceRegex);const a=e.match(n);if(!a){return{matched:false}}const r={};for(let e=1;e<a.length;e++){if(a[e]!==undefined){r[String(e)]=a[e]}}if(a.groups){Object.assign(r,a.groups)}return{matched:true,params:r,regexMatches:a}}function applyOnMatchHeaders(e,t,n,a){const r=new Headers(a);for(const a of e){const e=matchRoute(a,t,n);if(e.matched&&e.headers){for(const[t,n]of Object.entries(e.headers)){r.set(t,n)}}}return r}function checkDynamicRoutes(e,t,n,a,r,s,o,i,c,l){let d=t;if(l&&c){d=denormalizeNextDataUrl(t,o,i)}for(const t of e){const e=matchDynamicRoute(d.pathname,t);if(e.matched){const o=checkHasConditions(t.has,d,a);const i=checkMissingConditions(t.missing,d,a);if(o.matched&&i){const i=t.destination?replaceDestination(t.destination,e.regexMatches||null,o.captures):undefined;const c=i?i.split("?")[0]:d.pathname;const l=matchesPathname(c,n);if(l){const t=i?mergeDestinationQueryIntoUrl(d,i):d;const n=applyOnMatchHeaders(s,t,a,r);const o=withResolvedInvocationTarget({result:{routeMatches:e.params,resolvedHeaders:n},url:t,resolvedPathname:l,invocationPathname:d.pathname});return{matched:true,result:o,resetUrl:d}}}}}return{matched:false}}async function resolveRoutes(e){const{url:t,basePath:n,requestBody:a,headers:r,pathnames:s,routes:o,invokeMiddleware:i,buildId:c,i18n:l}=e;const{shouldNormalizeNextData:d}=o;let u=new URL(t.toString());let f=new Headers(r);let h=new Headers;let m;const p=t.origin;let g=false;if(d){const e=`${n}/_next/data/${c}/`;g=t.pathname.startsWith(e);if(g){u=normalizeNextDataUrl(u,n,c)}}if(l&&!g){const e=u.pathname.startsWith(n)?u.pathname.slice(n.length)||"/":u.pathname;if(!e.startsWith("/_next/")&&!e.startsWith("/api/")){const t=u.hostname;const a=f.get("cookie")||undefined;const r=f.get("accept-language")||undefined;const s=normalizeLocalePath(e,l.locales);const o=!!s.detectedLocale;const i=detectDomainLocale(l.domains,t);const c=i?.defaultLocale||l.defaultLocale;let d=s.detectedLocale||c;if(l.localeDetection!==false&&!o){const s=detectLocale({pathname:e,hostname:t,cookieHeader:a,acceptLanguageHeader:r,i18n:l});d=s.locale;if(d!==c){const a=detectDomainLocale(l.domains,undefined,d);if(a&&a.domain!==t){const t=a.http?"http":"https";const r=d===a.defaultLocale?"":`/${d}`;const s=new URL(`${t}://${a.domain}${n}${r}${e}${u.search}`);return{redirect:{url:s,status:307},resolvedHeaders:h}}if(!a||a&&a.domain===t){const t=new URL(u.toString());t.pathname=`${n}/${d}${e}`;return{redirect:{url:t,status:307},resolvedHeaders:h}}}}if(!o){const t=d||i?.defaultLocale||l.defaultLocale;u.pathname=`${n}/${t}${e}`}}}const w=processRoutes(o.beforeMiddleware,u,f,h,p);if(w.status){m=w.status}if(w.redirect){return{redirect:w.redirect,resolvedHeaders:h,status:m}}if(w.externalRewrite){return{externalRewrite:w.externalRewrite,resolvedHeaders:h,status:m}}u=w.url;if(g&&d){u=denormalizeNextDataUrl(u,n,c)}const L=await i({url:u,headers:f,requestBody:a});if(L.bodySent){return{middlewareResponded:true}}if(L.requestHeaders){f=new Headers(L.requestHeaders)}if(L.responseHeaders){L.responseHeaders.forEach(((e,t)=>{if(t.toLowerCase()==="set-cookie"){h.append(t,e)}else{h.set(t,e)}}))}if(L.redirect){if(!h.has("location")){h.set("Location",L.redirect.url.toString())}return{resolvedHeaders:h,status:L.redirect.status}}if(L.rewrite){u=L.rewrite;if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}}if(g&&d){u=normalizeNextDataUrl(u,n,c)}const R=processRoutes(o.beforeFiles,u,f,h,p);if(R.status){m=R.status}if(R.redirect){return{redirect:R.redirect,resolvedHeaders:h,status:m}}if(R.externalRewrite){return{externalRewrite:R.externalRewrite,resolvedHeaders:h,status:m}}u=R.url;if(g&&d){u=denormalizeNextDataUrl(u,n,c)}let y=matchesPathname(u.pathname,s);if(y){for(const e of o.dynamicRoutes){const t=matchDynamicRoute(u.pathname,e);if(t.matched){const n=checkHasConditions(e.has,u,f);const a=checkMissingConditions(e.missing,u,f);if(n.matched&&a){const a=e.destination?replaceDestination(e.destination,t.regexMatches||null,n.captures):undefined;const r=a?mergeDestinationQueryIntoUrl(u,a):u;const s=applyOnMatchHeaders(o.onMatch,r,f,h);return withResolvedInvocationTarget({result:{routeMatches:t.params,resolvedHeaders:s,status:m},url:r,resolvedPathname:y,invocationPathname:u.pathname})}}}const e=applyOnMatchHeaders(o.onMatch,u,f,h);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:m},url:u,resolvedPathname:y,invocationPathname:u.pathname})}if(g&&d){u=normalizeNextDataUrl(u,n,c)}for(const e of o.afterFiles){const t=matchRoute(e,u,f);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){h.set(e,n)}}if(e.status){m=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:h,status:m}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:h,status:m}}u=applyDestination(u,t.destination);if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}const a=checkDynamicRoutes(o.dynamicRoutes,u,s,f,h,o.onMatch,n,c,d,g);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:m}}let r=u;if(g&&d){r=denormalizeNextDataUrl(u,n,c)}y=matchesPathname(r.pathname,s);if(y){const e=applyOnMatchHeaders(o.onMatch,r,f,h);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:m},url:r,resolvedPathname:y,invocationPathname:r.pathname})}}}}for(const e of o.dynamicRoutes){const t=matchDynamicRoute(u.pathname,e);if(t.matched){const n=checkHasConditions(e.has,u,f);const a=checkMissingConditions(e.missing,u,f);if(n.matched&&a){const a=e.destination?replaceDestination(e.destination,t.regexMatches||null,n.captures):undefined;const r=a?a.split("?")[0]:u.pathname;y=matchesPathname(r,s);if(y){const e=a?mergeDestinationQueryIntoUrl(u,a):u;const n=applyOnMatchHeaders(o.onMatch,e,f,h);return withResolvedInvocationTarget({result:{routeMatches:t.params,resolvedHeaders:n,status:m},url:e,resolvedPathname:y,invocationPathname:u.pathname})}}}}for(const e of o.fallback){const t=matchRoute(e,u,f);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){h.set(e,n)}}if(e.status){m=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:h,status:m}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:h,status:m}}u=applyDestination(u,t.destination);if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}const a=checkDynamicRoutes(o.dynamicRoutes,u,s,f,h,o.onMatch,n,c,d,g);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:m}}let r=u;if(g&&d){r=denormalizeNextDataUrl(u,n,c)}y=matchesPathname(r.pathname,s);if(y){const e=applyOnMatchHeaders(o.onMatch,r,f,h);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:m},url:r,resolvedPathname:y,invocationPathname:r.pathname})}}}}return{resolvedHeaders:h,status:m}}function responseToMiddlewareResult(e,t,a){const r={};const s={};e.headers.forEach(((e,t)=>{if(s[t]){const n=s[t];if(Array.isArray(n)){n.push(e)}else{s[t]=[n,e]}}else{s[t]=e}}));if(s["x-middleware-override-headers"]){const e=new Set;let n=s["x-middleware-override-headers"];if(typeof n==="string"){n=n.split(",")}for(const t of n){e.add(t.trim())}delete s["x-middleware-override-headers"];const a=[];t.forEach(((t,n)=>{if(!e.has(n)){a.push(n)}}));for(const e of a){t.delete(e)}for(const n of e.keys()){const e="x-middleware-request-"+n;const a=s[e];if(a===undefined||a===null){t.delete(n)}else if(Array.isArray(a)){t.set(n,a[0]);for(let e=1;e<a.length;e++){t.append(n,a[e])}}else{t.set(n,a)}delete s[e]}}if(!s["x-middleware-rewrite"]&&!s["x-middleware-next"]&&!s["location"]){s["x-middleware-refresh"]="1"}delete s["x-middleware-next"];const o=new Headers;for(const[e,n]of Object.entries(s)){if(["content-length","x-middleware-rewrite","x-middleware-redirect","x-middleware-refresh"].includes(e)){continue}if(e==="x-middleware-set-cookie"){if(n!==undefined){if(Array.isArray(n)){for(const a of n){t.append(e,a)}}else{t.set(e,n)}}continue}if(n!==undefined){if(Array.isArray(n)){for(const a of n){o.append(e,a);t.append(e,a)}}else{o.set(e,n);t.set(e,n)}}}r.responseHeaders=o;r.requestHeaders=t;if(s["x-middleware-rewrite"]){const e=s["x-middleware-rewrite"];const t=getRelativeURL(e,a.toString());o.set("x-middleware-rewrite",t);try{const e=new URL(t,a);if(e.origin!==a.origin){r.rewrite=e;return r}r.rewrite=e}catch{r.rewrite=new URL(t,a)}}if(s["location"]){const t=s["location"];const i=n.has(e.status);if(i){const n=getRelativeURL(t,a.toString());o.set("location",n);try{const t=new URL(n,a);r.redirect={url:t,status:e.status};return r}catch{r.redirect={url:new URL(n,a),status:e.status};return r}}else{o.set("location",t);return r}}if(s["x-middleware-refresh"]){r.bodySent=true;return r}return r}function getRelativeURL(e,t){try{const n=new URL(e,t);const a=new URL(t);if(n.origin===a.origin){return n.pathname+n.search+n.hash}return n.toString()}catch{return e}}const n=new Set([301,302,303,307,308]);module.exports=t})();
|
package/dist/types.d.ts
CHANGED
|
@@ -59,6 +59,18 @@ export type ResolveRoutesParams = {
|
|
|
59
59
|
};
|
|
60
60
|
invokeMiddleware: (ctx: MiddlewareContext) => Promise<MiddlewareResult>;
|
|
61
61
|
};
|
|
62
|
+
export type ResolveRoutesQueryValue = string | string[];
|
|
63
|
+
export type ResolveRoutesQuery = Record<string, ResolveRoutesQueryValue>;
|
|
64
|
+
export type RouteInvocationTarget = {
|
|
65
|
+
/**
|
|
66
|
+
* Concrete pathname that should be invoked after routing resolution.
|
|
67
|
+
*/
|
|
68
|
+
pathname: string;
|
|
69
|
+
/**
|
|
70
|
+
* Concrete query that should be invoked after routing resolution.
|
|
71
|
+
*/
|
|
72
|
+
query: ResolveRoutesQuery;
|
|
73
|
+
};
|
|
62
74
|
export type ResolveRoutesResult = {
|
|
63
75
|
middlewareResponded?: boolean;
|
|
64
76
|
externalRewrite?: URL;
|
|
@@ -66,7 +78,19 @@ export type ResolveRoutesResult = {
|
|
|
66
78
|
url: URL;
|
|
67
79
|
status: number;
|
|
68
80
|
};
|
|
69
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Resolved pathname selected by route matching. For dynamic routes this is
|
|
83
|
+
* the matched template pathname.
|
|
84
|
+
*/
|
|
85
|
+
resolvedPathname?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Merged query produced by rewrite/middleware routing.
|
|
88
|
+
*/
|
|
89
|
+
resolvedQuery?: ResolveRoutesQuery;
|
|
90
|
+
/**
|
|
91
|
+
* Concrete invocation target to use when invoking the resolved route/module.
|
|
92
|
+
*/
|
|
93
|
+
invocationTarget?: RouteInvocationTarget;
|
|
70
94
|
resolvedHeaders?: Headers;
|
|
71
95
|
status?: number;
|
|
72
96
|
routeMatches?: Record<string, string>;
|