Skip to content

Commit

Permalink
Sync from frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
inetol committed Oct 20, 2024
1 parent 879fe26 commit 4d2c590
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 115 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## SERVER:
# Set log verbosity [3]:integer
# (0=none <- 1=error <- 2=warn <- 3=info <- 4=debug)
#LOGLEVEL=3

# Port for the server [3000]:integer
#PORT=3000
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ updates:
schedule:
interval: weekly
labels:
- dependency:gha
- dependency:gha
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
container:
if: ${{ github.repository_owner == 'jspaste' && inputs.image-action != 'none' }}
name: Build container image
runs-on: ubuntu-24.04
runs-on: ubuntu-latest
env:
REGISTRY: ghcr.io

Expand Down Expand Up @@ -108,6 +108,6 @@ jobs:
name: Attest image
uses: actions/attest-build-provenance@1c608d11d69870c2092266b3f9a6f3abbf17002c # v1.4.3
with:
subject-name: ${{ env.REGISTRY }}/${{ steps.build-image.outputs.image }}
subject-name: "${{ env.REGISTRY }}/${{ steps.build-image.outputs.image }}"
subject-digest: ${{ steps.push-image.outputs.digest }}
push-to-registry: false
push-to-registry: false
39 changes: 2 additions & 37 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,9 @@ on:
permissions: read-all

jobs:
codeql:
name: CodeQL
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
include:
- language: javascript-typescript
build-mode: none

permissions:
security-events: write

steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
persist-credentials: false

- name: Setup CodeQL
uses: github/codeql-action/init@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}

- name: Run analysis
uses: github/codeql-action/analyze@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
with:
category: /language:${{matrix.language}}

scoreboard:
name: Scorecard
runs-on: ubuntu-24.04
runs-on: ubuntu-latest
permissions:
security-events: write
id-token: write
Expand All @@ -84,4 +49,4 @@ jobs:
- name: Upload to code-scanning
uses: github/codeql-action/upload-sarif@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
with:
sarif_file: scoreboard.sarif
sarif_file: scoreboard.sarif
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ permissions:
contents: read

jobs:
lint:
test:
name: Test
runs-on: ubuntu-24.04
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
Expand All @@ -50,4 +50,4 @@ jobs:
run: bun install --frozen-lockfile

- name: Run lint
run: bun run lint
run: bun run lint
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM docker.io/oven/bun:1-distroless
WORKDIR /frontend/

COPY --chown=nonroot ./dist ./
COPY --chown=nonroot ./dist/ ./
COPY --chown=nonroot ./LICENSE ./

LABEL org.opencontainers.image.url="https://jspaste.eu" \
Expand Down
5 changes: 3 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.3/schema.json",
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"files": {
"ignore": ["./dist/**", "*.spec.ts"],
"ignoreUnknown": true
Expand All @@ -26,7 +26,8 @@
"noStaticOnlyClass": "off"
},
"suspicious": {
"noAssignInExpressions": "off"
"noAssignInExpressions": "off",
"noConsoleLog": "error"
}
}
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion bunfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ auto = "disable"

