Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document revamp & server-side encryption #86

Merged
merged 7 commits into from
Apr 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix lint?
tnfAngel committed Apr 13, 2024
commit 7f2ee8dbb378365c0b33f8eaf8cf29e2a1bf7953
24 changes: 21 additions & 3 deletions src/classes/DocumentHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { unlink } from 'node:fs/promises';
import type { BunFile } from 'bun';
import { decode, encode } from 'cbor-x';
import type { DocumentV1, Parameters } from '../types/DocumentHandler.ts';
import type { DocumentV1, Parameters, ResponsesV1, ResponsesV2 } from '../types/DocumentHandler.ts';
import { ErrorCode } from '../types/ErrorHandler.ts';
import { ServerEndpointVersion } from '../types/Server.ts';
import { CryptoUtils } from '../utils/CryptoUtils.ts';
@@ -10,6 +10,10 @@ import { ValidatorUtils } from '../utils/ValidatorUtils.ts';
import { ErrorHandler } from './ErrorHandler.ts';
import { Server } from './Server.ts';

type ResponseByVersion<V extends ServerEndpointVersion> = V extends ServerEndpointVersion.V1
? ResponsesV1
: ResponsesV2;

export class DocumentHandler {
public static async accessRaw(params: Parameters['access']) {
DocumentHandler.validateKey(params.key);
@@ -31,7 +35,10 @@ export class DocumentHandler {
return new TextDecoder().decode(data);
}

public static async access(params: Parameters['access'], version: ServerEndpointVersion) {
public static async access<EndpointVersion extends ServerEndpointVersion>(
params: Parameters['access'],
version: EndpointVersion
): Promise<ResponseByVersion<EndpointVersion>['access']> {
DocumentHandler.validateKey(params.key);

const file = await DocumentHandler.retrieveDocument(params.key);
@@ -62,6 +69,10 @@ export class DocumentHandler {
expirationTimestamp: 0
};
}

default: {
throw new Error(`Unsupported version: ${version}`);
}
}
}

@@ -91,7 +102,10 @@ export class DocumentHandler {
return Bun.file(Server.DOCUMENT_PATH + params.key).exists();
}

public static async publish(params: Parameters['publish'], version: ServerEndpointVersion) {
public static async publish<EndpointVersion extends ServerEndpointVersion>(
params: Parameters['publish'],
version: EndpointVersion
): Promise<ResponseByVersion<EndpointVersion>['publish']> {
DocumentHandler.validateSelectedKey(params.selectedKey);
DocumentHandler.validateSelectedKeyLength(params.selectedKeyLength);
DocumentHandler.validatePasswordLength(params.password);
@@ -134,6 +148,10 @@ export class DocumentHandler {
expirationTimestamp: 0
};
}

default: {
throw new Error(`Unsupported version: ${version}`);
}
}
}

22 changes: 21 additions & 1 deletion src/types/DocumentHandler.ts
Original file line number Diff line number Diff line change
@@ -35,4 +35,24 @@ type Parameters = {
};
};

export type { DocumentV1, Parameters };
// TODO: Type every response for consistency
type ResponsesV1 = {
access: {
key: string;
data: string;
};
publish: {
key: string;
secret: string;
};
};

type ResponsesV2 = {
access: ResponsesV1['access'] & {
url: string;
expirationTimestamp: number;
};
publish: ResponsesV1['publish'] & { url: string; expirationTimestamp: number };
};

export type { DocumentV1, Parameters, ResponsesV1, ResponsesV2 };