-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathroute-handler.ts
272 lines (234 loc) · 7.97 KB
/
route-handler.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
import type { RoutesManifest, HeaderRoutes } from "gatsby"
import { tmpdir } from "os"
import { Transform } from "stream"
import { join, basename } from "path"
import fs from "fs-extra"
import { createStaticAssetsPathHandler } from "./pretty-urls"
const NETLIFY_REDIRECT_KEYWORDS_ALLOWLIST = new Set([
`query`,
`conditions`,
`headers`,
`signed`,
`edge_handler`,
])
const NETLIFY_CONDITIONS_ALLOWLIST = new Set([`language`, `country`])
const toNetlifyPath = (fromPath: string, toPath: string): Array<string> => {
// Modifies query parameter redirects, having no effect on other fromPath strings
const netlifyFromPath = fromPath.replace(/[&?]/, ` `)
// Modifies wildcard & splat redirects, having no effect on other toPath strings
const netlifyToPath = toPath.replace(/\*/, `:splat`)
return [netlifyFromPath, netlifyToPath]
}
export const ADAPTER_MARKER_START = `# gatsby-adapter-netlify start`
export const ADAPTER_MARKER_END = `# gatsby-adapter-netlify end`
export const NETLIFY_PLUGIN_MARKER_START = `# @netlify/plugin-gatsby redirects start`
export const NETLIFY_PLUGIN_MARKER_END = `# @netlify/plugin-gatsby redirects end`
export const GATSBY_PLUGIN_MARKER_START = `## Created with gatsby-plugin-netlify`
export async function injectEntries(
fileName: string,
content: string
): Promise<void> {
await fs.ensureFile(fileName)
const tmpFile = join(
await fs.mkdtemp(join(tmpdir(), basename(fileName))),
`out.txt`
)
let tail = ``
let insideNetlifyPluginGatsby = false
let insideGatsbyPluginNetlify = false
let insideGatsbyAdapterNetlify = false
let injectedEntries = false
const annotatedContent = `${ADAPTER_MARKER_START}\n${content}\n${ADAPTER_MARKER_END}\n`
function getContentToAdd(final: boolean): string {
const lines = tail.split(`\n`)
tail = ``
let contentToAdd = ``
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
if (!final && i === lines.length - 1) {
tail = line
break
}
let skipLine =
insideGatsbyAdapterNetlify ||
insideGatsbyPluginNetlify ||
insideNetlifyPluginGatsby
if (line.includes(ADAPTER_MARKER_START)) {
skipLine = true
insideGatsbyAdapterNetlify = true
} else if (line.includes(ADAPTER_MARKER_END)) {
insideGatsbyAdapterNetlify = false
contentToAdd += annotatedContent
injectedEntries = true
} else if (line.includes(NETLIFY_PLUGIN_MARKER_START)) {
insideNetlifyPluginGatsby = true
skipLine = true
} else if (line.includes(NETLIFY_PLUGIN_MARKER_END)) {
insideNetlifyPluginGatsby = false
} else if (line.includes(GATSBY_PLUGIN_MARKER_START)) {
insideGatsbyPluginNetlify = true
skipLine = true
}
if (!skipLine) {
contentToAdd += line + `\n`
}
}
return contentToAdd
}
const streamReplacer = new Transform({
transform(chunk, _encoding, callback): void {
tail = tail + chunk.toString()
try {
callback(null, getContentToAdd(false))
} catch (e) {
callback(e)
}
},
flush(callback): void {
try {
let contentToAdd = getContentToAdd(true)
if (!injectedEntries) {
contentToAdd += annotatedContent
}
callback(null, contentToAdd)
} catch (e) {
callback(e)
}
},
})
await new Promise<void>((resolve, reject) => {
const writeStream = fs.createWriteStream(tmpFile)
const pipeline = fs
.createReadStream(fileName)
.pipe(streamReplacer)
.pipe(writeStream)
pipeline.on(`finish`, resolve)
pipeline.on(`error`, reject)
streamReplacer.on(`error`, reject)
})
// remove previous file and move new file from tmp to final path
await fs.remove(fileName)
await fs.move(tmpFile, fileName)
}
function buildHeaderString(path, headers): string {
return `${encodeURI(path)}\n${headers.reduce((acc, curr) => {
acc += ` ${curr.key}: ${curr.value}\n`
return acc
}, ``)}`
}
export function processRoutesManifest(
routesManifest: RoutesManifest,
headerRoutes: HeaderRoutes
): {
redirects: string
headers: string
lambdasThatUseCaching: Map<string, string>
fileMovingPromise: Promise<void>
} {
const lambdasThatUseCaching = new Map<string, string>()
const { ensureStaticAssetPath, fileMovingDone } =
createStaticAssetsPathHandler()
let _redirects = ``
let _headers = ``
for (const route of routesManifest) {
const fromPath = route.path.replace(/\*.*/, `*`)
if (route.type === `function`) {
let functionName = route.functionId
if (route.cache) {
functionName = `${route.functionId}-odb`
if (!lambdasThatUseCaching.has(route.functionId)) {
lambdasThatUseCaching.set(route.functionId, functionName)
}
}
const invocationURL = `/.netlify/${
route.cache ? `builders` : `functions`
}/${functionName}`
_redirects += `${encodeURI(fromPath)} ${invocationURL} 200\n`
} else if (route.type === `redirect`) {
const {
status: routeStatus,
toPath,
// TODO: add headers handling
headers,
...rest
} = route
let status = String(routeStatus)
if (rest.force) {
status = `${status}!`
}
const [netlifyFromPath, netlifyToPath] = toNetlifyPath(fromPath, toPath)
// The order of the first 3 parameters is significant.
// The order for rest params (key-value pairs) is arbitrary.
const pieces = [netlifyFromPath, netlifyToPath, status]
for (const [key, value] of Object.entries(rest)) {
if (NETLIFY_REDIRECT_KEYWORDS_ALLOWLIST.has(key)) {
if (key === `conditions`) {
// "conditions" key from Gatsby contains only "language" and "country"
// which need special transformation to match Netlify _redirects
// https://www.gatsbyjs.com/docs/reference/config-files/actions/#createRedirect
if (value && typeof value === `object`) {
for (const [conditionKey, conditionValueRaw] of Object.entries(
value
)) {
if (NETLIFY_CONDITIONS_ALLOWLIST.has(conditionKey)) {
const conditionValue = Array.isArray(conditionValueRaw)
? conditionValueRaw.join(`,`)
: conditionValueRaw
// Gatsby gives us "country", we want "Country"
const conditionName =
conditionKey.charAt(0).toUpperCase() + conditionKey.slice(1)
pieces.push(`${conditionName}=${conditionValue}`)
}
}
}
} else {
pieces.push(`${key}=${value}`)
}
}
}
_redirects += pieces.join(` `) + `\n`
} else if (route.type === `static`) {
const { finalFilePath, isDynamic } = ensureStaticAssetPath(
route.filePath,
fromPath
)
if (isDynamic) {
_redirects += `${encodeURI(fromPath)} ${finalFilePath.replace(
/^public/,
``
)} 200\n`
}
if (!headerRoutes) {
// don't generate _headers from routesManifest if headerRoutes are provided
_headers += buildHeaderString(route.path, route.headers)
}
}
}
if (headerRoutes) {
_headers = headerRoutes.reduce((acc, curr) => {
acc += buildHeaderString(curr.path, curr.headers)
return acc
}, ``)
}
return {
redirects: _redirects,
headers: _headers,
lambdasThatUseCaching,
fileMovingPromise: fileMovingDone(),
}
}
export async function handleRoutesManifest(
routesManifest: RoutesManifest,
headerRoutes: HeaderRoutes
): Promise<{
lambdasThatUseCaching: Map<string, string>
}> {
const { redirects, headers, lambdasThatUseCaching, fileMovingPromise } =
processRoutesManifest(routesManifest, headerRoutes)
await injectEntries(`public/_redirects`, redirects)
await injectEntries(`public/_headers`, headers)
await fileMovingPromise
return {
lambdasThatUseCaching,
}
}