[run]
bun = true
silent = true
silent = true
36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
"type": "module",
"scripts": {
"build": "bun run build:server",
"build:server": "rm -rf ./dist/ && bun --bun vite build && bun run script:standalone && bun run script:minify && bun run script:compress",
"build:server:safe": "rm -rf ./dist/ && bun --bun vite build && bun run script:standalone",
"build:server": "rm -rf ./dist/ && bun vite build && bun run script:standalone && bun run script:minify && bun run script:compress",
"clean:git:all": "bun run clean:git:untracked && bun run clean:git:gc && bun run clean:git:hooks",
"clean:git:all:force": "bun run clean:git:untracked:force && bun run clean:git:gc && bun run clean:git:hooks",
"clean:git:gc": "git gc --aggressive --prune",
"clean:git:hooks": "rm -rf ./.git/hooks/ && bun install -f",
"clean:git:untracked": "git clean -d -x -i",
"clean:git:untracked:force": "git clean -d -x -f",
"dev": "bun run start:dev",
"fix": "bun run fix:biome; bun run fix:package",
"fix:biome": "bun biome check --write",
Expand All @@ -19,29 +24,24 @@
"script:minify": "bun run ./scripts/minify.ts",
"script:standalone": "bun run ./scripts/standalone.ts",
"start": "bun run start:server",
"start:dev": "bun --bun vite",
"start:dev": "bun vite",
"start:rebuild": "bun run build:server && bun run start:server",
"start:rebuild:safe": "bun run build:server:safe && bun run start:server",
"start:server": "NODE_ENV=production bun run --cwd=./dist/ ./server/index.js"
},
"dependencies": {
"@codemirror/autocomplete": "~6.18.1",
"@codemirror/commands": "~6.7.0",
"@codemirror/lang-angular": "~0.1.3",
"@codemirror/lang-cpp": "~6.0.2",
"@codemirror/lang-css": "~6.3.0",
"@codemirror/lang-go": "~6.0.1",
"@codemirror/lang-html": "~6.4.9",
"@codemirror/lang-java": "~6.0.1",
"@codemirror/lang-javascript": "~6.2.2",
"@codemirror/lang-json": "~6.0.1",
"@codemirror/lang-liquid": "~6.2.1",
"@codemirror/lang-markdown": "~6.3.0",
"@codemirror/lang-php": "~6.0.1",
"@codemirror/lang-python": "~6.1.6",
"@codemirror/lang-rust": "~6.0.1",
"@codemirror/lang-sass": "~6.0.2",
"@codemirror/lang-vue": "~0.1.3",
"@codemirror/lang-xml": "~6.1.0",
"@codemirror/lang-yaml": "~6.1.1",
"@codemirror/language": "~6.10.3",
Expand All @@ -51,24 +51,24 @@
"@solid-primitives/scheduled": "~1.4.3",
"@solid-primitives/storage": "~4.2.1",
"@tabler/icons-solidjs": "~3.19.0",
"@types/bun": "~1.1.10",
"@types/bun": "~1.1.11",
"@types/node": "~22.7.7",
"@uiw/codemirror-extensions-hyper-link": "~4.23.5",
"@uiw/codemirror-themes": "~4.23.5",
"autoprefixer": "~10.4.20",
"chalk": "~5.3.0",
"daisyui": "~4.12.12",
"loglevel": "~1.9.2",
"daisyui": "~4.12.13",
"env-var": "~7.5.0",
"postcss": "~8.4.47",
"solid-js": "~1.9.2",
"tailwindcss": "~3.4.13",
"typescript": "~5.6.2",
"vike": "~0.4.198",
"tailwindcss": "~3.4.14",
"typescript": "~5.6.3",
"vike": "~0.4.199",
"vike-solid": "~0.7.6",
"vite": "~5.4.8"
"vite": "~5.4.9"
},
"devDependencies": {
"@biomejs/biome": "~1.9.3",
"lefthook": "~1.7.18",
"@biomejs/biome": "~1.9.4",
"lefthook": "~1.7.22",
"sort-package-json": "~2.10.1"
},
"trustedDependencies": [
Expand Down
4 changes: 2 additions & 2 deletions scripts/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import esbuild from 'esbuild';
const root = process.cwd();
const serverOutDir = './dist/server/';
const serverOutDirAbs = join(root, serverOutDir);
const serverEntrypoint = ['./src/server/index.ts'];
const serverEntrypoint = ['./src/server.ts'];

const buildStandalone = async () => {
const result = await esbuild.build({
Expand All @@ -19,7 +19,7 @@ const buildStandalone = async () => {
external: ['bun'],
entryPoints: serverEntrypoint,
sourcemap: false,
outdir: serverOutDirAbs,
outfile: `${serverOutDirAbs}index.js`,
splitting: false,
allowOverwrite: true,
metafile: true,
Expand Down
18 changes: 11 additions & 7 deletions src/server/index.ts → src/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { logger } from '@x-util/logger';
import { env, serve } from 'bun';
import { LogLevels, logger } from '@x-util/logger.ts';
import { serve } from 'bun';
import { get as envvar } from 'env-var';
import { renderPage } from 'vike/server';

process.on('SIGTERM', () => frontend.stop());
process.on('SIGTERM', async () => await frontend.stop());

logger.set(Number.parseInt(env.LOGLEVEL as string, 10));
export const env = {
port: envvar('PORT').default(3000).asPortNumber(),
logLevel: envvar('LOGLEVEL').default(LogLevels.info).asIntPositive()
} as const;

logger.set(env.logLevel);

const encodings = {
br: '.br',
Expand All @@ -17,8 +23,6 @@ const encodingsSortedKeys = Object.keys(encodings) as (keyof typeof encodings)[]
const encodingCompatibleMIME =
/^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;

const port = env.PORT || 3000;

// TODO: 103 Early Hints -> https://github.com/oven-sh/bun/issues/8690
const frontend = serve({
async fetch(req) {
Expand Down Expand Up @@ -97,7 +101,7 @@ const frontend = serve({
static: {
'/github': Response.redirect('https://github.com/jspaste', 301)
},
port: port
port: env.port
});

logger.info(`Listening on http://${frontend.hostname}:${frontend.port}`);
22 changes: 22 additions & 0 deletions src/utils/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { type ColorInput, color as bunColor } from 'bun';

const colorString =
(color: ColorInput) =>
(...text: unknown[]): string => {
return bunColor(color, 'ansi') + text.join(' ') + colors.reset;
};

export const colors = {
red: colorString('#ef5454'),
orange: colorString('#ef8354'),
yellow: colorString('#efd554'),
green: colorString('#70ef54'),
turquoise: colorString('#54efef'),
blue: colorString('#5954ef'),
purple: colorString('#a454ef'),
pink: colorString('#ef54d5'),
gray: colorString('#888'),
black: colorString('#000'),
white: colorString('#fff'),
reset: '\x1b[0m'
} as const;
4 changes: 0 additions & 4 deletions src/utils/langs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const langs = {
angular: () => import('@codemirror/lang-angular').then((lang) => lang.angular),
cpp: () => import('@codemirror/lang-cpp').then((lang) => lang.cpp),
css: () => import('@codemirror/lang-css').then((lang) => lang.css),
go: () => import('@codemirror/lang-go').then((lang) => lang.go),
Expand All @@ -8,13 +7,10 @@ export const langs = {
javascript: () => import('@codemirror/lang-javascript').then((lang) => lang.javascript),
typescript: () => import('@codemirror/lang-javascript').then((lang) => lang.javascript),
json: () => import('@codemirror/lang-json').then((lang) => lang.json),
liquid: () => import('@codemirror/lang-liquid').then((lang) => lang.liquid),
markdown: () => import('@codemirror/lang-markdown').then((lang) => lang.markdown),
php: () => import('@codemirror/lang-php').then((lang) => lang.php),
python: () => import('@codemirror/lang-python').then((lang) => lang.python),
rust: () => import('@codemirror/lang-rust').then((lang) => lang.rust),
sass: () => import('@codemirror/lang-sass').then((lang) => lang.sass),
vue: () => import('@codemirror/lang-vue').then((lang) => lang.vue),
xml: () => import('@codemirror/lang-xml').then((lang) => lang.xml),
yaml: () => import('@codemirror/lang-yaml').then((lang) => lang.yaml)
} as const;
Expand Down
65 changes: 29 additions & 36 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
import chalk from 'chalk';
import log from 'loglevel';
import { colors } from '@x-util/colors.ts';

export enum LogLevels {
none = 0,
error = 1,
warn = 2,
info = 3,
debug = 4
}

let logLevel: LogLevels = LogLevels.info;

export const logger = {
set: (level: number | undefined) => {
switch (level) {
case 0: {
log.setLevel('error');
break;
}
case 1: {
log.setLevel('warn');
break;
}
case 2: {
log.setLevel('info');
break;
}
case 3: {
log.setLevel('debug');
break;
}
default: {
log.setLevel('info');
}
}
set: (level: LogLevels): void => {
logLevel = level;
},

// biome-ignore lint/suspicious/noExplicitAny: We don't know the type
error: (...msg: any[]) => {
log.error(chalk.red('[ERROR]'), chalk.yellow('[SERVER]'), ...msg);
error: (...text: unknown[]): void => {
if (logLevel >= LogLevels.error) {
console.error(colors.gray('[FRONTEND]'), colors.red('[ERROR]'), text.join('\n'));
}
},

// biome-ignore lint/suspicious/noExplicitAny: We don't know the type
warn: (...msg: any[]) => {
log.warn(chalk.yellow('[WARN]'), chalk.yellow('[SERVER]'), ...msg);
warn: (...text: unknown[]): void => {
if (logLevel >= LogLevels.warn) {
console.warn(colors.gray('[FRONTEND]'), colors.yellow('[WARN]'), text.join('\n'));
}
},

// biome-ignore lint/suspicious/noExplicitAny: We don't know the type
info: (...msg: any[]) => {
log.info(chalk.blue('[INFO]'), chalk.yellow('[SERVER]'), ...msg);
info: (...text: unknown[]): void => {
if (logLevel >= LogLevels.info) {
console.info(colors.gray('[FRONTEND]'), colors.blue('[INFO]'), text.join('\n'));
}
},

// biome-ignore lint/suspicious/noExplicitAny: We don't know the type
debug: (...msg: any[]) => {
log.debug(chalk.gray('[DEBUG]'), chalk.yellow('[SERVER]'), ...msg);
debug: (...text: unknown[]): void => {
if (logLevel >= LogLevels.debug) {
console.debug(colors.gray('[FRONTEND]'), colors.gray('[DEBUG]'), text.join('\n'));
}
}
} as const;

0 comments on commit 4d2c590

Please sign in to comment.