Skip to content

Instantly share code, notes, and snippets.

@scripting
Created April 22, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scripting/22aabb93041a4b04e1ac69bc1ebb36aa to your computer and use it in GitHub Desktop.
Save scripting/22aabb93041a4b04e1ac69bc1ebb36aa to your computer and use it in GitHub Desktop.
Chuck this is how I run filter scripts. I probably will convert to requireFromString.
function runFilterScript (host, callback) { //3/23/20 by DW
var f = getFullFilePath (domainsPath) + host + filterFname;
fs.readFile (f, function (err, data) {
if (err) {
callback (false); //file doesn't exist -- we didn't run the filter script
}
else {
try {
const options = {
httpRequest,
serveLocalFile: function (f) {
console.log ("serveLocalFile (" + f + ")");
serveFile (f, config);
}
};
const filterfile = __dirname + "/" + f;
if (require.cache [filterfile] !== undefined) {
delete require.cache [filterfile];
}
require (filterfile).filter (options, function (err, httpResponse) {
if (err) {
httpRespond (500, "text/plain", err.message);
callback (true); //we handled it
}
else {
if (httpResponse.flNotHandled) { //the plugin doesn't want this, let other functions in pagePark have a try
callback (false);
}
else {
httpRespond (httpResponse.code, httpResponse.type, httpResponse.val);
callback (true); //we handled it
}
}
});
}
catch (err) {
httpRespond (500, "text/plain", err.message);
callback (true); //we handled it
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment