Skip to content

Commit

Permalink
Bugfixes (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
inetol authored Jul 26, 2024
1 parent f653200 commit 80dc46c
Show file tree
Hide file tree
Showing 24 changed files with 115 additions and 108 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#LOGLEVEL=2

# Port for the server [4000]:integer
# (Don't expose the server to the internet)
#PORT=4000

# Root path for the server (NOT IMPLEMENTED) [/api]:string
# (Everything will be served under this path)
#PATH=/api

# Is website served over HTTPS? [true]:boolean
#TLS=true

Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ FROM cgr.dev/chainguard/cc-dynamic:latest
WORKDIR /backend/

COPY --chown=nonroot --from=builder /build/dist/backend ./
COPY --chown=nonroot --from=builder /build/LICENSE ./

LABEL org.opencontainers.image.url="https://jspaste.eu" \
org.opencontainers.image.source="https://github.com/jspaste/backend" \
org.opencontainers.image.title="jspaste-backend" \
org.opencontainers.image.description="The backend for JSPaste, built with Bun and ElysiaJS" \
org.opencontainers.image.title="@jspaste/backend" \
org.opencontainers.image.description="The backend for JSPaste" \
org.opencontainers.image.documentation="https://docs.jspaste.eu" \
org.opencontainers.image.licenses="EUPL-1.2"

Expand Down
41 changes: 23 additions & 18 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"files": {
"ignore": ["**/node_modules/", "documents/", "dist/"],
"ignore": ["dist/**", "documents/**", "*.spec.ts"],
"ignoreUnknown": true
},
"formatter": {
Expand All @@ -12,6 +12,27 @@
"lineEnding": "lf",
"lineWidth": 120
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"noStaticOnlyClass": "off"
},
"style": {
"noParameterAssign": "off"
},
"suspicious": {
"noExplicitAny": "off",
"noConsoleLog": "error"
}
}
},
"css": {
"formatter": {
"enabled": true
}
},
"javascript": {
"formatter": {
"arrowParentheses": "always",
Expand All @@ -30,22 +51,6 @@
"enabled": true
}
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"style": {
"noParameterAssign": "off"
},
"complexity": {
"noStaticOnlyClass": "off"
},
"suspicious": {
"noExplicitAny": "off",
"noConsoleLog": "error"
}
}
},
"organizeImports": {
"enabled": true
}
Expand Down
Binary file modified bun.lockb
Binary file not shown.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
"lint": "bun run lint:biome && bun run lint:tsc",
"lint:biome": "bun biome lint",
"lint:tsc": "bun tsc --noEmit",
"start": "bun run build -- --sourcemap && bun ./dist/backend.js",
"start:dev": "bun ./src/index.ts"
"start": "bun run build && bun ./dist/backend.js",
"start:dev": "bun run ./src/index.ts"
},
"dependencies": {
"@hono/zod-openapi": "~0.14.2",
"@scalar/hono-api-reference": "~0.5.62",
"@types/bun": "~1.1.3",
"@hono/zod-openapi": "~0.15.1",
"@scalar/hono-api-reference": "~0.5.118",
"@types/bun": "~1.1.6",
"cbor-x": "~1.5.9",
"chalk": "~5.3.0",
"env-var": "~7.5.0",
"hono": "~4.4.5",
"hono": "~4.5.1",
"loglevel": "~1.9.1",
"typescript": "~5.4.5"
"typescript": "~5.5.4"
},
"devDependencies": {
"@biomejs/biome": "~1.8.1",
"lefthook": "~1.6.15",
"@biomejs/biome": "~1.8.3",
"lefthook": "~1.7.9",
"sort-package-json": "~2.10.0"
},
"trustedDependencies": [
Expand Down
2 changes: 1 addition & 1 deletion src/document/compression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type InputType, brotliCompress, brotliDecompress } from 'node:zlib';
import { errorHandler } from '../errorHandler.ts';
import { errorHandler } from '../server/errorHandler.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';

