Skip to content

Commit

Permalink
Merge branch 'skyra-project:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mzrtamp authored Nov 9, 2024
2 parents 3817480 + 1f01384 commit feaea3d
Show file tree
Hide file tree
Showing 9 changed files with 723 additions and 929 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ jobs:
- name: Checkout Project
uses: actions/checkout@v4
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3.6.1
uses: docker/setup-buildx-action@v3.7.1
- name: Login to GitHub Container Registry
uses: docker/login-action@v3.3.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Haste Server Docker image
uses: docker/build-push-action@v6.7.0
uses: docker/build-push-action@v6.9.0
with:
push: true
context: .
Expand Down
725 changes: 367 additions & 358 deletions .yarn/releases/yarn-4.5.0.cjs → .yarn/releases/yarn-4.5.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ enableGlobalCache: true

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.5.0.cjs
yarnPath: .yarn/releases/yarn-4.5.1.cjs
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Base Stage #
# ================ #

FROM node:20-alpine as base
FROM node:22-alpine AS base

WORKDIR /usr/src/app

RUN apk add --no-cache dumb-init python3 make g++
RUN apk add --no-cache dumb-init g++ make python3

COPY --chown=node:node yarn.lock .
COPY --chown=node:node package.json .
Expand All @@ -20,7 +20,7 @@ ENTRYPOINT ["dumb-init", "--"]
# Builder Stage #
# ================ #

FROM base as builder
FROM base AS builder

ENV NODE_ENV="development"

Expand All @@ -29,8 +29,8 @@ COPY --chown=node:node vite.config.ts .
COPY --chown=node:node tsup.config.ts .
COPY --chown=node:node src/ src/

RUN yarn install --immutable
RUN yarn build
RUN yarn install --immutable \
&& yarn build

# ================ #
# Runner Stage #
Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,41 @@
"format": "prettier --write src"
},
"dependencies": {
"@fastify/rate-limit": "^10.0.1",
"@fastify/rate-limit": "^10.1.1",
"@fastify/sensible": "^6.0.1",
"@fastify/static": "^8.0.1",
"@fastify/swagger": "^9.0.0",
"@fastify/swagger-ui": "^5.0.1",
"@fastify/type-provider-typebox": "^5.0.0",
"@fastify/static": "^8.0.2",
"@fastify/swagger": "^9.2.0",
"@fastify/swagger-ui": "^5.1.0",
"@fastify/type-provider-typebox": "^5.0.1",
"@pnotify/core": "^5.2.0",
"@sapphire/fetch": "^3.0.3",
"@sinclair/typebox": "^0.33.12",
"@sapphire/fetch": "^3.0.5",
"@sinclair/typebox": "^0.33.21",
"@skyra/env-utilities": "^1.3.0",
"fastify": "^5.0.0",
"fastify": "^5.1.0",
"highlight.js": "^11.10.0",
"ioredis": "^5.4.1"
},
"devDependencies": {
"@sapphire/eslint-config": "^5.0.5",
"@sapphire/prettier-config": "^2.0.0",
"@sapphire/ts-config": "^5.0.1",
"@types/node": "^20.16.5",
"@types/node": "^22.9.0",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"autoprefixer": "^10.4.20",
"concurrently": "^9.0.1",
"concurrently": "^9.1.0",
"esbuild-plugin-version-injector": "^1.2.1",
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"tsup": "^8.3.0",
"typescript": "^5.6.2",
"vite": "^5.4.7"
"tsup": "^8.3.5",
"typescript": "^5.6.3",
"vite": "^5.4.10"
},
"eslintConfig": {
"extends": "@sapphire"
},
"packageManager": "yarn@4.5.0"
}
"packageManager": "yarn@4.5.1"
}
2 changes: 1 addition & 1 deletion src/backend/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const config: Config = {

rateLimits: {
max: envParseInteger('RATE_LIMIT_MAX', 500),
timeWindow: envParseString('RATE_LIMIT_TIME_WINDOW', '1 minute')
timeWindow: envParseInteger('RATE_LIMIT_TIME_WINDOW', 1000 * 60)
},

storage: {
Expand Down
19 changes: 15 additions & 4 deletions src/backend/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@ export interface Config {

/** The rate limit configuration */
interface RateLimitsConfig {
/** The maximum amount of requests within the timespan {@link RateLimitsConfig.timeWindow timeWindow} */
/**
* Maximum number of requests a single client can perform inside a {@link RateLimitsConfig.timeWindow timeWindow}.
*
* It can be an async function with the signature `async (request, key) => {}` where `request` is the
* Fastify request object and `key` is the value generated by the `keyGenerator`. The function **must** return a number.
*/
max: number;
/** The time window in which the requests count, in the {@linkplain https://github.com/zeit/ms ms} string format */
timeWindow: string;
/**
* The duration of the time window.
*
* It can be expressed in milliseconds, as a string (in the [`ms`](https://github.com/zeit/ms) format), or as an
* async function with the signature `async (request, key) => {}` where `request` is the Fastify request object and
* `key` is the value generated by the `keyGenerator`. The function **must** return a number.
*/
timeWindow: number;
}

/** The storage options */
Expand Down Expand Up @@ -80,7 +91,7 @@ declare module '@skyra/env-utilities' {
KEY_LENGTH: IntegerString;
MAX_LENGTH: IntegerString;
RATE_LIMIT_MAX: IntegerString;
RATE_LIMIT_TIME_WINDOW: string;
RATE_LIMIT_TIME_WINDOW: IntegerString;
STORAGE_TYPE: 'file' | 'redis';
STORAGE_HOST: string;
STORAGE_PORT: IntegerString;
Expand Down
16 changes: 15 additions & 1 deletion src/backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@ fastify.setErrorHandler((error, _, reply) => {
// Register the rate limit handler
await fastify.register(import('@fastify/rate-limit'), {
max: config.rateLimits.max,
timeWindow: config.rateLimits.timeWindow
timeWindow: config.rateLimits.timeWindow,
keyGenerator: (request) => {
const xRealIp = request.headers['x-real-ip'];
if (typeof xRealIp === 'string') return xRealIp;
return request.ip;
},
errorResponseBuilder: (_, context) => {
return {
statusCode: 429,
error: 'Too Many Requests',
message: `Rate limit exceeded. Only ${context.max} requests per ${context.after} to this service are allowed. Retry in ${context.after}ms.`,
date: Date.now(),
expiresIn: context.ttl
};
}
});

// Register Fastify Sensible for sending errors
Expand Down
Loading

0 comments on commit feaea3d

Please sign in to comment.