<commit_before>var Hop = require('./api');
var combustion = require('combustion')();
var fs = require('fs');
var path = require('path');
require('./util');


/**
Inserts the Hop calls into express

@param {string} basePath 
	the path which the Hop will hang off of
@param {object} app 
	the express app

@for Hop
@method apiHook
**/
Hop.apiHook=function(basePath,app,options){
	
	options=options||{};

	Hop.basePath=basePath;
	for(var objectName in Hop.Objects){
		var obj = Hop.Objects[objectName];
		for(var methodName in obj.methods){
			var method = obj.methods[methodName];
			Hop.log(method.method,method.getPath());	
			(function(obj,method){
							app[method.method](method.getPath(),function(req,res){
								Hop.log("-",method.method,method.getPath());	

								var input = {};
						
								if(req.body._hopRedirect || req.query._hopRedirect){
									var redirect = req.body._hopRedirect || req.query._hopRedirect;
								}

								if(req.body._hopTemplate || req.query._hopTemplate){
									var template = req.body._hopTemplate || req.query._hopTemplate;
								}
	
								for(var paramName in method.params){
									var param = method.params[paramName];
									var value=undefined;
									if(method.method=="get" || method.method=="del" || method.method=="delete")
										value = req.params[paramName] || req.query[paramName];
									else if(method.method=="post" || method.method=="put") {
										if(req.files && (param.demandFile || param.optionalFile)){
											value = req.files[paramName];	
										} else if(req.body){
											value = req.params[paramName] || req.body[paramName];
										} else {
											value = req.params[paramName];
											
										}
									}
									if(param.demand && typeof value=="undefined"){
										return res.send(400,"Missing parameter:"+paramName);
									}
									input[paramName]=value;
								}
								//try {
									req._response = res;
									req.getResponse=function(){ return res; };
									var called=false;
									Hop.call(obj.name+"."+method.name,input,function(err,result){
											if(called){
												Hop.error(obj.name+"."+method.name,"called onComplete twice");	
											} else {
												called=true;
											try {
												if(redirect)
													return res.redirect(redirect);
												if(template)
													return res.render(template,{ result: result, error: err});	

												if(err){
													res.send(500,err);
													Hop.error(err);
												} else {
													if(result instanceof Hop.File){
														result.send(res);
													} else {
														if(result===null || result===undefined){
															res.send(404,"Not found");
														} else { 
															res.json(result);
														}
													}
												}
											} catch(e){
												Hop.error(e);
											}
										}
									},req);
								/*
								} catch(e){
									res.send(500,e.toString()+"\n"+e.stack());	
								}*/
							});
			})(obj,method);
		}
	}
	var cwd = path.join(path.dirname(module.filename),"../");
	Hop.docURL = webpath.join(basePath,"/");
	app.get(webpath.join(basePath,"/"),function(req,res){
		var apiChecksum = (Hop.checksum());
		if(req.get("If-Non-Match")==apiChecksum){
			return res.send("Not modified",304);
		}
		fs.readFile(path.join(cwd,"static/doc.comb"),function(err,data){
			var template = combustion(data.toString());
			res.set('Content-Type', 'text/html');
			res.set("ETag",apiChecksum);	
			res.send(template(Hop));
		});
	});
	Hop.apiURL = webpath.join(basePath,"api.js");
	app.get(webpath.join(basePath,"api.js"),function(req,res){
		var apiChecksum = (Hop.checksum());
		if(req.get("If-Non-Match")==apiChecksum){
			return res.send("Not modified",304);
		}
		var file = path.join(cwd,"static/api.comb");
		fs.readFile(file,function(err,data){
			try {
				var template = combustion(data.toString());
			

				res.set('Content-Type', 'application/javascript');
				res.set('Content-Type', 'text/html');
				res.set("ETag",apiChecksum);	
				res.set("Hop-Checksum",apiChecksum);
				
				res.send(template({ Objects: Hop.Objects, Models: Hop.Models, _csrf: (req.session?req.session._csrf:null)}));

			} catch (e){
				Hop.error("Error parsing : api.js",e);
				Hop.error(e.stack());
			}
		});
	});
	Hop.testURL=webpath.join(basePath,"test.js");
	app.get(webpath.join(basePath,"test.js"),function(req,res){
		var apiChecksum = (Hop.checksum());
		if(req.get("If-Non-Match")==apiChecksum){
			return res.send("Not modified",304);
		}
		fs.readFile(path.join(cwd,"static/test.comb"),function(err,data){
			var template = combustion(data.toString());
			res.set('Content-Type', 'application/javascript');
			res.set("ETag",apiChecksum);	
			res.send(template(Hop));
		});
	});
	app.get(webpath.join(basePath,"/api.json"),function(req,res){
		res.contentType("application/json");
		res.send(Hop.toJSON());
	});
	app.get("/_hopjs/",function(req,res){
		res.redirect(basePath);
	});
	app.get("/_hopjs/api.json",function(req,res){
		res.contentType("application/json");
		res.send(Hop.toJSON());
	});
	app.get("/_hopjs/apitest.json",function(req,res){
		res.contentType("application/json");
		res.send(JSON.stringify(Hop.TestCases));
	});
	app.get("/_hopjs/apitest.js",function(req,res){
		res.contentType("text/javascript");
		res.send(Hop.TestCase.toStandAlone());
	});
}

