-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathmanager.js
executable file
·573 lines (407 loc) · 16.6 KB
/
manager.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
'use strict';
const Fs = require('fs');
const Path = require('path');
const Boom = require('@hapi/boom');
const Bounce = require('@hapi/bounce');
const Hoek = require('@hapi/hoek');
const Validate = require('@hapi/validate');
const Schemas = require('./schemas');
const Utils = require('./utils');
// Additional helper modules required in constructor
const internals = {};
internals.defaults = {
// defaultExtension: '',
// path: '',
// relativeTo: '',
compileOptions: {},
runtimeOptions: {},
layout: false,
layoutKeyword: 'content',
encoding: 'utf8',
isCached: true,
allowAbsolutePaths: false,
allowInsecureAccess: false,
// partialsPath: '',
contentType: 'text/html',
compileMode: 'sync',
context: null
};
module.exports = class Manager {
constructor(server, options) {
Validate.assert(options, Schemas.manager);
// Save non-defaults values
const engines = options.engines;
const defaultExtension = options.defaultExtension;
// Clone options
const defaults = Hoek.applyToDefaults(internals.defaults, options, { shallow: ['engines', 'context'] });
delete defaults.engines;
delete defaults.defaultExtension;
// Prepare manager state
const extensions = Object.keys(engines);
Hoek.assert(extensions.length, 'Views manager requires at least one registered extension handler');
// Private class props
this._server = server;
this._context = defaults.context;
this._engines = {};
this._defaultExtension = defaultExtension || (extensions.length === 1 ? extensions[0] : '');
// Load engines
for (const extension of extensions) {
const config = engines[extension];
const engine = {};
if (typeof config.compile === 'function') {
engine.module = config;
engine.config = defaults;
}
else {
Validate.assert(config, Schemas.view);
engine.module = config.module;
engine.config = Hoek.applyToDefaults(defaults, config, { shallow: ['module'] });
}
engine.suffix = '.' + extension;
engine.compileFunc = engine.module.compile;
if (engine.config.compileMode === 'sync') {
engine.compileFunc = function (str, opt, next) {
let compiled = null;
try {
compiled = engine.module.compile(str, opt);
}
catch (err) {
return next(err);
}
const renderer = function (context, runtimeOptions, renderNext) {
let rendered = null;
try {
rendered = compiled(context, runtimeOptions);
}
catch (err) {
return renderNext(err);
}
return renderNext(null, rendered);
};
return next(null, renderer);
};
}
if (engine.config.isCached) {
engine.cache = {};
}
// When a prepare function is provided, state needs to be initialized before trying to compile and render
engine.ready = !(engine.module.prepare && typeof engine.module.prepare === 'function');
// Load partials and helpers
this._loadPartials(engine);
this._loadHelpers(engine);
// Set engine
this._engines[extension] = engine;
}
}
_loadPartials(engine) {
if (!engine.config.partialsPath ||
!engine.module.registerPartial) {
return;
}
const traverse = function (path) {
let files = [];
Fs.readdirSync(path).forEach((file) => {
file = Path.join(path, file);
const stat = Fs.statSync(file);
if (stat.isDirectory()) {
files = files.concat(traverse(file));
return;
}
if (Path.basename(file)[0] !== '.' &&
Path.extname(file) === engine.suffix) {
files.push(file);
}
});
return files;
};
const partialsPaths = [].concat(engine.config.partialsPath);
for (const partialsPath of partialsPaths) {
const path = internals.path(engine.config.relativeTo, partialsPath);
const files = traverse(path);
for (const file of files) {
const offset = path.slice(-1) === Path.sep ? 0 : 1;
const name = file.slice(path.length + offset, -engine.suffix.length).replace(/\\/g, '/');
const src = Fs.readFileSync(file).toString(engine.config.encoding);
engine.module.registerPartial(name, src);
}
}
}
_loadHelpers(engine) {
if (!engine.config.helpersPath ||
!engine.module.registerHelper) {
return;
}
const helpersPaths = [].concat(engine.config.helpersPath);
for (const helpersPath of helpersPaths) {
let path = internals.path(engine.config.relativeTo, helpersPath);
if (!Path.isAbsolute(path)) {
path = Path.join(process.cwd(), path);
}
Fs.readdirSync(path).forEach((file) => {
file = Path.join(path, file);
const stat = Fs.statSync(file);
if (stat.isDirectory() ||
Path.basename(file).startsWith('.') ||
!Object.keys(require.extensions).includes(Path.extname(file))) {
return;
}
try {
if (!engine.config.isCached) {
this._bustRequireCache(file);
}
const required = require(file);
const offset = path.slice(-1) === Path.sep ? 0 : 1;
const name = file.slice(path.length + offset, -Path.extname(file).length);
const helper = required[name] || required.default || required;
if (typeof helper === 'function') {
engine.module.registerHelper(name, helper);
}
}
catch (err) {
this._server.log(['vision', 'helper', 'load', 'error'], { file, err });
}
});
}
}
async _prepare(template, options) {
options = options || {};
const fileExtension = Path.extname(template).slice(1);
const extension = fileExtension || this._defaultExtension;
if (!extension) {
throw Boom.badImplementation('Unknown extension and no defaultExtension configured for view template: ' + template);
}
const engine = this._engines[extension];
if (!engine) {
throw Boom.badImplementation('No view engine found for file: ' + template);
}
template = template + (fileExtension ? '' : engine.suffix);
if (!engine.ready) {
await this._prepareEngine(engine);
}
return this._prepareTemplates(template, engine, options);
}
_prepareEngine(engine) {
// _prepareEngine can only be invoked when the prepare function is defined
return new Promise((resolve, reject) => {
try {
return engine.module.prepare(engine.config, (err) => {
if (err) {
return reject(err);
}
engine.ready = true;
return resolve();
});
}
catch (err) {
return reject(err);
}
});
}
async _prepareTemplates(template, engine, options) {
const compiled = {
settings: Hoek.applyToDefaults(engine.config, options)
};
const templatePath = await this._path(template, compiled.settings, false);
if (!engine.config.isCached) {
this._loadPartials(engine);
this._loadHelpers(engine);
}
const compiledTemplate = await this._compile(templatePath, engine, compiled.settings);
compiled.template = compiledTemplate;
if (compiled.settings.layout) {
const layoutPath = await this._path((compiled.settings.layout === true ? 'layout' : compiled.settings.layout) + engine.suffix, compiled.settings, true);
compiled.layout = await this._compile(layoutPath, engine, compiled.settings);
}
return compiled;
}
async _path(template, settings, isLayout) {
// Validate path
const isAbsolutePath = Path.isAbsolute(template);
const isInsecurePath = template.match(/\.\.\//g);
if (!settings.allowAbsolutePaths &&
isAbsolutePath) {
throw Boom.badImplementation('Absolute paths are not allowed in views');
}
if (!settings.allowInsecureAccess &&
isInsecurePath) {
throw Boom.badImplementation('View paths cannot lookup templates outside root path (path includes one or more \'../\')');
}
// Resolve path and extension
let paths;
if (isAbsolutePath) {
paths = [template];
}
else {
paths = [].concat((isLayout && settings.layoutPath) || settings.path);
for (let i = 0; i < paths.length; ++i) {
paths[i] = internals.path(settings.relativeTo, paths[i], template);
}
}
for (const path of paths) {
try {
const stats = await Utils.stat(path);
if (stats.isFile()) {
return path;
}
}
catch (err) {
Bounce.rethrow(err, 'system');
}
}
throw Boom.badImplementation('View file not found: `' + template + '`. Locations searched: [' + paths.join(',') + ']');
}
async _compile(template, engine, settings) {
if (engine.cache &&
engine.cache[template]) {
return engine.cache[template];
}
settings.compileOptions.filename = template; // Pass the template to Pug via this copy of compileOptions
// Read file
try {
var data = await Utils.readFile(template, { encoding: settings.encoding });
}
catch (err) {
throw Boom.badImplementation('Failed to read view file: ' + template);
}
return new Promise((resolve, reject) => {
engine.compileFunc(data, settings.compileOptions, (err, compiled) => {
if (err) {
reject(Boom.boomify(err));
return;
}
if (engine.cache) {
engine.cache[template] = compiled;
}
resolve(compiled);
});
});
}
async _render(compiled, context, request, callback) {
if (this._context) {
let base = this._context;
if (typeof this._context === 'function') {
base = await base(request);
}
if (context) {
base = Object.assign({}, base); // Shallow cloned
const keys = Object.keys(context);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
base[key] = context[key];
}
}
context = base;
}
context = context || {};
if (compiled.layout &&
context.hasOwnProperty(compiled.settings.layoutKeyword)) {
throw Boom.badImplementation('settings.layoutKeyword conflict', { context, keyword: compiled.settings.layoutKeyword });
}
return new Promise((resolve, reject) => {
compiled.template(context, compiled.settings.runtimeOptions, (err, renderedContent) => {
if (err) {
reject(Boom.badImplementation(err.message, err));
return;
}
// No layout
if (!compiled.layout) {
resolve(renderedContent);
return;
}
// With layout
context[compiled.settings.layoutKeyword] = renderedContent;
compiled.layout(context, compiled.settings.runtimeOptions, (err, renderedWithLayout) => {
delete context[compiled.settings.layoutKeyword];
if (err) {
reject(Boom.badImplementation(err.message, err));
return;
}
resolve(renderedWithLayout);
});
});
});
}
_response(template, context, options, request) {
Validate.assert(options, Schemas.viewOverride);
const source = { manager: this, template, context, options };
return request.generateResponse(source, { variety: 'view', marshal: internals.marshal, prepare: internals.prepare });
}
_bustRequireCache(path) {
const modulekey = require.resolve(path);
const mod = require.cache[modulekey];
if (mod) {
// Remove module from require cache
delete require.cache[modulekey];
// Remove module references from parent module
mod.parent.children = mod.parent.children.filter((el) => el !== mod);
}
}
getEngine(ext) {
let engine;
if (!ext) {
// The _defaultExtension is set if there is a single extension in _engines
if (!this._defaultExtension) {
throw new Error('Must provide an extension or set defaultExtension in manager options');
}
engine = this._engines[this._defaultExtension];
}
else {
engine = this._engines[ext];
if (!engine) {
throw new Error(`Extension "${ext}" not found on manager`);
}
}
return engine;
}
clearCache(template, engine) {
if (!template) {
throw new Error('template is required');
}
// The _defaultExtension is set if there is a single extension in _engines
if (!engine && !Path.extname(template) && !this._defaultExtension) {
throw new Error('Must pass the engine, have a single engine on the manager, have an extension on the template name, or set defaultExtension on manager options');
}
const extension = Path.extname(template) ? Path.extname(template).slice(1) : this._defaultExtension;
engine = engine || this.getEngine(extension);
const templateWithSuffix = Path.extname(template) ? template : (template + engine.suffix);
const templateFullPath = internals.path(engine.config.relativeTo, engine.config.path, templateWithSuffix);
if (!engine.cache[templateFullPath]) {
this._server.log(['vision', 'cache', 'clear', 'error'], { template: templateFullPath });
}
delete engine.cache[templateFullPath];
}
registerHelper(name, helper) {
Object.keys(this._engines).forEach((extension) => {
const engine = this._engines[extension];
if (typeof engine.module.registerHelper === 'function') {
engine.module.registerHelper(name, helper);
}
});
}
async render(filename, context, options, request) {
const compiled = await this._prepare(filename, options);
return await this._render(compiled, context, request);
}
};
internals.path = function (base, path, file) {
if (path &&
Path.isAbsolute(path)) {
return Path.join(path, file || '');
}
return Path.join(base || '', path || '', file || '');
};
internals.marshal = async function (response) {
const manager = response.source.manager;
const rendered = await manager._render(response.source.compiled, response.source.context, response.request);
const config = response.source.compiled.settings;
if (!response.headers['content-type']) {
response.type(config.contentType);
}
response.encoding(config.encoding);
return rendered;
};
internals.prepare = async function (response) {
const manager = response.source.manager;
response.source.compiled = await manager._prepare(response.source.template, response.source.options);
return response;
};