-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (135 loc) · 4.31 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
})
const VARIANTS_URL = 'https://cfw-takehome.developers.workers.dev/api/variants';
/**
* Responds with a random page from our constant VARIANTS_URL response
* Handles 500 error if there is an issue with our data from VARIANTS_URL
*/
async function handleRequest(request) {
const cookies = getCookies(request.headers.get('Cookie'));
const cookieVariantUrl = cookies['variant'];
const res = await fetch(VARIANTS_URL);
if (res.ok) {
const data = await res.json();
// Check for variants field and length
if (data.variants && data.variants.length && data.variants.length > 0) {
// Check if our given cookie variant URL is included for security reasons (don't want peopel requesting a random URL via cookie modification)
const variantUrl = cookieVariantUrl && data.variants.includes(cookieVariantUrl) ? cookieVariantUrl : chooseRandom(data.variants);
const variantResponse = await fetch(variantUrl);
const response = addVariantCookie(variantResponse, variantUrl)
return modifyResponse(response);
}
}
// We only hit this if !res.ok or data.variants is invalid
return new Response('An internal error occurred', {
headers: {
'content-type': 'text/plain'
},
status: 500,
statusText: 'Internal Server Error'
})
}
/**
* Parses cookie string into an object
* @param cookieString Cookies from string given in headers
*/
function getCookies(cookieString) {
const cookies = {};
cookieString && cookieString.split(';').forEach((cookie) =>{
const parts = cookie.match(/(.*?)=(.*)$/)
cookies[ parts[1].trim() ] = (parts[2] || '').trim();
});
return cookies;
};
/**
* Chooses a random element from the array
* If the array is empty, will return undefined
* @param array array to choose random from
*/
function chooseRandom(array) {
if (array.length == 0) {
return undefined;
}
const index = Math.floor(Math.random() * array.length);
return array[index];
}
function addVariantCookie(response, variantUrl) {
// Make the headers mutable by re-constructing the Response.
const modifiedResponse = new Response(response.body, response)
modifiedResponse.headers.set('Set-Cookie', `variant=${variantUrl}`);
return modifiedResponse;
}
class TitleRewriter {
/**
* Modifies the title element of page
* @param element element to modify
*/
element(element) {
element.setInnerContent('Your Variant!')
}
}
class PrependTextRewriter {
buffer;
regexMatch;
prependText;
constructor(regexMatch, prependText) {
this.regexMatch = regexMatch;
this.prependText = prependText;
this.buffer = '';
}
/**
* Adds prepend text before the regex we're looking for
* i.e. 'Variant X' -> 'Variant #X' where we match X and prepend '#'
* @param textChunk text we're processing
*/
text(textChunk) {
this.buffer += textChunk.text;
if (textChunk.lastInTextNode) {
// Replace last text chunk with our modified version of the whole node
textChunk.replace(this.buffer.replace(this.regexMatch, `${this.prependText}$1`))
this.buffer = '';
} else {
// Remove text chunk if its not the last one.
textChunk.remove();
}
}
}
class URLRewriter {
buffer = '';
/**
* Changes the link to my LinkedIn (which also links to my GitHub and side project/company :) )
* @param element element to process
*/
element(element) {
element.setAttribute('href', 'https://www.linkedin.com/in/linaresfelipe/')
}
/**
* Modifies the text of the element to my custom text
* @param textChunk textChunk to process
*/
text(textChunk) {
this.buffer += textChunk.text;
if (textChunk.lastInTextNode) {
textChunk.replace('Check out my Projects and Experience!')
this.buffer = '';
} else {
textChunk.remove();
}
}
}
/**
* Rewriter to modify our outgoing HTML response
*/
const HTML_REWRITER = new HTMLRewriter()
.on('title', new TitleRewriter())
.on('h1#title', new PrependTextRewriter(/(\d)/g, '#'))
.on('p#description', new PrependTextRewriter(/((one)|(two))/, 'number '))
.on('a#url', new URLRewriter());
/**
* Modifies a response given our html rewriter
* @param response response to modify
*/
function modifyResponse(response) {
return HTML_REWRITER.transform(response);
}