Skip to content

Commit 8f323f4

Browse files
authored
Merge pull request danmactough#281 from danmactough/update-deps
2 parents fb2377e + 607b2cc commit 8f323f4

13 files changed

Lines changed: 2055 additions & 240 deletions

File tree

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,3 @@ insert_final_newline = true
99
charset = utf-8
1010
indent_style = space
1111
indent_size = 2
12-
13-
[Makefile]
14-
indent_style = tab

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lts/dubnium

.travis.yml

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1+
os: linux
2+
dist: bionic
13
language: node_js
2-
3-
env:
4-
- CXX=g++-4.8
5-
6-
addons:
7-
apt:
8-
sources:
9-
- ubuntu-toolchain-r-test
10-
packages:
11-
- g++-4.8
12-
13-
sudo: false
14-
15-
before_install:
16-
- $CXX --version
17-
184
node_js:
19-
- "4"
20-
- "6"
21-
- "8"
5+
- "10"
6+
- "12"
7+
- "14"

Makefile

Lines changed: 0 additions & 6 deletions
This file was deleted.

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,27 @@ npm install feedparser
2626

2727
This example is just to briefly demonstrate basic concepts.
2828

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

3232
```js
3333

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

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

40-
req.on('error', function (error) {
41-
// handle any request errors
42-
});
43-
44-
req.on('response', function (res) {
45-
var stream = this; // `this` is `req`, which is a stream
46-
47-
if (res.statusCode !== 200) {
48-
this.emit('error', new Error('Bad status code'));
40+
req.then(function (res) {
41+
if (res.status !== 200) {
42+
throw new Error('Bad status code');
4943
}
5044
else {
51-
stream.pipe(feedparser);
45+
// The response `body` -- res.body -- is a stream
46+
res.body.pipe(feedparser);
5247
}
48+
}, function (err) {
49+
// handle any request errors
5350
});
5451

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

203200
(The MIT License)
204201

205-
Copyright (c) 2011-2018 Dan MacTough and contributors
202+
Copyright (c) 2011-2020 Dan MacTough and contributors
206203

207204
Permission is hereby granted, free of charge, to any person obtaining a copy of
208205
this software and associated documentation files (the 'Software'), to deal in

examples/complete.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Tip
3+
* ====
4+
* - Set `user-agent` and `accept` headers when sending requests. Some services will not respond as expected without them.
5+
*/
6+
7+
var fetch = require('node-fetch')
8+
, FeedParser = require(__dirname+'/..')
9+
, iconv = require('iconv-lite');
10+
11+
function get(feed) {
12+
// Get a response stream
13+
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) {
14+
15+
// Setup feedparser stream
16+
var feedparser = new FeedParser();
17+
feedparser.on('error', done);
18+
feedparser.on('end', done);
19+
feedparser.on('readable', function() {
20+
var post;
21+
while (post = this.read()) {
22+
console.log(JSON.stringify(post, ' ', 4));
23+
}
24+
});
25+
26+
// Handle our response and pipe it to feedparser
27+
if (res.status != 200) throw new Error('Bad status code');
28+
var charset = getParams(res.headers.get('content-type') || '').charset;
29+
var responseStream = res.body;
30+
responseStream = maybeTranslate(responseStream, charset);
31+
// And boom goes the dynamite
32+
responseStream.pipe(feedparser);
33+
34+
}).catch(done);
35+
}
36+
37+
function maybeTranslate (res, charset) {
38+
var iconvStream;
39+
// Decode using iconv-lite if its not utf8 already.
40+
if (!iconvStream && charset && !/utf-*8/i.test(charset)) {
41+
try {
42+
iconvStream = iconv.decodeStream(charset);
43+
console.log('Converting from charset %s to utf-8', charset);
44+
iconvStream.on('error', done);
45+
// If we're using iconvStream, stream will be the output of iconvStream
46+
// otherwise it will remain the output of request
47+
res = res.pipe(iconvStream);
48+
} catch(err) {
49+
res.emit('error', err);
50+
}
51+
}
52+
return res;
53+
}
54+
55+
function getParams(str) {
56+
var params = str.split(';').reduce(function (params, param) {
57+
var parts = param.split('=').map(function (part) { return part.trim(); });
58+
if (parts.length === 2) {
59+
params[parts[0]] = parts[1];
60+
}
61+
return params;
62+
}, {});
63+
return params;
64+
}
65+
66+
function done(err) {
67+
if (err) {
68+
console.log(err, err.stack);
69+
return process.exit(1);
70+
}
71+
server.close();
72+
process.exit();
73+
}
74+
75+
// Don't worry about this. It's just a localhost file server so you can be
76+
// certain the "remote" feed is available when you run this example.
77+
var server = require('http').createServer(function (req, res) {
78+
var stream = require('fs').createReadStream(require('path').resolve(__dirname, '../test/feeds' + req.url));
79+
res.setHeader('Content-Type', 'text/xml; charset=Windows-1251');
80+
res.setHeader('Content-Encoding', 'gzip');
81+
stream.pipe(res);
82+
});
83+
server.listen(0, function () {
84+
get('http://localhost:' + this.address().port + '/compressed.xml');
85+
});

examples/compressed.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

examples/iconv.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

lib/feedparser/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ FeedParser.prototype.handleCloseTag = function (el){
345345
} else {
346346
stdEl = node['#local'] || node['#name'];
347347
}
348-
if (!this.stack[0].hasOwnProperty(stdEl)) {
348+
if (!Object.prototype.hasOwnProperty.call(this.stack[0], stdEl)) {
349349
this.stack[0][stdEl] = n;
350350
} else if (this.stack[0][stdEl] instanceof Array) {
351351
this.stack[0][stdEl].push(n);

0 commit comments

Comments
 (0)