Skip to content

Commit

Permalink
feat: use @fastify/http-proxy (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
samialdury committed May 9, 2023
1 parent a79b46f commit ccdd079
Show file tree
Hide file tree
Showing 50 changed files with 1,498 additions and 2,028 deletions.
6 changes: 0 additions & 6 deletions .eslintignore

This file was deleted.

69 changes: 69 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/** @type {import('eslint').Linter.Config} */
const config = {
ignorePatterns: [
'**/node_modules/**',
'**/build/**',
'**/coverage/**',
'**/cache/**',
'**/commitlint.config.cjs',
'**/lint-staged.config.cjs',
'**/vitest.config.ts',
'**/prettier.config.cjs',
'**/.eslintrc.cjs',
],
root: true,
env: {
node: true,
es2022: true,
},
parser: '@typescript-eslint/parser',
parserOptions: {
project: ['./tsconfig.json'],
},
plugins: ['@typescript-eslint'],
extends: [
// Common
'eslint:recommended',
'plugin:import/recommended',
// TS
'plugin:import/typescript',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:@typescript-eslint/strict',
// Prettier
'plugin:prettier/recommended',
],
settings: {
'import/resolver': {
typescript: {},
},
},
rules: {
'import/newline-after-import': 'error',
'import/order': [
'error',
{
'newlines-between': 'always',
alphabetize: { order: 'asc', caseInsensitive: true },
},
],
'object-shorthand': ['error', 'always'],
'no-console': 'error',
'func-style': ['error', 'declaration'],
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/promise-function-async': 'error',
'@typescript-eslint/consistent-type-imports': [
'warn',
{
prefer: 'type-imports',
fixStyle: 'inline-type-imports',
},
],
'@typescript-eslint/no-unused-vars': [
'warn',
{ argsIgnorePattern: '^_' },
],
},
}

module.exports = config
45 changes: 0 additions & 45 deletions .eslintrc.json

This file was deleted.

4 changes: 3 additions & 1 deletion .example.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
NODE_ENV="development"

LOGGER_LEVEL="trace"
LOG_REQUESTS="true"

PROXY_PORT=8000
COMMIT_SHA="local"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
node_modules
build
coverage
.eslintcache
cache

.env
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
build
coverage
cache
.husky
pnpm-lock.yaml
14 changes: 0 additions & 14 deletions .prettierrc.json

This file was deleted.

5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"editor.tabSize": 4
"editor.tabSize": 4,
"editor.insertSpaces": true,
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ HUSKY := $(NPM_BIN)/husky

COMMIT_SHA := $(shell git rev-parse HEAD)

ESLINT_CACHE := ./cache/.eslintcache
PRETTIER_CACHE := ./cache/.prettiercache

.PHONY: help
## Display this help
help:
Expand Down Expand Up @@ -39,6 +42,11 @@ build: ## build TS
rm -rf ./build
$(TSC) --build --force

.PHONY: build-prod
build-prod: ## build TS (for production)
rm -rf ./build
$(TSC) --project ./tsconfig.prod.json

.PHONY: build-image
build-image: ## build Docker image (args=<build args>, tag=<string>)
docker build $(or $(args), --build-arg COMMIT_SHA='dev,$(COMMIT_SHA)') -t $(or $(tag), $(PROJECT_NAME)) . -f ./Dockerfile
Expand All @@ -61,11 +69,11 @@ test-coverage: ## run tests (with coverage)

.PHONY: prettier
prettier: ## run Prettier (autofix)
$(PRETTIER) --cache --write .
$(PRETTIER) --cache --cache-location=$(PRETTIER_CACHE) --write .

.PHONY: eslint
eslint: ## run ESLint (autofix)
$(ESLINT) --max-warnings 0 --cache --fix .
$(ESLINT) --max-warnings 0 --cache --cache-location $(ESLINT_CACHE) --fix .

.PHONY: lint
lint: prettier eslint ## run Prettier & ESlint (autofix)
Expand Down
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ Images are available on [Docker Hub](https://hub.docker.com/r/smartlook/relay-pr
**All variables are optional, default values are listed in the table below**.

```bash
docker run --name="smartlook-relay-proxy" -d \
# can be also set with "--env-file"
-e LOGGER_LEVEL=<level> \
-e MANAGER_HOST=<host> \
-e WEB_SDK_WRITER_HOST=<host> \
-e ASSETS_PROXY_HOST=<host> \
-p <port>:8000 \ # internally runs on port 8000
smartlook/relay-proxy:latest # or smartlook/relay-proxy:<version>
docker run --name="smartlook-relay-proxy" \
-p <port>:8000 \ # runs on port 8000 by default
smartlook/relay-proxy:latest
```

2. Create a new subdomain (e.g. `sl.yourdomain.com`) and point it to the container.
Expand Down Expand Up @@ -57,13 +52,15 @@ docker run --name="smartlook-relay-proxy" -d \

## Environment variables

| Name | Type | Default value | Description |
| --------------------- | -------- | --------------------------------------- | ------------------------------------------------------------------- |
| `LOGGER_LEVEL` | `string` | `info` | One of `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `silent` |
| `PROXY_PORT` | `number` | `8000` | Port which will the HTTP server listen on |
| `MANAGER_HOST` | `string` | `https://manager.eu.smartlook.cloud` | Smartlook Manager host |
| `WEB_SDK_WRITER_HOST` | `string` | `https://web-writer.eu.smartlook.cloud` | Smartlook Web Writer host |
| `ASSETS_PROXY_HOST` | `string` | `https://assets-proxy.smartlook.cloud` | Smartlook Assets Proxy host |
| Name | Type | Default value | Description |
| --------------------- | --------- | --------------------------------------- | -------------------------------------------------------------------------------------------------- |
| `LOGGER_LEVEL` | `string` | `info` | One of `trace`, `debug`, `info`, `warn`, `error`, `fatal`, `silent` |
| `PROXY_PORT` | `number` | `8000` | Port which will the HTTP server listen on |
| `MANAGER_HOST` | `string` | `https://manager.eu.smartlook.cloud` | Smartlook Manager host |
| `WEB_SDK_WRITER_HOST` | `string` | `https://web-writer.eu.smartlook.cloud` | Smartlook Web Writer host |
| `ASSETS_PROXY_HOST` | `string` | `https://assets-proxy.smartlook.cloud` | Smartlook Assets Proxy host |
| `LOG_REQUESTS` | `boolean` | `false` | Log all requests (useful for debugging) |
| `TRUST_PROXY` | `boolean` | `true` | See Fastify's [trustProxy](https://www.fastify.io/docs/latest/Reference/Server/#trustproxy) option |

## Regional data storage setup

Expand Down
6 changes: 4 additions & 2 deletions commitlint.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
const config = {
extends: ['@commitlint/config-conventional'],
}

module.exports = config
45 changes: 27 additions & 18 deletions lint-staged.config.cjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
const path = require('node:path')
const { ESLint } = require('eslint')

/**
* @param {string} file
*/
function toRelative(file) {
return path.relative(process.cwd(), file)
return path.relative(process.cwd(), file)
}

/**
* @param {string[]} files
*/
async function removeIgnoredFiles(files) {
const eslint = new ESLint()
const isIgnored = await Promise.all(
files.map((file) => eslint.isPathIgnored(file))
)
const filteredFiles = files.filter((_, i) => !isIgnored[i])
return filteredFiles
const eslint = new ESLint()
const isIgnored = await Promise.all(
files.map((file) => eslint.isPathIgnored(file))
)
const filteredFiles = files.filter((_, i) => !isIgnored[i])
return filteredFiles
}

module.exports = {
'*': async (files) => {
const filesToFormat = files.map(toRelative).join(' ')
/** @type {import('lint-staged').Config} */
const config = {
'*': async (files) => {
const filesToFormat = files.map(toRelative).join(' ')

return [`prettier --check ${filesToFormat}`]
},
'**/*.ts': async (files) => {
const filesToLint = (await removeIgnoredFiles(files))
.map(toRelative)
.join(' ')
return [`prettier --check ${filesToFormat}`]
},
'**/*.ts': async (files) => {
const filesToLint = (await removeIgnoredFiles(files))
.map(toRelative)
.join(' ')

return [`eslint --max-warnings=0 ${filesToLint}`]
},
return [`eslint --max-warnings=0 ${filesToLint}`]
},
}

module.exports = config
85 changes: 43 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
{
"name": "smartlook-relay-proxy",
"description": "Smartlook Relay Proxy",
"license": "MIT",
"version": "3.1.2",
"private": true,
"type": "module",
"exports": "./build/src/main.js",
"engines": {
"node": ">=18",
"pnpm": ">=8"
},
"dependencies": {
"fast-json-stringify": "5.7.0",
"pino": "8.11.0",
"undici": "5.21.0",
"zod": "3.21.4"
},
"devDependencies": {
"@commitlint/cli": "17.5.1",
"@commitlint/config-conventional": "17.4.4",
"@types/convict": "6.1.1",
"@types/node": "18.15.11",
"@types/supertest": "2.0.12",
"@typescript-eslint/eslint-plugin": "5.57.0",
"@typescript-eslint/parser": "5.57.0",
"@vitest/coverage-c8": "0.29.8",
"dotenv": "16.0.3",
"eslint": "8.37.0",
"eslint-config-prettier": "8.8.0",
"eslint-import-resolver-typescript": "3.5.4",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-prettier": "4.2.1",
"husky": "8.0.3",
"lint-staged": "13.2.0",
"pino-pretty": "10.0.0",
"prettier": "2.8.7",
"supertest": "6.3.3",
"tsc-watch": "6.0.0",
"tsx": "3.12.6",
"typescript": "5.0.3",
"vitest": "0.29.8"
}
"name": "smartlook-relay-proxy",
"description": "Smartlook Relay Proxy",
"license": "MIT",
"version": "3.1.2",
"private": true,
"type": "module",
"exports": "./build/src/main.js",
"engines": {
"node": ">=18",
"pnpm": ">=8"
},
"dependencies": {
"@fastify/http-proxy": "9.1.0",
"envey": "1.1.0",
"fastify": "4.17.0",
"gracy": "1.0.2",
"pino": "8.14.1",
"zod": "3.21.4"
},
"devDependencies": {
"@commitlint/cli": "17.6.3",
"@commitlint/config-conventional": "17.6.3",
"@types/eslint": "8.37.0",
"@types/lint-staged": "13.2.0",
"@types/node": "20.1.1",
"@types/prettier": "2.7.2",
"@typescript-eslint/eslint-plugin": "5.59.5",
"@typescript-eslint/parser": "5.59.5",
"@vitest/coverage-c8": "0.31.0",
"dotenv": "16.0.3",
"eslint": "8.40.0",
"eslint-config-prettier": "8.8.0",
"eslint-import-resolver-typescript": "3.5.5",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-prettier": "4.2.1",
"husky": "8.0.3",
"lint-staged": "13.2.2",
"pino-pretty": "10.0.0",
"prettier": "2.8.8",
"tsx": "3.12.7",
"typescript": "5.0.4",
"vitest": "0.31.0"
}
}
Loading

0 comments on commit ccdd079

Please sign in to comment.