Skip to content

Commit

Permalink
refactor: port to ESM + pnpm
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy committed Jun 17, 2024
1 parent 6a1cfcd commit 3658057
Show file tree
Hide file tree
Showing 135 changed files with 9,311 additions and 23,875 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ charset = utf-8
end_of_line = lf
insert_final_newline = true

[*.{js,jsx,ts,tsx,css,scss,json,swig,html,hbs}]
[*.{js,cjs,mjs,jsx,ts,tsx,css,scss,json,swig,html,hbs,md}]
quote_type = double
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
max_line_length = 120
77 changes: 0 additions & 77 deletions .eslintrc.json

This file was deleted.

3 changes: 2 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"require": "test/fixtures.ts",
"reporter": "mochawesome",
"exit": true
"exit": true,
"node-option": ["import=tsx"]
}
31 changes: 20 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
FROM node:16-alpine
WORKDIR /usr/src/krist
# Build
FROM node:20-alpine AS base

# Install packages
COPY package*.json ./
RUN apk add git ca-certificates
RUN npm install --legacy-peer-deps
RUN apk add git ca-certificates make g++

# Install source
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

WORKDIR /app
COPY . .

# Generate docs
RUN npm run docs
FROM base AS prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

FROM base AS build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build
RUN pnpm run docs
RUN cp /app/static/docs.bundle.js /app/static/docs/assets/main.bundle.js

# Run Krist
FROM base
COPY --from=prod-deps /app/node_modules /app/node_modules
COPY --from=build /app/dist /app/dist
EXPOSE 8080
ENV NODE_ENV=production
CMD ["npm", "start"]
CMD ["node", "dist/src/index.js"]
81 changes: 38 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@ This is the new official Krist node. It is written in Node.js and TypeScript.

## Conventional Commits

Commit messages are written with
Commit messages are written with
[Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).

## [API Documentation](https://krist.dev/docs)

