@next/routing 16.1.1-canary.7

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 ADDED
@@ -0,0 +1,89 @@
1
+ # @next/routing
2
+
3
+ Shared route resolving package for Next.js.
4
+
5
+ **NOTE: This package is experimental and will become stable along with adapters API**
6
+
7
+ ## Overview
8
+
9
+ This package provides a comprehensive route resolution system that handles rewrites, redirects, middleware invocation, and dynamic route matching with support for conditional routing based on headers, cookies, queries, and host.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install @next/routing
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```typescript
20
+ import { resolveRoutes } from '@next/routing'
21
+
22
+ const result = await resolveRoutes({
23
+ url: new URL('https://example.com/api/users'),
24
+ basePath: '',
25
+ requestBody: readableStream,
26
+ headers: new Headers(),
27
+ pathnames: ['/api/users', '/api/posts'],
28
+ routes: {
29
+ beforeMiddleware: [],
30
+ beforeFiles: [],
31
+ afterFiles: [],
32
+ dynamicRoutes: [],
33
+ onMatch: [],
34
+ fallback: [],
35
+ },
36
+ invokeMiddleware: async (ctx) => {
37
+ // Your middleware logic
38
+ return {}
39
+ },
40
+ })
41
+
42
+ if (result.matchedPathname) {
43
+ console.log('Matched:', result.matchedPathname)
44
+ }
45
+ ```
46
+
47
+ ## Route Resolution Flow
48
+
49
+ 1. **beforeMiddleware routes** - Applied before middleware execution
50
+ 2. **invokeMiddleware** - Custom middleware logic
51
+ 3. **beforeFiles routes** - Applied before checking filesystem
52
+ 4. **Static pathname matching** - Check against provided pathnames
53
+ 5. **afterFiles routes** - Applied after filesystem checks
54
+ 6. **dynamicRoutes** - Dynamic route matching with parameter extraction
55
+ 7. **fallback routes** - Final fallback routes
56
+
57
+ ## Route Configuration
58
+
59
+ Each route can have:
60
+
61
+ - `sourceRegex` - Regular expression to match against pathname
62
+ - `destination` - Destination path with support for replacements ($1, $name)
63
+ - `headers` - Headers to apply on match
64
+ - `has` - Conditions that must match
65
+ - `missing` - Conditions that must not match
66
+ - `status` - HTTP status code (3xx for redirects)
67
+
68
+ ### Redirects
69
+
70
+ When a route has:
71
+ - A redirect status code (300-399)
72
+ - Headers containing `Location` or `Refresh`
73
+
74
+ The routing will end immediately and return a `redirect` result with the destination URL and status code.
75
+
76
+ ### Has/Missing Conditions
77
+
78
+ Conditions support:
79
+
80
+ - `header` - Match HTTP headers
81
+ - `cookie` - Match cookies
82
+ - `query` - Match query parameters
83
+ - `host` - Match hostname
84
+
85
+ Values can be:
86
+
87
+ - `undefined` - Match if key exists
88
+ - String - Direct string match
89
+ - Regex string - Match against regex pattern
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Replaces $1, $2, etc. and $name placeholders in the destination string
3
+ * with matches from the regex and has conditions
4
+ */
5
+ export declare function replaceDestination(destination: string, regexMatches: RegExpMatchArray | null, hasCaptures: Record<string, string>): string;
6
+ /**
7
+ * Checks if a destination is an external rewrite (starts with http/https)
8
+ */
9
+ export declare function isExternalDestination(destination: string): boolean;
10
+ /**
11
+ * Applies a destination to a URL, updating the pathname or creating a new URL
12
+ * if it's external
13
+ */
14
+ export declare function applyDestination(currentUrl: URL, destination: string): URL;
15
+ /**
16
+ * Checks if a status code is a redirect status code
17
+ */
18
+ export declare function isRedirectStatus(status: number | undefined): boolean;
19
+ /**
20
+ * Checks if headers contain redirect headers (Location or Refresh)
21
+ */
22
+ export declare function hasRedirectHeaders(headers: Record<string, string>): boolean;
package/dist/i18n.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * i18n utilities for locale detection and handling
3
+ */
4
+ export interface I18nDomain {
5
+ defaultLocale: string;
6
+ domain: string;
7
+ http?: true;
8
+ locales?: string[];
9
+ }
10
+ export interface I18nConfig {
11
+ defaultLocale: string;
12
+ domains?: I18nDomain[];
13
+ localeDetection?: false;
14
+ locales: string[];
15
+ }
16
+ /**
17
+ * Detects the domain locale based on hostname or detected locale
18
+ */
19
+ export declare function detectDomainLocale(domains: I18nDomain[] | undefined, hostname: string | undefined, detectedLocale?: string): I18nDomain | undefined;
20
+ /**
21
+ * Normalizes a pathname by removing the locale prefix if present
22
+ */
23
+ export declare function normalizeLocalePath(pathname: string, locales: string[]): {
24
+ pathname: string;
25
+ detectedLocale?: string;
26
+ };
27
+ /**
28
+ * Parses the Accept-Language header and returns the best matching locale
29
+ */
30
+ export declare function getAcceptLanguageLocale(acceptLanguageHeader: string, locales: string[]): string | undefined;
31
+ /**
32
+ * Gets the locale from the NEXT_LOCALE cookie
33
+ */
34
+ export declare function getCookieLocale(cookieHeader: string | undefined, locales: string[]): string | undefined;
35
+ /**
36
+ * Detects the appropriate locale based on path, domain, cookie, and accept-language
37
+ */
38
+ export declare function detectLocale(params: {
39
+ pathname: string;
40
+ hostname: string | undefined;
41
+ cookieHeader: string | undefined;
42
+ acceptLanguageHeader: string | undefined;
43
+ i18n: I18nConfig;
44
+ }): {
45
+ locale: string;
46
+ pathnameWithoutLocale: string;
47
+ localeInPath: boolean;
48
+ };
@@ -0,0 +1,5 @@
1
+ export { resolveRoutes } from './resolve-routes';
2
+ export type { RouteHas, Route, MiddlewareContext, MiddlewareResult, ResolveRoutesParams, ResolveRoutesResult, } from './types';
3
+ export type { I18nConfig, I18nDomain } from './i18n';
4
+ export { detectLocale, detectDomainLocale, normalizeLocalePath, getAcceptLanguageLocale, getCookieLocale, } from './i18n';
5
+ export { responseToMiddlewareResult } from './middleware';
package/dist/index.js ADDED
@@ -0,0 +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 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 d=detectDomainLocale(s.domains,n);if(d){return{locale:d.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;return{matched:true,destination:i,regexMatches:r,hasCaptures:s.captures}}function processRoutes(e,t,n,a){let r=t;let s;for(const t of e){const e=matchRoute(t,r,n);if(e.matched){if(t.headers){for(const[e,a]of Object.entries(t.headers)){n.set(e,a)}}if(t.status){s=t.status}if(e.destination){if(isRedirectStatus(t.status)&&t.headers&&hasRedirectHeaders(t.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:s}}if(isExternalDestination(e.destination)){return{url:r,externalRewrite:new URL(e.destination),stopped:true,status:s}}r=applyDestination(r,e.destination);if(r.origin!==a){return{url:r,externalRewrite:r,stopped:true,status:s}}}}}return{url:r,stopped:false,status:s}}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 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}}function applyOnMatchHeaders(e,t){const n=new Headers(t);for(const t of e){if(t.headers){for(const[e,a]of Object.entries(t.headers)){n.set(e,a)}}}return n}function checkDynamicRoutes(e,t,n,a,r,s,o,i,c){let d=t;if(c&&i){d=denormalizeNextDataUrl(t,s,o)}for(const t of e){const e=matchDynamicRoute(d.pathname,t);if(e.matched){const s=checkHasConditions(t.has,d,a);const o=checkMissingConditions(t.missing,d,a);if(s.matched&&o){const t=matchesPathname(d.pathname,n);if(t){const n=applyOnMatchHeaders(r,a);return{matched:true,result:{matchedPathname:t,routeMatches:e.params,resolvedHeaders:n},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:d}=e;const{shouldNormalizeNextData:l}=o;let u=new URL(t.toString());let f=new Headers(r);let h;const m=t.origin;let p=false;if(l){const e=`${n}/_next/data/${c}/`;p=t.pathname.startsWith(e);if(p){u=normalizeNextDataUrl(u,n,c)}}if(d&&!p){const e=u.pathname.startsWith(n)?u.pathname.slice(n.length)||"/":u.pathname;if(!e.startsWith("/_next/")){const t=u.hostname;const a=f.get("cookie")||undefined;const r=f.get("accept-language")||undefined;const s=normalizeLocalePath(e,d.locales);const o=!!s.detectedLocale;const i=detectDomainLocale(d.domains,t);const c=i?.defaultLocale||d.defaultLocale;let l=s.detectedLocale||c;if(d.localeDetection!==false&&!o){const s=detectLocale({pathname:e,hostname:t,cookieHeader:a,acceptLanguageHeader:r,i18n:d});l=s.locale;if(l!==c){const a=detectDomainLocale(d.domains,undefined,l);if(a&&a.domain!==t){const t=a.http?"http":"https";const r=l===a.defaultLocale?"":`/${l}`;const s=new URL(`${t}://${a.domain}${n}${r}${e}${u.search}`);return{redirect:{url:s,status:307},resolvedHeaders:f}}if(!a||a&&a.domain===t){const t=new URL(u.toString());t.pathname=`${n}/${l}${e}`;return{redirect:{url:t,status:307},resolvedHeaders:f}}}}if(!o){const t=l||i?.defaultLocale||d.defaultLocale;u.pathname=`${n}/${t}${e}`}}}const g=processRoutes(o.beforeMiddleware,u,f,m);if(g.status){h=g.status}if(g.redirect){return{redirect:g.redirect,resolvedHeaders:f,status:h}}if(g.externalRewrite){return{externalRewrite:g.externalRewrite,resolvedHeaders:f,status:h}}u=g.url;if(p&&l){u=denormalizeNextDataUrl(u,n,c)}const w=await i({url:u,headers:f,requestBody:a});if(w.bodySent){return{middlewareResponded:true}}if(w.requestHeaders){f=new Headers(w.requestHeaders)}if(w.redirect){f.set("Location",w.redirect.url.toString());return{resolvedHeaders:f,status:w.redirect.status}}if(w.rewrite){u=w.rewrite;if(u.origin!==m){return{externalRewrite:u,resolvedHeaders:f,status:h}}}if(p&&l){u=normalizeNextDataUrl(u,n,c)}const L=processRoutes(o.beforeFiles,u,f,m);if(L.status){h=L.status}if(L.redirect){return{redirect:L.redirect,resolvedHeaders:f,status:h}}if(L.externalRewrite){return{externalRewrite:L.externalRewrite,resolvedHeaders:f,status:h}}u=L.url;if(p&&l){u=denormalizeNextDataUrl(u,n,c)}let R=matchesPathname(u.pathname,s);if(R){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,f);return{matchedPathname:R,routeMatches:t.params,resolvedHeaders:e,status:h}}}}const e=applyOnMatchHeaders(o.onMatch,f);return{matchedPathname:R,resolvedHeaders:e,status:h}}if(p&&l){u=normalizeNextDataUrl(u,n,c)}for(const e of o.afterFiles){const t=matchRoute(e,u,f);if(t.matched){if(e.headers){for(const[t,n]of Object.entries(e.headers)){f.set(t,n)}}if(e.status){h=e.status}if(t.destination){if(isRedirectStatus(e.status)&&e.headers&&hasRedirectHeaders(e.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:f,status:h}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:f,status:h}}u=applyDestination(u,t.destination);if(u.origin!==m){return{externalRewrite:u,resolvedHeaders:f,status:h}}const a=checkDynamicRoutes(o.dynamicRoutes,u,s,f,o.onMatch,n,c,l,p);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:h}}let r=u;if(p&&l){r=denormalizeNextDataUrl(u,n,c)}R=matchesPathname(r.pathname,s);if(R){const e=applyOnMatchHeaders(o.onMatch,f);return{matchedPathname:R,resolvedHeaders:e,status:h}}}}}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){R=matchesPathname(u.pathname,s);if(R){const e=applyOnMatchHeaders(o.onMatch,f);return{matchedPathname:R,routeMatches:t.params,resolvedHeaders:e,status:h}}}}}for(const e of o.fallback){const t=matchRoute(e,u,f);if(t.matched){if(e.headers){for(const[t,n]of Object.entries(e.headers)){f.set(t,n)}}if(e.status){h=e.status}if(t.destination){if(isRedirectStatus(e.status)&&e.headers&&hasRedirectHeaders(e.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:f,status:h}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:f,status:h}}u=applyDestination(u,t.destination);if(u.origin!==m){return{externalRewrite:u,resolvedHeaders:f,status:h}}const a=checkDynamicRoutes(o.dynamicRoutes,u,s,f,o.onMatch,n,c,l,p);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:h}}let r=u;if(p&&l){r=denormalizeNextDataUrl(u,n,c)}R=matchesPathname(r.pathname,s);if(R){const e=applyOnMatchHeaders(o.onMatch,f);return{matchedPathname:R,resolvedHeaders:e,status:h}}}}}return{resolvedHeaders:f,status:h}}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})();
@@ -0,0 +1,12 @@
1
+ import type { RouteHas } from './types';
2
+ /**
3
+ * Checks if all "has" conditions are satisfied
4
+ */
5
+ export declare function checkHasConditions(has: RouteHas[] | undefined, url: URL, headers: Headers): {
6
+ matched: boolean;
7
+ captures: Record<string, string>;
8
+ };
9
+ /**
10
+ * Checks if all "missing" conditions are satisfied (i.e., none of them match)
11
+ */
12
+ export declare function checkMissingConditions(missing: RouteHas[] | undefined, url: URL, headers: Headers): boolean;
@@ -0,0 +1,12 @@
1
+ import type { MiddlewareResult } from './types';
2
+ /**
3
+ * Converts a middleware Response object to a MiddlewareResult.
4
+ * This function processes middleware response headers and applies transformations
5
+ * such as header overrides, rewrites, redirects, and refresh signals.
6
+ *
7
+ * @param response - The Response object returned from middleware
8
+ * @param requestHeaders - The request Headers object to be mutated
9
+ * @param url - The original request URL
10
+ * @returns A MiddlewareResult object with processed headers and routing information
11
+ */
12
+ export declare function responseToMiddlewareResult(response: Response, requestHeaders: Headers, url: URL): MiddlewareResult;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Normalizes Next.js data URL by removing /_next/data/{buildId}/ prefix and .json extension
3
+ * ${basePath}/_next/data/$buildId/$path.json -> ${basePath}/$path
4
+ */
5
+ export declare function normalizeNextDataUrl(url: URL, basePath: string, buildId: string): URL;
6
+ /**
7
+ * Denormalizes URL by adding /_next/data/{buildId}/ prefix and .json extension
8
+ * ${basePath}/$path -> ${basePath}/_next/data/$buildId/$path.json
9
+ */
10
+ export declare function denormalizeNextDataUrl(url: URL, basePath: string, buildId: string): URL;
@@ -0,0 +1,2 @@
1
+ import type { ResolveRoutesParams, ResolveRoutesResult } from './types';
2
+ export declare function resolveRoutes(params: ResolveRoutesParams): Promise<ResolveRoutesResult>;
@@ -0,0 +1,73 @@
1
+ export type RouteHas = {
2
+ type: 'header' | 'cookie' | 'query';
3
+ key: string;
4
+ value?: string;
5
+ } | {
6
+ type: 'host';
7
+ key?: undefined;
8
+ value: string;
9
+ };
10
+ export type Route = {
11
+ sourceRegex: string;
12
+ destination?: string;
13
+ headers?: Record<string, string>;
14
+ has?: RouteHas[];
15
+ missing?: RouteHas[];
16
+ status?: number;
17
+ };
18
+ export type MiddlewareContext = {
19
+ url: URL;
20
+ headers: Headers;
21
+ requestBody: ReadableStream;
22
+ };
23
+ export type MiddlewareResult = {
24
+ bodySent?: boolean;
25
+ requestHeaders?: Headers;
26
+ responseHeaders?: Headers;
27
+ redirect?: {
28
+ url: URL;
29
+ status: number;
30
+ };
31
+ rewrite?: URL;
32
+ };
33
+ export type ResolveRoutesParams = {
34
+ url: URL;
35
+ buildId: string;
36
+ basePath: string;
37
+ requestBody: ReadableStream;
38
+ headers: Headers;
39
+ pathnames: string[];
40
+ i18n?: {
41
+ defaultLocale: string;
42
+ domains?: Array<{
43
+ defaultLocale: string;
44
+ domain: string;
45
+ http?: true;
46
+ locales?: string[];
47
+ }>;
48
+ localeDetection?: false;
49
+ locales: string[];
50
+ };
51
+ routes: {
52
+ beforeMiddleware: Array<Route>;
53
+ beforeFiles: Array<Route>;
54
+ afterFiles: Array<Route>;
55
+ dynamicRoutes: Array<Route>;
56
+ onMatch: Array<Route>;
57
+ fallback: Array<Route>;
58
+ shouldNormalizeNextData?: boolean;
59
+ };
60
+ invokeMiddleware: (ctx: MiddlewareContext) => Promise<MiddlewareResult>;
61
+ };
62
+ export type ResolveRoutesResult = {
63
+ middlewareResponded?: boolean;
64
+ externalRewrite?: URL;
65
+ redirect?: {
66
+ url: URL;
67
+ status: number;
68
+ };
69
+ matchedPathname?: string;
70
+ resolvedHeaders?: Headers;
71
+ status?: number;
72
+ routeMatches?: Record<string, string>;
73
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@next/routing",
3
+ "version": "16.1.1-canary.7",
4
+ "keywords": [
5
+ "react",
6
+ "next",
7
+ "next.js",
8
+ "routing"
9
+ ],
10
+ "description": "Next.js shared route resolving",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/vercel/next.js",
14
+ "directory": "packages/next-routing"
15
+ },
16
+ "author": "Next.js Team <support@vercel.com>",
17
+ "license": "MIT",
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "scripts": {
24
+ "dev": "ncc build ./src/index.ts -w -o dist/",
25
+ "prerelease": "node ../../scripts/rm.mjs dist",
26
+ "types": "tsc --declaration --emitDeclarationOnly --declarationDir dist",
27
+ "release": "ncc build ./src/index.ts -o ./dist/ --minify --no-cache --no-source-map-register",
28
+ "build": "pnpm release && pnpm types",
29
+ "test": "jest",
30
+ "test:watch": "jest --watch",
31
+ "prepublishOnly": "cd ../../ && turbo run build"
32
+ },
33
+ "devDependencies": {
34
+ "@types/jest": "^29.5.0",
35
+ "@vercel/ncc": "0.34.0",
36
+ "jest": "^29.5.0",
37
+ "ts-jest": "^29.1.0"
38
+ }
39
+ }