forked from netlify/gatsby-plugin-netlify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-headers-program.ts
241 lines (206 loc) · 6.91 KB
/
build-headers-program.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
/* eslint-disable max-lines */
import { writeFile, existsSync } from 'fs-extra'
import mergeWith from 'lodash.mergewith'
import {
HEADER_COMMENT,
IMMUTABLE_CACHING_HEADER,
SECURITY_HEADERS,
CACHING_HEADERS,
LINK_REGEX,
NETLIFY_HEADERS_FILENAME,
} from './constants'
import { isBoolean, flow } from './util'
const getHeaderName = (header: any) => {
const matches = header.match(/^([^:]+):/)
return matches && matches[1]
}
const validHeaders = (headers: any, reporter: any) => {
if (!headers || typeof headers !== 'object') {
return false
}
return Object.entries(headers).every(
([path, headersList]) =>
Array.isArray(headersList) &&
headersList.every((header) => {
if (typeof header === 'string' && !getHeaderName(header)) {
reporter.panic(
`[gatsby-plugin-netlify] ${path} contains an invalid header (${header}). Please check your plugin configuration`,
)
}
return true
}),
)
}
const headersPath = (pathPrefix: any, path: any) => `${pathPrefix}${path}`
const unionMerge = (objValue: any, srcValue: any) => {
if (Array.isArray(objValue)) {
return [...new Set([...objValue, ...srcValue])]
}
// opt into default merge behavior
}
const defaultMerge = (...headers: any[]) => mergeWith({}, ...headers, unionMerge)
const headersMerge = (userHeaders: any, defaultHeaders: any) => {
const merged = {}
Object.keys(defaultHeaders).forEach((path) => {
if (!userHeaders[path]) {
merged[path] = defaultHeaders[path]
return
}
const headersMap = {}
defaultHeaders[path].forEach((header: any) => {
headersMap[getHeaderName(header)] = header
})
userHeaders[path].forEach((header: any) => {
// override if exists
headersMap[getHeaderName(header)] = header
})
merged[path] = Object.values(headersMap)
})
Object.keys(userHeaders).forEach((path) => {
if (!merged[path]) {
merged[path] = userHeaders[path]
}
})
return merged
}
const transformLink = (manifest: any, publicFolder: any, pathPrefix: any) => (header: any) => header.replace(LINK_REGEX, (__: any, prefix: any, file: any, suffix: any) => {
const hashed = manifest[file]
if (hashed) {
return `${prefix}${pathPrefix}${hashed}${suffix}`
}
if (existsSync(publicFolder(file))) {
return `${prefix}${pathPrefix}${file}${suffix}`
}
throw new Error(
`Could not find the file specified in the Link header \`${header}\`.` +
`The gatsby-plugin-netlify is looking for a matching file (with or without a ` +
`webpack hash). Check the public folder and your gatsby-config.js to ensure you are ` +
`pointing to a public file.`,
)
})
// Writes out headers file format, with two spaces for indentation
// https://www.netlify.com/docs/headers-and-basic-auth/
const stringifyHeaders = (headers: any) => Object.entries(headers).reduce((text, [path, headerList]: [string, Array<string>]) => {
const headersString = headerList.reduce((accum, header) => `${accum} ${header}\n`, ``)
return `${text}${path}\n${headersString}`
}, ``)
// program methods
const validateUserOptions = (pluginOptions: any, reporter: any) => (headers: any) => {
if (!validHeaders(headers, reporter)) {
throw new Error(
`The "headers" option to gatsby-plugin-netlify is in the wrong shape. ` +
`You should pass in a object with string keys (representing the paths) and an array ` +
`of strings as the value (representing the headers). ` +
`Check your gatsby-config.js.`,
)
}
[`mergeSecurityHeaders`, `mergeCachingHeaders`].forEach((mergeOption) => {
if (!isBoolean(pluginOptions[mergeOption])) {
throw new TypeError(
`The "${mergeOption}" option to gatsby-plugin-netlify must be a boolean. Check your gatsby-config.js.`,
)
}
})
if (typeof pluginOptions.transformHeaders !== 'function') {
throw new TypeError(
`The "transformHeaders" option to gatsby-plugin-netlify must be a function ` +
`that returns an array of header strings. ` +
`Check your gatsby-config.js.`,
)
}
return headers
}
const mapUserLinkHeaders =
({
manifest,
pathPrefix,
publicFolder
}: any) =>
(headers: any) => Object.fromEntries(
Object.entries(headers).map(([path, headerList]: [string, Array<string>]) => [
path,
headerList.map(transformLink(manifest, publicFolder, pathPrefix)),
]),
)
const mapUserLinkAllPageHeaders =
(pluginData: any, {
allPageHeaders
}: any) =>
(headers: any) => {
if (!allPageHeaders) {
return headers
}
const { pages, manifest, publicFolder, pathPrefix } = pluginData
const headersList = allPageHeaders.map(transformLink(manifest, publicFolder, pathPrefix))
const duplicateHeadersByPage = {}
pages.forEach((page: any) => {
const pathKey = headersPath(pathPrefix, page.path)
duplicateHeadersByPage[pathKey] = headersList
})
return defaultMerge(headers, duplicateHeadersByPage)
}
const applySecurityHeaders =
({
mergeSecurityHeaders
}: any) =>
(headers: any) => {
if (!mergeSecurityHeaders) {
return headers
}
return headersMerge(headers, SECURITY_HEADERS)
}
const applyCachingHeaders =
(pluginData: any, {
mergeCachingHeaders
}: any) =>
(headers: any) => {
if (!mergeCachingHeaders) {
return headers
}
const files = new Set()
for (const fileNameOrArrayOfFileNames of Object.values(pluginData.manifest)) {
if (Array.isArray(fileNameOrArrayOfFileNames)) {
for (const filename of fileNameOrArrayOfFileNames) {
files.add(filename)
}
} else if (typeof fileNameOrArrayOfFileNames === `string`) {
files.add(fileNameOrArrayOfFileNames)
}
}
const cachingHeaders = {}
files.forEach((file) => {
if (typeof file === `string`) {
cachingHeaders[`/${file}`] = [IMMUTABLE_CACHING_HEADER]
}
})
return defaultMerge(headers, cachingHeaders, CACHING_HEADERS)
}
const applyTransformHeaders =
({
transformHeaders
}: any) =>
(headers: any) =>
Object.entries(headers).reduce((temp, [key, value]) => {
temp[key] = transformHeaders(value, key)
return temp
}, {})
const transformToString = (headers: any) => `${HEADER_COMMENT}\n\n${stringifyHeaders(headers)}`
const writeHeadersFile =
({
publicFolder
}: any) =>
(contents: any) => writeFile(publicFolder(NETLIFY_HEADERS_FILENAME), contents)
const buildHeadersProgram = (pluginData: any, pluginOptions: any, reporter: any) =>
flow(
[
validateUserOptions(pluginOptions, reporter),
mapUserLinkHeaders(pluginData),
applySecurityHeaders(pluginOptions),
applyCachingHeaders(pluginData, pluginOptions),
mapUserLinkAllPageHeaders(pluginData, pluginOptions),
applyTransformHeaders(pluginOptions),
transformToString,
writeHeadersFile(pluginData),
])(pluginOptions.headers)
export default buildHeadersProgram
/* eslint-enable max-lines */