Skip to content

Commit

Permalink
Merge dev to stable (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnfAngel authored Oct 26, 2024
2 parents 302e51b + 293bf94 commit f754f74
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 115 deletions.
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## SERVER:
# Set log verbosity [2]:integer
# (0=error <- 1=warn <- 2=info <- 3=debug)
#LOGLEVEL=2
# Set log verbosity [3]:integer
# (0=none <- 1=error <- 2=warn <- 3=info <- 4=debug)
#LOGLEVEL=3

# Port for the server [4000]:integer
#PORT=4000
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
artifacts: dist/*.tar.gz,dist/*.zip
makeLatest: true
prerelease: ${{ github.ref != 'refs/heads/stable' }}
generateReleaseNotes: true
generateReleaseNotes: ${{ github.ref == 'refs/heads/stable' }}

- if: ${{ inputs.artifact-action == 'build-release' }}
name: Attest artifact
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ jobs:
publish_results: true

- name: Upload to code-scanning
uses: github/codeql-action/upload-sarif@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
uses: github/codeql-action/upload-sarif@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13
with:
sarif_file: scoreboard.sarif
Binary file modified bun.lockb
Binary file not shown.
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"build": "bun run build:server",
"build:server": "rm -rf ./dist/ && bun build ./src/server.ts --outdir=./dist/ --target=bun --minify --sourcemap=inline",
"build:server": "bun build ./src/server.ts --outfile=./dist/server.js --target=bun --minify --sourcemap=inline",
"build:standalone": "bun build ./src/server.ts --outfile=./dist/backend --compile --minify --sourcemap=inline",
"build:standalone:darwin-arm64": "bun run build:standalone -- --target=bun-darwin-arm64",
"build:standalone:linux-amd64": "bun run build:standalone -- --target=bun-linux-x64-modern",
Expand All @@ -31,19 +31,17 @@
"start:server": "bun run ./dist/server.js"
},
"dependencies": {
"@hono/swagger-ui": "~0.4.1",
"@hono/zod-openapi": "~0.16.4",
"@scalar/hono-api-reference": "~0.5.155",
"@types/bun": "~1.1.11",
"@types/node": "~22.7.7",
"chalk": "~5.3.0",
"@types/bun": "~1.1.12",
"@types/node": "~22.8.1",
"env-var": "~7.5.0",
"hono": "~4.6.5",
"loglevel": "~1.9.2",
"hono": "~4.6.7",
"typescript": "~5.6.3"
},
"devDependencies": {
"@biomejs/biome": "~1.9.4",
"lefthook": "~1.7.22",
"lefthook": "~1.8.1",
"sort-package-json": "~2.10.1"
},
"trustedDependencies": [
Expand Down
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { env } from '@x-util/env.ts';

export const config = {
protocol: env.tls ? 'https://' : 'http://',
apiPath: '/api',
storagePath: 'storage/',
documentNameLengthMin: 2,
documentNameLengthMax: 32,
documentNameLengthDefault: 8
} as const;
2 changes: 1 addition & 1 deletion src/document/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deserialize, serialize } from 'bun:jsc';
import { config } from '../server.ts';
import { config } from '../config.ts';
import { errorHandler } from '../server/errorHandler.ts';
import type { Document } from '../types/Document.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';
Expand Down
2 changes: 1 addition & 1 deletion src/document/validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from '../server.ts';
import { config } from '../config.ts';
import { errorHandler } from '../server/errorHandler.ts';
import type { Document } from '../types/Document.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v1/access.route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { config } from '../../config.ts';
import { compression } from '../../document/compression.ts';
import { storage } from '../../document/storage.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v1/accessRaw.route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { config } from '../../config.ts';
import { compression } from '../../document/compression.ts';
import { storage } from '../../document/storage.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v1/remove.route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { unlink } from 'node:fs/promises';
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { config } from '../../config.ts';
import { storage } from '../../document/storage.ts';
import { validator } from '../../document/validator.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v2/access.route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { storage } from '@x-document/storage.ts';
import { config } from '../../config.ts';
import { compression } from '../../document/compression.ts';
import { validator } from '../../document/validator.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v2/accessRaw.route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { storage } from '@x-document/storage.ts';
import { config } from '../../config.ts';
import { compression } from '../../document/compression.ts';
import { validator } from '../../document/validator.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v2/edit.route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { storage } from '@x-document/storage.ts';
import { config } from '../../config.ts';
import { compression } from '../../document/compression.ts';
import { validator } from '../../document/validator.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { middleware } from '../../server/middleware.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v2/exists.route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { config } from '../../config.ts';
import { validator } from '../../document/validator.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v2/publish.route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { storage } from '@x-document/storage.ts';
import { StringUtils } from '@x-util/StringUtils.ts';
import { config } from '../../config.ts';
import { compression } from '../../document/compression.ts';
import { crypto } from '../../document/crypto.ts';
import { validator } from '../../document/validator.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { middleware } from '../../server/middleware.ts';
import { DocumentVersion } from '../../types/Document.ts';
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/v2/remove.route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { unlink } from 'node:fs/promises';
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { storage } from '@x-document/storage.ts';
import { config } from '../../config.ts';
import { validator } from '../../document/validator.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

Expand Down
44 changes: 0 additions & 44 deletions src/logger.ts

This file was deleted.

33 changes: 6 additions & 27 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import { env } from '@x-util/env.ts';
import { logger } from '@x-util/logger.ts';
import { serve } from 'bun';
import { get as envvar } from 'env-var';
import { cors } from 'hono/cors';
import { HTTPException } from 'hono/http-exception';
import { logger } from './logger.ts';
import { config } from './config.ts';
import { documentation } from './server/documentation.ts';
import { endpoints } from './server/endpoints.ts';
import { errorHandler } from './server/errorHandler.ts';
import { ErrorCode } from './types/ErrorHandler.ts';

export const env = {
port: envvar('PORT').default(4000).asPortNumber(),
logLevel: envvar('LOGLEVEL').default(2).asIntPositive(),
tls: envvar('TLS').asBoolStrict() ?? true,
documentMaxSize: envvar('DOCUMENT_MAXSIZE').default(1024).asIntPositive(),
docsEnabled: envvar('DOCS_ENABLED').asBoolStrict() ?? false,
docsPath: envvar('DOCS_PATH').default('/docs').asString()
} as const;

export const config = {
protocol: env.tls ? 'https://' : 'http://',
apiPath: '/api',
storagePath: 'storage/',
documentNameLengthMin: 2,
documentNameLengthMax: 32,
documentNameLengthDefault: 8
} as const;
process.on('SIGTERM', async () => await backend.stop());

logger.set(env.logLevel);

process.on('SIGTERM', async () => {
await backend.stop();
});

const instance = new OpenAPIHono().basePath(config.apiPath);

export const server = (): typeof instance => {
logger.set(env.logLevel);

instance.use('*', cors());

instance.onError((err) => {
Expand All @@ -63,6 +42,6 @@ export const server = (): typeof instance => {
};

const backend = serve({
port: env.port,
fetch: server().fetch
fetch: server().fetch,
port: env.port
});
14 changes: 10 additions & 4 deletions src/server/documentation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { swaggerUI } from '@hono/swagger-ui';
import type { OpenAPIHono } from '@hono/zod-openapi';
import { config, env } from '../server.ts';
import { middleware } from './middleware.ts';
import { env } from '@x-util/env.ts';
import { config } from '../config.ts';

export const documentation = (instance: OpenAPIHono): void => {
instance.doc31('/oas.json', (ctx) => ({
Expand All @@ -27,8 +28,13 @@ export const documentation = (instance: OpenAPIHono): void => {
url: 'https://paste.inetol.net',
description: 'Inetol Infrastructure instance'
}
]
].filter((server, index, self) => self.findIndex((x) => x.url === server.url) === index)
}));

instance.get(env.docsPath, middleware.scalar());
instance.get(
env.docsPath,
swaggerUI({
url: config.apiPath.concat('/oas.json')
})
);
};
2 changes: 1 addition & 1 deletion src/server/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { OpenAPIHono } from '@hono/zod-openapi';
import { v1 } from '@x-v1/index.ts';
import { v2 } from '@x-v2/index.ts';
import { config } from '../server.ts';
import { config } from '../config.ts';

export const endpoints = (instance: OpenAPIHono): void => {
instance.get('/documents/*', (ctx) => {
Expand Down
15 changes: 1 addition & 14 deletions src/server/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { apiReference } from '@scalar/hono-api-reference';
import { env } from '@x-util/env.ts';
import { bodyLimit as middlewareBodyLimit } from 'hono/body-limit';
import { config, env } from '../server.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';
import { errorHandler } from './errorHandler.ts';

Expand All @@ -12,17 +11,5 @@ export const middleware = {
throw errorHandler.send(ErrorCode.documentInvalidSize);
}
});
},

scalar: () => {
return apiReference({
pageTitle: 'JSPaste Documentation',
theme: 'saturn',
layout: 'classic',
isEditable: false,
spec: {
url: config.apiPath.concat('/oas.json')
}
});
}
} as const;
2 changes: 1 addition & 1 deletion src/utils/StringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { config } from '../server.ts';
import { config } from '../config.ts';
import type { Range } from '../types/Range.ts';
import { ValidatorUtils } from './ValidatorUtils.ts';

Expand Down
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;
11 changes: 11 additions & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LogLevels } from '@x-util/logger.ts';
import { get } from 'env-var';

export const env = {
port: get('PORT').default(4000).asPortNumber(),
logLevel: get('LOGLEVEL').default(LogLevels.info).asIntPositive(),
tls: get('TLS').asBoolStrict() ?? true,
documentMaxSize: get('DOCUMENT_MAXSIZE').default(1024).asIntPositive(),
docsEnabled: get('DOCS_ENABLED').asBoolStrict() ?? false,
docsPath: get('DOCS_PATH').default('/docs').asString()
} as const;
Loading

0 comments on commit f754f74

Please sign in to comment.