From 110edeb8dc30f7326fe38ba99b97ec4d5433d647 Mon Sep 17 00:00:00 2001 From: Maxwell Date: Sat, 14 Oct 2023 17:18:49 +0200 Subject: [PATCH] refactor: remove upstash and ratelimit --- .github/workflows/deno-verify.yml | 18 +++--- supabase/functions/_shared/core/factory.ts | 2 - .../functions/_shared/middleware/limit.ts | 63 ------------------- supabase/functions/import_map.json | 4 +- 4 files changed, 12 insertions(+), 75 deletions(-) delete mode 100644 supabase/functions/_shared/middleware/limit.ts diff --git a/.github/workflows/deno-verify.yml b/.github/workflows/deno-verify.yml index bf2311e..cf4fb02 100644 --- a/.github/workflows/deno-verify.yml +++ b/.github/workflows/deno-verify.yml @@ -4,8 +4,14 @@ on: pull_request: types: [opened, synchronize] +permissions: + contents: read + jobs: format: + permissions: + contents: write + pull-requests: write runs-on: ubuntu-latest steps: - name: Checkout code @@ -26,11 +32,9 @@ jobs: - name: create pull request with formatting corrections if: ${{contains(steps.git_status.outputs.status, ' ')}} - uses: peter-evans/create-pull-request@v5 + uses: stefanzweifel/git-auto-commit-action@v4 with: - branch: "${{ github.base_ref }}/auto-formatting" - title: "${{ github.base_ref }}/auto-formatting" - body: "Automated PR contains format corrections generated by a Github action for `${{ github.base_ref }}`" - base: ${{ github.base_ref }} - head: ${{ github.head_ref }} - signoff: false + commit_message: "refactor: auto format" + commit_options: '--no-verify --signoff' + file_pattern: '*.ts' + repository: . diff --git a/supabase/functions/_shared/core/factory.ts b/supabase/functions/_shared/core/factory.ts index a8a7786..35fe4e8 100644 --- a/supabase/functions/_shared/core/factory.ts +++ b/supabase/functions/_shared/core/factory.ts @@ -1,7 +1,6 @@ import { Application, Router } from 'x/oak'; import { FactoryOptions } from '../types/options.d.ts'; import state from '../core/setup.ts'; -import limit from '../middleware/limit.ts'; import timing from '../middleware/timing.ts'; import error from '../middleware/error.ts'; import header from '../middleware/header.ts'; @@ -22,7 +21,6 @@ export default (opts: FactoryOptions): Application => { app.use( timing, header, - limit, growth, attribute, error, diff --git a/supabase/functions/_shared/middleware/limit.ts b/supabase/functions/_shared/middleware/limit.ts deleted file mode 100644 index d6142c3..0000000 --- a/supabase/functions/_shared/middleware/limit.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { Ratelimit } from 'skypack/upstash/ratelimit'; -import { Redis } from 'x/upstash_redis'; -import type { AppContext } from '../types/core.d.ts'; -import { Response, Status } from 'x/oak'; -import { logger } from '../core/logger.ts'; -import { generateUUID } from '../core/utils.ts'; -import { between } from 'x/optic/profiler'; -import { env } from '../core/env.ts'; - -const ephemeralCache = new Map(); - -const requests = env('RATE_LIMIT_REQUESTS'); -const window = env('RATE_LIMIT_WINDOW'); - -const ratelimit = new Ratelimit({ - redis: Redis.fromEnv(), - ephemeralCache: ephemeralCache, - limiter: Ratelimit.slidingWindow( - requests, - `${window} s`, - ), - analytics: true, -}); - -const applyHeaders = ( - response: Response, - limit: number, - remaining: number, - reset: number, -) => { - response.headers.append('RateLimit-Policy', `${requests};w=${window}`); - response.headers.append('X-RateLimit-Limit', limit.toString()); - response.headers.append('X-RateLimit-Remaining', remaining.toString()); - response.headers.append('X-RateLimit-Reset', reset.toString()); -}; - -export default async ( - { state, request, response }: AppContext, - next: () => Promise, -) => { - logger.mark('limiter_start'); - const { envrionment } = state; - const identifier = await generateUUID( - envrionment.namespace, - request.ip, - ); - const { success, limit, remaining, reset } = await ratelimit.limit( - identifier, - ); - applyHeaders(response, limit, remaining, reset); - logger.mark('limiter_end'); - logger.measure(between('limiter_start', 'limiter_end')); - if (!success) { - response.status = Status.TooManyRequests; - response.type = 'application/json'; - response.body = { - message: 'Too many request', - }; - logger.warn(`Rate limit reached, blocking request`); - return; - } - await next(); -}; diff --git a/supabase/functions/import_map.json b/supabase/functions/import_map.json index 25b3373..ff44f81 100644 --- a/supabase/functions/import_map.json +++ b/supabase/functions/import_map.json @@ -17,11 +17,9 @@ "x/mongo": "https://deno.land/x/mongo@v0.32.0/mod.ts", "x/oak": "https://deno.land/x/oak@v12.6.1/mod.ts", "x/xml": "https://deno.land/x/xml@2.1.2/mod.ts", - "x/upstash_redis": "https://deno.land/x/upstash_redis@v1.23.3/mod.ts", "x/deepmerge": "https://deno.land/x/deepmergets@v5.1.0/dist/deno/index.ts", "esm/ua-agent-parser": "https://esm.sh/ua-parser-js@2.0.0-beta.1/", "esm/supabase": "https://esm.sh/@supabase/supabase-js@2.38.0", - "esm/growthbook": "https://esm.sh/@growthbook/growthbook@0.29.0", - "skypack/upstash/ratelimit": "https://cdn.skypack.dev/@upstash/ratelimit@latest" + "esm/growthbook": "https://esm.sh/@growthbook/growthbook@0.29.0" } }