Skip to content

Commit

Permalink
bugfixes (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
inetol committed Feb 3, 2024
1 parent eff7fe9 commit affe810
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
4 changes: 0 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ DOCS_PLAYGROUND_DOMAIN=jspaste.eu

# Port for documentation playground [443]:number
DOCS_PLAYGROUND_PORT=443

### Compression:
# Compression level for Zlib [6]:number<0-9>
ZLIB_LEVEL=6
2 changes: 1 addition & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ LABEL org.opencontainers.image.licenses="EUPL-1.2"
VOLUME /home/nonroot/documents
EXPOSE 4000/tcp

CMD ["src/index.ts"]
CMD ["./src/index.ts"]
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"@elysiajs/swagger": "^0.8.5",
"@protobuf-ts/plugin": "^2.9.3",
"@protobuf-ts/runtime": "^2.9.3",
"elysia": "^0.8.15"
"elysia": "^0.8.15",
"env-var": "^7.4.1"
},
"devDependencies": {
"@types/bun": "^1.0.4",
Expand Down
12 changes: 9 additions & 3 deletions src/classes/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ export class Server {
swagger({
documentation: {
servers: [
{ url: `${this.serverConfig.docs.playground.domain}:${this.serverConfig.docs.playground.port}` }
{
url: (this.serverConfig.docs.playground.https ? 'https://' : 'http://').concat(
this.serverConfig.docs.playground.domain,
':',
this.serverConfig.docs.playground.port.toString()
)
}
],
info: {
title: 'JSPaste documentation',
version: this.serverConfig.versions.map((v) => `v${v}`).join(', '),
version: this.serverConfig.versions.map((version) => `v${version}`).join(', '),
description:
'The JSPaste API documentation. Note that you can use /documents instead of /api/vX/documents to use the latest API version by default.',
license: {
Expand All @@ -65,7 +71,7 @@ export class Server {
syntaxHighlight: { activate: true, theme: 'monokai' }
},
path: this.serverConfig.docs.path,
exclude: [this.serverConfig.docs.path, `${this.serverConfig.docs.path}/json`, /^\/documents/]
exclude: [this.serverConfig.docs.path, this.serverConfig.docs.path.concat('/json'), /^\/documents/]
})
);
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/ServerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ServerOptions {
enabled: boolean;
path: string;
playground: {
https: boolean;
domain: string;
port: number;
};
Expand Down
16 changes: 10 additions & 6 deletions src/utils/ValidatorUtils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
export class ValidatorUtils {
public static isValidNumber(value: number): boolean {
return Number.isFinite(value);
public static isValidType<T>(value: unknown, type: new (...args: any[]) => T): value is T {
return value instanceof type;
}

public static isValidPrimitiveType<T>(value: unknown, type: string): value is T {
return typeof value === type;
}

public static isValidInteger(value: number): boolean {
return ValidatorUtils.isValidNumber(value) && Number.isInteger(value);
public static isValidNumber(value: number): boolean {
return Number.isFinite(value) && Number.isInteger(value);
}

public static isValidString(value: string): boolean {
return typeof value === 'string' && !!value.trim();
return ValidatorUtils.isValidPrimitiveType(value, 'string') && !!value.trim();
}

public static isValidArray<T>(value: T[], validator: (value: T) => boolean): boolean {
Expand Down Expand Up @@ -41,7 +45,7 @@ export class ValidatorUtils {
}

public static isStringLengthBetweenLimits(value: string, min: number, max: number): boolean {
return ValidatorUtils.isValidString(value) && value.length >= min && value.length <= max;
return ValidatorUtils.isValidString(value) && ValidatorUtils.isLengthBetweenLimits(value, min, max);
}

public static isStringArrayLengthBetweenLimits(min: number, max: number, values: string[]): boolean {
Expand Down
28 changes: 12 additions & 16 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import type { ServerOptions } from '../interfaces/ServerOptions.ts';
import type { ZlibCompressionOptions } from 'bun';
import type { JSPError } from '../classes/ErrorSender.ts';
import { ValidatorUtils } from './ValidatorUtils.ts';
import * as env from 'env-var';

// interface Bun.env
declare module 'bun' {
interface Env {
PORT: number;
DOCS_ENABLED: string;
DOCS_ENABLED: boolean;
DOCS_PATH: string;
DOCS_PLAYGROUND_HTTPS: string;
DOCS_PLAYGROUND_HTTPS: boolean;
DOCS_PLAYGROUND_DOMAIN: string;
DOCS_PLAYGROUND_PORT: number;
ZLIB_LEVEL: Range<0, 9>;
}
}

Expand All @@ -37,25 +36,22 @@ export enum JSPErrorCode {
documentInvalidSecretLength = 'jsp.document.invalid_secret_length'
}

export const serverConfig: ServerOptions = {
port: Bun.env.PORT || 4000,
export const serverConfig: Required<ServerOptions> = {
port: env.get('PORT').default(4000).asPortNumber(),
versions: [ServerVersion.v1, ServerVersion.v2],
docs: {
enabled: Bun.env.DOCS_ENABLED === 'true' || true,
path: Bun.env.DOCS_PATH || '/docs',
enabled: env.get('DOCS_ENABLED').asBoolStrict() ?? true,
path: env.get('DOCS_PATH').default('/docs').asString(),
playground: {
domain: ValidatorUtils.isValidDomain(Bun.env.DOCS_PLAYGROUND_DOMAIN)
? (Bun.env.DOCS_PLAYGROUND_HTTPS === 'true' ? 'https://' : 'http://').concat(
Bun.env.DOCS_PLAYGROUND_DOMAIN
)
: 'https://jspaste.eu',
port: Bun.env.DOCS_PLAYGROUND_PORT || 443
https: env.get('DOCS_PLAYGROUND_HTTPS').asBoolStrict() ?? true,
domain: env.get('DOCS_PLAYGROUND_DOMAIN').default('jspaste.eu').asString(),
port: env.get('DOCS_PLAYGROUND_PORT').default(443).asPortNumber()
}
}
} as const satisfies Required<ServerOptions>;
} as const;

export const zlibConfig: ZlibCompressionOptions = {
level: Bun.env.ZLIB_LEVEL || 6
level: 6
} as const;

// FIXME(inetol): Migrate to new config system
Expand Down

0 comments on commit affe810

Please sign in to comment.