forked from vercel/next.js
/
next-url.ts
344 lines (284 loc) · 8.42 KB
/
next-url.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
import type { DomainLocale, I18NConfig } from '../config-shared'
import { detectDomainLocale } from '../../shared/lib/i18n/detect-domain-locale'
import { formatNextPathnameInfo } from '../../shared/lib/router/utils/format-next-pathname-info'
import { getHostname } from '../../shared/lib/get-hostname'
import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-pathname-info'
interface Options {
base?: string | URL
headers?: { [key: string]: string | string[] | undefined }
forceLocale?: boolean
nextConfig?: {
basePath?: string
i18n?: I18NConfig | null
trailingSlash?: boolean
}
}
const FLIGHT_PARAMETERS = [
'__flight__',
'__flight_router_state_tree__',
'__flight_prefetch__',
] as const
const REGEX_LOCALHOST_HOSTNAME =
/(?!^https?:\/\/)(127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|::1|localhost)/
function parseURL(url: string | URL, base?: string | URL) {
return new URL(
String(url).replace(REGEX_LOCALHOST_HOSTNAME, 'localhost'),
base && String(base).replace(REGEX_LOCALHOST_HOSTNAME, 'localhost')
)
}
function parseFlightParameters(
searchParams: URLSearchParams
): Record<string, string> | undefined {
let flightSearchParameters: Record<string, string> = {}
let flightSearchParametersUpdated = false
for (const name of FLIGHT_PARAMETERS) {
const value = searchParams.get(name)
if (value === null) {
continue
}
flightSearchParameters[name] = value
flightSearchParametersUpdated = true
}
if (!flightSearchParametersUpdated) {
return undefined
}
return flightSearchParameters
}
const Internal = Symbol('NextURLInternal')
export class NextURL {
[Internal]: {
basePath: string
buildId?: string
flightSearchParameters?: Record<string, string>
defaultLocale?: string
domainLocale?: DomainLocale
locale?: string
options: Options
trailingSlash?: boolean
url: URL
}
constructor(input: string | URL, base?: string | URL, opts?: Options)
constructor(input: string | URL, opts?: Options)
constructor(
input: string | URL,
baseOrOpts?: string | URL | Options,
opts?: Options
) {
let base: undefined | string | URL
let options: Options
if (
(typeof baseOrOpts === 'object' && 'pathname' in baseOrOpts) ||
typeof baseOrOpts === 'string'
) {
base = baseOrOpts
options = opts || {}
} else {
options = opts || baseOrOpts || {}
}
this[Internal] = {
url: parseURL(input, base ?? options.base),
options: options,
basePath: '',
}
this.analyzeUrl()
}
private analyzeUrl() {
const pathnameInfo = getNextPathnameInfo(this[Internal].url.pathname, {
nextConfig: this[Internal].options.nextConfig,
parseData: true,
})
this[Internal].domainLocale = detectDomainLocale(
this[Internal].options.nextConfig?.i18n?.domains,
getHostname(this[Internal].url, this[Internal].options.headers)
)
const defaultLocale =
this[Internal].domainLocale?.defaultLocale ||
this[Internal].options.nextConfig?.i18n?.defaultLocale
this[Internal].url.pathname = pathnameInfo.pathname
this[Internal].defaultLocale = defaultLocale
this[Internal].basePath = pathnameInfo.basePath ?? ''
this[Internal].buildId = pathnameInfo.buildId
this[Internal].locale = pathnameInfo.locale ?? defaultLocale
this[Internal].trailingSlash = pathnameInfo.trailingSlash
this[Internal].flightSearchParameters = parseFlightParameters(
this[Internal].url.searchParams
)
}
private formatPathname() {
return formatNextPathnameInfo({
basePath: this[Internal].basePath,
buildId: this[Internal].buildId,
defaultLocale: !this[Internal].options.forceLocale
? this[Internal].defaultLocale
: undefined,
locale: this[Internal].locale,
pathname: this[Internal].url.pathname,
trailingSlash: this[Internal].trailingSlash,
})
}
private formatSearch() {
const flightSearchParameters = this[Internal].flightSearchParameters
// If no flight parameters are set, return the search string as is.
// This is a fast path to ensure URLSearchParams only has to be recreated on Flight requests.
if (!flightSearchParameters) {
return this[Internal].url.search
}
// Create separate URLSearchParams to ensure the original search string is not modified.
const searchParams = new URLSearchParams(this[Internal].url.searchParams)
// If any exist this loop is always limited to the amount of FLIGHT_PARAMETERS.
for (const name in flightSearchParameters) {
searchParams.set(name, flightSearchParameters[name])
}
const params = searchParams.toString()
return params === '' ? '' : `?${params}`
}
public get buildId() {
return this[Internal].buildId
}
public set buildId(buildId: string | undefined) {
this[Internal].buildId = buildId
}
public get flightSearchParameters() {
return this[Internal].flightSearchParameters
}
public set flightSearchParameters(
flightSearchParams: Record<string, string> | undefined
) {
if (flightSearchParams) {
for (const name of FLIGHT_PARAMETERS) {
// Ensure only the provided values are set
if (flightSearchParams[name]) {
this[Internal].url.searchParams.set(name, flightSearchParams[name])
} else {
// Delete the ones that are not provided as flightData should be overridden.
this[Internal].url.searchParams.delete(name)
}
}
} else {
for (const name of FLIGHT_PARAMETERS) {
this[Internal].url.searchParams.delete(name)
}
}
this[Internal].flightSearchParameters = flightSearchParams
}
public get locale() {
return this[Internal].locale ?? ''
}
public set locale(locale: string) {
if (
!this[Internal].locale ||
!this[Internal].options.nextConfig?.i18n?.locales.includes(locale)
) {
throw new TypeError(
`The NextURL configuration includes no locale "${locale}"`
)
}
this[Internal].locale = locale
}
get defaultLocale() {
return this[Internal].defaultLocale
}
get domainLocale() {
return this[Internal].domainLocale
}
get searchParams() {
return this[Internal].url.searchParams
}
get host() {
return this[Internal].url.host
}
set host(value: string) {
this[Internal].url.host = value
}
get hostname() {
return this[Internal].url.hostname
}
set hostname(value: string) {
this[Internal].url.hostname = value
}
get port() {
return this[Internal].url.port
}
set port(value: string) {
this[Internal].url.port = value
}
get protocol() {
return this[Internal].url.protocol
}
set protocol(value: string) {
this[Internal].url.protocol = value
}
get href() {
const pathname = this.formatPathname()
const search = this.formatSearch()
return `${this.protocol}//${this.host}${pathname}${search}`
}
set href(url: string) {
this[Internal].url = parseURL(url)
this.analyzeUrl()
}
get origin() {
return this[Internal].url.origin
}
get pathname() {
return this[Internal].url.pathname
}
set pathname(value: string) {
this[Internal].url.pathname = value
}
get hash() {
return this[Internal].url.hash
}
set hash(value: string) {
this[Internal].url.hash = value
}
get search() {
return this[Internal].url.search
}
set search(value: string) {
this[Internal].url.search = value
}
get password() {
return this[Internal].url.password
}
set password(value: string) {
this[Internal].url.password = value
}
get username() {
return this[Internal].url.username
}
set username(value: string) {
this[Internal].url.username = value
}
get basePath() {
return this[Internal].basePath
}
set basePath(value: string) {
this[Internal].basePath = value.startsWith('/') ? value : `/${value}`
}
toString() {
return this.href
}
toJSON() {
return this.href
}
[Symbol.for('edge-runtime.inspect.custom')]() {
return {
href: this.href,
origin: this.origin,
protocol: this.protocol,
username: this.username,
password: this.password,
host: this.host,
hostname: this.hostname,
port: this.port,
pathname: this.pathname,
search: this.search,
searchParams: this.searchParams,
hash: this.hash,
}
}
clone() {
return new NextURL(String(this), this[Internal].options)
}
}