-
-
Notifications
You must be signed in to change notification settings - Fork 10k
/
get.js
368 lines (311 loc) · 10.9 KB
/
get.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
// # Get Helper
// Usage: `{{#get "posts" limit="5"}}`, `{{#get "tags" limit="all"}}`
// Fetches data from the API
const {config, api, prepareContextResource} = require('../services/proxy');
const {hbs, SafeString} = require('../services/handlebars');
const logging = require('@tryghost/logging');
const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const Sentry = require('@sentry/node');
const _ = require('lodash');
const jsonpath = require('jsonpath');
const nqlLang = require('@tryghost/nql-lang');
const messages = {
mustBeCalledAsBlock: 'The {\\{{helperName}}} helper must be called as a block. E.g. {{#{helperName}}}...{{/{helperName}}}',
invalidResource: 'Invalid "{resource}" resource given to get helper'
};
const createFrame = hbs.handlebars.createFrame;
const RESOURCES = {
posts: {
alias: 'postsPublic'
},
tags: {
alias: 'tagsPublic'
},
pages: {
alias: 'pagesPublic'
},
authors: {
alias: 'authorsPublic'
},
tiers: {
alias: 'tiersPublic'
},
newsletters: {
alias: 'newslettersPublic'
}
};
// Short forms of paths which we should understand
const pathAliases = {
'post.tags': 'post.tags[*].slug',
'post.author': 'post.author.slug'
};
/**
* ## Is Browse
* Is this a Browse request or a Read request?
* @param {Object} options
* @returns {boolean}
*/
function isBrowse(options) {
let browse = true;
if (options.id || options.slug) {
browse = false;
}
return browse;
}
/**
* ## Resolve Paths
* Find and resolve path strings
*
* @param {Object} data
* @param {String} value
* @returns {String}
*/
function resolvePaths(globals, data, value) {
const regex = /\{\{(.*?)\}\}/g;
value = value.replace(regex, function (match, path) {
let result;
// Handle aliases
path = pathAliases[path] ? pathAliases[path] : path;
// Handle Handlebars .[] style arrays
path = path.replace(/\.\[/g, '[');
if (path.charAt(0) === '@') {
result = jsonpath.query(globals, path.slice(1));
} else {
// Do the query, which always returns an array of matches
result = jsonpath.query(data, path);
}
// Handle the case where the single data property we return is a Date
// Data.toString() is not DB compatible, so use `toISOString()` instead
if (_.isDate(result[0])) {
result[0] = result[0].toISOString();
}
// Concatenate the results with a comma, handles common case of multiple tag slugs
return result.join(',');
});
return value;
}
/**
* ## Parse Options
* Ensure options passed in make sense
*
* @param {Object} data
* @param {Object} options
* @returns {*}
*/
function parseOptions(globals, data, options) {
if (_.isString(options.filter)) {
options.filter = resolvePaths(globals, data, options.filter);
}
if (options.limit === 'all' && config.get('getHelperLimitAllMax')) {
options.limit = config.get('getHelperLimitAllMax');
}
return options;
}
function optimizeFilterCacheablity(resource, options) {
const noOptimisation = {
options,
parseResult(result) {
return result;
}
};
if (resource !== 'posts') {
return noOptimisation;
}
if (!options.filter) {
return noOptimisation;
}
try {
if (options.filter.split('id:-').length !== 2) {
return noOptimisation;
}
const parsedFilter = nqlLang.parse(options.filter);
// Support either `id:blah` or `id:blah+other:stuff`
if (!parsedFilter.$and && !parsedFilter.id) {
return noOptimisation;
}
const queries = parsedFilter.$and || [parsedFilter];
const query = queries.find((q) => {
return q?.id?.$ne;
});
if (!query) {
return noOptimisation;
}
const idToFilter = query.id.$ne;
let limit = options.limit;
if (options.limit !== 'all') {
limit = options.limit ? 1 + parseInt(options.limit, 10) : 16;
}
// We replace with id:-null so we don't have to deal with leading/trailing AND operators
const filter = options.filter.replace(/id:-[a-f0-9A-F]{24}/, 'id:-null');
const parseResult = function parseResult(result) {
const filteredPosts = result?.posts?.filter((post) => {
return post.id !== idToFilter;
}) || [];
const modifiedResult = {
...result,
posts: limit === 'all' ? filteredPosts : filteredPosts.slice(0, limit - 1)
};
modifiedResult.meta = modifiedResult.meta || {};
modifiedResult.meta.cacheablityOptimisation = true;
if (typeof modifiedResult?.meta?.pagination?.limit === 'number') {
modifiedResult.meta.pagination.limit = modifiedResult.meta.pagination.limit - 1;
}
return modifiedResult;
};
return {
options: {
...options,
limit,
filter
},
parseResult
};
} catch (err) {
logging.warn(err);
return noOptimisation;
}
}
/**
*
* @param {String} resource
* @param {String} controllerName
* @param {String} action
* @param {Object} apiOptions
* @returns {Promise<Object>}
*/
async function makeAPICall(resource, controllerName, action, apiOptions) {
const controller = api[controllerName];
let makeRequest = options => controller[action](options);
const {
options,
parseResult
} = optimizeFilterCacheablity(resource, apiOptions);
let timer;
try {
let response;
if (config.get('optimization:getHelper:timeout:threshold')) {
const logLevel = config.get('optimization:getHelper:timeout:level') || 'error';
const threshold = config.get('optimization:getHelper:timeout:threshold');
const apiResponse = makeRequest(options).then(parseResult);
const timeout = new Promise((resolve) => {
timer = setTimeout(() => {
logging[logLevel](new errors.HelperWarning({
message: `{{#get}} took longer than ${threshold}ms and was aborted`,
code: 'ABORTED_GET_HELPER',
errorDetails: {
api: `${controllerName}.${action}`,
apiOptions
}
}));
resolve({[resource]: [], '@@ABORTED_GET_HELPER@@': true});
}, threshold);
});
response = await Promise.race([apiResponse, timeout]);
clearTimeout(timer);
} else {
response = await makeRequest(options).then(parseResult);
}
return response;
} catch (err) {
clearTimeout(timer);
throw err;
}
}
/**
* ## Get
* @param {String} resource
* @param {Object} options
* @returns {Promise<any>}
*/
module.exports = async function get(resource, options) {
options = options || {};
options.hash = options.hash || {};
options.data = options.data || {};
const self = this;
const start = Date.now();
const data = createFrame(options.data);
const ghostGlobals = _.omit(data, ['_parent', 'root']);
let apiOptions = options.hash;
let returnedRowsCount;
if (!options.fn) {
data.error = tpl(messages.mustBeCalledAsBlock, {helperName: 'get'});
logging.warn(data.error);
return;
}
if (!RESOURCES[resource]) {
data.error = tpl(messages.invalidResource, {resource});
logging.warn(data.error);
return options.inverse(self, {data: data});
}
const controllerName = RESOURCES[resource].alias;
const action = isBrowse(apiOptions) ? 'browse' : 'read';
// Parse the options we're going to pass to the API
apiOptions = parseOptions(ghostGlobals, this, apiOptions);
let apiOptionsString = Object.entries(apiOptions)
.map(([key, value]) => ` ${key}="${value}"`)
.join('');
apiOptions.context = {member: data.member};
try {
const spanName = `{{#get "${resource}"${apiOptionsString}}} ${data.member ? 'member' : 'public'}`;
const result = await Sentry.startSpan({
op: 'frontend.helpers.get',
name: spanName,
tags: {
resource,
...apiOptions,
context: data.member ? 'member' : 'public'
}
}, async (span) => {
const response = await makeAPICall(resource, controllerName, action, apiOptions);
// prepare data properties for use with handlebars
if (response[resource] && response[resource].length) {
response[resource].forEach(prepareContextResource);
}
// used for logging details of slow requests
returnedRowsCount = response[resource] && response[resource].length;
span?.setTag('returnedRows', returnedRowsCount);
// block params allows the theme developer to name the data using something like
// `{{#get "posts" as |result pageInfo|}}`
const blockParams = [response[resource]];
if (response.meta && response.meta.pagination) {
response.pagination = response.meta.pagination;
blockParams.push(response.meta.pagination);
}
// Call the main template function
const rendered = options.fn(response, {
data: data,
blockParams: blockParams
});
if (response['@@ABORTED_GET_HELPER@@']) {
return new SafeString(`<span data-aborted-get-helper>Could not load content</span>` + rendered);
} else {
return rendered;
}
});
return result;
} catch (error) {
logging.error(error);
data.error = error.message;
return options.inverse(self, {data: data});
} finally {
if (config.get('optimization:getHelper:notify:threshold')) {
const totalMs = Date.now() - start;
const logLevel = config.get('optimization:getHelper:notify:level') || 'warn';
const threshold = config.get('optimization:getHelper:notify:threshold');
if (totalMs > threshold) {
logging[logLevel](new errors.HelperWarning({
message: `{{#get}} helper took ${totalMs}ms to complete`,
code: 'SLOW_GET_HELPER',
errorDetails: {
api: `${controllerName}.${action}`,
apiOptions,
returnedRows: returnedRowsCount
}
}));
}
}
}
};
module.exports.async = true;
module.exports.optimizeFilterCacheablity = optimizeFilterCacheablity;