-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
index.js
682 lines (578 loc) · 19.8 KB
/
index.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
'use strict'
const { readFile } = require('node:fs/promises')
const fp = require('fastify-plugin')
const { accessSync, existsSync, mkdirSync, readdirSync } = require('node:fs')
const { basename, dirname, extname, join, resolve } = require('node:path')
const { LruMap } = require('toad-cache')
const supportedEngines = ['ejs', 'nunjucks', 'pug', 'handlebars', 'mustache', 'art-template', 'twig', 'liquid', 'dot', 'eta']
const viewCache = Symbol('@fastify/view/cache')
const fastifyViewCache = fp(
async function cachePlugin (fastify, opts) {
const lru = new LruMap(opts.maxCache || 100)
fastify.decorate(viewCache, lru)
},
{
fastify: '5.x',
name: '@fastify/view/cache'
}
)
async function fastifyView (fastify, opts) {
if (fastify[viewCache] === undefined) {
await fastify.register(fastifyViewCache, opts)
}
if (!opts.engine) {
throw new Error('Missing engine')
}
const type = Object.keys(opts.engine)[0]
if (supportedEngines.indexOf(type) === -1) {
throw new Error(`'${type}' not yet supported, PR? :)`)
}
const charset = opts.charset || 'utf-8'
const propertyName = opts.propertyName || 'view'
const asyncPropertyName = opts.asyncPropertyName || `${propertyName}Async`
const engine = await opts.engine[type]
const globalOptions = opts.options || {}
const templatesDir = resolveTemplateDir(opts)
const includeViewExtension = opts.includeViewExtension || false
const viewExt = opts.viewExt || ''
const prod = typeof opts.production === 'boolean' ? opts.production : process.env.NODE_ENV === 'production'
const defaultCtx = opts.defaultContext
const globalLayoutFileName = opts.layout
/**
* @type {Map<string, Promise>}
*/
const readFileMap = new Map()
function readFileSemaphore (filePath) {
if (readFileMap.has(filePath) === false) {
const promise = readFile(filePath, 'utf-8')
readFileMap.set(filePath, promise)
return promise.finally(() => readFileMap.delete(filePath))
}
return readFileMap.get(filePath)
}
function templatesDirIsValid (_templatesDir) {
if (Array.isArray(_templatesDir) && type !== 'nunjucks') {
throw new Error('Only Nunjucks supports the "templates" option as an array')
}
}
function layoutIsValid (_layoutFileName) {
if (type !== 'dot' && type !== 'handlebars' && type !== 'ejs' && type !== 'eta') {
throw new Error('Only Dot, Handlebars, EJS, and Eta support the "layout" option')
}
if (!hasAccessToLayoutFile(_layoutFileName, getDefaultExtension(type))) {
throw new Error(`unable to access template "${_layoutFileName}"`)
}
}
function setupNunjucksEnv (_engine) {
if (type === 'nunjucks') {
const env = _engine.configure(templatesDir, globalOptions)
if (typeof globalOptions.onConfigure === 'function') {
globalOptions.onConfigure(env)
}
return env
}
return null
}
templatesDirIsValid(templatesDir)
if (globalLayoutFileName) {
layoutIsValid(globalLayoutFileName)
}
const dotRender = type === 'dot' ? viewDot.call(fastify, preProcessDot.call(fastify, templatesDir, globalOptions)) : null
const nunjucksEnv = setupNunjucksEnv(engine)
const renders = {
ejs: withLayout(viewEjs, globalLayoutFileName),
handlebars: withLayout(viewHandlebars, globalLayoutFileName),
mustache: viewMustache,
nunjucks: viewNunjucks,
'art-template': viewArtTemplate,
twig: viewTwig,
liquid: viewLiquid,
dot: withLayout(dotRender, globalLayoutFileName),
eta: withLayout(viewEta, globalLayoutFileName),
_default: view
}
const renderer = renders[type] ? renders[type] : renders._default
async function asyncRender (page) {
if (!page) {
throw new Error('Missing page')
}
let result = await renderer.apply(this, arguments)
if (minify && !isPathExcludedMinification(this)) {
result = await minify(result, globalOptions.htmlMinifierOptions)
}
if (this.getHeader && !this.getHeader('Content-Type')) {
this.header('Content-Type', 'text/html; charset=' + charset)
}
return result
}
function viewDecorator (page) {
const args = Array.from(arguments)
let done
if (typeof args[args.length - 1] === 'function') {
done = args.pop()
}
const promise = asyncRender.apply({}, args)
if (typeof done === 'function') {
promise.then(done.bind(null, null), done)
return
}
return promise
}
viewDecorator.clearCache = function () {
fastify[viewCache].clear()
}
fastify.decorate(propertyName, viewDecorator)
fastify.decorateReply(propertyName, async function (page, data, opts) {
try {
const html = await asyncRender.call(this, page, data, opts)
this.send(html)
} catch (err) {
this.send(err)
}
return this
})
fastify.decorateReply(asyncPropertyName, asyncRender)
if (!fastify.hasReplyDecorator('locals')) {
fastify.decorateReply('locals', null)
fastify.addHook('onRequest', (req, reply, done) => {
reply.locals = {}
done()
})
}
function getPage (page, extension) {
const pageLRU = `getPage-${page}-${extension}`
let result = fastify[viewCache].get(pageLRU)
if (typeof result === 'string') {
return result
}
const filename = basename(page, extname(page))
result = join(dirname(page), filename + getExtension(page, extension))
fastify[viewCache].set(pageLRU, result)
return result
}
function getDefaultExtension (type) {
const mappedExtensions = {
'art-template': 'art',
handlebars: 'hbs',
nunjucks: 'njk'
}
return viewExt || (mappedExtensions[type] || type)
}
function getExtension (page, extension) {
let filextension = extname(page)
if (!filextension) {
filextension = '.' + getDefaultExtension(type)
}
return viewExt ? `.${viewExt}` : (includeViewExtension ? `.${extension}` : filextension)
}
const minify = typeof globalOptions.useHtmlMinifier?.minify === 'function'
? globalOptions.useHtmlMinifier.minify
: null
const minifyExcludedPaths = Array.isArray(globalOptions.pathsToExcludeHtmlMinifier)
? new Set(globalOptions.pathsToExcludeHtmlMinifier)
: null
function getRequestedPath (fastify) {
return fastify?.request?.routeOptions.url ?? null
}
function isPathExcludedMinification (that) {
return minifyExcludedPaths?.has(getRequestedPath(that))
}
function onTemplatesLoaded (file, data) {
if (type === 'handlebars') {
data = engine.compile(data, globalOptions.compileOptions)
}
fastify[viewCache].set(file, data)
return data
}
// Gets template as string (or precompiled for Handlebars)
// from LRU cache or filesystem.
const getTemplate = async function (file) {
if (typeof file === 'function') {
return file
}
let isRaw = false
if (typeof file === 'object' && file.raw) {
isRaw = true
file = file.raw
}
const data = fastify[viewCache].get(file)
if (data && prod) {
return data
}
if (isRaw) {
return onTemplatesLoaded(file, file)
}
const fileData = await readFileSemaphore(join(templatesDir, file))
return onTemplatesLoaded(file, fileData)
}
// Gets partials as collection of strings from LRU cache or filesystem.
const getPartials = async function (page, { partials, requestedPath }) {
const cacheKey = getPartialsCacheKey(page, partials, requestedPath)
const partialsObj = fastify[viewCache].get(cacheKey)
if (partialsObj && prod) {
return partialsObj
} else {
const partialKeys = Object.keys(partials)
if (partialKeys.length === 0) {
return {}
}
const partialsHtml = {}
await Promise.all(partialKeys.map(async (key) => {
partialsHtml[key] = await readFileSemaphore(join(templatesDir, partials[key]))
}))
fastify[viewCache].set(cacheKey, partialsHtml)
return partialsHtml
}
}
function getPartialsCacheKey (page, partials, requestedPath) {
let cacheKey = page
for (const key of Object.keys(partials)) {
cacheKey += `|${key}:${partials[key]}`
}
cacheKey += `|${requestedPath}-Partials`
return cacheKey
}
function readCallbackParser (page, html, localOptions) {
if ((type === 'ejs') && viewExt && !globalOptions.includer) {
globalOptions.includer = (originalPath, parsedPath) => {
return {
filename: parsedPath || join(templatesDir, originalPath + '.' + viewExt)
}
}
}
if (localOptions) {
for (const key in globalOptions) {
if (!Object.hasOwn(localOptions, key)) localOptions[key] = globalOptions[key]
}
} else localOptions = globalOptions
const compiledPage = engine.compile(html, localOptions)
fastify[viewCache].set(page, compiledPage)
return compiledPage
}
function readCallback (page, data, localOptions, html) {
globalOptions.filename = join(templatesDir, page)
return readCallbackParser(page, html, localOptions)
}
function preProcessDot (templatesDir, options) {
// Process all templates to in memory functions
// https://github.com/olado/doT#security-considerations
const destinationDir = options.destination || join(__dirname, 'out')
if (!existsSync(destinationDir)) {
mkdirSync(destinationDir)
}
const renderer = engine.process(Object.assign(
{},
options,
{
path: templatesDir,
destination: destinationDir
}
))
// .jst files are compiled to .js files so we need to require them
for (const file of readdirSync(destinationDir, { withFileTypes: false })) {
renderer[basename(file, '.js')] = require(resolve(join(destinationDir, file)))
}
if (Object.keys(renderer).length === 0) {
this.log.warn(`WARN: no template found in ${templatesDir}`)
}
return renderer
}
async function view (page, data, opts) {
data = Object.assign({}, defaultCtx, this.locals, data)
if (typeof page === 'function') {
return page(data)
}
let isRaw = false
if (typeof page === 'object' && page.raw) {
isRaw = true
page = page.raw.toString()
} else {
// append view extension
page = getPage(page, type)
}
const toHtml = fastify[viewCache].get(page)
if (toHtml && prod) {
return toHtml(data)
} else if (isRaw) {
const compiledPage = readCallbackParser(page, page, opts)
return compiledPage(data)
}
const file = await readFileSemaphore(join(templatesDir, page))
const render = readCallback(page, data, opts, file)
return render(data)
}
async function viewEjs (page, data, opts) {
if (opts?.layout) {
layoutIsValid(opts.layout)
return withLayout(viewEjs, opts.layout).call(this, page, data)
}
data = Object.assign({}, defaultCtx, this.locals, data)
if (typeof page === 'function') {
return page(data)
}
let isRaw = false
if (typeof page === 'object' && page.raw) {
isRaw = true
page = page.raw.toString()
} else {
// append view extension
page = getPage(page, type)
}
const toHtml = fastify[viewCache].get(page)
if (toHtml && prod) {
return toHtml(data)
} else if (isRaw) {
const compiledPage = readCallbackParser(page, page, opts)
return compiledPage(data)
}
const file = await readFileSemaphore(join(templatesDir, page))
const render = readCallback(page, data, opts, file)
return render(data)
}
async function viewArtTemplate (page, data) {
data = Object.assign({}, defaultCtx, this.locals, data)
if (typeof page === 'string') {
// Append view extension.
page = getPage(page, 'art')
}
const defaultSetting = {
debug: process.env.NODE_ENV !== 'production',
root: templatesDir
}
// merge engine options
const confs = Object.assign({}, defaultSetting, globalOptions)
function render (filename, data) {
let render
if (typeof filename === 'string') {
confs.filename = join(templatesDir, filename)
render = engine.compile(confs)
} else if (typeof filename === 'function') {
render = filename
} else if (typeof filename === 'object' && filename.raw) {
confs.source = filename.raw.toString()
render = engine.compile(confs)
} else {
throw new Error('Unknown template type')
}
return render(data)
}
return render(page, data)
}
async function viewNunjucks (page, data) {
data = Object.assign({}, defaultCtx, this.locals, data)
let render
if (typeof page === 'string') {
// Append view extension.
page = getPage(page, 'njk')
render = nunjucksEnv.render.bind(nunjucksEnv, page)
} else if (typeof page === 'object' && typeof page.render === 'function') {
render = page.render.bind(page)
} else if (typeof page === 'object' && page.raw) {
render = nunjucksEnv.renderString.bind(nunjucksEnv, page.raw.toString())
} else {
throw new Error('Unknown template type')
}
return new Promise((resolve, reject) => {
render(data, (err, html) => {
if (err) {
reject(err)
return
}
resolve(html)
})
})
}
async function viewHandlebars (page, data, opts) {
if (opts?.layout) {
layoutIsValid(opts.layout)
return withLayout(viewHandlebars, opts.layout).call(this, page, data)
}
let options
if (globalOptions.useDataVariables) {
options = {
data: defaultCtx ? Object.assign({}, defaultCtx, this.locals) : this.locals
}
} else {
data = Object.assign({}, defaultCtx, this.locals, data)
}
if (typeof page === 'string') {
// append view extension
page = getPage(page, 'hbs')
}
const requestedPath = getRequestedPath(this)
const template = await getTemplate(page)
if (prod) {
const html = template(data, options)
return html
} else {
const partialsObject = await getPartials(type, { partials: globalOptions.partials || {}, requestedPath })
Object.keys(partialsObject).forEach((name) => {
engine.registerPartial(name, engine.compile(partialsObject[name], globalOptions.compileOptions))
})
return template(data, options)
}
}
async function viewMustache (page, data, opts) {
const options = Object.assign({}, opts)
data = Object.assign({}, defaultCtx, this.locals, data)
if (typeof page === 'string') {
// append view extension
page = getPage(page, 'mustache')
}
const requestedPath = getRequestedPath(this)
const templateString = await getTemplate(page)
const partialsObject = await getPartials(page, { partials: options.partials || {}, requestedPath })
let html
if (typeof templateString === 'function') {
html = templateString(data, partialsObject)
} else {
html = engine.render(templateString, data, partialsObject)
}
return html
}
async function viewTwig (page, data, opts) {
data = Object.assign({}, defaultCtx, globalOptions, this.locals, data)
let render
if (typeof page === 'string') {
// Append view extension.
page = getPage(page, 'twig')
render = engine.renderFile.bind(engine, join(templatesDir, page))
} else if (typeof page === 'object' && typeof page.render === 'function') {
render = (data, cb) => cb(null, page.render(data))
} else if (typeof page === 'object' && page.raw) {
render = (data, cb) => cb(null, engine.twig({ data: page.raw.toString() }).render(data))
} else {
throw new Error('Unknown template type')
}
return new Promise((resolve, reject) => {
render(data, (err, html) => {
if (err) {
reject(err)
return
}
resolve(html)
})
})
}
async function viewLiquid (page, data, opts) {
data = Object.assign({}, defaultCtx, this.locals, data)
let render
if (typeof page === 'string') {
// Append view extension.
page = getPage(page, 'liquid')
render = engine.renderFile.bind(engine, join(templatesDir, page))
} else if (typeof page === 'function') {
render = page
} else if (typeof page === 'object' && page.raw) {
const templates = engine.parse(page.raw)
render = engine.render.bind(engine, templates)
}
return render(data, opts)
}
function viewDot (renderModule) {
return async function _viewDot (page, data, opts) {
if (opts?.layout) {
layoutIsValid(opts.layout)
return withLayout(dotRender, opts.layout).call(this, page, data)
}
data = Object.assign({}, defaultCtx, this.locals, data)
let render
if (typeof page === 'function') {
render = page
} else if (typeof page === 'object' && page.raw) {
render = engine.template(page.raw.toString(), { ...engine.templateSettings, ...globalOptions, ...page.settings }, page.imports)
} else {
render = renderModule[page]
}
return render(data)
}
}
async function viewEta (page, data, opts) {
if (opts?.layout) {
layoutIsValid(opts.layout)
return withLayout(viewEta, opts.layout).call(this, page, data)
}
if (globalOptions.templatesSync) {
engine.templatesSync = globalOptions.templatesSync
}
engine.configure({
views: templatesDir,
cache: prod || globalOptions.templatesSync
})
const config = Object.assign({
cache: prod,
views: templatesDir
}, globalOptions)
data = Object.assign({}, defaultCtx, this.locals, data)
if (typeof page === 'function') {
const ret = await page.call(engine, data, config)
return ret
}
let render, renderAsync
if (typeof page === 'object' && page.raw) {
page = page.raw.toString()
render = engine.renderString.bind(engine)
renderAsync = engine.renderStringAsync.bind(engine)
} else {
// Append view extension (Eta will append '.eta' by default,
// but this also allows custom extensions)
page = getPage(page, 'eta')
render = engine.render.bind(engine)
renderAsync = engine.renderAsync.bind(engine)
}
/* c8 ignore next */
if (opts?.async ?? globalOptions.async) {
return renderAsync(page, data, config)
} else {
return render(page, data, config)
}
}
if (prod && type === 'handlebars' && globalOptions.partials) {
const partialsObject = await getPartials(type, { partials: globalOptions.partials, requestedPath: getRequestedPath(this) })
Object.keys(partialsObject).forEach((name) => {
engine.registerPartial(name, engine.compile(partialsObject[name], globalOptions.compileOptions))
})
}
function withLayout (render, layout) {
if (layout) {
return async function (page, data, opts) {
if (opts?.layout) throw new Error('A layout can either be set globally or on render, not both.')
data = Object.assign({}, defaultCtx, this.locals, data)
const result = await render.call(this, page, data, opts)
data = Object.assign(data, { body: result })
return render.call(this, layout, data, opts)
}
}
return render
}
function resolveTemplateDir (_opts) {
if (_opts.root) {
return _opts.root
}
return Array.isArray(_opts.templates)
? _opts.templates.map((dir) => resolve(dir))
: resolve(_opts.templates || './')
}
function hasAccessToLayoutFile (fileName, ext) {
const layoutKey = `layout-${fileName}-${ext}`
let result = fastify[viewCache].get(layoutKey)
if (typeof result === 'boolean') {
return result
}
try {
accessSync(join(templatesDir, getPage(fileName, ext)))
result = true
} catch (e) {
result = false
}
fastify[viewCache].set(layoutKey, result)
return result
}
}
module.exports = fp(fastifyView, {
fastify: '5.x',
name: '@fastify/view'
})
module.exports.default = fastifyView
module.exports.fastifyView = fastifyView
module.exports.fastifyViewCache = viewCache