generated from florian-lefebvre/astro-integration-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from hkbertoson/script-changes
Script changes
- Loading branch information
Showing
10 changed files
with
121 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro-turnstile": minor | ||
--- | ||
|
||
Updated script to only inject on pages where component is rendered |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import type { MiddlewareHandler } from "astro"; | ||
|
||
const COMPONENT_IDENTIFIER = 'data-turnstile-container'; | ||
const TURNSTILE_SCRIPT = ` | ||
<script | ||
src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback" | ||
async | ||
defer | ||
id="turnstile-script"> | ||
</script>`; | ||
|
||
const MAX_SCAN_BYTES = 16384; // 16KB | ||
let bytesScanned = 0; | ||
|
||
export const onRequest: MiddlewareHandler = async (_, next) => { | ||
const response = await next(); | ||
|
||
const contentType = response.headers.get('Content-Type'); | ||
if (!contentType?.includes('text/html') || !response.body) { | ||
return response; | ||
} | ||
|
||
const transformedBody = response.body | ||
.pipeThrough(new TextDecoderStream()) | ||
.pipeThrough(createFastScriptInjectionTransform()) | ||
.pipeThrough(new TextEncoderStream()); | ||
|
||
return new Response(transformedBody, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: response.headers | ||
}); | ||
}; | ||
|
||
function createFastScriptInjectionTransform(): TransformStream<string, string> { | ||
let hasInjected = false; | ||
let hasFoundComponent = false; | ||
let buffer = ''; | ||
|
||
return new TransformStream({ | ||
transform(chunk: string, controller) { | ||
// Fast path: already injected or scanned too much | ||
if (hasInjected || bytesScanned > MAX_SCAN_BYTES) { | ||
controller.enqueue(chunk); | ||
return; | ||
} | ||
|
||
bytesScanned += chunk.length; | ||
|
||
// Fast path: haven't found component yet | ||
if (!hasFoundComponent) { | ||
// Look for the data attribute | ||
if (chunk.includes(COMPONENT_IDENTIFIER)) { | ||
hasFoundComponent = true; | ||
buffer = chunk; | ||
} else { | ||
controller.enqueue(chunk); | ||
return; | ||
} | ||
} else { | ||
buffer += chunk; | ||
} | ||
|
||
const headCloseIndex = buffer.indexOf('</head>'); | ||
if (headCloseIndex === -1) { | ||
if (buffer.length > MAX_SCAN_BYTES) { | ||
controller.enqueue(buffer); | ||
buffer = ''; | ||
hasInjected = true; | ||
} | ||
return; | ||
} | ||
|
||
const injectedContent = | ||
buffer.slice(0, headCloseIndex) + | ||
TURNSTILE_SCRIPT + | ||
buffer.slice(headCloseIndex); | ||
|
||
controller.enqueue(injectedContent); | ||
hasInjected = true; | ||
buffer = ''; | ||
}, | ||
|
||
flush(controller) { | ||
if (buffer) { | ||
controller.enqueue(buffer); | ||
} | ||
bytesScanned = 0; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters