diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index d60455a9..a6b6e93f 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,18 +1,87 @@ -name: Build & Push to container registry +name: Build & Push Docker Image to container image registry on: - release: - types: [created] - push: - branches: - - "**" - pull_request: - branches: - - "**" - paths: - - "Dockerfile" + workflow_call: + inputs: + ADD_FLAVOR: + description: "Whether to add flavor (only -dev atm) to the image tag" + type: boolean + required: false + default: true + NO_CACHE: + description: "Whether to use build cache" + type: boolean + required: false + default: false + SCOPE: + description: "Scope of the image (e.g. 'bot')" + type: string + required: true + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false jobs: - docker: - uses: nezuchan/workflows/.github/workflows/docker-build.yml@main - secrets: inherit \ No newline at end of file + build: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + scope: ${{ fromJson(inputs.SCOPE) }} + steps: + - name: Check Out Repo + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + fetch-depth: 2 + + #- name: Set up QEMU (We don't need QEMU, because we don't build images for platforms other than linux/amd64, which is our current native arch in our infra + # uses: docker/setup-qemu-action@v1.2.0 + + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 + + - name: Login to GitHub Container Registry + uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 + if: ${{ github.event_name != 'pull_request' }} + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract tag name + id: tags + if: ${{ github.event_name == 'release' }} + run: echo ::set-output name=name::${GITHUB_REF##*/} + + - name: Extract flavor + id: flavor + if: ${{ inputs.ADD_FLAVOR == true }} + run: if [ "${{ github.event_name }}" = "release" ]; then echo ::set-output name=name::; else echo ::set-output name=name::-dev; fi + + - name: Generate Docker image metadata + uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 + id: img_meta + with: + flavor: | + latest=auto + suffix=${{ steps.flavor.outputs.name }} + images: ghcr.io/nezuchan/${{ matrix.scope }} + tags: | + ${{ steps.tags.outputs.name }} + latest + + - name: Build and push + id: docker_build + uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 + with: + context: ./ + tags: ${{ steps.img_meta.outputs.tags }} + labels: ${{ steps.img_meta.outputs.labels }} + push: ${{ github.event_name != 'pull_request' }} + build-args: | + "SCOPE=@nezuchan/${{ matrix.scope }}" + cache-from: type=gha,mode=max + cache-to: type=gha,mode=max + no-cache: ${{ inputs.NO_CACHE == true }} diff --git a/.github/workflows/services-release.yml b/.github/workflows/services-release.yml new file mode 100644 index 00000000..7edffc77 --- /dev/null +++ b/.github/workflows/services-release.yml @@ -0,0 +1,12 @@ +name: Release services + +on: + release: + types: [created] + +jobs: + docker: + uses: ./.github/workflows/docker.yml + with: + scope: '["kanao-gateway"]' + secrets: inherit \ No newline at end of file diff --git a/.github/workflows/services.yml b/.github/workflows/services.yml new file mode 100644 index 00000000..01caff12 --- /dev/null +++ b/.github/workflows/services.yml @@ -0,0 +1,45 @@ +# Copyright 2023 Hazmi35 (https://github.com/Hazmi35) +name: Services Nightly Builds + +on: + push: + branches: + - main + paths: + - "Dockerfile" + - "services/**" + pull_request: + branches: + - main + paths: + - "Dockerfile" + +jobs: + build: + runs-on: ubuntu-latest + outputs: + folders: ${{ steps.json.outputs.folders }} + steps: + - name: Check Out Repo + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + fetch-depth: 2 + - name: List changed folders + id: changed + run: | + echo ::set-output name=folders::$(git diff --name-only HEAD~1 HEAD | grep -E 'services/.*' | cut -d'/' -f2 | sort | uniq | tr '\n' ',' | sed 's/,$//') + - name: List all folders if no changes + if: steps.changed.outputs.folders == '' + id: all + run: | + echo ::set-output name=folders::$(ls services | tr '\n' ',' | sed 's/,$//') + - name: Turn output into JSON array + id: json + run: | + echo ::set-output name=folders::[$(echo ${{ steps.changed.outputs.folders || steps.all.outputs.folders }} | sed 's/,/","/g;s/^/"/;s/$/"/')] + docker: + needs: build + uses: ./.github/workflows/docker.yml + with: + scope: ${{ needs.build.outputs.folders }} + secrets: inherit diff --git a/.turbo/cookies/0.cookie b/.turbo/cookies/0.cookie new file mode 100644 index 00000000..e69de29b diff --git a/.turbo/cookies/1.cookie b/.turbo/cookies/1.cookie new file mode 100644 index 00000000..e69de29b diff --git a/Dockerfile b/Dockerfile index e891ea21..47fd1ca6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,52 @@ -FROM ghcr.io/hazmi35/node:20-dev-alpine as build-stage +# Copyright 2023 Hazmi35 (https://github.com/Hazmi35) +FROM ghcr.io/hazmi35/node:20-dev-alpine as dev -LABEL name "NezukoChan Gateway (Docker Build)" -LABEL maintainer "KagChi" +# Prepare with corepack (experimental feature) +RUN corepack enable -RUN corepack enable && corepack prepare pnpm@latest +# Prepare +FROM dev as prepare -COPY package*.json . -COPY pnpm-lock.yaml . - -RUN pnpm install --frozen-lockfile +ARG SCOPE +WORKDIR /prepare +# Scoped install for monorepo +RUN npm install --global turbo COPY . . +RUN turbo prune --scope=${SCOPE} --docker +RUN cp -r tsconfig* /prepare/out/json + +# Build the project +FROM dev AS builder + +WORKDIR /builder + +# Set NPM_CONFIG_USERCONFIG build args +ARG SCOPE + +# First install the dependencies (as they change less often) +COPY --from=prepare /prepare/out/json/ . +COPY --from=prepare /prepare/out/pnpm-lock.yaml ./pnpm-lock.yaml + +# Install dependencies +RUN pnpm install --frozen-lockfile +# Build the project +COPY --from=prepare /prepare/out/full/ . RUN pnpm run build -RUN pnpm prune --production +# Deploy the service +RUN pnpm deploy -P --filter=${SCOPE} /out +# Get ready for production FROM ghcr.io/hazmi35/node:20-alpine -LABEL name "NezukoChan Gateway Production" -LABEL maintainer "KagChi" +LABEL name "${SCOPE}" -COPY --from=build-stage /tmp/build/package.json . -COPY --from=build-stage /tmp/build/pnpm-lock.yaml . -COPY --from=build-stage /tmp/build/node_modules ./node_modules -COPY --from=build-stage /tmp/build/dist ./dist +# Copy needed files +COPY --from=builder /out/package.json . +COPY --from=builder /out/node_modules ./node_modules +COPY --from=builder /out/dist ./dist -CMD ["node", "dist/index.js"] +# Start the app with node +CMD ["npm", "start"] \ No newline at end of file diff --git a/README.md b/README.md index b01cec51..4b78570f 100644 --- a/README.md +++ b/README.md @@ -11,33 +11,4 @@ -# Requirements -- NodeJS 16+ - -# Features -- Zero downtime deployments, You will almost never need to restart the gateway service, allowing absolute 100% uptime for your bot. Even when a restart is required, kanao will resume the sessions, so you will not lose a single event. -- Big Bot Sharding support -- Docker replica - -# Docker Replica - -To deploy replica the container must connected to docker sock in order know which replica in they are -```yaml -version: '3.8' - -services: - kanao: - deploy: - resources: - limits: - memory: "256M" - replicas: ${GATEWAY_REPLICA_COUNT:-3} - restart: always - image: 'ghcr.io/nezuchan/kanao:latest' - env_file: - - .env - volumes: - - /var/run/docker.sock:/var/run/docker.sock -``` - Being used in production by NezukoChan, Musical Tune, and more. diff --git a/package.json b/package.json index d63b8226..e989a05f 100644 --- a/package.json +++ b/package.json @@ -1,61 +1,21 @@ { - "name": "@nezuchan/kanao", - "version": "4.0.0", - "description": "A standalone service for connecting to the Discord gateway.", + "name": "kanao", + "version": "1.0.0", + "description": "A standalone service for connecting to the Discord gateway", + "type": "module", + "scripts": { + "build": "turbo run build", + "lint": "turbo run lint", + "lint:fix": "turbo run lint:fix" + }, + "keywords": [], + "author": "", "license": "GPL-3.0", - "author": "KagChi", - "type": "module", - "main": "dist", - "scripts": { - "build": "rimraf dist && swc ./src -d dist --config-file .swcrc --strip-leading-paths", - "lint": "eslint src", - "lint:fix": "eslint src --fix", - "start": "node -r dotenv/config dist/index.js", - "start:dev": "npm run build && node -r dotenv/config dist/index.js" - }, - "dependencies": { - "@discordjs/collection": "^2.0.0", - "@discordjs/rest": "^2.2.0", - "@discordjs/ws": "^1.0.2", - "@nezuchan/constants": "^0.8.0", - "@nezuchan/utilities": "^0.6.2", - "@sapphire/pieces": "^4.2.2", - "@sapphire/result": "^2.6.6", - "@sapphire/time-utilities": "^1.7.12", - "@sapphire/utilities": "^3.15.3", - "@skyra/start-banner": "^2.0.1", - "amqp-connection-manager": "^4.1.14", - "amqplib": "^0.10.3", - "bufferutil": "^4.0.8", - "discord-api-types": "^0.37.69", - "dockerode": "^4.0.2", - "dotenv": "^16.4.3", - "drizzle-orm": "^0.29.3", - "gradient-string": "^2.0.2", - "pino": "^8.18.0", - "pino-pretty": "^10.3.1", - "postgres": "^3.4.3", - "prometheus-middleware": "^1.3.3", - "tslib": "^2.6.2", - "utf-8-validate": "^6.0.3" - }, "devDependencies": { "@hazmi35/eslint-config": "^13.3.1", - "@swc/cli": "^0.3.9", - "@swc/core": "^1.4.0", - "@types/amqplib": "^0.10.4", - "@types/dockerode": "^3.3.23", - "@types/gradient-string": "^1.1.5", - "@types/node": "^20.11.17", - "@vladfrangu/async_event_emitter": "^2.2.4", - "drizzle-kit": "^0.20.14", "eslint": "^8.56.0", "rimraf": "^5.0.5", + "turbo": "^1.12.3", "typescript": "^5.3.3" - }, - "optionalDependencies": { - "ioredis": "^5.3.2", - "pino-loki": "^2.2.1", - "zlib-sync": "^0.1.9" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 71eaff71..de82f1a4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,128 +4,141 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false -dependencies: - '@discordjs/collection': - specifier: ^2.0.0 - version: 2.0.0 - '@discordjs/rest': - specifier: ^2.2.0 - version: 2.2.0 - '@discordjs/ws': - specifier: ^1.0.2 - version: 1.0.2(bufferutil@4.0.8)(utf-8-validate@6.0.3) - '@nezuchan/constants': - specifier: ^0.8.0 - version: 0.8.0 - '@nezuchan/utilities': - specifier: ^0.6.2 - version: 0.6.2(amqplib@0.10.3) - '@sapphire/pieces': - specifier: ^4.2.2 - version: 4.2.2 - '@sapphire/result': - specifier: ^2.6.6 - version: 2.6.6 - '@sapphire/time-utilities': - specifier: ^1.7.12 - version: 1.7.12 - '@sapphire/utilities': - specifier: ^3.15.3 - version: 3.15.3 - '@skyra/start-banner': - specifier: ^2.0.1 - version: 2.0.1 - amqp-connection-manager: - specifier: ^4.1.14 - version: 4.1.14(amqplib@0.10.3) - amqplib: - specifier: ^0.10.3 - version: 0.10.3 - bufferutil: - specifier: ^4.0.8 - version: 4.0.8 - discord-api-types: - specifier: ^0.37.69 - version: 0.37.69 - dockerode: - specifier: ^4.0.2 - version: 4.0.2 - dotenv: - specifier: ^16.4.3 - version: 16.4.3 - drizzle-orm: - specifier: ^0.29.3 - version: 0.29.3(postgres@3.4.3) - gradient-string: - specifier: ^2.0.2 - version: 2.0.2 - pino: - specifier: ^8.18.0 - version: 8.18.0 - pino-pretty: - specifier: ^10.3.1 - version: 10.3.1 - postgres: - specifier: ^3.4.3 - version: 3.4.3 - prometheus-middleware: - specifier: ^1.3.3 - version: 1.3.3 - tslib: - specifier: ^2.6.2 - version: 2.6.2 - utf-8-validate: - specifier: ^6.0.3 - version: 6.0.3 - -optionalDependencies: - ioredis: - specifier: ^5.3.2 - version: 5.3.2 - pino-loki: - specifier: ^2.2.1 - version: 2.2.1 - zlib-sync: - specifier: ^0.1.9 - version: 0.1.9 - -devDependencies: - '@hazmi35/eslint-config': - specifier: ^13.3.1 - version: 13.3.1(@eslint/eslintrc@3.0.1)(@stylistic/eslint-plugin@1.6.1)(@typescript-eslint/eslint-plugin@6.21.0)(@typescript-eslint/parser@6.21.0)(eslint-config-prettier@9.1.0)(eslint-import-resolver-typescript@3.6.1)(eslint-plugin-import@2.29.1)(eslint-plugin-jsdoc@48.0.6)(eslint-plugin-n@16.6.2)(eslint-plugin-promise@6.1.1)(eslint-plugin-tsdoc@0.2.17)(eslint-plugin-unicorn@51.0.1)(eslint@8.56.0)(globals@14.0.0)(typescript@5.3.3) - '@swc/cli': - specifier: ^0.3.9 - version: 0.3.9(@swc/core@1.4.0) - '@swc/core': - specifier: ^1.4.0 - version: 1.4.0 - '@types/amqplib': - specifier: ^0.10.4 - version: 0.10.4 - '@types/dockerode': - specifier: ^3.3.23 - version: 3.3.23 - '@types/gradient-string': - specifier: ^1.1.5 - version: 1.1.5 - '@types/node': - specifier: ^20.11.17 - version: 20.11.17 - '@vladfrangu/async_event_emitter': - specifier: ^2.2.4 - version: 2.2.4 - drizzle-kit: - specifier: ^0.20.14 - version: 0.20.14 - eslint: - specifier: ^8.56.0 - version: 8.56.0 - rimraf: - specifier: ^5.0.5 - version: 5.0.5 - typescript: - specifier: ^5.3.3 - version: 5.3.3 +importers: + + .: + devDependencies: + '@hazmi35/eslint-config': + specifier: ^13.3.1 + version: 13.3.1(@eslint/eslintrc@3.0.1)(@stylistic/eslint-plugin@1.6.1)(@typescript-eslint/eslint-plugin@6.21.0)(@typescript-eslint/parser@6.21.0)(eslint-config-prettier@9.1.0)(eslint-import-resolver-typescript@3.6.1)(eslint-plugin-import@2.29.1)(eslint-plugin-jsdoc@48.0.6)(eslint-plugin-n@16.6.2)(eslint-plugin-promise@6.1.1)(eslint-plugin-tsdoc@0.2.17)(eslint-plugin-unicorn@51.0.1)(eslint@8.56.0)(globals@14.0.0)(typescript@5.3.3) + eslint: + specifier: ^8.56.0 + version: 8.56.0 + rimraf: + specifier: ^5.0.5 + version: 5.0.5 + turbo: + specifier: ^1.12.3 + version: 1.12.3 + typescript: + specifier: ^5.3.3 + version: 5.3.3 + + services/kanao-gateway: + dependencies: + '@discordjs/collection': + specifier: ^2.0.0 + version: 2.0.0 + '@discordjs/rest': + specifier: ^2.2.0 + version: 2.2.0 + '@discordjs/ws': + specifier: ^1.0.2 + version: 1.0.2(bufferutil@4.0.8)(utf-8-validate@6.0.3) + '@nezuchan/constants': + specifier: ^0.8.0 + version: 0.8.0 + '@nezuchan/utilities': + specifier: ^0.6.2 + version: 0.6.2(amqplib@0.10.3) + '@sapphire/pieces': + specifier: ^4.2.2 + version: 4.2.2 + '@sapphire/result': + specifier: ^2.6.6 + version: 2.6.6 + '@sapphire/time-utilities': + specifier: ^1.7.12 + version: 1.7.12 + '@sapphire/utilities': + specifier: ^3.15.3 + version: 3.15.3 + '@skyra/start-banner': + specifier: ^2.0.1 + version: 2.0.1 + amqp-connection-manager: + specifier: ^4.1.14 + version: 4.1.14(amqplib@0.10.3) + amqplib: + specifier: ^0.10.3 + version: 0.10.3 + bufferutil: + specifier: ^4.0.8 + version: 4.0.8 + discord-api-types: + specifier: ^0.37.69 + version: 0.37.69 + dockerode: + specifier: ^4.0.2 + version: 4.0.2 + dotenv: + specifier: ^16.4.3 + version: 16.4.3 + drizzle-orm: + specifier: ^0.29.3 + version: 0.29.3(postgres@3.4.3) + gradient-string: + specifier: ^2.0.2 + version: 2.0.2 + pino: + specifier: ^8.18.0 + version: 8.18.0 + pino-pretty: + specifier: ^10.3.1 + version: 10.3.1 + postgres: + specifier: ^3.4.3 + version: 3.4.3 + prometheus-middleware: + specifier: ^1.3.3 + version: 1.3.3 + tslib: + specifier: ^2.6.2 + version: 2.6.2 + utf-8-validate: + specifier: ^6.0.3 + version: 6.0.3 + optionalDependencies: + ioredis: + specifier: ^5.3.2 + version: 5.3.2 + pino-loki: + specifier: ^2.2.1 + version: 2.2.1 + zlib-sync: + specifier: ^0.1.9 + version: 0.1.9 + devDependencies: + '@swc/cli': + specifier: ^0.3.9 + version: 0.3.9(@swc/core@1.4.0) + '@swc/core': + specifier: ^1.4.0 + version: 1.4.0 + '@types/amqplib': + specifier: ^0.10.4 + version: 0.10.4 + '@types/dockerode': + specifier: ^3.3.23 + version: 3.3.23 + '@types/gradient-string': + specifier: ^1.1.5 + version: 1.1.5 + '@types/node': + specifier: ^20.11.17 + version: 20.11.17 + '@vladfrangu/async_event_emitter': + specifier: ^2.2.4 + version: 2.2.4 + drizzle-kit: + specifier: ^0.20.14 + version: 0.20.14 + rimraf: + specifier: ^5.0.5 + version: 5.0.5 + typescript: + specifier: ^5.3.3 + version: 5.3.3 packages: @@ -1015,7 +1028,7 @@ packages: fast-glob: 3.3.2 minimatch: 9.0.3 piscina: 4.3.1 - semver: 7.5.4 + semver: 7.6.0 slash: 3.0.0 source-map: 0.7.4 dev: true @@ -1120,7 +1133,7 @@ packages: '@swc/helpers': optional: true dependencies: - '@swc/counter': 0.1.2 + '@swc/counter': 0.1.3 '@swc/types': 0.1.5 optionalDependencies: '@swc/core-darwin-arm64': 1.4.0 @@ -1135,10 +1148,6 @@ packages: '@swc/core-win32-x64-msvc': 1.4.0 dev: true - /@swc/counter@0.1.2: - resolution: {integrity: sha512-9F4ys4C74eSTEUNndnER3VJ15oru2NumfQxS8geE+f3eB5xvfxpWyqE5XlVnxb/R14uoXi6SLbBwwiDSkv+XEw==} - dev: true - /@swc/counter@0.1.3: resolution: {integrity: sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ==} dev: true @@ -1607,7 +1616,7 @@ packages: engines: {node: '>=12'} dependencies: bin-version: 6.0.0 - semver: 7.5.4 + semver: 7.6.0 semver-truncate: 3.0.0 dev: true @@ -4299,7 +4308,7 @@ packages: resolution: {integrity: sha512-LJWA9kSvMolR51oDE6PN3kALBNaUdkxzAGcexw8gjMA8xr5zUqK0JiR3CgARSqanYF3Z1YHvsErb1KDgh+v7Rg==} engines: {node: '>=12'} dependencies: - semver: 7.5.4 + semver: 7.6.0 dev: true /semver@5.7.2: @@ -4312,14 +4321,6 @@ packages: hasBin: true dev: true - /semver@7.5.4: - resolution: {integrity: sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==} - engines: {node: '>=10'} - hasBin: true - dependencies: - lru-cache: 6.0.0 - dev: true - /semver@7.6.0: resolution: {integrity: sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==} engines: {node: '>=10'} @@ -4728,6 +4729,66 @@ packages: resolution: {integrity: sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==} dev: false + /turbo-darwin-64@1.12.3: + resolution: {integrity: sha512-dDglIaux+A4jOnB9CDH69sujmrnuLJLrKw1t3J+if6ySlFuxSwC++gDq9TVuOZo2+S7lFkGh+x5ytn3wp+jE8Q==} + cpu: [x64] + os: [darwin] + requiresBuild: true + dev: true + optional: true + + /turbo-darwin-arm64@1.12.3: + resolution: {integrity: sha512-5TqqeujEyHMoVUWGzSzUl5ERSg7HDCdbU3gBs5ziWTpFRpeJ/+Y15kYyZJcMQcubRIH3Y1hL/yA5IhlGdgXOMA==} + cpu: [arm64] + os: [darwin] + requiresBuild: true + dev: true + optional: true + + /turbo-linux-64@1.12.3: + resolution: {integrity: sha512-yUreU+/gq4vlBtcdyfjz7slwz4zM1RG8sSXvyHmAS+QXqSrGkegg4qLl2fRbv/c3EyA/XbfcZuD6tcrXkejr6g==} + cpu: [x64] + os: [linux] + requiresBuild: true + dev: true + optional: true + + /turbo-linux-arm64@1.12.3: + resolution: {integrity: sha512-XRwAsp2eRSqZmaMVNrmHoKqofeJMuD87zmefZLTRAObh38hIwKgyl2QRsJIbteob5RN77yFbv3lAJ36UIY5h7w==} + cpu: [arm64] + os: [linux] + requiresBuild: true + dev: true + optional: true + + /turbo-windows-64@1.12.3: + resolution: {integrity: sha512-CPnRfnUCtmFeShOtUdMCthySjmyHaoTyh9JueiYFvtCNeO3WfDMj63dpOQstQWHdJFYmIrIGfhAclcds9ePQYA==} + cpu: [x64] + os: [win32] + requiresBuild: true + dev: true + optional: true + + /turbo-windows-arm64@1.12.3: + resolution: {integrity: sha512-cYA/wlzvp4vlCNHYJ2AjNS3FLXWwUC/5CJompBkTeKFFB6AviE/iLkbIhFikCVSNXZk/3AGanpMUXIkt3bdlwg==} + cpu: [arm64] + os: [win32] + requiresBuild: true + dev: true + optional: true + + /turbo@1.12.3: + resolution: {integrity: sha512-a6q8I0TK9ohACYbkmxzG/JYPuDC4VCvfmXLTlf321qQ4BIAhoyaOj/O2g+zJ6L1vNYnZ82G4LrbMfgLLngbLsg==} + hasBin: true + optionalDependencies: + turbo-darwin-64: 1.12.3 + turbo-darwin-arm64: 1.12.3 + turbo-linux-64: 1.12.3 + turbo-linux-arm64: 1.12.3 + turbo-windows-64: 1.12.3 + turbo-windows-arm64: 1.12.3 + dev: true + /tweetnacl@0.14.5: resolution: {integrity: sha512-KXXFFdAbFXY4geFIwoyNK+f5Z1b7swfXABfL7HXCmoIWMKU3dmS26672A4EeQtDzLKy7SXmfBu51JolvEKwtGA==} dev: false diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 00000000..c66016f4 --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,2 @@ +packages: + - 'services/*' \ No newline at end of file diff --git a/.example.env b/services/kanao-gateway/.example.env similarity index 100% rename from .example.env rename to services/kanao-gateway/.example.env diff --git a/.swcrc b/services/kanao-gateway/.swcrc similarity index 94% rename from .swcrc rename to services/kanao-gateway/.swcrc index e2219e01..6b9a900a 100644 --- a/.swcrc +++ b/services/kanao-gateway/.swcrc @@ -1,12 +1,12 @@ -{ - "jsc": { - "parser": { - "syntax": "typescript", - "decorators": true - }, - "target": "es2022" - }, - "module": { - "type": "es6" - } +{ + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2022" + }, + "module": { + "type": "es6" + } } \ No newline at end of file diff --git a/services/kanao-gateway/README.md b/services/kanao-gateway/README.md new file mode 100644 index 00000000..b01cec51 --- /dev/null +++ b/services/kanao-gateway/README.md @@ -0,0 +1,43 @@ +
+ +Logo + +# @nezuchan/kanao + +**A standalone service for connecting to the Discord gateway.** + +[![GitHub](https://img.shields.io/github/license/nezuchan/kanao)](https://github.com/nezuchan/kanao/blob/main/LICENSE) +[![Discord](https://discordapp.com/api/guilds/785715968608567297/embed.png)](https://nezu.my.id) + +
+ +# Requirements +- NodeJS 16+ + +# Features +- Zero downtime deployments, You will almost never need to restart the gateway service, allowing absolute 100% uptime for your bot. Even when a restart is required, kanao will resume the sessions, so you will not lose a single event. +- Big Bot Sharding support +- Docker replica + +# Docker Replica + +To deploy replica the container must connected to docker sock in order know which replica in they are +```yaml +version: '3.8' + +services: + kanao: + deploy: + resources: + limits: + memory: "256M" + replicas: ${GATEWAY_REPLICA_COUNT:-3} + restart: always + image: 'ghcr.io/nezuchan/kanao:latest' + env_file: + - .env + volumes: + - /var/run/docker.sock:/var/run/docker.sock +``` + +Being used in production by NezukoChan, Musical Tune, and more. diff --git a/drizzle.config.ts b/services/kanao-gateway/drizzle.config.ts similarity index 100% rename from drizzle.config.ts rename to services/kanao-gateway/drizzle.config.ts diff --git a/services/kanao-gateway/package.json b/services/kanao-gateway/package.json new file mode 100644 index 00000000..fb2a7ce6 --- /dev/null +++ b/services/kanao-gateway/package.json @@ -0,0 +1,59 @@ +{ + "name": "@nezuchan/kanao-gateway", + "version": "4.0.0", + "description": "A standalone service for connecting to the Discord gateway.", + "license": "GPL-3.0", + "author": "KagChi", + "type": "module", + "main": "dist", + "scripts": { + "build": "rimraf dist && swc ./src -d dist --config-file .swcrc --strip-leading-paths", + "lint": "eslint src", + "lint:fix": "eslint src --fix", + "start": "node -r dotenv/config dist/index.js", + "start:dev": "npm run build && node -r dotenv/config dist/index.js" + }, + "dependencies": { + "@discordjs/collection": "^2.0.0", + "@discordjs/rest": "^2.2.0", + "@discordjs/ws": "^1.0.2", + "@nezuchan/constants": "^0.8.0", + "@nezuchan/utilities": "^0.6.2", + "@sapphire/pieces": "^4.2.2", + "@sapphire/result": "^2.6.6", + "@sapphire/time-utilities": "^1.7.12", + "@sapphire/utilities": "^3.15.3", + "@skyra/start-banner": "^2.0.1", + "amqp-connection-manager": "^4.1.14", + "amqplib": "^0.10.3", + "bufferutil": "^4.0.8", + "discord-api-types": "^0.37.69", + "dockerode": "^4.0.2", + "dotenv": "^16.4.3", + "drizzle-orm": "^0.29.3", + "gradient-string": "^2.0.2", + "pino": "^8.18.0", + "pino-pretty": "^10.3.1", + "postgres": "^3.4.3", + "prometheus-middleware": "^1.3.3", + "tslib": "^2.6.2", + "utf-8-validate": "^6.0.3" + }, + "devDependencies": { + "@swc/cli": "^0.3.9", + "@swc/core": "^1.4.0", + "@types/amqplib": "^0.10.4", + "@types/dockerode": "^3.3.23", + "@types/gradient-string": "^1.1.5", + "@types/node": "^20.11.17", + "@vladfrangu/async_event_emitter": "^2.2.4", + "drizzle-kit": "^0.20.14", + "rimraf": "^5.0.5", + "typescript": "^5.3.3" + }, + "optionalDependencies": { + "ioredis": "^5.3.2", + "pino-loki": "^2.2.1", + "zlib-sync": "^0.1.9" + } +} diff --git a/src/Listeners/Caches/Channels/ChannelCreateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Channels/ChannelCreateListener.ts similarity index 100% rename from src/Listeners/Caches/Channels/ChannelCreateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Channels/ChannelCreateListener.ts diff --git a/src/Listeners/Caches/Channels/ChannelDeleteListener.ts b/services/kanao-gateway/src/Listeners/Caches/Channels/ChannelDeleteListener.ts similarity index 100% rename from src/Listeners/Caches/Channels/ChannelDeleteListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Channels/ChannelDeleteListener.ts diff --git a/src/Listeners/Caches/Channels/ChannelUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Channels/ChannelUpdateListener.ts similarity index 100% rename from src/Listeners/Caches/Channels/ChannelUpdateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Channels/ChannelUpdateListener.ts diff --git a/src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts similarity index 100% rename from src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts rename to services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMemberAddListener.ts diff --git a/src/Listeners/Caches/GuildMembers/GuildMemberRemoveListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMemberRemoveListener.ts similarity index 100% rename from src/Listeners/Caches/GuildMembers/GuildMemberRemoveListener.ts rename to services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMemberRemoveListener.ts diff --git a/src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts similarity index 100% rename from src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMemberUpdateListener.ts diff --git a/src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts similarity index 100% rename from src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts rename to services/kanao-gateway/src/Listeners/Caches/GuildMembers/GuildMembersChunkListener.ts diff --git a/src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts similarity index 100% rename from src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleCreateListener.ts diff --git a/src/Listeners/Caches/GuildRoles/GuildRoleDeleteListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleDeleteListener.ts similarity index 100% rename from src/Listeners/Caches/GuildRoles/GuildRoleDeleteListener.ts rename to services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleDeleteListener.ts diff --git a/src/Listeners/Caches/GuildRoles/GuildRoleUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleUpdateListener.ts similarity index 100% rename from src/Listeners/Caches/GuildRoles/GuildRoleUpdateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/GuildRoles/GuildRoleUpdateListener.ts diff --git a/src/Listeners/Caches/Guilds/GuildCreateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Guilds/GuildCreateListener.ts similarity index 100% rename from src/Listeners/Caches/Guilds/GuildCreateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Guilds/GuildCreateListener.ts diff --git a/src/Listeners/Caches/Guilds/GuildDeleteListener.ts b/services/kanao-gateway/src/Listeners/Caches/Guilds/GuildDeleteListener.ts similarity index 100% rename from src/Listeners/Caches/Guilds/GuildDeleteListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Guilds/GuildDeleteListener.ts diff --git a/src/Listeners/Caches/Guilds/GuildUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Guilds/GuildUpdateListener.ts similarity index 100% rename from src/Listeners/Caches/Guilds/GuildUpdateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Guilds/GuildUpdateListener.ts diff --git a/src/Listeners/Caches/Messages/MessageCreateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Messages/MessageCreateListener.ts similarity index 100% rename from src/Listeners/Caches/Messages/MessageCreateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Messages/MessageCreateListener.ts diff --git a/src/Listeners/Caches/Messages/MessageDeleteBulkListener.ts b/services/kanao-gateway/src/Listeners/Caches/Messages/MessageDeleteBulkListener.ts similarity index 100% rename from src/Listeners/Caches/Messages/MessageDeleteBulkListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Messages/MessageDeleteBulkListener.ts diff --git a/src/Listeners/Caches/Messages/MessageDeleteListener.ts b/services/kanao-gateway/src/Listeners/Caches/Messages/MessageDeleteListener.ts similarity index 100% rename from src/Listeners/Caches/Messages/MessageDeleteListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Messages/MessageDeleteListener.ts diff --git a/src/Listeners/Caches/Messages/MessageUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Messages/MessageUpdateListener.ts similarity index 100% rename from src/Listeners/Caches/Messages/MessageUpdateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Messages/MessageUpdateListener.ts diff --git a/src/Listeners/Caches/Users/UserUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Users/UserUpdateListener.ts similarity index 100% rename from src/Listeners/Caches/Users/UserUpdateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Users/UserUpdateListener.ts diff --git a/src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts b/services/kanao-gateway/src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts similarity index 100% rename from src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts rename to services/kanao-gateway/src/Listeners/Caches/Voices/VoiceStateUpdateListener.ts diff --git a/src/Listeners/ClosedListener.ts b/services/kanao-gateway/src/Listeners/ClosedListener.ts similarity index 100% rename from src/Listeners/ClosedListener.ts rename to services/kanao-gateway/src/Listeners/ClosedListener.ts diff --git a/src/Listeners/DispatchListener.ts b/services/kanao-gateway/src/Listeners/DispatchListener.ts similarity index 100% rename from src/Listeners/DispatchListener.ts rename to services/kanao-gateway/src/Listeners/DispatchListener.ts diff --git a/src/Listeners/HeartBeatCompleteListener.ts b/services/kanao-gateway/src/Listeners/HeartBeatCompleteListener.ts similarity index 100% rename from src/Listeners/HeartBeatCompleteListener.ts rename to services/kanao-gateway/src/Listeners/HeartBeatCompleteListener.ts diff --git a/src/Listeners/ReadyListener.ts b/services/kanao-gateway/src/Listeners/ReadyListener.ts similarity index 100% rename from src/Listeners/ReadyListener.ts rename to services/kanao-gateway/src/Listeners/ReadyListener.ts diff --git a/src/Listeners/ResumeListener.ts b/services/kanao-gateway/src/Listeners/ResumeListener.ts similarity index 100% rename from src/Listeners/ResumeListener.ts rename to services/kanao-gateway/src/Listeners/ResumeListener.ts diff --git a/src/Schema/collections/channel.ts b/services/kanao-gateway/src/Schema/collections/channel.ts similarity index 100% rename from src/Schema/collections/channel.ts rename to services/kanao-gateway/src/Schema/collections/channel.ts diff --git a/src/Schema/collections/guild.ts b/services/kanao-gateway/src/Schema/collections/guild.ts similarity index 100% rename from src/Schema/collections/guild.ts rename to services/kanao-gateway/src/Schema/collections/guild.ts diff --git a/src/Schema/collections/member.ts b/services/kanao-gateway/src/Schema/collections/member.ts similarity index 100% rename from src/Schema/collections/member.ts rename to services/kanao-gateway/src/Schema/collections/member.ts diff --git a/src/Schema/collections/message.ts b/services/kanao-gateway/src/Schema/collections/message.ts similarity index 100% rename from src/Schema/collections/message.ts rename to services/kanao-gateway/src/Schema/collections/message.ts diff --git a/src/Schema/collections/roles.ts b/services/kanao-gateway/src/Schema/collections/roles.ts similarity index 100% rename from src/Schema/collections/roles.ts rename to services/kanao-gateway/src/Schema/collections/roles.ts diff --git a/src/Schema/collections/session.ts b/services/kanao-gateway/src/Schema/collections/session.ts similarity index 100% rename from src/Schema/collections/session.ts rename to services/kanao-gateway/src/Schema/collections/session.ts diff --git a/src/Schema/collections/status.ts b/services/kanao-gateway/src/Schema/collections/status.ts similarity index 100% rename from src/Schema/collections/status.ts rename to services/kanao-gateway/src/Schema/collections/status.ts diff --git a/src/Schema/collections/user.ts b/services/kanao-gateway/src/Schema/collections/user.ts similarity index 100% rename from src/Schema/collections/user.ts rename to services/kanao-gateway/src/Schema/collections/user.ts diff --git a/src/Schema/collections/voice.ts b/services/kanao-gateway/src/Schema/collections/voice.ts similarity index 100% rename from src/Schema/collections/voice.ts rename to services/kanao-gateway/src/Schema/collections/voice.ts diff --git a/src/Schema/index.ts b/services/kanao-gateway/src/Schema/index.ts similarity index 100% rename from src/Schema/index.ts rename to services/kanao-gateway/src/Schema/index.ts diff --git a/src/Stores/Listener.ts b/services/kanao-gateway/src/Stores/Listener.ts similarity index 100% rename from src/Stores/Listener.ts rename to services/kanao-gateway/src/Stores/Listener.ts diff --git a/src/Stores/ListenerStore.ts b/services/kanao-gateway/src/Stores/ListenerStore.ts similarity index 100% rename from src/Stores/ListenerStore.ts rename to services/kanao-gateway/src/Stores/ListenerStore.ts diff --git a/src/Structures/NezuGateway.ts b/services/kanao-gateway/src/Structures/NezuGateway.ts similarity index 100% rename from src/Structures/NezuGateway.ts rename to services/kanao-gateway/src/Structures/NezuGateway.ts diff --git a/src/Utilities/Logger.ts b/services/kanao-gateway/src/Utilities/Logger.ts similarity index 100% rename from src/Utilities/Logger.ts rename to services/kanao-gateway/src/Utilities/Logger.ts diff --git a/src/Utilities/WebSockets/ProcessBootstrapper.ts b/services/kanao-gateway/src/Utilities/WebSockets/ProcessBootstrapper.ts similarity index 100% rename from src/Utilities/WebSockets/ProcessBootstrapper.ts rename to services/kanao-gateway/src/Utilities/WebSockets/ProcessBootstrapper.ts diff --git a/src/Utilities/WebSockets/ProcessContextFetchingStrategy.ts b/services/kanao-gateway/src/Utilities/WebSockets/ProcessContextFetchingStrategy.ts similarity index 100% rename from src/Utilities/WebSockets/ProcessContextFetchingStrategy.ts rename to services/kanao-gateway/src/Utilities/WebSockets/ProcessContextFetchingStrategy.ts diff --git a/src/Utilities/WebSockets/ProcessShardingStrategy.ts b/services/kanao-gateway/src/Utilities/WebSockets/ProcessShardingStrategy.ts similarity index 100% rename from src/Utilities/WebSockets/ProcessShardingStrategy.ts rename to services/kanao-gateway/src/Utilities/WebSockets/ProcessShardingStrategy.ts diff --git a/src/Utilities/WebSockets/ShardProcess.ts b/services/kanao-gateway/src/Utilities/WebSockets/ShardProcess.ts similarity index 100% rename from src/Utilities/WebSockets/ShardProcess.ts rename to services/kanao-gateway/src/Utilities/WebSockets/ShardProcess.ts diff --git a/src/config.ts b/services/kanao-gateway/src/config.ts similarity index 100% rename from src/config.ts rename to services/kanao-gateway/src/config.ts diff --git a/src/index.ts b/services/kanao-gateway/src/index.ts similarity index 100% rename from src/index.ts rename to services/kanao-gateway/src/index.ts diff --git a/services/kanao-gateway/tsconfig.json b/services/kanao-gateway/tsconfig.json new file mode 100644 index 00000000..2a0598dc --- /dev/null +++ b/services/kanao-gateway/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + }, + "include": ["src"] +} \ No newline at end of file diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 00000000..f11bcdb6 --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.json", + "include": [ + "./services/**/*.ts" + ] +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 025a68fc..421c3a67 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,6 @@ "moduleResolution": "Node16", "lib": ["ES2021"], "strict": true, - "outDir": "dist", "esModuleInterop": false, "allowSyntheticDefaultImports": true, "experimentalDecorators": true, @@ -16,8 +15,6 @@ "resolveJsonModule": true, "strictNullChecks": true, "noEmit": true, - "typeRoots": ["node_modules/@types", "src/typings"] - }, - "exclude": ["node_modules", "node_modules/**/**.d.ts"], - "include": ["src/**/**.ts", "src/**/**.json", "drizzle.config.ts"], + "typeRoots": ["node_modules/@types"] + } } \ No newline at end of file diff --git a/turbo.json b/turbo.json new file mode 100644 index 00000000..abbd00c6 --- /dev/null +++ b/turbo.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://turborepo.org/schema.json", + "pipeline": { + "build": { + "dependsOn": ["^build"], + "outputs": ["dist/**"] + }, + "lint": { + "outputs": [] + }, + "lint:fix": { + "outputs": [] + } + } +} \ No newline at end of file