Adding strong checks on messages exchanged using generic-type-guard
This commit is contained in:
parent
83fa23a82d
commit
5178dff108
5 changed files with 39 additions and 14 deletions
8
front/src/Api/Events/ChatEvent.ts
Normal file
8
front/src/Api/Events/ChatEvent.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isChatEvent =
|
||||
new tg.IsInterface().withProperties({
|
||||
message: tg.isString,
|
||||
author: tg.isString,
|
||||
}).get();
|
||||
export type ChatEvent = tg.GuardedType<typeof isChatEvent>;
|
7
front/src/Api/Events/IframeEvent.ts
Normal file
7
front/src/Api/Events/IframeEvent.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
interface IframeEvent {
|
||||
type: string;
|
||||
data: unknown;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const isIframeEventWrapper = (event: any): event is IframeEvent => typeof event.type === 'string' && typeof event.data === 'object';
|
|
@ -1,9 +1,8 @@
|
|||
import {Subject} from "rxjs";
|
||||
import {ChatEvent, isChatEvent} from "./Events/ChatEvent";
|
||||
import {isIframeEventWrapper} from "./Events/IframeEvent";
|
||||
|
||||
|
||||
interface ChatEvent {
|
||||
message: string,
|
||||
author: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens to messages from iframes and turn those messages into easy to use observables.
|
||||
|
@ -21,12 +20,14 @@ class IframeListener {
|
|||
// event.source is window.opener
|
||||
// event.data is the data sent by the iframe
|
||||
|
||||
// FIXME: this is WAAAAAAAY too sloppy as "any" let's us put anything in the message.
|
||||
|
||||
if (event.data.type === 'chat') {
|
||||
this._chatStream.next(event.data.data);
|
||||
const payload = event.data;
|
||||
if (isIframeEventWrapper(payload)) {
|
||||
if (payload.type === 'chat' && isChatEvent(payload.data)) {
|
||||
this._chatStream.next(payload.data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue