|
1 | | -import type { RateLimit } from '#imports'; // Assuming RateLimit type is made available via #imports |
2 | | -import { useRuntimeConfig } from '#imports'; |
| 1 | +import type { RateLimit } from '#imports' // Assuming RateLimit type is made available via #imports |
| 2 | +import { useRuntimeConfig } from '#imports' |
3 | 3 |
|
4 | 4 | export default defineTask({ |
5 | 5 | meta: { |
6 | 6 | name: 'shield:cleanIpData', |
7 | 7 | description: 'Clean old IP tracking data from nuxt-api-shield storage.', |
8 | 8 | }, |
9 | 9 | async run() { |
10 | | - const shieldStorage = useStorage('shield'); |
11 | | - const config = useRuntimeConfig().public.nuxtApiShield; |
| 10 | + const shieldStorage = useStorage('shield') |
| 11 | + const config = useRuntimeConfig().public.nuxtApiShield |
12 | 12 |
|
13 | 13 | // ipTTL is expected to be in seconds from config, module applies default if not set by user |
14 | | - const ipTTLseconds = config.ipTTL; |
| 14 | + const ipTTLseconds = config.ipTTL |
15 | 15 |
|
16 | 16 | if (!ipTTLseconds || ipTTLseconds <= 0) { |
17 | | - console.log('[nuxt-api-shield] IP data cleanup (ipTTL) is disabled or invalid.'); |
18 | | - return { result: { cleanedCount: 0, status: 'disabled_or_invalid_ttl' } }; |
| 17 | + console.log('[nuxt-api-shield] IP data cleanup (ipTTL) is disabled or invalid.') |
| 18 | + return { result: { cleanedCount: 0, status: 'disabled_or_invalid_ttl' } } |
19 | 19 | } |
20 | | - const ipTTLms = ipTTLseconds * 1000; |
| 20 | + const ipTTLms = ipTTLseconds * 1000 |
21 | 21 |
|
22 | | - const ipKeys = await shieldStorage.getKeys('ip:'); |
23 | | - const currentTime = Date.now(); |
24 | | - let cleanedCount = 0; |
| 22 | + const ipKeys = await shieldStorage.getKeys('ip:') |
| 23 | + const currentTime = Date.now() |
| 24 | + let cleanedCount = 0 |
25 | 25 |
|
26 | 26 | for (const key of ipKeys) { |
27 | | - const entry = await shieldStorage.getItem(key) as RateLimit | null; |
| 27 | + const entry = await shieldStorage.getItem(key) as RateLimit | null |
28 | 28 |
|
29 | 29 | if (entry && typeof entry.time === 'number') { |
30 | 30 | if ((currentTime - entry.time) > ipTTLms) { |
31 | | - await shieldStorage.removeItem(key); |
32 | | - cleanedCount++; |
| 31 | + await shieldStorage.removeItem(key) |
| 32 | + cleanedCount++ |
33 | 33 | } |
34 | | - } else { |
| 34 | + } |
| 35 | + else { |
35 | 36 | // Clean up potentially malformed (not RateLimit object), null, or missing 'time' property entries |
36 | | - await shieldStorage.removeItem(key); |
37 | | - cleanedCount++; |
| 37 | + await shieldStorage.removeItem(key) |
| 38 | + cleanedCount++ |
38 | 39 | } |
39 | 40 | } |
40 | 41 |
|
41 | | - console.log(`[nuxt-api-shield] Cleaned ${cleanedCount} old/malformed IP data entries.`); |
42 | | - return { result: { cleanedCount } }; |
| 42 | + console.log(`[nuxt-api-shield] Cleaned ${cleanedCount} old/malformed IP data entries.`) |
| 43 | + return { result: { cleanedCount } } |
43 | 44 | }, |
44 | | -}); |
| 45 | +}) |
0 commit comments