Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hkbertoson committed Dec 11, 2024
1 parent 9a444e1 commit 31d452f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-readers-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-turnstile": minor
---

Updated to use data attribute instead of comment
2 changes: 1 addition & 1 deletion package/lib/components/TurnstileForm.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ const {
const siteUrl = Astro.site;
---

<!-- Component: Turnstile -->
<form
id="at-turnstile-captcha-form"
data-siteurl={siteUrl}
data-captcha={config.endpointPath}
data-turnstile-container
action={action}
method={method}
enctype={enctype}
Expand Down
2 changes: 1 addition & 1 deletion package/lib/components/TurnstileWidget.astro
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ const {
} = getStyleSettings(size);
---

<!-- Component: Turnstile -->
<div
id="at-turnstile-captcha"
data-sitekey={TURNSTILE_SITE_KEY}
data-theme={theme}
data-size={size}
data-turnstile-container
>
</div>

Expand Down
10 changes: 3 additions & 7 deletions package/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MiddlewareHandler } from "astro";

const COMPONENT_IDENTIFIER = '<!-- Component: Turnstile -->';
const COMPONENT_IDENTIFIER = 'data-turnstile-container';
const TURNSTILE_SCRIPT = `
<script
src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback"
Expand All @@ -9,7 +9,6 @@ const TURNSTILE_SCRIPT = `
id="turnstile-script">
</script>`;

// Max bytes to scan for component identifier before giving up
const MAX_SCAN_BYTES = 16384; // 16KB
let bytesScanned = 0;

Expand Down Expand Up @@ -50,6 +49,7 @@ function createFastScriptInjectionTransform(): TransformStream<string, string> {

// Fast path: haven't found component yet
if (!hasFoundComponent) {
// Look for the data attribute
if (chunk.includes(COMPONENT_IDENTIFIER)) {
hasFoundComponent = true;
buffer = chunk;
Expand All @@ -61,19 +61,16 @@ function createFastScriptInjectionTransform(): TransformStream<string, string> {
buffer += chunk;
}

// Look for </head> only if we found the component
const headCloseIndex = buffer.indexOf('</head>');
if (headCloseIndex === -1) {
// If buffer is getting too large, flush it and give up
if (buffer.length > MAX_SCAN_BYTES) {
controller.enqueue(buffer);
buffer = '';
hasInjected = true; // Prevent further processing
hasInjected = true;
}
return;
}

// Inject the script
const injectedContent =
buffer.slice(0, headCloseIndex) +
TURNSTILE_SCRIPT +
Expand All @@ -88,7 +85,6 @@ function createFastScriptInjectionTransform(): TransformStream<string, string> {
if (buffer) {
controller.enqueue(buffer);
}
// Reset static counter for next request
bytesScanned = 0;
}
});
Expand Down

0 comments on commit 31d452f

Please sign in to comment.