Skip to content

Commit

Permalink
fix: adjust conditions, cleanup mocks, fix client init
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Jan 26, 2025
1 parent cccfdc2 commit 1c5e5d4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
34 changes: 21 additions & 13 deletions shared/handlers/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
} from '../types/messages';
import { DataConnection } from 'peerjs';
import { withMessageLock } from '../lib/messages';
import { DROP_API_PATH } from '../config/paths';
import { createClient } from '../client';

export const createDropHandlers = <
Expand All @@ -42,8 +41,7 @@ export const createDropHandlers = <
apiUri = '',
initPeer,
}: DropHandlerInputs<FileType>) => {
const dropApiUrl = apiUri + DROP_API_PATH;
const client = createClient(dropApiUrl);
const client = createClient(apiUri);
const timers = new Map<MessageType, NodeJS.Timeout>();

const clearTimer = (msgType: MessageType) => {
Expand All @@ -56,16 +54,18 @@ export const createDropHandlers = <
};

const cleanup = async () => {
const dropId = ctx.id!;

await client.drop
.$delete({ json: { id: dropId } })
.catch((err) =>
console.error(
`Failed to clear session data from cache (drop: ${dropId})`,
err,
),
);
if (ctx.id) {
const dropId = ctx.id;

await client.drop
.$delete({ json: { id: dropId } })
.catch((err) =>
console.error(
`Failed to clear session data from cache (drop: ${dropId})`,
err,
),
);
}

cleanupSession(ctx);
};
Expand Down Expand Up @@ -265,6 +265,14 @@ export const createDropHandlers = <
},
});

if (resp.status === 500) {
logger.error('Failed to create drop!');

cleanup();

return;
}

const { id, nonce }: InitDropResult = await resp.json();

ctx.id = id;
Expand Down
10 changes: 4 additions & 6 deletions shared/handlers/grab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
GrabMessageOrderMap,
MessageType,
} from '../lib/constants';
import { DROP_API_PATH } from '../config/paths';
import {
decryptRaw,
deriveKey,
Expand All @@ -40,8 +39,7 @@ export const createGrabHandlers = <
apiUri = '',
onRetryExceeded,
}: GrabHandlerInputs<FileType>) => {
const dropApiUrl = apiUri + DROP_API_PATH;
const client = createClient(dropApiUrl);
const client = createClient(apiUri);
const timers = new Map<MessageType, NodeJS.Timeout>();

const clearTimer = (msgType: MessageType) => {
Expand Down Expand Up @@ -200,16 +198,16 @@ export const createGrabHandlers = <
try {
const resp = await client.drop.$get({ query: { id: ctx.id! } });

const details: DropDetails = await resp.json();

if (!details) {
if (resp.status === 404) {
logger.error(
`Drop instance ${ctx.id} not found, closing connection...`,
);

throw new Error('Invalid drop ID provided');
}

const details: DropDetails = await resp.json();

logger.info(`Drop ${ctx.id} found!`);

ctx.dropperId = details.peerId;
Expand Down
7 changes: 3 additions & 4 deletions shared/tests/lib/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
encode,
encodeJson,
} from 'lib/data';
import { base64String } from 'tests/mocks/constants';
import { describe, expect, expectTypeOf, it } from 'vitest';

describe('data transform utilities', () => {
Expand Down Expand Up @@ -36,15 +37,13 @@ describe('data transform utilities', () => {
});

it('should transform base64 to and from ArrayBuffer', () => {
const input = 'aGVsbG8gdGhlcmU='; // 'hello there' in base64

const encodedInput = bufferFromBase64(input);
const encodedInput = bufferFromBase64(base64String);

expectTypeOf(encodedInput).toMatchTypeOf<ArrayBuffer>();
expect(encodedInput instanceof ArrayBuffer).toBeTruthy();

const decodedInput = base64FromBuffer(encodedInput);

expect(decodedInput).toStrictEqual(input);
expect(decodedInput).toStrictEqual(base64String);
});
});
2 changes: 2 additions & 0 deletions shared/tests/mocks/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const filePath = `./shared/tests/mocks/${filename}`;
export const fileType = 'application/json';
export const fileHash =
'0acb3809d0403bfa58626a36f0f0a64a6a14f302865913a951188d783ae81549';

export const base64String = 'aGVsbG8gdGhlcmU='; // 'hello there' in base64
19 changes: 19 additions & 0 deletions web/types/worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface Env {
DROP_STORE: KVNamespace;

// Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
PEER_SERVER: DurableObjectNamespace;

// variables
DAILY_DROP_LIMIT: number;

// secrets
CLERK_SECRET_KEY: string;
CLERK_PUBLISHABLE_KEY: string;

UPSTASH_REDIS_REST_URL: string;
UPSTASH_REDIS_REST_TOKEN: string;

TURSO_ORGANIZATION: string;
TURSO_PLATFORM_API_TOKEN: string;
}

0 comments on commit 1c5e5d4

Please sign in to comment.