This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
nodejs-razor/Processor/File.js

45 lines
1.0 KiB
JavaScript

let parser = require("./Parse");
let crypto = require("node:crypto");
let vm = require("node:vm");
module.exports = File;
/**
* @class File
*/
function File()
{
this.content = "";
this.contentmd5 = "";
this.parsedContent = "";
this.cachedData = undefined;
};
function sha256(content)
{
return crypto.createHash("sha256").update(content,'utf-8').digest('hex');
}
File.prototype.build = function(){
this.contentmd5 = sha256(this.content);
this.parsedContent = parser(this);
};
File.prototype.isRequiredRebuild = function(content){
return this.contentmd5 == sha256(content);
};
File.prototype.execute = function(memoriable){
let output = [];
let t = new vm.Script(this.parsedContent.ast,{
cachedData: this.cachedData,
});
let content = vm.createContext({
...memoriable,
output,
echo:(...args) => output.push(...args),
_external: e => {}
});
t.runInContext(content);
this.cachedData = t.createCachedData();
return output.join('');
};