mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-27 16:47:13 +09:00
Add SubscriptionFilters class
This commit is contained in:
1 file changed
+181
-64
@@ -8,6 +8,120 @@ import { logger } from "./logging";
|
|||||||
export type SubscriptionRequestHandler<T> = RequestHandler<SubscriptionRequest, Event<T> | null>;
|
export type SubscriptionRequestHandler<T> = RequestHandler<SubscriptionRequest, Event<T> | null>;
|
||||||
export type EventSubscriberCallback<T> = (event: T | null, synced: boolean) => void
|
export type EventSubscriberCallback<T> = (event: T | null, synced: boolean) => void
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a set of string subscription filters.
|
||||||
|
* When isAll() is true, all strings match this filter.
|
||||||
|
* When isEmpty() is true, no strings match this filter.
|
||||||
|
* Otherwise, literal string matching is performed against a set.
|
||||||
|
*/
|
||||||
|
export class SubscriptionFilters {
|
||||||
|
private constructor(
|
||||||
|
private _all: boolean,
|
||||||
|
private _filters: Set<string>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
static jsonSchema = Type.Array(Type.String());
|
||||||
|
|
||||||
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||||
|
return new this(false, new Set(json));
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return [...this._filters];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Creates a subscription filter accepting all strings */
|
||||||
|
static all() {
|
||||||
|
return new this(true, new Set());
|
||||||
|
}
|
||||||
|
|
||||||
|
isAll() {
|
||||||
|
return this._all;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Creates a subscription filter rejecting all strings */
|
||||||
|
static empty() {
|
||||||
|
return new this(false, new Set());
|
||||||
|
}
|
||||||
|
|
||||||
|
isEmpty() {
|
||||||
|
return !this._all && this._filters.size === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a subscription filter from undefined, string, or array of strings.
|
||||||
|
* Strings work as expected, undefined will produce a filter accepting all values.
|
||||||
|
* @param filters Shorthand representation
|
||||||
|
*/
|
||||||
|
static fromShorthand(filters?: string | string[]) {
|
||||||
|
if (filters === undefined) {
|
||||||
|
return this.all();
|
||||||
|
}
|
||||||
|
if (Array.isArray(filters)) {
|
||||||
|
return new this(false, new Set(filters));
|
||||||
|
}
|
||||||
|
return new this(false, new Set([filters]));
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
return `[SubscriptionFilters ${this._all ? "All" : String(this._filters)}]`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns true if this filter accepts this string */
|
||||||
|
accepts(value: string) {
|
||||||
|
return this._all || this._filters.has(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns true if there is overlap between two filters */
|
||||||
|
intersects(other: this) {
|
||||||
|
if (this._all) {
|
||||||
|
return !other.isEmpty();
|
||||||
|
}
|
||||||
|
if (other._all) {
|
||||||
|
return !this.isEmpty();
|
||||||
|
}
|
||||||
|
for (const filter of other._filters) {
|
||||||
|
if (this._filters.has(filter)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Add filters from a filter into this one. */
|
||||||
|
union(other: this) {
|
||||||
|
if (this._all) {
|
||||||
|
return; // Already all
|
||||||
|
}
|
||||||
|
if (other._all) {
|
||||||
|
this._all = true;
|
||||||
|
this._filters.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const filter of other._filters) {
|
||||||
|
this._filters.add(filter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove filters in a filter from this one.
|
||||||
|
* You cannot subtract filters from a filter accepting all.
|
||||||
|
* Subtracting a filter accepting all produces an empty filter.
|
||||||
|
*/
|
||||||
|
subtract(other: this) {
|
||||||
|
if (this._all) {
|
||||||
|
return; // Cannot subtract from all
|
||||||
|
}
|
||||||
|
if (other._all) {
|
||||||
|
this._all = false;
|
||||||
|
this._filters.clear();
|
||||||
|
}
|
||||||
|
for (const filter of other._filters) {
|
||||||
|
this._filters.delete(filter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A subscription request sent by a subscriber, this updates what events the subscriber will be sent
|
* A subscription request sent by a subscriber, this updates what events the subscriber will be sent
|
||||||
* The permission for this request copies the permission from the event being subscribed to
|
* The permission for this request copies the permission from the event being subscribed to
|
||||||
@@ -20,7 +134,7 @@ export class SubscriptionRequest {
|
|||||||
static dst = "controller" as const;
|
static dst = "controller" as const;
|
||||||
static permission(user: IUser, message: MessageRequest) {
|
static permission(user: IUser, message: MessageRequest) {
|
||||||
if (typeof message.data === "object" && message.data !== null) {
|
if (typeof message.data === "object" && message.data !== null) {
|
||||||
const data = message.data as Static<typeof SubscriptionRequest.jsonSchema>;
|
const data = message.data as Static<typeof this.jsonSchema>;
|
||||||
const entry = Link._eventsByName.get(data[0]);
|
const entry = Link._eventsByName.get(data[0]);
|
||||||
if (entry && entry.Event.permission) {
|
if (entry && entry.Event.permission) {
|
||||||
if (typeof entry.Event.permission === "string") {
|
if (typeof entry.Event.permission === "string") {
|
||||||
@@ -34,16 +148,37 @@ export class SubscriptionRequest {
|
|||||||
|
|
||||||
/** Indicates if updates have been sent */
|
/** Indicates if updates have been sent */
|
||||||
static Response = JsonBoolean;
|
static Response = JsonBoolean;
|
||||||
|
public filters: SubscriptionFilters;
|
||||||
|
|
||||||
|
/** @deprecated Use SubscriptionFilters instead */
|
||||||
|
constructor(
|
||||||
|
eventName: string,
|
||||||
|
subscribe: boolean,
|
||||||
|
lastRequestTimeMs: number | undefined,
|
||||||
|
filters?: string | string[],
|
||||||
|
);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
eventName: string,
|
||||||
|
subscribe: boolean,
|
||||||
|
lastRequestTimeMs: number | undefined,
|
||||||
|
filters?: SubscriptionFilters,
|
||||||
|
);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public eventName: string,
|
public eventName: string,
|
||||||
public subscribe: boolean,
|
public subscribe: boolean,
|
||||||
public lastRequestTimeMs: number = 0,
|
public lastRequestTimeMs: number = 0,
|
||||||
public filters: string | string[] | undefined = undefined,
|
filters: undefined | string | string[] | SubscriptionFilters,
|
||||||
) {
|
) {
|
||||||
if (!Link._eventsByName.has(eventName)) {
|
if (!Link._eventsByName.has(eventName)) {
|
||||||
throw new Error(`Unregistered Event class ${eventName}`);
|
throw new Error(`Unregistered Event class ${eventName}`);
|
||||||
}
|
}
|
||||||
|
if (filters instanceof SubscriptionFilters) { // Easier migration for plugins
|
||||||
|
this.filters = filters;
|
||||||
|
} else {
|
||||||
|
this.filters = SubscriptionFilters.fromShorthand(filters);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static jsonSchema = Type.Union([
|
static jsonSchema = Type.Union([
|
||||||
@@ -56,28 +191,32 @@ export class SubscriptionRequest {
|
|||||||
Type.String(),
|
Type.String(),
|
||||||
Type.Boolean(),
|
Type.Boolean(),
|
||||||
Type.Number(),
|
Type.Number(),
|
||||||
Type.Union([Type.String(), Type.Array(Type.String())]),
|
SubscriptionFilters.jsonSchema,
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
if (this.filters !== undefined) {
|
if (!this.filters.isAll()) {
|
||||||
return [this.eventName, this.subscribe, this.lastRequestTimeMs, this.filters];
|
return [this.eventName, this.subscribe, this.lastRequestTimeMs, this.filters.toJSON()];
|
||||||
}
|
}
|
||||||
return [this.eventName, this.subscribe, this.lastRequestTimeMs];
|
return [this.eventName, this.subscribe, this.lastRequestTimeMs];
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromJSON(json: Static<typeof SubscriptionRequest.jsonSchema>): SubscriptionRequest {
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
||||||
const [eventName, subscribe, lastRequestTimeMs, filters] = json;
|
const [eventName, subscribe, lastRequestTimeMs, filters] = json;
|
||||||
return new this(eventName, subscribe, lastRequestTimeMs, filters);
|
return new this(
|
||||||
|
eventName,
|
||||||
|
subscribe,
|
||||||
|
lastRequestTimeMs,
|
||||||
|
filters ? SubscriptionFilters.fromJSON(filters) : SubscriptionFilters.all()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Subscriber = {
|
type Subscriber = {
|
||||||
link: Link,
|
link: Link,
|
||||||
dst: Address,
|
dst: Address,
|
||||||
/** Optional filters this subscriber is interested in. Undefined means all events */
|
filters: SubscriptionFilters,
|
||||||
filters?: string[],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventData = {
|
type EventData = {
|
||||||
@@ -92,13 +231,6 @@ type EventData = {
|
|||||||
export class SubscriptionController {
|
export class SubscriptionController {
|
||||||
_events = new Map<string, EventData>();
|
_events = new Map<string, EventData>();
|
||||||
|
|
||||||
private static normalizeFilters(value?: string | string[]): string[] | undefined {
|
|
||||||
if (value === undefined) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return Array.isArray(value) ? value : [value];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow clients to subscribe to an event by telling the subscription controller to accept them
|
* Allow clients to subscribe to an event by telling the subscription controller to accept them
|
||||||
* Has an optional subscription update handler which is called when a client subscribes
|
* Has an optional subscription update handler which is called when a client subscribes
|
||||||
@@ -124,7 +256,7 @@ export class SubscriptionController {
|
|||||||
* Broadcast an event to all subscribers of that event
|
* Broadcast an event to all subscribers of that event
|
||||||
* @param event - Event to broadcast.
|
* @param event - Event to broadcast.
|
||||||
*/
|
*/
|
||||||
broadcast<T>(event: Event<T>, filter?: string | string[]) {
|
broadcast<T>(event: Event<T> & { filters?: string | string[] }, filters?: string | string[]) {
|
||||||
const entry = Link._eventsByClass.get(event.constructor);
|
const entry = Link._eventsByClass.get(event.constructor);
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
throw new Error(`Unregistered Event class ${event.constructor.name}`);
|
throw new Error(`Unregistered Event class ${event.constructor.name}`);
|
||||||
@@ -134,18 +266,13 @@ export class SubscriptionController {
|
|||||||
throw new Error(`Event ${entry.name} is not a registered as subscribable`);
|
throw new Error(`Event ${entry.name} is not a registered as subscribable`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const filtersToMatch = SubscriptionController.normalizeFilters(filter);
|
const broadcastFilters = SubscriptionFilters.fromShorthand(filters);
|
||||||
for (const [addressIndex, subscriber] of eventData.subscriptions) {
|
for (const [addressIndex, subscriber] of eventData.subscriptions) {
|
||||||
if ((subscriber.link.connector as WebSocketBaseConnector).closing) {
|
if ((subscriber.link.connector as WebSocketBaseConnector).closing) {
|
||||||
eventData.subscriptions.delete(addressIndex);
|
eventData.subscriptions.delete(addressIndex);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (subscriber.filters.intersects(broadcastFilters)) {
|
||||||
if (
|
|
||||||
filtersToMatch === undefined
|
|
||||||
|| subscriber.filters === undefined
|
|
||||||
|| subscriber.filters.some(f => filtersToMatch.includes(f))
|
|
||||||
) {
|
|
||||||
subscriber.link.sendTo(subscriber.dst, event);
|
subscriber.link.sendTo(subscriber.dst, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,58 +308,48 @@ export class SubscriptionController {
|
|||||||
/**
|
/**
|
||||||
* Handle incoming subscription requests on a link
|
* Handle incoming subscription requests on a link
|
||||||
* @param link - Link message was received on
|
* @param link - Link message was received on
|
||||||
* @param event - incomming event.
|
* @param request - incoming request.
|
||||||
* @param src - Source address of incomming request.
|
* @param src - Source address of incomming request.
|
||||||
* @param dst - destination address of incomming request.
|
* @param dst - destination address of incomming request.
|
||||||
*/
|
*/
|
||||||
async handleRequest(link: Link, event: SubscriptionRequest, src: Address, dst: Address) {
|
async handleRequest(link: Link, request: SubscriptionRequest, src: Address, dst: Address) {
|
||||||
if (!Link._eventsByName.has(event.eventName)) {
|
if (!Link._eventsByName.has(request.eventName)) {
|
||||||
throw new Error(`Event ${event.eventName} is not a registered event`);
|
throw new Error(`Event ${request.eventName} is not a registered event`);
|
||||||
}
|
}
|
||||||
const eventData = this._events.get(event.eventName);
|
const eventData = this._events.get(request.eventName);
|
||||||
if (!eventData) {
|
if (!eventData) {
|
||||||
throw new Error(`Event ${event.eventName} is not a registered as subscribable`);
|
throw new Error(`Event ${request.eventName} is not a registered as subscribable`);
|
||||||
}
|
}
|
||||||
const addressIndex = src.addressIndex();
|
const addressIndex = src.addressIndex();
|
||||||
const subscriber = eventData.subscriptions.get(addressIndex);
|
const subscriber = eventData.subscriptions.get(addressIndex);
|
||||||
if (event.subscribe === false) {
|
switch (request.subscribe) {
|
||||||
// Unsubscribe logic
|
case false:
|
||||||
if (!subscriber) {
|
if (!subscriber) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (event.filters === undefined || subscriber.filters === undefined) {
|
subscriber.filters.subtract(request.filters);
|
||||||
// Remove entire subscription for this address
|
if (subscriber.filters.isEmpty()) {
|
||||||
eventData.subscriptions.delete(addressIndex);
|
|
||||||
} else {
|
|
||||||
const filtersToRemove = Array.isArray(event.filters) ? event.filters : [event.filters];
|
|
||||||
subscriber.filters = subscriber.filters.filter(f => !filtersToRemove.includes(f));
|
|
||||||
if (subscriber.filters.length === 0) {
|
|
||||||
// No filters left, remove subscription
|
|
||||||
eventData.subscriptions.delete(addressIndex);
|
eventData.subscriptions.delete(addressIndex);
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
} else {
|
|
||||||
// Subscribe logic
|
case true:
|
||||||
const newFilters = SubscriptionController.normalizeFilters(event.filters);
|
if (!subscriber) {
|
||||||
if (!subscriber) {
|
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: request.filters });
|
||||||
eventData.subscriptions.set(addressIndex, { link: link, dst: src, filters: newFilters });
|
} else {
|
||||||
} else if (subscriber.filters === undefined || newFilters === undefined) {
|
subscriber.filters.union(request.filters);
|
||||||
// Either was already subscribed to all or new request wants all
|
}
|
||||||
subscriber.filters = undefined;
|
if (eventData.subscriptionUpdate) {
|
||||||
} else {
|
const eventReplay = await eventData.subscriptionUpdate(request, src, dst);
|
||||||
for (const f of newFilters) {
|
if (eventReplay) {
|
||||||
if (!subscriber.filters.includes(f)) {
|
link.sendTo(src, eventReplay);
|
||||||
subscriber.filters.push(f);
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
if (eventData.subscriptionUpdate) {
|
|
||||||
const eventReplay = await eventData.subscriptionUpdate(event, src, dst);
|
default:
|
||||||
if (eventReplay) {
|
throw new Error(`unreachable case: ${String(request.subscribe)}`);
|
||||||
link.sendTo(src, eventReplay);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user