@bonniernews/local-esi
Advanced tools
Comparing version 0.0.1 to 0.0.2
77
index.js
@@ -9,8 +9,15 @@ "use strict"; | ||
function localEsi(html, req, callback) { | ||
const listener = ESIListener(ListenerContext(req)); | ||
transformHtml(html, listener, callback); | ||
function localEsi(html, req, res, next) { | ||
const context = ListenerContext(req, res); | ||
const listener = ESIListener(context); | ||
transformHtml(html, listener, (err, parsed) => { | ||
if (err) return next(err); | ||
if (context.replacement) { | ||
return res.send(context.replacement); | ||
} | ||
res.send(parsed); | ||
}); | ||
} | ||
function ListenerContext(req) { | ||
function ListenerContext(req, res) { | ||
return { | ||
@@ -25,3 +32,4 @@ esiChooseTags: [], | ||
req, | ||
isInEsiVarsContext: false, | ||
res, | ||
inEsiStatementProcessingContext: false, | ||
inAttempt: false, | ||
@@ -31,3 +39,4 @@ lastAttemptWasError: false, | ||
includeError: false, | ||
ignoreUntilNextEndChoose: false | ||
ignoreUntilNextEndChoose: false, | ||
replacement: "" | ||
}; | ||
@@ -79,7 +88,7 @@ } | ||
open(attribs, next) { | ||
context.isInEsiVarsContext = true; | ||
context.inEsiStatementProcessingContext = true; | ||
next(); | ||
}, | ||
close(next) { | ||
context.isInEsiVarsContext = false; | ||
context.inEsiStatementProcessingContext = false; | ||
next(); | ||
@@ -147,4 +156,9 @@ } | ||
lastChooseTag.shouldWrite = result; | ||
context.inEsiStatementProcessingContext = true; | ||
return next(); | ||
}, | ||
close(next) { | ||
context.inEsiStatementProcessingContext = false; | ||
next(); | ||
} | ||
@@ -156,3 +170,8 @@ }; | ||
getLastChooseTag().shouldWrite = !getLastChooseTag().foundMatchingTestAttribute; | ||
context.inEsiStatementProcessingContext = true; | ||
return next(); | ||
}, | ||
close(next) { | ||
context.inEsiStatementProcessingContext = false; | ||
next(); | ||
} | ||
@@ -241,13 +260,43 @@ }; | ||
function ontext(text, next) { | ||
if (!context.isInEsiVarsContext) { | ||
return writeToResult(text, next); | ||
if (context.inEsiStatementProcessingContext) { | ||
return writeToResult(handleProcessingInstructions(text), next); | ||
} | ||
writeToResult(text, next); | ||
} | ||
function handleProcessingInstructions(text) { | ||
text = text.replace(/\$add_header\('Set-Cookie', '([^']+).*?\)/ig, (_, cookieString) => { // PĂĽl should have all the credit for this | ||
const splitCookie = cookieString.split(/=|;/); | ||
context.req.res.cookie(splitCookie[0], splitCookie[1].replace(";", "")); | ||
if (shouldWrite()) { | ||
context.res.cookie(splitCookie[0], splitCookie[1].replace(";", "")); | ||
} | ||
return ""; | ||
}); | ||
writeToResult(text, next); | ||
text = text.replace(/\$set_response_code\(\s*(\d{3})\s*\)/ig, (_, responseCode) => { | ||
if (shouldWrite()) { | ||
context.res.status(parseInt(responseCode)); | ||
} | ||
return ""; | ||
}); | ||
text = text.replace(/\$set_response_code\(\s*(\d{3})\s*,\s*(')((.*?)\2\s*\))?/ig, (_, responseCode, _2, _3, content) => { | ||
if (!shouldWrite()) return ""; | ||
context.res.status(parseInt(responseCode)); | ||
if (content) { | ||
context.replacement = content; | ||
return ""; | ||
} | ||
context.inReplacement = true; | ||
return ""; | ||
}); | ||
if (context.inReplacement && text === "')") { | ||
context.inReplacement = false; | ||
text = ""; | ||
} | ||
return text; | ||
} | ||
@@ -290,2 +339,6 @@ | ||
function writeToResult(chunk, next) { | ||
if (context.inReplacement) { | ||
context.replacement += chunk; | ||
return next(); | ||
} | ||
if (shouldWrite()) { | ||
@@ -292,0 +345,0 @@ return next(null, chunk); |
{ | ||
"name": "@bonniernews/local-esi", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Local Edge Side Includes parser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17605
507