Skip to content

Commit 0806019

Browse files
author
Andre Bluehs
committed
catch more errors, remove unneeded validation step
1 parent a34a6b6 commit 0806019

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
## Configure and Deploy
2525

2626
1. Edit `wrangler.toml` to add `DOMAIN`, `SUBDOMAIN`, and `ACCOUNTID`.
27-
2. Edit `src/index.ts` (line 45) to send the decoded data anywhere you would like.
27+
2. Edit `src/index.ts` (line 36) to send the decoded data anywhere you would like.
2828
3. Run `npx wrangler secret put MATCHED_PAYLOAD_PRIVATE_KEY`:
2929
- Enter the private key generated when deploying a matched payload.
3030
- Refer to the [documentation](https://developers.cloudflare.com/waf/managed-rules/payload-logging/) for more information on generating the private/public key pair.

src/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@ const router = AutoRouter();
1010

1111
router
1212
.put("/logs/:logdata+", async (req: IRequest, env: Env) => {
13-
// Validation step
14-
if (/\/ownership-challenge-[a-fA-F0-9]{8}.txt/.test(req.params.logdata)) {
15-
return new Response("OK");
16-
}
17-
18-
// Test push
19-
// for some reason the inflate() from pako doesn't like the test.txt.gz file
20-
// so we'll just accept it and move on as there's nothing of value in there anyway
13+
// When configuring logpush the test.txt.gz isn't actually gzipped
14+
// so our DecompressionStream("gzip") below fails, but we can just accept it
2115
if (/\d{8}\/test.txt.gz/.test(req.params.logdata)) {
2216
return new Response("OK");
2317
}
18+
2419
// sanity check
2520
if (!req.body) {
2621
return new Response("No body", { status: 400 });
@@ -48,7 +43,8 @@ router
4843
)
4944
// we need to have a writable stream (even if it's a no-op)
5045
// so that we can await on a promise and complete our work
51-
.pipeTo(new WritableStream());
46+
.pipeTo(new WritableStream())
47+
.catch((err) => console.error(err));
5248

5349
return new Response("OK");
5450
})

src/matched_data.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ export function matchedDataTransformer(
1919
dataProcessor: (data: string | null) => void
2020
) {
2121
return new TransformNewlineStream(async (line: string) => {
22-
const data = await decode(line, privateKey);
23-
// we are pre-emptively await-ing this even though
24-
// in the default example we don't need to in case
25-
// any users in the future need to send the data somewhere
26-
await dataProcessor(data);
22+
try {
23+
const data = await decode(line, privateKey);
24+
// we are pre-emptively await-ing this even though
25+
// in the default example we don't need to in case
26+
// any users in the future need to send the data somewhere
27+
await dataProcessor(data);
28+
} catch (err) {
29+
console.error(err);
30+
}
2731
});
2832
}
2933

src/transform_newline_stream.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ export class TransformNewlineStream extends TransformStream {
1717
const end = flush ? "" : parts.pop() ?? "";
1818

1919
for (const line of [start].concat(parts)) {
20-
const res = await lineProcessor(line);
21-
if (res) {
22-
controller.enqueue(res + (flush ? "\n" : ""));
20+
try {
21+
const res = await lineProcessor(line);
22+
if (res) {
23+
controller.enqueue(res + (flush ? "\n" : ""));
24+
}
25+
} catch (err) {
26+
console.error(err);
2327
}
2428
}
2529

@@ -29,8 +33,9 @@ export class TransformNewlineStream extends TransformStream {
2933
super({
3034
start() {},
3135
async transform(chunk, controller) {
32-
if (!chunk) return;
33-
prevChunkEnd = await processChunk(chunk, prevChunkEnd, controller);
36+
if (chunk) {
37+
prevChunkEnd = await processChunk(chunk, prevChunkEnd, controller);
38+
}
3439
},
3540
async flush(controller) {
3641
if (prevChunkEnd.length > 0) {

0 commit comments

Comments
 (0)