var cwd = path.join(path.dirname(module.filename),"../");


/**
Render a template using the cumbustion template engine

@param {string} filename The filename of the template relative to the "./static" path
@param {object} input The inputs to the template

@for Hop
@method renderTemplate
**/
Hop.renderTemplate=function(filename,input){
	try {
		var file = path.join(cwd,"static",filename);
		if(path.existsSync(file)){
			var data = fs.readFileSync(file);
		} else {
			var data = fs.readFileSync(filename);
		}
		if(!data){
			return "Invalid template specified:"+filename;	
		}
		var template = combustion(data.toString(),input);
		return template;
	} catch(e){
		Hop.error(e);
		Hop.error(e.stack());
		return (file+":"+e.toString());
	}
}
<commit_msg>Change path.existsSync to fs.existsSync<commit_after>var Hop = require('./api');
var combustion = require('combustion')();
var fs = require('fs');
var path = require('path');
require('./util');


/**
Inserts the Hop calls into express

@param {string} basePath 
	the path which the Hop will hang off of
@param {object} app 
	the express app

@for Hop
@method apiHook
**/
Hop.apiHook=function(basePath,app,options){
	
	options=options||{};

	Hop.basePath=basePath;
	for(var objectName in Hop.Objects){
		var obj = Hop.Objects[objectName];
		for(var methodName in obj.methods){
			var method = obj.methods[methodName];
			Hop.log(method.method,method.getPath());	
			(function(obj,method){
							app[method.method](method.getPath(),function(req,res){
								Hop.log("-",method.method,method.getPath());	

								var input = {};
						
								if(req.body._hopRedirect || req.query._hopRedirect){
									var redirect = req.body._hopRedirect || req.query._hopRedirect;
								}

								if(req.body._hopTemplate || req.query._hopTemplate){
									var template = req.body._hopTemplate || req.query._hopTemplate;
								}
	
								for(var paramName in method.params){
									var param = method.params[paramName];
									var value=undefined;
									if(method.method=="get" || method.method=="del" || method.method=="delete")
										value = req.params[paramName] || req.query[paramName];
									else if(method.method=="post" || method.method=="put") {
										if(req.files && (param.demandFile || param.optionalFile)){
											value = req.files[paramName];	
										} else if(req.body){
											value = req.params[paramName] || req.body[paramName];
										} else {
											value = req.params[paramName];
											
										}
									}
									if(param.demand && typeof value=="undefined"){
										return res.send(400,"Missing parameter:"+paramName);
									}
									input[paramName]=value;
								}
								//try {
									req._response = res;
									req.getResponse=function(){ return res; };
									var called=false;
									Hop.call(obj.name+"."+method.name,input,function(err,result){
											if(called){
												Hop.error(obj.name+"."+method.name,"called onComplete twice");	
											} else {
												called=true;
											try {
												if(redirect)
													return res.redirect(redirect);
												if(template)
													return res.render(template,{ result: result, error: err});	

												if(err){
													res.send(500,err);
													Hop.error(err);
												} else {
													if(result instanceof Hop.File){
														result.send(res);
													} else {
														if(result===null || result===undefined){
															res.send(404,"Not found");
														} else { 
															res.json(result);
														}
													}
												}
											} catch(e){
												Hop.error(e);
											}
										}
									},req);
								/*
								} catch(e){
									res.send(500,e.toString()+"\n"+e.stack());	
								}*/
							});
			})(obj,method);
		}
	}
	var cwd = path.join(path.dirname(module.filename),"../");
	Hop.docURL = webpath.join(basePath,"/");
	app.get(webpath.join(basePath,"/"),function(req,res){
		var apiChecksum = (Hop.checksum());
		if(req.get("If-Non-Match")==apiChecksum){
			return res.send("Not modified",304);
		}
		fs.readFile(path.join(cwd,"static/doc.comb"),function(err,data){
			var template = combustion(data.toString());
			res.set('Content-Type', 'text/html');
			res.set("ETag",apiChecksum);	
			res.send(template(Hop));
		});
	});
	Hop.apiURL = webpath.join(basePath,"api.js");
	app.get(webpath.join(basePath,"api.js"),function(req,res){
		var apiChecksum = (Hop.checksum());
		if(req.get("If-Non-Match")==apiChecksum){
			return res.send("Not modified",304);
		}
		var file = path.join(cwd,"static/api.comb");
		fs.readFile(file,function(err,data){
			try {
				var template = combustion(data.toString());
			

				res.set('Content-Type', 'application/javascript');
				res.set('Content-Type', 'text/html');
				res.set("ETag",apiChecksum);	
				res.set("Hop-Checksum",apiChecksum);
				
				res.send(template({ Objects: Hop.Objects, Models: Hop.Models, _csrf: (req.session?req.session._csrf:null)}));

			} catch (e){
				Hop.error("Error parsing : api.js",e);
				Hop.error(e.stack());
			}
		});
	});
	Hop.testURL=webpath.join(basePath,"test.js");
	app.get(webpath.join(basePath,"test.js"),function(req,res){
		var apiChecksum = (Hop.checksum());
		if(req.get("If-Non-Match")==apiChecksum){
			return res.send("Not modified",304);
		}
		fs.readFile(path.join(cwd,"static/test.comb"),function(err,data){
			var template = combustion(data.toString());
			res.set('Content-Type', 'application/javascript');
			res.set("ETag",apiChecksum);	
			res.send(template(Hop));
		});
	});
	app.get(webpath.join(basePath,"/api.json"),function(req,res){
		res.contentType("application/json");
		res.send(Hop.toJSON());
	});
	app.get("/_hopjs/",function(req,res){
		res.redirect(basePath);
	});
	app.get("/_hopjs/api.json",function(req,res){
		res.contentType("application/json");
		res.send(Hop.toJSON());
	});
	app.get("/_hopjs/apitest.json",function(req,res){
		res.contentType("application/json");
		res.send(JSON.stringify(Hop.TestCases));
	});
	app.get("/_hopjs/apitest.js",function(req,res){
		res.contentType("text/javascript");
		res.send(Hop.TestCase.toStandAlone());
	});
}

var cwd = path.join(path.dirname(module.filename),"../");


/**
Render a template using the cumbustion template engine

@param {string} filename The filename of the template relative to the "./static" path
@param {object} input The inputs to the template

@for Hop
@method renderTemplate
**/
Hop.renderTemplate=function(filename,input){
	try {
		var file = path.join(cwd,"static",filename);
		if(fs.existsSync(file)){
			var data = fs.readFileSync(file);
		} else {
			var data = fs.readFileSync(filename);
		}
		if(!data){
			return "Invalid template specified:"+filename;	
		}
		var template = combustion(data.toString(),input);
		return template;
	} catch(e){
		Hop.error(e);
		Hop.error(e.stack());
		return (file+":"+e.toString());
	}
}

{'fraction_non_alphanumeric': 0.17803337113235057, 'fraction_numerical': 0.003401911550299692, 'mean_word_length': 2.868107769423559, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6292764', 'n_tokens_mistral': 5608, 'n_tokens_neox': 4061, 'n_words': 631}