45 lines
1.0 KiB
JavaScript
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('');
|
|
}; |