Feat: fileloader should be independent

For security reasons, Sometimes the template file may have some
pre-processing.For example, inserts some checking data in the form of
time reading the html file.

This amendment provides similar 'cache' to set custom fileloader.

resolve mde/ejs#125
This commit is contained in:
xinglong.wangwxl
2016-01-30 23:22:15 +08:00
parent aa1771e28b
commit d456f05a9a
3 changed files with 64 additions and 7 deletions

View File

@@ -162,6 +162,21 @@ If you want to clear the EJS cache, call `ejs.clearCache`. If you're using the
LRU cache and need a different limit, simple reset `ejs.cache` to a new instance
of the LRU.
## Custom FileLoader
The default file loader is `fs.readFileSync`, if you want to customize, You can override the fileLoader method.
```javascript
var ejs = require('ejs');
var myFileLoad = function (filePath) {
return 'myFileLoad: ' + fs.readFileSync(filePath);
};
ejs.fileLoader = myFileLoad;
```
With this feature, you can preprocess the template before reading it.
## Layouts
EJS does not specifically support blocks, but layouts can be implemented by

View File

@@ -68,6 +68,17 @@ var _BOM = /^\uFEFF/;
exports.cache = utils.cache;
/**
* For security reasons, Sometimes the template file may have some
* pre-processing.For example, inserts some checking data in the form of
* time reading the html file.
*
* @type {Function}
* @public
*/
exports.fileLoader = fs.readFileSync;
/**
* Name of the object containing the locals.
*
@@ -154,7 +165,7 @@ function handleCache(options, template) {
return func;
}
if (!hasTemplate) {
template = fs.readFileSync(filename).toString().replace(_BOM, '');
template = fileLoader(filename).toString().replace(_BOM, '');
}
}
else if (!hasTemplate) {
@@ -163,7 +174,7 @@ function handleCache(options, template) {
throw new Error('Internal EJS error: no file name or template '
+ 'provided');
}
template = fs.readFileSync(filename).toString().replace(_BOM, '');
template = fileLoader(filename).toString().replace(_BOM, '');
}
func = exports.compile(template, options);
if (options.cache) {
@@ -172,6 +183,18 @@ function handleCache(options, template) {
return func;
}
/**
* fileLoader is independent
*
* @param {String} filePath ejs file path.
* @return {String} The contents of the specified file.
* @static
*/
function fileLoader(filePath){
return exports.fileLoader(filePath);
}
/**
* Get the template function.
*
@@ -205,8 +228,8 @@ function includeSource(path, options) {
var opts = utils.shallowCopy({}, options);
var includePath;
var template;
includePath = getIncludePath(path,opts);
template = fs.readFileSync(includePath).toString().replace(_BOM, '');
includePath = getIncludePath(path, opts);
template = fileLoader(includePath).toString().replace(_BOM, '');
opts.filename = includePath;
var templ = new Template(template, opts);
templ.generateSource();
@@ -254,7 +277,7 @@ function rethrow(err, str, flnm, lineno, esc){
throw err;
}
function stripSemi(str) {
function stripSemi(str){
return str.replace(/;(\s*$)/, '$1');
}
@@ -335,7 +358,7 @@ exports.renderFile = function () {
var cb = args.pop();
var data = args.shift() || {};
var opts = args.pop() || {};
var optsKeys =_OPTS.slice();
var optsKeys = _OPTS.slice();
var result;
// Don't pollute passed in opts obj with new vals
@@ -766,7 +789,7 @@ if (require.extensions) {
filename: filename,
client: true
};
var template = fs.readFileSync(filename).toString();
var template = fileLoader(filename).toString();
var fn = exports.compile(template, options);
module._compile('module.exports = ' + fn.toString() + ';', filename);
};

View File

@@ -984,6 +984,25 @@ suite('require', function () {
});
});
suite('test fileloader', function () {
var myFileLoad = function (filePath) {
return 'myFileLoad: ' + fs.readFileSync(filePath);
};
test('test custom fileload', function (done) {
ejs.fileLoader = myFileLoad;
ejs.renderFile('test/fixtures/para.ejs', function(err, html) {
if (err) {
return done(err);
}
assert.equal(html, 'myFileLoad: <p>hey</p>\n');
done();
});
});
});
suite('examples', function () {
function noop () {}
fs.readdirSync('examples').forEach(function (f) {