rss-feed-emitter
Advanced tools
Comparing version 3.0.0 to 3.1.0
{ | ||
"name": "rss-feed-emitter", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Super RSS News Feed aggregator written in Node.js and ES6", | ||
@@ -45,3 +45,2 @@ "main": "src/FeedEmitter.js", | ||
"dependencies": { | ||
"atom-ui-reporter": "0.0.1", | ||
"feedparser": "1.1.4", | ||
@@ -59,3 +58,6 @@ "request": "^2.88.0" | ||
"node": ">=8.16.2" | ||
}, | ||
"release": { | ||
"branch": "master" | ||
} | ||
} |
@@ -68,5 +68,5 @@ <p align="center"> | ||
``` js | ||
let RssFeedEmitter = require('rss-feed-emitter'); | ||
let feeder = new RssFeedEmitter(); | ||
```js | ||
const RssFeedEmitter = require('rss-feed-emitter'); | ||
const feeder = new RssFeedEmitter(); | ||
``` | ||
@@ -76,4 +76,4 @@ | ||
``` js | ||
let feeder = new RssFeedEmitter({ userAgent: 'Your UA string' }); | ||
```js | ||
const feeder = new RssFeedEmitter({ userAgent: 'Your UA string' }); | ||
``` | ||
@@ -83,3 +83,3 @@ | ||
``` js | ||
```js | ||
feeder.add({ | ||
@@ -93,6 +93,24 @@ url: 'http://www.nintendolife.com/feeds/news', | ||
You can also add multiple at once by either providing an array of urls for the `url` field: | ||
```js | ||
feeder.add({ | ||
url: ['http://www.nintendolife.com/feeds/news', 'http://feeds.bbci.co.uk/news/rss.xml' ], | ||
refresh: 2000 | ||
}); | ||
``` | ||
or by passing multiple configs: | ||
```js | ||
feeder.add({ | ||
url: 'http://www.nintendolife.com/feeds/news', | ||
refresh: 2000 | ||
}, { | ||
url: 'http://feeds.bbci.co.uk/news/rss.xml', | ||
refresh: 5000 | ||
}); | ||
``` | ||
### Listening to new items | ||
``` js | ||
```js | ||
feeder.on('new-item', function(item) { | ||
@@ -103,9 +121,37 @@ console.log(item); | ||
you can also override the default `'new-item'` event name with a new value of your choice by providing the event name in the feed config. | ||
```js | ||
feeder.add({ | ||
url: 'http://www.nintendolife.com/feeds/news', | ||
refresh: 2000, | ||
eventName: 'nintendo' | ||
}); | ||
feeder.on('nintendo', function(item) { | ||
console.log(item); | ||
}); | ||
``` | ||
### Ignoring the first load of items | ||
```js | ||
const feeder = new RssFeedEmitter({ skipFirstLoad: true }); | ||
feeder.add({ | ||
url: 'http://www.nintendolife.com/feeds/news', | ||
refresh: 2000, | ||
eventName: 'nintendo' | ||
}); | ||
// this item will only be from the new items, not from old items. | ||
feeder.on('nintendo', function(item) { | ||
console.log(item); | ||
}); | ||
``` | ||
### Listing all feeds in the instance | ||
``` js | ||
feeder.list(); | ||
The list is now an ES6 getter to make the field a bit more plain to access. | ||
```js | ||
feeder.list; | ||
``` | ||
### Removing a single feed | ||
@@ -112,0 +158,0 @@ |
@@ -37,2 +37,3 @@ 'use strict'; | ||
userAgent: this.userAgent, | ||
eventName: this.eventName, | ||
} = data); | ||
@@ -61,2 +62,6 @@ | ||
} | ||
if (!this.eventName) { | ||
this.eventName = 'new-item'; | ||
} | ||
} | ||
@@ -95,3 +100,3 @@ | ||
// eslint-disable-next-line no-async-promise-executor | ||
return new Promise(async (resolve, reject) => { | ||
return new Promise(async (resolve) => { | ||
const items = []; | ||
@@ -105,3 +110,3 @@ const feedparser = new FeedParser(); | ||
feedparser.on('error', () => { | ||
reject(new FeedError(`Cannot parse ${this.url} XML`, 'invalid_feed', this.url)); | ||
this.handleError(new FeedError(`Cannot parse ${this.url} XML`, 'invalid_feed', this.url)); | ||
}); | ||
@@ -112,7 +117,7 @@ feedparser.on('end', () => { | ||
this.get(feedparser, reject); | ||
this.get(feedparser); | ||
}); | ||
} | ||
get(feedparser, reject) { | ||
get(feedparser) { | ||
request | ||
@@ -128,7 +133,7 @@ .get({ | ||
if (res.statusCode !== RESPONSE_CODES.OK) { | ||
reject(new FeedError(`This URL returned a ${res.statusCode} status code`, 'fetch_url_error', this.url)); | ||
this.handleError(new FeedError(`This URL returned a ${res.statusCode} status code`, 'fetch_url_error', this.url)); | ||
} | ||
}) | ||
.on('error', () => { | ||
reject(new FeedError(`Cannot connect to ${this.url}`, 'fetch_url_error', this.url)); | ||
this.handleError(new FeedError(`Cannot connect to ${this.url}`, 'fetch_url_error', this.url)); | ||
}) | ||
@@ -139,2 +144,10 @@ .pipe(feedparser) | ||
handleError(error) { | ||
if (this.handler) { | ||
this.handler.handle(error); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
destroy() { | ||
@@ -141,0 +154,0 @@ clearInterval(this.interval); |
@@ -29,4 +29,4 @@ 'use strict'; | ||
const checkUrl = (feed) => { | ||
if (!feed.url || typeof feed.url !== 'string') { | ||
throw new FeedError('Your configuration object should have an "url" key with a string value', 'type_error'); | ||
if (!feed.url || !(typeof feed.url === 'string' || Array.isArray(feed.url))) { | ||
throw new FeedError('Your configuration object should have an "url" key with a string or array value', 'type_error'); | ||
} | ||
@@ -66,3 +66,3 @@ }; | ||
*/ | ||
constructor(options = { userAgent: DEFAULT_UA }) { | ||
constructor(options = { userAgent: DEFAULT_UA, skipFirstLoad: false }) { | ||
super(); | ||
@@ -79,5 +79,19 @@ | ||
this.userAgent = options.userAgent; | ||
/** | ||
* Whether or not to skip the normal emit event on first load | ||
* @type {boolean} | ||
*/ | ||
this.skipFirstLoad = options.skipFirstLoad; | ||
} | ||
/** | ||
* UserFeedConfig typedef | ||
* @typedef {Object} UserFeedConfig | ||
* @property {(string|string[])} url Url string or string array. Cannot be null or empty | ||
* @property {Number} refresh Refresh cycle duration for the feed. | ||
*/ | ||
/** | ||
* ADD | ||
@@ -90,11 +104,29 @@ * The #add method is one of the main ones. Basically it | ||
* } | ||
* @param {Object} userFeedConfig user feed config | ||
* @param {UserFeedConfig[]} userFeedConfig user feed config | ||
* @returns {Feed[]} | ||
*/ | ||
add(...userFeedConfig) { | ||
if (userFeedConfig.length > 1) { | ||
userFeedConfig.forEach((f) => this.add(f)); | ||
return this.feedList; | ||
} | ||
add(userFeedConfig) { | ||
FeedEmitter.validateFeedObject(userFeedConfig, this.userAgent); | ||
const config = userFeedConfig[0]; | ||
const feed = new Feed(userFeedConfig); | ||
FeedEmitter.validateFeedObject(config, this.userAgent); | ||
if (Array.isArray(config.url)) { | ||
config.url.forEach((url) => { | ||
this.add({ | ||
...config, | ||
url, | ||
}); | ||
}); | ||
return this.feedList; | ||
} | ||
const feed = new Feed(config); | ||
this.addOrUpdateFeedList(feed); | ||
return this.feedList; | ||
@@ -163,3 +195,3 @@ } | ||
const feedManager = new FeedManager(this, feed); | ||
feedManager.getContent(); | ||
feedManager.getContent(true); | ||
return setInterval(feedManager.getContent.bind(feedManager), feed.refresh); | ||
@@ -166,0 +198,0 @@ } |
@@ -12,2 +12,6 @@ 'use strict'; | ||
this.feed = feed; | ||
this.feed.handler = { | ||
handle: this.onError.bind(this), | ||
}; | ||
} | ||
@@ -42,7 +46,10 @@ | ||
* @param {Object} data data to mutate | ||
* @param {boolean} firstload Whether or not this is the first laod | ||
*/ | ||
populateNewItemsInFeed(data) { | ||
populateNewItemsInFeed(data, firstload) { | ||
data.newItems.forEach((item) => { | ||
this.feed.addItem(item); | ||
this.instance.emit('new-item', item); | ||
if (!(firstload && this.instance.skipFirstLoad)) { | ||
this.instance.emit(this.feed.eventName, item); | ||
} | ||
}); | ||
@@ -55,3 +62,3 @@ } | ||
async getContent() { | ||
async getContent(firstload) { | ||
this.feed.fetchData() | ||
@@ -67,2 +74,5 @@ .then((items) => { | ||
this.populateNewItemsInFeed(data); | ||
if (firstload && !this.instance.skipFirstLoad) { | ||
this.instance.emit(`initial-load:${this.feed.url}`, { url: this.feed.url, items: this.feed.items }); | ||
} | ||
}) | ||
@@ -69,0 +79,0 @@ .catch(this.onError.bind(this)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2
380
176
0
18739
7
- Removedatom-ui-reporter@0.0.1
- Removedatom-ui-reporter@0.0.1(transitive)