-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
619 lines (597 loc) · 16 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// functions/_middleware.ts
var onRequestGet = async ({
next
}) => {
const response = await next();
return new HTMLRewriter().on("form", {
element(form) {
const formName = form.getAttribute("data-static-form-name");
form.setAttribute("method", "POST");
form.removeAttribute("action");
form.append(`<input type="hidden" name="static-form-name" value="${formName}" />`, {
html: true
});
}
}).transform(response);
};
// api/index.ts
var sendEmail = async (payload) => {
const response = await fetch("https://api.mailchannels.net/tx/v1/send", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
if (response.status === 202)
return {
success: true
};
try {
const {
errors
} = await response.clone().json();
return {
success: false,
errors
};
} catch {
return {
success: false,
errors: [response.statusText]
};
}
};
var textPlainContent = ({
request,
formData,
name
}) => {
return `At ${new Date().toISOString()}, you received a new ${name} form submission from ${request.headers.get("CF-Connecting-IP")}:
${[...formData.entries()].map(([field, value]) => `${field}
${value}
`).join("\n")}`;
};
var textHTMLContent = ({
request,
formData,
name
}) => {
return `<!DOCTYPE html>
<html>
<body>
<h1>New contact form submission</h1>
<div>At ${new Date().toISOString()}, you received a new ${name} form submission from ${request.headers.get("CF-Connecting-IP")}:</div>
<table>
<tbody>
${[...formData.entries()].map(([field, value]) => `<tr><td><strong>${field}</strong></td><td>${value}</td></tr>`).join("\n")}
</tbody>
</table>
</body>
</html>`;
};
var onFormSubmit = async ({
request,
env,
next,
pluginArgs
}) => {
let formData, name;
try {
formData = await request.formData();
name = formData.get("static-form-name").toString();
} catch {}
if (pluginArgs.turnstile) {
let token, secret;
token = formData.get('cf-turnstile-response') ? formData.get('cf-turnstile-response').toString() : false;
secret = env.TURNSTILE_KEY ? env.TURNSTILE_KEY.toString() : false;
if (!token) {
return new Response(`Turnstile = true - but no token found. Check the widget is rendering inside the <form> of your page: https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/.`, {
status: 512
})
};
if (!secret) {
return new Response(`Turnstile token found - but no secrey key set. Set an Environment variable with your Turnstile secret called "TURNSTILE_KEY" under Pages > Settings > Environment variables.`, {
status: 512
});
}
let ip = request.headers.get('CF-Connecting-IP');
let captchaData = new FormData();
captchaData.append('secret', secret);
captchaData.append('response', token);
captchaData.append('remoteip', ip);
let url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
let result = await fetch(url, {
body: captchaData,
method: 'POST',
});
let outcome = await result.json();
if (!outcome.success) {
console.log("Token Failure from " + ip);
return next();
}
formData.delete("cf-turnstile-response");
}
if (name) {
formData.delete("static-form-name");
let submission = {
formData,
name,
request
};
let personalizations = typeof pluginArgs.personalizations === "function" ? pluginArgs.personalizations(submission) : pluginArgs.personalizations;
let from = typeof pluginArgs.from === "function" ? pluginArgs.from(submission) : pluginArgs.from;
let subject = typeof pluginArgs.subject === "function" ? pluginArgs.subject(submission) : pluginArgs.subject || `New ${name} form submission`;
let content = pluginArgs.content ? pluginArgs.content(submission) : [{
type: "text/plain",
value: textPlainContent(submission)
},
{
type: "text/html",
value: textHTMLContent(submission)
}
];
let {
success
} = await sendEmail({
personalizations,
from,
subject,
content
});
if (success) {
return pluginArgs.respondWith(submission);
}
return new Response(`Could not send your email. Please try again.`, {
status: 512
});
}
return next();
};
// ../../../../../../var/folders/ww/hfjqy4gs3p12j4qfrlf026lr0000gp/T/functionsRoutes.mjs
var routes = [{
routePath: "/",
mountPath: "/",
method: "GET",
middlewares: [onRequestGet],
modules: []
},
{
routePath: "/",
mountPath: "/",
method: "POST",
middlewares: [onFormSubmit],
modules: []
}
];
// ../../node_modules/path-to-regexp/dist.es2015/index.js
function lexer(str) {
var tokens = [];
var i = 0;
while (i < str.length) {
var char = str[i];
if (char === "*" || char === "+" || char === "?") {
tokens.push({
type: "MODIFIER",
index: i,
value: str[i++]
});
continue;
}
if (char === "\\") {
tokens.push({
type: "ESCAPED_CHAR",
index: i++,
value: str[i++]
});
continue;
}
if (char === "{") {
tokens.push({
type: "OPEN",
index: i,
value: str[i++]
});
continue;
}
if (char === "}") {
tokens.push({
type: "CLOSE",
index: i,
value: str[i++]
});
continue;
}
if (char === ":") {
var name = "";
var j = i + 1;
while (j < str.length) {
var code = str.charCodeAt(j);
if (code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 95) {
name += str[j++];
continue;
}
break;
}
if (!name)
throw new TypeError("Missing parameter name at " + i);
tokens.push({
type: "NAME",
index: i,
value: name
});
i = j;
continue;
}
if (char === "(") {
var count = 1;
var pattern = "";
var j = i + 1;
if (str[j] === "?") {
throw new TypeError('Pattern cannot start with "?" at ' + j);
}
while (j < str.length) {
if (str[j] === "\\") {
pattern += str[j++] + str[j++];
continue;
}
if (str[j] === ")") {
count--;
if (count === 0) {
j++;
break;
}
} else if (str[j] === "(") {
count++;
if (str[j + 1] !== "?") {
throw new TypeError("Capturing groups are not allowed at " + j);
}
}
pattern += str[j++];
}
if (count)
throw new TypeError("Unbalanced pattern at " + i);
if (!pattern)
throw new TypeError("Missing pattern at " + i);
tokens.push({
type: "PATTERN",
index: i,
value: pattern
});
i = j;
continue;
}
tokens.push({
type: "CHAR",
index: i,
value: str[i++]
});
}
tokens.push({
type: "END",
index: i,
value: ""
});
return tokens;
}
function parse(str, options) {
if (options === void 0) {
options = {};
}
var tokens = lexer(str);
var _a = options.prefixes,
prefixes = _a === void 0 ? "./" : _a;
var defaultPattern = "[^" + escapeString(options.delimiter || "/#?") + "]+?";
var result = [];
var key = 0;
var i = 0;
var path = "";
var tryConsume = function (type) {
if (i < tokens.length && tokens[i].type === type)
return tokens[i++].value;
};
var mustConsume = function (type) {
var value2 = tryConsume(type);
if (value2 !== void 0)
return value2;
var _a2 = tokens[i],
nextType = _a2.type,
index = _a2.index;
throw new TypeError("Unexpected " + nextType + " at " + index + ", expected " + type);
};
var consumeText = function () {
var result2 = "";
var value2;
while (value2 = tryConsume("CHAR") || tryConsume("ESCAPED_CHAR")) {
result2 += value2;
}
return result2;
};
while (i < tokens.length) {
var char = tryConsume("CHAR");
var name = tryConsume("NAME");
var pattern = tryConsume("PATTERN");
if (name || pattern) {
var prefix = char || "";
if (prefixes.indexOf(prefix) === -1) {
path += prefix;
prefix = "";
}
if (path) {
result.push(path);
path = "";
}
result.push({
name: name || key++,
prefix,
suffix: "",
pattern: pattern || defaultPattern,
modifier: tryConsume("MODIFIER") || ""
});
continue;
}
var value = char || tryConsume("ESCAPED_CHAR");
if (value) {
path += value;
continue;
}
if (path) {
result.push(path);
path = "";
}
var open = tryConsume("OPEN");
if (open) {
var prefix = consumeText();
var name_1 = tryConsume("NAME") || "";
var pattern_1 = tryConsume("PATTERN") || "";
var suffix = consumeText();
mustConsume("CLOSE");
result.push({
name: name_1 || (pattern_1 ? key++ : ""),
pattern: name_1 && !pattern_1 ? defaultPattern : pattern_1,
prefix,
suffix,
modifier: tryConsume("MODIFIER") || ""
});
continue;
}
mustConsume("END");
}
return result;
}
function match(str, options) {
var keys = [];
var re = pathToRegexp(str, keys, options);
return regexpToFunction(re, keys, options);
}
function regexpToFunction(re, keys, options) {
if (options === void 0) {
options = {};
}
var _a = options.decode,
decode = _a === void 0 ? function (x) {
return x;
} : _a;
return function (pathname) {
var m = re.exec(pathname);
if (!m)
return false;
var path = m[0],
index = m.index;
var params = /* @__PURE__ */ Object.create(null);
var _loop_1 = function (i2) {
if (m[i2] === void 0)
return "continue";
var key = keys[i2 - 1];
if (key.modifier === "*" || key.modifier === "+") {
params[key.name] = m[i2].split(key.prefix + key.suffix).map(function (value) {
return decode(value, key);
});
} else {
params[key.name] = decode(m[i2], key);
}
};
for (var i = 1; i < m.length; i++) {
_loop_1(i);
}
return {
path,
index,
params
};
};
}
function escapeString(str) {
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
}
function flags(options) {
return options && options.sensitive ? "" : "i";
}
function regexpToRegexp(path, keys) {
if (!keys)
return path;
var groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;
var index = 0;
var execResult = groupsRegex.exec(path.source);
while (execResult) {
keys.push({
name: execResult[1] || index++,
prefix: "",
suffix: "",
modifier: "",
pattern: ""
});
execResult = groupsRegex.exec(path.source);
}
return path;
}
function arrayToRegexp(paths, keys, options) {
var parts = paths.map(function (path) {
return pathToRegexp(path, keys, options).source;
});
return new RegExp("(?:" + parts.join("|") + ")", flags(options));
}
function stringToRegexp(path, keys, options) {
return tokensToRegexp(parse(path, options), keys, options);
}
function tokensToRegexp(tokens, keys, options) {
if (options === void 0) {
options = {};
}
var _a = options.strict,
strict = _a === void 0 ? false : _a,
_b = options.start,
start = _b === void 0 ? true : _b,
_c = options.end,
end = _c === void 0 ? true : _c,
_d = options.encode,
encode = _d === void 0 ? function (x) {
return x;
} : _d;
var endsWith = "[" + escapeString(options.endsWith || "") + "]|$";
var delimiter = "[" + escapeString(options.delimiter || "/#?") + "]";
var route = start ? "^" : "";
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
var token = tokens_1[_i];
if (typeof token === "string") {
route += escapeString(encode(token));
} else {
var prefix = escapeString(encode(token.prefix));
var suffix = escapeString(encode(token.suffix));
if (token.pattern) {
if (keys)
keys.push(token);
if (prefix || suffix) {
if (token.modifier === "+" || token.modifier === "*") {
var mod = token.modifier === "*" ? "?" : "";
route += "(?:" + prefix + "((?:" + token.pattern + ")(?:" + suffix + prefix + "(?:" + token.pattern + "))*)" + suffix + ")" + mod;
} else {
route += "(?:" + prefix + "(" + token.pattern + ")" + suffix + ")" + token.modifier;
}
} else {
route += "(" + token.pattern + ")" + token.modifier;
}
} else {
route += "(?:" + prefix + suffix + ")" + token.modifier;
}
}
}
if (end) {
if (!strict)
route += delimiter + "?";
route += !options.endsWith ? "$" : "(?=" + endsWith + ")";
} else {
var endToken = tokens[tokens.length - 1];
var isEndDelimited = typeof endToken === "string" ? delimiter.indexOf(endToken[endToken.length - 1]) > -1 : endToken === void 0;
if (!strict) {
route += "(?:" + delimiter + "(?=" + endsWith + "))?";
}
if (!isEndDelimited) {
route += "(?=" + delimiter + "|" + endsWith + ")";
}
}
return new RegExp(route, flags(options));
}
function pathToRegexp(path, keys, options) {
if (path instanceof RegExp)
return regexpToRegexp(path, keys);
if (Array.isArray(path))
return arrayToRegexp(path, keys, options);
return stringToRegexp(path, keys, options);
}
// ../../node_modules/wrangler/pages/functions/template-plugin.ts
function* executeRequest(request, relativePathname) {
for (const route of [...routes].reverse()) {
if (route.method && route.method !== request.method) {
continue;
}
const routeMatcher = match(route.routePath, {
end: false
});
const mountMatcher = match(route.mountPath, {
end: false
});
const matchResult = routeMatcher(relativePathname);
const mountMatchResult = mountMatcher(relativePathname);
if (matchResult && mountMatchResult) {
for (const handler of route.middlewares.flat()) {
yield {
handler,
params: matchResult.params,
path: mountMatchResult.path
};
}
}
}
for (const route of routes) {
if (route.method && route.method !== request.method) {
continue;
}
const routeMatcher = match(route.routePath, {
end: true
});
const mountMatcher = match(route.mountPath, {
end: false
});
const matchResult = routeMatcher(relativePathname);
const mountMatchResult = mountMatcher(relativePathname);
if (matchResult && mountMatchResult && route.modules.length) {
for (const handler of route.modules.flat()) {
yield {
handler,
params: matchResult.params,
path: matchResult.path
};
}
break;
}
}
}
function template_plugin_default(pluginArgs) {
const onRequest = async (workerContext) => {
let {
request
} = workerContext;
const {
env,
next,
data
} = workerContext;
const url = new URL(request.url);
const relativePathname = `/${url.pathname.split(workerContext.functionPath)[1] || ""}`.replace(/^\/\//, "/");
const handlerIterator = executeRequest(request, relativePathname);
const pluginNext = async (input, init) => {
if (input !== void 0) {
request = new Request(input, init);
}
const result = handlerIterator.next();
if (result.done === false) {
const {
handler,
params,
path
} = result.value;
const context = {
request,
functionPath: workerContext.functionPath + path,
next: pluginNext,
params,
data,
pluginArgs,
env,
waitUntil: workerContext.waitUntil.bind(workerContext)
};
const response = await handler(context);
return response
} else {
return next();
}
};
return pluginNext();
};
return onRequest;
}
export {
template_plugin_default as
default
};