Skip to content

Commit

Permalink
support canary events
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Nov 2, 2023
1 parent 1f64084 commit 177f426
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 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,16 @@ export interface WikimediaStreamOptions extends EventSourceInitDict {
* @default true
*/
autoStart?: boolean;
/**
* Enable listening to canary events. Canary events are sent multiple times
* an hour to ensure that streams are not broken. They are filtered by
* default to avoid "fake" events from being received by a client.
*
* @see https://w.wiki/7$2z EventStreams documentation on canary events
* @see https://w.wiki/7$2v Technical documentation on canary events
* @default false
*/
enableCanary?: boolean;
}

export type ErrorEvent = Event & { type: 'error', message: string | undefined };
Expand Down Expand Up @@ -581,11 +591,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.
Expand Down

0 comments on commit 177f426

Please sign in to comment.