@nuasite/components 0.47.3 → 0.47.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@nuasite/components",
3
3
  "description": "Nua Site astro components.",
4
4
  "license": "Apache-2.0",
5
- "version": "0.47.3",
5
+ "version": "0.47.5",
6
6
  "files": [
7
7
  "dist/**",
8
8
  "src/**",
@@ -15,6 +15,7 @@ const {
15
15
  tryAgainMessage = "Try again",
16
16
  fastSubmitMessage = "Please wait a moment before submitting.",
17
17
  submitAnywayLabel = "Send anyway",
18
+ class: className,
18
19
  ...rest
19
20
  } = Astro.props
20
21
 
@@ -41,7 +42,7 @@ const tokenId = `${tokenFieldName}_${formId}`
41
42
  <astro-form>
42
43
  <form
43
44
  action={action}
44
- class="astro-form"
45
+ class:list={['astro-form', className]}
45
46
  data-form-id={formId}
46
47
  data-token-field={tokenFieldName}
47
48
  data-success-message={successMessage}
@@ -127,6 +128,7 @@ const tokenId = `${tokenFieldName}_${formId}`
127
128
  private startTime = Date.now();
128
129
  private honeypotFieldNames: string[] = []
129
130
  private abortController = new AbortController()
131
+ private tokenRefreshInterval: ReturnType<typeof setInterval> | undefined
130
132
 
131
133
  connectedCallback() {
132
134
  const form = this.querySelector('form')
@@ -219,9 +221,7 @@ const tokenId = `${tokenFieldName}_${formId}`
219
221
  let lastHumanityFailure: string | null = null
220
222
  let bypassHumanityCheck = false
221
223
 
222
- const refreshSubmissionGuards = () => {
223
- activationTime = performance.now()
224
-
224
+ const refreshToken = () => {
225
225
  if (tokenInput) {
226
226
  const { value } = createTokenValue()
227
227
  tokenInput.value = value
@@ -229,8 +229,20 @@ const tokenId = `${tokenFieldName}_${formId}`
229
229
  }
230
230
  }
231
231
 
232
+ const refreshSubmissionGuards = () => {
233
+ activationTime = performance.now()
234
+ refreshToken()
235
+ }
236
+
232
237
  refreshSubmissionGuards()
233
238
 
239
+ // The server rejects tokens older than its expiry window — keep the token fresh
240
+ // for long-open tabs and pages restored from the back/forward cache
241
+ this.tokenRefreshInterval = setInterval(refreshToken, 4 * 60 * 1000)
242
+ window.addEventListener('pageshow', (e) => {
243
+ if (e.persisted) refreshToken()
244
+ }, { signal: this.abortController.signal })
245
+
234
246
  const trackForm = (action: string, detail?: Record<string, unknown>) => {
235
247
  if (typeof window !== 'undefined' && (window as any).analytics?.form) {
236
248
  (window as any).analytics.form(formId, action, detail)
@@ -240,7 +252,6 @@ const tokenId = `${tokenFieldName}_${formId}`
240
252
  submitAnywayButton?.addEventListener('click', () => {
241
253
  bypassHumanityCheck = true
242
254
  submitAnywayButton.classList.add('hidden')
243
- activationTime = performance.now()
244
255
  form.requestSubmit()
245
256
  }, { signal: this.abortController.signal })
246
257
 
@@ -255,13 +266,17 @@ const tokenId = `${tokenFieldName}_${formId}`
255
266
 
256
267
  trackForm('attempt', { formData: formDataForTracking })
257
268
 
258
- if (bypassHumanityCheck) {
269
+ const bypassed = bypassHumanityCheck
270
+ if (bypassed) {
259
271
  trackForm('humanity_override', {
260
272
  reason: `accepted_after_humanity_warning:${lastHumanityFailure ?? 'unknown'}`,
261
273
  formData: formDataForTracking,
262
274
  })
263
275
  bypassHumanityCheck = false
264
276
  lastHumanityFailure = null
277
+ // The user explicitly confirmed — drop autofilled honeypot values so the
278
+ // server accepts the submission as fully valid
279
+ this.clearHoneypotFields(form)
265
280
  } else {
266
281
  const humanityFailure = this.checkHumanity(form)
267
282
  if (humanityFailure) {
@@ -273,7 +288,7 @@ const tokenId = `${tokenFieldName}_${formId}`
273
288
  }
274
289
 
275
290
  const elapsed = performance.now() - activationTime
276
- if (minSubmitDelay > 0 && elapsed < minSubmitDelay) {
291
+ if (!bypassed && minSubmitDelay > 0 && elapsed < minSubmitDelay) {
277
292
  trackForm('reject', { reason: 'submitted_too_fast', formData: formDataForTracking })
278
293
  showError(fastSubmitMessage || tryAgainMessage)
279
294
  return
@@ -289,6 +304,14 @@ const tokenId = `${tokenFieldName}_${formId}`
289
304
 
290
305
  showLoading()
291
306
 
307
+ // The server rejects tokens younger than its minimum submit delay — if the
308
+ // token was refreshed moments ago, wait out the remainder before sending
309
+ const tokenIssuedAt = tokenInput ? Number(tokenInput.value.split(':')[1]) : NaN
310
+ const tokenAge = Date.now() - tokenIssuedAt
311
+ if (Number.isFinite(tokenAge) && tokenAge >= 0 && tokenAge < 700) {
312
+ await new Promise(resolve => setTimeout(resolve, 700 - tokenAge))
313
+ }
314
+
292
315
  try {
293
316
  const response = await fetch(form.action, {
294
317
  method: form.method || 'POST',
@@ -353,6 +376,9 @@ const tokenId = `${tokenFieldName}_${formId}`
353
376
 
354
377
  disconnectedCallback() {
355
378
  this.abortController.abort()
379
+ if (this.tokenRefreshInterval !== undefined) {
380
+ clearInterval(this.tokenRefreshInterval)
381
+ }
356
382
  }
357
383
 
358
384
  private getInteractionScore(): number {
@@ -371,8 +397,6 @@ const tokenId = `${tokenFieldName}_${formId}`
371
397
 
372
398
  // WebDriver API presence (set to true in automated browsers)
373
399
  if (navigator.webdriver === true) return 'webdriver'
374
- // Empty plugins list is only suspicious on desktop — mobile browsers normally have none
375
- if (!isMobile && !window.navigator.plugins.length) return 'no_plugins'
376
400
  if (!window.navigator.languages.length) return 'no_languages'
377
401
 
378
402
  if (ua.includes('HeadlessChrome')) return 'headless_chrome'
@@ -387,6 +411,15 @@ const tokenId = `${tokenFieldName}_${formId}`
387
411
  return null
388
412
  }
389
413
 
414
+ private clearHoneypotFields(form: HTMLFormElement) {
415
+ for (const fieldName of this.honeypotFieldNames) {
416
+ const field = form.querySelector(`[name="${fieldName}"]`) as HTMLInputElement | null
417
+ if (field) field.value = ''
418
+ }
419
+ const cssHoneypot = form.querySelector('input[style*="position: absolute; left: -9999px"]') as HTMLInputElement | null
420
+ if (cssHoneypot) cssHoneypot.value = ''
421
+ }
422
+
390
423
  private checkHumanity(form: HTMLFormElement): string | null {
391
424
  const timeSpent = Date.now() - this.startTime;
392
425