b3d90e1 · Redact ids outside the wall · 5 days ago

typescript32 lines · 795 B
1import type { Mapping } from "./mapping.ts";
2
3export class ScopeError extends Error {}
4
5export class ScopeGuard {
6 readonly scope: string;
7 private ids: Set<string>;
8
9 constructor(mapping: Mapping) {
10 this.scope = mapping.scope;
11 this.ids = new Set(mapping.channels.map((c) => c.id));
12 }
13
14 allow(channelId: string): boolean {
15 return this.ids.has(channelId);
16 }
17
18 redact(channelId: string): string {
19 return this.allow(channelId) ? channelId : "";
20 }
21
22 require(channelId: string, action: string): void {
23 if (!this.allow(channelId)) {
24 throw new ScopeError(`blocked ${action}: outside scope ${this.scope}`);
25 }
26 }
27
28 drop(_channelId: string, reason: string): void {
29 console.info(`dropped event source=<outside-scope> reason=${reason}`);
30 }
31}
32