Appearance
Guards
Guard는 IPC 요청의 유효성을 검증하는 파이프라인이다. 모든 Guard가 통과해야 핸들러가 실행된다.
파이프라인 흐름
요청 → Guard(canActivate) → Pipe(transform) → Handler → 응답
↓ (실패) ↓ (에러) ↓ (에러)
ExceptionHandler ExceptionHandler ExceptionHandler
↓ ↓ ↓
응답 응답 응답- 모든 Guard의
canActivate()를 순서대로 실행한다 - Guard가
false를 반환하면 첫 번째 ExceptionHandler의catch()에 에러를 전달한다 - Guard 통과 후 Pipe를 실행한다
- Pipe 통과 후 핸들러를 실행한다
IpcContext
Guard와 ExceptionHandler에 전달되는 컨텍스트 객체:
ts
interface IpcContext {
event: IpcMainInvokeEvent; // Electron IPC 이벤트
channel: string; // IPC 채널 이름
window: BrowserWindow; // 등록된 윈도우
args: unknown[]; // 핸들러 인자
}IpcGuard 인터페이스
ts
interface IpcGuard {
canActivate(ctx: IpcContext): boolean | Promise<boolean>;
}커스텀 Guard 작성
ts
import type { IpcGuard, IpcContext } from '@repo/electron-ipc';
class AuthGuard implements IpcGuard {
canActivate(ctx: IpcContext): boolean {
// 인증된 요청만 허용
const token = ctx.args[0];
return typeof token === 'string' && isValidToken(token);
}
}SenderGuard
패키지에 내장된 Guard. IPC 메시지가 등록된 윈도우의 webContents에서 온 것인지 검증한다.
ts
import { SenderGuard } from '@repo/electron-ipc';
createApp({
guards: [new SenderGuard()],
// ...
});내부 구현:
ts
class SenderGuard implements IpcGuard {
canActivate(ctx: IpcContext): boolean {
return ctx.event.sender === ctx.window.webContents;
}
}적용
Guard는 createApp()의 guards 배열로 전달한다. 배열 순서대로 실행되며, 하나라도 false면 즉시 중단한다.
ts
createApp({
guards: [new SenderGuard(), new AuthGuard()],
modules: [WindowModule, MyModule],
});다음 단계
- Pipes — Guard 이후 인자 변환과 검증
- Exception Handlers — Guard 실패와 핸들러 에러 처리
- API Reference — IpcGuard 전체 시그니처