Skip to content

Commit

Permalink
make DocumentHandler class static
Browse files Browse the repository at this point in the history
I prefer not to have to deal with more "undefined" things
  • Loading branch information
inetol committed Mar 12, 2024
1 parent ebd688b commit 4fe7672
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 72 deletions.
83 changes: 34 additions & 49 deletions src/classes/DocumentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,18 @@ import { ErrorHandler } from './ErrorHandler.ts';
import { Server } from './Server.ts';

export class DocumentHandler {
private VERSION: ServerEndpointVersion | undefined;
public static async access(params: Parameters['access'], version: ServerEndpointVersion) {
DocumentHandler.validateKey(params.key);

public setVersion(version: ServerEndpointVersion): this {
this.VERSION = version;
return this;
}

public async access(params: Parameters['access']) {
this.validateKey(params.key);

const file = await this.retrieveDocument(params.key);
const file = await DocumentHandler.retrieveDocument(params.key);
const document = await DocumentManager.read(file);

this.validateTimestamp(params.key, document.expirationTimestamp);
this.validatePassword(params.password, document.password);
DocumentHandler.validateTimestamp(params.key, document.expirationTimestamp);
DocumentHandler.validatePassword(params.password, document.password);

const data = new TextDecoder().decode(document.rawFileData);

switch (this.VERSION) {
switch (version) {
case ServerEndpointVersion.V1: {
return { key: params.key, data };
}
Expand All @@ -42,24 +35,20 @@ export class DocumentHandler {
expirationTimestamp: document.expirationTimestamp
};
}

default: {
return ErrorHandler.get(ErrorCode.crash);
}
}
}

public async edit(params: Parameters['edit']) {
this.validateKey(params.key);
public static async edit(params: Parameters['edit']) {
DocumentHandler.validateKey(params.key);

const file = await this.retrieveDocument(params.key);
const file = await DocumentHandler.retrieveDocument(params.key);
const document = await DocumentManager.read(file);

this.validateSecret(params.secret, document.secret);
DocumentHandler.validateSecret(params.secret, document.secret);

const buffer = Buffer.from(params.body as ArrayBuffer);

this.validateSizeBetweenLimits(buffer);
DocumentHandler.validateSizeBetweenLimits(buffer);

document.rawFileData = buffer;

Expand All @@ -70,24 +59,24 @@ export class DocumentHandler {
};
}

public async exists(params: Parameters['exists']) {
this.validateKey(params.key);
public static async exists(params: Parameters['exists']) {
DocumentHandler.validateKey(params.key);

return Bun.file(Server.DOCUMENT_PATH + params.key).exists();
}

public async publish(params: Parameters['publish']) {
this.validateSelectedKey(params.selectedKey);
this.validateSelectedKeyLength(params.selectedKeyLength);
this.validatePasswordLength(params.password);
public static async publish(params: Parameters['publish'], version: ServerEndpointVersion) {
DocumentHandler.validateSelectedKey(params.selectedKey);
DocumentHandler.validateSelectedKeyLength(params.selectedKeyLength);
DocumentHandler.validatePasswordLength(params.password);

const secret = params.selectedSecret || StringUtils.createSecret();

this.validateSecretLength(secret);
DocumentHandler.validateSecretLength(secret);

const bodyBuffer = Buffer.from(params.body as ArrayBuffer);

this.validateSizeBetweenLimits(bodyBuffer);
DocumentHandler.validateSizeBetweenLimits(bodyBuffer);

let lifetime = params.lifetime ?? Server.DOCUMENT_MAX_TIME;

Expand All @@ -112,7 +101,7 @@ export class DocumentHandler {

await DocumentManager.write(Server.DOCUMENT_PATH + key, document);

switch (this.VERSION) {
switch (version) {
case ServerEndpointVersion.V1: {
return { key, secret };
}
Expand All @@ -125,20 +114,16 @@ export class DocumentHandler {
expirationTimestamp: Number(expirationTimestamp ?? 0)
};
}

default: {
return ErrorHandler.get(ErrorCode.crash);
}
}
}

public async remove(params: Parameters['remove']) {
this.validateKey(params.key);
public static async remove(params: Parameters['remove']) {
DocumentHandler.validateKey(params.key);

const file = await this.retrieveDocument(params.key);
const file = await DocumentHandler.retrieveDocument(params.key);
const document = await DocumentManager.read(file);

this.validateSecret(params.secret, document.secret);
DocumentHandler.validateSecret(params.secret, document.secret);

return {
removed: await unlink(Server.DOCUMENT_PATH + params.key)
Expand All @@ -147,7 +132,7 @@ export class DocumentHandler {
};
}

private async retrieveDocument(key: string): Promise<BunFile> {
private static async retrieveDocument(key: string): Promise<BunFile> {
const file = Bun.file(Server.DOCUMENT_PATH + key);

if (!(await file.exists())) {
Expand All @@ -157,7 +142,7 @@ export class DocumentHandler {
return file;
}

private validateKey(key: string): void {
private static validateKey(key: string): void {
if (
!ValidatorUtils.isValidBase64URL(key) ||
!ValidatorUtils.isLengthWithinRange(
Expand All @@ -170,13 +155,13 @@ export class DocumentHandler {
}
}

private validateSecret(secret: string, documentSecret: string): void {
private static validateSecret(secret: string, documentSecret: string): void {
if (documentSecret && documentSecret !== secret) {
ErrorHandler.send(ErrorCode.documentInvalidSecret);
}
}

private validateSecretLength(secret: string): void {
private static validateSecretLength(secret: string): void {
if (
ValidatorUtils.isEmptyString(secret) ||
!ValidatorUtils.isLengthWithinRange(Bun.stringWidth(secret), 1, 255)
Expand All @@ -185,15 +170,15 @@ export class DocumentHandler {
}
}

private validatePassword(password: string | undefined, documentPassword: string | null | undefined): void {
private static validatePassword(password: string | undefined, documentPassword: string | null | undefined): void {
if (password) {
if (documentPassword && password !== documentPassword) {
ErrorHandler.send(ErrorCode.documentInvalidPassword);
}
}
}

private validatePasswordLength(password: string | undefined): void {
private static validatePasswordLength(password: string | undefined): void {
if (
password &&
(ValidatorUtils.isEmptyString(password) ||
Expand All @@ -203,21 +188,21 @@ export class DocumentHandler {
}
}

private validateTimestamp(key: string, timestamp: number): void {
private static validateTimestamp(key: string, timestamp: number): void {
if (timestamp && ValidatorUtils.isLengthWithinRange(timestamp, 0, Date.now())) {
unlink(Server.DOCUMENT_PATH + key);

ErrorHandler.send(ErrorCode.documentNotFound);
}
}

private validateSizeBetweenLimits(body: Buffer): void {
private static validateSizeBetweenLimits(body: Buffer): void {
if (!ValidatorUtils.isLengthWithinRange(body.length, 1, Server.DOCUMENT_MAX_LENGTH)) {
ErrorHandler.send(ErrorCode.documentInvalidLength);
}
}

private validateSelectedKey(key: string | undefined): void {
private static validateSelectedKey(key: string | undefined): void {
if (
key &&
(!ValidatorUtils.isValidBase64URL(key) ||
Expand All @@ -231,7 +216,7 @@ export class DocumentHandler {
}
}

private validateSelectedKeyLength(length: number | undefined): void {
private static validateSelectedKeyLength(length: number | undefined): void {
if (
length &&
ValidatorUtils.isLengthWithinRange(length, Server.DOCUMENT_KEY_LENGTH_MIN, Server.DOCUMENT_KEY_LENGTH_MAX)
Expand Down
6 changes: 0 additions & 6 deletions src/classes/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { RemoveV1 } from '../endpoints/RemoveV1.ts';
import { RemoveV2 } from '../endpoints/RemoveV2.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';
import { ServerEndpointVersion } from '../types/Server.ts';
import { DocumentHandler } from './DocumentHandler.ts';
import { ErrorHandler } from './ErrorHandler.ts';

export class Server {
Expand All @@ -33,7 +32,6 @@ export class Server {
public static readonly ZLIB_LEVEL = 6;

private readonly ELYSIA: Elysia = new Elysia({ precompile: true });
private readonly DOCUMENT_HANDLER: DocumentHandler = new DocumentHandler();

public constructor() {
Server.DOCS_ENABLED && this.initDocs();
Expand All @@ -48,10 +46,6 @@ export class Server {
return this.ELYSIA;
}

public get documentHandler(): DocumentHandler {
return this.DOCUMENT_HANDLER;
}

private initDocs(): void {
this.ELYSIA.use(
swagger({
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints/AccessV1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';
import { ServerEndpointVersion } from '../types/Server.ts';

Expand All @@ -8,7 +9,7 @@ export class AccessV1 extends AbstractEndpoint {
this.SERVER.elysia.get(
this.PREFIX.concat('/:key'),
async ({ params }) => {
return this.SERVER.documentHandler.setVersion(ServerEndpointVersion.V1).access({ key: params.key });
return DocumentHandler.access({ key: params.key }, ServerEndpointVersion.V1);
},
{
params: t.Object({
Expand Down
8 changes: 5 additions & 3 deletions src/endpoints/AccessV2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';
import { ServerEndpointVersion } from '../types/Server.ts';

Expand All @@ -8,9 +9,10 @@ export class AccessV2 extends AbstractEndpoint {
this.SERVER.elysia.get(
this.PREFIX.concat('/:key'),
async ({ query, headers, params }) => {
return this.SERVER.documentHandler
.setVersion(ServerEndpointVersion.V2)
.access({ key: params.key, password: headers.password || query.p });
return DocumentHandler.access(
{ key: params.key, password: headers.password || query.p },
ServerEndpointVersion.V2
);
},
{
params: t.Object({
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints/EditV2.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';

export class EditV2 extends AbstractEndpoint {
protected override run(): void {
this.SERVER.elysia.patch(
this.PREFIX.concat('/:key'),
async ({ headers, body, params }) => {
return this.SERVER.documentHandler.edit({
return DocumentHandler.edit({
key: params.key,
body: body,
secret: headers.secret
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints/ExistsV2.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';

export class ExistsV2 extends AbstractEndpoint {
protected override run(): void {
this.SERVER.elysia.get(
this.PREFIX.concat('/:key/exists'),
async ({ params }) => {
return this.SERVER.documentHandler.exists(params);
return DocumentHandler.exists(params);
},
{
params: t.Object({
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints/PublishV1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';
import { ServerEndpointVersion } from '../types/Server.ts';

Expand All @@ -8,7 +9,7 @@ export class PublishV1 extends AbstractEndpoint {
this.SERVER.elysia.post(
this.PREFIX,
async ({ body }) => {
return this.SERVER.documentHandler.setVersion(ServerEndpointVersion.V1).publish({ body });
return DocumentHandler.publish({ body }, ServerEndpointVersion.V1);
},
{
type: 'arrayBuffer',
Expand Down
20 changes: 12 additions & 8 deletions src/endpoints/PublishV2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';
import { Server } from '../classes/Server.ts';
import { ServerEndpointVersion } from '../types/Server.ts';
Expand All @@ -9,14 +10,17 @@ export class PublishV2 extends AbstractEndpoint {
this.SERVER.elysia.post(
this.PREFIX,
async ({ headers, body }) => {
return this.SERVER.documentHandler.setVersion(ServerEndpointVersion.V2).publish({
body: body,
selectedKey: headers.key,
selectedKeyLength: headers.keyLength,
selectedSecret: headers.secret,
lifetime: headers.lifetime,
password: headers.password
});
return DocumentHandler.publish(
{
body: body,
selectedKey: headers.key,
selectedKeyLength: headers.keyLength,
selectedSecret: headers.secret,
lifetime: headers.lifetime,
password: headers.password
},
ServerEndpointVersion.V2
);
},
{
type: 'arrayBuffer',
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints/RemoveV1.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';

export class RemoveV1 extends AbstractEndpoint {
protected override run(): void {
this.SERVER.elysia.delete(
this.PREFIX.concat('/:key'),
async ({ headers, params }) => {
return this.SERVER.documentHandler.remove({
return DocumentHandler.remove({
key: params.key,
secret: headers.secret
});
Expand Down
3 changes: 2 additions & 1 deletion src/endpoints/RemoveV2.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { t } from 'elysia';
import { AbstractEndpoint } from '../classes/AbstractEndpoint.ts';
import { DocumentHandler } from '../classes/DocumentHandler.ts';
import { ErrorHandler } from '../classes/ErrorHandler.ts';

export class RemoveV2 extends AbstractEndpoint {
protected override run(): void {
this.SERVER.elysia.delete(
this.PREFIX.concat('/:key'),
async ({ headers, params }) => {
return this.SERVER.documentHandler.remove({
return DocumentHandler.remove({
key: params.key,
secret: headers.secret
});
Expand Down

0 comments on commit 4fe7672

Please sign in to comment.