forked from cdloh/cloudflare-esi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processEscaping.ts
43 lines (38 loc) · 1.02 KB
/
processEscaping.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { tagParser } from "./tagParser";
/**
* Processes chunk of text and handles <!--esi tags
*
* @param {string} chunk Chunk of text to process
* @param {string[]} [res] Array of chunks of text already processed (used internally within function)
* @returns {Promise<string>} processed string
*/
export async function process(
chunk: string,
res: Array<string> = []
): Promise<string> {
const parser = new tagParser(chunk);
let hasEscaping = false;
do {
const [tag, before, after] = await parser.next("!--esi");
if (tag && tag.closing && tag.contents) {
hasEscaping = true;
if (before) {
res.push(before);
}
if (tag.contents.search(/<!--esi/) !== -1) {
return process(tag.contents, res);
} else {
res.push(tag.contents);
if (after) res.push(after);
}
} else if (!tag) {
break;
}
// eslint-disable-next-line no-constant-condition
} while (true);
if (hasEscaping) {
return res.join("");
} else {
return chunk;
}
}