Skip to content

Commit

Permalink
fix(platform pluv): fix events (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
pluvrt authored Oct 25, 2024
1 parent c6b6e8c commit 25292d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-trainers-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pluv/platform-pluv": patch
---

Fix event payload types to only contain serializable fields.
38 changes: 27 additions & 11 deletions packages/platform-pluv/src/PluvIO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GetInitialStorageFn, JWTEncodeParams, PluvIOListeners } from "@pluv/io";
import type { GetInitialStorageFn, JWTEncodeParams } from "@pluv/io";
import type { BaseUser, IOLike, InputZodLike } from "@pluv/types";
import { Hono } from "hono";
import { handle } from "hono/vercel";
Expand All @@ -15,9 +15,23 @@ interface PluvIOEndpoints {
createToken: string;
}

export type PluvIOConfig<TUser extends BaseUser> = Partial<
Pick<PluvIOListeners<PluvPlatform, PluvAuthorize<TUser>, {}, {}>, "onRoomDeleted">
> & {
type RoomDeletedMessageEventData = {
encodedState: string | null;
room: string;
};

type StorageUpdatedEventData<TUser extends BaseUser> = {
encodedState: string | null;
room: string;
user: TUser;
};

type PluvIOListeners<TUser extends BaseUser> = {
onRoomDeleted: (event: RoomDeletedMessageEventData) => void;
onStorageUpdated: (event: StorageUpdatedEventData<TUser>) => void;
};

export type PluvIOConfig<TUser extends BaseUser> = Partial<PluvIOListeners<TUser>> & {
/**
* @ignore
* @readonly
Expand All @@ -40,7 +54,7 @@ export class PluvIO<TUser extends BaseUser> implements IOLike<PluvAuthorize<TUse
private readonly _basePath: string;
private readonly _endpoints: PluvIOEndpoints;
private readonly _getInitialStorage?: GetInitialStorageFn<PluvPlatform>;
private readonly _listeners: Pick<PluvIOListeners<PluvPlatform, PluvAuthorize<TUser>, {}, {}>, "onRoomDeleted">;
private readonly _listeners: PluvIOListeners<TUser>;
private readonly _publicKey: string;
private readonly _secretKey: string;

Expand Down Expand Up @@ -91,7 +105,7 @@ export class PluvIO<TUser extends BaseUser> implements IOLike<PluvAuthorize<TUse
const room = data.room;
const encodedState = data.storage;

await Promise.resolve(this._listeners.onRoomDeleted({ context: {}, encodedState, room }));
await Promise.resolve(this._listeners.onRoomDeleted({ encodedState, room }));

return c.json({ data: { room } }, 200);
}
Expand All @@ -101,7 +115,8 @@ export class PluvIO<TUser extends BaseUser> implements IOLike<PluvAuthorize<TUse
});

constructor(options: PluvIOConfig<TUser>) {
const { _defs, authorize, basePath, onRoomDeleted, getInitialStorage, publicKey, secretKey } = options;
const { _defs, authorize, basePath, onRoomDeleted, onStorageUpdated, getInitialStorage, publicKey, secretKey } =
options;

this._authorize = {
required: true,
Expand All @@ -114,7 +129,10 @@ export class PluvIO<TUser extends BaseUser> implements IOLike<PluvAuthorize<TUse
..._defs?.endpoints,
};
this._getInitialStorage = getInitialStorage;
this._listeners = { onRoomDeleted: (event) => onRoomDeleted?.(event) };
this._listeners = {
onRoomDeleted: (event) => onRoomDeleted?.(event),
onStorageUpdated: (event) => onStorageUpdated?.(event),
};
this._publicKey = publicKey;
this._secretKey = secretKey;
}
Expand All @@ -140,9 +158,7 @@ export class PluvIO<TUser extends BaseUser> implements IOLike<PluvAuthorize<TUse

const token = await res.text().catch(() => null);

if (typeof token !== "string") {
throw new Error("Authorization failed");
}
if (typeof token !== "string") throw new Error("Authorization failed");

return token;
}
Expand Down

0 comments on commit 25292d4

Please sign in to comment.