@@ -2,31 +2,36 @@ import {
2
2
addVirtualImports ,
3
3
createResolver ,
4
4
defineIntegration ,
5
- } from "astro-integration-kit" ;
6
- import { envField } from "astro/config" ;
7
- import { AstroError } from "astro/errors" ;
8
- import { loadEnv } from "vite" ;
9
- import { name } from "../package.json" ;
10
- import { AstroTurnstileOptionsSchema as optionsSchema } from "./schema.ts" ;
11
- import { ErrorMessages , envDefaults , loggerStrings } from "./strings.ts" ;
12
- import Dts from "./stubs.ts" ;
5
+ } from 'astro-integration-kit' ;
6
+ import { envField } from 'astro/config' ;
7
+ import { AstroError } from 'astro/errors' ;
8
+ import { loadEnv } from 'vite' ;
9
+ import { name } from '../package.json' ;
10
+ import { AstroTurnstileOptionsSchema as optionsSchema } from './schema.ts' ;
11
+ import {
12
+ ErrorMessages ,
13
+ envDefaults ,
14
+ loggerStrings ,
15
+ svgIcons ,
16
+ } from './strings.ts' ;
17
+ import Dts from './stubs.ts' ;
13
18
14
19
// Load the Turnstile environment variables for the Server runtime and Verification
15
20
// that the environment variables are NOT set to Turnstile demo values during build.
16
- const env = loadEnv ( " TURNSTILE_" , process . cwd ( ) ) ;
21
+ const env = loadEnv ( ' TURNSTILE_' , process . cwd ( ) ) ;
17
22
18
23
export const astroTurnstile = defineIntegration ( {
19
24
name,
20
25
optionsSchema,
21
26
setup ( {
22
27
name,
23
28
options,
24
- options : { endpointPath, verbose, disableClientScript, disableDevToolbar } ,
29
+ options : { endpointPath, verbose, disableClientScript, disableDevToolbar} ,
25
30
} ) {
26
- const { resolve } = createResolver ( import . meta. url ) ;
31
+ const { resolve} = createResolver ( import . meta. url ) ;
27
32
return {
28
33
hooks : {
29
- " astro:config:setup" : ( params ) => {
34
+ ' astro:config:setup' : ( params ) => {
30
35
// Destructure the params object
31
36
const {
32
37
logger,
@@ -45,30 +50,25 @@ export const astroTurnstile = defineIntegration({
45
50
// environment variables and Set the 'checkOrigin' security option to true
46
51
verbose && logger . info ( loggerStrings . updateConfig ) ;
47
52
updateConfig ( {
48
- security : {
49
- checkOrigin : true ,
50
- } ,
51
- experimental : {
52
- env : {
53
- validateSecrets : true ,
54
- schema : {
55
- TURNSTILE_SECRET_KEY : envField . string ( {
56
- access : "secret" ,
57
- context : "server" ,
58
- optional : false ,
59
- // The default value is only usable in 'dev'/'preview' and should be replaced with the actual secret key.
60
- // See https://developers.cloudflare.com/turnstile/troubleshooting/testing/#dummy-sitekeys-and-secret-keys for more information.
61
- default : envDefaults . secretKey ( command ) ,
62
- } ) ,
63
- TURNSTILE_SITE_KEY : envField . string ( {
64
- access : "public" ,
65
- context : "client" ,
66
- optional : false ,
67
- // The default value is only usable in 'dev'/'preview' and should be replaced with the actual secret key.
68
- // See https://developers.cloudflare.com/turnstile/troubleshooting/testing/#dummy-sitekeys-and-secret-keys for more information.
69
- default : envDefaults . siteKey ( command ) ,
70
- } ) ,
71
- } ,
53
+ env : {
54
+ validateSecrets : true ,
55
+ schema : {
56
+ TURNSTILE_SECRET_KEY : envField . string ( {
57
+ access : 'secret' ,
58
+ context : 'server' ,
59
+ optional : false ,
60
+ // The default value is only usable in 'dev'/'preview' and should be replaced with the actual secret key.
61
+ // See https://developers.cloudflare.com/turnstile/troubleshooting/testing/#dummy-sitekeys-and-secret-keys for more information.
62
+ default : envDefaults . secretKey ( command ) ,
63
+ } ) ,
64
+ TURNSTILE_SITE_KEY : envField . string ( {
65
+ access : 'public' ,
66
+ context : 'client' ,
67
+ optional : false ,
68
+ // The default value is only usable in 'dev'/'preview' and should be replaced with the actual secret key.
69
+ // See https://developers.cloudflare.com/turnstile/troubleshooting/testing/#dummy-sitekeys-and-secret-keys for more information.
70
+ default : envDefaults . siteKey ( command ) ,
71
+ } ) ,
72
72
} ,
73
73
} ,
74
74
} ) ;
@@ -85,15 +85,15 @@ export const astroTurnstile = defineIntegration({
85
85
}
86
86
87
87
// If environment variables are set to Turnstile demo values during build, error
88
- if ( command === " build" ) {
88
+ if ( command === ' build' ) {
89
89
// Check TURNSTILE_SECRET_KEY
90
90
if ( env . TURNSTILE_SECRET_KEY ) {
91
91
checkKeys ( {
92
92
key : env . TURNSTILE_SECRET_KEY ,
93
93
knownKeys : [
94
- " 1x0000000000000000000000000000000AA" ,
95
- " 2x0000000000000000000000000000000AA" ,
96
- " 3x0000000000000000000000000000000AA" ,
94
+ ' 1x0000000000000000000000000000000AA' ,
95
+ ' 2x0000000000000000000000000000000AA' ,
96
+ ' 3x0000000000000000000000000000000AA' ,
97
97
] ,
98
98
error : ErrorMessages . demoSecret ,
99
99
} ) ;
@@ -104,11 +104,11 @@ export const astroTurnstile = defineIntegration({
104
104
checkKeys ( {
105
105
key : env . TURNSTILE_SITE_KEY ,
106
106
knownKeys : [
107
- " 1x00000000000000000000AA" ,
108
- " 1x00000000000000000000BB" ,
109
- " 2x00000000000000000000AB" ,
110
- " 2x00000000000000000000BB" ,
111
- " 3x00000000000000000000FF" ,
107
+ ' 1x00000000000000000000AA' ,
108
+ ' 1x00000000000000000000BB' ,
109
+ ' 2x00000000000000000000AB' ,
110
+ ' 2x00000000000000000000BB' ,
111
+ ' 3x00000000000000000000FF' ,
112
112
] ,
113
113
error : ErrorMessages . demoSiteKey ,
114
114
} ) ;
@@ -123,13 +123,24 @@ export const astroTurnstile = defineIntegration({
123
123
// Inject the required Turnstile client-side script if not disabled
124
124
if ( ! disableClientScript ) {
125
125
verbose && logger . info ( loggerStrings . injectScript ) ;
126
- injectScript ( " page" , `import '${ name } /client'` ) ;
126
+ injectScript ( ' page' , `import '${ name } /client'` ) ;
127
127
}
128
128
129
+ /*
130
+
131
+ name: 'Astro Turnstile',
132
+ id: 'astro-turnstile-dev-toolbar',
133
+ icon: svgIcons.turnstile,
134
+ */
129
135
// Add Development Toolbar App for Astro Turnstile testing
130
136
if ( ! disableDevToolbar ) {
131
137
verbose && logger . info ( loggerStrings . addDevToolbarApp ) ;
132
- addDevToolbarApp ( resolve ( "toolbar.ts" ) ) ;
138
+ addDevToolbarApp ( {
139
+ name : 'Astro Turnstile' ,
140
+ id : 'astro-turnstile-dev-toolbar' ,
141
+ icon : svgIcons . turnstile ,
142
+ entrypoint : resolve ( './toolbar.ts' ) ,
143
+ } ) ;
133
144
}
134
145
135
146
// Inject the required Turnstile server-side route
@@ -145,60 +156,60 @@ export const astroTurnstile = defineIntegration({
145
156
addVirtualImports ( params , {
146
157
name,
147
158
imports : {
148
- " virtual:astro-turnstile/config" : `export default ${ JSON . stringify (
149
- options ,
159
+ ' virtual:astro-turnstile/config' : `export default ${ JSON . stringify (
160
+ options
150
161
) } `,
151
- " astro-turnstile:components/TurnstileWidget" : `import Widget from '${ name } /components/TurnstileWidget'; export default Widget;` ,
152
- " astro-turnstile:components/TurnstileForm" : `import Form from '${ name } /components/TurnstileForm'; export default Form;` ,
162
+ ' astro-turnstile:components/TurnstileWidget' : `import Widget from '${ name } /components/TurnstileWidget'; export default Widget;` ,
163
+ ' astro-turnstile:components/TurnstileForm' : `import Form from '${ name } /components/TurnstileForm'; export default Form;` ,
153
164
} ,
154
165
} ) ;
155
166
156
167
// Log completion message
157
168
verbose && logger . info ( loggerStrings . setupComplete ) ;
158
169
} ,
159
- " astro:config:done" : ( { injectTypes, logger } ) => {
170
+ ' astro:config:done' : ( { injectTypes, logger} ) => {
160
171
// Inject the required Turnstile types for the Astro config and components
161
172
verbose && logger . info ( loggerStrings . injectTypes ) ;
162
173
injectTypes ( Dts . config ) ;
163
174
injectTypes ( Dts . components ) ;
164
175
} ,
165
- " astro:server:setup" : async ( { toolbar, logger } ) => {
176
+ ' astro:server:setup' : async ( { toolbar, logger} ) => {
166
177
// Add a Server Event Listener for the 'sendverify' event from the Astro Dev Toolbar app
167
178
if ( ! disableDevToolbar ) {
168
179
verbose &&
169
- logger . info ( " Adding Server Event Listener Dev Toolbar App..." ) ;
170
- toolbar . on ( " sendverify" , async ( data : { key : string } ) => {
180
+ logger . info ( ' Adding Server Event Listener Dev Toolbar App...' ) ;
181
+ toolbar . on ( ' sendverify' , async ( data : { key : string } ) => {
171
182
const formData = new FormData ( ) ;
172
183
173
184
// Get the Turnstile site token from the environment variables
174
185
// or use a dummy token for testing
175
186
const siteToken =
176
- env . TURNSTILE_SITE_KEY || " 1x00000000000000000000AA" ;
187
+ env . TURNSTILE_SITE_KEY || ' 1x00000000000000000000AA' ;
177
188
178
189
// Add the secret key and token to the form data
179
- formData . append ( " secret" , data . key ) ;
180
- formData . append ( " response" , siteToken ) ;
190
+ formData . append ( ' secret' , data . key ) ;
191
+ formData . append ( ' response' , siteToken ) ;
181
192
182
193
// Send the token to the Turnstile API for verification
183
194
try {
184
195
const response = await fetch (
185
- " https://challenges.cloudflare.com/turnstile/v0/siteverify" ,
196
+ ' https://challenges.cloudflare.com/turnstile/v0/siteverify' ,
186
197
{
187
- method : " POST" ,
198
+ method : ' POST' ,
188
199
body : formData ,
189
200
headers : {
190
- " content-type" : " application/x-www-form-urlencoded" ,
201
+ ' content-type' : ' application/x-www-form-urlencoded' ,
191
202
} ,
192
- } ,
203
+ }
193
204
) ;
194
205
195
206
// Send the verification response back to the Astro Dev Toolbar app
196
- toolbar . send ( " verifyresponse" , {
207
+ toolbar . send ( ' verifyresponse' , {
197
208
success : response . ok ,
198
209
} ) ;
199
210
} catch ( error ) {
200
211
// Send the verification response back to the Astro Dev Toolbar app
201
- toolbar . send ( " verifyresponse" , { success : false } ) ;
212
+ toolbar . send ( ' verifyresponse' , { success : false } ) ;
202
213
}
203
214
} ) ;
204
215
}
0 commit comments