-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
normalize.js
277 lines (234 loc) · 7.25 KB
/
normalize.js
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
const { URL } = require(`url`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const path = require(`path`)
const probeImageSize = require(`probe-image-size`)
const { getOptions } = require(`./plugin-options`)
const getHref = link => {
if (typeof link === `object`) {
return link.href
}
return link
}
exports.getHref = getHref
const imageCDNState = {
foundPlaceholderStyle: false,
hasLoggedNoPlaceholderStyle: false,
}
exports.imageCDNState = imageCDNState
/**
* This FN takes in node data and returns Gatsby Image CDN fields that should be added to that node. If the input node isn't an image an empty object is returned.
*/
const getGatsbyImageCdnFields = async ({
node,
type,
pluginOptions,
fileNodesExtendedData,
reporter,
}) => {
if (!pluginOptions?.skipFileDownloads) {
return {}
}
const isFile = isFileNode({
internal: {
type,
},
})
if (!isFile) {
return {}
}
const mimeType = node.attributes.filemime
const { filename } = node.attributes
if (!mimeType || !filename) {
return {}
}
const url = getFileUrl(node.attributes, pluginOptions.baseUrl)?.href
if (!url) {
return {}
}
if (!mimeType.includes(`image/`)) {
return {
mimeType,
filename,
url,
}
}
const extraNodeData = fileNodesExtendedData?.get?.(node.id) || null
try {
const { placeholderStyleName } = getOptions()
const placeholderUrl =
extraNodeData?.imageDerivatives?.links?.[placeholderStyleName]?.href ||
extraNodeData?.imageDerivatives?.links?.placeholder?.href ||
url
if (placeholderUrl !== url) {
imageCDNState.foundPlaceholderStyle = true
}
const hasRequiredData = input => input && input.width && input.height
// extraNodeData comes from the fileNodesExtendedData Map which is built up in sourceNodes in gatsby-node. The data in this Map is found by looking at connections to file nodes from other node types. This is needed because Drupal's JSON API doesn't provide image widths/heights and placeholder urls for file nodes when querying directly for file nodes. This data can only be found on other nodes with relationships to file nodes. In the case that we don't have this data, we use probe-image-size to find the width/height of the image so that image CDN still works even if we don't have the data.
const imageSize = hasRequiredData(extraNodeData)
? extraNodeData
: await probeImageSize(url)
if (!hasRequiredData(imageSize) || !placeholderUrl) {
return {}
}
const gatsbyImageCdnFields = {
filename,
url,
placeholderUrl,
width: imageSize.width,
height: imageSize.height,
mimeType,
}
return gatsbyImageCdnFields
} catch (e) {
if (e.message.includes(`unrecognized file format`)) {
reporter.error(
`[gatsby-source-drupal] Encountered corrupt file while requesting image dimensions for ${url}`
)
return {}
}
reporter.error(e)
reporter.info(
JSON.stringify(
{
extraNodeData,
url,
attributes: node.attributes,
},
null,
2
)
)
reporter.panic(
`[gatsby-source-drupal] Encountered an unrecoverable error while generating Gatsby Image CDN fields. See above for additional information.`
)
}
return {}
}
const nodeFromData = async (
datum,
createNodeId,
entityReferenceRevisions = [],
pluginOptions,
fileNodesExtendedData,
reporter
) => {
const { attributes: { id: attributeId, ...attributes } = {} } = datum
const preservedId =
typeof attributeId !== `undefined` ? { _attributes_id: attributeId } : {}
const langcode = attributes.langcode || `und`
const type = datum.type.replace(/-|__|:|\.|\s/g, `_`)
const gatsbyImageCdnFields = await getGatsbyImageCdnFields({
node: datum,
type,
pluginOptions,
fileNodesExtendedData,
reporter,
})
return {
id: createNodeId(
createNodeIdWithVersion(
datum.id,
datum.type,
langcode,
attributes.drupal_internal__revision_id,
entityReferenceRevisions
)
),
drupal_id: datum.id,
parent: null,
drupal_parent_menu_item: attributes.parent,
children: [],
...attributes,
...preservedId,
...gatsbyImageCdnFields,
drupal_relationships: datum.relationships,
relationships: {},
internal: {
type,
},
}
}
exports.nodeFromData = nodeFromData
const isEntityReferenceRevision = (type, entityReferenceRevisions = []) =>
entityReferenceRevisions.findIndex(
revisionType => type.indexOf(revisionType) === 0
) !== -1
const createNodeIdWithVersion = (
id,
type,
langcode,
revisionId,
entityReferenceRevisions = []
) => {
// Fallback to default language for entities that don't translate.
if (getOptions()?.languageConfig?.nonTranslatableEntities.includes(type)) {
langcode = getOptions().languageConfig.defaultLanguage
}
// If the source plugin hasn't enabled `translation` then always just set langcode
// to "undefined".
let langcodeNormalized = getOptions().languageConfig ? langcode : `und`
if (
getOptions().languageConfig &&
!getOptions().languageConfig?.enabledLanguages.includes(langcodeNormalized)
) {
langcodeNormalized = getOptions().languageConfig.defaultLanguage
}
// The relationship between an entity and another entity also depends on the revision ID if the field is of type
// entity reference revision such as for paragraphs.
return isEntityReferenceRevision(type, entityReferenceRevisions)
? `${langcodeNormalized}.${id}.${revisionId || 0}`
: `${langcodeNormalized}.${id}`
}
exports.createNodeIdWithVersion = createNodeIdWithVersion
const isFileNode = node => {
const type = node?.internal?.type
return type === `files` || type === `file__file`
}
exports.isFileNode = isFileNode
const getFileUrl = (node, baseUrl) => {
let fileUrl = node.url
if (typeof node.uri === `object`) {
// Support JSON API 2.x file URI format https://www.drupal.org/node/2982209
fileUrl = node.uri.url
}
// Resolve w/ baseUrl if node.uri isn't absolute.
const url = new URL(fileUrl, baseUrl)
return url
}
exports.downloadFile = async (
{ node, store, cache, createNode, createNodeId, getCache, reporter },
{ basicAuth, baseUrl }
) => {
// handle file downloads
if (isFileNode(node)) {
let fileType
if (typeof node.uri === `object`) {
// get file type from uri prefix ("S3:", "public:", etc.)
const uriPrefix = node.uri.value.match(/^\w*:/)
fileType = uriPrefix ? uriPrefix[0] : null
}
const url = getFileUrl(node, baseUrl)
// If we have basicAuth credentials, add them to the request.
const basicAuthFileSystems = [`public:`, `private:`, `temporary:`]
const auth =
typeof basicAuth === `object` && basicAuthFileSystems.includes(fileType)
? {
htaccess_user: basicAuth.username,
htaccess_pass: basicAuth.password,
}
: {}
const fileNode = await createRemoteFileNode({
url: url.href,
name: path.parse(decodeURIComponent(url.pathname)).name,
cache,
createNode,
createNodeId,
getCache,
parentNodeId: node.id,
auth,
})
if (fileNode) {
node.localFile___NODE = fileNode.id
}
}
}