-
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.
- Loading branch information
Showing
14 changed files
with
395 additions
and
108 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
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,30 @@ | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import { before, after, beforeEach, describe, it } from 'node:test' | ||
import assert from 'node:assert' | ||
import { buildAiWarpApp } from '../utils/stackable'; | ||
import { AiWarpConfig } from '../../config'; | ||
|
||
const aiProvider: AiWarpConfig['aiProvider'] = { | ||
openai: { | ||
model: 'gpt-3.5-turbo', | ||
apiKey: '' | ||
} | ||
} | ||
|
||
it('returns 401 for unauthorized user', async () => { | ||
const [app, port] = await buildAiWarpApp({ | ||
aiProvider, | ||
auth: { | ||
required: true | ||
} | ||
}) | ||
|
||
try { | ||
await app.start() | ||
|
||
const response = await fetch(`http://localhost:${port}`); | ||
assert.strictEqual(response.status, 401) | ||
} finally { | ||
await app.close() | ||
} | ||
}) |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
import './api.test' | ||
import './rate-limiting.test' | ||
import './auth.test' |
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,121 @@ | ||
/* eslint-disable @typescript-eslint/no-floating-promises */ | ||
import { it } from 'node:test' | ||
import assert from 'node:assert' | ||
import fastifyPlugin from 'fastify-plugin' | ||
import { AiWarpConfig } from '../../config' | ||
import { buildAiWarpApp } from '../utils/stackable' | ||
|
||
const aiProvider: AiWarpConfig['aiProvider'] = { | ||
openai: { | ||
model: 'gpt-3.5-turbo', | ||
apiKey: '' | ||
} | ||
} | ||
|
||
it('calls ai.rateLimiting.max callback', async () => { | ||
const [app, port] = await buildAiWarpApp({ aiProvider }) | ||
|
||
try { | ||
const expectedMax = 100 | ||
let callbackCalled = false | ||
await app.register(fastifyPlugin(async () => { | ||
app.ai.rateLimiting.max = () => { | ||
console.log('user land rate limit max callback') | ||
callbackCalled = true | ||
return expectedMax | ||
} | ||
})) | ||
|
||
await app.start() | ||
|
||
const res = await fetch(`http://localhost:${port}`) | ||
assert.strictEqual(callbackCalled, true) | ||
assert.strictEqual(res.headers.get('x-ratelimit-limit'), expectedMax) | ||
} finally { | ||
await app.close() | ||
} | ||
}) | ||
|
||
// it('calls ai.rateLimiting.allowList callback', async () => { | ||
// const [app, port] = await buildAiWarpApp({ aiProvider }) | ||
|
||
// let callbackCalled = false | ||
// app.register(fastifyPlugin(async () => { | ||
// app.ai.rateLimiting.allowList = () => { | ||
// callbackCalled = true | ||
// return true | ||
// } | ||
// })) | ||
|
||
// await app.start() | ||
|
||
// await fetch(`http://localhost:${port}`) | ||
// assert.strictEqual(callbackCalled, true) | ||
|
||
// await app.close() | ||
// }) | ||
|
||
// it('calls ai.rateLimiting.onBanReach callback', async () => { | ||
// const [app, port] = await buildAiWarpApp({ aiProvider }) | ||
|
||
// let onBanReachCalled = false | ||
// let errorResponseBuilderCalled = false | ||
// app.register(fastifyPlugin(async () => { | ||
// app.ai.rateLimiting.max = () => 0 | ||
|
||
// app.ai.rateLimiting.onBanReach = () => { | ||
// onBanReachCalled = true | ||
// } | ||
|
||
// app.ai.rateLimiting.errorResponseBuilder = () => { | ||
// errorResponseBuilderCalled = true | ||
// return { error: 'rate limited' } | ||
// } | ||
// })) | ||
|
||
// await app.start() | ||
|
||
// await fetch(`http://localhost:${port}`) | ||
// assert.strictEqual(onBanReachCalled, true) | ||
// assert.strictEqual(errorResponseBuilderCalled, true) | ||
|
||
// await app.close() | ||
// }) | ||
|
||
// it('calls ai.rateLimiting.keyGenerator callback', async () => { | ||
// const [app, port] = await buildAiWarpApp({ aiProvider }) | ||
|
||
// let callbackCalled = false | ||
// app.register(fastifyPlugin(async () => { | ||
// app.ai.rateLimiting.keyGenerator = (req) => { | ||
// callbackCalled = true | ||
// return req.ip | ||
// } | ||
// })) | ||
|
||
// await app.start() | ||
|
||
// await fetch(`http://localhost:${port}`) | ||
// assert.strictEqual(callbackCalled, true) | ||
|
||
// await app.close() | ||
// }) | ||
|
||
// // it('calls ai.rateLimiting.errorResponseBuilder callback', async () => { | ||
// // const [app, port] = await buildAiWarpApp({ aiProvider }) | ||
|
||
// // let callbackCalled = false | ||
// // app.register(fastifyPlugin(async () => { | ||
// // app.ai.rateLimiting.errorResponseBuilder = () => { | ||
// // callbackCalled = true | ||
// // return { error: 'rate limited' } | ||
// // } | ||
// // })) | ||
|
||
// // await app.start() | ||
|
||
// // await fetch(`http://localhost:${port}`) | ||
// // assert.strictEqual(callbackCalled, true) | ||
|
||
// // await app.close() | ||
// // }) |
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,37 @@ | ||
import { ReadableStream } from 'node:stream/web' | ||
import { FastifyInstance, FastifyRequest } from 'fastify' | ||
import { expectAssignable } from 'tsd' | ||
import '../../index.d' | ||
import { errorResponseBuilderContext } from '@fastify/rate-limit' | ||
|
||
expectAssignable<FastifyInstance['ai']['warp']>(async (_: FastifyRequest, _2: string) => '') | ||
|
||
expectAssignable<FastifyInstance['ai']['warpStream']>(async (_: FastifyRequest, _2: string) => new ReadableStream()) | ||
|
||
expectAssignable<FastifyInstance['ai']['preResponseCallback']>((_: FastifyRequest, _2: string) => '') | ||
expectAssignable<FastifyInstance['ai']['preResponseCallback']>(async (_: FastifyRequest, _2: string) => '') | ||
|
||
expectAssignable<FastifyInstance['ai']['preResponseChunkCallback']>((_: FastifyRequest, _2: string) => '') | ||
expectAssignable<FastifyInstance['ai']['preResponseChunkCallback']>(async (_: FastifyRequest, _2: string) => '') | ||
|
||
expectAssignable<FastifyInstance['ai']['rateLimiting']['max']>((_: FastifyRequest, _2: string) => 0) | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['max']>(async (_: FastifyRequest, _2: string) => 0) | ||
|
||
expectAssignable<FastifyInstance['ai']['rateLimiting']['allowList']>((_: FastifyRequest, _2: string) => true) | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['allowList']>(async (_: FastifyRequest, _2: string) => true) | ||
|
||
expectAssignable<FastifyInstance['ai']['rateLimiting']['onBanReach']>((_: FastifyRequest, _2: string) => {}) | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['onBanReach']>(async (_: FastifyRequest, _2: string) => {}) | ||
|
||
expectAssignable<FastifyInstance['ai']['rateLimiting']['keyGenerator']>((_: FastifyRequest) => '') | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['keyGenerator']>((_: FastifyRequest) => 0) | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['keyGenerator']>(async (_: FastifyRequest) => '') | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['keyGenerator']>(async (_: FastifyRequest) => 0) | ||
|
||
expectAssignable<FastifyInstance['ai']['rateLimiting']['errorResponseBuilder']>((_: FastifyRequest, _2: errorResponseBuilderContext) => { return {} }) | ||
|
||
expectAssignable<FastifyInstance['ai']['rateLimiting']['onExceeding']>((_: FastifyRequest, _2: string) => {}) | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['onExceeding']>(async (_: FastifyRequest, _2: string) => {}) | ||
|
||
expectAssignable<FastifyInstance['ai']['rateLimiting']['onExceeded']>((_: FastifyRequest, _2: string) => {}) | ||
expectAssignable<FastifyInstance['ai']['rateLimiting']['onExceeded']>(async (_: FastifyRequest, _2: string) => {}) |
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
Oops, something went wrong.