This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
520 lines (439 loc) · 16 KB
/
gatsby-node.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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _agentkeepalive = _interopRequireDefault(require("agentkeepalive"));
const got = require(`got`);
const _ = require(`lodash`);
const urlJoin = require(`url-join`);
// const http2wrapper = require(`http2-wrapper`)
const {
HttpsAgent
} = _agentkeepalive.default;
const {
setOptions,
getOptions
} = require(`./plugin-options`);
const {
nodeFromData,
downloadFile,
isFileNode,
createNodeIdWithVersion
} = require(`./normalize`);
const {
handleReferences,
handleWebhookUpdate
} = require(`./utils`);
const agent = {
http: new _agentkeepalive.default(),
https: new HttpsAgent() // http2: new http2wrapper.Agent(),
};
async function worker([url, options]) {
return got(url, {
agent,
cache: false,
// request: http2wrapper.auto,
// http2: true,
...options
});
}
const requestQueue = require(`fastq`).promise(worker, 20);
const asyncPool = require(`tiny-async-pool`);
const bodyParser = require(`body-parser`);
function gracefullyRethrow(activity, error) {
// activity.panicOnBuild was implemented at some point in gatsby@2
// but plugin can still be used with older version of gatsby core
// so need to do some checking here
if (activity.panicOnBuild) {
activity.panicOnBuild(error);
}
activity.end();
if (!activity.panicOnBuild) {
throw error;
}
}
exports.onPreBootstrap = (_, pluginOptions) => {
setOptions(pluginOptions);
};
exports.sourceNodes = async ({
actions,
store,
cache,
createNodeId,
createContentDigest,
getCache,
getNode,
getNodes,
parentSpan,
reporter,
webhookBody
}, pluginOptions) => {
const {
baseUrl,
apiBase = `jsonapi`,
basicAuth = {},
filters,
headers,
params = {},
concurrentFileRequests = 20,
concurrentAPIRequests = 20,
disallowedLinkTypes = [`self`, `describedby`, `contact_message--feedback`, `contact_message--personal`],
skipFileDownloads = false,
fastBuilds = false,
entityReferenceRevisions = [],
languageConfig = {
defaultLanguage: `und`,
enabledLanguages: [`und`],
translatableEntities: [],
nonTranslatableEntities: []
}
} = pluginOptions;
const {
createNode,
setPluginStatus,
touchNode
} = actions; // Update the concurrency limit from the plugin options
requestQueue.concurrency = concurrentAPIRequests;
if (webhookBody && Object.keys(webhookBody).length) {
const changesActivity = reporter.activityTimer(`loading Drupal content changes`, {
parentSpan
});
changesActivity.start();
try {
const {
secret,
action,
data
} = webhookBody;
if (pluginOptions.secret && pluginOptions.secret !== secret) {
reporter.warn(`The secret in this request did not match your plugin options secret.`);
changesActivity.end();
return;
}
if (action === `delete`) {
let nodesToDelete = data;
if (!Array.isArray(data)) {
nodesToDelete = [data];
}
for (const nodeToDelete of nodesToDelete) {
var _nodeToDelete$attribu, _nodeToDelete$attribu2;
const nodeIdToDelete = createNodeId(createNodeIdWithVersion(nodeToDelete.id, nodeToDelete.type, getOptions().languageConfig ? (_nodeToDelete$attribu = nodeToDelete.attributes) === null || _nodeToDelete$attribu === void 0 ? void 0 : _nodeToDelete$attribu.langcode : `und`, (_nodeToDelete$attribu2 = nodeToDelete.attributes) === null || _nodeToDelete$attribu2 === void 0 ? void 0 : _nodeToDelete$attribu2.drupal_internal__revision_id, entityReferenceRevisions));
actions.deleteNode(getNode(nodeIdToDelete));
reporter.log(`Deleted node: ${nodeIdToDelete}`);
}
changesActivity.end();
return;
}
let nodesToUpdate = data;
if (!Array.isArray(data)) {
nodesToUpdate = [data];
}
for (const nodeToUpdate of nodesToUpdate) {
await handleWebhookUpdate({
nodeToUpdate,
actions,
cache,
createNodeId,
createContentDigest,
getCache,
getNode,
reporter,
store,
languageConfig
}, pluginOptions);
}
} catch (e) {
gracefullyRethrow(changesActivity, e);
return;
}
changesActivity.end();
return;
}
if (fastBuilds) {
var _store$getState$statu, _store$getState$statu2, _store$getState$statu3;
const lastFetched = (_store$getState$statu = (_store$getState$statu2 = store.getState().status.plugins) === null || _store$getState$statu2 === void 0 ? void 0 : (_store$getState$statu3 = _store$getState$statu2[`gatsby-source-drupal`]) === null || _store$getState$statu3 === void 0 ? void 0 : _store$getState$statu3.lastFetched) !== null && _store$getState$statu !== void 0 ? _store$getState$statu : 0;
reporter.verbose(`[gatsby-source-drupal]: value of lastFetched for fastbuilds "${lastFetched}"`);
const drupalFetchIncrementalActivity = reporter.activityTimer(`Fetch incremental changes from Drupal`);
let requireFullRebuild = false;
drupalFetchIncrementalActivity.start();
try {
// Hit fastbuilds endpoint with the lastFetched date.
const res = await requestQueue.push([urlJoin(baseUrl, `gatsby-fastbuilds/sync/`, lastFetched.toString()), {
username: basicAuth.username,
password: basicAuth.password,
headers,
searchParams: params,
responseType: `json`
}]); // Fastbuilds returns a -1 if:
// - the timestamp has expired
// - if old fastbuild logs were purged
// - it's been a really long time since you synced so you just do a full fetch.
if (res.body.status === -1) {
// The incremental data is expired or this is the first fetch.
reporter.info(`Unable to pull incremental data changes from Drupal`);
setPluginStatus({
lastFetched: res.body.timestamp
});
requireFullRebuild = true;
} else {
// Touch nodes so they are not garbage collected by Gatsby.
getNodes().forEach(node => {
if (node.internal.owner === `gatsby-source-drupal`) {
touchNode(node);
}
}); // Process sync data from Drupal.
const nodesToSync = res.body.entities;
for (const nodeSyncData of nodesToSync) {
if (nodeSyncData.action === `delete`) {
var _nodeSyncData$attribu;
actions.deleteNode(getNode(createNodeId(createNodeIdWithVersion(nodeSyncData.id, nodeSyncData.type, getOptions().languageConfig ? nodeSyncData.attributes.langcode : `und`, (_nodeSyncData$attribu = nodeSyncData.attributes) === null || _nodeSyncData$attribu === void 0 ? void 0 : _nodeSyncData$attribu.drupal_internal__revision_id, entityReferenceRevisions))));
} else {
// The data could be a single Drupal entity or an array of Drupal
// entities to update.
let nodesToUpdate = nodeSyncData.data;
if (!Array.isArray(nodeSyncData.data)) {
nodesToUpdate = [nodeSyncData.data];
}
for (const nodeToUpdate of nodesToUpdate) {
await handleWebhookUpdate({
nodeToUpdate,
actions,
cache,
createNodeId,
createContentDigest,
getCache,
getNode,
reporter,
store,
languageConfig
}, pluginOptions);
}
}
}
setPluginStatus({
lastFetched: res.body.timestamp
});
}
} catch (e) {
gracefullyRethrow(drupalFetchIncrementalActivity, e);
return;
}
drupalFetchIncrementalActivity.end();
if (!requireFullRebuild) {
return;
}
}
const drupalFetchActivity = reporter.activityTimer(`Fetch all data from Drupal`); // Fetch articles.
reporter.info(`Starting to fetch all data from Drupal`);
drupalFetchActivity.start();
let allData;
try {
const res = await requestQueue.push([urlJoin(baseUrl, apiBase), {
username: basicAuth.username,
password: basicAuth.password,
headers,
searchParams: params,
responseType: `json`
}]);
allData = await Promise.all(_.map(res.body.links, async (url, type) => {
const dataArray = [];
if (disallowedLinkTypes.includes(type)) return;
if (!url) return;
if (!type) return; // Lookup this type in our list of language alterable entities.
const isTranslatable = languageConfig.translatableEntities.some(entityType => entityType === type);
const getNext = async url => {
if (typeof url === `object`) {
// url can be string or object containing href field
url = url.href; // Apply any filters configured in gatsby-config.js. Filters
// can be any valid JSON API filter query string.
// See https://www.drupal.org/docs/8/modules/jsonapi/filtering
if (typeof filters === `object`) {
if (filters.hasOwnProperty(type)) {
url = new URL(url);
const filterParams = new URLSearchParams(filters[type]);
const filterKeys = Array.from(filterParams.keys());
filterKeys.forEach(filterKey => {
// Only add filter params to url if it has not already been
// added.
if (!url.searchParams.has(filterKey)) {
url.searchParams.set(filterKey, filterParams.get(filterKey));
}
});
url = url.toString();
}
}
}
let d;
try {
d = await requestQueue.push([url, {
username: basicAuth.username,
password: basicAuth.password,
headers,
responseType: `json`
}]);
} catch (error) {
if (error.response && error.response.statusCode == 405) {
// The endpoint doesn't support the GET method, so just skip it.
return;
} else {
console.error(`Failed to fetch ${url}`, error.message);
console.log(error);
throw error;
}
}
dataArray.push(...d.body.data); // Add support for includes. Includes allow entity data to be expanded
// based on relationships. The expanded data is exposed as `included`
// in the JSON API response.
// See https://www.drupal.org/docs/8/modules/jsonapi/includes
if (d.body.included) {
dataArray.push(...d.body.included);
}
if (d.body.links && d.body.links.next) {
await getNext(d.body.links.next);
}
};
if (isTranslatable === false) {
await getNext(url);
} else {
for (let i = 0; i < languageConfig.enabledLanguages.length; i++) {
let currentLanguage = languageConfig.enabledLanguages[i];
const urlPath = url.href.split(`${apiBase}/`).pop();
const baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/, ``); // The default language's JSON API is at the root.
if (currentLanguage === getOptions().languageConfig.defaultLanguage || baseUrlWithoutTrailingSlash.slice(-currentLanguage.length) == currentLanguage) {
currentLanguage = ``;
}
const joinedUrl = urlJoin(baseUrlWithoutTrailingSlash, currentLanguage, apiBase, urlPath);
await getNext(joinedUrl);
}
}
const result = {
type,
data: dataArray
}; // eslint-disable-next-line consistent-return
return result;
}));
} catch (e) {
gracefullyRethrow(drupalFetchActivity, e);
return;
}
drupalFetchActivity.end();
const nodes = new Map(); // first pass - create basic nodes
_.each(allData, contentType => {
if (!contentType) return;
_.each(contentType.data, datum => {
if (!datum) return;
const node = nodeFromData(datum, createNodeId, entityReferenceRevisions);
nodes.set(node.id, node);
});
}); // second pass - handle relationships and back references
nodes.forEach(node => {
handleReferences(node, {
getNode: nodes.get.bind(nodes),
createNodeId,
entityReferenceRevisions
});
});
if (skipFileDownloads) {
reporter.info(`Skipping remote file download from Drupal`);
} else {
reporter.info(`Downloading remote files from Drupal`); // Download all files (await for each pool to complete to fix concurrency issues)
const fileNodes = [...nodes.values()].filter(isFileNode);
if (fileNodes.length) {
const downloadingFilesActivity = reporter.activityTimer(`Remote file download`);
downloadingFilesActivity.start();
try {
await asyncPool(concurrentFileRequests, fileNodes, async node => {
await downloadFile({
node,
store,
cache,
createNode,
createNodeId,
getCache,
reporter
}, pluginOptions);
});
} catch (e) {
gracefullyRethrow(downloadingFilesActivity, e);
return;
}
downloadingFilesActivity.end();
}
} // Create each node
for (const node of nodes.values()) {
node.internal.contentDigest = createContentDigest(node);
createNode(node);
}
return;
}; // This is maintained for legacy reasons and will eventually be removed.
exports.onCreateDevServer = ({
app,
createNodeId,
getNode,
actions,
store,
cache,
createContentDigest,
getCache,
reporter
}, pluginOptions) => {
app.use(`/___updatePreview/`, bodyParser.text({
type: `application/json`
}), async (req, res) => {
console.warn(`The ___updatePreview callback is now deprecated and will be removed in the future. Please use the __refresh callback instead.`);
if (!_.isEmpty(req.body)) {
const requestBody = JSON.parse(JSON.parse(req.body));
const {
secret,
action,
id
} = requestBody;
if (pluginOptions.secret && pluginOptions.secret !== secret) {
return reporter.warn(`The secret in this request did not match your plugin options secret.`);
}
if (action === `delete`) {
actions.deleteNode(getNode(createNodeId(id)));
return reporter.log(`Deleted node: ${id}`);
}
const nodeToUpdate = JSON.parse(JSON.parse(req.body)).data;
return await handleWebhookUpdate({
nodeToUpdate,
actions,
cache,
createNodeId,
createContentDigest,
getCache,
getNode,
reporter,
store
}, pluginOptions);
} else {
res.status(400).send(`Received body was empty!`);
return reporter.log(`Received body was empty!`);
}
});
};
exports.pluginOptionsSchema = ({
Joi
}) => Joi.object({
baseUrl: Joi.string().required().description(`The URL to root of your Drupal instance`),
apiBase: Joi.string().description(`The path to the root of the JSONAPI — defaults to "jsonapi"`),
basicAuth: Joi.object({
username: Joi.string().required(),
password: Joi.string().required()
}).description(`Enables basicAuth`),
filters: Joi.object().description(`Pass filters to the JSON API for specific collections`),
headers: Joi.object().description(`Set request headers for requests to the JSON API`),
params: Joi.object().description(`Append optional GET params to requests`),
concurrentFileRequests: Joi.number().integer().default(20).min(1),
concurrentAPIRequests: Joi.number().integer().default(20).min(1),
disallowedLinkTypes: Joi.array().items(Joi.string()),
skipFileDownloads: Joi.boolean(),
fastBuilds: Joi.boolean(),
entityReferenceRevisions: Joi.array().items(Joi.string()),
secret: Joi.string().description(`an optional secret token for added security shared between your Drupal instance and Gatsby preview`),
languageConfig: Joi.object({
defaultLanguage: Joi.string().required(),
enabledLanguages: Joi.array().items(Joi.string()).required(),
translatableEntities: Joi.array().items(Joi.string()).required(),
nonTranslatableEntities: Joi.array().items(Joi.string()).required()
})
});