Skip to content

Commit 815f4d9

Browse files
committed
fix linter errors
1 parent 80f061c commit 815f4d9

File tree

5 files changed

+40
-53
lines changed

5 files changed

+40
-53
lines changed

playground/nuxt.config.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
export default defineNuxtConfig({
22
modules: ['../src/module'],
3-
nuxtApiShield: {
4-
limit: {
5-
max: 12,
6-
duration: 10,
7-
ban: 30,
8-
},
9-
delayOnBan: true,
10-
retryAfterHeader: true,
11-
log: {
12-
path: 'logs',
13-
attempts: 5,
14-
},
15-
},
3+
devtools: { enabled: true },
164
nitro: {
175
storage: {
186
shield: {
@@ -28,5 +16,17 @@ export default defineNuxtConfig({
2816
'*/5 * * * *': ['shield:clean'],
2917
},
3018
},
31-
devtools: { enabled: true },
19+
nuxtApiShield: {
20+
limit: {
21+
max: 12,
22+
duration: 10,
23+
ban: 30,
24+
},
25+
delayOnBan: true,
26+
retryAfterHeader: true,
27+
log: {
28+
path: 'logs',
29+
attempts: 5,
30+
},
31+
},
3232
})

src/runtime/server/middleware/shield.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { RateLimit } from '../types/RateLimit'
2-
import { isBanExpired } from '../utils/isBanExpired'
32
import shieldLog from '../utils/shieldLog'
43
import {
54
createError,
@@ -29,7 +28,7 @@ export default defineEventHandler(async (event) => {
2928
const banKey = `ban:${requestIP}`
3029
const bannedUntilRaw = await shieldStorage.getItem(banKey)
3130
const bannedUntil = typeof bannedUntilRaw === 'number' ? bannedUntilRaw : Number(bannedUntilRaw)
32-
if (bannedUntilRaw && !isNaN(bannedUntil) && Date.now() < bannedUntil) {
31+
if (bannedUntilRaw && !Number.isNaN(bannedUntil) && Date.now() < bannedUntil) {
3332
if (config.retryAfterHeader) {
3433
const retryAfter = Math.ceil((bannedUntil - Date.now()) / 1000)
3534
event.node.res.setHeader('Retry-After', retryAfter)
@@ -38,7 +37,8 @@ export default defineEventHandler(async (event) => {
3837
statusCode: 429,
3938
message: config.errorMessage,
4039
})
41-
} else if (bannedUntilRaw && !isNaN(bannedUntil) && Date.now() >= bannedUntil) {
40+
}
41+
else if (bannedUntilRaw && !Number.isNaN(bannedUntil) && Date.now() >= bannedUntil) {
4242
await shieldStorage.removeItem(banKey)
4343
await shieldStorage.setItem(`ip:${requestIP}`, {
4444
count: 1,
@@ -74,7 +74,6 @@ export default defineEventHandler(async (event) => {
7474
count: 1,
7575
time: Date.now(),
7676
})
77-
7877

7978
if (config.retryAfterHeader) {
8079
event.node.res.setHeader('Retry-After', config.limit.ban)
@@ -97,17 +96,3 @@ const isRateLimited = (req: RateLimit) => {
9796
// console.log(" ", (Date.now() - req.time) / 1000, "<", options.limit.duration);
9897
return (Date.now() - req.time) / 1000 < options.limit.duration
9998
}
100-
101-
const banDelay = async (req: RateLimit) => {
102-
const options = useRuntimeConfig().public.nuxtApiShield
103-
// console.log(" delayOnBan is: " + options.delayOnBan);
104-
if (options.delayOnBan && req.count > options.limit.max) {
105-
// INFO Nuxt Devtools will send a new request if the response is slow,
106-
// so we get the count incremented twice or more times, based on the ban delay time
107-
// console.log(` Applying ban delay for ${(req.count - options.limit.max) * options.limit.ban} sec (${Date.now()})`);
108-
await new Promise(resolve =>
109-
setTimeout(resolve, (req.count - options.limit.max) * options.limit.ban * 1000),
110-
)
111-
// console.log(` Ban delay completed (${Date.now()})`);
112-
}
113-
}

test/basic.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fileURLToPath } from 'node:url'
2-
import { readFile, rm } from 'node:fs/promises'
2+
import { rm } from 'node:fs/promises'
33
import { beforeEach, describe, it, expect } from 'vitest'
44
import { setup, $fetch } from '@nuxt/test-utils/e2e'
55
import type { ApiResponse } from './ApiResponse'
@@ -102,7 +102,8 @@ describe('shield', async () => {
102102
try {
103103
await $fetch('/api/basicexample?c=77/3', { method: 'GET', retryStatusCodes: [] })
104104
throw new Error('Nem dobott hibát a 3. kérésnél!')
105-
} catch (err) {
105+
}
106+
catch (err) {
106107
const typedErr = err as { statusCode: number, statusMessage: string }
107108
expect(typedErr.statusCode).toBe(429)
108109
}
@@ -112,7 +113,8 @@ describe('shield', async () => {
112113
try {
113114
await $fetch('/api/basicexample?c=77/4', { method: 'GET', retryStatusCodes: [] })
114115
throw new Error('Nem dobott hibát a ban idő alatt!')
115-
} catch (err) {
116+
}
117+
catch (err) {
116118
const typedErr = err as { statusCode: number, statusMessage: string }
117119
expect(typedErr.statusCode).toBe(429)
118120
}

test/fixtures/basic/nuxt.config.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import nuxtApiShield from '../../../src/module'
22

33
export default defineNuxtConfig({
44
modules: [nuxtApiShield],
5+
nitro: {
6+
storage: {
7+
shield: {
8+
// driver: "memory",
9+
driver: 'fs',
10+
base: '_testBasicShield',
11+
},
12+
},
13+
},
514
nuxtApiShield: {
615
limit: {
716
max: 2,
@@ -15,13 +24,4 @@ export default defineNuxtConfig({
1524
attempts: 3,
1625
},
1726
},
18-
nitro: {
19-
storage: {
20-
shield: {
21-
// driver: "memory",
22-
driver: 'fs',
23-
base: '_testBasicShield',
24-
},
25-
},
26-
},
2727
})

test/fixtures/withroutes/nuxt.config.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import nuxtApiShield from '../../../src/module'
22

33
export default defineNuxtConfig({
44
modules: [nuxtApiShield],
5+
nitro: {
6+
storage: {
7+
shield: {
8+
// driver: "memory",
9+
driver: 'fs',
10+
base: '_testWithRoutesShield',
11+
},
12+
},
13+
},
514
nuxtApiShield: {
615
limit: {
716
max: 2,
@@ -13,13 +22,4 @@ export default defineNuxtConfig({
1322
log: { path: '', attempts: 0 },
1423
routes: ['/api/v3'],
1524
},
16-
nitro: {
17-
storage: {
18-
shield: {
19-
// driver: "memory",
20-
driver: 'fs',
21-
base: '_testWithRoutesShield',
22-
},
23-
},
24-
},
2525
})

0 commit comments

Comments
 (0)