diff --git a/src/WikimediaStream.ts b/src/WikimediaStream.ts index 467d294..5149040 100644 --- a/src/WikimediaStream.ts +++ b/src/WikimediaStream.ts @@ -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'; @@ -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 }; @@ -581,11 +587,17 @@ export class WikimediaStream extends EventEmitter { } } ); + const skipCanary = !( options.enableCanary ?? false ); this.eventSource.addEventListener( 'message', async ( event: MessageEvent ) => { this._lastEventId = event.lastEventId; const data: WikimediaEventBase = JSON.parse( event.data ); + if ( skipCanary && ( 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.