-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic client to consume from the Wikimedia EventStreams API
- Loading branch information
1 parent
789a0a9
commit 617926a
Showing
4 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const EventSource = require('eventsource'); | ||
|
||
module.exports = function (mwn, bot) { | ||
|
||
class EventStream extends EventSource { | ||
|
||
/** | ||
* Access the Wikimedia EventStreams API | ||
* @see https://wikitech.wikimedia.org/wiki/Event_Platform/EventStreams | ||
* @param {string[]} streams | ||
* @param {Object} [config={}] | ||
* @config {string|Date} since | ||
* @config {Function} onopen | ||
* @config {Function} onerror | ||
*/ | ||
constructor(streams, config = {}) { | ||
if (Array.isArray(streams)) { | ||
streams = streams.join(','); | ||
} | ||
|
||
let since = config.since ? `?since=${new bot.date(config.since).format('YYYYMMDDHHmmss')}` : ''; | ||
super(`https://stream.wikimedia.org/v2/stream/${streams}${since}`, { | ||
headers: { | ||
'User-Agent': bot.userAgent | ||
} | ||
}); | ||
|
||
this.onopen = config.onopen || function () { | ||
mwn.log(`[S] Opened eventsource connection for ${streams} stream(s)`); | ||
}; | ||
this.onerror = config.onerror || function (err) { | ||
mwn.log(`[W] event source encountered error: ${err}`); | ||
}; | ||
|
||
} | ||
|
||
/** | ||
* Register a function to trigger for every message data from the source. | ||
* @param {Function} action | ||
* @param {Function | Object} [filter={}] | ||
*/ | ||
addListener(action, filter = {}) { | ||
let filterer = typeof filter === 'function' ? | ||
filter : | ||
function(data) { | ||
for (let key of Object.keys(filter)) { | ||
if (data[key] !== filter[key]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
this.onmessage = function(event) { | ||
let data = JSON.parse(event.data); | ||
if (!filterer(data)) { | ||
return; | ||
} | ||
action(data); | ||
}; | ||
} | ||
|
||
/** | ||
* Access the recentchange EventStreams API | ||
* @see https://wikitech.wikimedia.org/wiki/Event_Platform/EventStreams | ||
* @param {{wiki: string, type: ("edit"|"log"|"new"|"categorize"), title: string, namespace: number, | ||
* user: string, bot: boolean, minor: boolean} | Function} filter | ||
* @param {Function} action | ||
*/ | ||
static recentchange(filter, action) { | ||
let stream = new EventStream('recentchange'); | ||
stream.addListener(filter, action); | ||
return stream; | ||
} | ||
|
||
} | ||
|
||
return EventStream; | ||
|
||
}; |