Skip to content

Commit

Permalink
filter out canary events
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Oct 27, 2023
1 parent df5e84e commit b6d4f27
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/WikimediaStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import EventSource, { EventSourceInitDict } from 'eventsource';
import WikimediaEventBase from './streams/EventStream';
import WikimediaEventBase, { WikimediaEventMeta } from './streams/EventStream';
import type MediaWikiRevisionCreateEvent from './streams/MediaWikiRevisionCreateEvent';
import type MediaWikiPageDeleteEvent from './streams/MediaWikiPageDeleteEvent';
import type MediaWikiPageLinksChangeEvent from './streams/MediaWikiPageLinksChangeEvent';
Expand Down Expand Up @@ -206,6 +206,12 @@ export interface WikimediaStreamOptions extends EventSourceInitDict {
* @default true
*/
autoStart?: boolean;
/**
* Enable listening to canary events.
*
* @default false
*/
enableCanary?: boolean;
}

export type ErrorEvent = Event & { type: 'error', message: string | undefined };
Expand Down Expand Up @@ -581,11 +587,18 @@ export class WikimediaStream extends EventEmitter {
}
} );

const listenToCanary = this.startingOptions.enableCanary;
this.eventSource.addEventListener( 'message', async ( event: MessageEvent ) => {
this._lastEventId = event.lastEventId;

const data: WikimediaEventBase = JSON.parse( event.data );

if ( listenToCanary || ( data.meta as WikimediaEventMeta )?.domain === 'canary' ) {
// Block all incoming canary events unless requested by
// user.
return;
}

// Emit event.
this.emit( data.meta.stream, data, event );
// Emit event to aliases of event stream.
Expand Down

0 comments on commit b6d4f27

Please sign in to comment.