Skip to content

Commit

Permalink
Merge pull request #281 from danmactough/update-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
danmactough authored May 2, 2020
2 parents fb2377e + 607b2cc commit 8f323f4
Show file tree
Hide file tree
Showing 13 changed files with 2,055 additions and 240 deletions.
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2

[Makefile]
indent_style = tab
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/dubnium
24 changes: 5 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
os: linux
dist: bionic
language: node_js

env:
- CXX=g++-4.8

addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8

sudo: false

before_install:
- $CXX --version

node_js:
- "4"
- "6"
- "8"
- "10"
- "12"
- "14"
6 changes: 0 additions & 6 deletions Makefile

This file was deleted.

25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,27 @@ npm install feedparser

This example is just to briefly demonstrate basic concepts.

**Please** also review the [compressed example](examples/compressed.js) for a
**Please** also review the [complete example](examples/complete.js) for a
thorough working example that is a suitable starting point for your app.

```js

var FeedParser = require('feedparser');
var request = require('request'); // for fetching the feed
var fetch = require('node-fetch'); // for fetching the feed

var req = request('http://somefeedurl.xml')
var req = fetch('http://somefeedurl.xml')
var feedparser = new FeedParser([options]);

req.on('error', function (error) {
// handle any request errors
});

req.on('response', function (res) {
var stream = this; // `this` is `req`, which is a stream

if (res.statusCode !== 200) {
this.emit('error', new Error('Bad status code'));
req.then(function (res) {
if (res.status !== 200) {
throw new Error('Bad status code');
}
else {
stream.pipe(feedparser);
// The response `body` -- res.body -- is a stream
res.body.pipe(feedparser);
}
}, function (err) {
// handle any request errors
});

feedparser.on('error', function (error) {
Expand Down Expand Up @@ -202,7 +199,7 @@ the original inspiration and a starting point.

(The MIT License)

Copyright (c) 2011-2018 Dan MacTough and contributors
Copyright (c) 2011-2020 Dan MacTough and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
Expand Down
85 changes: 85 additions & 0 deletions examples/complete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Tip
* ====
* - Set `user-agent` and `accept` headers when sending requests. Some services will not respond as expected without them.
*/

var fetch = require('node-fetch')
, FeedParser = require(__dirname+'/..')
, iconv = require('iconv-lite');

function get(feed) {
// Get a response stream
fetch(feed, { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36', 'accept': 'text/html,application/xhtml+xml' }).then(function (res) {

// Setup feedparser stream
var feedparser = new FeedParser();
feedparser.on('error', done);
feedparser.on('end', done);
feedparser.on('readable', function() {
var post;
while (post = this.read()) {
console.log(JSON.stringify(post, ' ', 4));
}
});

// Handle our response and pipe it to feedparser
if (res.status != 200) throw new Error('Bad status code');
var charset = getParams(res.headers.get('content-type') || '').charset;
var responseStream = res.body;
responseStream = maybeTranslate(responseStream, charset);
// And boom goes the dynamite
responseStream.pipe(feedparser);

}).catch(done);
}

function maybeTranslate (res, charset) {
var iconvStream;
// Decode using iconv-lite if its not utf8 already.
if (!iconvStream && charset && !/utf-*8/i.test(charset)) {
try {
iconvStream = iconv.decodeStream(charset);
console.log('Converting from charset %s to utf-8', charset);
iconvStream.on('error', done);
// If we're using iconvStream, stream will be the output of iconvStream
// otherwise it will remain the output of request
res = res.pipe(iconvStream);
} catch(err) {
res.emit('error', err);
}
}
return res;
}

function getParams(str) {
var params = str.split(';').reduce(function (params, param) {
var parts = param.split('=').map(function (part) { return part.trim(); });
if (parts.length === 2) {
params[parts[0]] = parts[1];
}
return params;
}, {});
return params;
}

function done(err) {
if (err) {
console.log(err, err.stack);
return process.exit(1);
}
server.close();
process.exit();
}

// Don't worry about this. It's just a localhost file server so you can be
// certain the "remote" feed is available when you run this example.
var server = require('http').createServer(function (req, res) {
var stream = require('fs').createReadStream(require('path').resolve(__dirname, '../test/feeds' + req.url));
res.setHeader('Content-Type', 'text/xml; charset=Windows-1251');
res.setHeader('Content-Encoding', 'gzip');
stream.pipe(res);
});
server.listen(0, function () {
get('http://localhost:' + this.address().port + '/compressed.xml');
});
96 changes: 0 additions & 96 deletions examples/compressed.js

This file was deleted.

89 changes: 0 additions & 89 deletions examples/iconv.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/feedparser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ FeedParser.prototype.handleCloseTag = function (el){
} else {
stdEl = node['#local'] || node['#name'];
}
if (!this.stack[0].hasOwnProperty(stdEl)) {
if (!Object.prototype.hasOwnProperty.call(this.stack[0], stdEl)) {
this.stack[0][stdEl] = n;
} else if (this.stack[0][stdEl] instanceof Array) {
this.stack[0][stdEl].push(n);
Expand Down
Loading

0 comments on commit 8f323f4

Please sign in to comment.