-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathrouting.ts
451 lines (395 loc) · 14.8 KB
/
routing.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**
* Various router utils ported to Deno from Next.js source
* Licence: https://github.com/vercel/next.js/blob/7280c3ced186bb9a7ae3d7012613ef93f20b0fa9/license.md
*
* Some types have been re-implemented to be more compatible with Deno or avoid chains of dependent files
*/
import type { Key } from '../vendor/deno.land/x/[email protected]/index.ts'
import { compile, pathToRegexp } from '../vendor/deno.land/x/[email protected]/index.ts'
import { getCookies } from '../vendor/deno.land/[email protected]/http/cookie.ts'
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ Inlined/re-implemented types │
└─────────────────────────────────────────────────────────────────────────┘
*/
export interface ParsedUrlQuery {
[key: string]: string | string[]
}
export interface Params {
[param: string]: any
}
export type RouteHas =
| {
type: 'header' | 'query' | 'cookie'
key: string
value?: string
}
| {
type: 'host'
key?: undefined
value: string
}
export type Rewrite = {
source: string
destination: string
basePath?: false
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
regex: string
}
export type Header = {
source: string
basePath?: false
locale?: false
headers: Array<{ key: string; value: string }>
has?: RouteHas[]
missing?: RouteHas[]
regex: string
}
export type Redirect = {
source: string
destination: string
basePath?: false
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
statusCode?: number
permanent?: boolean
regex: string
}
export type DynamicRoute = {
page: string
regex: string
namedRegex?: string
routeKeys?: { [key: string]: string }
}
export type RoutesManifest = {
basePath: string
redirects: Redirect[]
headers: Header[]
rewrites: {
beforeFiles: Rewrite[]
afterFiles: Rewrite[]
fallback: Rewrite[]
}
dynamicRoutes: DynamicRoute[]
}
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ packages/next/src/shared/lib/escape-regexp.ts │
└─────────────────────────────────────────────────────────────────────────┘
*/
// regexp is based on https://github.com/sindresorhus/escape-string-regexp
const reHasRegExp = /[|\\{}()[\]^$+*?.-]/
const reReplaceRegExp = /[|\\{}()[\]^$+*?.-]/g
export function escapeStringRegexp(str: string) {
// see also: https://github.com/lodash/lodash/blob/2da024c3b4f9947a48517639de7560457cd4ec6c/escapeRegExp.js#L23
if (reHasRegExp.test(str)) {
return str.replace(reReplaceRegExp, '\\$&')
}
return str
}
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ packages/next/src/shared/lib/router/utils/querystring.ts │
└─────────────────────────────────────────────────────────────────────────┘
*/
export function searchParamsToUrlQuery(searchParams: URLSearchParams): ParsedUrlQuery {
const query: ParsedUrlQuery = {}
searchParams.forEach((value, key) => {
if (typeof query[key] === 'undefined') {
query[key] = value
} else if (Array.isArray(query[key])) {
;(query[key] as string[]).push(value)
} else {
query[key] = [query[key] as string, value]
}
})
return query
}
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ packages/next/src/shared/lib/router/utils/parse-url.ts │
└─────────────────────────────────────────────────────────────────────────┘
*/
interface ParsedUrl {
hash: string
hostname?: string | null
href: string
pathname: string
port?: string | null
protocol?: string | null
query: ParsedUrlQuery
search: string
}
export function parseUrl(url: string): ParsedUrl {
const parsedURL = url.startsWith('/') ? new URL(url, 'http://n') : new URL(url)
return {
hash: parsedURL.hash,
hostname: parsedURL.hostname,
href: parsedURL.href,
pathname: parsedURL.pathname,
port: parsedURL.port,
protocol: parsedURL.protocol,
query: searchParamsToUrlQuery(parsedURL.searchParams),
search: parsedURL.search,
}
}
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ packages/next/src/shared/lib/router/utils/prepare-destination.ts │
│ — Changed to use WHATWG Fetch `Request` instead of │
│ `http.IncomingMessage`. │
└─────────────────────────────────────────────────────────────────────────┘
*/
export function matchHas(
req: Pick<Request, 'headers' | 'url'>,
query: Params,
has: RouteHas[] = [],
missing: RouteHas[] = [],
): false | Params {
const params: Params = {}
const cookies = getCookies(req.headers)
const url = new URL(req.url)
const hasMatch = (hasItem: RouteHas) => {
let value: undefined | string | null
let key = hasItem.key
switch (hasItem.type) {
case 'header': {
key = hasItem.key.toLowerCase()
value = req.headers.get(key)
break
}
case 'cookie': {
value = cookies[hasItem.key]
break
}
case 'query': {
value = query[hasItem.key]
break
}
case 'host': {
value = url.hostname
break
}
default: {
break
}
}
if (!hasItem.value && value && key) {
params[getSafeParamName(key)] = value
return true
} else if (value) {
const matcher = new RegExp(`^${hasItem.value}$`)
const matches = Array.isArray(value)
? value.slice(-1)[0].match(matcher)
: value.match(matcher)
if (matches) {
if (Array.isArray(matches)) {
if (matches.groups) {
Object.keys(matches.groups).forEach((groupKey) => {
params[groupKey] = matches.groups![groupKey]
})
} else if (hasItem.type === 'host' && matches[0]) {
params.host = matches[0]
}
}
return true
}
}
return false
}
const allMatch = has.every((item) => hasMatch(item)) && !missing.some((item) => hasMatch(item))
if (allMatch) {
return params
}
return false
}
export function compileNonPath(value: string, params: Params): string {
if (!value.includes(':')) {
return value
}
for (const key of Object.keys(params)) {
if (value.includes(`:${key}`)) {
value = value
.replace(new RegExp(`:${key}\\*`, 'g'), `:${key}--ESCAPED_PARAM_ASTERISKS`)
.replace(new RegExp(`:${key}\\?`, 'g'), `:${key}--ESCAPED_PARAM_QUESTION`)
.replace(new RegExp(`:${key}\\+`, 'g'), `:${key}--ESCAPED_PARAM_PLUS`)
.replace(new RegExp(`:${key}(?!\\w)`, 'g'), `--ESCAPED_PARAM_COLON${key}`)
}
}
value = value
.replace(/(:|\*|\?|\+|\(|\)|\{|\})/g, '\\$1')
.replace(/--ESCAPED_PARAM_PLUS/g, '+')
.replace(/--ESCAPED_PARAM_COLON/g, ':')
.replace(/--ESCAPED_PARAM_QUESTION/g, '?')
.replace(/--ESCAPED_PARAM_ASTERISKS/g, '*')
// the value needs to start with a forward-slash to be compiled
// correctly
return compile(`/${value}`, { validate: false })(params).slice(1)
}
export function prepareDestination(args: {
appendParamsToQuery: boolean
destination: string
params: Params
query: ParsedUrlQuery
}) {
const query = Object.assign({}, args.query)
delete query.__nextLocale
delete query.__nextDefaultLocale
delete query.__nextDataReq
let escapedDestination = args.destination
for (const param of Object.keys({ ...args.params, ...query })) {
escapedDestination = escapeSegment(escapedDestination, param)
}
const parsedDestination: ParsedUrl = parseUrl(escapedDestination)
const destQuery = parsedDestination.query
const destPath = unescapeSegments(`${parsedDestination.pathname!}${parsedDestination.hash || ''}`)
const destHostname = unescapeSegments(parsedDestination.hostname || '')
const destPathParamKeys: Key[] = []
const destHostnameParamKeys: Key[] = []
pathToRegexp(destPath, destPathParamKeys)
pathToRegexp(destHostname, destHostnameParamKeys)
const destParams: (string | number)[] = []
destPathParamKeys.forEach((key) => destParams.push(key.name))
destHostnameParamKeys.forEach((key) => destParams.push(key.name))
const destPathCompiler = compile(
destPath,
// we don't validate while compiling the destination since we should
// have already validated before we got to this point and validating
// breaks compiling destinations with named pattern params from the source
// e.g. /something:hello(.*) -> /another/:hello is broken with validation
// since compile validation is meant for reversing and not for inserting
// params from a separate path-regex into another
{ validate: false },
)
const destHostnameCompiler = compile(destHostname, { validate: false })
// update any params in query values
for (const [key, strOrArray] of Object.entries(destQuery)) {
// the value needs to start with a forward-slash to be compiled
// correctly
if (Array.isArray(strOrArray)) {
destQuery[key] = strOrArray.map((value) =>
compileNonPath(unescapeSegments(value), args.params),
)
} else {
destQuery[key] = compileNonPath(unescapeSegments(strOrArray), args.params)
}
}
// add path params to query if it's not a redirect and not
// already defined in destination query or path
const paramKeys = Object.keys(args.params).filter((name) => name !== 'nextInternalLocale')
if (args.appendParamsToQuery && !paramKeys.some((key) => destParams.includes(key))) {
for (const key of paramKeys) {
if (!(key in destQuery)) {
destQuery[key] = args.params[key]
}
}
}
let newUrl
try {
newUrl = destPathCompiler(args.params)
const [pathname, hash] = newUrl.split('#')
parsedDestination.hostname = destHostnameCompiler(args.params)
parsedDestination.pathname = pathname
parsedDestination.hash = `${hash ? '#' : ''}${hash || ''}`
delete (parsedDestination as any).search
} catch (err: any) {
if (err.message.match(/Expected .*? to not repeat, but got an array/)) {
throw new Error(
`To use a multi-match in the destination you must add \`*\` at the end of the param name to signify it should repeat. https://nextjs.org/docs/messages/invalid-multi-match`,
)
}
throw err
}
// Query merge order lowest priority to highest
// 1. initial URL query values
// 2. path segment values
// 3. destination specified query values
parsedDestination.query = {
...query,
...parsedDestination.query,
}
return {
newUrl,
destQuery,
parsedDestination,
}
}
/**
* Ensure only a-zA-Z are used for param names for proper interpolating
* with path-to-regexp
*/
function getSafeParamName(paramName: string) {
let newParamName = ''
for (let i = 0; i < paramName.length; i++) {
const charCode = paramName.charCodeAt(i)
if (
(charCode > 64 && charCode < 91) || // A-Z
(charCode > 96 && charCode < 123) // a-z
) {
newParamName += paramName[i]
}
}
return newParamName
}
function escapeSegment(str: string, segmentName: string) {
return str.replace(
new RegExp(`:${escapeStringRegexp(segmentName)}`, 'g'),
`__ESC_COLON_${segmentName}`,
)
}
function unescapeSegments(str: string) {
return str.replace(/__ESC_COLON_/gi, ':')
}
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ packages/next/src/shared/lib/router/utils/is-dynamic.ts │
└─────────────────────────────────────────────────────────────────────────┘
*/
// Identify /[param]/ in route string
const TEST_ROUTE = /\/\[[^/]+?\](?=\/|$)/
export function isDynamicRoute(route: string): boolean {
return TEST_ROUTE.test(route)
}
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ packages/next/shared/lib/router/utils/middleware-route-matcher.ts │
└─────────────────────────────────────────────────────────────────────────┘
*/
export interface MiddlewareRouteMatch {
(
pathname: string | null | undefined,
request: Pick<Request, 'headers' | 'url'>,
query: Params,
): boolean
}
export interface MiddlewareMatcher {
regexp: string
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
}
export function getMiddlewareRouteMatcher(matchers: MiddlewareMatcher[]): MiddlewareRouteMatch {
return (
pathname: string | null | undefined,
req: Pick<Request, 'headers' | 'url'>,
query: Params,
) => {
for (const matcher of matchers) {
const routeMatch = new RegExp(matcher.regexp).exec(pathname!)
if (!routeMatch) {
continue
}
if (matcher.has || matcher.missing) {
const hasParams = matchHas(req, query, matcher.has, matcher.missing)
if (!hasParams) {
continue
}
}
return true
}
return false
}
}