The documentation is generated with [apiDoc](https://apidocjs.com). You can find
a live copy of the API documentation [here](https://krist.dev/docs). Additional
documentation for Krist itself and libraries can be found
The documentation is generated with [apiDoc](https://apidocjs.com). You can find a live copy of the API documentation
[here](https://krist.dev/docs). Additional documentation for Krist itself and libraries can be found
[here](https://docs.krist.dev).

## Docker Installation

The preferred method of installation is Docker. The Docker images are published
on the GitHub Container Registry under `ghcr.io/tmpim/krist`.
The preferred method of installation is Docker. The Docker images are published on the GitHub Container Registry under
`ghcr.io/tmpim/krist`.

MySQL/MariaDB and Redis are required too. There are two options to connect to
the databases. The easier way is to run MySQL and Redis on the host machine,
and specify the `DB_HOST=${DOCKER_GATEWAY}` and `REDIS_HOST=${DOCKER_GATEWAY}`
environment variables to Krist. Alternatively, you can run MariaDB and Redis
in Docker too (left as an exercise to the reader).
MariaDB and Redis are required too. There are two options to connect to the databases. The easier way is to run MariaDB
and Redis on the host machine, and specify the `DB_HOST=${DOCKER_GATEWAY}` and `REDIS_HOST=${DOCKER_GATEWAY}`
environment variables to Krist. Alternatively, you can run MariaDB and Redis in Docker too (left as an exercise to the
reader).

Example usage with Docker Compose:

Expand All @@ -47,54 +45,51 @@ services:
### Requirements
- Node.js v16
- MySQL or MariaDB
- Node.js v20
- MariaDB
- Redis
#### Webserver Configuration
This Krist node is supposed to be ran behind a serverside proxy. The file
`casket_example.casket` includes a basic configuration for how to set up the
proxy in [Casket](https://github.com/tmpim/casket). The Node.js webserver is not
This Krist node is supposed to be ran behind a serverside proxy. The file `casket_example.casket` includes a basic
configuration for how to set up the proxy in [Casket](https://github.com/tmpim/casket). The Node.js webserver is not
designed to and should not be exposed to the public web. HTTPS is required.

## Configuration

Basic configuration is now done via environment variables. You must supply the
following environment variables:
Basic configuration is now done via environment variables. You must supply the following environment variables:

| Variable | Default | Description |
|--------------|--------------|---------------------------------------------------------|
| `DB_PASS` | **required** | The password of the database user. |
| Variable | Default | Description |
|--------------|--------------|--------------------------------------------------|
| `DB_PASS` | **required** | The password of the database user. |
| `PUBLIC_URL` | **required** | The FQDN of the Krist server (e.g. `krist.dev`). |


The following optional environment variables may also be specified:

| Variable | Default | Description |
|---|---|---|
| `DB_HOST` | `127.0.0.1` | The hostname of the database. |
| `DB_PORT` | `3306` | The port of the database. |
| `DB_NAME` | `krist` | The name of the database. |
| `DB_USER` | `krist` | The username of the database user. |
| `TEST_DB_NAME` | `test_krist` | *Required for testing*. If `NODE_ENV` is `test`, the name of the database. |
| `TEST_DB_USER` | `test_krist` | *Required for testing*. If `NODE_ENV` is `test`, the username of the database user. |
| `TEST_DB_PASS` | | *Required for testing*. If `NODE_ENV` is `test`, the password of the database user. |
| `WEB_LISTEN` | `8080` | The port that the webserver listens on. |
| `PUBLIC_URL` | `localhost:8080` | The public URL of the websocket gateway. |
| `REDIS_HOST` | `127.0.0.1` | The hostname of the redis server. |
| `REDIS_PORT` | `6379` | The port of the redis server. |
| `REDIS_PREFIX` | `krist:` | The prefix of the redis keys. |
| `TEST_REDIS_PREFIX` | `test_krist:` | *Required for testing*. If `NODE_ENV` is `test` (e.g. running Jest), the prefix of the redis keys. |
| `NODE_ENV` | `development` | Either `development` or `production`. If `development`, the Krist server runs in debug mode. |
| `FORCE_INSECURE` | `false` | If `true`, force the websocket gateway to return `ws://` URLs instead of `wss://`. Used for development only. |
| `GITHUB_TOKEN` | | Any valid GitHub token (e.g. a PAT with no scopes) to obtain avatars for the homepage. Completely optional. |
| `USE_PROMETHEUS` | `false` | If `true`, enables Prometheus metrics on the `/metrics` endpoint. |
| `PROMETHEUS_PASSWORD` | | If set, require HTTP basic authentication to access Prometheus metrics, with the username `prometheus`. |
| Variable | Default | Description |
|-----------------------|------------------|---------------------------------------------------------------------------------------------------------------|
| `DB_HOST` | `127.0.0.1` | The hostname of the database. |
| `DB_PORT` | `3306` | The port of the database. |
| `DB_NAME` | `krist` | The name of the database. |
| `DB_USER` | `krist` | The username of the database user. |
| `TEST_DB_NAME` | `test_krist` | *Required for testing*. If `NODE_ENV` is `test`, the name of the database. |
| `TEST_DB_USER` | `test_krist` | *Required for testing*. If `NODE_ENV` is `test`, the username of the database user. |
| `TEST_DB_PASS` | | *Required for testing*. If `NODE_ENV` is `test`, the password of the database user. |
| `WEB_LISTEN` | `8080` | The port that the webserver listens on. |
| `PUBLIC_URL` | `localhost:8080` | The public URL of the websocket gateway. |
| `REDIS_HOST` | `127.0.0.1` | The hostname of the redis server. |
| `REDIS_PORT` | `6379` | The port of the redis server. |
| `REDIS_PREFIX` | `krist:` | The prefix of the redis keys. |
| `TEST_REDIS_PREFIX` | `test_krist:` | *Required for testing*. If `NODE_ENV` is `test` (e.g. running Jest), the prefix of the redis keys. |
| `NODE_ENV` | `development` | Either `development` or `production`. If `development`, the Krist server runs in debug mode. |
| `FORCE_INSECURE` | `false` | If `true`, force the websocket gateway to return `ws://` URLs instead of `wss://`. Used for development only. |
| `GITHUB_TOKEN` | | Any valid GitHub token (e.g. a PAT with no scopes) to obtain avatars for the homepage. Completely optional. |
| `USE_PROMETHEUS` | `false` | If `true`, enables Prometheus metrics on the `/metrics` endpoint. |
| `PROMETHEUS_PASSWORD` | | If set, require HTTP basic authentication to access Prometheus metrics, with the username `prometheus`. |

For convenience, you may specify environment variables in a `.env` file.

## License

This project is released under **GPL-3.0**. More information can be found in the
`LICENSE` file.
This project is released under **GPL-3.0**. More information can be found in the `LICENSE` file.
3 changes: 3 additions & 0 deletions apidoc-template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
2 changes: 1 addition & 1 deletion apidoc-template/src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 - 2022 Drew Edwards, tmpim
* Copyright 2016 - 2024 Drew Edwards, tmpim
*
* This file is part of Krist.
*
Expand Down
76 changes: 76 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export default {
root: true,
parser: "@typescript-eslint/parser",
plugins: [
"@typescript-eslint"
],
parserOptions: {
project: "./tsconfig.json",
tsconfigRootDir: ".",
ecmaVersion: 2018,
sourceType: "module"
},
env: {
es6: true,
node: true,
mocha: true
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
rules: {
quotes: ["error", "double", { allowTemplateLiterals: true }],
semi: "error",
indent: ["error", 2, {
FunctionDeclaration: { parameters: "first" }
}],
"prefer-arrow-callback": "warn",
"eol-last": ["error", "always"],
"object-shorthand": ["error", "always"],
"no-unused-vars": 0,
"no-lonely-if": "warn",
"no-trailing-spaces": "warn",
"no-whitespace-before-property": "warn",
"no-multiple-empty-lines": "warn",
"space-before-blocks": "warn",
"space-in-parens": ["warn", "never"],
"space-infix-ops": "warn",
"eqeqeq": "warn",
"no-shadow": ["error"],
"no-var": ["error"],
"prefer-const": ["error"],
"one-var": ["error", { separateRequires: true }],

"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-module-boundary-types": ["warn", {
allowArgumentsExplicitlyTypedAsAny: true,
allowDirectConstAssertionInArrowFunctions: true,
allowedNames: [],
allowHigherOrderFunctions: true,
allowTypedFunctionExpressions: true
}],
"@typescript-eslint/consistent-type-definitions": ["error", "interface"],
"@typescript-eslint/member-delimiter-style": ["error", {
multiline: { delimiter: "semi", requireLast: true },
singleline: { delimiter: "semi", requireLast: false }
}],
"@typescript-eslint/no-unused-vars": ["warn", {
ignoreRestSiblings: true,
argsIgnorePattern: "^_"
}],
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/space-before-function-paren": ["warn", {
anonymous: "never",
named: "never",
asyncArrow: "always"
}],

"@typescript-eslint/naming-convention": [
"error",
{ selector: "typeLike", format: ["StrictPascalCase"] },
{ selector: "variable", format: ["strictCamelCase", "UPPER_CASE", "StrictPascalCase"] },
{ selector: "variable", modifiers: ["destructured"], format: null }
]
}
}
Loading

0 comments on commit 3658057

Please sign in to comment.