-
Notifications
You must be signed in to change notification settings - Fork 19
/
chromeMiddleware.js
89 lines (81 loc) · 3.31 KB
/
chromeMiddleware.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const chromeCache = require("./chromeCache");
const { createTokenSlicer } = require("token-slice");
function SPACommentResolver(conf) {
return async function SPACommentResolverMiddleware(data, req, res) {
const isHTML = (res.getHeader("content-type") || "").includes("html");
if (isHTML) {
const host = req.headers["x-spandx-origin"];
const chromeParts = await chromeCache.getParts({
host,
legacy: true,
});
return data
.toString()
.replace(/<!--\s+SPA_HEAD\s+-->/, chromeParts.head)
.replace(/<!--\s+SPA_HEADER\s+-->/, chromeParts.header)
.replace(/<!--\s+SPA_FOOTER\s+-->/, chromeParts.footer);
} else {
return data;
}
};
}
function chromeSwapper(conf) {
return async function ChromeSwapperMiddleware(data, req, res) {
const isHTML = (res.getHeader("content-type") || "").includes("html");
const isPrimerAlready = req.url.startsWith("/services/primer");
if (isHTML && !isPrimerAlready) {
const origin = req.headers["x-spandx-origin"];
console.log({ origin });
const chromeParts = await chromeCache.getParts({
host: origin,
path: "/services/primer/",
useCached: false,
});
return data
.toString()
.replace(
/<!--\s*CP_PRIMER_HEAD\s*-->.*<!--\s*\/CP_PRIMER_HEAD\s*-->/s,
`${chromeParts.head}`
)
.replace(
/<!--\s*CP_PRIMER_HEADER\s*-->.*<!--\s*\/CP_PRIMER_HEADER\s*-->/s,
`${chromeParts.header}`
)
.replace(
/<!--\s*CP_PRIMER_FOOTER\s*-->.*<!--\s*\/CP_PRIMER_FOOTER\s*-->/s,
`${chromeParts.footer}`
);
// if any tokens were found, swap them
// if (tokens.result.length) {
// tokens.result.sort(
// (a, b) => a.outer.startIndex - b.outer.startIndex
// );
// // TODO make this work when only 1 or 2 of the token pairs were found
// const ret =
// // body pre-head
// body.slice(0, tokens.result[0].outer.startIndex) +
// // head
// chromeParts.head +
// // body post-head
// body.slice(
// tokens.result[0].outer.endIndex,
// tokens.result[1].outer.startIndex
// ) +
// // header
// chromeParts.header +
// // body post-header
// body.slice(
// tokens.result[1].outer.endIndex,
// tokens.result[2].outer.startIndex
// ) +
// // footer
// chromeParts.footer +
// // body post-footer
// body.slice(tokens.result[2].outer.endIndex, body.length);
// return "HELLO";
// }
}
return data;
};
}
module.exports = { SPACommentResolver, chromeSwapper };