forked from vercel/next.js
/
server-route-utils.ts
165 lines (149 loc) · 4.38 KB
/
server-route-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type {
Header,
Redirect,
Rewrite,
RouteType,
} from '../lib/load-custom-routes'
import type { Route } from './router'
import type { BaseNextRequest } from './base-http'
import type { ParsedUrlQuery } from 'querystring'
import { getRedirectStatus, modifyRouteRegex } from '../lib/load-custom-routes'
import pathMatch from '../shared/lib/router/utils/path-match'
import {
compileNonPath,
prepareDestination,
} from '../shared/lib/router/utils/prepare-destination'
import { getRequestMeta } from './request-meta'
import { stringify as stringifyQs } from 'querystring'
import { format as formatUrl } from 'url'
import { normalizeRepeatedSlashes } from '../shared/lib/utils'
const getCustomRouteMatcher = pathMatch(true)
export const getCustomRoute = ({
type,
rule,
restrictedRedirectPaths,
}: {
rule: Rewrite | Redirect | Header
type: RouteType
restrictedRedirectPaths: string[]
}) => {
const match = getCustomRouteMatcher(
rule.source,
!(rule as any).internal
? (regex: string) =>
modifyRouteRegex(
regex,
type === 'redirect' ? restrictedRedirectPaths : undefined
)
: undefined
)
return {
...rule,
type,
match,
name: type,
fn: async (_req, _res, _params, _parsedUrl) => ({ finished: false }),
} as Route & Rewrite & Header
}
export const createHeaderRoute = ({
rule,
restrictedRedirectPaths,
}: {
rule: Header
restrictedRedirectPaths: string[]
}) => {
const headerRoute = getCustomRoute({
type: 'header',
rule,
restrictedRedirectPaths,
})
return {
match: headerRoute.match,
has: headerRoute.has,
type: headerRoute.type,
name: `${headerRoute.type} ${headerRoute.source} header route`,
fn: async (_req, res, params, _parsedUrl) => {
const hasParams = Object.keys(params).length > 0
for (const header of (headerRoute as Header).headers) {
let { key, value } = header
if (hasParams) {
key = compileNonPath(key, params)
value = compileNonPath(value, params)
}
res.setHeader(key, value)
}
return { finished: false }
},
} as Route
}
export const createRedirectRoute = ({
rule,
restrictedRedirectPaths,
}: {
rule: Redirect
restrictedRedirectPaths: string[]
}) => {
const redirectRoute = getCustomRoute({
type: 'redirect',
rule,
restrictedRedirectPaths,
})
return {
internal: redirectRoute.internal,
type: redirectRoute.type,
match: redirectRoute.match,
has: redirectRoute.has,
statusCode: redirectRoute.statusCode,
name: `Redirect route ${redirectRoute.source}`,
fn: async (req, res, params, parsedUrl) => {
const { parsedDestination } = prepareDestination({
appendParamsToQuery: false,
destination: redirectRoute.destination,
params: params,
query: parsedUrl.query,
})
const { query } = parsedDestination
delete (parsedDestination as any).query
parsedDestination.search = stringifyQuery(req, query)
let updatedDestination = formatUrl(parsedDestination)
if (updatedDestination.startsWith('/')) {
updatedDestination = normalizeRepeatedSlashes(updatedDestination)
}
res
.redirect(
updatedDestination,
getRedirectStatus(redirectRoute as Redirect)
)
.body(updatedDestination)
.send()
return {
finished: true,
}
},
} as Route
}
// since initial query values are decoded by querystring.parse
// we need to re-encode them here but still allow passing through
// values from rewrites/redirects
export const stringifyQuery = (req: BaseNextRequest, query: ParsedUrlQuery) => {
const initialQuery = getRequestMeta(req, '__NEXT_INIT_QUERY') || {}
const initialQueryValues: Array<string | string[]> =
Object.values(initialQuery)
return stringifyQs(query, undefined, undefined, {
encodeURIComponent(value) {
if (
value in initialQuery ||
initialQueryValues.some((initialQueryVal: string | string[]) => {
// `value` always refers to a query value, even if it's nested in an array
return Array.isArray(initialQueryVal)
? initialQueryVal.includes(value)
: initialQueryVal === value
})
) {
// Encode keys and values from initial query
return encodeURIComponent(value)
}
return value
},
})
}