export const compression = {
Expand Down
10 changes: 5 additions & 5 deletions src/document/storage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { decode, encode } from 'cbor-x';
import { errorHandler } from '../errorHandler.ts';
import { config } from '../server.ts';
import type { DocumentV1 } from '../types/Document.ts';
import { errorHandler } from '../server/errorHandler.ts';
import type { Document } from '../types/Document.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';
import { validator } from './validator.ts';

export const storage = {
read: async (name: string): Promise<DocumentV1> => {
read: async (name: string): Promise<Document> => {
validator.validateName(name);

const file = Bun.file(config.storagePath + name);
Expand All @@ -18,7 +18,7 @@ export const storage = {
return decode(Buffer.from(await file.arrayBuffer()));
},

write: async (name: string, document: Omit<DocumentV1, 'version'>): Promise<void> => {
await Bun.write(config.storagePath + name, encode({ ...document, version: 1 }));
write: async (name: string, document: Document): Promise<void> => {
await Bun.write(config.storagePath + name, encode(document));
}
} as const;
8 changes: 4 additions & 4 deletions src/document/validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { errorHandler } from '../errorHandler.ts';
import { config } from '../server.ts';
import type { DocumentV1 } from '../types/Document.ts';
import { errorHandler } from '../server/errorHandler.ts';
import type { Document } from '../types/Document.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';
import { ValidatorUtils } from '../utils/ValidatorUtils.ts';
import { crypto } from './crypto.ts';
Expand Down Expand Up @@ -28,7 +28,7 @@ export const validator = {
}
},

validatePassword: (password: string, dataHash: DocumentV1['header']['passwordHash']): void => {
validatePassword: (password: string, dataHash: Document['header']['passwordHash']): void => {
if (dataHash && !crypto.compare(password, dataHash)) {
errorHandler.send(ErrorCode.documentInvalidPassword);
}
Expand All @@ -44,7 +44,7 @@ export const validator = {
}
},

validateSecret: (secret: string, secretHash: DocumentV1['header']['secretHash']): void => {
validateSecret: (secret: string, secretHash: Document['header']['secretHash']): void => {
if (!crypto.compare(secret, secretHash)) {
errorHandler.send(ErrorCode.documentInvalidSecret);
}
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/v1/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 { compression } from '../../document/compression.ts';
import { storage } from '../../document/storage.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

export const accessRoute = (endpoint: OpenAPIHono): void => {
Expand Down Expand Up @@ -65,7 +65,7 @@ export const accessRoute = (endpoint: OpenAPIHono): void => {
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/v1/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 { compression } from '../../document/compression.ts';
import { storage } from '../../document/storage.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

export const accessRawRoute = (endpoint: OpenAPIHono): void => {
Expand Down Expand Up @@ -55,7 +55,7 @@ export const accessRawRoute = (endpoint: OpenAPIHono): void => {
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
10 changes: 6 additions & 4 deletions src/endpoints/v1/publish.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { compression } from '../../document/compression.ts';
import { crypto } from '../../document/crypto.ts';
import { storage } from '../../document/storage.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { middleware } from '../../middleware.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { middleware } from '../../server/middleware.ts';
import { DocumentVersion } from '../../types/Document.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';
import { StringUtils } from '../../utils/StringUtils.ts';

Expand Down Expand Up @@ -64,14 +65,15 @@ export const publishRoute = (endpoint: OpenAPIHono): void => {
name: name,
secretHash: crypto.hash(secret) as string,
passwordHash: null
}
},
version: DocumentVersion.V1
});

return ctx.json({ key: name, secret: secret });
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
14 changes: 7 additions & 7 deletions src/endpoints/v1/remove.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { unlink } from 'node:fs/promises';
import { type OpenAPIHono, createRoute, z } from '@hono/zod-openapi';
import { storage } from '../../document/storage.ts';
import { validator } from '../../document/validator.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

export const removeRoute = (endpoint: OpenAPIHono): void => {
Expand Down Expand Up @@ -57,15 +57,15 @@ export const removeRoute = (endpoint: OpenAPIHono): void => {

validator.validateSecret(headers.secret, document.header.secretHash);

return ctx.json({
removed: await unlink(config.storagePath + params.name)
.then(() => true)
.catch(() => false)
});
const result = await unlink(config.storagePath + params.name)
.then(() => true)
.catch(() => false);

return ctx.json({ removed: result });
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/v2/access.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { compression } from '../../document/compression.ts';
import { crypto } from '../../document/crypto.ts';
import { storage } from '../../document/storage.ts';
import { validator } from '../../document/validator.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

export const accessRoute = (endpoint: OpenAPIHono): void => {
Expand Down Expand Up @@ -72,7 +72,7 @@ export const accessRoute = (endpoint: OpenAPIHono): void => {

if (document.header.passwordHash) {
if (!headers.password) {
throw errorHandler.send(ErrorCode.documentPasswordNeeded);
return errorHandler.send(ErrorCode.documentPasswordNeeded);
}

validator.validatePassword(headers.password, document.header.passwordHash);
Expand All @@ -92,7 +92,7 @@ export const accessRoute = (endpoint: OpenAPIHono): void => {
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/v2/accessRaw.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { compression } from '../../document/compression.ts';
import { crypto } from '../../document/crypto.ts';
import { storage } from '../../document/storage.ts';
import { validator } from '../../document/validator.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

export const accessRawRoute = (endpoint: OpenAPIHono): void => {
Expand Down Expand Up @@ -69,7 +69,7 @@ export const accessRawRoute = (endpoint: OpenAPIHono): void => {

if (document.header.passwordHash) {
if (!options.password) {
throw errorHandler.send(ErrorCode.documentPasswordNeeded);
return errorHandler.send(ErrorCode.documentPasswordNeeded);
}

validator.validatePassword(options.password, document.header.passwordHash);
Expand All @@ -84,7 +84,7 @@ export const accessRawRoute = (endpoint: OpenAPIHono): void => {
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
8 changes: 4 additions & 4 deletions src/endpoints/v2/edit.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { compression } from '../../document/compression.ts';
import { crypto } from '../../document/crypto.ts';
import { storage } from '../../document/storage.ts';
import { validator } from '../../document/validator.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { middleware } from '../../middleware.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';

export const editRoute = (endpoint: OpenAPIHono): void => {
Expand Down Expand Up @@ -76,7 +76,7 @@ export const editRoute = (endpoint: OpenAPIHono): void => {

if (document.header.passwordHash) {
if (!headers.password) {
throw errorHandler.send(ErrorCode.documentPasswordNeeded);
return errorHandler.send(ErrorCode.documentPasswordNeeded);
}

validator.validatePassword(headers.password, document.header.passwordHash);
Expand All @@ -95,7 +95,7 @@ export const editRoute = (endpoint: OpenAPIHono): void => {
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/v2/exists.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 { validator } from '../../document/validator.ts';
import { errorHandler, schema } from '../../errorHandler.ts';
import { config } from '../../server.ts';
import { errorHandler, schema } from '../../server/errorHandler.ts';
import { ErrorCode } from '../../types/ErrorHandler.ts';

export const existsRoute = (endpoint: OpenAPIHono): void => {
Expand Down Expand Up @@ -56,7 +56,7 @@ export const existsRoute = (endpoint: OpenAPIHono): void => {
},
(result) => {
if (!result.success) {
throw errorHandler.send(ErrorCode.validation);
return errorHandler.send(ErrorCode.validation);
}
}
);
Expand Down
Loading

0 comments on commit 80dc46c

Please sign in to comment.