Skip to content

Commit

Permalink
progress on abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Dec 3, 2023
1 parent 129730f commit 80ac5c3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
8 changes: 4 additions & 4 deletions cli/actions/drop.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inquirer from 'inquirer';
import { loader } from 'lib/loader';
import { displayWelcomeMessage, logDebug, logError, logInfo } from 'lib/log';
import { initPeer } from 'lib/peer';
import { dropMachine, initDropContext } from '@shared/lib/machines/drop';
import { DropEventType, MessageType } from '@shared/lib/constants';
import { AnyDropEvent, InitDropEvent } from '@shared/types/drop';
Expand Down Expand Up @@ -40,12 +39,13 @@ export const drop = async (input: string | undefined, options: DropOptions) => {
debug: logDebug,
},
file: {
encrypt: encryptFile,
encrypt: (...args) =>
encryptFile(args[0], args[1], args[2]).then((res) => res.data),
hash: hashFile,
},
initPeer,
cleanupSession,
apiUri: process.env.DEADDROP_API_URL!,
peerServerUri: process.env.PEER_SERVER_URL!,
});

ctx.message = input || options.input || null;
Expand All @@ -65,7 +65,7 @@ export const drop = async (input: string | undefined, options: DropOptions) => {
ctx.message = answer.input;
}

await stagePayload(ctx.message as string, 'raw');
await stagePayload(ctx.message as string, ctx.mode);

loader.start('Initializing drop session...');

Expand Down
5 changes: 3 additions & 2 deletions shared/handlers/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ import { DataConnection } from 'peerjs';
import { withMessageLock } from '../lib/messages';
import { deleteReq, post } from '../lib/fetch';
import { DROP_API_PATH } from '../config/paths';
import { createPeer } from '../lib/peer';

export const createDropHandlers = ({
ctx,
timers,
sendEvent,
logger,
file,
initPeer,
cleanupSession,
apiUri,
peerServerUri,
}: DropHandlerInputs) => {
const dropApiUrl = apiUri + DROP_API_PATH;

Expand Down Expand Up @@ -249,7 +250,7 @@ export const createDropHandlers = ({
const init = async () => {
ctx.keyPair = await generateKeyPair();

ctx.peer = await initPeer();
ctx.peer = await createPeer(peerServerUri);

ctx.peer.on('connection', onConnection);

Expand Down
18 changes: 18 additions & 0 deletions shared/types/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DataConnection } from 'peerjs';
import type Peer from 'peerjs';
import { DropMessageMeta } from './messages';

export type BaseContext = {
id: string | null;
Expand Down Expand Up @@ -35,3 +36,20 @@ export type DropPayload =
content: string;
type: string;
};

export type EncryptFile<InputType = string> = (
key: CryptoKey,
iv: string,
pathOrInput: InputType,
) => Promise<string>;

export type DecryptFile<InputType = string> = (
key: CryptoKey,
iv: string,
pathOrInput: InputType,
meta: DropMessageMeta,
) => Promise<string>;

export type HashFile<InputType = string> = (
pathOrInput: InputType,
) => Promise<string>;
12 changes: 4 additions & 8 deletions shared/types/drop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BaseContext } from './common';
import type { BaseContext, EncryptFile, HashFile } from './common';
import type { EventObject } from 'xstate/lib/types';
import type Peer from 'peerjs';
import type { DataConnection } from 'peerjs';
Expand Down Expand Up @@ -68,14 +68,10 @@ export type DropHandlerInputs = {
debug: (message: string) => void;
};
file: {
encrypt: (
key: CryptoKey,
iv: string,
pathOrInput: any,
) => Promise<string>;
hash: (pathOrInput: any) => Promise<string>;
encrypt: EncryptFile;
hash: HashFile;
};
initPeer: () => Promise<Peer>;
cleanupSession: (ctx: DropContext) => void;
apiUri: string;
peerServerUri: string;
};
11 changes: 3 additions & 8 deletions shared/types/grab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BaseContext } from './common';
import type { BaseContext, DecryptFile, HashFile } from './common';
import { GrabEventType, MessageType } from '../lib/constants';
import type { EventObject } from 'xstate/lib/types';
import type Peer from 'peerjs';
Expand Down Expand Up @@ -45,13 +45,8 @@ export type GrabHandlerInputs = {
debug: (message: string) => void;
};
file: {
decrypt: (
key: CryptoKey,
iv: string,
pathOrInput: any,
meta?: any,
) => Promise<string>;
hash: (pathOrInput: any) => Promise<string>;
decrypt: DecryptFile;
hash: HashFile;
};
initPeer: () => Promise<Peer>;
cleanupSession: (ctx: GrabContext) => void;
Expand Down
4 changes: 3 additions & 1 deletion shared/types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export interface HandshakeMessage extends BaseMessage {
input: string;
}

export type DropMessageMeta = { type: string; name: string };

export interface DropMessage extends BaseMessage {
type: MessageType.Payload;
mode: PayloadMode;
payload: string;
meta?: { type: string; name: string };
meta?: DropMessageMeta;
}

export interface VerifyMessage extends BaseMessage {
Expand Down

0 comments on commit 80ac5c3

Please sign in to comment.